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 / Buffer + 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//! - [`KeybindingMode`] / [`VimMode`] — mode enums used by host apps.
21//! - [`ex::run`] / [`ex::ExEffect`] — drive ex-mode commands.
22
23mod buf_helpers;
24mod buffer_impl;
25mod editor;
26mod input;
27pub mod keymap_motion;
28pub mod motions;
29mod registers;
30pub mod search;
31pub mod substitute;
32pub mod types;
33mod viewport_math;
34pub mod vim;
35
36pub use editor::{Editor, LspIntent, MarkJump, StepBookkeeping};
37pub use input::{Input, Key, decode_macro, from_planned as decode_planned_input};
38pub use registers::{Registers, Slot};
39
40pub use buffer_impl::{BufferFoldProvider, BufferFoldProviderMut};
41pub use keymap_motion::MotionKind;
42pub use substitute::{
43 SubstError, SubstFlags, SubstituteCmd, SubstituteMatch, SubstituteOutcome,
44 apply_collected_matches, apply_substitute, collect_substitute_matches, parse_substitute,
45};
46pub use types::{
47 Attrs, Buffer, BufferEdit, BufferId, Color, ContentEdit, Cursor, CursorShape, DefaultHost,
48 Edit, EditorSnapshot, EngineError, FoldOp, FoldProvider, Highlight, HighlightKind, Host,
49 Input as PlannedInput, Mode, Modifiers, MouseEvent, MouseKind, NoopFoldProvider, OptionValue,
50 Options, Pos, Query, RenderFrame, Search, Selection, SelectionKind, SelectionSet, SnapshotMode,
51 SpecialKey, Style, Viewport, WrapMode,
52};
53pub use vim::{
54 InsertDir, InsertEntry, InsertReason, InsertSession, LastChange, LastVisual, Motion, Operator,
55 Pending, RangeKind, ScrollDir, SearchPrompt, op_is_change, parse_motion,
56};
57
58/// The FSM-internal mode discriminator used by `Editor::fsm_mode()` and
59/// `Editor::set_fsm_mode()`. Re-exported as `FsmMode` to avoid clashing with
60/// the `types::Mode` buffer-side enum that is already exported as `Mode`.
61///
62/// Used by `hjkl-vim::normal` and `hjkl-vim::dispatch_input` for mode
63/// comparisons.
64pub use vim::Mode as FsmMode;
65
66// 0.0.32 dropped the `#[deprecated]` re-export aliases introduced at
67// 0.0.31 (`SpecBuffer`, `SpecBufferEdit`, `EditOp`, `PlannedViewport`).
68// Consumers must use the canonical names: `Buffer`, `BufferEdit`,
69// `Edit`, `Viewport`.
70
71/// Which keyboard discipline the editor uses. Currently vim-only, but
72/// kept as an enum so future emacs / plain bindings can slot in without
73/// touching the public signature.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
75pub enum KeybindingMode {
76 #[default]
77 Vim,
78}
79
80#[cfg(feature = "serde")]
81impl serde::Serialize for KeybindingMode {
82 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
83 s.serialize_str("vim")
84 }
85}
86
87#[cfg(feature = "serde")]
88impl<'de> serde::Deserialize<'de> for KeybindingMode {
89 fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
90 let _ = String::deserialize(d)?;
91 Ok(KeybindingMode::Vim)
92 }
93}
94
95/// Coarse vim-mode a host app can display in its status line.
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
97pub enum VimMode {
98 #[default]
99 Normal,
100 Insert,
101 Visual,
102 VisualLine,
103 VisualBlock,
104}
105
106/// A read-only *view* layered over the real input [`VimMode`]. Unlike a vim
107/// mode (which decides how keystrokes are interpreted), a `ViewMode` only
108/// changes what the buffer presents — input is still interpreted as Normal.
109///
110/// `Blame` is the git-blame overlay: the editor is read-only and the host
111/// renders per-commit framing. It is only meaningful while the input mode is
112/// `Normal`; any transition to Insert/Visual/etc. drops it back to `Normal`
113/// (see [`Editor::is_blame`]). New read-only overlays (diff, conflict, …)
114/// become additional variants here without touching `VimMode`.
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
116pub enum ViewMode {
117 #[default]
118 Normal,
119 Blame,
120}