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