hjkl_engine/lib.rs
1//! Vim-mode editor engine built on top of [`hjkl_buffer`].
2//!
3//! Exposes an [`Editor`] you can drop into a ratatui layout, a command
4//! grammar that covers the bulk of vim's normal / insert / visual /
5//! visual-line / visual-block modes, text-object operators, dot-repeat,
6//! and ex-command handling (`:s/foo/bar/g`, `:w`, `:q`, `:noh`, ...).
7//! Rendering goes through `hjkl_buffer::BufferView`; selection / gutter
8//! highlights are painted in the same single-pass as text.
9//!
10//! Imported wholesale from sqeel-vim with full git history. The trait
11//! extraction (Selection / SelectionSet / Buffer + Host sub-traits) lands
12//! progressively under [`crate::types`]. Pre-1.0 churn — the public surface
13//! may change in patch bumps. See [docs.rs](https://docs.rs/hjkl-engine) for
14//! the canonical API reference.
15//!
16//! The legacy public surface is intentionally narrow:
17//!
18//! - [`Editor`] — the editor widget.
19//! - [`KeybindingMode`] / [`VimMode`] — mode enums used by host apps.
20//! - [`ex::run`] / [`ex::ExEffect`] — drive ex-mode commands.
21
22mod buf_helpers;
23mod buffer_impl;
24mod editor;
25mod input;
26pub mod motions;
27mod registers;
28pub mod search;
29pub mod types;
30mod viewport_math;
31mod vim;
32
33pub use editor::{Editor, LspIntent};
34pub use input::{Input, Key};
35pub use registers::{Registers, Slot};
36
37pub use buffer_impl::{BufferFoldProvider, BufferFoldProviderMut};
38pub use types::{
39 Attrs, Buffer, BufferEdit, BufferId, Color, ContentEdit, Cursor, CursorShape, DefaultHost,
40 Edit, EditorSnapshot, EngineError, FoldOp, FoldProvider, Highlight, HighlightKind, Host,
41 Input as PlannedInput, Mode, Modifiers, MouseEvent, MouseKind, NoopFoldProvider, OptionValue,
42 Options, Pos, Query, RenderFrame, Search, Selection, SelectionKind, SelectionSet, SnapshotMode,
43 SpecialKey, Style, Viewport, WrapMode,
44};
45pub use vim::SearchPrompt;
46
47/// Drive the vim FSM with one [`Input`]. Returns `true` if the engine
48/// consumed the keystroke. Hosts that don't pull in the `crossterm`
49/// feature reach the FSM through this function (the `crossterm`-gated
50/// [`Editor::handle_key`] is a thin wrapper around it).
51pub fn step<H: types::Host>(editor: &mut Editor<hjkl_buffer::Buffer, H>, input: Input) -> bool {
52 vim::step(editor, input)
53}
54
55// 0.0.32 dropped the `#[deprecated]` re-export aliases introduced at
56// 0.0.31 (`SpecBuffer`, `SpecBufferEdit`, `EditOp`, `PlannedViewport`).
57// Consumers must use the canonical names: `Buffer`, `BufferEdit`,
58// `Edit`, `Viewport`.
59
60/// Which keyboard discipline the editor uses. Currently vim-only, but
61/// kept as an enum so future emacs / plain bindings can slot in without
62/// touching the public signature.
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
64pub enum KeybindingMode {
65 #[default]
66 Vim,
67}
68
69#[cfg(feature = "serde")]
70impl serde::Serialize for KeybindingMode {
71 fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
72 s.serialize_str("vim")
73 }
74}
75
76#[cfg(feature = "serde")]
77impl<'de> serde::Deserialize<'de> for KeybindingMode {
78 fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
79 let _ = String::deserialize(d)?;
80 Ok(KeybindingMode::Vim)
81 }
82}
83
84/// Coarse vim-mode a host app can display in its status line.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
86pub enum VimMode {
87 #[default]
88 Normal,
89 Insert,
90 Visual,
91 VisualLine,
92 VisualBlock,
93}