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 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 vim_buf;
39pub(crate) mod visual;
40pub(crate) mod visual_ops;
41pub(crate) mod word_object;
42
43pub use hjkl_engine::abbrev::{Abbrev, AbbrevKind, AbbrevTrigger};
44pub use hjkl_engine::search::SearchPrompt;
45pub use hjkl_engine::types::{
46    CHANGE_LIST_MAX, InsertDir, JUMPLIST_MAX, SEARCH_HISTORY_MAX, ScrollDir,
47};
48pub use hjkl_vim_types::{
49    InsertEntry, InsertReason, InsertSession, LastChange, LastHorizontalMotion, LastVisual, Mode,
50    Motion, Operator, Pending, RangeKind, TextObject,
51};
52
53// Flat intra-crate namespace: the FSM was one file until #267, and its
54// helpers still call each other freely across these module lines.
55pub(crate) use bridges::*;
56pub(crate) use command::*;
57pub(crate) use comment::*;
58pub(crate) use dot_repeat::*;
59pub(crate) use entry::*;
60pub(crate) use insert_bridges::*;
61pub(crate) use insert_ops::*;
62pub(crate) use jumplist::*;
63pub(crate) use linewise::*;
64pub(crate) use motion::*;
65pub(crate) use op_motion::*;
66pub(crate) use operator::*;
67pub(crate) use range_ops::*;
68pub(crate) use sneak::*;
69pub(crate) use state::*;
70pub(crate) use text_object::*;
71pub(crate) use text_object_ops::*;
72pub(crate) use visual::*;
73pub(crate) use visual_ops::*;
74pub(crate) use word_object::*;
75
76// The vim discipline's actual public API.
77pub use motion::parse_motion;
78pub use state::{MAX_COUNT, VimState};
79// `matching_tag_pair` and the tag-matching group moved to `hjkl_engine::tag`
80// (#265): a pure buffer query any discipline can use, not vim grammar.
81pub use visual::{drop_blame_if_left_normal, op_is_change};
82
83use hjkl_engine::Editor;
84
85/// Install the vim discipline on `ed`, replacing whatever was there.
86///
87/// [`Editor::new`] leaves the discipline slot at
88/// [`NoDiscipline`](hjkl_engine::NoDiscipline) — the engine cannot name a concrete
89/// discipline. Every editor that should interpret keys as vim must be handed to
90/// this once at construction; [`vim_editor`] does both in one call.
91///
92/// Dispatching vim input at an editor that skipped this panics on the first
93/// state access (see `hjkl_vim::vim_state`) rather than silently behaving like
94/// a keyboard-less buffer.
95pub fn install<H: hjkl_engine::types::Host>(ed: &mut Editor<hjkl_buffer::View, H>) {
96    ed.set_discipline(Box::new(VimState::default()));
97}
98/// Build an [`Editor`] with the vim discipline already installed.
99///
100/// The vim-flavoured counterpart of [`Editor::new`], which yields an editor
101/// with no discipline at all.
102pub fn vim_editor<H: hjkl_engine::types::Host>(
103    buffer: hjkl_buffer::View,
104    host: H,
105    options: hjkl_engine::types::Options,
106) -> Editor<hjkl_buffer::View, H> {
107    let mut ed = Editor::new(buffer, host, options);
108    install(&mut ed);
109    ed
110}