Skip to main content

hjkl_vim/vim/
mod.rs

1//! Vim-mode engine.
2//!
3//! Implements a command grammar of the form
4//!
5//! ```text
6//! Command := count? (operator count? (motion | text-object)
7//!                   | motion
8//!                   | insert-entry
9//!                   | misc)
10//! ```
11//!
12//! The parser is a small state machine driven by one `Input` at a time.
13//! Motions and text objects produce a [`Range`] (with inclusive/exclusive
14//! / linewise classification). A single [`Operator`] implementation
15//! applies a range — so `dw`, `d$`, `daw`, and visual `d` all go through
16//! the same code path.
17//!
18//! The most recent mutating command is stored in
19//! [`VimState::last_change`] so `.` can replay it.
20
21pub(crate) mod bridges;
22pub(crate) mod command;
23pub(crate) mod comment;
24pub(crate) mod dot_repeat;
25pub(crate) mod entry;
26pub(crate) mod insert_bridges;
27pub(crate) mod insert_ops;
28pub(crate) mod jumplist;
29pub(crate) mod linewise;
30pub(crate) mod motion;
31pub(crate) mod op_motion;
32pub(crate) mod operator;
33pub(crate) mod range_ops;
34pub(crate) mod sneak;
35pub(crate) mod state;
36pub(crate) mod text_object;
37pub(crate) mod text_object_ops;
38pub(crate) mod visual;
39pub(crate) mod visual_ops;
40
41pub use hjkl_engine::abbrev::{Abbrev, AbbrevKind, AbbrevTrigger};
42pub use hjkl_engine::search::SearchPrompt;
43pub use hjkl_engine::types::{
44    CHANGE_LIST_MAX, InsertDir, JUMPLIST_MAX, SEARCH_HISTORY_MAX, ScrollDir,
45};
46pub use hjkl_vim_types::{
47    InsertEntry, InsertReason, InsertSession, LastChange, LastHorizontalMotion, LastVisual, Mode,
48    Motion, Operator, Pending, RangeKind, TextObject,
49};
50
51// Flat intra-crate namespace: the FSM was one file until #267, and its
52// helpers still call each other freely across these module lines.
53pub(crate) use bridges::*;
54pub(crate) use command::*;
55pub(crate) use comment::*;
56pub(crate) use dot_repeat::*;
57pub(crate) use entry::*;
58pub(crate) use insert_bridges::*;
59pub(crate) use insert_ops::*;
60pub(crate) use jumplist::*;
61pub(crate) use linewise::*;
62pub(crate) use motion::*;
63pub(crate) use op_motion::*;
64pub(crate) use operator::*;
65pub(crate) use range_ops::*;
66pub(crate) use sneak::*;
67pub(crate) use state::*;
68pub(crate) use text_object::*;
69pub(crate) use text_object_ops::*;
70pub(crate) use visual::*;
71pub(crate) use visual_ops::*;
72
73// The vim discipline's actual public API.
74pub use motion::parse_motion;
75pub use state::{MAX_COUNT, VimState};
76// `matching_tag_pair` and the tag-matching group moved to `hjkl_engine::tag`
77// (#265): a pure buffer query any discipline can use, not vim grammar.
78pub use visual::{drop_blame_if_left_normal, op_is_change};
79
80use hjkl_engine::Editor;
81
82/// Install the vim discipline on `ed`, replacing whatever was there.
83///
84/// [`Editor::new`] leaves the discipline slot at
85/// [`NoDiscipline`](hjkl_engine::NoDiscipline) — the engine cannot name a concrete
86/// discipline. Every editor that should interpret keys as vim must be handed to
87/// this once at construction; [`vim_editor`] does both in one call.
88///
89/// Dispatching vim input at an editor that skipped this panics on the first
90/// state access (see `hjkl_vim::vim_state`) rather than silently behaving like
91/// a keyboard-less buffer.
92pub fn install<H: hjkl_engine::types::Host>(ed: &mut Editor<hjkl_buffer::View, H>) {
93    ed.set_discipline(Box::new(VimState::default()));
94}
95/// Build an [`Editor`] with the vim discipline already installed.
96///
97/// The vim-flavoured counterpart of [`Editor::new`], which yields an editor
98/// with no discipline at all.
99pub fn vim_editor<H: hjkl_engine::types::Host>(
100    buffer: hjkl_buffer::View,
101    host: H,
102    options: hjkl_engine::types::Options,
103) -> Editor<hjkl_buffer::View, H> {
104    let mut ed = Editor::new(buffer, host, options);
105    install(&mut ed);
106    ed
107}