hjkl_engine/lib.rs
1//! Vim-mode editor engine built on top of [`hjkl_buffer`].
2//!
3//! Exposes an [`Editor`] that is fully toolkit-agnostic. Covers the bulk
4//! of vim's normal / insert / visual / visual-line / visual-block modes,
5//! text-object operators, dot-repeat, and ex-command handling
6//! (`:s/foo/bar/g`, `:w`, `:q`, `:noh`, ...). Rendering goes through
7//! `hjkl_buffer::BufferView`; selection / gutter highlights are painted in
8//! the same single-pass as text. TUI/crossterm adapters live in the
9//! `hjkl-engine-tui` companion crate.
10//!
11//! Imported wholesale from sqeel-vim with full git history. The trait
12//! extraction (Selection / SelectionSet / View + Host sub-traits) lands
13//! progressively under [`crate::types`]. Pre-1.0 churn — the public surface
14//! may change in patch bumps. See [docs.rs](https://docs.rs/hjkl-engine) for
15//! the canonical API reference.
16//!
17//! The legacy public surface is intentionally narrow:
18//!
19//! - [`Editor`] — the editor widget.
20//! - [`VimMode`] — mode enum used by host apps.
21//! - [`ex::run`] / [`ex::ExEffect`] — drive ex-mode commands.
22
23pub mod abbrev;
24pub mod buf_helpers;
25mod buffer_impl;
26mod discipline;
27mod editor;
28pub mod input;
29pub mod keymap_motion;
30pub mod motions;
31mod registers;
32pub mod rope_util;
33pub mod search;
34pub mod selection_shift;
35pub mod substitute;
36pub mod tag;
37pub mod types;
38mod viewport_math;
39
40pub use discipline::{DisciplineState, NoDiscipline};
41pub use editor::{
42 ChangeBank, CursorScrollTarget, Editor, GlobalMarks, LspIntent, MarkJump, SearchBank, Settings,
43 UndoGranularity,
44};
45pub use input::{Input, Key, decode_macro, from_planned as decode_planned_input};
46pub use registers::{Registers, Slot};
47pub use selection_shift::{Sel, shift_position, shift_sel};
48
49pub use buffer_impl::{BufferFoldProvider, BufferFoldProviderMut, SnapshotFoldProvider};
50pub use keymap_motion::MotionKind;
51pub use substitute::{
52 SubstError, SubstFlags, SubstituteCmd, SubstituteMatch, SubstituteOutcome,
53 apply_collected_matches, apply_substitute, collect_substitute_matches, parse_substitute,
54};
55pub use types::{
56 Attrs, BufferEdit, BufferId, Color, ContentEdit, Cursor, CursorShape, DefaultHost, Edit,
57 EditorSnapshot, EngineError, FoldOp, FoldProvider, Highlight, HighlightKind, Host,
58 Input as PlannedInput, Mode, Modifiers, MouseEvent, MouseKind, NoopFoldProvider, OptionValue,
59 Options, Pos, Query, RenderFrame, Search, Selection, SelectionKind, SelectionSet, SnapshotMode,
60 SpecialKey, Style, View, Viewport, WrapMode,
61};
62// The vim FSM itself now lives in `hjkl-vim` (#267). What stays here is the
63// engine-owned substrate it happens to use — abbreviations, the search prompt,
64// scroll/insert directions — plus the shared vocabulary types from
65// `hjkl-vim-types`, which both crates name and neither owns.
66pub use abbrev::{Abbrev, AbbrevTrigger};
67pub use search::SearchPrompt;
68pub use tag::matching_tag_pair;
69pub use types::{InsertDir, ScrollDir};
70
71pub use hjkl_vim_types::{
72 InsertEntry, InsertReason, InsertSession, LastChange, LastVisual, Motion, Operator, Pending,
73 RangeKind,
74};
75
76/// The FSM-internal mode discriminator used by `Editor::fsm_mode()` and
77/// `Editor::set_fsm_mode()`. Re-exported as `FsmMode` to avoid clashing with
78/// the `types::Mode` buffer-side enum that is already exported as `Mode`.
79///
80/// Used by `hjkl-vim::normal` and `hjkl-vim::dispatch_input` for mode
81/// comparisons.
82pub use hjkl_vim_types::Mode as FsmMode;
83
84// 0.0.32 dropped the `#[deprecated]` re-export aliases introduced at
85// 0.0.31 (`SpecBuffer`, `SpecBufferEdit`, `EditOp`, `PlannedViewport`).
86// Consumers must use the canonical names: `View`, `BufferEdit`,
87// `Edit`, `Viewport`.
88
89/// Coarse vim-mode a host app can display in its status line.
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
91pub enum VimMode {
92 #[default]
93 Normal,
94 Insert,
95 Visual,
96 VisualLine,
97 VisualBlock,
98}
99
100/// Discipline-agnostic coarse mode for app chrome (status badge, cursor
101/// shape). Unlike [`VimMode`] — which names vim-specific states — `CoarseMode`
102/// is a minimal projection: "are we inserting text, selecting, or idle?"
103///
104/// App chrome reads this instead of `VimMode` so it stays behind the engine's
105/// discipline seam ([`DisciplineState`]): the installed discipline (vim today)
106/// maps its own modes onto these variants via `DisciplineState::coarse_mode`.
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
108pub enum CoarseMode {
109 /// Idle / command-ready (vim Normal).
110 #[default]
111 Normal,
112 /// Text is being inserted at the caret (vim Insert).
113 Insert,
114 /// A character-wise selection is active (vim Visual).
115 Select,
116 /// A line-wise selection is active (vim VisualLine).
117 SelectLine,
118 /// A block / column selection is active (vim VisualBlock).
119 SelectBlock,
120}
121
122/// A read-only *view* layered over the real input [`VimMode`]. Unlike a vim
123/// mode (which decides how keystrokes are interpreted), a `ViewMode` only
124/// changes what the buffer presents — input is still interpreted as Normal.
125///
126/// `Blame` is the git-blame overlay: the editor is read-only and the host
127/// renders per-commit framing. It is only meaningful while the input mode is
128/// `Normal`; any transition to Insert/Visual/etc. drops it back to `Normal`
129/// (see [`Editor::is_blame`]). New read-only overlays (diff, conflict, …)
130/// become additional variants here without touching `VimMode`.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
132pub enum ViewMode {
133 #[default]
134 Normal,
135 Blame,
136}