hjkl_engine/lib.rs
1//! Vim-mode editor engine built on top of [`hjkl_buffer`].
2//!
3//! Exposes an [`Editor`] that is fully toolkit-agnostic. Covers the bulk
4//! of vim's normal / insert / visual / visual-line / visual-block modes,
5//! text-object operators, dot-repeat, and ex-command handling
6//! (`:s/foo/bar/g`, `:w`, `:q`, `:noh`, ...). Rendering goes through
7//! `hjkl_buffer::BufferView`; selection / gutter highlights are painted in
8//! the same single-pass as text. TUI/crossterm adapters live in the
9//! `hjkl-engine-tui` companion crate.
10//!
11//! Imported wholesale from sqeel-vim with full git history. The trait
12//! extraction (Selection / SelectionSet / View + Host sub-traits) lands
13//! progressively under [`crate::types`]. Pre-1.0 churn — the public surface
14//! may change in patch bumps. See [docs.rs](https://docs.rs/hjkl-engine) for
15//! the canonical API reference.
16//!
17//! The legacy public surface is intentionally narrow:
18//!
19//! - [`Editor`] — the editor widget.
20//! - [`VimMode`] — mode enum used by host apps.
21//! - [`ex::run`] / [`ex::ExEffect`] — drive ex-mode commands.
22
23pub mod abbrev;
24pub mod buf_helpers;
25mod buffer_impl;
26mod discipline;
27mod editor;
28pub mod input;
29pub mod keymap_motion;
30pub mod motions;
31pub mod policy;
32mod registers;
33pub mod rope_util;
34pub mod search;
35pub mod selection_shift;
36pub mod substitute;
37pub mod tag;
38pub mod types;
39mod viewport_math;
40
41pub use discipline::{DisciplineState, NoDiscipline};
42pub use editor::{
43 ChangeBank, CursorScrollTarget, Editor, GlobalMarks, LspIntent, MarkJump, SearchBank, Settings,
44 UndoGranularity,
45};
46pub use input::{Input, Key, decode_macro, from_planned as decode_planned_input};
47pub use registers::{Registers, Slot};
48pub use selection_shift::{Sel, shift_position, shift_sel};
49
50pub use buffer_impl::{BufferFoldProvider, BufferFoldProviderMut, SnapshotFoldProvider};
51pub use keymap_motion::MotionKind;
52pub use substitute::{
53 SubstError, SubstFlags, SubstituteCmd, SubstituteMatch, SubstituteOutcome,
54 apply_collected_matches, apply_substitute, collect_substitute_matches, parse_substitute,
55};
56pub use types::{
57 Attrs, BufferEdit, BufferId, Color, ContentEdit, Cursor, CursorShape, DefaultHost, Edit,
58 EditorSnapshot, EngineError, FoldOp, FoldProvider, Highlight, HighlightKind, Host,
59 Input as PlannedInput, Mode, Modifiers, MouseEvent, MouseKind, NoopFoldProvider, OptionValue,
60 Options, Pos, Query, RenderFrame, Search, Selection, SelectionKind, SelectionSet, SnapshotMode,
61 SpecialKey, Style, View, Viewport, WrapMode,
62};
63// The vim FSM itself now lives in `hjkl-vim` (#267). What stays here is the
64// engine-owned substrate it happens to use — abbreviations, the search prompt,
65// scroll/insert directions — plus the shared vocabulary types from
66// `hjkl-vim-types`, which both crates name and neither owns.
67pub use abbrev::{Abbrev, AbbrevTrigger};
68pub use search::SearchPrompt;
69pub use tag::matching_tag_pair;
70pub use types::{InsertDir, ScrollDir};
71
72pub use hjkl_vim_types::{
73 InsertEntry, InsertReason, InsertSession, LastChange, LastVisual, Motion, Operator, Pending,
74 RangeKind,
75};
76
77/// The FSM-internal mode discriminator used by `Editor::fsm_mode()` and
78/// `Editor::set_fsm_mode()`. Re-exported as `FsmMode` to avoid clashing with
79/// the `types::Mode` buffer-side enum that is already exported as `Mode`.
80///
81/// Used by `hjkl-vim::normal` and `hjkl-vim::dispatch_input` for mode
82/// comparisons.
83pub use hjkl_vim_types::Mode as FsmMode;
84
85// 0.0.32 dropped the `#[deprecated]` re-export aliases introduced at
86// 0.0.31 (`SpecBuffer`, `SpecBufferEdit`, `EditOp`, `PlannedViewport`).
87// Consumers must use the canonical names: `View`, `BufferEdit`,
88// `Edit`, `Viewport`.
89
90/// Coarse vim-mode a host app can display in its status line.
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
92pub enum VimMode {
93 #[default]
94 Normal,
95 Insert,
96 Visual,
97 VisualLine,
98 VisualBlock,
99}
100
101/// Discipline-agnostic coarse mode for app chrome (status badge, cursor
102/// shape). Unlike [`VimMode`] — which names vim-specific states — `CoarseMode`
103/// is a minimal projection: "are we inserting text, selecting, or idle?"
104///
105/// App chrome reads this instead of `VimMode` so it stays behind the engine's
106/// discipline seam ([`DisciplineState`]): the installed discipline (vim today)
107/// maps its own modes onto these variants via `DisciplineState::coarse_mode`.
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
109pub enum CoarseMode {
110 /// Idle / command-ready (vim Normal).
111 #[default]
112 Normal,
113 /// Text is being inserted at the caret (vim Insert).
114 Insert,
115 /// A character-wise selection is active (vim Visual).
116 Select,
117 /// A line-wise selection is active (vim VisualLine).
118 SelectLine,
119 /// A block / column selection is active (vim VisualBlock).
120 SelectBlock,
121}
122
123/// A read-only *view* layered over the real input [`VimMode`]. Unlike a vim
124/// mode (which decides how keystrokes are interpreted), a `ViewMode` only
125/// changes what the buffer presents — input is still interpreted as Normal.
126///
127/// `Blame` is the git-blame overlay: the editor is read-only and the host
128/// renders per-commit framing. It is only meaningful while the input mode is
129/// `Normal`; any transition to Insert/Visual/etc. drops it back to `Normal`
130/// (see [`Editor::is_blame`]). New read-only overlays (diff, conflict, …)
131/// become additional variants here without touching `VimMode`.
132#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
133pub enum ViewMode {
134 #[default]
135 Normal,
136 Blame,
137}