Skip to main content

hjkl_vim/
lib.rs

1pub mod cmd;
2pub mod count;
3pub mod descriptors;
4pub mod insert;
5pub mod motion;
6pub mod normal;
7pub mod operator;
8pub mod pending;
9pub mod search_prompt;
10
11pub use cmd::EngineCmd;
12pub use count::CountAccumulator;
13// MotionKind moved to hjkl-engine (Phase 6.6 cycle-break); re-exported here for back-compat.
14pub use hjkl_engine::MotionKind;
15pub use operator::OperatorKind;
16pub use pending::{Key, Outcome, PendingState, step};
17
18/// Mode discriminator for the hjkl editor stack.
19///
20/// Used as the mode parameter in `hjkl-keymap`'s generic `Keymap<A, M: Mode>`.
21/// Satisfies the `hjkl_keymap::Mode` trait via its blanket impl for any
22/// `Copy + Eq + Hash + Debug` type.
23#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
24pub enum Mode {
25    Normal,
26    Insert,
27    Visual,
28    VisualLine,
29    VisualBlock,
30    OpPending,
31    CommandLine,
32}
33
34/// Drive the vim FSM with a [`hjkl_engine::PlannedInput`]. Translates the
35/// planned input to engine [`hjkl_engine::Input`], dispatches through
36/// [`dispatch_input`], and emits cursor-shape changes.
37///
38/// Returns `true` if the engine consumed the keystroke. Returns `false` for
39/// variants the legacy FSM does not dispatch (`Mouse`, `Paste`, `FocusGained`,
40/// `FocusLost`, `Resize`) and for special-key variants that map to `Key::Null`.
41pub fn feed_input<H: hjkl_engine::Host>(
42    editor: &mut hjkl_engine::Editor<hjkl_buffer::Buffer, H>,
43    input: hjkl_engine::PlannedInput,
44) -> bool {
45    let Some(event) = hjkl_engine::decode_planned_input(input) else {
46        return false;
47    };
48    let consumed = dispatch_input(editor, event);
49    editor.emit_cursor_shape_if_changed();
50    consumed
51}
52
53/// Drive the vim FSM with one [`hjkl_engine::Input`].
54///
55/// This is the Phase 6.6 entry-point that decouples callers from the engine's
56/// internal FSM. Returns `true` if the engine consumed the keystroke.
57///
58/// # Migration guide
59///
60/// Replace `editor.step_input(input)` with `hjkl_vim::dispatch_input(&mut editor, input)`.
61/// The `Editor::step_input` method is deprecated; remove it in a later release.
62///
63/// # Phase 6.6c / 6.6d / 6.6e
64///
65/// Search-prompt mode (6.6c) is intercepted here before `begin_step` because
66/// it is a true short-circuit (no prelude/epilogue needed).
67///
68/// Insert mode (6.6d) is hosted in `hjkl-vim::insert::step_insert`.
69///
70/// Normal / Visual / VisualLine / VisualBlock / operator-pending modes (6.6e)
71/// are hosted in `hjkl-vim::normal::step_normal`. Both are wrapped with
72/// `begin_step` / `end_step` so macro recording, viewport scrolling, and
73/// `current_mode` sync all fire correctly.
74///
75/// The deprecated `Editor::step_input_raw` shim path is retained for
76/// back-compat until Phase 6.6h.
77pub fn dispatch_input<H: hjkl_engine::Host>(
78    editor: &mut hjkl_engine::Editor<hjkl_buffer::Buffer, H>,
79    input: hjkl_engine::Input,
80) -> bool {
81    // Search-prompt intercept: short-circuits before begin_step, matching
82    // vim::step's own search-prompt handling (which also skips begin_step).
83    if editor.search_prompt_state().is_some() {
84        return search_prompt::step_search_prompt(editor, input);
85    }
86    // Run the prelude (timestamps, chord-timeout, macro-stop, snapshots).
87    let bk = match editor.begin_step(input) {
88        Ok(bk) => bk,
89        Err(consumed) => return consumed,
90    };
91    // Per-mode FSM dispatch — hjkl-vim hosts all modes.
92    let consumed = match editor.vim_mode() {
93        hjkl_engine::VimMode::Insert => insert::step_insert(editor, input),
94        _ => normal::step_normal(editor, input),
95    };
96    // Run the epilogue (marks, one-shot-normal, sync, recorder, mode sync).
97    editor.end_step(input, bk, consumed)
98}