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