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::{CursorScrollTarget, Editor, LspIntent, MarkJump, Settings, UndoGranularity};
42pub use input::{Input, Key, decode_macro, from_planned as decode_planned_input};
43pub use registers::{Registers, Slot};
44pub use selection_shift::{Sel, shift_position, shift_sel};
45
46pub use buffer_impl::{BufferFoldProvider, BufferFoldProviderMut, SnapshotFoldProvider};
47pub use keymap_motion::MotionKind;
48pub use substitute::{
49 SubstError, SubstFlags, SubstituteCmd, SubstituteMatch, SubstituteOutcome,
50 apply_collected_matches, apply_substitute, collect_substitute_matches, parse_substitute,
51};
52pub use types::{
53 Attrs, BufferEdit, BufferId, Color, ContentEdit, Cursor, CursorShape, DefaultHost, Edit,
54 EditorSnapshot, EngineError, FoldOp, FoldProvider, Highlight, HighlightKind, Host,
55 Input as PlannedInput, Mode, Modifiers, MouseEvent, MouseKind, NoopFoldProvider, OptionValue,
56 Options, Pos, Query, RenderFrame, Search, Selection, SelectionKind, SelectionSet, SnapshotMode,
57 SpecialKey, Style, View, Viewport, WrapMode,
58};
59// The vim FSM itself now lives in `hjkl-vim` (#267). What stays here is the
60// engine-owned substrate it happens to use — abbreviations, the search prompt,
61// scroll/insert directions — plus the shared vocabulary types from
62// `hjkl-vim-types`, which both crates name and neither owns.
63pub use abbrev::{Abbrev, AbbrevTrigger};
64pub use search::SearchPrompt;
65pub use tag::matching_tag_pair;
66pub use types::{InsertDir, ScrollDir};
67
68pub use hjkl_vim_types::{
69 InsertEntry, InsertReason, InsertSession, LastChange, LastVisual, Motion, Operator, Pending,
70 RangeKind,
71};
72
73/// The FSM-internal mode discriminator used by `Editor::fsm_mode()` and
74/// `Editor::set_fsm_mode()`. Re-exported as `FsmMode` to avoid clashing with
75/// the `types::Mode` buffer-side enum that is already exported as `Mode`.
76///
77/// Used by `hjkl-vim::normal` and `hjkl-vim::dispatch_input` for mode
78/// comparisons.
79pub use hjkl_vim_types::Mode as FsmMode;
80
81// 0.0.32 dropped the `#[deprecated]` re-export aliases introduced at
82// 0.0.31 (`SpecBuffer`, `SpecBufferEdit`, `EditOp`, `PlannedViewport`).
83// Consumers must use the canonical names: `View`, `BufferEdit`,
84// `Edit`, `Viewport`.
85
86/// Coarse vim-mode a host app can display in its status line.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
88pub enum VimMode {
89 #[default]
90 Normal,
91 Insert,
92 Visual,
93 VisualLine,
94 VisualBlock,
95}
96
97/// Discipline-agnostic coarse mode for app chrome (status badge, cursor
98/// shape). Unlike [`VimMode`] — which names vim-specific states — `CoarseMode`
99/// is a minimal projection: "are we inserting text, selecting, or idle?"
100///
101/// App chrome reads this instead of `VimMode` so it stays behind the engine's
102/// discipline seam ([`DisciplineState`]): the installed discipline (vim today)
103/// maps its own modes onto these variants via `DisciplineState::coarse_mode`.
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
105pub enum CoarseMode {
106 /// Idle / command-ready (vim Normal).
107 #[default]
108 Normal,
109 /// Text is being inserted at the caret (vim Insert).
110 Insert,
111 /// A character-wise selection is active (vim Visual).
112 Select,
113 /// A line-wise selection is active (vim VisualLine).
114 SelectLine,
115 /// A block / column selection is active (vim VisualBlock).
116 SelectBlock,
117}
118
119/// A read-only *view* layered over the real input [`VimMode`]. Unlike a vim
120/// mode (which decides how keystrokes are interpreted), a `ViewMode` only
121/// changes what the buffer presents — input is still interpreted as Normal.
122///
123/// `Blame` is the git-blame overlay: the editor is read-only and the host
124/// renders per-commit framing. It is only meaningful while the input mode is
125/// `Normal`; any transition to Insert/Visual/etc. drops it back to `Normal`
126/// (see [`Editor::is_blame`]). New read-only overlays (diff, conflict, …)
127/// become additional variants here without touching `VimMode`.
128#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
129pub enum ViewMode {
130 #[default]
131 Normal,
132 Blame,
133}