Skip to main content

hjkl_engine/
editor.rs

1//! Editor — the public sqeel-vim type, layered over `hjkl_buffer::Buffer`.
2//!
3//! This file owns the public Editor API — construction, content access,
4//! mouse and goto helpers, the (buffer-level) undo stack, and insert-mode
5//! session bookkeeping. All vim-specific keyboard handling lives in
6//! [`vim`] and communicates with Editor through a small internal API
7//! exposed via `pub(super)` fields and helper methods.
8
9use crate::input::Input;
10use crate::vim::{self, VimState};
11use crate::{KeybindingMode, VimMode};
12use std::sync::atomic::{AtomicU16, Ordering};
13
14/// Map a [`hjkl_buffer::Edit`] to one or more SPEC
15/// [`crate::types::Edit`] (`EditOp`) records.
16///
17/// Most buffer edits map to a single EditOp. Block ops
18/// ([`hjkl_buffer::Edit::InsertBlock`] /
19/// [`hjkl_buffer::Edit::DeleteBlockChunks`]) emit one EditOp per row
20/// touched — they edit non-contiguous cells and a single
21/// `range..range` can't represent the rectangle.
22///
23/// Returns an empty vec when the edit isn't representable (no buffer
24/// variant currently fails this check).
25fn edit_to_editops(edit: &hjkl_buffer::Edit) -> Vec<crate::types::Edit> {
26    use crate::types::{Edit as Op, Pos};
27    use hjkl_buffer::Edit as B;
28    let to_pos = |p: hjkl_buffer::Position| Pos {
29        line: p.row as u32,
30        col: p.col as u32,
31    };
32    match edit {
33        B::InsertChar { at, ch } => vec![Op {
34            range: to_pos(*at)..to_pos(*at),
35            replacement: ch.to_string(),
36        }],
37        B::InsertStr { at, text } => vec![Op {
38            range: to_pos(*at)..to_pos(*at),
39            replacement: text.clone(),
40        }],
41        B::DeleteRange { start, end, .. } => vec![Op {
42            range: to_pos(*start)..to_pos(*end),
43            replacement: String::new(),
44        }],
45        B::Replace { start, end, with } => vec![Op {
46            range: to_pos(*start)..to_pos(*end),
47            replacement: with.clone(),
48        }],
49        B::JoinLines {
50            row,
51            count,
52            with_space,
53        } => {
54            // Joining `count` rows after `row` collapses
55            // [(row+1, 0) .. (row+count, EOL)] into the joined
56            // sentinel. The replacement is either an empty string
57            // (gJ) or " " between segments (J).
58            let start = Pos {
59                line: *row as u32 + 1,
60                col: 0,
61            };
62            let end = Pos {
63                line: (*row + *count) as u32,
64                col: u32::MAX, // covers to EOL of the last source row
65            };
66            vec![Op {
67                range: start..end,
68                replacement: if *with_space {
69                    " ".into()
70                } else {
71                    String::new()
72                },
73            }]
74        }
75        B::SplitLines {
76            row,
77            cols,
78            inserted_space: _,
79        } => {
80            // SplitLines reverses a JoinLines: insert a `\n`
81            // (and optional dropped space) at each col on `row`.
82            cols.iter()
83                .map(|c| {
84                    let p = Pos {
85                        line: *row as u32,
86                        col: *c as u32,
87                    };
88                    Op {
89                        range: p..p,
90                        replacement: "\n".into(),
91                    }
92                })
93                .collect()
94        }
95        B::InsertBlock { at, chunks } => {
96            // One EditOp per row in the block — non-contiguous edits.
97            chunks
98                .iter()
99                .enumerate()
100                .map(|(i, chunk)| {
101                    let p = Pos {
102                        line: at.row as u32 + i as u32,
103                        col: at.col as u32,
104                    };
105                    Op {
106                        range: p..p,
107                        replacement: chunk.clone(),
108                    }
109                })
110                .collect()
111        }
112        B::DeleteBlockChunks { at, widths } => {
113            // One EditOp per row, deleting `widths[i]` chars at
114            // `(at.row + i, at.col)`.
115            widths
116                .iter()
117                .enumerate()
118                .map(|(i, w)| {
119                    let start = Pos {
120                        line: at.row as u32 + i as u32,
121                        col: at.col as u32,
122                    };
123                    let end = Pos {
124                        line: at.row as u32 + i as u32,
125                        col: at.col as u32 + *w as u32,
126                    };
127                    Op {
128                        range: start..end,
129                        replacement: String::new(),
130                    }
131                })
132                .collect()
133        }
134    }
135}
136
137/// Sum of bytes from the start of the buffer to the start of `row`.
138/// Walks lines + their separating `\n` bytes — matches the canonical
139/// `lines().join("\n")` byte rendering used by syntax tooling.
140#[inline]
141fn buffer_byte_of_row(buf: &hjkl_buffer::Buffer, row: usize) -> usize {
142    let n = buf.row_count();
143    let row = row.min(n);
144    let mut acc = 0usize;
145    for r in 0..row {
146        acc += buf.line(r).map(|s| s.len()).unwrap_or(0);
147        if r + 1 < n {
148            acc += 1; // separator '\n'
149        }
150    }
151    acc
152}
153
154/// Convert an `hjkl_buffer::Position` (char-indexed col) into byte
155/// coordinates `(byte_within_buffer, (row, col_byte))` against the
156/// **pre-edit** buffer.
157fn position_to_byte_coords(
158    buf: &hjkl_buffer::Buffer,
159    pos: hjkl_buffer::Position,
160) -> (usize, (u32, u32)) {
161    let row = pos.row.min(buf.row_count().saturating_sub(1));
162    let line = buf.line(row).unwrap_or_default();
163    let col_byte = pos.byte_offset(&line);
164    let byte = buffer_byte_of_row(buf, row) + col_byte;
165    (byte, (row as u32, col_byte as u32))
166}
167
168/// Compute the byte position after inserting `text` starting at
169/// `start_byte` / `start_pos`. Returns `(end_byte, end_position)`.
170fn advance_by_text(text: &str, start_byte: usize, start_pos: (u32, u32)) -> (usize, (u32, u32)) {
171    let new_end_byte = start_byte + text.len();
172    let newlines = text.bytes().filter(|&b| b == b'\n').count();
173    let end_pos = if newlines == 0 {
174        (start_pos.0, start_pos.1 + text.len() as u32)
175    } else {
176        // Bytes after the last newline determine the trailing column.
177        let last_nl = text.rfind('\n').unwrap();
178        let tail_bytes = (text.len() - last_nl - 1) as u32;
179        (start_pos.0 + newlines as u32, tail_bytes)
180    };
181    (new_end_byte, end_pos)
182}
183
184/// Translate a single `hjkl_buffer::Edit` into one or more
185/// [`crate::types::ContentEdit`] records using the **pre-edit** buffer
186/// state for byte/position lookups. Block ops fan out to one entry per
187/// touched row (matches `edit_to_editops`).
188fn content_edits_from_buffer_edit(
189    buf: &hjkl_buffer::Buffer,
190    edit: &hjkl_buffer::Edit,
191) -> Vec<crate::types::ContentEdit> {
192    use hjkl_buffer::Edit as B;
193    use hjkl_buffer::Position;
194
195    let mut out: Vec<crate::types::ContentEdit> = Vec::new();
196
197    match edit {
198        B::InsertChar { at, ch } => {
199            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
200            let new_end_byte = start_byte + ch.len_utf8();
201            let new_end_pos = (start_pos.0, start_pos.1 + ch.len_utf8() as u32);
202            out.push(crate::types::ContentEdit {
203                start_byte,
204                old_end_byte: start_byte,
205                new_end_byte,
206                start_position: start_pos,
207                old_end_position: start_pos,
208                new_end_position: new_end_pos,
209            });
210        }
211        B::InsertStr { at, text } => {
212            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
213            let (new_end_byte, new_end_pos) = advance_by_text(text, start_byte, start_pos);
214            out.push(crate::types::ContentEdit {
215                start_byte,
216                old_end_byte: start_byte,
217                new_end_byte,
218                start_position: start_pos,
219                old_end_position: start_pos,
220                new_end_position: new_end_pos,
221            });
222        }
223        B::DeleteRange { start, end, kind } => {
224            let (start, end) = if start <= end {
225                (*start, *end)
226            } else {
227                (*end, *start)
228            };
229            match kind {
230                hjkl_buffer::MotionKind::Char => {
231                    let (start_byte, start_pos) = position_to_byte_coords(buf, start);
232                    let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
233                    out.push(crate::types::ContentEdit {
234                        start_byte,
235                        old_end_byte,
236                        new_end_byte: start_byte,
237                        start_position: start_pos,
238                        old_end_position: old_end_pos,
239                        new_end_position: start_pos,
240                    });
241                }
242                hjkl_buffer::MotionKind::Line => {
243                    // Linewise delete drops rows [start.row..=end.row]. Map
244                    // to a span from start of `start.row` through start of
245                    // (end.row + 1). The buffer's own `do_delete_range`
246                    // collapses to row `start.row` after dropping.
247                    let lo = start.row;
248                    let hi = end.row.min(buf.row_count().saturating_sub(1));
249                    let start_byte = buffer_byte_of_row(buf, lo);
250                    let next_row_byte = if hi + 1 < buf.row_count() {
251                        buffer_byte_of_row(buf, hi + 1)
252                    } else {
253                        // No row after; clamp to end-of-buffer byte.
254                        buffer_byte_of_row(buf, buf.row_count())
255                            + buf
256                                .line(buf.row_count().saturating_sub(1))
257                                .map(|s| s.len())
258                                .unwrap_or(0)
259                    };
260                    out.push(crate::types::ContentEdit {
261                        start_byte,
262                        old_end_byte: next_row_byte,
263                        new_end_byte: start_byte,
264                        start_position: (lo as u32, 0),
265                        old_end_position: ((hi + 1) as u32, 0),
266                        new_end_position: (lo as u32, 0),
267                    });
268                }
269                hjkl_buffer::MotionKind::Block => {
270                    // Block delete removes a rectangle of chars per row.
271                    // Fan out to one ContentEdit per row.
272                    let (left_col, right_col) = (start.col.min(end.col), start.col.max(end.col));
273                    for row in start.row..=end.row {
274                        let row_start_pos = Position::new(row, left_col);
275                        let row_end_pos = Position::new(row, right_col + 1);
276                        let (sb, sp) = position_to_byte_coords(buf, row_start_pos);
277                        let (eb, ep) = position_to_byte_coords(buf, row_end_pos);
278                        if eb <= sb {
279                            continue;
280                        }
281                        out.push(crate::types::ContentEdit {
282                            start_byte: sb,
283                            old_end_byte: eb,
284                            new_end_byte: sb,
285                            start_position: sp,
286                            old_end_position: ep,
287                            new_end_position: sp,
288                        });
289                    }
290                }
291            }
292        }
293        B::Replace { start, end, with } => {
294            let (start, end) = if start <= end {
295                (*start, *end)
296            } else {
297                (*end, *start)
298            };
299            let (start_byte, start_pos) = position_to_byte_coords(buf, start);
300            let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
301            let (new_end_byte, new_end_pos) = advance_by_text(with, start_byte, start_pos);
302            out.push(crate::types::ContentEdit {
303                start_byte,
304                old_end_byte,
305                new_end_byte,
306                start_position: start_pos,
307                old_end_position: old_end_pos,
308                new_end_position: new_end_pos,
309            });
310        }
311        B::JoinLines {
312            row,
313            count,
314            with_space,
315        } => {
316            // Joining `count` rows after `row` collapses the bytes
317            // between EOL of `row` and EOL of `row + count` into either
318            // an empty string (gJ) or a single space per join (J — but
319            // only when both sides are non-empty; we approximate with
320            // a single space for simplicity).
321            let row = (*row).min(buf.row_count().saturating_sub(1));
322            let last_join_row = (row + count).min(buf.row_count().saturating_sub(1));
323            let line = buf.line(row).unwrap_or_default();
324            let row_eol_byte = buffer_byte_of_row(buf, row) + line.len();
325            let row_eol_col = line.len() as u32;
326            let next_row_after = last_join_row + 1;
327            let old_end_byte = if next_row_after < buf.row_count() {
328                buffer_byte_of_row(buf, next_row_after).saturating_sub(1)
329            } else {
330                buffer_byte_of_row(buf, buf.row_count())
331                    + buf
332                        .line(buf.row_count().saturating_sub(1))
333                        .map(|s| s.len())
334                        .unwrap_or(0)
335            };
336            let last_line = buf.line(last_join_row).unwrap_or_default();
337            let old_end_pos = (last_join_row as u32, last_line.len() as u32);
338            let replacement_len = if *with_space { 1 } else { 0 };
339            let new_end_byte = row_eol_byte + replacement_len;
340            let new_end_pos = (row as u32, row_eol_col + replacement_len as u32);
341            out.push(crate::types::ContentEdit {
342                start_byte: row_eol_byte,
343                old_end_byte,
344                new_end_byte,
345                start_position: (row as u32, row_eol_col),
346                old_end_position: old_end_pos,
347                new_end_position: new_end_pos,
348            });
349        }
350        B::SplitLines {
351            row,
352            cols,
353            inserted_space,
354        } => {
355            // Splits insert "\n" (or "\n " inverse) at each col on `row`.
356            // The buffer applies all splits left-to-right via the
357            // do_split_lines path; we emit one ContentEdit per col,
358            // each treated as an insert at that col on `row`. Note: the
359            // buffer state during emission is *pre-edit*, so all cols
360            // index into the same pre-edit row.
361            let row = (*row).min(buf.row_count().saturating_sub(1));
362            let line = buf.line(row).unwrap_or_default();
363            let row_byte = buffer_byte_of_row(buf, row);
364            let insert = if *inserted_space { "\n " } else { "\n" };
365            for &c in cols {
366                let pos = Position::new(row, c);
367                let col_byte = pos.byte_offset(&line);
368                let start_byte = row_byte + col_byte;
369                let start_pos = (row as u32, col_byte as u32);
370                let (new_end_byte, new_end_pos) = advance_by_text(insert, start_byte, start_pos);
371                out.push(crate::types::ContentEdit {
372                    start_byte,
373                    old_end_byte: start_byte,
374                    new_end_byte,
375                    start_position: start_pos,
376                    old_end_position: start_pos,
377                    new_end_position: new_end_pos,
378                });
379            }
380        }
381        B::InsertBlock { at, chunks } => {
382            // One ContentEdit per chunk; each lands at `(at.row + i,
383            // at.col)` in the pre-edit buffer.
384            for (i, chunk) in chunks.iter().enumerate() {
385                let pos = Position::new(at.row + i, at.col);
386                let (start_byte, start_pos) = position_to_byte_coords(buf, pos);
387                let (new_end_byte, new_end_pos) = advance_by_text(chunk, start_byte, start_pos);
388                out.push(crate::types::ContentEdit {
389                    start_byte,
390                    old_end_byte: start_byte,
391                    new_end_byte,
392                    start_position: start_pos,
393                    old_end_position: start_pos,
394                    new_end_position: new_end_pos,
395                });
396            }
397        }
398        B::DeleteBlockChunks { at, widths } => {
399            for (i, w) in widths.iter().enumerate() {
400                let row = at.row + i;
401                let start_pos = Position::new(row, at.col);
402                let end_pos = Position::new(row, at.col + *w);
403                let (sb, sp) = position_to_byte_coords(buf, start_pos);
404                let (eb, ep) = position_to_byte_coords(buf, end_pos);
405                if eb <= sb {
406                    continue;
407                }
408                out.push(crate::types::ContentEdit {
409                    start_byte: sb,
410                    old_end_byte: eb,
411                    new_end_byte: sb,
412                    start_position: sp,
413                    old_end_position: ep,
414                    new_end_position: sp,
415                });
416            }
417        }
418    }
419
420    out
421}
422
423/// Where the cursor should land in the viewport after a `z`-family
424/// scroll (`zz` / `zt` / `zb`).
425#[derive(Debug, Clone, Copy, PartialEq, Eq)]
426pub(super) enum CursorScrollTarget {
427    Center,
428    Top,
429    Bottom,
430}
431
432// ── Trait-surface cast helpers ────────────────────────────────────
433//
434// 0.0.42 (Patch C-δ.7): the helpers introduced in 0.0.41 were
435// promoted to [`crate::buf_helpers`] so `vim.rs` free fns can route
436// their reaches through the same primitives. Re-import via
437// `use` so the editor body keeps its terse call shape.
438
439use crate::buf_helpers::{
440    apply_buffer_edit, buf_cursor_pos, buf_cursor_rc, buf_cursor_row, buf_line, buf_line_chars,
441    buf_lines_to_vec, buf_row_count, buf_set_cursor_rc,
442};
443
444pub struct Editor<
445    B: crate::types::Buffer = hjkl_buffer::Buffer,
446    H: crate::types::Host = crate::types::DefaultHost,
447> {
448    pub keybinding_mode: KeybindingMode,
449    /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
450    pub last_yank: Option<String>,
451    /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
452    /// Internal — exposed via Editor accessor methods
453    /// ([`Editor::buffer_mark`], [`Editor::last_jump_back`],
454    /// [`Editor::last_edit_pos`], [`Editor::take_lsp_intent`], …).
455    pub(crate) vim: VimState,
456    /// Undo history: each entry is (lines, cursor) before the edit.
457    /// Internal — managed by [`Editor::push_undo`] / [`Editor::restore`]
458    /// / [`Editor::pop_last_undo`].
459    pub(crate) undo_stack: Vec<(Vec<String>, (usize, usize))>,
460    /// Redo history: entries pushed when undoing.
461    pub(super) redo_stack: Vec<(Vec<String>, (usize, usize))>,
462    /// Set whenever the buffer content changes; cleared by `take_dirty`.
463    pub(super) content_dirty: bool,
464    /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
465    /// so repeated `content_arc()` calls within the same un-mutated
466    /// window are free (ref-count bump instead of a full-buffer join).
467    /// Invalidated by every [`mark_content_dirty`] call.
468    pub(super) cached_content: Option<std::sync::Arc<String>>,
469    /// Last rendered viewport height (text rows only, no chrome). Written
470    /// by the draw path via [`set_viewport_height`] so the scroll helpers
471    /// can clamp the cursor to stay visible without plumbing the height
472    /// through every call.
473    pub(super) viewport_height: AtomicU16,
474    /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
475    /// goto-definition). The host app drains this each step and fires
476    /// the matching request against its own LSP client.
477    pub(super) pending_lsp: Option<LspIntent>,
478    /// Pending [`crate::types::FoldOp`]s raised by `z…` keystrokes,
479    /// the `:fold*` Ex commands, or the edit pipeline's
480    /// "edits-inside-a-fold open it" invalidation. Drained by hosts
481    /// via [`Editor::take_fold_ops`]; the engine also applies each op
482    /// locally through [`crate::buffer_impl::BufferFoldProviderMut`]
483    /// so the in-tree buffer fold storage stays in sync without host
484    /// cooperation. Introduced in 0.0.38 (Patch C-δ.4).
485    pub(super) pending_fold_ops: Vec<crate::types::FoldOp>,
486    /// Buffer storage.
487    ///
488    /// 0.1.0 (Patch C-δ): generic over `B: Buffer` per SPEC §"Editor
489    /// surface". Default `B = hjkl_buffer::Buffer`. The vim FSM body
490    /// and `Editor::mutate_edit` are concrete on `hjkl_buffer::Buffer`
491    /// for 0.1.0 — see `crate::buf_helpers::apply_buffer_edit`.
492    pub(super) buffer: B,
493    /// Engine-native style intern table. Opaque `Span::style` ids index
494    /// into this table; the render path resolves ids back to
495    /// [`crate::types::Style`]. Ratatui hosts convert at the boundary via
496    /// `hjkl_engine_tui::style_to_ratatui`. Always present — no cfg-mutex.
497    pub(super) style_table: Vec<crate::types::Style>,
498    /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
499    /// every `p` / `P` via the active selector (default unnamed).
500    /// Internal — read via [`Editor::registers`]; mutated by yank /
501    /// delete / paste FSM paths and by [`Editor::seed_yank`].
502    pub(crate) registers: crate::registers::Registers,
503    /// Per-row syntax styling in engine-native form. Always present —
504    /// populated by [`Editor::install_syntax_spans`]. Ratatui hosts use
505    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`.
506    pub styled_spans: Vec<Vec<(usize, usize, crate::types::Style)>>,
507    /// Per-editor settings tweakable via `:set`. Exposed by reference
508    /// so handlers (indent, search) read the live value rather than a
509    /// snapshot taken at startup. Read via [`Editor::settings`];
510    /// mutate via [`Editor::settings_mut`].
511    pub(crate) settings: Settings,
512    /// Unified named-marks map. Lowercase letters (`'a`–`'z`) are
513    /// per-Editor / "buffer-scope-equivalent" — set by `m{a-z}`, read
514    /// by `'{a-z}` / `` `{a-z} ``. Uppercase letters (`'A`–`'Z`) are
515    /// "file marks" that survive [`Editor::set_content`] calls so
516    /// they persist across tab swaps within the same Editor.
517    ///
518    /// 0.0.36: consolidated from three former storages:
519    /// - `hjkl_buffer::Buffer::marks` (deleted; was unused dead code).
520    /// - `vim::VimState::marks` (lowercase) (deleted).
521    /// - `Editor::file_marks` (uppercase) (replaced by this map).
522    ///
523    /// `BTreeMap` so iteration is deterministic for snapshot tests
524    /// and the `:marks` ex command. Mark-shift on edits is handled
525    /// by [`Editor::shift_marks_after_edit`].
526    pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
527    /// Block ranges (`(start_row, end_row)` inclusive) the host has
528    /// extracted from a syntax tree. `:foldsyntax` reads these to
529    /// populate folds. The host refreshes them on every re-parse via
530    /// [`Editor::set_syntax_fold_ranges`]; ex commands read them via
531    /// [`Editor::syntax_fold_ranges`].
532    pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
533    /// Pending edit log drained by [`Editor::take_changes`]. Each entry
534    /// is a SPEC [`crate::types::Edit`] mapped from the underlying
535    /// `hjkl_buffer::Edit` operation. Compound ops (JoinLines,
536    /// SplitLines, InsertBlock, DeleteBlockChunks) emit a single
537    /// best-effort EditOp covering the touched range; hosts wanting
538    /// per-cell deltas should diff their own snapshot of `lines()`.
539    /// Sealed at 0.1.0 trait extraction.
540    /// Drained by [`Editor::take_changes`].
541    pub(crate) change_log: Vec<crate::types::Edit>,
542    /// Vim's "sticky column" (curswant). `None` before the first
543    /// motion — the next vertical motion bootstraps from the live
544    /// cursor column. Horizontal motions refresh this to the new
545    /// column; vertical motions read it back so bouncing through a
546    /// shorter row doesn't drag the cursor to col 0. Hoisted out of
547    /// `hjkl_buffer::Buffer` (and `VimState`) in 0.0.28 — Editor is
548    /// the single owner now. Buffer motion methods that need it
549    /// take a `&mut Option<usize>` parameter.
550    pub(crate) sticky_col: Option<usize>,
551    /// Host adapter for clipboard, cursor-shape, time, viewport, and
552    /// search-prompt / cancellation side-channels.
553    ///
554    /// 0.1.0 (Patch C-δ): generic over `H: Host` per SPEC §"Editor
555    /// surface". Default `H = DefaultHost`. The pre-0.1.0 `EngineHost`
556    /// dyn-shim is gone — every method now dispatches through `H`'s
557    /// `Host` trait surface directly.
558    pub(crate) host: H,
559    /// Last public mode the cursor-shape emitter saw. Drives
560    /// [`Editor::emit_cursor_shape_if_changed`] so `Host::emit_cursor_shape`
561    /// fires exactly once per mode transition without sprinkling the
562    /// call across every `vim.mode = ...` site.
563    pub(crate) last_emitted_mode: crate::VimMode,
564    /// Search FSM state (pattern + per-row match cache + wrapscan).
565    /// 0.0.35: relocated out of `hjkl_buffer::Buffer` per
566    /// `DESIGN_33_METHOD_CLASSIFICATION.md` step 1.
567    /// 0.0.37: the buffer-side bridge (`Buffer::search_pattern`) is
568    /// gone; `BufferView` now takes the active regex as a `&Regex`
569    /// parameter, sourced from `Editor::search_state().pattern`.
570    pub(crate) search_state: crate::search::SearchState,
571    /// Per-row syntax span overlay. Source of truth for the host's
572    /// renderer ([`hjkl_buffer::BufferView::spans`]). Populated by
573    /// [`Editor::install_syntax_spans`] (ratatui hosts use
574    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`)
575    /// and, in due course, by `Host::syntax_highlights` once the engine
576    /// drives that path directly.
577    ///
578    /// 0.0.37: lifted out of `hjkl_buffer::Buffer` per step 3 of
579    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer-side cache +
580    /// `Buffer::set_spans` / `Buffer::spans` accessors are gone.
581    pub(crate) buffer_spans: Vec<Vec<hjkl_buffer::Span>>,
582    /// Pending `ContentEdit` records emitted by `mutate_edit`. Drained by
583    /// hosts via [`Editor::take_content_edits`] for fan-in to a syntax
584    /// tree (or any other content-change observer that needs byte-level
585    /// position deltas). Edges are byte-indexed and `(row, col_byte)`.
586    pub(crate) pending_content_edits: Vec<crate::types::ContentEdit>,
587    /// Pending "reset" flag set when the entire buffer is replaced
588    /// (e.g. `set_content` / `restore`). Supersedes any queued
589    /// `pending_content_edits` on the same frame: hosts call
590    /// [`Editor::take_content_reset`] before draining edits.
591    pub(crate) pending_content_reset: bool,
592    /// Row range touched by the most recent `auto_indent_rows` call.
593    /// `(top_row, bot_row)` inclusive. Set by the engine after every
594    /// auto-indent operation; drained (and cleared) by the host via
595    /// [`Editor::take_last_indent_range`] so it can display a brief
596    /// visual flash over the reindented rows.
597    pub(crate) last_indent_range: Option<(usize, usize)>,
598}
599
600/// Vim-style options surfaced by `:set`. New fields land here as
601/// individual ex commands gain `:set` plumbing.
602#[derive(Debug, Clone)]
603pub struct Settings {
604    /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
605    pub shiftwidth: usize,
606    /// Visual width of a `\t` character. Stored for future render
607    /// hookup; not yet consumed by the buffer renderer.
608    pub tabstop: usize,
609    /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
610    /// without an explicit `i` flag.
611    pub ignore_case: bool,
612    /// When true *and* `ignore_case` is true, an uppercase letter in
613    /// the pattern flips that search back to case-sensitive. Matches
614    /// vim's `:set smartcase`. Default `false`.
615    pub smartcase: bool,
616    /// Wrap searches past buffer ends. Matches vim's `:set wrapscan`.
617    /// Default `true`.
618    pub wrapscan: bool,
619    /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
620    pub textwidth: usize,
621    /// When `true`, the Tab key in insert mode inserts `tabstop` spaces
622    /// instead of a literal `\t`. Matches vim's `:set expandtab`.
623    /// Default `false`.
624    pub expandtab: bool,
625    /// Soft tab stop in spaces. When `> 0`, Tab inserts spaces to the
626    /// next softtabstop boundary (when `expandtab`), and Backspace at the
627    /// end of a softtabstop-aligned space run deletes the entire run as
628    /// if it were one tab. `0` disables. Matches vim's `:set softtabstop`.
629    pub softtabstop: usize,
630    /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
631    /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
632    /// past the right edge and `top_col` clips the left side.
633    /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
634    /// to word-break wrap; `:set nowrap` resets.
635    pub wrap: hjkl_buffer::Wrap,
636    /// When true, the engine drops every edit before it touches the
637    /// buffer — undo, dirty flag, and change log all stay clean.
638    /// Matches vim's `:set readonly` / `:set ro`. Default `false`.
639    pub readonly: bool,
640    /// When `true`, pressing Enter in insert mode copies the leading
641    /// whitespace of the current line onto the new line. Matches vim's
642    /// `:set autoindent`. Default `true` (vim parity).
643    pub autoindent: bool,
644    /// When `true`, bumps indent by one `shiftwidth` after a line ending
645    /// in `{` / `(` / `[`, and strips one indent unit when the user types
646    /// `}` / `)` / `]` on a whitespace-only line. See `compute_enter_indent`
647    /// in `vim.rs` for the tree-sitter plug-in seam. Default `true`.
648    pub smartindent: bool,
649    /// Cap on undo-stack length. Older entries are pruned past this
650    /// bound. `0` means unlimited. Matches vim's `:set undolevels`.
651    /// Default `1000`.
652    pub undo_levels: u32,
653    /// When `true`, cursor motions inside insert mode break the
654    /// current undo group (so a single `u` only reverses the run of
655    /// keystrokes that preceded the motion). Default `true`.
656    /// Currently a no-op — engine doesn't yet break the undo group
657    /// on insert-mode motions; field is wired through `:set
658    /// undobreak` for forward compatibility.
659    pub undo_break_on_motion: bool,
660    /// Vim-flavoured "what counts as a word" character class.
661    /// Comma-separated tokens: `@` = `is_alphabetic()`, `_` = literal
662    /// `_`, `48-57` = decimal char range, bare integer = single char
663    /// code, single ASCII punctuation = literal. Default
664    /// `"@,48-57,_,192-255"` matches vim.
665    pub iskeyword: String,
666    /// Multi-key sequence timeout (e.g. `gg`, `dd`). When the user
667    /// pauses longer than this between keys, any pending prefix is
668    /// abandoned and the next key starts a fresh sequence. Matches
669    /// vim's `:set timeoutlen` / `:set tm` (millis). Default 1000ms.
670    pub timeout_len: core::time::Duration,
671    /// When true, render absolute line numbers in the gutter. Matches
672    /// vim's `:set number` / `:set nu`. Default `true`.
673    pub number: bool,
674    /// When true, render line numbers as offsets from the cursor row.
675    /// Combined with `number`, the cursor row shows its absolute number
676    /// while other rows show the relative offset (vim's `nu+rnu` hybrid).
677    /// Matches vim's `:set relativenumber` / `:set rnu`. Default `false`.
678    pub relativenumber: bool,
679    /// Minimum gutter width in cells for the line-number column.
680    /// Width grows past this to fit the largest displayed number.
681    /// Matches vim's `:set numberwidth` / `:set nuw`. Default `4`.
682    /// Range 1..=20.
683    pub numberwidth: usize,
684    /// Highlight the row where the cursor sits. Matches vim's `:set cursorline`.
685    /// Default `false`.
686    pub cursorline: bool,
687    /// Highlight the column where the cursor sits. Matches vim's `:set cursorcolumn`.
688    /// Default `false`.
689    pub cursorcolumn: bool,
690    /// Sign-column display mode. Matches vim's `:set signcolumn`.
691    /// Default [`crate::types::SignColumnMode::Auto`].
692    pub signcolumn: crate::types::SignColumnMode,
693    /// Number of cells reserved for a fold-marker gutter.
694    /// Matches vim's `:set foldcolumn`. Default `0`.
695    pub foldcolumn: u32,
696    /// Comma-separated 1-based column indices for vertical rulers.
697    /// Matches vim's `:set colorcolumn`. Default `""`.
698    pub colorcolumn: String,
699}
700
701impl Default for Settings {
702    fn default() -> Self {
703        Self {
704            shiftwidth: 4,
705            tabstop: 4,
706            softtabstop: 4,
707            ignore_case: false,
708            smartcase: false,
709            wrapscan: true,
710            textwidth: 79,
711            expandtab: true,
712            wrap: hjkl_buffer::Wrap::None,
713            readonly: false,
714            autoindent: true,
715            smartindent: true,
716            undo_levels: 1000,
717            undo_break_on_motion: true,
718            iskeyword: "@,48-57,_,192-255".to_string(),
719            timeout_len: core::time::Duration::from_millis(1000),
720            number: true,
721            relativenumber: false,
722            numberwidth: 4,
723            cursorline: false,
724            cursorcolumn: false,
725            signcolumn: crate::types::SignColumnMode::Auto,
726            foldcolumn: 0,
727            colorcolumn: String::new(),
728        }
729    }
730}
731
732/// Translate a SPEC [`crate::types::Options`] into the engine's
733/// internal [`Settings`] representation. Field-by-field map; the
734/// shapes are isomorphic except for type widths
735/// (`u32` vs `usize`, [`crate::types::WrapMode`] vs
736/// [`hjkl_buffer::Wrap`]). 0.1.0 (Patch C-δ) collapses both into one
737/// type once the `Editor<B, H>::new(buffer, host, options)` constructor
738/// is the canonical entry point.
739fn settings_from_options(o: &crate::types::Options) -> Settings {
740    Settings {
741        shiftwidth: o.shiftwidth as usize,
742        tabstop: o.tabstop as usize,
743        softtabstop: o.softtabstop as usize,
744        ignore_case: o.ignorecase,
745        smartcase: o.smartcase,
746        wrapscan: o.wrapscan,
747        textwidth: o.textwidth as usize,
748        expandtab: o.expandtab,
749        wrap: match o.wrap {
750            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
751            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
752            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
753        },
754        readonly: o.readonly,
755        autoindent: o.autoindent,
756        smartindent: o.smartindent,
757        undo_levels: o.undo_levels,
758        undo_break_on_motion: o.undo_break_on_motion,
759        iskeyword: o.iskeyword.clone(),
760        timeout_len: o.timeout_len,
761        number: o.number,
762        relativenumber: o.relativenumber,
763        numberwidth: o.numberwidth,
764        cursorline: o.cursorline,
765        cursorcolumn: o.cursorcolumn,
766        signcolumn: o.signcolumn,
767        foldcolumn: o.foldcolumn,
768        colorcolumn: o.colorcolumn.clone(),
769    }
770}
771
772/// Host-observable LSP requests triggered by editor bindings. The
773/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
774/// intent that the TUI layer picks up and routes to `sqls`.
775#[derive(Debug, Clone, Copy, PartialEq, Eq)]
776pub enum LspIntent {
777    /// `gd` — textDocument/definition at the cursor.
778    GotoDefinition,
779}
780
781impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
782    /// Build an [`Editor`] from a buffer, host adapter, and SPEC options.
783    ///
784    /// 0.1.0 (Patch C-δ): canonical, frozen constructor per SPEC §"Editor
785    /// surface". Replaces the pre-0.1.0 `Editor::new(KeybindingMode)` /
786    /// `with_host` / `with_options` triad — there is no shim.
787    ///
788    /// Consumers that don't need a custom host pass
789    /// [`crate::types::DefaultHost::new()`]; consumers that don't need
790    /// custom options pass [`crate::types::Options::default()`].
791    pub fn new(buffer: hjkl_buffer::Buffer, host: H, options: crate::types::Options) -> Self {
792        let settings = settings_from_options(&options);
793        Self {
794            keybinding_mode: KeybindingMode::Vim,
795            last_yank: None,
796            vim: VimState::default(),
797            undo_stack: Vec::new(),
798            redo_stack: Vec::new(),
799            content_dirty: false,
800            cached_content: None,
801            viewport_height: AtomicU16::new(0),
802            pending_lsp: None,
803            pending_fold_ops: Vec::new(),
804            buffer,
805            style_table: Vec::new(),
806            registers: crate::registers::Registers::default(),
807            styled_spans: Vec::new(),
808            settings,
809            marks: std::collections::BTreeMap::new(),
810            syntax_fold_ranges: Vec::new(),
811            change_log: Vec::new(),
812            sticky_col: None,
813            host,
814            last_emitted_mode: crate::VimMode::Normal,
815            search_state: crate::search::SearchState::new(),
816            buffer_spans: Vec::new(),
817            pending_content_edits: Vec::new(),
818            pending_content_reset: false,
819            last_indent_range: None,
820        }
821    }
822}
823
824impl<B: crate::types::Buffer, H: crate::types::Host> Editor<B, H> {
825    /// Borrow the buffer (typed `&B`). Host renders through this via
826    /// `hjkl_buffer::BufferView` when `B = hjkl_buffer::Buffer`.
827    pub fn buffer(&self) -> &B {
828        &self.buffer
829    }
830
831    /// Mutably borrow the buffer (typed `&mut B`).
832    pub fn buffer_mut(&mut self) -> &mut B {
833        &mut self.buffer
834    }
835
836    /// Borrow the host adapter directly (typed `&H`).
837    pub fn host(&self) -> &H {
838        &self.host
839    }
840
841    /// Mutably borrow the host adapter (typed `&mut H`).
842    pub fn host_mut(&mut self) -> &mut H {
843        &mut self.host
844    }
845}
846
847impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
848    /// Update the active `iskeyword` spec for word motions
849    /// (`w`/`b`/`e`/`ge` and engine-side `*`/`#` pickup). 0.0.28
850    /// hoisted iskeyword storage out of `Buffer` — `Editor` is the
851    /// single owner now. Equivalent to assigning
852    /// `settings_mut().iskeyword` directly; the dedicated setter is
853    /// retained for source-compatibility with 0.0.27 callers.
854    pub fn set_iskeyword(&mut self, spec: impl Into<String>) {
855        self.settings.iskeyword = spec.into();
856    }
857
858    /// Emit `Host::emit_cursor_shape` if the public mode has changed
859    /// since the last emit. Engine calls this at the end of every input
860    /// step so mode transitions surface to the host without sprinkling
861    /// the call across every `vim.mode = ...` site.
862    pub fn emit_cursor_shape_if_changed(&mut self) {
863        let mode = self.vim_mode();
864        if mode == self.last_emitted_mode {
865            return;
866        }
867        let shape = match mode {
868            crate::VimMode::Insert => crate::types::CursorShape::Bar,
869            _ => crate::types::CursorShape::Block,
870        };
871        self.host.emit_cursor_shape(shape);
872        self.last_emitted_mode = mode;
873    }
874
875    /// Record a yank/cut payload. Writes both the legacy
876    /// [`Editor::last_yank`] field (drained directly by 0.0.28-era
877    /// hosts) and the new [`crate::types::Host::write_clipboard`]
878    /// side-channel (Patch B). Consumers should migrate to a `Host`
879    /// impl whose `write_clipboard` queues the platform-clipboard
880    /// write; the `last_yank` mirror will be removed at 0.1.0.
881    pub(crate) fn record_yank_to_host(&mut self, text: String) {
882        self.host.write_clipboard(text.clone());
883        self.last_yank = Some(text);
884    }
885
886    /// Vim's sticky column (curswant). `None` before the first motion;
887    /// hosts shouldn't normally need to read this directly — it's
888    /// surfaced for migration off `Buffer::sticky_col` and for
889    /// snapshot tests.
890    pub fn sticky_col(&self) -> Option<usize> {
891        self.sticky_col
892    }
893
894    /// Replace the sticky column. Hosts should rarely touch this —
895    /// motion code maintains it through the standard horizontal /
896    /// vertical motion paths.
897    pub fn set_sticky_col(&mut self, col: Option<usize>) {
898        self.sticky_col = col;
899    }
900
901    /// Host hook: replace the cached syntax-derived block ranges that
902    /// `:foldsyntax` consumes. the host calls this on every re-parse;
903    /// the cost is just a `Vec` swap.
904    /// Look up a named mark by character. Returns `(row, col)` if
905    /// set; `None` otherwise. Both lowercase (`'a`–`'z`) and
906    /// uppercase (`'A`–`'Z`) marks live in the same unified
907    /// [`Editor::marks`] map as of 0.0.36.
908    pub fn mark(&self, c: char) -> Option<(usize, usize)> {
909        self.marks.get(&c).copied()
910    }
911
912    /// Set the named mark `c` to `(row, col)`. Used by the FSM's
913    /// `m{a-zA-Z}` keystroke and by [`Editor::restore_snapshot`].
914    pub fn set_mark(&mut self, c: char, pos: (usize, usize)) {
915        self.marks.insert(c, pos);
916    }
917
918    /// Remove the named mark `c` (no-op if unset).
919    pub fn clear_mark(&mut self, c: char) {
920        self.marks.remove(&c);
921    }
922
923    /// Look up a buffer-local lowercase mark (`'a`–`'z`). Kept as a
924    /// thin wrapper over [`Editor::mark`] for source compatibility
925    /// with pre-0.0.36 callers; new code should call
926    /// [`Editor::mark`] directly.
927    #[deprecated(
928        since = "0.0.36",
929        note = "use Editor::mark — lowercase + uppercase marks now live in a single map"
930    )]
931    pub fn buffer_mark(&self, c: char) -> Option<(usize, usize)> {
932        self.mark(c)
933    }
934
935    /// Discard the most recent undo entry. Used by ex commands that
936    /// pre-emptively pushed an undo state (`:s`, `:r`) but ended up
937    /// matching nothing — popping prevents a no-op undo step from
938    /// polluting the user's history.
939    ///
940    /// Returns `true` if an entry was discarded.
941    pub fn pop_last_undo(&mut self) -> bool {
942        self.undo_stack.pop().is_some()
943    }
944
945    /// Read all named marks set this session — both lowercase
946    /// (`'a`–`'z`) and uppercase (`'A`–`'Z`). Iteration is
947    /// deterministic (BTreeMap-ordered) so snapshot / `:marks`
948    /// output is stable.
949    pub fn marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
950        self.marks.iter().map(|(c, p)| (*c, *p))
951    }
952
953    /// Read all buffer-local lowercase marks. Kept for source
954    /// compatibility with pre-0.0.36 callers (e.g. `:marks` ex
955    /// command); new code should use [`Editor::marks`] which
956    /// iterates the unified map.
957    #[deprecated(
958        since = "0.0.36",
959        note = "use Editor::marks — lowercase + uppercase marks now live in a single map"
960    )]
961    pub fn buffer_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
962        self.marks
963            .iter()
964            .filter(|(c, _)| c.is_ascii_lowercase())
965            .map(|(c, p)| (*c, *p))
966    }
967
968    /// Position the cursor was at when the user last jumped via
969    /// `<C-o>` / `g;` / similar. `None` before any jump.
970    pub fn last_jump_back(&self) -> Option<(usize, usize)> {
971        self.vim.jump_back.last().copied()
972    }
973
974    /// Position of the last edit (where `.` would replay). `None` if
975    /// no edit has happened yet in this session.
976    pub fn last_edit_pos(&self) -> Option<(usize, usize)> {
977        self.vim.last_edit_pos
978    }
979
980    /// Read-only view of the file-marks table — uppercase / "file"
981    /// marks (`'A`–`'Z`) the host has set this session. Returns an
982    /// iterator of `(mark_char, (row, col))` pairs.
983    ///
984    /// Mutate via the FSM (`m{A-Z}` keystroke) or via
985    /// [`Editor::restore_snapshot`].
986    ///
987    /// 0.0.36: file marks now live in the unified [`Editor::marks`]
988    /// map; this accessor is kept for source compatibility and
989    /// filters the unified map to uppercase entries.
990    pub fn file_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
991        self.marks
992            .iter()
993            .filter(|(c, _)| c.is_ascii_uppercase())
994            .map(|(c, p)| (*c, *p))
995    }
996
997    /// Read-only view of the cached syntax-derived block ranges that
998    /// `:foldsyntax` consumes. Returns the slice the host last
999    /// installed via [`Editor::set_syntax_fold_ranges`]; empty when
1000    /// no syntax integration is active.
1001    pub fn syntax_fold_ranges(&self) -> &[(usize, usize)] {
1002        &self.syntax_fold_ranges
1003    }
1004
1005    pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
1006        self.syntax_fold_ranges = ranges;
1007    }
1008
1009    /// Live settings (read-only). `:set` mutates these via
1010    /// [`Editor::settings_mut`].
1011    pub fn settings(&self) -> &Settings {
1012        &self.settings
1013    }
1014
1015    /// Live settings (mutable). `:set` flows through here to mutate
1016    /// shiftwidth / tabstop / textwidth / ignore_case / wrap. Hosts
1017    /// configuring at startup typically construct a [`Settings`]
1018    /// snapshot and overwrite via `*editor.settings_mut() = …`.
1019    pub fn settings_mut(&mut self) -> &mut Settings {
1020        &mut self.settings
1021    }
1022
1023    /// Returns `true` when `:set readonly` is active. Convenience
1024    /// accessor for hosts that cannot import the internal [`Settings`]
1025    /// type. Phase 5 binary uses this to gate `:w` writes.
1026    pub fn is_readonly(&self) -> bool {
1027        self.settings.readonly
1028    }
1029
1030    /// Borrow the engine search state. Hosts inspecting the
1031    /// committed `/` / `?` pattern (e.g. for status-line display) or
1032    /// feeding the active regex into `BufferView::search_pattern`
1033    /// read it from here.
1034    pub fn search_state(&self) -> &crate::search::SearchState {
1035        &self.search_state
1036    }
1037
1038    /// Mutable engine search state. Hosts driving search
1039    /// programmatically (test fixtures, scripted demos) write the
1040    /// pattern through here.
1041    pub fn search_state_mut(&mut self) -> &mut crate::search::SearchState {
1042        &mut self.search_state
1043    }
1044
1045    /// Install `pattern` as the active search regex on the engine
1046    /// state and clear the cached row matches. Pass `None` to clear.
1047    /// 0.0.37: dropped the buffer-side mirror that 0.0.35 introduced
1048    /// — `BufferView` now takes the regex through its `search_pattern`
1049    /// field per step 3 of `DESIGN_33_METHOD_CLASSIFICATION.md`.
1050    pub fn set_search_pattern(&mut self, pattern: Option<regex::Regex>) {
1051        self.search_state.set_pattern(pattern);
1052    }
1053
1054    /// Drive `n` (or the `/` commit equivalent) — advance the cursor
1055    /// to the next match of `search_state.pattern` from the cursor's
1056    /// current position. Returns `true` when a match was found.
1057    /// `skip_current = true` excludes a match the cursor sits on.
1058    pub fn search_advance_forward(&mut self, skip_current: bool) -> bool {
1059        crate::search::search_forward(&mut self.buffer, &mut self.search_state, skip_current)
1060    }
1061
1062    /// Drive `N` — symmetric counterpart of [`Editor::search_advance_forward`].
1063    pub fn search_advance_backward(&mut self, skip_current: bool) -> bool {
1064        crate::search::search_backward(&mut self.buffer, &mut self.search_state, skip_current)
1065    }
1066
1067    /// Snapshot of the unnamed register (the default `p` / `P` source).
1068    pub fn yank(&self) -> &str {
1069        &self.registers.unnamed.text
1070    }
1071
1072    /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
1073    pub fn registers(&self) -> &crate::registers::Registers {
1074        &self.registers
1075    }
1076
1077    /// Mutably borrow the full register bank. Hosts that share registers
1078    /// across multiple editors (e.g. multi-buffer `yy` / `p`) overwrite
1079    /// the slots here on buffer switch.
1080    pub fn registers_mut(&mut self) -> &mut crate::registers::Registers {
1081        &mut self.registers
1082    }
1083
1084    /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
1085    /// register slot. the host calls this before letting vim consume a
1086    /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
1087    /// stale snapshot from the last yank.
1088    pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
1089        self.registers.set_clipboard(text, linewise);
1090    }
1091
1092    /// Return the user's pending register selection (set via `"<reg>` chord
1093    /// before an operator). `None` if no register was selected — caller should
1094    /// use the unnamed register `"`.
1095    ///
1096    /// Read-only — does not consume / clear the pending selection. The
1097    /// register is cleared by the engine after the next operator fires.
1098    ///
1099    /// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
1100    /// honor `"a` + visual op chord sequences.
1101    pub fn pending_register(&self) -> Option<char> {
1102        self.vim.pending_register
1103    }
1104
1105    /// True when the user's pending register selector is `+` or `*`.
1106    /// the host peeks this so it can refresh `sync_clipboard_register`
1107    /// only when a clipboard read is actually about to happen.
1108    pub fn pending_register_is_clipboard(&self) -> bool {
1109        matches!(self.vim.pending_register, Some('+') | Some('*'))
1110    }
1111
1112    /// Register currently being recorded into via `q{reg}`. `None` when
1113    /// no recording is active. Hosts use this to surface a "recording @r"
1114    /// indicator in the status line.
1115    pub fn recording_register(&self) -> Option<char> {
1116        self.vim.recording_macro
1117    }
1118
1119    /// Pending repeat count the user has typed but not yet resolved
1120    /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1121    /// Hosts surface this in a "showcmd" area.
1122    pub fn pending_count(&self) -> Option<u32> {
1123        self.vim.pending_count_val()
1124    }
1125
1126    /// The operator character for any in-flight operator that is waiting
1127    /// for a motion (e.g. `d` after the user types `d` but before a
1128    /// motion). Returns `None` when no operator is pending.
1129    pub fn pending_op(&self) -> Option<char> {
1130        self.vim.pending_op_char()
1131    }
1132
1133    /// `true` when the engine is in any pending chord state — waiting for
1134    /// the next key to complete a command (e.g. `r<char>` replace,
1135    /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1136    /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1137    /// register selection `"<reg>`, macro recording target, etc).
1138    ///
1139    /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1140    /// and forward keys directly to the engine so in-flight commands can
1141    /// complete without the host eating their continuation keys.
1142    pub fn is_chord_pending(&self) -> bool {
1143        self.vim.is_chord_pending()
1144    }
1145
1146    /// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
1147    /// is waiting for the next typed character to name the register to paste.
1148    /// The dispatcher should call `insert_paste_register(c)` instead of
1149    /// `insert_char(c)` for the next printable key, then the flag auto-clears.
1150    ///
1151    /// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
1152    /// without having to drive the full FSM.
1153    pub fn is_insert_register_pending(&self) -> bool {
1154        self.vim.insert_pending_register
1155    }
1156
1157    /// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
1158    /// before `insert_paste_register(c)` in app-level dispatchers so that the
1159    /// flag does not persist into the next key. Call before
1160    /// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
1161    ///
1162    /// Phase 6.5: used by `dispatch_insert_key` in the app crate.
1163    pub fn clear_insert_register_pending(&mut self) {
1164        self.vim.insert_pending_register = false;
1165    }
1166
1167    /// Read-only view of the jump-back list (positions pushed on "big"
1168    /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1169    #[allow(clippy::type_complexity)]
1170    pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1171        (&self.vim.jump_back, &self.vim.jump_fwd)
1172    }
1173
1174    /// Read-only view of the change list (positions of recent edits) plus
1175    /// the current walk cursor. Newest entry is at the back.
1176    pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1177        (&self.vim.change_list, self.vim.change_list_cursor)
1178    }
1179
1180    /// Replace the unnamed register without touching any other slot.
1181    /// For host-driven imports (e.g. system clipboard); operator
1182    /// code uses [`record_yank`] / [`record_delete`].
1183    pub fn set_yank(&mut self, text: impl Into<String>) {
1184        let text = text.into();
1185        let linewise = self.vim.yank_linewise;
1186        self.registers.unnamed = crate::registers::Slot { text, linewise };
1187    }
1188
1189    /// Record a yank into `"` and `"0`, plus the named target if the
1190    /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1191    /// paste path.
1192    pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1193        self.vim.yank_linewise = linewise;
1194        let target = self.vim.pending_register.take();
1195        self.registers.record_yank(text, linewise, target);
1196    }
1197
1198    /// Direct write to a named register slot — bypasses the unnamed
1199    /// `"` and `"0` updates that `record_yank` does. Used by the
1200    /// macro recorder so finishing a `q{reg}` recording doesn't
1201    /// pollute the user's last yank.
1202    pub fn set_named_register_text(&mut self, reg: char, text: String) {
1203        if let Some(slot) = match reg {
1204            'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1205            'A'..='Z' => {
1206                Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1207            }
1208            _ => None,
1209        } {
1210            slot.text = text;
1211            slot.linewise = false;
1212        }
1213    }
1214
1215    /// Record a delete / change into `"` and the `"1`–`"9` ring.
1216    /// Honours the active named-register prefix.
1217    pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1218        self.vim.yank_linewise = linewise;
1219        let target = self.vim.pending_register.take();
1220        self.registers.record_delete(text, linewise, target);
1221    }
1222
1223    /// Install styled syntax spans using the engine-native
1224    /// [`crate::types::Style`]. Always available — engine is ratatui-free.
1225    /// Ratatui hosts use
1226    /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`
1227    /// which converts at the boundary and delegates here.
1228    ///
1229    /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1230    /// 0.1.0 freeze the unprefixed name is the universally-available
1231    /// engine-native variant.
1232    pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1233        let line_byte_lens: Vec<usize> = (0..buf_row_count(&self.buffer))
1234            .map(|r| buf_line(&self.buffer, r).map(|s| s.len()).unwrap_or(0))
1235            .collect();
1236        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1237        let mut engine_spans: Vec<Vec<(usize, usize, crate::types::Style)>> =
1238            Vec::with_capacity(spans.len());
1239        for (row, row_spans) in spans.iter().enumerate() {
1240            let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
1241            let mut translated = Vec::with_capacity(row_spans.len());
1242            let mut translated_e = Vec::with_capacity(row_spans.len());
1243            for (start, end, style) in row_spans {
1244                let end_clamped = (*end).min(line_len);
1245                if end_clamped <= *start {
1246                    continue;
1247                }
1248                let id = self.intern_style(*style);
1249                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1250                translated_e.push((*start, end_clamped, *style));
1251            }
1252            by_row.push(translated);
1253            engine_spans.push(translated_e);
1254        }
1255        self.buffer_spans = by_row;
1256        self.styled_spans = engine_spans;
1257    }
1258
1259    /// Read-only view of the style table in engine-native form —
1260    /// id `i` → `style_table[i]`. Always available, no cfg gate.
1261    ///
1262    /// Ratatui hosts that need a `ratatui::style::Style` slice should
1263    /// use `hjkl_engine_tui::EditorRatatuiExt::ratatui_style_table` or
1264    /// convert individual entries via `hjkl_engine_tui::style_to_ratatui`.
1265    pub fn style_table(&self) -> &[crate::types::Style] {
1266        &self.style_table
1267    }
1268
1269    /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1270    /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1271    /// per draw frame.
1272    ///
1273    /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1274    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1275    /// caches spans; they live on the engine and route through the
1276    /// `Host::syntax_highlights` pipeline.
1277    pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1278        &self.buffer_spans
1279    }
1280
1281    /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1282    /// Engine-native — the unified `style_table` is always engine-native.
1283    /// Linear-scan dedup — the table grows only as new tree-sitter token
1284    /// kinds appear, so it stays tiny. Ratatui callers use
1285    /// `hjkl_engine_tui::EditorRatatuiExt::intern_ratatui_style` which
1286    /// converts at the boundary and delegates here.
1287    ///
1288    /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1289    /// the unprefixed name is the universally-available engine-native
1290    /// variant.
1291    pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1292        if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1293            return idx as u32;
1294        }
1295        self.style_table.push(style);
1296        (self.style_table.len() - 1) as u32
1297    }
1298
1299    /// Look up an interned style by id and return it as a SPEC
1300    /// [`crate::types::Style`]. Returns `None` for ids past the end
1301    /// of the table.
1302    pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1303        self.style_table.get(id as usize).copied()
1304    }
1305
1306    /// Historical reverse-sync hook from when the textarea mirrored
1307    /// the buffer. Now that Buffer is the cursor authority this is a
1308    /// no-op; call sites can remain in place during the migration.
1309    pub fn push_buffer_cursor_to_textarea(&mut self) {}
1310
1311    /// Force the host viewport's top row without touching the
1312    /// cursor. Used by tests that simulate a scroll without the
1313    /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1314    /// apply.
1315    ///
1316    /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1317    /// instead of the (now-deleted) `Buffer::viewport_mut`.
1318    pub fn set_viewport_top(&mut self, row: usize) {
1319        let last = buf_row_count(&self.buffer).saturating_sub(1);
1320        let target = row.min(last);
1321        self.host.viewport_mut().top_row = target;
1322    }
1323
1324    /// Set the cursor to `(row, col)`, clamped to the buffer's
1325    /// content. Hosts use this for goto-line, jump-to-mark, and
1326    /// programmatic cursor placement.
1327    ///
1328    /// Resets `sticky_col` (curswant) to `col` — every explicit jump
1329    /// (goto-line, jump-to-mark, search hit, click, `]d`) follows vim
1330    /// semantics. Only `j`/`k`/`+`/`-` READ `sticky_col`; everything
1331    /// else resets it to the column where the cursor actually landed.
1332    pub fn jump_cursor(&mut self, row: usize, col: usize) {
1333        buf_set_cursor_rc(&mut self.buffer, row, col);
1334        self.sticky_col = Some(col);
1335    }
1336
1337    /// Set the cursor to `(row, col)` without modifying `sticky_col`.
1338    ///
1339    /// Use this for host-side state restores (viewport sync, snapshot
1340    /// replay) where the cursor was already at this position semantically
1341    /// and the host's sticky tracking should remain authoritative.
1342    ///
1343    /// For user-facing jumps (goto-line, search hit, picker `<CR>`, `]d`,
1344    /// click), use [`Editor::jump_cursor`] which DOES reset `sticky_col`
1345    /// per vim curswant semantics.
1346    pub fn set_cursor_quiet(&mut self, row: usize, col: usize) {
1347        buf_set_cursor_rc(&mut self.buffer, row, col);
1348    }
1349
1350    /// `(row, col)` cursor read sourced from the migration buffer.
1351    /// Equivalent to `self.textarea.cursor()` when the two are in
1352    /// sync — which is the steady state during Phase 7f because
1353    /// every step opens with `sync_buffer_content_from_textarea` and
1354    /// every ported motion pushes the result back. Prefer this over
1355    /// `self.textarea.cursor()` so call sites keep working unchanged
1356    /// once the textarea field is ripped.
1357    pub fn cursor(&self) -> (usize, usize) {
1358        buf_cursor_rc(&self.buffer)
1359    }
1360
1361    /// Drain any pending LSP intent raised by the last key. Returns
1362    /// `None` when no intent is armed.
1363    pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1364        self.pending_lsp.take()
1365    }
1366
1367    /// Drain every [`crate::types::FoldOp`] raised since the last
1368    /// call. Hosts that mirror the engine's fold storage (or that
1369    /// project folds onto a separate fold tree, LSP folding ranges,
1370    /// …) drain this each step and dispatch as their own
1371    /// [`crate::types::Host::Intent`] requires.
1372    ///
1373    /// The engine has already applied every op locally against the
1374    /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1375    /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1376    /// don't track folds independently can ignore the queue
1377    /// (or simply never call this drain).
1378    ///
1379    /// Introduced in 0.0.38 (Patch C-δ.4).
1380    pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1381        std::mem::take(&mut self.pending_fold_ops)
1382    }
1383
1384    /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1385    /// surface: queue it for host observation (drained by
1386    /// [`Editor::take_fold_ops`]) and apply it locally against the
1387    /// in-tree buffer fold storage via
1388    /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1389    /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1390    /// invalidation) route every fold mutation through this method.
1391    ///
1392    /// Introduced in 0.0.38 (Patch C-δ.4).
1393    pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1394        use crate::types::FoldProvider;
1395        self.pending_fold_ops.push(op);
1396        let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1397        provider.apply(op);
1398    }
1399
1400    /// Refresh the host viewport's height from the cached
1401    /// `viewport_height_value()`. Called from the per-step
1402    /// boilerplate; was the textarea → buffer mirror before Phase 7f
1403    /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1404    /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1405    /// `Host::viewport_mut`.
1406    pub fn sync_buffer_from_textarea(&mut self) {
1407        let height = self.viewport_height_value();
1408        self.host.viewport_mut().height = height;
1409    }
1410
1411    /// Was the full textarea → buffer content sync. Buffer is the
1412    /// content authority now; this remains as a no-op so the per-step
1413    /// call sites don't have to be ripped in the same patch.
1414    pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1415        self.sync_buffer_from_textarea();
1416    }
1417
1418    /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1419    /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1420    /// the cursor without going through the vim engine's motion
1421    /// machinery, where push_jump fires automatically.
1422    pub fn record_jump(&mut self, pos: (usize, usize)) {
1423        const JUMPLIST_MAX: usize = 100;
1424        self.vim.jump_back.push(pos);
1425        if self.vim.jump_back.len() > JUMPLIST_MAX {
1426            self.vim.jump_back.remove(0);
1427        }
1428        self.vim.jump_fwd.clear();
1429    }
1430
1431    /// Host apps call this each draw with the current text area height so
1432    /// scroll helpers can clamp the cursor without recomputing layout.
1433    pub fn set_viewport_height(&self, height: u16) {
1434        self.viewport_height.store(height, Ordering::Relaxed);
1435    }
1436
1437    /// Last height published by `set_viewport_height` (in rows).
1438    pub fn viewport_height_value(&self) -> u16 {
1439        self.viewport_height.load(Ordering::Relaxed)
1440    }
1441
1442    /// Apply `edit` against the buffer and return the inverse so the
1443    /// host can push it onto an undo stack. Side effects: dirty
1444    /// flag, change-list ring, mark / jump-list shifts, change_log
1445    /// append, fold invalidation around the touched rows.
1446    ///
1447    /// The primary edit funnel — both FSM operators and ex commands
1448    /// route mutations through here so the side effects fire
1449    /// uniformly.
1450    pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1451        // `:set readonly` short-circuits every mutation funnel: no
1452        // buffer change, no dirty flag, no undo entry, no change-log
1453        // emission. We swallow the requested `edit` and hand back a
1454        // self-inverse no-op (`InsertStr` of an empty string at the
1455        // current cursor) so callers that push the return value onto
1456        // an undo stack still get a structurally valid round trip.
1457        if self.settings.readonly {
1458            let _ = edit;
1459            return hjkl_buffer::Edit::InsertStr {
1460                at: buf_cursor_pos(&self.buffer),
1461                text: String::new(),
1462            };
1463        }
1464        let pre_row = buf_cursor_row(&self.buffer);
1465        let pre_rows = buf_row_count(&self.buffer);
1466        // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1467        // Vim's `:h '.` says "the position where the last change was made",
1468        // meaning the change-start, not the post-insert cursor. We snap it
1469        // here before `apply_buffer_edit` moves the cursor.
1470        let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1471        // Map the underlying buffer edit to a SPEC EditOp for
1472        // change-log emission before consuming it. Coarse — see
1473        // change_log field doc on the struct.
1474        self.change_log.extend(edit_to_editops(&edit));
1475        // Compute ContentEdit fan-out from the pre-edit buffer state.
1476        // Done before `apply_buffer_edit` consumes `edit` so we can
1477        // inspect the operation's fields and the buffer's pre-edit row
1478        // bytes (needed for byte_of_row / col_byte conversion). Edits
1479        // are pushed onto `pending_content_edits` for host drain.
1480        let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1481        self.pending_content_edits.extend(content_edits);
1482        // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1483        // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1484        // the 0.0.42 plan — see that fn's doc comment). The free fn
1485        // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1486        // no longer carries a `self.buffer.<inherent>` hop.
1487        let inverse = apply_buffer_edit(&mut self.buffer, edit);
1488        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1489        // Drop any folds the edit's range overlapped — vim opens the
1490        // surrounding fold automatically when you edit inside it. The
1491        // approximation here invalidates folds covering either the
1492        // pre-edit cursor row or the post-edit cursor row, which
1493        // catches the common single-line / multi-line edit shapes.
1494        let lo = pre_row.min(pos_row);
1495        let hi = pre_row.max(pos_row);
1496        self.apply_fold_op(crate::types::FoldOp::Invalidate {
1497            start_row: lo,
1498            end_row: hi,
1499        });
1500        // Dot mark records the PRE-edit position (change start), matching
1501        // vim's `:h '.` semantics. Previously this stored the post-edit
1502        // cursor, which diverged from nvim on `iX<Esc>j`.
1503        self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1504        // Append to the change-list ring (skip when the cursor sits on
1505        // the same cell as the last entry — back-to-back keystrokes on
1506        // one column shouldn't pollute the ring). A new edit while
1507        // walking the ring trims the forward half, vim style.
1508        let entry = (pos_row, pos_col);
1509        if self.vim.change_list.last() != Some(&entry) {
1510            if let Some(idx) = self.vim.change_list_cursor.take() {
1511                self.vim.change_list.truncate(idx + 1);
1512            }
1513            self.vim.change_list.push(entry);
1514            let len = self.vim.change_list.len();
1515            if len > crate::vim::CHANGE_LIST_MAX {
1516                self.vim
1517                    .change_list
1518                    .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1519            }
1520        }
1521        self.vim.change_list_cursor = None;
1522        // Shift / drop marks + jump-list entries to track the row
1523        // delta the edit produced. Without this, every line-changing
1524        // edit silently invalidates `'a`-style positions.
1525        let post_rows = buf_row_count(&self.buffer);
1526        let delta = post_rows as isize - pre_rows as isize;
1527        if delta != 0 {
1528            self.shift_marks_after_edit(pre_row, delta);
1529        }
1530        self.push_buffer_content_to_textarea();
1531        self.mark_content_dirty();
1532        inverse
1533    }
1534
1535    /// Migrate user marks + jumplist entries when an edit at row
1536    /// `edit_start` changes the buffer's row count by `delta` (positive
1537    /// for inserts, negative for deletes). Marks tied to a deleted row
1538    /// are dropped; marks past the affected band shift by `delta`.
1539    fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1540        if delta == 0 {
1541            return;
1542        }
1543        // Deleted-row band (only meaningful for delta < 0). Inclusive
1544        // start, exclusive end.
1545        let drop_end = if delta < 0 {
1546            edit_start.saturating_add((-delta) as usize)
1547        } else {
1548            edit_start
1549        };
1550        let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1551
1552        // 0.0.36: lowercase + uppercase marks share the unified
1553        // `marks` map; one pass migrates both.
1554        let mut to_drop: Vec<char> = Vec::new();
1555        for (c, (row, _col)) in self.marks.iter_mut() {
1556            if (edit_start..drop_end).contains(row) {
1557                to_drop.push(*c);
1558            } else if *row >= shift_threshold {
1559                *row = ((*row as isize) + delta).max(0) as usize;
1560            }
1561        }
1562        for c in to_drop {
1563            self.marks.remove(&c);
1564        }
1565
1566        let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1567            entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1568            for (row, _) in entries.iter_mut() {
1569                if *row >= shift_threshold {
1570                    *row = ((*row as isize) + delta).max(0) as usize;
1571                }
1572            }
1573        };
1574        shift_jumps(&mut self.vim.jump_back);
1575        shift_jumps(&mut self.vim.jump_fwd);
1576    }
1577
1578    /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1579    /// the textarea from the buffer's lines + cursor, preserving yank
1580    /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1581    /// textarea field disappears at the end of Phase 7f anyway.
1582    /// No-op since Buffer is the content authority. Retained as a
1583    /// shim so call sites in `mutate_edit` and friends don't have to
1584    /// be ripped in lockstep with the field removal.
1585    pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1586
1587    /// Single choke-point for "the buffer just changed". Sets the
1588    /// dirty flag and drops the cached `content_arc` snapshot so
1589    /// subsequent reads rebuild from the live textarea. Callers
1590    /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1591    /// path) must invoke this to keep the cache honest.
1592    pub fn mark_content_dirty(&mut self) {
1593        self.content_dirty = true;
1594        self.cached_content = None;
1595    }
1596
1597    /// Returns true if content changed since the last call, then clears the flag.
1598    pub fn take_dirty(&mut self) -> bool {
1599        let dirty = self.content_dirty;
1600        self.content_dirty = false;
1601        dirty
1602    }
1603
1604    /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1605    /// the last call. Each entry corresponds to a single buffer
1606    /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1607    /// fan out to one entry per row touched.
1608    ///
1609    /// Hosts call this each frame (after [`Editor::take_content_reset`])
1610    /// to fan edits into a tree-sitter parser via `Tree::edit`.
1611    pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1612        std::mem::take(&mut self.pending_content_edits)
1613    }
1614
1615    /// Returns `true` if a bulk buffer replacement happened since the
1616    /// last call (e.g. `set_content` / `restore` / undo restore), then
1617    /// clears the flag. When this returns `true`, hosts should drop
1618    /// any retained syntax tree before consuming
1619    /// [`Editor::take_content_edits`].
1620    pub fn take_content_reset(&mut self) -> bool {
1621        let r = self.pending_content_reset;
1622        self.pending_content_reset = false;
1623        r
1624    }
1625
1626    /// Pull-model coarse change observation. If content changed since
1627    /// the last call, returns `Some(Arc<String>)` with the new content
1628    /// and clears the dirty flag; otherwise returns `None`.
1629    ///
1630    /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1631    /// the character level) should diff against their own previous
1632    /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1633    /// once every edit path inside the engine is instrumented; this
1634    /// coarse form covers the pull-model use case in the meantime.
1635    pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1636        if !self.content_dirty {
1637            return None;
1638        }
1639        let arc = self.content_arc();
1640        self.content_dirty = false;
1641        Some(arc)
1642    }
1643
1644    /// Width in cells of the line-number gutter for the current buffer
1645    /// and settings. Matches what [`Editor::cursor_screen_pos`] reserves
1646    /// in front of the text column. Returns `0` when both `number` and
1647    /// `relativenumber` are off.
1648    pub fn lnum_width(&self) -> u16 {
1649        if self.settings.number || self.settings.relativenumber {
1650            let needed = buf_row_count(&self.buffer).to_string().len() + 1;
1651            needed.max(self.settings.numberwidth) as u16
1652        } else {
1653            0
1654        }
1655    }
1656
1657    /// Returns the cursor's row within the visible textarea (0-based), updating
1658    /// the stored viewport top so subsequent calls remain accurate.
1659    pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
1660        let cursor = buf_cursor_row(&self.buffer);
1661        let top = self.host.viewport().top_row;
1662        cursor.saturating_sub(top).min(height as usize - 1) as u16
1663    }
1664
1665    /// Returns the cursor's screen position `(x, y)` for the textarea
1666    /// described by `(area_x, area_y, area_width, area_height)`.
1667    /// Accounts for line-number gutter, viewport scroll, and any extra
1668    /// gutter width to the left of the number column (sign column, fold
1669    /// column). Returns `None` if the cursor is outside the visible
1670    /// viewport. Always available (engine-native; no ratatui dependency).
1671    ///
1672    /// `extra_gutter_width` is added to the number-column width before
1673    /// computing the cursor x position. Callers (e.g. `apps/hjkl/src/render.rs`)
1674    /// pass `sign_w + fold_w` here so the cursor lands on the correct cell
1675    /// when a dedicated sign or fold column is present.
1676    ///
1677    /// Renamed from `cursor_screen_pos_xywh` in 0.0.32.
1678    pub fn cursor_screen_pos(
1679        &self,
1680        area_x: u16,
1681        area_y: u16,
1682        area_width: u16,
1683        area_height: u16,
1684        extra_gutter_width: u16,
1685    ) -> Option<(u16, u16)> {
1686        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1687        let v = self.host.viewport();
1688        if pos_row < v.top_row || pos_col < v.top_col {
1689            return None;
1690        }
1691        let lnum_width = self.lnum_width();
1692        // Full offset from the left edge of the window to the first text cell.
1693        let gutter_total = lnum_width + extra_gutter_width;
1694        let dy = (pos_row - v.top_row) as u16;
1695        // Convert char column to visual column so cursor lands on the
1696        // correct cell when the line contains tabs (which the renderer
1697        // expands to TAB_WIDTH stops). Tab width must match the renderer.
1698        let line = self.buffer.line(pos_row).unwrap_or_default();
1699        let tab_width = if v.tab_width == 0 {
1700            4
1701        } else {
1702            v.tab_width as usize
1703        };
1704        let visual_pos = visual_col_for_char(&line, pos_col, tab_width);
1705        let visual_top = visual_col_for_char(&line, v.top_col, tab_width);
1706        let dx = (visual_pos - visual_top) as u16;
1707        if dy >= area_height || dx + gutter_total >= area_width {
1708            return None;
1709        }
1710        Some((area_x + gutter_total + dx, area_y + dy))
1711    }
1712
1713    /// Returns the current vim mode. Phase 6.3: reads from the stable
1714    /// `current_mode` field (kept in sync by both the FSM step loop and
1715    /// the Phase 6.3 primitive bridges) rather than deriving from the
1716    /// FSM-internal `mode` field via `public_mode()`.
1717    pub fn vim_mode(&self) -> VimMode {
1718        self.vim.current_mode
1719    }
1720
1721    /// Bounds of the active visual-block rectangle as
1722    /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
1723    /// `None` when we're not in VisualBlock mode.
1724    /// Read-only view of the live `/` or `?` prompt. `None` outside
1725    /// search-prompt mode.
1726    pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
1727        self.vim.search_prompt.as_ref()
1728    }
1729
1730    /// Most recent committed search pattern (persists across `n` / `N`
1731    /// and across prompt exits). `None` before the first search.
1732    pub fn last_search(&self) -> Option<&str> {
1733        self.vim.last_search.as_deref()
1734    }
1735
1736    /// Whether the last committed search was a forward `/` (`true`) or
1737    /// a backward `?` (`false`). `n` and `N` consult this to honour the
1738    /// direction the user committed.
1739    pub fn last_search_forward(&self) -> bool {
1740        self.vim.last_search_forward
1741    }
1742
1743    /// Set the most recent committed search text + direction. Used by
1744    /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
1745    /// outside the engine's vim FSM) so `n` / `N` repeat the host's
1746    /// most recent commit with the right direction. Pass `None` /
1747    /// `true` to clear.
1748    pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
1749        self.vim.last_search = text;
1750        self.vim.last_search_forward = forward;
1751    }
1752
1753    /// Start/end `(row, col)` of the active char-wise Visual selection
1754    /// (inclusive on both ends, positionally ordered). `None` when not
1755    /// in Visual mode.
1756    pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
1757        if self.vim_mode() != VimMode::Visual {
1758            return None;
1759        }
1760        let anchor = self.vim.visual_anchor;
1761        let cursor = self.cursor();
1762        let (start, end) = if anchor <= cursor {
1763            (anchor, cursor)
1764        } else {
1765            (cursor, anchor)
1766        };
1767        Some((start, end))
1768    }
1769
1770    /// Top/bottom rows of the active VisualLine selection (inclusive).
1771    /// `None` when we're not in VisualLine mode.
1772    pub fn line_highlight(&self) -> Option<(usize, usize)> {
1773        if self.vim_mode() != VimMode::VisualLine {
1774            return None;
1775        }
1776        let anchor = self.vim.visual_line_anchor;
1777        let cursor = buf_cursor_row(&self.buffer);
1778        Some((anchor.min(cursor), anchor.max(cursor)))
1779    }
1780
1781    pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
1782        if self.vim_mode() != VimMode::VisualBlock {
1783            return None;
1784        }
1785        let (ar, ac) = self.vim.block_anchor;
1786        let cr = buf_cursor_row(&self.buffer);
1787        let cc = self.vim.block_vcol;
1788        let top = ar.min(cr);
1789        let bot = ar.max(cr);
1790        let left = ac.min(cc);
1791        let right = ac.max(cc);
1792        Some((top, bot, left, right))
1793    }
1794
1795    /// Active selection in `hjkl_buffer::Selection` shape. `None` when
1796    /// not in a Visual mode. Phase 7d-i wiring — the host hands this
1797    /// straight to `BufferView` once render flips off textarea
1798    /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
1799    /// switch).
1800    pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
1801        use hjkl_buffer::{Position, Selection};
1802        match self.vim_mode() {
1803            VimMode::Visual => {
1804                let (ar, ac) = self.vim.visual_anchor;
1805                let head = buf_cursor_pos(&self.buffer);
1806                Some(Selection::Char {
1807                    anchor: Position::new(ar, ac),
1808                    head,
1809                })
1810            }
1811            VimMode::VisualLine => {
1812                let anchor_row = self.vim.visual_line_anchor;
1813                let head_row = buf_cursor_row(&self.buffer);
1814                Some(Selection::Line {
1815                    anchor_row,
1816                    head_row,
1817                })
1818            }
1819            VimMode::VisualBlock => {
1820                let (ar, ac) = self.vim.block_anchor;
1821                let cr = buf_cursor_row(&self.buffer);
1822                let cc = self.vim.block_vcol;
1823                Some(Selection::Block {
1824                    anchor: Position::new(ar, ac),
1825                    head: Position::new(cr, cc),
1826                })
1827            }
1828            _ => None,
1829        }
1830    }
1831
1832    /// Force back to normal mode (used when dismissing completions etc.)
1833    pub fn force_normal(&mut self) {
1834        self.vim.force_normal();
1835    }
1836
1837    pub fn content(&self) -> String {
1838        let n = buf_row_count(&self.buffer);
1839        let mut s = String::new();
1840        for r in 0..n {
1841            if r > 0 {
1842                s.push('\n');
1843            }
1844            s.push_str(&crate::types::Query::line(&self.buffer, r as u32));
1845        }
1846        s.push('\n');
1847        s
1848    }
1849
1850    /// Same logical output as [`content`], but returns a cached
1851    /// `Arc<String>` so back-to-back reads within an un-mutated window
1852    /// are ref-count bumps instead of multi-MB joins. The cache is
1853    /// invalidated by every [`mark_content_dirty`] call.
1854    pub fn content_arc(&mut self) -> std::sync::Arc<String> {
1855        if let Some(arc) = &self.cached_content {
1856            return std::sync::Arc::clone(arc);
1857        }
1858        let arc = std::sync::Arc::new(self.content());
1859        self.cached_content = Some(std::sync::Arc::clone(&arc));
1860        arc
1861    }
1862
1863    pub fn set_content(&mut self, text: &str) {
1864        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1865        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1866            lines.pop();
1867        }
1868        if lines.is_empty() {
1869            lines.push(String::new());
1870        }
1871        let _ = lines;
1872        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
1873        self.undo_stack.clear();
1874        self.redo_stack.clear();
1875        // Whole-buffer replace supersedes any queued ContentEdits.
1876        self.pending_content_edits.clear();
1877        self.pending_content_reset = true;
1878        self.mark_content_dirty();
1879    }
1880
1881    /// Whole-buffer replace that **preserves the undo history**.
1882    ///
1883    /// Equivalent to [`Editor::set_content`] but pushes the current buffer
1884    /// state onto the undo stack first, so a subsequent `u` walks back to
1885    /// the pre-replacement content. Use this for any operation the user
1886    /// expects to undo as a single step — e.g. external formatter output
1887    /// (`hjkl-mangler`) installed via the async [`crate::app::FormatWorker`].
1888    ///
1889    /// Like `push_undo`, this clears the redo stack (vim semantics: any
1890    /// new edit invalidates redo).
1891    pub fn set_content_undoable(&mut self, text: &str) {
1892        self.push_undo();
1893        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1894        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1895            lines.pop();
1896        }
1897        if lines.is_empty() {
1898            lines.push(String::new());
1899        }
1900        let _ = lines;
1901        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
1902        // Whole-buffer replace supersedes any queued ContentEdits.
1903        self.pending_content_edits.clear();
1904        self.pending_content_reset = true;
1905        self.mark_content_dirty();
1906    }
1907
1908    /// Drain the pending change log produced by buffer mutations.
1909    ///
1910    /// Returns a `Vec<EditOp>` covering edits applied since the last
1911    /// call. Empty when no edits ran. Pull-model, complementary to
1912    /// [`Editor::take_content_change`] which gives back the new full
1913    /// content.
1914    ///
1915    /// Mapping coverage:
1916    /// - InsertChar / InsertStr → exact `EditOp` with empty range +
1917    ///   replacement.
1918    /// - DeleteRange (`Char` kind) → exact range + empty replacement.
1919    /// - Replace → exact range + new replacement.
1920    /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
1921    ///   InsertBlock, DeleteBlockChunks → best-effort placeholder
1922    ///   covering the touched range. Hosts wanting per-cell deltas
1923    ///   should diff their own `lines()` snapshot.
1924    pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
1925        std::mem::take(&mut self.change_log)
1926    }
1927
1928    /// Read the engine's current settings as a SPEC
1929    /// [`crate::types::Options`].
1930    ///
1931    /// Bridges between the legacy [`Settings`] (which carries fewer
1932    /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
1933    /// not present in `Settings` fall back to vim defaults (e.g.,
1934    /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
1935    /// Once trait extraction lands, this becomes the canonical config
1936    /// reader and `Settings` retires.
1937    pub fn current_options(&self) -> crate::types::Options {
1938        crate::types::Options {
1939            shiftwidth: self.settings.shiftwidth as u32,
1940            tabstop: self.settings.tabstop as u32,
1941            softtabstop: self.settings.softtabstop as u32,
1942            textwidth: self.settings.textwidth as u32,
1943            expandtab: self.settings.expandtab,
1944            ignorecase: self.settings.ignore_case,
1945            smartcase: self.settings.smartcase,
1946            wrapscan: self.settings.wrapscan,
1947            wrap: match self.settings.wrap {
1948                hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
1949                hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
1950                hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
1951            },
1952            readonly: self.settings.readonly,
1953            autoindent: self.settings.autoindent,
1954            smartindent: self.settings.smartindent,
1955            undo_levels: self.settings.undo_levels,
1956            undo_break_on_motion: self.settings.undo_break_on_motion,
1957            iskeyword: self.settings.iskeyword.clone(),
1958            timeout_len: self.settings.timeout_len,
1959            ..crate::types::Options::default()
1960        }
1961    }
1962
1963    /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
1964    /// Only the fields backed by today's [`Settings`] take effect;
1965    /// remaining options become live once trait extraction wires them
1966    /// through.
1967    pub fn apply_options(&mut self, opts: &crate::types::Options) {
1968        self.settings.shiftwidth = opts.shiftwidth as usize;
1969        self.settings.tabstop = opts.tabstop as usize;
1970        self.settings.softtabstop = opts.softtabstop as usize;
1971        self.settings.textwidth = opts.textwidth as usize;
1972        self.settings.expandtab = opts.expandtab;
1973        self.settings.ignore_case = opts.ignorecase;
1974        self.settings.smartcase = opts.smartcase;
1975        self.settings.wrapscan = opts.wrapscan;
1976        self.settings.wrap = match opts.wrap {
1977            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
1978            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
1979            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
1980        };
1981        self.settings.readonly = opts.readonly;
1982        self.settings.autoindent = opts.autoindent;
1983        self.settings.smartindent = opts.smartindent;
1984        self.settings.undo_levels = opts.undo_levels;
1985        self.settings.undo_break_on_motion = opts.undo_break_on_motion;
1986        self.set_iskeyword(opts.iskeyword.clone());
1987        self.settings.timeout_len = opts.timeout_len;
1988        self.settings.number = opts.number;
1989        self.settings.relativenumber = opts.relativenumber;
1990        self.settings.numberwidth = opts.numberwidth;
1991        self.settings.cursorline = opts.cursorline;
1992        self.settings.cursorcolumn = opts.cursorcolumn;
1993        self.settings.signcolumn = opts.signcolumn;
1994        self.settings.foldcolumn = opts.foldcolumn;
1995        self.settings.colorcolumn = opts.colorcolumn.clone();
1996    }
1997
1998    /// Active visual selection as a SPEC [`crate::types::Highlight`]
1999    /// with [`crate::types::HighlightKind::Selection`].
2000    ///
2001    /// Returns `None` when the editor isn't in a Visual mode.
2002    /// Visual-line and visual-block selections collapse to the
2003    /// bounding char range of the selection — the SPEC `Selection`
2004    /// kind doesn't carry sub-line info today; hosts that need full
2005    /// line / block geometry continue to read [`buffer_selection`]
2006    /// (the legacy [`hjkl_buffer::Selection`] shape).
2007    pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2008        use crate::types::{Highlight, HighlightKind, Pos};
2009        let sel = self.buffer_selection()?;
2010        let (start, end) = match sel {
2011            hjkl_buffer::Selection::Char { anchor, head } => {
2012                let a = (anchor.row, anchor.col);
2013                let h = (head.row, head.col);
2014                if a <= h { (a, h) } else { (h, a) }
2015            }
2016            hjkl_buffer::Selection::Line {
2017                anchor_row,
2018                head_row,
2019            } => {
2020                let (top, bot) = if anchor_row <= head_row {
2021                    (anchor_row, head_row)
2022                } else {
2023                    (head_row, anchor_row)
2024                };
2025                let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2026                ((top, 0), (bot, last_col))
2027            }
2028            hjkl_buffer::Selection::Block { anchor, head } => {
2029                let (top, bot) = if anchor.row <= head.row {
2030                    (anchor.row, head.row)
2031                } else {
2032                    (head.row, anchor.row)
2033                };
2034                let (left, right) = if anchor.col <= head.col {
2035                    (anchor.col, head.col)
2036                } else {
2037                    (head.col, anchor.col)
2038                };
2039                ((top, left), (bot, right))
2040            }
2041        };
2042        Some(Highlight {
2043            range: Pos {
2044                line: start.0 as u32,
2045                col: start.1 as u32,
2046            }..Pos {
2047                line: end.0 as u32,
2048                col: end.1 as u32,
2049            },
2050            kind: HighlightKind::Selection,
2051        })
2052    }
2053
2054    /// SPEC-typed highlights for `line`.
2055    ///
2056    /// Two emission modes:
2057    ///
2058    /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2059    ///   `Editor::search_prompt` is `Some`. Live-preview matches of
2060    ///   the in-flight pattern surface as
2061    ///   [`crate::types::HighlightKind::IncSearch`].
2062    /// - **SearchMatch**: the prompt has been committed (or absent)
2063    ///   and the buffer's armed pattern is non-empty. Matches surface
2064    ///   as [`crate::types::HighlightKind::SearchMatch`].
2065    ///
2066    /// Selection / MatchParen / Syntax(id) variants land once the
2067    /// trait extraction routes the FSM's selection set + the host's
2068    /// syntax pipeline through the [`crate::types::Host`] trait.
2069    ///
2070    /// Returns an empty vec when there is nothing to highlight or
2071    /// `line` is out of bounds.
2072    pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2073        use crate::types::{Highlight, HighlightKind, Pos};
2074        let row = line as usize;
2075        if row >= buf_row_count(&self.buffer) {
2076            return Vec::new();
2077        }
2078
2079        // Live preview while the prompt is open beats the committed
2080        // pattern.
2081        if let Some(prompt) = self.search_prompt() {
2082            if prompt.text.is_empty() {
2083                return Vec::new();
2084            }
2085            let translated = crate::search::vim_to_rust_regex(&prompt.text);
2086            let Ok(re) = regex::Regex::new(&translated) else {
2087                return Vec::new();
2088            };
2089            let Some(haystack) = buf_line(&self.buffer, row) else {
2090                return Vec::new();
2091            };
2092            return re
2093                .find_iter(&haystack)
2094                .map(|m| Highlight {
2095                    range: Pos {
2096                        line,
2097                        col: m.start() as u32,
2098                    }..Pos {
2099                        line,
2100                        col: m.end() as u32,
2101                    },
2102                    kind: HighlightKind::IncSearch,
2103                })
2104                .collect();
2105        }
2106
2107        if self.search_state.pattern.is_none() {
2108            return Vec::new();
2109        }
2110        let dgen = crate::types::Query::dirty_gen(&self.buffer);
2111        crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2112            .into_iter()
2113            .map(|(start, end)| Highlight {
2114                range: Pos {
2115                    line,
2116                    col: start as u32,
2117                }..Pos {
2118                    line,
2119                    col: end as u32,
2120                },
2121                kind: HighlightKind::SearchMatch,
2122            })
2123            .collect()
2124    }
2125
2126    /// Build the engine's [`crate::types::RenderFrame`] for the
2127    /// current state. Hosts call this once per redraw and diff
2128    /// across frames.
2129    ///
2130    /// Coarse today — covers mode + cursor + cursor shape + viewport
2131    /// top + line count. SPEC-target fields (selections, highlights,
2132    /// command line, search prompt, status line) land once trait
2133    /// extraction routes them through `SelectionSet` and the
2134    /// `Highlight` pipeline.
2135    pub fn render_frame(&self) -> crate::types::RenderFrame {
2136        use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2137        let (cursor_row, cursor_col) = self.cursor();
2138        let (mode, shape) = match self.vim_mode() {
2139            crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2140            crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2141            crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2142            crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2143            crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2144        };
2145        RenderFrame {
2146            mode,
2147            cursor_row: cursor_row as u32,
2148            cursor_col: cursor_col as u32,
2149            cursor_shape: shape,
2150            viewport_top: self.host.viewport().top_row as u32,
2151            line_count: crate::types::Query::line_count(&self.buffer),
2152        }
2153    }
2154
2155    /// Capture the editor's coarse state into a serde-friendly
2156    /// [`crate::types::EditorSnapshot`].
2157    ///
2158    /// Today's snapshot covers mode, cursor, lines, viewport top.
2159    /// Registers, marks, jump list, undo tree, and full options arrive
2160    /// once phase 5 trait extraction lands the generic
2161    /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2162    /// stays stable; only the snapshot's internal fields grow.
2163    ///
2164    /// Distinct from the internal `snapshot` used by undo (which
2165    /// returns `(Vec<String>, (usize, usize))`); host-facing
2166    /// persistence goes through this one.
2167    pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2168        use crate::types::{EditorSnapshot, SnapshotMode};
2169        let mode = match self.vim_mode() {
2170            crate::VimMode::Normal => SnapshotMode::Normal,
2171            crate::VimMode::Insert => SnapshotMode::Insert,
2172            crate::VimMode::Visual => SnapshotMode::Visual,
2173            crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2174            crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2175        };
2176        let cursor = self.cursor();
2177        let cursor = (cursor.0 as u32, cursor.1 as u32);
2178        let lines: Vec<String> = buf_lines_to_vec(&self.buffer);
2179        let viewport_top = self.host.viewport().top_row as u32;
2180        let marks = self
2181            .marks
2182            .iter()
2183            .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2184            .collect();
2185        EditorSnapshot {
2186            version: EditorSnapshot::VERSION,
2187            mode,
2188            cursor,
2189            lines,
2190            viewport_top,
2191            registers: self.registers.clone(),
2192            marks,
2193        }
2194    }
2195
2196    /// Restore editor state from an [`EditorSnapshot`]. Returns
2197    /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2198    /// `version` doesn't match [`EditorSnapshot::VERSION`].
2199    ///
2200    /// Mode is best-effort: `SnapshotMode` only round-trips the
2201    /// status-line summary, not the full FSM state. Visual / Insert
2202    /// mode entry happens through synthetic key dispatch when needed.
2203    pub fn restore_snapshot(
2204        &mut self,
2205        snap: crate::types::EditorSnapshot,
2206    ) -> Result<(), crate::EngineError> {
2207        use crate::types::EditorSnapshot;
2208        if snap.version != EditorSnapshot::VERSION {
2209            return Err(crate::EngineError::SnapshotVersion(
2210                snap.version,
2211                EditorSnapshot::VERSION,
2212            ));
2213        }
2214        let text = snap.lines.join("\n");
2215        self.set_content(&text);
2216        self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2217        self.host.viewport_mut().top_row = snap.viewport_top as usize;
2218        self.registers = snap.registers;
2219        self.marks = snap
2220            .marks
2221            .into_iter()
2222            .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2223            .collect();
2224        Ok(())
2225    }
2226
2227    /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2228    /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2229    /// shape their payload.
2230    pub fn seed_yank(&mut self, text: String) {
2231        let linewise = text.ends_with('\n');
2232        self.vim.yank_linewise = linewise;
2233        self.registers.unnamed = crate::registers::Slot { text, linewise };
2234    }
2235
2236    /// Scroll the viewport down by `rows`. The cursor stays on its
2237    /// absolute line (vim convention) unless the scroll would take it
2238    /// off-screen — in that case it's clamped to the first row still
2239    /// visible.
2240    pub fn scroll_down(&mut self, rows: i16) {
2241        self.scroll_viewport(rows);
2242    }
2243
2244    /// Scroll the viewport up by `rows`. Cursor stays unless it would
2245    /// fall off the bottom of the new viewport, then clamp to the
2246    /// bottom-most visible row.
2247    pub fn scroll_up(&mut self, rows: i16) {
2248        self.scroll_viewport(-rows);
2249    }
2250
2251    /// Scroll the viewport right by `cols` columns. Only the horizontal
2252    /// offset (`top_col`) moves — the cursor is NOT adjusted (matches
2253    /// vim's `zl` behaviour for horizontal scroll without wrap).
2254    pub fn scroll_right(&mut self, cols: i16) {
2255        let vp = self.host.viewport_mut();
2256        let cols_i = cols as isize;
2257        let new_top = (vp.top_col as isize + cols_i).max(0) as usize;
2258        vp.top_col = new_top;
2259    }
2260
2261    /// Scroll the viewport left by `cols` columns. Delegates to
2262    /// `scroll_right` with a negated argument so the floor-at-zero
2263    /// clamp is shared.
2264    pub fn scroll_left(&mut self, cols: i16) {
2265        self.scroll_right(-cols);
2266    }
2267
2268    /// Vim's `scrolloff` default — keep the cursor at least this many
2269    /// rows away from the top / bottom edge of the viewport while
2270    /// scrolling. Collapses to `height / 2` for tiny viewports.
2271    const SCROLLOFF: usize = 5;
2272
2273    /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2274    /// rows from each edge. Replaces the bare
2275    /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2276    /// don't park the cursor on the very last visible row.
2277    pub fn ensure_cursor_in_scrolloff(&mut self) {
2278        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2279        if height == 0 {
2280            // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2281            // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2282            // Disjoint-field borrow split: `self.buffer` (immutable via
2283            // `folds` snapshot + cursor) and `self.host` (mutable
2284            // viewport ref) live on distinct struct fields, so one
2285            // statement satisfies the borrow checker.
2286            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2287            crate::viewport_math::ensure_cursor_visible(
2288                &self.buffer,
2289                &folds,
2290                self.host.viewport_mut(),
2291            );
2292            return;
2293        }
2294        // Cap margin at (height - 1) / 2 so the upper + lower bands
2295        // can't overlap on tiny windows (margin=5 + height=10 would
2296        // otherwise produce contradictory clamp ranges).
2297        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2298        // Soft-wrap path: scrolloff math runs in *screen rows*, not
2299        // doc rows, since a wrapped doc row spans many visual lines.
2300        if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2301            self.ensure_scrolloff_wrap(height, margin);
2302            return;
2303        }
2304        let cursor_row = buf_cursor_row(&self.buffer);
2305        let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2306        let v = self.host.viewport_mut();
2307        // Top edge: cursor_row should sit at >= top_row + margin.
2308        if cursor_row < v.top_row + margin {
2309            v.top_row = cursor_row.saturating_sub(margin);
2310        }
2311        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2312        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2313        if cursor_row > v.top_row + max_bottom {
2314            v.top_row = cursor_row.saturating_sub(max_bottom);
2315        }
2316        // Clamp top_row so we never scroll past the buffer's bottom.
2317        let max_top = last_row.saturating_sub(height.saturating_sub(1));
2318        if v.top_row > max_top {
2319            v.top_row = max_top;
2320        }
2321        // Defer to Buffer for column-side scroll (no scrolloff for
2322        // horizontal scrolling — vim default `sidescrolloff = 0`).
2323        let cursor = buf_cursor_pos(&self.buffer);
2324        self.host.viewport_mut().ensure_visible(cursor);
2325    }
2326
2327    /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2328    /// at a time so the cursor's *screen* row stays inside
2329    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2330    /// buffer's bottom never leaves blank rows below it.
2331    fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2332        let cursor_row = buf_cursor_row(&self.buffer);
2333        // Step 1 — cursor above viewport: snap top to cursor row,
2334        // then we'll fix up the margin below.
2335        if cursor_row < self.host.viewport().top_row {
2336            let v = self.host.viewport_mut();
2337            v.top_row = cursor_row;
2338            v.top_col = 0;
2339        }
2340        // Step 2 — push top forward until cursor's screen row is
2341        // within the bottom margin (`csr <= height - 1 - margin`).
2342        // 0.0.33 (Patch C-γ): fold-iteration goes through the
2343        // [`crate::types::FoldProvider`] surface via
2344        // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2345        // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2346        // a `&Viewport` parameter; the host owns the viewport, so the
2347        // disjoint `(self.host, self.buffer)` borrows split cleanly.
2348        let max_csr = height.saturating_sub(1).saturating_sub(margin);
2349        loop {
2350            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2351            let csr =
2352                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2353                    .unwrap_or(0);
2354            if csr <= max_csr {
2355                break;
2356            }
2357            let top = self.host.viewport().top_row;
2358            let row_count = buf_row_count(&self.buffer);
2359            let next = {
2360                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2361                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2362            };
2363            let Some(next) = next else {
2364                break;
2365            };
2366            // Don't walk past the cursor's row.
2367            if next > cursor_row {
2368                self.host.viewport_mut().top_row = cursor_row;
2369                break;
2370            }
2371            self.host.viewport_mut().top_row = next;
2372        }
2373        // Step 3 — pull top backward until cursor's screen row is
2374        // past the top margin (`csr >= margin`).
2375        loop {
2376            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2377            let csr =
2378                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2379                    .unwrap_or(0);
2380            if csr >= margin {
2381                break;
2382            }
2383            let top = self.host.viewport().top_row;
2384            let prev = {
2385                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2386                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2387            };
2388            let Some(prev) = prev else {
2389                break;
2390            };
2391            self.host.viewport_mut().top_row = prev;
2392        }
2393        // Step 4 — clamp top so the buffer's bottom doesn't leave
2394        // blank rows below it. `max_top_for_height` walks segments
2395        // backward from the last row until it accumulates `height`
2396        // screen rows.
2397        let max_top = {
2398            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2399            crate::viewport_math::max_top_for_height(
2400                &self.buffer,
2401                &folds,
2402                self.host.viewport(),
2403                height,
2404            )
2405        };
2406        if self.host.viewport().top_row > max_top {
2407            self.host.viewport_mut().top_row = max_top;
2408        }
2409        self.host.viewport_mut().top_col = 0;
2410    }
2411
2412    fn scroll_viewport(&mut self, delta: i16) {
2413        if delta == 0 {
2414            return;
2415        }
2416        // Bump the host viewport's top within bounds.
2417        let total_rows = buf_row_count(&self.buffer) as isize;
2418        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2419        let cur_top = self.host.viewport().top_row as isize;
2420        let new_top = (cur_top + delta as isize)
2421            .max(0)
2422            .min((total_rows - 1).max(0)) as usize;
2423        self.host.viewport_mut().top_row = new_top;
2424        // Mirror to textarea so its viewport reads (still consumed by
2425        // a couple of helpers) stay accurate.
2426        let _ = cur_top;
2427        if height == 0 {
2428            return;
2429        }
2430        // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2431        // from the visible viewport edges.
2432        let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2433        let margin = Self::SCROLLOFF.min(height / 2);
2434        let min_row = new_top + margin;
2435        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2436        let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2437        if target_row != cursor_row {
2438            let line_len = buf_line(&self.buffer, target_row)
2439                .map(|l| l.chars().count())
2440                .unwrap_or(0);
2441            let target_col = cursor_col.min(line_len.saturating_sub(1));
2442            buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2443        }
2444    }
2445
2446    pub fn goto_line(&mut self, line: usize) {
2447        let row = line.saturating_sub(1);
2448        let max = buf_row_count(&self.buffer).saturating_sub(1);
2449        let target = row.min(max);
2450        buf_set_cursor_rc(&mut self.buffer, target, 0);
2451        // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2452        // the cursor lands off-screen and the user has to scroll
2453        // manually to see it.
2454        self.ensure_cursor_in_scrolloff();
2455    }
2456
2457    /// Scroll so the cursor row lands at the given viewport position:
2458    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2459    /// Cursor stays on its absolute line; only the viewport moves.
2460    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2461        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2462        if height == 0 {
2463            return;
2464        }
2465        let cur_row = buf_cursor_row(&self.buffer);
2466        let cur_top = self.host.viewport().top_row;
2467        // Scrolloff awareness: `zt` lands the cursor at the top edge
2468        // of the viable area (top + margin), `zb` at the bottom edge
2469        // (top + height - 1 - margin). Match the cap used by
2470        // `ensure_cursor_in_scrolloff` so contradictory bounds are
2471        // impossible on tiny viewports.
2472        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2473        let new_top = match pos {
2474            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2475            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2476            CursorScrollTarget::Bottom => {
2477                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2478            }
2479        };
2480        if new_top == cur_top {
2481            return;
2482        }
2483        self.host.viewport_mut().top_row = new_top;
2484    }
2485
2486    /// Jump the cursor to the given 1-based line/column, clamped to the document.
2487    pub fn jump_to(&mut self, line: usize, col: usize) {
2488        let r = line.saturating_sub(1);
2489        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2490        let r = r.min(max_row);
2491        let line_len = buf_line(&self.buffer, r)
2492            .map(|l| l.chars().count())
2493            .unwrap_or(0);
2494        let c = col.saturating_sub(1).min(line_len);
2495        buf_set_cursor_rc(&mut self.buffer, r, c);
2496    }
2497
2498    // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
2499    //
2500    // These primitives operate on document (row, col) coordinates that the HOST
2501    // computes from its own layout knowledge (cell geometry for the TUI host,
2502    // pixel geometry for the future GUI host). The engine has no u16 terminal
2503    // assumption here — it just moves the cursor in doc-space.
2504
2505    /// Set the cursor to the given doc-space `(row, col)`, clamped to the
2506    /// document bounds. Hosts use this for programmatic cursor placement and
2507    /// as the building block for the mouse-click path.
2508    ///
2509    /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
2510    /// position); values beyond that are clamped to `char_count`.
2511    pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
2512        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2513        let r = row.min(max_row);
2514        let line_len = buf_line(&self.buffer, r)
2515            .map(|l| l.chars().count())
2516            .unwrap_or(0);
2517        let c = col.min(line_len);
2518        buf_set_cursor_rc(&mut self.buffer, r, c);
2519    }
2520
2521    /// Handle a left-button click at doc-space `(row, col)`.
2522    ///
2523    /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
2524    /// parity for `undo_break_on_motion`), then moves the cursor. The host
2525    /// performs cell→doc or pixel→doc translation before calling this.
2526    ///
2527    /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
2528    /// cursor lives on chars and never on the implicit `\n` — `col` is
2529    /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
2530    /// allows the one-past-EOL insert position (`col == chars().count()`).
2531    ///
2532    /// Resets `sticky_col` to the clicked column so the next `j`/`k`
2533    /// motion uses the clicked column as the intended visual column
2534    /// (otherwise the cursor would snap back to the keyboard-tracked
2535    /// column on the first vertical motion after a click).
2536    pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
2537        if self.vim.is_visual() {
2538            self.vim.force_normal();
2539        }
2540        // Mouse-position click counts as a motion — break the active
2541        // insert-mode undo group when the toggle is on (vim parity).
2542        crate::vim::break_undo_group_in_insert(self);
2543
2544        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2545        let r = row.min(max_row);
2546        let line_len = buf_line(&self.buffer, r)
2547            .map(|l| l.chars().count())
2548            .unwrap_or(0);
2549        let cap = if self.vim.current_mode == crate::VimMode::Insert {
2550            line_len
2551        } else {
2552            line_len.saturating_sub(1)
2553        };
2554        let c = col.min(cap);
2555        buf_set_cursor_rc(&mut self.buffer, r, c);
2556        self.sticky_col = Some(c);
2557    }
2558
2559    /// Begin a mouse-drag selection: anchor at the current cursor and enter
2560    /// Visual-char mode. Idempotent if already in Visual-char mode.
2561    pub fn mouse_begin_drag(&mut self) {
2562        if !self.vim.is_visual_char() {
2563            vim::enter_visual_char_bridge(self);
2564        }
2565    }
2566
2567    /// Extend an in-progress mouse drag to doc-space `(row, col)`.
2568    ///
2569    /// Moves the live cursor; the Visual anchor stays where
2570    /// [`Editor::mouse_begin_drag`] set it. Call after the host has
2571    /// translated the drag position to doc coordinates.
2572    pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
2573        self.set_cursor_doc(row, col);
2574    }
2575
2576    pub fn insert_str(&mut self, text: &str) {
2577        let pos = crate::types::Cursor::cursor(&self.buffer);
2578        crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2579        self.push_buffer_content_to_textarea();
2580        self.mark_content_dirty();
2581    }
2582
2583    pub fn accept_completion(&mut self, completion: &str) {
2584        use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2585        let cursor_pos = CursorTrait::cursor(&self.buffer);
2586        let cursor_row = cursor_pos.line as usize;
2587        let cursor_col = cursor_pos.col as usize;
2588        let line = buf_line(&self.buffer, cursor_row).unwrap_or_default();
2589        let chars: Vec<char> = line.chars().collect();
2590        let prefix_len = chars[..cursor_col.min(chars.len())]
2591            .iter()
2592            .rev()
2593            .take_while(|c| c.is_alphanumeric() || **c == '_')
2594            .count();
2595        if prefix_len > 0 {
2596            let start = Pos {
2597                line: cursor_row as u32,
2598                col: (cursor_col - prefix_len) as u32,
2599            };
2600            BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2601        }
2602        let cursor = CursorTrait::cursor(&self.buffer);
2603        BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2604        self.push_buffer_content_to_textarea();
2605        self.mark_content_dirty();
2606    }
2607
2608    pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
2609        let rc = buf_cursor_rc(&self.buffer);
2610        (buf_lines_to_vec(&self.buffer), rc)
2611    }
2612
2613    /// Walk one step back through the undo history. Equivalent to the
2614    /// user pressing `u` in normal mode. Drains the most recent undo
2615    /// entry and pushes it onto the redo stack.
2616    pub fn undo(&mut self) {
2617        crate::vim::do_undo(self);
2618    }
2619
2620    /// Walk one step forward through the redo history. Equivalent to
2621    /// `<C-r>` in normal mode.
2622    pub fn redo(&mut self) {
2623        crate::vim::do_redo(self);
2624    }
2625
2626    /// Snapshot current buffer state onto the undo stack and clear
2627    /// the redo stack. Bounded by `settings.undo_levels` — older
2628    /// entries pruned. Call before any group of buffer mutations the
2629    /// user might want to undo as a single step.
2630    pub fn push_undo(&mut self) {
2631        let snap = self.snapshot();
2632        self.undo_stack.push(snap);
2633        self.cap_undo();
2634        self.redo_stack.clear();
2635    }
2636
2637    /// Trim the undo stack down to `settings.undo_levels`, dropping
2638    /// the oldest entries. `undo_levels == 0` is treated as
2639    /// "unlimited" (vim's 0-means-no-undo semantics intentionally
2640    /// skipped — guarding with `> 0` is one line shorter than gating
2641    /// the cap path with an explicit zero-check above the call site).
2642    pub(crate) fn cap_undo(&mut self) {
2643        let cap = self.settings.undo_levels as usize;
2644        if cap > 0 && self.undo_stack.len() > cap {
2645            let diff = self.undo_stack.len() - cap;
2646            self.undo_stack.drain(..diff);
2647        }
2648    }
2649
2650    /// Test-only accessor for the undo stack length.
2651    #[doc(hidden)]
2652    pub fn undo_stack_len(&self) -> usize {
2653        self.undo_stack.len()
2654    }
2655
2656    /// Replace the buffer with `lines` joined by `\n` and set the
2657    /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
2658    /// paths. Marks the editor dirty.
2659    pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
2660        let text = lines.join("\n");
2661        crate::types::BufferEdit::replace_all(&mut self.buffer, &text);
2662        buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
2663        // Bulk replace — supersedes any queued ContentEdits.
2664        self.pending_content_edits.clear();
2665        self.pending_content_reset = true;
2666        self.mark_content_dirty();
2667    }
2668
2669    /// Returns true if the key was consumed by the editor.
2670    /// Replace the char under the cursor with `ch`, `count` times. Matches
2671    /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
2672    /// snapshot taken once at start. Promoted to public surface in 0.5.5
2673    /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
2674    /// re-entering the FSM.
2675    pub fn replace_char_at(&mut self, ch: char, count: usize) {
2676        vim::replace_char(self, ch, count);
2677    }
2678
2679    /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
2680    /// to the `count`-th occurrence of `ch` on the current line, respecting
2681    /// `forward` (direction) and `till` (stop one char before target).
2682    /// Records `last_find` so `;` / `,` repeat work.
2683    ///
2684    /// No-op if the target char isn't on the current line within range.
2685    /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
2686    pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
2687        vim::apply_find_char(self, ch, forward, till, count.max(1));
2688    }
2689
2690    /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
2691    /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
2692    /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
2693    ///
2694    /// Promoted to public surface in 0.5.10 so hjkl-vim's
2695    /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
2696    /// re-entering the engine FSM.
2697    pub fn after_g(&mut self, ch: char, count: usize) {
2698        vim::apply_after_g(self, ch, count);
2699    }
2700
2701    /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
2702    /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
2703    /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
2704    /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
2705    ///
2706    /// Promoted to public surface in 0.5.11 so hjkl-vim's
2707    /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
2708    /// re-entering the engine FSM.
2709    pub fn after_z(&mut self, ch: char, count: usize) {
2710        vim::apply_after_z(self, ch, count);
2711    }
2712
2713    /// Apply an operator over a single-key motion. `op` is the engine `Operator`
2714    /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
2715    /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
2716    /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
2717    /// then calls `apply_op_with_motion`. `total_count` is already the product of
2718    /// the prefix count and any inner count accumulated by the reducer.
2719    ///
2720    /// No-op when `motion_key` does not map to a known motion (engine silently
2721    /// cancels the operator, matching vim's behaviour on unknown motions).
2722    ///
2723    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2724    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
2725    /// re-entering the engine FSM.
2726    pub fn apply_op_motion(
2727        &mut self,
2728        op: crate::vim::Operator,
2729        motion_key: char,
2730        total_count: usize,
2731    ) {
2732        vim::apply_op_motion_key(self, op, motion_key, total_count);
2733    }
2734
2735    /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
2736    /// `total_count` is the product of prefix count and inner count.
2737    ///
2738    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2739    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
2740    /// re-entering the engine FSM.
2741    pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
2742        vim::apply_op_double(self, op, total_count);
2743    }
2744
2745    /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
2746    /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
2747    /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
2748    /// updates `last_change` when `op` is Change (for dot-repeat).
2749    ///
2750    /// `total_count` is the product of prefix count and any inner count
2751    /// accumulated by the reducer — already folded at transition time.
2752    ///
2753    /// Promoted to the public surface in 0.5.14 so the hjkl-vim
2754    /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
2755    /// re-entering the engine FSM. `handle_op_find_target` (used by the
2756    /// chord-init op path) delegates here to avoid logic duplication.
2757    pub fn apply_op_find(
2758        &mut self,
2759        op: crate::vim::Operator,
2760        ch: char,
2761        forward: bool,
2762        till: bool,
2763        total_count: usize,
2764    ) {
2765        vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
2766    }
2767
2768    /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
2769    /// Maps `ch` to a `TextObject` per the standard vim table, calls
2770    /// `apply_op_with_text_object`, and records `last_change` when `op` is
2771    /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
2772    /// matching the engine FSM's behaviour on unrecognised text-object chars.
2773    ///
2774    /// `total_count` is accepted for API symmetry with `apply_op_motion` /
2775    /// `apply_op_find` but is currently unused — text objects don't repeat in
2776    /// vim's current grammar. Kept for future-proofing.
2777    ///
2778    /// Promoted to the public surface in 0.5.15 so the hjkl-vim
2779    /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
2780    /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
2781    /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
2782    /// duplication.
2783    pub fn apply_op_text_obj(
2784        &mut self,
2785        op: crate::vim::Operator,
2786        ch: char,
2787        inner: bool,
2788        total_count: usize,
2789    ) {
2790        vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
2791    }
2792
2793    /// Apply an operator over a g-chord motion or case-op linewise form
2794    /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
2795    ///
2796    /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
2797    ///   letter (`U`/`u`/`~`), executes the line op (linewise form).
2798    /// - Otherwise maps `ch` to a motion:
2799    ///   - `'g'` → `Motion::FileTop` (gg)
2800    ///   - `'e'` → `Motion::WordEndBack` (ge)
2801    ///   - `'E'` → `Motion::BigWordEndBack` (gE)
2802    ///   - `'j'` → `Motion::ScreenDown` (gj)
2803    ///   - `'k'` → `Motion::ScreenUp` (gk)
2804    ///   - unknown → no-op (silently ignored, matching engine FSM behaviour)
2805    /// - Updates `last_change` for dot-repeat when `op` is a change operator.
2806    ///
2807    /// `total_count` is the already-folded product of prefix and inner counts.
2808    ///
2809    /// Promoted to the public surface in 0.5.16 so the hjkl-vim
2810    /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
2811    /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
2812    /// delegates to the shared `apply_op_g_inner` helper to avoid logic
2813    /// duplication.
2814    pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
2815        vim::apply_op_g_inner(self, op, ch, total_count);
2816    }
2817
2818    // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
2819
2820    /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
2821    /// row and the motion's target row. Used by the app layer to compute the
2822    /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
2823    /// format job.
2824    ///
2825    /// Returns `None` when `motion_key` does not map to a known motion (same
2826    /// condition that makes `apply_op_motion` a no-op).
2827    ///
2828    /// The cursor is restored to its original position after the probe —
2829    /// the buffer content is not touched.
2830    pub fn range_for_op_motion(
2831        &mut self,
2832        motion_key: char,
2833        total_count: usize,
2834    ) -> Option<(usize, usize)> {
2835        let start = self.cursor();
2836        // Reuse the same logic as apply_op_motion_key but only read the
2837        // target row — we parse the motion, apply it to move the cursor,
2838        // then immediately restore.
2839        let input = crate::input::Input {
2840            key: crate::input::Key::Char(motion_key),
2841            ctrl: false,
2842            alt: false,
2843            shift: false,
2844        };
2845        let motion = vim::parse_motion(&input)?;
2846        // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
2847        let motion = match motion {
2848            vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
2849                Some((ch, forward, till)) => vim::Motion::Find {
2850                    ch,
2851                    forward: if reverse { !forward } else { forward },
2852                    till,
2853                },
2854                None => return None,
2855            },
2856            m => m,
2857        };
2858        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
2859        let end = self.cursor();
2860        // Restore cursor.
2861        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
2862        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2863        Some((r0, r1))
2864    }
2865
2866    /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
2867    /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
2868    /// linewise forms that don't map to a row range.
2869    ///
2870    /// The cursor is restored after the probe.
2871    pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
2872        let start = self.cursor();
2873        let motion = match ch {
2874            'g' => vim::Motion::FileTop,
2875            'e' => vim::Motion::WordEndBack,
2876            'E' => vim::Motion::BigWordEndBack,
2877            'j' => vim::Motion::ScreenDown,
2878            'k' => vim::Motion::ScreenUp,
2879            _ => return None,
2880        };
2881        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
2882        let end = self.cursor();
2883        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
2884        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2885        Some((r0, r1))
2886    }
2887
2888    /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
2889    /// matched region. Returns `None` when `ch` is not a known text-object
2890    /// kind or the text object could not be resolved (e.g. no enclosing bracket).
2891    ///
2892    /// The buffer is not mutated.
2893    pub fn range_for_op_text_obj(
2894        &self,
2895        ch: char,
2896        inner: bool,
2897        _total_count: usize,
2898    ) -> Option<(usize, usize)> {
2899        let obj = match ch {
2900            'w' => vim::TextObject::Word { big: false },
2901            'W' => vim::TextObject::Word { big: true },
2902            '"' | '\'' | '`' => vim::TextObject::Quote(ch),
2903            '(' | ')' | 'b' => vim::TextObject::Bracket('('),
2904            '[' | ']' => vim::TextObject::Bracket('['),
2905            '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
2906            '<' | '>' => vim::TextObject::Bracket('<'),
2907            'p' => vim::TextObject::Paragraph,
2908            't' => vim::TextObject::XmlTag,
2909            's' => vim::TextObject::Sentence,
2910            _ => return None,
2911        };
2912        let (start, end, _kind) = vim::text_object_range(self, obj, inner)?;
2913        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2914        Some((r0, r1))
2915    }
2916
2917    // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
2918    //
2919    // These do not consume input — the caller (hjkl-vim's visual-mode operator
2920    // path, chunk 4e) has already resolved the range from the visual selection
2921    // before calling in. Normal-mode op dispatch continues to use
2922    // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
2923
2924    /// Delete the region `[start, end)` and stash the removed text in
2925    /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
2926    /// select named registers.
2927    ///
2928    /// Pure range-mutation primitive — does not consume input. Called by
2929    /// hjkl-vim's visual-mode operator path which has already resolved the range
2930    /// from the visual selection.
2931    ///
2932    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2933    /// grammar migration (kryptic-sh/hjkl#70).
2934    pub fn delete_range(
2935        &mut self,
2936        start: (usize, usize),
2937        end: (usize, usize),
2938        kind: crate::vim::RangeKind,
2939        register: char,
2940    ) {
2941        vim::delete_range_bridge(self, start, end, kind, register);
2942    }
2943
2944    /// Yank (copy) the region `[start, end)` into `register` without mutating
2945    /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
2946    /// register; `'a'`–`'z'` select named registers.
2947    ///
2948    /// Pure range-mutation primitive — does not consume input. Called by
2949    /// hjkl-vim's visual-mode operator path which has already resolved the range
2950    /// from the visual selection.
2951    ///
2952    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2953    /// grammar migration (kryptic-sh/hjkl#70).
2954    pub fn yank_range(
2955        &mut self,
2956        start: (usize, usize),
2957        end: (usize, usize),
2958        kind: crate::vim::RangeKind,
2959        register: char,
2960    ) {
2961        vim::yank_range_bridge(self, start, end, kind, register);
2962    }
2963
2964    /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
2965    /// operator). The deleted text is stashed in `register`. On return the
2966    /// editor is in Insert mode; the caller must not issue further normal-mode
2967    /// ops until the insert session ends.
2968    ///
2969    /// Pure range-mutation primitive — does not consume input. Called by
2970    /// hjkl-vim's visual-mode operator path which has already resolved the range
2971    /// from the visual selection.
2972    ///
2973    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2974    /// grammar migration (kryptic-sh/hjkl#70).
2975    pub fn change_range(
2976        &mut self,
2977        start: (usize, usize),
2978        end: (usize, usize),
2979        kind: crate::vim::RangeKind,
2980        register: char,
2981    ) {
2982        vim::change_range_bridge(self, start, end, kind, register);
2983    }
2984
2985    /// Indent (`count > 0`) or outdent (`count < 0`) the row span
2986    /// `[start.0, end.0]`. Column components are ignored — indent is always
2987    /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
2988    /// this call; pass `0` to use the current editor setting. `count == 0` is a
2989    /// no-op.
2990    ///
2991    /// Pure range-mutation primitive — does not consume input. Called by
2992    /// hjkl-vim's visual-mode operator path which has already resolved the range
2993    /// from the visual selection.
2994    ///
2995    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
2996    /// grammar migration (kryptic-sh/hjkl#70).
2997    pub fn indent_range(
2998        &mut self,
2999        start: (usize, usize),
3000        end: (usize, usize),
3001        count: i32,
3002        shiftwidth: u32,
3003    ) {
3004        vim::indent_range_bridge(self, start, end, count, shiftwidth);
3005    }
3006
3007    /// Apply a case transformation (`Operator::Uppercase` /
3008    /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3009    /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3010    /// Yanks registers are left untouched — vim's case operators do not write
3011    /// to registers.
3012    ///
3013    /// Pure range-mutation primitive — does not consume input. Called by
3014    /// hjkl-vim's visual-mode operator path which has already resolved the range
3015    /// from the visual selection.
3016    ///
3017    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3018    /// grammar migration (kryptic-sh/hjkl#70).
3019    pub fn case_range(
3020        &mut self,
3021        start: (usize, usize),
3022        end: (usize, usize),
3023        kind: crate::vim::RangeKind,
3024        op: crate::vim::Operator,
3025    ) {
3026        vim::case_range_bridge(self, start, end, kind, op);
3027    }
3028
3029    // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3030    //
3031    // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3032    // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3033    // Ragged-edge handling (short lines not reaching `right_col`) matches the
3034    // engine FSM's `apply_block_operator` path — short lines lose only the
3035    // chars that exist.
3036    //
3037    // `register` is the target register; `'"'` selects the unnamed register.
3038
3039    /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3040    /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3041    /// bounds at the visual (display) column level. Ragged-edge handling
3042    /// matches engine FSM's VisualBlock op behavior — short lines that don't
3043    /// reach `right_col` lose only the chars that exist.
3044    ///
3045    /// `register` honors the user's pending register selection.
3046    ///
3047    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3048    pub fn delete_block(
3049        &mut self,
3050        top_row: usize,
3051        bot_row: usize,
3052        left_col: usize,
3053        right_col: usize,
3054        register: char,
3055    ) {
3056        vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3057    }
3058
3059    /// Yank a rectangular VisualBlock selection into `register` without
3060    /// mutating the buffer. `'"'` selects the unnamed register.
3061    ///
3062    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3063    pub fn yank_block(
3064        &mut self,
3065        top_row: usize,
3066        bot_row: usize,
3067        left_col: usize,
3068        right_col: usize,
3069        register: char,
3070    ) {
3071        vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3072    }
3073
3074    /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3075    /// operator). The deleted text is stashed in `register`. Mode is Insert
3076    /// on return; the caller must not issue further normal-mode ops until the
3077    /// insert session ends.
3078    ///
3079    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3080    pub fn change_block(
3081        &mut self,
3082        top_row: usize,
3083        bot_row: usize,
3084        left_col: usize,
3085        right_col: usize,
3086        register: char,
3087    ) {
3088        vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3089    }
3090
3091    /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3092    /// Column bounds are ignored — vim's block indent is always linewise.
3093    /// `count == 0` is a no-op.
3094    ///
3095    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3096    pub fn indent_block(
3097        &mut self,
3098        top_row: usize,
3099        bot_row: usize,
3100        _left_col: usize,
3101        _right_col: usize,
3102        count: i32,
3103    ) {
3104        vim::indent_block_bridge(self, top_row, bot_row, count);
3105    }
3106
3107    /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3108    /// Column components are ignored — auto-indent is always linewise.
3109    ///
3110    /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3111    /// row 0 to compute the correct depth at `start.0`, then for each line in
3112    /// the target range strips existing leading whitespace and prepends
3113    /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3114    /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3115    /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3116    /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3117    ///
3118    /// After the operation the cursor lands on the first non-whitespace
3119    /// character of `start_row` (vim parity for `==`).
3120    ///
3121    /// **v1 limitation**: the bracket scan does not detect brackets inside
3122    /// string literals or comments. Code such as `let s = "{";` will increment
3123    /// the depth counter even though the brace is not a structural opener.
3124    /// Tree-sitter / LSP indentation is deferred to a follow-up.
3125    pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3126        vim::auto_indent_range_bridge(self, start, end);
3127    }
3128
3129    /// Drain the row range set by the most recent auto-indent operation.
3130    ///
3131    /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3132    /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3133    /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3134    /// uses this to arm a brief visual flash over the reindented rows.
3135    pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3136        self.last_indent_range.take()
3137    }
3138
3139    // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
3140    //
3141    // Pure functions — no cursor mutation, no mode change, no register write.
3142    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3143    // the existing `word_text_object` private resolver in vim.rs.
3144    //
3145    // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
3146    // before invoking a range-mutation primitive (`delete_range`, etc.).
3147    //
3148    // Return value: `Some((start, end))` where both positions are `(row, col)`
3149    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3150    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3151    // Returns `None` when the cursor is on an empty line or the resolver cannot
3152    // find a word boundary.
3153
3154    /// Resolve the range of `iw` (inner word) at the current cursor position.
3155    ///
3156    /// An inner word is the contiguous run of keyword characters (or punctuation
3157    /// characters if the cursor is on punctuation) under the cursor, without any
3158    /// surrounding whitespace. Whitespace-only positions return `None`.
3159    ///
3160    /// Pure function — does not move the cursor or change any editor state.
3161    /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
3162    /// invoking a range-mutation primitive (`delete_range`, etc.).
3163    ///
3164    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3165    /// migration (kryptic-sh/hjkl#70).
3166    pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
3167        vim::text_object_inner_word_bridge(self)
3168    }
3169
3170    /// Resolve the range of `aw` (around word) at the current cursor position.
3171    ///
3172    /// Like `iw` but extends the range to include trailing whitespace after the
3173    /// word. If no trailing whitespace exists, leading whitespace before the word
3174    /// is absorbed instead (vim `:help text-objects` behaviour).
3175    ///
3176    /// Pure function — does not move the cursor or change any editor state.
3177    ///
3178    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3179    /// migration (kryptic-sh/hjkl#70).
3180    pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
3181        vim::text_object_around_word_bridge(self)
3182    }
3183
3184    /// Resolve the range of `iW` (inner WORD) at the current cursor position.
3185    ///
3186    /// A WORD is any contiguous run of non-whitespace characters — punctuation
3187    /// is not treated as a word boundary. Returns the span of the WORD under the
3188    /// cursor, without surrounding whitespace.
3189    ///
3190    /// Pure function — does not move the cursor or change any editor state.
3191    ///
3192    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3193    /// migration (kryptic-sh/hjkl#70).
3194    pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3195        vim::text_object_inner_big_word_bridge(self)
3196    }
3197
3198    /// Resolve the range of `aW` (around WORD) at the current cursor position.
3199    ///
3200    /// Like `iW` but extends the range to include trailing whitespace after the
3201    /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
3202    /// is absorbed instead.
3203    ///
3204    /// Pure function — does not move the cursor or change any editor state.
3205    ///
3206    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3207    /// migration (kryptic-sh/hjkl#70).
3208    pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3209        vim::text_object_around_big_word_bridge(self)
3210    }
3211
3212    // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
3213    //
3214    // Pure functions — no cursor mutation, no mode change, no register write.
3215    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3216    // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
3217    // in vim.rs.
3218    //
3219    // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
3220    // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
3221    // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
3222    // hjkl-vim grammar layer normalises close→open before calling these methods.
3223    //
3224    // Return value: `Some((start, end))` where both positions are `(row, col)`
3225    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3226    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3227    // `bracket_text_object` internally distinguishes Linewise vs Exclusive
3228    // ranges for multi-line pairs; that tag is stripped here — callers receive
3229    // the same flat shape as all other text-object resolvers.
3230
3231    /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
3232    ///
3233    /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
3234    /// cursor's line contains fewer than two occurrences of `quote`, or when no
3235    /// matching pair can be found around or ahead of the cursor.
3236    ///
3237    /// Inner range excludes the quote characters themselves.
3238    ///
3239    /// Pure function — no cursor mutation.
3240    ///
3241    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3242    /// migration (kryptic-sh/hjkl#70).
3243    pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
3244        vim::text_object_inner_quote_bridge(self, quote)
3245    }
3246
3247    /// Resolve the range of `a<quote>` (around quote) at the cursor position.
3248    ///
3249    /// Like `i<quote>` but includes the quote characters themselves plus
3250    /// surrounding whitespace on one side: trailing whitespace after the closing
3251    /// quote if any exists; otherwise leading whitespace before the opening
3252    /// quote. This matches vim `:help text-objects` behaviour.
3253    ///
3254    /// Pure function — no cursor mutation.
3255    ///
3256    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3257    /// migration (kryptic-sh/hjkl#70).
3258    pub fn text_object_around_quote(
3259        &self,
3260        quote: char,
3261    ) -> Option<((usize, usize), (usize, usize))> {
3262        vim::text_object_around_quote_bridge(self, quote)
3263    }
3264
3265    /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
3266    ///
3267    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
3268    /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
3269    /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
3270    /// calling this method. Returns `None` when no enclosing pair is found.
3271    ///
3272    /// The cursor may be anywhere inside the pair or on a bracket character
3273    /// itself. When not inside any pair the resolver falls back to a forward
3274    /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
3275    ///
3276    /// Inner range excludes the bracket characters. Multi-line pairs are
3277    /// supported; the returned range spans the full content between the
3278    /// brackets.
3279    ///
3280    /// Pure function — no cursor mutation.
3281    ///
3282    /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
3283    /// handled here.
3284    ///
3285    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3286    /// migration (kryptic-sh/hjkl#70).
3287    pub fn text_object_inner_bracket(
3288        &self,
3289        open: char,
3290    ) -> Option<((usize, usize), (usize, usize))> {
3291        vim::text_object_inner_bracket_bridge(self, open)
3292    }
3293
3294    /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
3295    ///
3296    /// Like `i<bracket>` but includes the bracket characters themselves.
3297    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
3298    ///
3299    /// Pure function — no cursor mutation.
3300    ///
3301    /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
3302    ///
3303    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3304    /// migration (kryptic-sh/hjkl#70).
3305    pub fn text_object_around_bracket(
3306        &self,
3307        open: char,
3308    ) -> Option<((usize, usize), (usize, usize))> {
3309        vim::text_object_around_bracket_bridge(self, open)
3310    }
3311
3312    // ── Sentence text objects (is / as) ───────────────────────────────────
3313
3314    /// Resolve `is` (inner sentence) at the cursor position.
3315    ///
3316    /// Returns the range of the current sentence, excluding trailing
3317    /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
3318    /// `?` / `!` followed by whitespace or end-of-paragraph).
3319    ///
3320    /// Pure function — no cursor mutation.
3321    ///
3322    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3323    /// grammar migration (kryptic-sh/hjkl#70).
3324    pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3325        vim::text_object_inner_sentence_bridge(self)
3326    }
3327
3328    /// Resolve `as` (around sentence) at the cursor position.
3329    ///
3330    /// Like `is` but includes trailing whitespace after the sentence
3331    /// terminator.
3332    ///
3333    /// Pure function — no cursor mutation.
3334    ///
3335    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3336    /// grammar migration (kryptic-sh/hjkl#70).
3337    pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3338        vim::text_object_around_sentence_bridge(self)
3339    }
3340
3341    // ── Paragraph text objects (ip / ap) ──────────────────────────────────
3342
3343    /// Resolve `ip` (inner paragraph) at the cursor position.
3344    ///
3345    /// A paragraph is a block of non-blank lines bounded by blank lines or
3346    /// buffer edges. Returns `None` when the cursor is on a blank line.
3347    ///
3348    /// Pure function — no cursor mutation.
3349    ///
3350    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3351    /// grammar migration (kryptic-sh/hjkl#70).
3352    pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3353        vim::text_object_inner_paragraph_bridge(self)
3354    }
3355
3356    /// Resolve `ap` (around paragraph) at the cursor position.
3357    ///
3358    /// Like `ip` but includes one trailing blank line when present.
3359    ///
3360    /// Pure function — no cursor mutation.
3361    ///
3362    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3363    /// grammar migration (kryptic-sh/hjkl#70).
3364    pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3365        vim::text_object_around_paragraph_bridge(self)
3366    }
3367
3368    // ── Tag text objects (it / at) ────────────────────────────────────────
3369
3370    /// Resolve `it` (inner tag) at the cursor position.
3371    ///
3372    /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
3373    /// inner content between the open and close tags (excluding the tags
3374    /// themselves).
3375    ///
3376    /// Pure function — no cursor mutation.
3377    ///
3378    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3379    /// grammar migration (kryptic-sh/hjkl#70).
3380    pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3381        vim::text_object_inner_tag_bridge(self)
3382    }
3383
3384    /// Resolve `at` (around tag) at the cursor position.
3385    ///
3386    /// Like `it` but includes the open and close tag delimiters themselves.
3387    ///
3388    /// Pure function — no cursor mutation.
3389    ///
3390    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3391    /// grammar migration (kryptic-sh/hjkl#70).
3392    pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3393        vim::text_object_around_tag_bridge(self)
3394    }
3395
3396    /// Execute a named cursor motion `kind` repeated `count` times.
3397    ///
3398    /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
3399    /// motion primitives, bypassing the engine FSM. Identical cursor semantics
3400    /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
3401    /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
3402    /// used by the FSM arms.
3403    ///
3404    /// Introduced in 0.6.1 as the host entry point for Phase 3a of
3405    /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
3406    /// calls this method rather than re-entering the engine FSM.
3407    ///
3408    /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
3409    /// intact for macro-replay coverage (macros re-feed raw keys through the
3410    /// FSM). This method is the keymap / controller path only.
3411    pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
3412        vim::apply_motion_kind(self, kind, count);
3413    }
3414
3415    /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
3416    /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
3417    /// chars are silently ignored (no-op), matching the engine FSM's
3418    /// `handle_select_register` behaviour.
3419    ///
3420    /// Promoted to the public surface in 0.5.17 so the hjkl-vim
3421    /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
3422    /// without re-entering the engine FSM. `handle_select_register` (engine FSM
3423    /// path for macro-replay / defensive coverage) delegates here to avoid
3424    /// logic duplication.
3425    pub fn set_pending_register(&mut self, reg: char) {
3426        if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
3427            self.vim.pending_register = Some(reg);
3428        }
3429        // Invalid chars silently no-op (matches engine FSM behavior).
3430    }
3431
3432    /// Record a mark named `ch` at the current cursor position.
3433    ///
3434    /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
3435    /// rules). Invalid chars are silently ignored (no-op), matching the engine
3436    /// FSM's `handle_set_mark` behaviour.
3437    ///
3438    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3439    /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
3440    /// without re-entering the engine FSM. `handle_set_mark` delegates here.
3441    pub fn set_mark_at_cursor(&mut self, ch: char) {
3442        vim::set_mark_at_cursor(self, ch);
3443    }
3444
3445    /// `.` dot-repeat: replay the last buffered change at the current cursor.
3446    /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
3447    /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
3448    ///
3449    /// Storage of `LastChange` stays inside engine for now; Phase 5c of
3450    /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
3451    /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
3452    /// FSM `.` arm stays for macro-replay defensive coverage.
3453    pub fn replay_last_change(&mut self, count: usize) {
3454        vim::replay_last_change(self, count);
3455    }
3456
3457    /// Jump to the mark named `ch`, linewise (row only; col snaps to first
3458    /// non-blank). Pushes the pre-jump position onto the jumplist if the
3459    /// cursor actually moved.
3460    ///
3461    /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
3462    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3463    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3464    /// are silently ignored (no-op), matching the engine FSM's
3465    /// `handle_goto_mark` behaviour.
3466    ///
3467    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3468    /// `PendingState::GotoMarkLine` reducer can dispatch
3469    /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
3470    pub fn goto_mark_line(&mut self, ch: char) {
3471        vim::goto_mark(self, ch, true);
3472    }
3473
3474    /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
3475    /// pre-jump position onto the jumplist if the cursor actually moved.
3476    ///
3477    /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
3478    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3479    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3480    /// are silently ignored (no-op), matching the engine FSM's
3481    /// `handle_goto_mark` behaviour.
3482    ///
3483    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3484    /// `PendingState::GotoMarkChar` reducer can dispatch
3485    /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
3486    pub fn goto_mark_char(&mut self, ch: char) {
3487        vim::goto_mark(self, ch, false);
3488    }
3489
3490    // ── Macro controller API (Phase 5b) ──────────────────────────────────────
3491
3492    /// Begin recording keystrokes into register `reg`. The caller (app) is
3493    /// responsible for stopping the recording via `stop_macro_record` when the
3494    /// user presses bare `q`.
3495    ///
3496    /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
3497    ///   recording by pre-seeding `recording_keys` with the decoded text of the
3498    ///   matching lowercase register, matching vim's capital-register append
3499    ///   semantics.
3500    /// - Lowercase `reg` clears `recording_keys` (fresh recording).
3501    /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
3502    ///
3503    /// Promoted to the public surface in Phase 5b so the app's
3504    /// `route_chord_key` can start a recording without re-entering the engine
3505    /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
3506    /// defensive coverage) continues to use the same logic via delegation.
3507    pub fn start_macro_record(&mut self, reg: char) {
3508        if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
3509            return;
3510        }
3511        self.vim.recording_macro = Some(reg);
3512        if reg.is_ascii_uppercase() {
3513            // Seed recording_keys with the existing lowercase register's text
3514            // decoded back to inputs so capital-register append continues from
3515            // where the previous recording left off.
3516            let lower = reg.to_ascii_lowercase();
3517            let text = self
3518                .registers
3519                .read(lower)
3520                .map(|s| s.text.clone())
3521                .unwrap_or_default();
3522            self.vim.recording_keys = crate::input::decode_macro(&text);
3523        } else {
3524            self.vim.recording_keys.clear();
3525        }
3526    }
3527
3528    /// Finalize the active recording: encode `recording_keys` as text and write
3529    /// to the matching (lowercase) named register. Clears both `recording_macro`
3530    /// and `recording_keys`. No-ops if no recording is active.
3531    ///
3532    /// Promoted to the public surface in Phase 5b so the app's `QChord` action
3533    /// can stop a recording when the user presses bare `q` without re-entering
3534    /// the engine FSM.
3535    pub fn stop_macro_record(&mut self) {
3536        let Some(reg) = self.vim.recording_macro.take() else {
3537            return;
3538        };
3539        let keys = std::mem::take(&mut self.vim.recording_keys);
3540        let text = crate::input::encode_macro(&keys);
3541        self.set_named_register_text(reg.to_ascii_lowercase(), text);
3542    }
3543
3544    /// Returns `true` while a `q{reg}` recording is in progress.
3545    /// Hosts use this to show a "recording @r" status indicator and to decide
3546    /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
3547    /// chord.
3548    pub fn is_recording_macro(&self) -> bool {
3549        self.vim.recording_macro.is_some()
3550    }
3551
3552    /// Returns `true` while a macro is being replayed. The app sets this flag
3553    /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
3554    /// re-feed loop so the recorder hook can skip double-capture.
3555    pub fn is_replaying_macro(&self) -> bool {
3556        self.vim.replaying_macro
3557    }
3558
3559    /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
3560    /// prepare for replay, returning the inputs the app should re-feed through
3561    /// `route_chord_key`.
3562    ///
3563    /// Resolves `reg`:
3564    /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
3565    /// - Any other char → lowercase it, read the register, decode.
3566    ///
3567    /// Side-effects:
3568    /// - Sets `vim.last_macro` to the resolved register.
3569    /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
3570    ///   replay. The app calls `end_macro_replay` after the loop finishes.
3571    ///
3572    /// Returns an empty vec (and no side-effects for `'@'`) if the register is
3573    /// unset or empty.
3574    pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
3575        let resolved = if reg == '@' {
3576            match self.vim.last_macro {
3577                Some(r) => r,
3578                None => return vec![],
3579            }
3580        } else {
3581            reg.to_ascii_lowercase()
3582        };
3583        let text = match self.registers.read(resolved) {
3584            Some(slot) if !slot.text.is_empty() => slot.text.clone(),
3585            _ => return vec![],
3586        };
3587        let keys = crate::input::decode_macro(&text);
3588        self.vim.last_macro = Some(resolved);
3589        self.vim.replaying_macro = true;
3590        // Multiply by count (minimum 1).
3591        keys.repeat(count.max(1))
3592    }
3593
3594    /// Clear the `replaying_macro` flag. Called by the app after the
3595    /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
3596    pub fn end_macro_replay(&mut self) {
3597        self.vim.replaying_macro = false;
3598    }
3599
3600    /// Append `input` to the active recording (`recording_keys`) if and only
3601    /// if a recording is in progress AND we are not currently replaying.
3602    /// Called by the app's `route_chord_key` recorder hook so that user
3603    /// keystrokes captured through the app-level chord path are recorded
3604    /// (rather than relying solely on the engine FSM's in-step hook).
3605    pub fn record_input(&mut self, input: crate::input::Input) {
3606        if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
3607            self.vim.recording_keys.push(input);
3608        }
3609    }
3610
3611    // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
3612    //
3613    // Each method is the publicly callable form of one insert-mode action.
3614    // All logic lives in the corresponding `vim::*_bridge` free function;
3615    // these methods are thin delegators so the public surface stays on `Editor`.
3616    //
3617    // Invariants (enforced by the bridge fns):
3618    //   - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
3619    //   - Navigation keys call `break_undo_group_in_insert` when the FSM did.
3620    //   - `push_buffer_cursor_to_textarea` is called after every mutation
3621    //     (currently a no-op, kept for migration hygiene).
3622
3623    /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
3624    /// the cursor instead of inserting; at end-of-line, always appends. With
3625    /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
3626    /// dedent on an otherwise-whitespace line.
3627    ///
3628    /// Callers must ensure the editor is in Insert or Replace mode before
3629    /// calling this method.
3630    pub fn insert_char(&mut self, ch: char) {
3631        let mutated = vim::insert_char_bridge(self, ch);
3632        if mutated {
3633            self.mark_content_dirty();
3634            let (row, _) = self.cursor();
3635            self.vim.widen_insert_row(row);
3636        }
3637    }
3638
3639    /// Insert a newline at the cursor, applying autoindent / smartindent to
3640    /// prefix the new line with the appropriate leading whitespace.
3641    ///
3642    /// Callers must ensure the editor is in Insert mode before calling.
3643    pub fn insert_newline(&mut self) {
3644        let mutated = vim::insert_newline_bridge(self);
3645        if mutated {
3646            self.mark_content_dirty();
3647            let (row, _) = self.cursor();
3648            self.vim.widen_insert_row(row);
3649        }
3650    }
3651
3652    /// Insert a tab character (or spaces up to the next `softtabstop` boundary
3653    /// when `expandtab` is set).
3654    ///
3655    /// Callers must ensure the editor is in Insert mode before calling.
3656    pub fn insert_tab(&mut self) {
3657        let mutated = vim::insert_tab_bridge(self);
3658        if mutated {
3659            self.mark_content_dirty();
3660            let (row, _) = self.cursor();
3661            self.vim.widen_insert_row(row);
3662        }
3663    }
3664
3665    /// Delete the character before the cursor (Backspace). With `softtabstop`
3666    /// active, deletes the entire soft-tab run at an aligned boundary. Joins
3667    /// with the previous line when at column 0.
3668    ///
3669    /// Callers must ensure the editor is in Insert mode before calling.
3670    pub fn insert_backspace(&mut self) {
3671        let mutated = vim::insert_backspace_bridge(self);
3672        if mutated {
3673            self.mark_content_dirty();
3674            let (row, _) = self.cursor();
3675            self.vim.widen_insert_row(row);
3676        }
3677    }
3678
3679    /// Delete the character under the cursor (Delete key). Joins with the
3680    /// next line when at end-of-line.
3681    ///
3682    /// Callers must ensure the editor is in Insert mode before calling.
3683    pub fn insert_delete(&mut self) {
3684        let mutated = vim::insert_delete_bridge(self);
3685        if mutated {
3686            self.mark_content_dirty();
3687            let (row, _) = self.cursor();
3688            self.vim.widen_insert_row(row);
3689        }
3690    }
3691
3692    /// Move the cursor one step in `dir` (arrow key), breaking the undo group
3693    /// per `undo_break_on_motion`.
3694    ///
3695    /// Callers must ensure the editor is in Insert mode before calling.
3696    pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
3697        vim::insert_arrow_bridge(self, dir);
3698        let (row, _) = self.cursor();
3699        self.vim.widen_insert_row(row);
3700    }
3701
3702    /// Move the cursor to the start of the current line (Home key), breaking
3703    /// the undo group.
3704    ///
3705    /// Callers must ensure the editor is in Insert mode before calling.
3706    pub fn insert_home(&mut self) {
3707        vim::insert_home_bridge(self);
3708        let (row, _) = self.cursor();
3709        self.vim.widen_insert_row(row);
3710    }
3711
3712    /// Move the cursor to the end of the current line (End key), breaking the
3713    /// undo group.
3714    ///
3715    /// Callers must ensure the editor is in Insert mode before calling.
3716    pub fn insert_end(&mut self) {
3717        vim::insert_end_bridge(self);
3718        let (row, _) = self.cursor();
3719        self.vim.widen_insert_row(row);
3720    }
3721
3722    /// Scroll up one full viewport height (PageUp), moving the cursor with it.
3723    /// `viewport_h` is the current viewport height in rows; pass
3724    /// `self.viewport_height_value()` if the stored value is current.
3725    ///
3726    /// Callers must ensure the editor is in Insert mode before calling.
3727    pub fn insert_pageup(&mut self, viewport_h: u16) {
3728        vim::insert_pageup_bridge(self, viewport_h);
3729        let (row, _) = self.cursor();
3730        self.vim.widen_insert_row(row);
3731    }
3732
3733    /// Scroll down one full viewport height (PageDown), moving the cursor with
3734    /// it. `viewport_h` is the current viewport height in rows.
3735    ///
3736    /// Callers must ensure the editor is in Insert mode before calling.
3737    pub fn insert_pagedown(&mut self, viewport_h: u16) {
3738        vim::insert_pagedown_bridge(self, viewport_h);
3739        let (row, _) = self.cursor();
3740        self.vim.widen_insert_row(row);
3741    }
3742
3743    /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
3744    /// At column 0, joins with the previous line (vim `b`-motion semantics).
3745    ///
3746    /// Callers must ensure the editor is in Insert mode before calling.
3747    pub fn insert_ctrl_w(&mut self) {
3748        let mutated = vim::insert_ctrl_w_bridge(self);
3749        if mutated {
3750            self.mark_content_dirty();
3751            let (row, _) = self.cursor();
3752            self.vim.widen_insert_row(row);
3753        }
3754    }
3755
3756    /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
3757    /// No-op when already at column 0.
3758    ///
3759    /// Callers must ensure the editor is in Insert mode before calling.
3760    pub fn insert_ctrl_u(&mut self) {
3761        let mutated = vim::insert_ctrl_u_bridge(self);
3762        if mutated {
3763            self.mark_content_dirty();
3764            let (row, _) = self.cursor();
3765            self.vim.widen_insert_row(row);
3766        }
3767    }
3768
3769    /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
3770    /// insert mode. Joins with the previous line when at col 0.
3771    ///
3772    /// Callers must ensure the editor is in Insert mode before calling.
3773    pub fn insert_ctrl_h(&mut self) {
3774        let mutated = vim::insert_ctrl_h_bridge(self);
3775        if mutated {
3776            self.mark_content_dirty();
3777            let (row, _) = self.cursor();
3778            self.vim.widen_insert_row(row);
3779        }
3780    }
3781
3782    /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
3783    /// complete normal-mode command, then return to insert automatically.
3784    ///
3785    /// Callers must ensure the editor is in Insert mode before calling.
3786    pub fn insert_ctrl_o_arm(&mut self) {
3787        vim::insert_ctrl_o_bridge(self);
3788    }
3789
3790    /// Arm the register-paste selector (`Ctrl-R`). The next call to
3791    /// `insert_paste_register(reg)` will insert the register contents.
3792    /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
3793    /// the armed state and paste register `c`.
3794    ///
3795    /// Callers must ensure the editor is in Insert mode before calling.
3796    pub fn insert_ctrl_r_arm(&mut self) {
3797        vim::insert_ctrl_r_bridge(self);
3798    }
3799
3800    /// Indent the current line by one `shiftwidth` and shift the cursor right
3801    /// by the same amount (`Ctrl-T`).
3802    ///
3803    /// Callers must ensure the editor is in Insert mode before calling.
3804    pub fn insert_ctrl_t(&mut self) {
3805        let mutated = vim::insert_ctrl_t_bridge(self);
3806        if mutated {
3807            self.mark_content_dirty();
3808            let (row, _) = self.cursor();
3809            self.vim.widen_insert_row(row);
3810        }
3811    }
3812
3813    /// Outdent the current line by up to one `shiftwidth` and shift the cursor
3814    /// left by the amount stripped (`Ctrl-D`).
3815    ///
3816    /// Callers must ensure the editor is in Insert mode before calling.
3817    pub fn insert_ctrl_d(&mut self) {
3818        let mutated = vim::insert_ctrl_d_bridge(self);
3819        if mutated {
3820            self.mark_content_dirty();
3821            let (row, _) = self.cursor();
3822            self.vim.widen_insert_row(row);
3823        }
3824    }
3825
3826    /// Paste the contents of register `reg` at the cursor (the commit arm of
3827    /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
3828    ///
3829    /// Callers must ensure the editor is in Insert mode before calling.
3830    pub fn insert_paste_register(&mut self, reg: char) {
3831        vim::insert_paste_register_bridge(self, reg);
3832        let (row, _) = self.cursor();
3833        self.vim.widen_insert_row(row);
3834    }
3835
3836    /// Exit insert mode to Normal: finish the insert session, step the cursor
3837    /// one cell left (vim convention on Esc), record the `gi` target position,
3838    /// and update the sticky column.
3839    ///
3840    /// Callers must ensure the editor is in Insert mode before calling.
3841    pub fn leave_insert_to_normal(&mut self) {
3842        vim::leave_insert_to_normal_bridge(self);
3843    }
3844
3845    // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
3846    //
3847    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
3848    // `vim.rs` following the same pattern as Phase 6.1. The FSM's
3849    // `handle_normal_only` now calls the same bridges so both paths are
3850    // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
3851
3852    /// `i` — transition to Insert mode at the current cursor position.
3853    /// `count` is stored in the insert session and replayed by dot-repeat
3854    /// as a repeat count on the inserted text.
3855    pub fn enter_insert_i(&mut self, count: usize) {
3856        vim::enter_insert_i_bridge(self, count);
3857    }
3858
3859    /// `I` — move to the first non-blank character on the line, then
3860    /// transition to Insert mode. `count` is stored for dot-repeat.
3861    pub fn enter_insert_shift_i(&mut self, count: usize) {
3862        vim::enter_insert_shift_i_bridge(self, count);
3863    }
3864
3865    /// `a` — advance the cursor one cell past the current position, then
3866    /// transition to Insert mode (append). `count` is stored for dot-repeat.
3867    pub fn enter_insert_a(&mut self, count: usize) {
3868        vim::enter_insert_a_bridge(self, count);
3869    }
3870
3871    /// `A` — move the cursor to the end of the line, then transition to
3872    /// Insert mode (append at end). `count` is stored for dot-repeat.
3873    pub fn enter_insert_shift_a(&mut self, count: usize) {
3874        vim::enter_insert_shift_a_bridge(self, count);
3875    }
3876
3877    /// `o` — open a new line below the current line with smart-indent, then
3878    /// transition to Insert mode. `count` is stored for dot-repeat replay.
3879    pub fn open_line_below(&mut self, count: usize) {
3880        vim::open_line_below_bridge(self, count);
3881    }
3882
3883    /// `O` — open a new line above the current line with smart-indent, then
3884    /// transition to Insert mode. `count` is stored for dot-repeat replay.
3885    pub fn open_line_above(&mut self, count: usize) {
3886        vim::open_line_above_bridge(self, count);
3887    }
3888
3889    /// `R` — enter Replace mode: subsequent typed characters overstrike the
3890    /// cell under the cursor rather than inserting. `count` is for replay.
3891    pub fn enter_replace_mode(&mut self, count: usize) {
3892        vim::enter_replace_mode_bridge(self, count);
3893    }
3894
3895    /// `x` — delete `count` characters forward from the cursor and write them
3896    /// to the unnamed register. No-op on an empty line. Records for `.`.
3897    pub fn delete_char_forward(&mut self, count: usize) {
3898        vim::delete_char_forward_bridge(self, count);
3899    }
3900
3901    /// `X` — delete `count` characters backward from the cursor and write
3902    /// them to the unnamed register. No-op at column 0. Records for `.`.
3903    pub fn delete_char_backward(&mut self, count: usize) {
3904        vim::delete_char_backward_bridge(self, count);
3905    }
3906
3907    /// `s` — substitute `count` characters: delete them (writing to the
3908    /// unnamed register) then enter Insert mode. Equivalent to `cl`.
3909    /// Records as `OpMotion { Change, Right }` for dot-repeat.
3910    pub fn substitute_char(&mut self, count: usize) {
3911        vim::substitute_char_bridge(self, count);
3912    }
3913
3914    /// `S` — substitute the current line: wipe its contents (writing to the
3915    /// unnamed register) then enter Insert mode. Equivalent to `cc`.
3916    /// Records as `LineOp { Change }` for dot-repeat.
3917    pub fn substitute_line(&mut self, count: usize) {
3918        vim::substitute_line_bridge(self, count);
3919    }
3920
3921    /// `D` — delete from the cursor to end-of-line, writing to the unnamed
3922    /// register. The cursor parks on the new last character. Records for `.`.
3923    pub fn delete_to_eol(&mut self) {
3924        vim::delete_to_eol_bridge(self);
3925    }
3926
3927    /// `C` — change from the cursor to end-of-line: delete to EOL then enter
3928    /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
3929    /// (the insert session records `DeleteToEol` on exit, like `c` motions).
3930    pub fn change_to_eol(&mut self) {
3931        vim::change_to_eol_bridge(self);
3932    }
3933
3934    /// `Y` — yank from the cursor to end-of-line into the unnamed register.
3935    /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
3936    pub fn yank_to_eol(&mut self, count: usize) {
3937        vim::yank_to_eol_bridge(self, count);
3938    }
3939
3940    /// `J` — join `count` lines (default 2) onto the current line, inserting
3941    /// a single space between each non-empty pair. Records for dot-repeat.
3942    pub fn join_line(&mut self, count: usize) {
3943        vim::join_line_bridge(self, count);
3944    }
3945
3946    /// `~` — toggle the case of `count` characters from the cursor, advancing
3947    /// right after each toggle. Records `ToggleCase` for dot-repeat.
3948    pub fn toggle_case_at_cursor(&mut self, count: usize) {
3949        vim::toggle_case_at_cursor_bridge(self, count);
3950    }
3951
3952    /// `p` — paste the unnamed register (or the register selected via `"r`)
3953    /// after the cursor. Linewise content opens a new line below; charwise
3954    /// content is inserted inline. Records `Paste { before: false }` for `.`.
3955    pub fn paste_after(&mut self, count: usize) {
3956        vim::paste_after_bridge(self, count);
3957    }
3958
3959    /// `P` — paste the unnamed register (or the `"r` register) before the
3960    /// cursor. Linewise content opens a new line above; charwise is inline.
3961    /// Records `Paste { before: true }` for dot-repeat.
3962    pub fn paste_before(&mut self, count: usize) {
3963        vim::paste_before_bridge(self, count);
3964    }
3965
3966    /// `<C-o>` — jump back `count` entries in the jumplist, saving the
3967    /// current position on the forward stack so `<C-i>` can return.
3968    pub fn jump_back(&mut self, count: usize) {
3969        vim::jump_back_bridge(self, count);
3970    }
3971
3972    /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
3973    /// saving the current position on the backward stack.
3974    pub fn jump_forward(&mut self, count: usize) {
3975        vim::jump_forward_bridge(self, count);
3976    }
3977
3978    /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
3979    /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
3980    /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
3981    pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
3982        vim::scroll_full_page_bridge(self, dir, count);
3983    }
3984
3985    /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
3986    /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
3987    pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
3988        vim::scroll_half_page_bridge(self, dir, count);
3989    }
3990
3991    /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
3992    /// the cursor (cursor is clamped to the new visible region if necessary).
3993    /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
3994    pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
3995        vim::scroll_line_bridge(self, dir, count);
3996    }
3997
3998    /// `n` — repeat the last `/` or `?` search `count` times in its original
3999    /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
4000    pub fn search_repeat(&mut self, forward: bool, count: usize) {
4001        vim::search_repeat_bridge(self, forward, count);
4002    }
4003
4004    /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
4005    /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
4006    /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
4007    pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
4008        vim::word_search_bridge(self, forward, whole_word, count);
4009    }
4010
4011    // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
4012    //
4013    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4014    // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
4015    // and these wrappers write `current_mode` so `vim_mode()` returns correct
4016    // values regardless of which path performed the transition.
4017    // See kryptic-sh/hjkl#89 for the full promotion plan.
4018
4019    /// `v` from Normal — enter charwise Visual mode, anchoring the selection
4020    /// at the current cursor position.
4021    pub fn enter_visual_char(&mut self) {
4022        vim::enter_visual_char_bridge(self);
4023    }
4024
4025    /// `V` from Normal — enter linewise Visual mode, anchoring on the current
4026    /// line. Motions extend the selection by whole lines.
4027    pub fn enter_visual_line(&mut self) {
4028        vim::enter_visual_line_bridge(self);
4029    }
4030
4031    /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
4032    /// rectangle whose corners are the anchor and the live cursor.
4033    pub fn enter_visual_block(&mut self) {
4034        vim::enter_visual_block_bridge(self);
4035    }
4036
4037    /// Esc from any visual mode — set `<` / `>` marks, stash the selection
4038    /// for `gv` re-entry, then return to Normal mode.
4039    pub fn exit_visual_to_normal(&mut self) {
4040        vim::exit_visual_to_normal_bridge(self);
4041    }
4042
4043    /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
4044    /// so the user can extend the other end of the selection. Does NOT
4045    /// mutate the selection range; only the active endpoint changes.
4046    pub fn visual_o_toggle(&mut self) {
4047        vim::visual_o_toggle_bridge(self);
4048    }
4049
4050    /// `gv` — restore the last visual selection (mode + anchor + cursor
4051    /// position). No-op when no visual selection has been exited yet.
4052    pub fn reenter_last_visual(&mut self) {
4053        vim::reenter_last_visual_bridge(self);
4054    }
4055
4056    /// Direct mode-transition entry point. Sets both the internal FSM mode
4057    /// and the stable `current_mode` field read by [`Editor::vim_mode`].
4058    ///
4059    /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
4060    /// …) which also set up required bookkeeping (anchors, sessions, …).
4061    /// Use `set_mode` only when you need a raw mode flip without side-effects.
4062    pub fn set_mode(&mut self, mode: VimMode) {
4063        vim::set_mode_bridge(self, mode);
4064    }
4065}
4066
4067// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
4068//
4069// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
4070// `Editor` accessors and mutators defined in this block. Each method gets a
4071// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
4072// rather than individual getters + setters (e.g. `accumulate_count_digit`).
4073
4074/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
4075///
4076/// Treat as opaque — construct by calling `begin_step` and pass the
4077/// returned value directly into `end_step` without modification.
4078/// The fields capture per-step pre-dispatch state that the epilogue
4079/// needs to run its invariants correctly.
4080pub struct StepBookkeeping {
4081    /// True when the pending chord before this step was a macro-chord
4082    /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
4083    /// keys so that only the *payload* keys enter `recording_keys`.
4084    pub pending_was_macro_chord: bool,
4085    /// True when the mode was Insert *before* the FSM body ran. Used by
4086    /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
4087    /// back into Insert.
4088    pub was_insert: bool,
4089    /// Pre-dispatch visual snapshot. When the FSM body transitions out of
4090    /// a visual mode the epilogue uses this to set the `<`/`>` marks and
4091    /// store `last_visual` for `gv`.
4092    pub pre_visual_snapshot: Option<vim::LastVisual>,
4093}
4094
4095impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
4096    // ── Pending chord ─────────────────────────────────────────────────────────
4097
4098    /// Return a clone of the current pending chord state.
4099    pub fn pending(&self) -> vim::Pending {
4100        self.vim.pending.clone()
4101    }
4102
4103    /// Overwrite the pending chord state.
4104    pub fn set_pending(&mut self, p: vim::Pending) {
4105        self.vim.pending = p;
4106    }
4107
4108    /// Atomically take the pending chord, replacing it with `Pending::None`.
4109    pub fn take_pending(&mut self) -> vim::Pending {
4110        std::mem::take(&mut self.vim.pending)
4111    }
4112
4113    // ── Count prefix ──────────────────────────────────────────────────────────
4114
4115    /// Return the raw digit-prefix count (`0` = no prefix typed yet).
4116    pub fn count(&self) -> usize {
4117        self.vim.count
4118    }
4119
4120    /// Overwrite the digit-prefix count directly.
4121    pub fn set_count(&mut self, c: usize) {
4122        self.vim.count = c;
4123    }
4124
4125    /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
4126    pub fn accumulate_count_digit(&mut self, digit: usize) {
4127        self.vim.count = self.vim.count.saturating_mul(10) + digit;
4128    }
4129
4130    /// Reset the count prefix to zero (no pending count).
4131    pub fn reset_count(&mut self) {
4132        self.vim.count = 0;
4133    }
4134
4135    /// Consume the count and return it; resets to zero. Returns `1` when no
4136    /// prefix was typed (mirrors `take_count` in vim.rs).
4137    pub fn take_count(&mut self) -> usize {
4138        if self.vim.count > 0 {
4139            let n = self.vim.count;
4140            self.vim.count = 0;
4141            n
4142        } else {
4143            1
4144        }
4145    }
4146
4147    // ── Internal FSM mode ─────────────────────────────────────────────────────
4148
4149    /// Return the FSM-internal mode (Normal / Insert / Visual / …).
4150    pub fn fsm_mode(&self) -> vim::Mode {
4151        self.vim.mode
4152    }
4153
4154    /// Overwrite the FSM-internal mode without side-effects. Prefer the
4155    /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
4156    pub fn set_fsm_mode(&mut self, m: vim::Mode) {
4157        self.vim.mode = m;
4158        self.vim.current_mode = self.vim.public_mode();
4159    }
4160
4161    // ── Replaying flag ────────────────────────────────────────────────────────
4162
4163    /// `true` while the `.` dot-repeat replay is running.
4164    pub fn is_replaying(&self) -> bool {
4165        self.vim.replaying
4166    }
4167
4168    /// Set or clear the dot-replay flag.
4169    pub fn set_replaying(&mut self, v: bool) {
4170        self.vim.replaying = v;
4171    }
4172
4173    // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
4174
4175    /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
4176    /// to Insert after the next complete command.
4177    pub fn is_one_shot_normal(&self) -> bool {
4178        self.vim.one_shot_normal
4179    }
4180
4181    /// Set or clear the Ctrl-o one-shot-normal flag.
4182    pub fn set_one_shot_normal(&mut self, v: bool) {
4183        self.vim.one_shot_normal = v;
4184    }
4185
4186    // ── Last find (f/F/t/T target) ────────────────────────────────────────────
4187
4188    /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
4189    /// `None` before any find command was executed.
4190    pub fn last_find(&self) -> Option<(char, bool, bool)> {
4191        self.vim.last_find
4192    }
4193
4194    /// Overwrite the stored last-find target.
4195    pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
4196        self.vim.last_find = target;
4197    }
4198
4199    // ── Last change (dot-repeat payload) ─────────────────────────────────────
4200
4201    /// Return a clone of the last recorded mutating change, or `None` before
4202    /// any change has been made.
4203    pub fn last_change(&self) -> Option<vim::LastChange> {
4204        self.vim.last_change.clone()
4205    }
4206
4207    /// Overwrite the stored last-change record.
4208    pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
4209        self.vim.last_change = lc;
4210    }
4211
4212    /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
4213    /// field after the insert session completes).
4214    pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
4215        self.vim.last_change.as_mut()
4216    }
4217
4218    // ── Insert session ────────────────────────────────────────────────────────
4219
4220    /// Borrow the active insert session, or `None` when not in Insert mode.
4221    pub fn insert_session(&self) -> Option<&vim::InsertSession> {
4222        self.vim.insert_session.as_ref()
4223    }
4224
4225    /// Borrow the active insert session mutably.
4226    pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
4227        self.vim.insert_session.as_mut()
4228    }
4229
4230    /// Atomically take the insert session out, leaving `None`.
4231    pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
4232        self.vim.insert_session.take()
4233    }
4234
4235    /// Install a new insert session, replacing any existing one.
4236    pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
4237        self.vim.insert_session = s;
4238    }
4239
4240    // ── Visual anchors ────────────────────────────────────────────────────────
4241
4242    /// Return the charwise Visual-mode anchor `(row, col)`.
4243    pub fn visual_anchor(&self) -> (usize, usize) {
4244        self.vim.visual_anchor
4245    }
4246
4247    /// Overwrite the charwise Visual-mode anchor.
4248    pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
4249        self.vim.visual_anchor = anchor;
4250    }
4251
4252    /// Return the VisualLine anchor row.
4253    pub fn visual_line_anchor(&self) -> usize {
4254        self.vim.visual_line_anchor
4255    }
4256
4257    /// Overwrite the VisualLine anchor row.
4258    pub fn set_visual_line_anchor(&mut self, row: usize) {
4259        self.vim.visual_line_anchor = row;
4260    }
4261
4262    /// Return the VisualBlock anchor `(row, col)`.
4263    pub fn block_anchor(&self) -> (usize, usize) {
4264        self.vim.block_anchor
4265    }
4266
4267    /// Overwrite the VisualBlock anchor.
4268    pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
4269        self.vim.block_anchor = anchor;
4270    }
4271
4272    /// Return the VisualBlock virtual column used to survive j/k row clamping.
4273    pub fn block_vcol(&self) -> usize {
4274        self.vim.block_vcol
4275    }
4276
4277    /// Overwrite the VisualBlock virtual column.
4278    pub fn set_block_vcol(&mut self, vcol: usize) {
4279        self.vim.block_vcol = vcol;
4280    }
4281
4282    // ── Yank linewise flag ────────────────────────────────────────────────────
4283
4284    /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
4285    pub fn yank_linewise(&self) -> bool {
4286        self.vim.yank_linewise
4287    }
4288
4289    /// Set or clear the linewise-yank flag.
4290    pub fn set_yank_linewise(&mut self, v: bool) {
4291        self.vim.yank_linewise = v;
4292    }
4293
4294    // ── Pending register selector ─────────────────────────────────────────────
4295    // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
4296    // Only the mutators are new here.
4297
4298    /// Overwrite the pending register selector (Phase 6.6b mutator companion to
4299    /// the existing `pending_register()` getter).
4300    pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
4301        self.vim.pending_register = reg;
4302    }
4303
4304    /// Atomically take the pending register, returning `None` afterward.
4305    pub fn take_pending_register_raw(&mut self) -> Option<char> {
4306        self.vim.pending_register.take()
4307    }
4308
4309    // ── Macro recording ───────────────────────────────────────────────────────
4310
4311    /// Return the register currently being recorded into, or `None`.
4312    pub fn recording_macro(&self) -> Option<char> {
4313        self.vim.recording_macro
4314    }
4315
4316    /// Overwrite the recording-macro target register.
4317    pub fn set_recording_macro(&mut self, reg: Option<char>) {
4318        self.vim.recording_macro = reg;
4319    }
4320
4321    /// Append one input to the in-progress macro recording buffer.
4322    pub fn push_recording_key(&mut self, input: crate::input::Input) {
4323        self.vim.recording_keys.push(input);
4324    }
4325
4326    /// Atomically take the recorded key sequence, leaving an empty vec.
4327    pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
4328        std::mem::take(&mut self.vim.recording_keys)
4329    }
4330
4331    /// Overwrite the recording-keys buffer (e.g. to seed from a register).
4332    pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
4333        self.vim.recording_keys = keys;
4334    }
4335
4336    /// Return the number of keys currently in the recording buffer.
4337    /// Useful for integration tests that verify macro-recording bookkeeping
4338    /// without draining the buffer via [`take_recording_keys`].
4339    pub fn recording_keys_len(&self) -> usize {
4340        self.vim.recording_keys.len()
4341    }
4342
4343    // ── Macro replay flag ─────────────────────────────────────────────────────
4344
4345    /// `true` while `@reg` macro replay is running (suppresses re-recording).
4346    pub fn is_replaying_macro_raw(&self) -> bool {
4347        self.vim.replaying_macro
4348    }
4349
4350    /// Set or clear the macro-replay-in-progress flag.
4351    pub fn set_replaying_macro_raw(&mut self, v: bool) {
4352        self.vim.replaying_macro = v;
4353    }
4354
4355    // ── Last macro register ───────────────────────────────────────────────────
4356
4357    /// Return the register of the most recently played macro (`@@` source).
4358    pub fn last_macro(&self) -> Option<char> {
4359        self.vim.last_macro
4360    }
4361
4362    /// Overwrite the last-played-macro register.
4363    pub fn set_last_macro(&mut self, reg: Option<char>) {
4364        self.vim.last_macro = reg;
4365    }
4366
4367    // ── Last insert position ──────────────────────────────────────────────────
4368
4369    /// Return the cursor position when Insert mode was last exited (for `gi`).
4370    pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
4371        self.vim.last_insert_pos
4372    }
4373
4374    /// Overwrite the stored last-insert position.
4375    pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
4376        self.vim.last_insert_pos = pos;
4377    }
4378
4379    // ── Last visual selection ─────────────────────────────────────────────────
4380
4381    /// Return the saved visual selection snapshot for `gv`, or `None`.
4382    pub fn last_visual(&self) -> Option<vim::LastVisual> {
4383        self.vim.last_visual
4384    }
4385
4386    /// Overwrite the saved visual selection snapshot.
4387    pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
4388        self.vim.last_visual = snap;
4389    }
4390
4391    // ── Viewport-pinned flag ──────────────────────────────────────────────────
4392
4393    /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
4394    /// the end-of-step scrolloff pass).
4395    pub fn viewport_pinned(&self) -> bool {
4396        self.vim.viewport_pinned
4397    }
4398
4399    /// Set or clear the viewport-pinned flag.
4400    pub fn set_viewport_pinned(&mut self, v: bool) {
4401        self.vim.viewport_pinned = v;
4402    }
4403
4404    // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
4405
4406    /// `true` while waiting for the register-name key after `Ctrl-R` in
4407    /// Insert mode.
4408    pub fn insert_pending_register(&self) -> bool {
4409        self.vim.insert_pending_register
4410    }
4411
4412    /// Set or clear the `Ctrl-R` register-wait flag.
4413    pub fn set_insert_pending_register(&mut self, v: bool) {
4414        self.vim.insert_pending_register = v;
4415    }
4416
4417    // ── Change-mark start ─────────────────────────────────────────────────────
4418
4419    /// Return the stashed `[` mark start for a Change operation, or `None`.
4420    pub fn change_mark_start(&self) -> Option<(usize, usize)> {
4421        self.vim.change_mark_start
4422    }
4423
4424    /// Atomically take the change-mark start, leaving `None`.
4425    pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
4426        self.vim.change_mark_start.take()
4427    }
4428
4429    /// Overwrite the change-mark start.
4430    pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
4431        self.vim.change_mark_start = pos;
4432    }
4433
4434    // ── Timeout tracking ──────────────────────────────────────────────────────
4435
4436    /// Return the wall-clock `Instant` of the last keystroke.
4437    pub fn last_input_at(&self) -> Option<std::time::Instant> {
4438        self.vim.last_input_at
4439    }
4440
4441    /// Overwrite the wall-clock last-input timestamp.
4442    pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
4443        self.vim.last_input_at = t;
4444    }
4445
4446    /// Return the `Host::now()` duration at the last keystroke.
4447    pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
4448        self.vim.last_input_host_at
4449    }
4450
4451    /// Overwrite the host-clock last-input timestamp.
4452    pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
4453        self.vim.last_input_host_at = d;
4454    }
4455
4456    // ── Search prompt ──────────────────────────────────────────────────────────
4457
4458    /// Borrow the live search prompt, or `None` when not in search-prompt mode.
4459    pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
4460        self.vim.search_prompt.as_ref()
4461    }
4462
4463    /// Borrow the live search prompt mutably.
4464    pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
4465        self.vim.search_prompt.as_mut()
4466    }
4467
4468    /// Atomically take the search prompt, leaving `None`.
4469    pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
4470        self.vim.search_prompt.take()
4471    }
4472
4473    /// Install a new search prompt (entering search-prompt mode).
4474    pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
4475        self.vim.search_prompt = prompt;
4476    }
4477
4478    // ── Last search pattern / direction ───────────────────────────────────────
4479    // Note: `last_search_forward()` getter already exists at line ~1909.
4480    // `set_last_search()` combined mutator exists at line ~1918.
4481    // Only new / complementary accessors are added here.
4482
4483    /// Return the most recently committed search pattern, or `None`.
4484    pub fn last_search_pattern(&self) -> Option<&str> {
4485        self.vim.last_search.as_deref()
4486    }
4487
4488    /// Overwrite the stored last-search pattern without changing direction
4489    /// (use the existing `set_last_search` for the combined update).
4490    pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
4491        self.vim.last_search = pattern;
4492    }
4493
4494    /// Overwrite only the last-search direction flag.
4495    pub fn set_last_search_forward_only(&mut self, forward: bool) {
4496        self.vim.last_search_forward = forward;
4497    }
4498
4499    // ── Search history ────────────────────────────────────────────────────────
4500
4501    /// Borrow the committed search-pattern history (oldest first).
4502    pub fn search_history(&self) -> &[String] {
4503        &self.vim.search_history
4504    }
4505
4506    /// Borrow the search history mutably (e.g. to push a new entry).
4507    pub fn search_history_mut(&mut self) -> &mut Vec<String> {
4508        &mut self.vim.search_history
4509    }
4510
4511    /// Return the current search-history navigation cursor index.
4512    pub fn search_history_cursor(&self) -> Option<usize> {
4513        self.vim.search_history_cursor
4514    }
4515
4516    /// Overwrite the search-history navigation cursor.
4517    pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
4518        self.vim.search_history_cursor = idx;
4519    }
4520
4521    // ── Jump lists ────────────────────────────────────────────────────────────
4522
4523    /// Borrow the back half of the jump list (entries Ctrl-o pops from).
4524    pub fn jump_back_list(&self) -> &[(usize, usize)] {
4525        &self.vim.jump_back
4526    }
4527
4528    /// Borrow the back jump list mutably (push / pop).
4529    pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4530        &mut self.vim.jump_back
4531    }
4532
4533    /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
4534    pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
4535        &self.vim.jump_fwd
4536    }
4537
4538    /// Borrow the forward jump list mutably (push / pop / clear).
4539    pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4540        &mut self.vim.jump_fwd
4541    }
4542
4543    // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
4544    //
4545    // `push_search_pattern`, `push_jump`, `record_search_history`, and
4546    // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
4547    // search-prompt and normal-mode FSM can call them via the public API.
4548
4549    /// Compile `pattern` into a regex and install it as the active search
4550    /// pattern. Respects `:set ignorecase` / `:set smartcase`. An empty or
4551    /// invalid pattern clears the highlight without raising an error.
4552    pub fn push_search_pattern(&mut self, pattern: &str) {
4553        let compiled = if pattern.is_empty() {
4554            None
4555        } else {
4556            let case_insensitive = self.settings().ignore_case
4557                && !(self.settings().smartcase && pattern.chars().any(|c| c.is_uppercase()));
4558            let translated = crate::search::vim_to_rust_regex(pattern);
4559            let effective: std::borrow::Cow<'_, str> = if case_insensitive {
4560                std::borrow::Cow::Owned(format!("(?i){translated}"))
4561            } else {
4562                std::borrow::Cow::Owned(translated)
4563            };
4564            regex::Regex::new(&effective).ok()
4565        };
4566        let wrap = self.settings().wrapscan;
4567        self.set_search_pattern(compiled);
4568        self.search_state_mut().wrap_around = wrap;
4569    }
4570
4571    /// Record a pre-jump cursor position onto the back jumplist. Called
4572    /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
4573    /// committed `/` or `?`, …). Branching off the history clears the
4574    /// forward half, matching vim's "redo-is-lost" semantics.
4575    pub fn push_jump(&mut self, from: (usize, usize)) {
4576        self.vim.jump_back.push(from);
4577        if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
4578            self.vim.jump_back.remove(0);
4579        }
4580        self.vim.jump_fwd.clear();
4581    }
4582
4583    /// Push `pattern` onto the committed search history. Skips if the
4584    /// most recent entry already matches (consecutive dedupe) and trims
4585    /// the oldest entries beyond the history cap.
4586    pub fn record_search_history(&mut self, pattern: &str) {
4587        if pattern.is_empty() {
4588            return;
4589        }
4590        if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
4591            return;
4592        }
4593        self.vim.search_history.push(pattern.to_string());
4594        let len = self.vim.search_history.len();
4595        if len > vim::SEARCH_HISTORY_MAX {
4596            self.vim
4597                .search_history
4598                .drain(0..len - vim::SEARCH_HISTORY_MAX);
4599        }
4600    }
4601
4602    /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
4603    /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
4604    /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
4605    /// active search prompt.
4606    pub fn walk_search_history(&mut self, dir: isize) {
4607        if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
4608            return;
4609        }
4610        let len = self.vim.search_history.len();
4611        let next_idx = match (self.vim.search_history_cursor, dir) {
4612            (None, -1) => Some(len - 1),
4613            (None, 1) => return,
4614            (Some(i), -1) => i.checked_sub(1),
4615            (Some(i), 1) if i + 1 < len => Some(i + 1),
4616            _ => None,
4617        };
4618        let Some(idx) = next_idx else {
4619            return;
4620        };
4621        self.vim.search_history_cursor = Some(idx);
4622        let text = self.vim.search_history[idx].clone();
4623        if let Some(prompt) = self.vim.search_prompt.as_mut() {
4624            prompt.cursor = text.chars().count();
4625            prompt.text = text.clone();
4626        }
4627        self.push_search_pattern(&text);
4628    }
4629
4630    // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
4631    //
4632    // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
4633    // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
4634
4635    /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
4636    ///
4637    /// Call this at the start of every step; pass the returned
4638    /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
4639    ///
4640    /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
4641    /// Returns `Err(consumed)` when the prelude itself handled the input
4642    /// (macro-stop chord); in that case skip the FSM body and do NOT call
4643    /// `end_step` — the macro-stop path is a true short-circuit with no
4644    /// epilogue needed.
4645    ///
4646    /// This method does NOT handle the search-prompt intercept — callers
4647    /// must check `search_prompt_state().is_some()` before calling `begin_step`
4648    /// and dispatch to the search-prompt FSM body directly.
4649    pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
4650        use crate::input::Key;
4651        use vim::{Mode, Pending};
4652        // ── Timestamps ───────────────────────────────────────────────────────
4653        // Phase 7f: sync buffer before motion handlers see it.
4654        self.sync_buffer_content_from_textarea();
4655        // `:set timeoutlen` chord-timeout handling.
4656        let now = std::time::Instant::now();
4657        let host_now = self.host.now();
4658        let timed_out = match self.vim.last_input_host_at {
4659            Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
4660            None => false,
4661        };
4662        if timed_out {
4663            let chord_in_flight = !matches!(self.vim.pending, Pending::None)
4664                || self.vim.count != 0
4665                || self.vim.pending_register.is_some()
4666                || self.vim.insert_pending_register;
4667            if chord_in_flight {
4668                self.vim.clear_pending_prefix();
4669            }
4670        }
4671        self.vim.last_input_at = Some(now);
4672        self.vim.last_input_host_at = Some(host_now);
4673        // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
4674        if self.vim.recording_macro.is_some()
4675            && !self.vim.replaying_macro
4676            && matches!(self.vim.pending, Pending::None)
4677            && self.vim.mode != Mode::Insert
4678            && input.key == Key::Char('q')
4679            && !input.ctrl
4680            && !input.alt
4681        {
4682            let reg = self.vim.recording_macro.take().unwrap();
4683            let keys = std::mem::take(&mut self.vim.recording_keys);
4684            let text = crate::input::encode_macro(&keys);
4685            self.set_named_register_text(reg.to_ascii_lowercase(), text);
4686            return Err(true);
4687        }
4688        // ── Snapshots for epilogue ────────────────────────────────────────────
4689        let pending_was_macro_chord = matches!(
4690            self.vim.pending,
4691            Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
4692        );
4693        let was_insert = self.vim.mode == Mode::Insert;
4694        let pre_visual_snapshot = match self.vim.mode {
4695            Mode::Visual => Some(vim::LastVisual {
4696                mode: Mode::Visual,
4697                anchor: self.vim.visual_anchor,
4698                cursor: self.cursor(),
4699                block_vcol: 0,
4700            }),
4701            Mode::VisualLine => Some(vim::LastVisual {
4702                mode: Mode::VisualLine,
4703                anchor: (self.vim.visual_line_anchor, 0),
4704                cursor: self.cursor(),
4705                block_vcol: 0,
4706            }),
4707            Mode::VisualBlock => Some(vim::LastVisual {
4708                mode: Mode::VisualBlock,
4709                anchor: self.vim.block_anchor,
4710                cursor: self.cursor(),
4711                block_vcol: self.vim.block_vcol,
4712            }),
4713            _ => None,
4714        };
4715        Ok(StepBookkeeping {
4716            pending_was_macro_chord,
4717            was_insert,
4718            pre_visual_snapshot,
4719        })
4720    }
4721
4722    /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
4723    ///
4724    /// `input` is the same input that was passed to `begin_step`.
4725    /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
4726    /// `consumed` is the return value of the FSM body; this method returns
4727    /// it after running all epilogue invariants.
4728    ///
4729    /// Must NOT be called when `begin_step` returned `Err(...)`.
4730    pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
4731        use crate::input::Key;
4732        use vim::{Mode, Pending};
4733        let StepBookkeeping {
4734            pending_was_macro_chord,
4735            was_insert,
4736            pre_visual_snapshot,
4737        } = bk;
4738        // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
4739        if let Some(snap) = pre_visual_snapshot
4740            && !matches!(
4741                self.vim.mode,
4742                Mode::Visual | Mode::VisualLine | Mode::VisualBlock
4743            )
4744        {
4745            let (lo, hi) = match snap.mode {
4746                Mode::Visual => {
4747                    if snap.anchor <= snap.cursor {
4748                        (snap.anchor, snap.cursor)
4749                    } else {
4750                        (snap.cursor, snap.anchor)
4751                    }
4752                }
4753                Mode::VisualLine => {
4754                    let r_lo = snap.anchor.0.min(snap.cursor.0);
4755                    let r_hi = snap.anchor.0.max(snap.cursor.0);
4756                    let last_col = self
4757                        .buffer()
4758                        .lines()
4759                        .get(r_hi)
4760                        .map(|l| l.chars().count().saturating_sub(1))
4761                        .unwrap_or(0);
4762                    ((r_lo, 0), (r_hi, last_col))
4763                }
4764                Mode::VisualBlock => {
4765                    let (r1, c1) = snap.anchor;
4766                    let (r2, c2) = snap.cursor;
4767                    ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
4768                }
4769                _ => {
4770                    if snap.anchor <= snap.cursor {
4771                        (snap.anchor, snap.cursor)
4772                    } else {
4773                        (snap.cursor, snap.anchor)
4774                    }
4775                }
4776            };
4777            self.set_mark('<', lo);
4778            self.set_mark('>', hi);
4779            self.vim.last_visual = Some(snap);
4780        }
4781        // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
4782        if !was_insert
4783            && self.vim.one_shot_normal
4784            && self.vim.mode == Mode::Normal
4785            && matches!(self.vim.pending, Pending::None)
4786        {
4787            self.vim.one_shot_normal = false;
4788            self.vim.mode = Mode::Insert;
4789        }
4790        // ── Content + viewport sync ───────────────────────────────────────────
4791        self.sync_buffer_content_from_textarea();
4792        if !self.vim.viewport_pinned {
4793            self.ensure_cursor_in_scrolloff();
4794        }
4795        self.vim.viewport_pinned = false;
4796        // ── Recorder hook ─────────────────────────────────────────────────────
4797        if self.vim.recording_macro.is_some()
4798            && !self.vim.replaying_macro
4799            && input.key != Key::Char('q')
4800            && !pending_was_macro_chord
4801        {
4802            self.vim.recording_keys.push(input);
4803        }
4804        // ── Phase 6.3: current_mode sync ─────────────────────────────────────
4805        self.vim.current_mode = self.vim.public_mode();
4806        consumed
4807    }
4808
4809    // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
4810
4811    /// `true` when the editor is in any visual mode (Visual / VisualLine /
4812    /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
4813    pub fn is_visual(&self) -> bool {
4814        matches!(
4815            self.vim.mode,
4816            vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
4817        )
4818    }
4819
4820    /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
4821    /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
4822    /// virtual column, which survives j/k clamping to shorter rows).
4823    ///
4824    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
4825    /// extents needed for VisualBlock `I` / `A` / `r` without accessing
4826    /// engine-private helpers.
4827    pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
4828        let (ar, ac) = self.vim.block_anchor;
4829        let (cr, _) = self.cursor();
4830        let cc = self.vim.block_vcol;
4831        let top = ar.min(cr);
4832        let bot = ar.max(cr);
4833        let left = ac.min(cc);
4834        let right = ac.max(cc);
4835        (top, bot, left, right)
4836    }
4837
4838    /// Return the character count (code-point count) of line `row`, or `0`
4839    /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
4840    /// I / A column computations.
4841    pub fn line_char_count(&self, row: usize) -> usize {
4842        buf_line_chars(&self.buffer, row)
4843    }
4844
4845    /// Apply operator over `motion` with `count` repetitions. The full
4846    /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
4847    ///
4848    /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
4849    /// relocated `handle_after_op` can call it directly with a parsed `Motion`
4850    /// without re-entering the engine FSM.
4851    pub fn apply_op_with_motion_direct(
4852        &mut self,
4853        op: crate::vim::Operator,
4854        motion: &crate::vim::Motion,
4855        count: usize,
4856    ) {
4857        vim::apply_op_with_motion(self, op, motion, count);
4858    }
4859
4860    /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
4861    /// `delta = 1` increments; `delta = -1` decrements; larger deltas
4862    /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
4863    /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
4864    pub fn adjust_number(&mut self, delta: i64) {
4865        vim::adjust_number(self, delta);
4866    }
4867
4868    /// Open the `/` or `?` search prompt. `forward = true` for `/`,
4869    /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
4870    /// dispatch `/` and `?` without re-entering the engine FSM.
4871    pub fn enter_search(&mut self, forward: bool) {
4872        vim::enter_search(self, forward);
4873    }
4874
4875    /// Enter Insert mode at the left edge of a VisualBlock selection for
4876    /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
4877    /// then begins an insert session with `InsertReason::BlockEdge`.
4878    ///
4879    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
4880    /// VisualBlock `I` command without accessing engine-private helpers.
4881    pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
4882        self.jump_cursor(top, col);
4883        self.vim.mode = vim::Mode::Normal;
4884        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
4885    }
4886
4887    /// Enter Insert mode at the right edge of a VisualBlock selection for
4888    /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
4889    /// then begins an insert session with `InsertReason::BlockEdge`.
4890    ///
4891    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
4892    /// VisualBlock `A` command without accessing engine-private helpers.
4893    pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
4894        self.jump_cursor(top, col);
4895        self.vim.mode = vim::Mode::Normal;
4896        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
4897    }
4898
4899    /// Execute a motion (cursor movement), push to the jumplist for big jumps,
4900    /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
4901    /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
4902    pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
4903        vim::execute_motion(self, motion, count);
4904    }
4905
4906    /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
4907    /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
4908    /// non-h/l motions leave it alone so the intended column survives clamping
4909    /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
4910    pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
4911        vim::update_block_vcol(self, motion);
4912    }
4913
4914    /// Apply `op` over the current visual selection (char-wise, linewise, or
4915    /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
4916    /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
4917    pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
4918        vim::apply_visual_operator(self, op);
4919    }
4920
4921    /// Replace each character cell in the current VisualBlock selection with
4922    /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
4923    /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
4924    pub fn replace_block_char(&mut self, ch: char) {
4925        vim::block_replace(self, ch);
4926    }
4927
4928    /// Extend the current visual selection to cover the text object identified
4929    /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
4930    /// via `text_object_range`, then updates the visual anchor and cursor.
4931    ///
4932    /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
4933    /// in `hjkl-vim::normal::handle_visual_text_obj`.
4934    pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
4935        use crate::vim::{Mode, TextObject};
4936        let obj = match ch {
4937            'w' => TextObject::Word { big: false },
4938            'W' => TextObject::Word { big: true },
4939            '"' | '\'' | '`' => TextObject::Quote(ch),
4940            '(' | ')' | 'b' => TextObject::Bracket('('),
4941            '[' | ']' => TextObject::Bracket('['),
4942            '{' | '}' | 'B' => TextObject::Bracket('{'),
4943            '<' | '>' => TextObject::Bracket('<'),
4944            'p' => TextObject::Paragraph,
4945            't' => TextObject::XmlTag,
4946            's' => TextObject::Sentence,
4947            _ => return,
4948        };
4949        let Some((start, end, kind)) = vim::text_object_range(self, obj, inner) else {
4950            return;
4951        };
4952        match kind {
4953            crate::vim::RangeKind::Linewise => {
4954                self.vim.visual_line_anchor = start.0;
4955                self.vim.mode = Mode::VisualLine;
4956                self.vim.current_mode = VimMode::VisualLine;
4957                self.jump_cursor(end.0, 0);
4958            }
4959            _ => {
4960                self.vim.mode = Mode::Visual;
4961                self.vim.current_mode = VimMode::Visual;
4962                self.vim.visual_anchor = (start.0, start.1);
4963                let (er, ec) = vim::retreat_one(self, end);
4964                self.jump_cursor(er, ec);
4965            }
4966        }
4967    }
4968}
4969
4970/// Visual column of the character at `char_col` in `line`, treating `\t`
4971/// as expansion to the next `tab_width` stop and every other char as
4972/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
4973/// the cursor math elsewhere also assumes single-cell chars.
4974fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
4975    let mut visual = 0usize;
4976    for (i, ch) in line.chars().enumerate() {
4977        if i >= char_col {
4978            break;
4979        }
4980        if ch == '\t' {
4981            visual += tab_width - (visual % tab_width);
4982        } else {
4983            visual += 1;
4984        }
4985    }
4986    visual
4987}