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