Skip to main content

hjkl_vim/
lib.rs

1pub mod cmd;
2pub mod count;
3#[cfg(debug_assertions)]
4mod curswant;
5pub mod descriptors;
6pub mod editor_ext;
7pub mod insert;
8pub mod motion;
9pub mod normal;
10pub mod operator;
11pub mod pending;
12pub mod search_prompt;
13mod step;
14pub mod vim;
15mod vim_state;
16
17pub use cmd::EngineCmd;
18pub use count::CountAccumulator;
19pub use editor_ext::VimEditorExt;
20pub use operator::OperatorKind;
21pub use pending::{Key, Outcome, PendingState, step};
22/// The byte budget one `p` / `P` may insert. Public so a host can report the
23/// limit it just hit rather than restating the number.
24pub use vim::command::MAX_PASTE_BYTES;
25/// Build an `Editor` that interprets keys as vim, or retro-fit the discipline
26/// onto one that already exists.
27///
28/// `Editor::new` leaves the discipline slot empty (the engine cannot name a
29/// concrete discipline), so an editor built through it ignores vim keys. Every
30/// vim-driven editor goes through one of these two (#267).
31pub use vim::{install as install_vim_discipline, vim_editor};
32
33/// Mode discriminator for the hjkl editor stack.
34///
35/// Used as the mode parameter in `hjkl-keymap`'s generic `Keymap<A, M: Mode>`.
36/// Satisfies the `hjkl_keymap::Mode` trait via its blanket impl for any
37/// `Copy + Eq + Hash + Debug` type.
38#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
39pub enum Mode {
40    Normal,
41    Insert,
42    Visual,
43    VisualLine,
44    VisualBlock,
45    OpPending,
46    CommandLine,
47}
48
49/// Drive the vim FSM with a [`hjkl_engine::PlannedInput`]. Translates the
50/// planned input to engine [`hjkl_engine::Input`], dispatches through
51/// [`dispatch_input`], and emits cursor-shape changes.
52///
53/// Returns `true` if the engine consumed the keystroke. Returns `false` for
54/// variants the legacy FSM does not dispatch (`Mouse`, `Paste`, `FocusGained`,
55/// `FocusLost`, `Resize`) and for special-key variants that map to `Key::Null`.
56pub fn feed_input<H: hjkl_engine::Host>(
57    editor: &mut hjkl_engine::Editor<hjkl_buffer::View, H>,
58    input: hjkl_engine::PlannedInput,
59) -> bool {
60    let Some(event) = hjkl_engine::decode_planned_input(input) else {
61        return false;
62    };
63    let consumed = dispatch_input(editor, event);
64    editor.emit_cursor_shape_if_changed();
65    consumed
66}
67
68/// Drive the vim FSM with one [`hjkl_engine::Input`].
69///
70/// This is the sole entry-point that decouples callers from the engine's
71/// internal FSM. Returns `true` if the engine consumed the keystroke.
72///
73/// # Phase 6.6c / 6.6d / 6.6e
74///
75/// Search-prompt mode (6.6c) is intercepted here before `begin_step` because
76/// it is a true short-circuit (no prelude/epilogue needed).
77///
78/// Insert mode (6.6d) is hosted in `hjkl-vim::insert::step_insert`.
79///
80/// Normal / Visual / VisualLine / VisualBlock / operator-pending modes (6.6e)
81/// are hosted in `hjkl-vim::normal::step_normal`. Both are wrapped with
82/// `begin_step` / `end_step` so macro recording, viewport scrolling, and
83/// `current_mode` sync all fire correctly.
84///
85/// In debug builds this is also where the `curswant` invariant is checked —
86/// see [`crate::curswant`]. Every keystroke that reaches the vim FSM passes
87/// through here, from the app (`hjkl_vim_tui::handle_key`), the compat-oracle
88/// driver, `:normal`, and macro replay alike, which makes it the one place
89/// the check has to live.
90pub fn dispatch_input<H: hjkl_engine::Host>(
91    editor: &mut hjkl_engine::Editor<hjkl_buffer::View, H>,
92    input: hjkl_engine::Input,
93) -> bool {
94    #[cfg(debug_assertions)]
95    let pre = curswant::capture(editor);
96    let consumed = dispatch_input_inner(editor, input);
97    #[cfg(debug_assertions)]
98    curswant::assert_invariant(editor, pre, input);
99    consumed
100}
101
102fn dispatch_input_inner<H: hjkl_engine::Host>(
103    editor: &mut hjkl_engine::Editor<hjkl_buffer::View, H>,
104    input: hjkl_engine::Input,
105) -> bool {
106    // Search-prompt intercept: short-circuits before begin_step because it
107    // needs no prelude/epilogue.
108    if editor.search_prompt_state().is_some() {
109        return search_prompt::step_search_prompt(editor, input);
110    }
111    // Run the prelude (timestamps, chord-timeout, macro-stop, snapshots).
112    let bk = match step::begin_step(editor, input) {
113        Ok(bk) => bk,
114        Err(consumed) => return consumed,
115    };
116    // Per-mode FSM dispatch — hjkl-vim hosts all modes.
117    let consumed = match editor.vim_mode() {
118        hjkl_engine::VimMode::Insert => insert::step_insert(editor, input),
119        _ => normal::step_normal(editor, input),
120    };
121    // Run the epilogue (marks, one-shot-normal, sync, recorder, mode sync).
122    step::end_step(editor, input, bk, consumed)
123}