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;
10#[cfg(feature = "crossterm")]
11use crate::input::Key;
12use crate::vim::{self, VimState};
13use crate::{KeybindingMode, VimMode};
14#[cfg(feature = "crossterm")]
15use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
16#[cfg(feature = "ratatui")]
17use ratatui::layout::Rect;
18use std::sync::atomic::{AtomicU16, Ordering};
19
20/// Convert a SPEC [`crate::types::Style`] to a [`ratatui::style::Style`].
21///
22/// Lossless within the styles each library represents. Lives behind the
23/// `ratatui` feature so wasm / no_std consumers that opt out don't pay
24/// for the dep. Use the engine-native [`crate::types::Style`] +
25/// [`Editor::intern_engine_style`] surface from feature-disabled hosts.
26#[cfg(feature = "ratatui")]
27pub(crate) fn engine_style_to_ratatui(s: crate::types::Style) -> ratatui::style::Style {
28    use crate::types::Attrs;
29    use ratatui::style::{Color as RColor, Modifier as RMod, Style as RStyle};
30    let mut out = RStyle::default();
31    if let Some(c) = s.fg {
32        out = out.fg(RColor::Rgb(c.0, c.1, c.2));
33    }
34    if let Some(c) = s.bg {
35        out = out.bg(RColor::Rgb(c.0, c.1, c.2));
36    }
37    let mut m = RMod::empty();
38    if s.attrs.contains(Attrs::BOLD) {
39        m |= RMod::BOLD;
40    }
41    if s.attrs.contains(Attrs::ITALIC) {
42        m |= RMod::ITALIC;
43    }
44    if s.attrs.contains(Attrs::UNDERLINE) {
45        m |= RMod::UNDERLINED;
46    }
47    if s.attrs.contains(Attrs::REVERSE) {
48        m |= RMod::REVERSED;
49    }
50    if s.attrs.contains(Attrs::DIM) {
51        m |= RMod::DIM;
52    }
53    if s.attrs.contains(Attrs::STRIKE) {
54        m |= RMod::CROSSED_OUT;
55    }
56    out.add_modifier(m)
57}
58
59/// Inverse of [`engine_style_to_ratatui`]. Lossy for ratatui colors
60/// the engine doesn't model (Indexed, named ANSI) — flattens to
61/// nearest RGB. Behind the `ratatui` feature.
62#[cfg(feature = "ratatui")]
63pub(crate) fn ratatui_style_to_engine(s: ratatui::style::Style) -> crate::types::Style {
64    use crate::types::{Attrs, Color, Style};
65    use ratatui::style::{Color as RColor, Modifier as RMod};
66    fn c(rc: RColor) -> Color {
67        match rc {
68            RColor::Rgb(r, g, b) => Color(r, g, b),
69            RColor::Black => Color(0, 0, 0),
70            RColor::Red => Color(205, 49, 49),
71            RColor::Green => Color(13, 188, 121),
72            RColor::Yellow => Color(229, 229, 16),
73            RColor::Blue => Color(36, 114, 200),
74            RColor::Magenta => Color(188, 63, 188),
75            RColor::Cyan => Color(17, 168, 205),
76            RColor::Gray => Color(229, 229, 229),
77            RColor::DarkGray => Color(102, 102, 102),
78            RColor::LightRed => Color(241, 76, 76),
79            RColor::LightGreen => Color(35, 209, 139),
80            RColor::LightYellow => Color(245, 245, 67),
81            RColor::LightBlue => Color(59, 142, 234),
82            RColor::LightMagenta => Color(214, 112, 214),
83            RColor::LightCyan => Color(41, 184, 219),
84            RColor::White => Color(255, 255, 255),
85            _ => Color(0, 0, 0),
86        }
87    }
88    let mut attrs = Attrs::empty();
89    if s.add_modifier.contains(RMod::BOLD) {
90        attrs |= Attrs::BOLD;
91    }
92    if s.add_modifier.contains(RMod::ITALIC) {
93        attrs |= Attrs::ITALIC;
94    }
95    if s.add_modifier.contains(RMod::UNDERLINED) {
96        attrs |= Attrs::UNDERLINE;
97    }
98    if s.add_modifier.contains(RMod::REVERSED) {
99        attrs |= Attrs::REVERSE;
100    }
101    if s.add_modifier.contains(RMod::DIM) {
102        attrs |= Attrs::DIM;
103    }
104    if s.add_modifier.contains(RMod::CROSSED_OUT) {
105        attrs |= Attrs::STRIKE;
106    }
107    Style {
108        fg: s.fg.map(c),
109        bg: s.bg.map(c),
110        attrs,
111    }
112}
113
114/// Map a [`hjkl_buffer::Edit`] to one or more SPEC
115/// [`crate::types::Edit`] (`EditOp`) records.
116///
117/// Most buffer edits map to a single EditOp. Block ops
118/// ([`hjkl_buffer::Edit::InsertBlock`] /
119/// [`hjkl_buffer::Edit::DeleteBlockChunks`]) emit one EditOp per row
120/// touched — they edit non-contiguous cells and a single
121/// `range..range` can't represent the rectangle.
122///
123/// Returns an empty vec when the edit isn't representable (no buffer
124/// variant currently fails this check).
125fn edit_to_editops(edit: &hjkl_buffer::Edit) -> Vec<crate::types::Edit> {
126    use crate::types::{Edit as Op, Pos};
127    use hjkl_buffer::Edit as B;
128    let to_pos = |p: hjkl_buffer::Position| Pos {
129        line: p.row as u32,
130        col: p.col as u32,
131    };
132    match edit {
133        B::InsertChar { at, ch } => vec![Op {
134            range: to_pos(*at)..to_pos(*at),
135            replacement: ch.to_string(),
136        }],
137        B::InsertStr { at, text } => vec![Op {
138            range: to_pos(*at)..to_pos(*at),
139            replacement: text.clone(),
140        }],
141        B::DeleteRange { start, end, .. } => vec![Op {
142            range: to_pos(*start)..to_pos(*end),
143            replacement: String::new(),
144        }],
145        B::Replace { start, end, with } => vec![Op {
146            range: to_pos(*start)..to_pos(*end),
147            replacement: with.clone(),
148        }],
149        B::JoinLines {
150            row,
151            count,
152            with_space,
153        } => {
154            // Joining `count` rows after `row` collapses
155            // [(row+1, 0) .. (row+count, EOL)] into the joined
156            // sentinel. The replacement is either an empty string
157            // (gJ) or " " between segments (J).
158            let start = Pos {
159                line: *row as u32 + 1,
160                col: 0,
161            };
162            let end = Pos {
163                line: (*row + *count) as u32,
164                col: u32::MAX, // covers to EOL of the last source row
165            };
166            vec![Op {
167                range: start..end,
168                replacement: if *with_space {
169                    " ".into()
170                } else {
171                    String::new()
172                },
173            }]
174        }
175        B::SplitLines {
176            row,
177            cols,
178            inserted_space: _,
179        } => {
180            // SplitLines reverses a JoinLines: insert a `\n`
181            // (and optional dropped space) at each col on `row`.
182            cols.iter()
183                .map(|c| {
184                    let p = Pos {
185                        line: *row as u32,
186                        col: *c as u32,
187                    };
188                    Op {
189                        range: p..p,
190                        replacement: "\n".into(),
191                    }
192                })
193                .collect()
194        }
195        B::InsertBlock { at, chunks } => {
196            // One EditOp per row in the block — non-contiguous edits.
197            chunks
198                .iter()
199                .enumerate()
200                .map(|(i, chunk)| {
201                    let p = Pos {
202                        line: at.row as u32 + i as u32,
203                        col: at.col as u32,
204                    };
205                    Op {
206                        range: p..p,
207                        replacement: chunk.clone(),
208                    }
209                })
210                .collect()
211        }
212        B::DeleteBlockChunks { at, widths } => {
213            // One EditOp per row, deleting `widths[i]` chars at
214            // `(at.row + i, at.col)`.
215            widths
216                .iter()
217                .enumerate()
218                .map(|(i, w)| {
219                    let start = Pos {
220                        line: at.row as u32 + i as u32,
221                        col: at.col as u32,
222                    };
223                    let end = Pos {
224                        line: at.row as u32 + i as u32,
225                        col: at.col as u32 + *w as u32,
226                    };
227                    Op {
228                        range: start..end,
229                        replacement: String::new(),
230                    }
231                })
232                .collect()
233        }
234    }
235}
236
237/// Sum of bytes from the start of the buffer to the start of `row`.
238/// Walks lines + their separating `\n` bytes — matches the canonical
239/// `lines().join("\n")` byte rendering used by syntax tooling.
240#[inline]
241fn buffer_byte_of_row(buf: &hjkl_buffer::Buffer, row: usize) -> usize {
242    let n = buf.row_count();
243    let row = row.min(n);
244    let mut acc = 0usize;
245    for r in 0..row {
246        acc += buf.line(r).map(str::len).unwrap_or(0);
247        if r + 1 < n {
248            acc += 1; // separator '\n'
249        }
250    }
251    acc
252}
253
254/// Convert an `hjkl_buffer::Position` (char-indexed col) into byte
255/// coordinates `(byte_within_buffer, (row, col_byte))` against the
256/// **pre-edit** buffer.
257fn position_to_byte_coords(
258    buf: &hjkl_buffer::Buffer,
259    pos: hjkl_buffer::Position,
260) -> (usize, (u32, u32)) {
261    let row = pos.row.min(buf.row_count().saturating_sub(1));
262    let line = buf.line(row).unwrap_or("");
263    let col_byte = pos.byte_offset(line);
264    let byte = buffer_byte_of_row(buf, row) + col_byte;
265    (byte, (row as u32, col_byte as u32))
266}
267
268/// Compute the byte position after inserting `text` starting at
269/// `start_byte` / `start_pos`. Returns `(end_byte, end_position)`.
270fn advance_by_text(text: &str, start_byte: usize, start_pos: (u32, u32)) -> (usize, (u32, u32)) {
271    let new_end_byte = start_byte + text.len();
272    let newlines = text.bytes().filter(|&b| b == b'\n').count();
273    let end_pos = if newlines == 0 {
274        (start_pos.0, start_pos.1 + text.len() as u32)
275    } else {
276        // Bytes after the last newline determine the trailing column.
277        let last_nl = text.rfind('\n').unwrap();
278        let tail_bytes = (text.len() - last_nl - 1) as u32;
279        (start_pos.0 + newlines as u32, tail_bytes)
280    };
281    (new_end_byte, end_pos)
282}
283
284/// Translate a single `hjkl_buffer::Edit` into one or more
285/// [`crate::types::ContentEdit`] records using the **pre-edit** buffer
286/// state for byte/position lookups. Block ops fan out to one entry per
287/// touched row (matches `edit_to_editops`).
288fn content_edits_from_buffer_edit(
289    buf: &hjkl_buffer::Buffer,
290    edit: &hjkl_buffer::Edit,
291) -> Vec<crate::types::ContentEdit> {
292    use hjkl_buffer::Edit as B;
293    use hjkl_buffer::Position;
294
295    let mut out: Vec<crate::types::ContentEdit> = Vec::new();
296
297    match edit {
298        B::InsertChar { at, ch } => {
299            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
300            let new_end_byte = start_byte + ch.len_utf8();
301            let new_end_pos = (start_pos.0, start_pos.1 + ch.len_utf8() as u32);
302            out.push(crate::types::ContentEdit {
303                start_byte,
304                old_end_byte: start_byte,
305                new_end_byte,
306                start_position: start_pos,
307                old_end_position: start_pos,
308                new_end_position: new_end_pos,
309            });
310        }
311        B::InsertStr { at, text } => {
312            let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
313            let (new_end_byte, new_end_pos) = advance_by_text(text, start_byte, start_pos);
314            out.push(crate::types::ContentEdit {
315                start_byte,
316                old_end_byte: start_byte,
317                new_end_byte,
318                start_position: start_pos,
319                old_end_position: start_pos,
320                new_end_position: new_end_pos,
321            });
322        }
323        B::DeleteRange { start, end, kind } => {
324            let (start, end) = if start <= end {
325                (*start, *end)
326            } else {
327                (*end, *start)
328            };
329            match kind {
330                hjkl_buffer::MotionKind::Char => {
331                    let (start_byte, start_pos) = position_to_byte_coords(buf, start);
332                    let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
333                    out.push(crate::types::ContentEdit {
334                        start_byte,
335                        old_end_byte,
336                        new_end_byte: start_byte,
337                        start_position: start_pos,
338                        old_end_position: old_end_pos,
339                        new_end_position: start_pos,
340                    });
341                }
342                hjkl_buffer::MotionKind::Line => {
343                    // Linewise delete drops rows [start.row..=end.row]. Map
344                    // to a span from start of `start.row` through start of
345                    // (end.row + 1). The buffer's own `do_delete_range`
346                    // collapses to row `start.row` after dropping.
347                    let lo = start.row;
348                    let hi = end.row.min(buf.row_count().saturating_sub(1));
349                    let start_byte = buffer_byte_of_row(buf, lo);
350                    let next_row_byte = if hi + 1 < buf.row_count() {
351                        buffer_byte_of_row(buf, hi + 1)
352                    } else {
353                        // No row after; clamp to end-of-buffer byte.
354                        buffer_byte_of_row(buf, buf.row_count())
355                            + buf
356                                .line(buf.row_count().saturating_sub(1))
357                                .map(str::len)
358                                .unwrap_or(0)
359                    };
360                    out.push(crate::types::ContentEdit {
361                        start_byte,
362                        old_end_byte: next_row_byte,
363                        new_end_byte: start_byte,
364                        start_position: (lo as u32, 0),
365                        old_end_position: ((hi + 1) as u32, 0),
366                        new_end_position: (lo as u32, 0),
367                    });
368                }
369                hjkl_buffer::MotionKind::Block => {
370                    // Block delete removes a rectangle of chars per row.
371                    // Fan out to one ContentEdit per row.
372                    let (left_col, right_col) = (start.col.min(end.col), start.col.max(end.col));
373                    for row in start.row..=end.row {
374                        let row_start_pos = Position::new(row, left_col);
375                        let row_end_pos = Position::new(row, right_col + 1);
376                        let (sb, sp) = position_to_byte_coords(buf, row_start_pos);
377                        let (eb, ep) = position_to_byte_coords(buf, row_end_pos);
378                        if eb <= sb {
379                            continue;
380                        }
381                        out.push(crate::types::ContentEdit {
382                            start_byte: sb,
383                            old_end_byte: eb,
384                            new_end_byte: sb,
385                            start_position: sp,
386                            old_end_position: ep,
387                            new_end_position: sp,
388                        });
389                    }
390                }
391            }
392        }
393        B::Replace { start, end, with } => {
394            let (start, end) = if start <= end {
395                (*start, *end)
396            } else {
397                (*end, *start)
398            };
399            let (start_byte, start_pos) = position_to_byte_coords(buf, start);
400            let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
401            let (new_end_byte, new_end_pos) = advance_by_text(with, start_byte, start_pos);
402            out.push(crate::types::ContentEdit {
403                start_byte,
404                old_end_byte,
405                new_end_byte,
406                start_position: start_pos,
407                old_end_position: old_end_pos,
408                new_end_position: new_end_pos,
409            });
410        }
411        B::JoinLines {
412            row,
413            count,
414            with_space,
415        } => {
416            // Joining `count` rows after `row` collapses the bytes
417            // between EOL of `row` and EOL of `row + count` into either
418            // an empty string (gJ) or a single space per join (J — but
419            // only when both sides are non-empty; we approximate with
420            // a single space for simplicity).
421            let row = (*row).min(buf.row_count().saturating_sub(1));
422            let last_join_row = (row + count).min(buf.row_count().saturating_sub(1));
423            let line = buf.line(row).unwrap_or("");
424            let row_eol_byte = buffer_byte_of_row(buf, row) + line.len();
425            let row_eol_col = line.len() as u32;
426            let next_row_after = last_join_row + 1;
427            let old_end_byte = if next_row_after < buf.row_count() {
428                buffer_byte_of_row(buf, next_row_after).saturating_sub(1)
429            } else {
430                buffer_byte_of_row(buf, buf.row_count())
431                    + buf
432                        .line(buf.row_count().saturating_sub(1))
433                        .map(str::len)
434                        .unwrap_or(0)
435            };
436            let last_line = buf.line(last_join_row).unwrap_or("");
437            let old_end_pos = (last_join_row as u32, last_line.len() as u32);
438            let replacement_len = if *with_space { 1 } else { 0 };
439            let new_end_byte = row_eol_byte + replacement_len;
440            let new_end_pos = (row as u32, row_eol_col + replacement_len as u32);
441            out.push(crate::types::ContentEdit {
442                start_byte: row_eol_byte,
443                old_end_byte,
444                new_end_byte,
445                start_position: (row as u32, row_eol_col),
446                old_end_position: old_end_pos,
447                new_end_position: new_end_pos,
448            });
449        }
450        B::SplitLines {
451            row,
452            cols,
453            inserted_space,
454        } => {
455            // Splits insert "\n" (or "\n " inverse) at each col on `row`.
456            // The buffer applies all splits left-to-right via the
457            // do_split_lines path; we emit one ContentEdit per col,
458            // each treated as an insert at that col on `row`. Note: the
459            // buffer state during emission is *pre-edit*, so all cols
460            // index into the same pre-edit row.
461            let row = (*row).min(buf.row_count().saturating_sub(1));
462            let line = buf.line(row).unwrap_or("");
463            let row_byte = buffer_byte_of_row(buf, row);
464            let insert = if *inserted_space { "\n " } else { "\n" };
465            for &c in cols {
466                let pos = Position::new(row, c);
467                let col_byte = pos.byte_offset(line);
468                let start_byte = row_byte + col_byte;
469                let start_pos = (row as u32, col_byte as u32);
470                let (new_end_byte, new_end_pos) = advance_by_text(insert, start_byte, start_pos);
471                out.push(crate::types::ContentEdit {
472                    start_byte,
473                    old_end_byte: start_byte,
474                    new_end_byte,
475                    start_position: start_pos,
476                    old_end_position: start_pos,
477                    new_end_position: new_end_pos,
478                });
479            }
480        }
481        B::InsertBlock { at, chunks } => {
482            // One ContentEdit per chunk; each lands at `(at.row + i,
483            // at.col)` in the pre-edit buffer.
484            for (i, chunk) in chunks.iter().enumerate() {
485                let pos = Position::new(at.row + i, at.col);
486                let (start_byte, start_pos) = position_to_byte_coords(buf, pos);
487                let (new_end_byte, new_end_pos) = advance_by_text(chunk, start_byte, start_pos);
488                out.push(crate::types::ContentEdit {
489                    start_byte,
490                    old_end_byte: start_byte,
491                    new_end_byte,
492                    start_position: start_pos,
493                    old_end_position: start_pos,
494                    new_end_position: new_end_pos,
495                });
496            }
497        }
498        B::DeleteBlockChunks { at, widths } => {
499            for (i, w) in widths.iter().enumerate() {
500                let row = at.row + i;
501                let start_pos = Position::new(row, at.col);
502                let end_pos = Position::new(row, at.col + *w);
503                let (sb, sp) = position_to_byte_coords(buf, start_pos);
504                let (eb, ep) = position_to_byte_coords(buf, end_pos);
505                if eb <= sb {
506                    continue;
507                }
508                out.push(crate::types::ContentEdit {
509                    start_byte: sb,
510                    old_end_byte: eb,
511                    new_end_byte: sb,
512                    start_position: sp,
513                    old_end_position: ep,
514                    new_end_position: sp,
515                });
516            }
517        }
518    }
519
520    out
521}
522
523/// Where the cursor should land in the viewport after a `z`-family
524/// scroll (`zz` / `zt` / `zb`).
525#[derive(Debug, Clone, Copy, PartialEq, Eq)]
526pub(super) enum CursorScrollTarget {
527    Center,
528    Top,
529    Bottom,
530}
531
532// ── Trait-surface cast helpers ────────────────────────────────────
533//
534// 0.0.42 (Patch C-δ.7): the helpers introduced in 0.0.41 were
535// promoted to [`crate::buf_helpers`] so `vim.rs` free fns can route
536// their reaches through the same primitives. Re-import via
537// `use` so the editor body keeps its terse call shape.
538
539use crate::buf_helpers::{
540    apply_buffer_edit, buf_cursor_pos, buf_cursor_rc, buf_cursor_row, buf_line, buf_line_chars,
541    buf_lines_to_vec, buf_row_count, buf_set_cursor_rc,
542};
543
544pub struct Editor<
545    B: crate::types::Buffer = hjkl_buffer::Buffer,
546    H: crate::types::Host = crate::types::DefaultHost,
547> {
548    pub keybinding_mode: KeybindingMode,
549    /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
550    pub last_yank: Option<String>,
551    /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
552    /// Internal — exposed via Editor accessor methods
553    /// ([`Editor::buffer_mark`], [`Editor::last_jump_back`],
554    /// [`Editor::last_edit_pos`], [`Editor::take_lsp_intent`], …).
555    pub(crate) vim: VimState,
556    /// Undo history: each entry is (lines, cursor) before the edit.
557    /// Internal — managed by [`Editor::push_undo`] / [`Editor::restore`]
558    /// / [`Editor::pop_last_undo`].
559    pub(crate) undo_stack: Vec<(Vec<String>, (usize, usize))>,
560    /// Redo history: entries pushed when undoing.
561    pub(super) redo_stack: Vec<(Vec<String>, (usize, usize))>,
562    /// Set whenever the buffer content changes; cleared by `take_dirty`.
563    pub(super) content_dirty: bool,
564    /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
565    /// so repeated `content_arc()` calls within the same un-mutated
566    /// window are free (ref-count bump instead of a full-buffer join).
567    /// Invalidated by every [`mark_content_dirty`] call.
568    pub(super) cached_content: Option<std::sync::Arc<String>>,
569    /// Last rendered viewport height (text rows only, no chrome). Written
570    /// by the draw path via [`set_viewport_height`] so the scroll helpers
571    /// can clamp the cursor to stay visible without plumbing the height
572    /// through every call.
573    pub(super) viewport_height: AtomicU16,
574    /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
575    /// goto-definition). The host app drains this each step and fires
576    /// the matching request against its own LSP client.
577    pub(super) pending_lsp: Option<LspIntent>,
578    /// Pending [`crate::types::FoldOp`]s raised by `z…` keystrokes,
579    /// the `:fold*` Ex commands, or the edit pipeline's
580    /// "edits-inside-a-fold open it" invalidation. Drained by hosts
581    /// via [`Editor::take_fold_ops`]; the engine also applies each op
582    /// locally through [`crate::buffer_impl::BufferFoldProviderMut`]
583    /// so the in-tree buffer fold storage stays in sync without host
584    /// cooperation. Introduced in 0.0.38 (Patch C-δ.4).
585    pub(super) pending_fold_ops: Vec<crate::types::FoldOp>,
586    /// Buffer storage.
587    ///
588    /// 0.1.0 (Patch C-δ): generic over `B: Buffer` per SPEC §"Editor
589    /// surface". Default `B = hjkl_buffer::Buffer`. The vim FSM body
590    /// and `Editor::mutate_edit` are concrete on `hjkl_buffer::Buffer`
591    /// for 0.1.0 — see `crate::buf_helpers::apply_buffer_edit`.
592    pub(super) buffer: B,
593    /// Style intern table for the migration buffer's opaque
594    /// `Span::style` ids. Phase 7d-ii-a wiring — `apply_window_spans`
595    /// produces `(start, end, Style)` tuples for the textarea; we
596    /// translate those to `hjkl_buffer::Span` by interning the
597    /// `Style` here and storing the table index. The render path's
598    /// `StyleResolver` looks the style back up by id.
599    ///
600    /// Behind the `ratatui` feature; non-ratatui hosts use the
601    /// engine-native [`crate::types::Style`] surface via
602    /// [`Editor::intern_engine_style`] (which lives on a parallel
603    /// engine-side table when ratatui is off).
604    #[cfg(feature = "ratatui")]
605    pub(super) style_table: Vec<ratatui::style::Style>,
606    /// Engine-native style intern table. Used directly by
607    /// [`Editor::intern_engine_style`] when the `ratatui` feature is
608    /// off; when it's on, the table is derived from `style_table` via
609    /// [`ratatui_style_to_engine`] / [`engine_style_to_ratatui`].
610    #[cfg(not(feature = "ratatui"))]
611    pub(super) engine_style_table: Vec<crate::types::Style>,
612    /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
613    /// every `p` / `P` via the active selector (default unnamed).
614    /// Internal — read via [`Editor::registers`]; mutated by yank /
615    /// delete / paste FSM paths and by [`Editor::seed_yank`].
616    pub(crate) registers: crate::registers::Registers,
617    /// Per-row syntax styling, kept here so the host can do
618    /// incremental window updates (see `apply_window_spans` in
619    /// the host). Same `(start_byte, end_byte, Style)` tuple shape
620    /// the textarea used to host. The Buffer-side opaque-id spans are
621    /// derived from this on every install. Behind the `ratatui`
622    /// feature.
623    #[cfg(feature = "ratatui")]
624    pub styled_spans: Vec<Vec<(usize, usize, ratatui::style::Style)>>,
625    /// Per-editor settings tweakable via `:set`. Exposed by reference
626    /// so handlers (indent, search) read the live value rather than a
627    /// snapshot taken at startup. Read via [`Editor::settings`];
628    /// mutate via [`Editor::settings_mut`].
629    pub(crate) settings: Settings,
630    /// Unified named-marks map. Lowercase letters (`'a`–`'z`) are
631    /// per-Editor / "buffer-scope-equivalent" — set by `m{a-z}`, read
632    /// by `'{a-z}` / `` `{a-z} ``. Uppercase letters (`'A`–`'Z`) are
633    /// "file marks" that survive [`Editor::set_content`] calls so
634    /// they persist across tab swaps within the same Editor.
635    ///
636    /// 0.0.36: consolidated from three former storages:
637    /// - `hjkl_buffer::Buffer::marks` (deleted; was unused dead code).
638    /// - `vim::VimState::marks` (lowercase) (deleted).
639    /// - `Editor::file_marks` (uppercase) (replaced by this map).
640    ///
641    /// `BTreeMap` so iteration is deterministic for snapshot tests
642    /// and the `:marks` ex command. Mark-shift on edits is handled
643    /// by [`Editor::shift_marks_after_edit`].
644    pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
645    /// Block ranges (`(start_row, end_row)` inclusive) the host has
646    /// extracted from a syntax tree. `:foldsyntax` reads these to
647    /// populate folds. The host refreshes them on every re-parse via
648    /// [`Editor::set_syntax_fold_ranges`]; ex commands read them via
649    /// [`Editor::syntax_fold_ranges`].
650    pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
651    /// Pending edit log drained by [`Editor::take_changes`]. Each entry
652    /// is a SPEC [`crate::types::Edit`] mapped from the underlying
653    /// `hjkl_buffer::Edit` operation. Compound ops (JoinLines,
654    /// SplitLines, InsertBlock, DeleteBlockChunks) emit a single
655    /// best-effort EditOp covering the touched range; hosts wanting
656    /// per-cell deltas should diff their own snapshot of `lines()`.
657    /// Sealed at 0.1.0 trait extraction.
658    /// Drained by [`Editor::take_changes`].
659    pub(crate) change_log: Vec<crate::types::Edit>,
660    /// Vim's "sticky column" (curswant). `None` before the first
661    /// motion — the next vertical motion bootstraps from the live
662    /// cursor column. Horizontal motions refresh this to the new
663    /// column; vertical motions read it back so bouncing through a
664    /// shorter row doesn't drag the cursor to col 0. Hoisted out of
665    /// `hjkl_buffer::Buffer` (and `VimState`) in 0.0.28 — Editor is
666    /// the single owner now. Buffer motion methods that need it
667    /// take a `&mut Option<usize>` parameter.
668    pub(crate) sticky_col: Option<usize>,
669    /// Host adapter for clipboard, cursor-shape, time, viewport, and
670    /// search-prompt / cancellation side-channels.
671    ///
672    /// 0.1.0 (Patch C-δ): generic over `H: Host` per SPEC §"Editor
673    /// surface". Default `H = DefaultHost`. The pre-0.1.0 `EngineHost`
674    /// dyn-shim is gone — every method now dispatches through `H`'s
675    /// `Host` trait surface directly.
676    pub(crate) host: H,
677    /// Last public mode the cursor-shape emitter saw. Drives
678    /// [`Editor::emit_cursor_shape_if_changed`] so `Host::emit_cursor_shape`
679    /// fires exactly once per mode transition without sprinkling the
680    /// call across every `vim.mode = ...` site.
681    pub(crate) last_emitted_mode: crate::VimMode,
682    /// Search FSM state (pattern + per-row match cache + wrapscan).
683    /// 0.0.35: relocated out of `hjkl_buffer::Buffer` per
684    /// `DESIGN_33_METHOD_CLASSIFICATION.md` step 1.
685    /// 0.0.37: the buffer-side bridge (`Buffer::search_pattern`) is
686    /// gone; `BufferView` now takes the active regex as a `&Regex`
687    /// parameter, sourced from `Editor::search_state().pattern`.
688    pub(crate) search_state: crate::search::SearchState,
689    /// Per-row syntax span overlay. Source of truth for the host's
690    /// renderer ([`hjkl_buffer::BufferView::spans`]). Populated by
691    /// [`Editor::install_syntax_spans`] /
692    /// [`Editor::install_ratatui_syntax_spans`] (and, in due course,
693    /// by `Host::syntax_highlights` once the engine drives that path
694    /// directly).
695    ///
696    /// 0.0.37: lifted out of `hjkl_buffer::Buffer` per step 3 of
697    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer-side cache +
698    /// `Buffer::set_spans` / `Buffer::spans` accessors are gone.
699    pub(crate) buffer_spans: Vec<Vec<hjkl_buffer::Span>>,
700    /// Pending `ContentEdit` records emitted by `mutate_edit`. Drained by
701    /// hosts via [`Editor::take_content_edits`] for fan-in to a syntax
702    /// tree (or any other content-change observer that needs byte-level
703    /// position deltas). Edges are byte-indexed and `(row, col_byte)`.
704    pub(crate) pending_content_edits: Vec<crate::types::ContentEdit>,
705    /// Pending "reset" flag set when the entire buffer is replaced
706    /// (e.g. `set_content` / `restore`). Supersedes any queued
707    /// `pending_content_edits` on the same frame: hosts call
708    /// [`Editor::take_content_reset`] before draining edits.
709    pub(crate) pending_content_reset: bool,
710    /// Row range touched by the most recent `auto_indent_rows` call.
711    /// `(top_row, bot_row)` inclusive. Set by the engine after every
712    /// auto-indent operation; drained (and cleared) by the host via
713    /// [`Editor::take_last_indent_range`] so it can display a brief
714    /// visual flash over the reindented rows.
715    pub(crate) last_indent_range: Option<(usize, usize)>,
716}
717
718/// Vim-style options surfaced by `:set`. New fields land here as
719/// individual ex commands gain `:set` plumbing.
720#[derive(Debug, Clone)]
721pub struct Settings {
722    /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
723    pub shiftwidth: usize,
724    /// Visual width of a `\t` character. Stored for future render
725    /// hookup; not yet consumed by the buffer renderer.
726    pub tabstop: usize,
727    /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
728    /// without an explicit `i` flag.
729    pub ignore_case: bool,
730    /// When true *and* `ignore_case` is true, an uppercase letter in
731    /// the pattern flips that search back to case-sensitive. Matches
732    /// vim's `:set smartcase`. Default `false`.
733    pub smartcase: bool,
734    /// Wrap searches past buffer ends. Matches vim's `:set wrapscan`.
735    /// Default `true`.
736    pub wrapscan: bool,
737    /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
738    pub textwidth: usize,
739    /// When `true`, the Tab key in insert mode inserts `tabstop` spaces
740    /// instead of a literal `\t`. Matches vim's `:set expandtab`.
741    /// Default `false`.
742    pub expandtab: bool,
743    /// Soft tab stop in spaces. When `> 0`, Tab inserts spaces to the
744    /// next softtabstop boundary (when `expandtab`), and Backspace at the
745    /// end of a softtabstop-aligned space run deletes the entire run as
746    /// if it were one tab. `0` disables. Matches vim's `:set softtabstop`.
747    pub softtabstop: usize,
748    /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
749    /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
750    /// past the right edge and `top_col` clips the left side.
751    /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
752    /// to word-break wrap; `:set nowrap` resets.
753    pub wrap: hjkl_buffer::Wrap,
754    /// When true, the engine drops every edit before it touches the
755    /// buffer — undo, dirty flag, and change log all stay clean.
756    /// Matches vim's `:set readonly` / `:set ro`. Default `false`.
757    pub readonly: bool,
758    /// When `true`, pressing Enter in insert mode copies the leading
759    /// whitespace of the current line onto the new line. Matches vim's
760    /// `:set autoindent`. Default `true` (vim parity).
761    pub autoindent: bool,
762    /// When `true`, bumps indent by one `shiftwidth` after a line ending
763    /// in `{` / `(` / `[`, and strips one indent unit when the user types
764    /// `}` / `)` / `]` on a whitespace-only line. See `compute_enter_indent`
765    /// in `vim.rs` for the tree-sitter plug-in seam. Default `true`.
766    pub smartindent: bool,
767    /// Cap on undo-stack length. Older entries are pruned past this
768    /// bound. `0` means unlimited. Matches vim's `:set undolevels`.
769    /// Default `1000`.
770    pub undo_levels: u32,
771    /// When `true`, cursor motions inside insert mode break the
772    /// current undo group (so a single `u` only reverses the run of
773    /// keystrokes that preceded the motion). Default `true`.
774    /// Currently a no-op — engine doesn't yet break the undo group
775    /// on insert-mode motions; field is wired through `:set
776    /// undobreak` for forward compatibility.
777    pub undo_break_on_motion: bool,
778    /// Vim-flavoured "what counts as a word" character class.
779    /// Comma-separated tokens: `@` = `is_alphabetic()`, `_` = literal
780    /// `_`, `48-57` = decimal char range, bare integer = single char
781    /// code, single ASCII punctuation = literal. Default
782    /// `"@,48-57,_,192-255"` matches vim.
783    pub iskeyword: String,
784    /// Multi-key sequence timeout (e.g. `gg`, `dd`). When the user
785    /// pauses longer than this between keys, any pending prefix is
786    /// abandoned and the next key starts a fresh sequence. Matches
787    /// vim's `:set timeoutlen` / `:set tm` (millis). Default 1000ms.
788    pub timeout_len: core::time::Duration,
789    /// When true, render absolute line numbers in the gutter. Matches
790    /// vim's `:set number` / `:set nu`. Default `true`.
791    pub number: bool,
792    /// When true, render line numbers as offsets from the cursor row.
793    /// Combined with `number`, the cursor row shows its absolute number
794    /// while other rows show the relative offset (vim's `nu+rnu` hybrid).
795    /// Matches vim's `:set relativenumber` / `:set rnu`. Default `false`.
796    pub relativenumber: bool,
797    /// Minimum gutter width in cells for the line-number column.
798    /// Width grows past this to fit the largest displayed number.
799    /// Matches vim's `:set numberwidth` / `:set nuw`. Default `4`.
800    /// Range 1..=20.
801    pub numberwidth: usize,
802    /// Highlight the row where the cursor sits. Matches vim's `:set cursorline`.
803    /// Default `false`.
804    pub cursorline: bool,
805    /// Highlight the column where the cursor sits. Matches vim's `:set cursorcolumn`.
806    /// Default `false`.
807    pub cursorcolumn: bool,
808    /// Sign-column display mode. Matches vim's `:set signcolumn`.
809    /// Default [`crate::types::SignColumnMode::Auto`].
810    pub signcolumn: crate::types::SignColumnMode,
811    /// Number of cells reserved for a fold-marker gutter.
812    /// Matches vim's `:set foldcolumn`. Default `0`.
813    pub foldcolumn: u32,
814    /// Comma-separated 1-based column indices for vertical rulers.
815    /// Matches vim's `:set colorcolumn`. Default `""`.
816    pub colorcolumn: String,
817}
818
819impl Default for Settings {
820    fn default() -> Self {
821        Self {
822            shiftwidth: 4,
823            tabstop: 4,
824            softtabstop: 4,
825            ignore_case: false,
826            smartcase: false,
827            wrapscan: true,
828            textwidth: 79,
829            expandtab: true,
830            wrap: hjkl_buffer::Wrap::None,
831            readonly: false,
832            autoindent: true,
833            smartindent: true,
834            undo_levels: 1000,
835            undo_break_on_motion: true,
836            iskeyword: "@,48-57,_,192-255".to_string(),
837            timeout_len: core::time::Duration::from_millis(1000),
838            number: true,
839            relativenumber: false,
840            numberwidth: 4,
841            cursorline: false,
842            cursorcolumn: false,
843            signcolumn: crate::types::SignColumnMode::Auto,
844            foldcolumn: 0,
845            colorcolumn: String::new(),
846        }
847    }
848}
849
850/// Translate a SPEC [`crate::types::Options`] into the engine's
851/// internal [`Settings`] representation. Field-by-field map; the
852/// shapes are isomorphic except for type widths
853/// (`u32` vs `usize`, [`crate::types::WrapMode`] vs
854/// [`hjkl_buffer::Wrap`]). 0.1.0 (Patch C-δ) collapses both into one
855/// type once the `Editor<B, H>::new(buffer, host, options)` constructor
856/// is the canonical entry point.
857fn settings_from_options(o: &crate::types::Options) -> Settings {
858    Settings {
859        shiftwidth: o.shiftwidth as usize,
860        tabstop: o.tabstop as usize,
861        softtabstop: o.softtabstop as usize,
862        ignore_case: o.ignorecase,
863        smartcase: o.smartcase,
864        wrapscan: o.wrapscan,
865        textwidth: o.textwidth as usize,
866        expandtab: o.expandtab,
867        wrap: match o.wrap {
868            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
869            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
870            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
871        },
872        readonly: o.readonly,
873        autoindent: o.autoindent,
874        smartindent: o.smartindent,
875        undo_levels: o.undo_levels,
876        undo_break_on_motion: o.undo_break_on_motion,
877        iskeyword: o.iskeyword.clone(),
878        timeout_len: o.timeout_len,
879        number: o.number,
880        relativenumber: o.relativenumber,
881        numberwidth: o.numberwidth,
882        cursorline: o.cursorline,
883        cursorcolumn: o.cursorcolumn,
884        signcolumn: o.signcolumn,
885        foldcolumn: o.foldcolumn,
886        colorcolumn: o.colorcolumn.clone(),
887    }
888}
889
890/// Host-observable LSP requests triggered by editor bindings. The
891/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
892/// intent that the TUI layer picks up and routes to `sqls`.
893#[derive(Debug, Clone, Copy, PartialEq, Eq)]
894pub enum LspIntent {
895    /// `gd` — textDocument/definition at the cursor.
896    GotoDefinition,
897}
898
899impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
900    /// Build an [`Editor`] from a buffer, host adapter, and SPEC options.
901    ///
902    /// 0.1.0 (Patch C-δ): canonical, frozen constructor per SPEC §"Editor
903    /// surface". Replaces the pre-0.1.0 `Editor::new(KeybindingMode)` /
904    /// `with_host` / `with_options` triad — there is no shim.
905    ///
906    /// Consumers that don't need a custom host pass
907    /// [`crate::types::DefaultHost::new()`]; consumers that don't need
908    /// custom options pass [`crate::types::Options::default()`].
909    pub fn new(buffer: hjkl_buffer::Buffer, host: H, options: crate::types::Options) -> Self {
910        let settings = settings_from_options(&options);
911        Self {
912            keybinding_mode: KeybindingMode::Vim,
913            last_yank: None,
914            vim: VimState::default(),
915            undo_stack: Vec::new(),
916            redo_stack: Vec::new(),
917            content_dirty: false,
918            cached_content: None,
919            viewport_height: AtomicU16::new(0),
920            pending_lsp: None,
921            pending_fold_ops: Vec::new(),
922            buffer,
923            #[cfg(feature = "ratatui")]
924            style_table: Vec::new(),
925            #[cfg(not(feature = "ratatui"))]
926            engine_style_table: Vec::new(),
927            registers: crate::registers::Registers::default(),
928            #[cfg(feature = "ratatui")]
929            styled_spans: Vec::new(),
930            settings,
931            marks: std::collections::BTreeMap::new(),
932            syntax_fold_ranges: Vec::new(),
933            change_log: Vec::new(),
934            sticky_col: None,
935            host,
936            last_emitted_mode: crate::VimMode::Normal,
937            search_state: crate::search::SearchState::new(),
938            buffer_spans: Vec::new(),
939            pending_content_edits: Vec::new(),
940            pending_content_reset: false,
941            last_indent_range: None,
942        }
943    }
944}
945
946impl<B: crate::types::Buffer, H: crate::types::Host> Editor<B, H> {
947    /// Borrow the buffer (typed `&B`). Host renders through this via
948    /// `hjkl_buffer::BufferView` when `B = hjkl_buffer::Buffer`.
949    pub fn buffer(&self) -> &B {
950        &self.buffer
951    }
952
953    /// Mutably borrow the buffer (typed `&mut B`).
954    pub fn buffer_mut(&mut self) -> &mut B {
955        &mut self.buffer
956    }
957
958    /// Borrow the host adapter directly (typed `&H`).
959    pub fn host(&self) -> &H {
960        &self.host
961    }
962
963    /// Mutably borrow the host adapter (typed `&mut H`).
964    pub fn host_mut(&mut self) -> &mut H {
965        &mut self.host
966    }
967}
968
969impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
970    /// Update the active `iskeyword` spec for word motions
971    /// (`w`/`b`/`e`/`ge` and engine-side `*`/`#` pickup). 0.0.28
972    /// hoisted iskeyword storage out of `Buffer` — `Editor` is the
973    /// single owner now. Equivalent to assigning
974    /// `settings_mut().iskeyword` directly; the dedicated setter is
975    /// retained for source-compatibility with 0.0.27 callers.
976    pub fn set_iskeyword(&mut self, spec: impl Into<String>) {
977        self.settings.iskeyword = spec.into();
978    }
979
980    /// Emit `Host::emit_cursor_shape` if the public mode has changed
981    /// since the last emit. Engine calls this at the end of every input
982    /// step so mode transitions surface to the host without sprinkling
983    /// the call across every `vim.mode = ...` site.
984    pub fn emit_cursor_shape_if_changed(&mut self) {
985        let mode = self.vim_mode();
986        if mode == self.last_emitted_mode {
987            return;
988        }
989        let shape = match mode {
990            crate::VimMode::Insert => crate::types::CursorShape::Bar,
991            _ => crate::types::CursorShape::Block,
992        };
993        self.host.emit_cursor_shape(shape);
994        self.last_emitted_mode = mode;
995    }
996
997    /// Record a yank/cut payload. Writes both the legacy
998    /// [`Editor::last_yank`] field (drained directly by 0.0.28-era
999    /// hosts) and the new [`crate::types::Host::write_clipboard`]
1000    /// side-channel (Patch B). Consumers should migrate to a `Host`
1001    /// impl whose `write_clipboard` queues the platform-clipboard
1002    /// write; the `last_yank` mirror will be removed at 0.1.0.
1003    pub(crate) fn record_yank_to_host(&mut self, text: String) {
1004        self.host.write_clipboard(text.clone());
1005        self.last_yank = Some(text);
1006    }
1007
1008    /// Vim's sticky column (curswant). `None` before the first motion;
1009    /// hosts shouldn't normally need to read this directly — it's
1010    /// surfaced for migration off `Buffer::sticky_col` and for
1011    /// snapshot tests.
1012    pub fn sticky_col(&self) -> Option<usize> {
1013        self.sticky_col
1014    }
1015
1016    /// Replace the sticky column. Hosts should rarely touch this —
1017    /// motion code maintains it through the standard horizontal /
1018    /// vertical motion paths.
1019    pub fn set_sticky_col(&mut self, col: Option<usize>) {
1020        self.sticky_col = col;
1021    }
1022
1023    /// Host hook: replace the cached syntax-derived block ranges that
1024    /// `:foldsyntax` consumes. the host calls this on every re-parse;
1025    /// the cost is just a `Vec` swap.
1026    /// Look up a named mark by character. Returns `(row, col)` if
1027    /// set; `None` otherwise. Both lowercase (`'a`–`'z`) and
1028    /// uppercase (`'A`–`'Z`) marks live in the same unified
1029    /// [`Editor::marks`] map as of 0.0.36.
1030    pub fn mark(&self, c: char) -> Option<(usize, usize)> {
1031        self.marks.get(&c).copied()
1032    }
1033
1034    /// Set the named mark `c` to `(row, col)`. Used by the FSM's
1035    /// `m{a-zA-Z}` keystroke and by [`Editor::restore_snapshot`].
1036    pub fn set_mark(&mut self, c: char, pos: (usize, usize)) {
1037        self.marks.insert(c, pos);
1038    }
1039
1040    /// Remove the named mark `c` (no-op if unset).
1041    pub fn clear_mark(&mut self, c: char) {
1042        self.marks.remove(&c);
1043    }
1044
1045    /// Look up a buffer-local lowercase mark (`'a`–`'z`). Kept as a
1046    /// thin wrapper over [`Editor::mark`] for source compatibility
1047    /// with pre-0.0.36 callers; new code should call
1048    /// [`Editor::mark`] directly.
1049    #[deprecated(
1050        since = "0.0.36",
1051        note = "use Editor::mark — lowercase + uppercase marks now live in a single map"
1052    )]
1053    pub fn buffer_mark(&self, c: char) -> Option<(usize, usize)> {
1054        self.mark(c)
1055    }
1056
1057    /// Discard the most recent undo entry. Used by ex commands that
1058    /// pre-emptively pushed an undo state (`:s`, `:r`) but ended up
1059    /// matching nothing — popping prevents a no-op undo step from
1060    /// polluting the user's history.
1061    ///
1062    /// Returns `true` if an entry was discarded.
1063    pub fn pop_last_undo(&mut self) -> bool {
1064        self.undo_stack.pop().is_some()
1065    }
1066
1067    /// Read all named marks set this session — both lowercase
1068    /// (`'a`–`'z`) and uppercase (`'A`–`'Z`). Iteration is
1069    /// deterministic (BTreeMap-ordered) so snapshot / `:marks`
1070    /// output is stable.
1071    pub fn marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1072        self.marks.iter().map(|(c, p)| (*c, *p))
1073    }
1074
1075    /// Read all buffer-local lowercase marks. Kept for source
1076    /// compatibility with pre-0.0.36 callers (e.g. `:marks` ex
1077    /// command); new code should use [`Editor::marks`] which
1078    /// iterates the unified map.
1079    #[deprecated(
1080        since = "0.0.36",
1081        note = "use Editor::marks — lowercase + uppercase marks now live in a single map"
1082    )]
1083    pub fn buffer_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1084        self.marks
1085            .iter()
1086            .filter(|(c, _)| c.is_ascii_lowercase())
1087            .map(|(c, p)| (*c, *p))
1088    }
1089
1090    /// Position the cursor was at when the user last jumped via
1091    /// `<C-o>` / `g;` / similar. `None` before any jump.
1092    pub fn last_jump_back(&self) -> Option<(usize, usize)> {
1093        self.vim.jump_back.last().copied()
1094    }
1095
1096    /// Position of the last edit (where `.` would replay). `None` if
1097    /// no edit has happened yet in this session.
1098    pub fn last_edit_pos(&self) -> Option<(usize, usize)> {
1099        self.vim.last_edit_pos
1100    }
1101
1102    /// Read-only view of the file-marks table — uppercase / "file"
1103    /// marks (`'A`–`'Z`) the host has set this session. Returns an
1104    /// iterator of `(mark_char, (row, col))` pairs.
1105    ///
1106    /// Mutate via the FSM (`m{A-Z}` keystroke) or via
1107    /// [`Editor::restore_snapshot`].
1108    ///
1109    /// 0.0.36: file marks now live in the unified [`Editor::marks`]
1110    /// map; this accessor is kept for source compatibility and
1111    /// filters the unified map to uppercase entries.
1112    pub fn file_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1113        self.marks
1114            .iter()
1115            .filter(|(c, _)| c.is_ascii_uppercase())
1116            .map(|(c, p)| (*c, *p))
1117    }
1118
1119    /// Read-only view of the cached syntax-derived block ranges that
1120    /// `:foldsyntax` consumes. Returns the slice the host last
1121    /// installed via [`Editor::set_syntax_fold_ranges`]; empty when
1122    /// no syntax integration is active.
1123    pub fn syntax_fold_ranges(&self) -> &[(usize, usize)] {
1124        &self.syntax_fold_ranges
1125    }
1126
1127    pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
1128        self.syntax_fold_ranges = ranges;
1129    }
1130
1131    /// Live settings (read-only). `:set` mutates these via
1132    /// [`Editor::settings_mut`].
1133    pub fn settings(&self) -> &Settings {
1134        &self.settings
1135    }
1136
1137    /// Live settings (mutable). `:set` flows through here to mutate
1138    /// shiftwidth / tabstop / textwidth / ignore_case / wrap. Hosts
1139    /// configuring at startup typically construct a [`Settings`]
1140    /// snapshot and overwrite via `*editor.settings_mut() = …`.
1141    pub fn settings_mut(&mut self) -> &mut Settings {
1142        &mut self.settings
1143    }
1144
1145    /// Returns `true` when `:set readonly` is active. Convenience
1146    /// accessor for hosts that cannot import the internal [`Settings`]
1147    /// type. Phase 5 binary uses this to gate `:w` writes.
1148    pub fn is_readonly(&self) -> bool {
1149        self.settings.readonly
1150    }
1151
1152    /// Borrow the engine search state. Hosts inspecting the
1153    /// committed `/` / `?` pattern (e.g. for status-line display) or
1154    /// feeding the active regex into `BufferView::search_pattern`
1155    /// read it from here.
1156    pub fn search_state(&self) -> &crate::search::SearchState {
1157        &self.search_state
1158    }
1159
1160    /// Mutable engine search state. Hosts driving search
1161    /// programmatically (test fixtures, scripted demos) write the
1162    /// pattern through here.
1163    pub fn search_state_mut(&mut self) -> &mut crate::search::SearchState {
1164        &mut self.search_state
1165    }
1166
1167    /// Install `pattern` as the active search regex on the engine
1168    /// state and clear the cached row matches. Pass `None` to clear.
1169    /// 0.0.37: dropped the buffer-side mirror that 0.0.35 introduced
1170    /// — `BufferView` now takes the regex through its `search_pattern`
1171    /// field per step 3 of `DESIGN_33_METHOD_CLASSIFICATION.md`.
1172    pub fn set_search_pattern(&mut self, pattern: Option<regex::Regex>) {
1173        self.search_state.set_pattern(pattern);
1174    }
1175
1176    /// Drive `n` (or the `/` commit equivalent) — advance the cursor
1177    /// to the next match of `search_state.pattern` from the cursor's
1178    /// current position. Returns `true` when a match was found.
1179    /// `skip_current = true` excludes a match the cursor sits on.
1180    pub fn search_advance_forward(&mut self, skip_current: bool) -> bool {
1181        crate::search::search_forward(&mut self.buffer, &mut self.search_state, skip_current)
1182    }
1183
1184    /// Drive `N` — symmetric counterpart of [`Editor::search_advance_forward`].
1185    pub fn search_advance_backward(&mut self, skip_current: bool) -> bool {
1186        crate::search::search_backward(&mut self.buffer, &mut self.search_state, skip_current)
1187    }
1188
1189    /// Install styled syntax spans using `ratatui::style::Style`. The
1190    /// ratatui-flavoured variant of [`Editor::install_syntax_spans`].
1191    /// Drops zero-width runs and clamps `end` to the line's char length
1192    /// so the buffer cache doesn't see runaway ranges. Behind the
1193    /// `ratatui` feature; non-ratatui hosts use the unprefixed
1194    /// [`Editor::install_syntax_spans`] (engine-native `Style`).
1195    ///
1196    /// Renamed from `install_syntax_spans` in 0.0.32 — the unprefixed
1197    /// name now belongs to the engine-native variant per SPEC 0.1.0
1198    /// freeze ("engine never imports ratatui").
1199    #[cfg(feature = "ratatui")]
1200    pub fn install_ratatui_syntax_spans(
1201        &mut self,
1202        spans: Vec<Vec<(usize, usize, ratatui::style::Style)>>,
1203    ) {
1204        // Look up `line_byte_lens` lazily — only fetch a row's length
1205        // when it has at least one span. On a 100k-line file with
1206        // ~50 visible rows, this avoids an O(N) buffer walk per frame.
1207        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1208        for (row, row_spans) in spans.iter().enumerate() {
1209            if row_spans.is_empty() {
1210                by_row.push(Vec::new());
1211                continue;
1212            }
1213            let line_len = buf_line(&self.buffer, row).map(str::len).unwrap_or(0);
1214            let mut translated = Vec::with_capacity(row_spans.len());
1215            for (start, end, style) in row_spans {
1216                let end_clamped = (*end).min(line_len);
1217                if end_clamped <= *start {
1218                    continue;
1219                }
1220                let id = self.intern_ratatui_style(*style);
1221                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1222            }
1223            by_row.push(translated);
1224        }
1225        self.buffer_spans = by_row;
1226        self.styled_spans = spans;
1227    }
1228
1229    /// Snapshot of the unnamed register (the default `p` / `P` source).
1230    pub fn yank(&self) -> &str {
1231        &self.registers.unnamed.text
1232    }
1233
1234    /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
1235    pub fn registers(&self) -> &crate::registers::Registers {
1236        &self.registers
1237    }
1238
1239    /// Mutably borrow the full register bank. Hosts that share registers
1240    /// across multiple editors (e.g. multi-buffer `yy` / `p`) overwrite
1241    /// the slots here on buffer switch.
1242    pub fn registers_mut(&mut self) -> &mut crate::registers::Registers {
1243        &mut self.registers
1244    }
1245
1246    /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
1247    /// register slot. the host calls this before letting vim consume a
1248    /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
1249    /// stale snapshot from the last yank.
1250    pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
1251        self.registers.set_clipboard(text, linewise);
1252    }
1253
1254    /// Return the user's pending register selection (set via `"<reg>` chord
1255    /// before an operator). `None` if no register was selected — caller should
1256    /// use the unnamed register `"`.
1257    ///
1258    /// Read-only — does not consume / clear the pending selection. The
1259    /// register is cleared by the engine after the next operator fires.
1260    ///
1261    /// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
1262    /// honor `"a` + visual op chord sequences.
1263    pub fn pending_register(&self) -> Option<char> {
1264        self.vim.pending_register
1265    }
1266
1267    /// True when the user's pending register selector is `+` or `*`.
1268    /// the host peeks this so it can refresh `sync_clipboard_register`
1269    /// only when a clipboard read is actually about to happen.
1270    pub fn pending_register_is_clipboard(&self) -> bool {
1271        matches!(self.vim.pending_register, Some('+') | Some('*'))
1272    }
1273
1274    /// Register currently being recorded into via `q{reg}`. `None` when
1275    /// no recording is active. Hosts use this to surface a "recording @r"
1276    /// indicator in the status line.
1277    pub fn recording_register(&self) -> Option<char> {
1278        self.vim.recording_macro
1279    }
1280
1281    /// Pending repeat count the user has typed but not yet resolved
1282    /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1283    /// Hosts surface this in a "showcmd" area.
1284    pub fn pending_count(&self) -> Option<u32> {
1285        self.vim.pending_count_val()
1286    }
1287
1288    /// The operator character for any in-flight operator that is waiting
1289    /// for a motion (e.g. `d` after the user types `d` but before a
1290    /// motion). Returns `None` when no operator is pending.
1291    pub fn pending_op(&self) -> Option<char> {
1292        self.vim.pending_op_char()
1293    }
1294
1295    /// `true` when the engine is in any pending chord state — waiting for
1296    /// the next key to complete a command (e.g. `r<char>` replace,
1297    /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1298    /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1299    /// register selection `"<reg>`, macro recording target, etc).
1300    ///
1301    /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1302    /// and forward keys directly to the engine so in-flight commands can
1303    /// complete without the host eating their continuation keys.
1304    pub fn is_chord_pending(&self) -> bool {
1305        self.vim.is_chord_pending()
1306    }
1307
1308    /// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
1309    /// is waiting for the next typed character to name the register to paste.
1310    /// The dispatcher should call `insert_paste_register(c)` instead of
1311    /// `insert_char(c)` for the next printable key, then the flag auto-clears.
1312    ///
1313    /// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
1314    /// without having to drive the full FSM.
1315    pub fn is_insert_register_pending(&self) -> bool {
1316        self.vim.insert_pending_register
1317    }
1318
1319    /// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
1320    /// before `insert_paste_register(c)` in app-level dispatchers so that the
1321    /// flag does not persist into the next key. Call before
1322    /// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
1323    ///
1324    /// Phase 6.5: used by `dispatch_insert_key` in the app crate.
1325    pub fn clear_insert_register_pending(&mut self) {
1326        self.vim.insert_pending_register = false;
1327    }
1328
1329    /// Read-only view of the jump-back list (positions pushed on "big"
1330    /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1331    #[allow(clippy::type_complexity)]
1332    pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1333        (&self.vim.jump_back, &self.vim.jump_fwd)
1334    }
1335
1336    /// Read-only view of the change list (positions of recent edits) plus
1337    /// the current walk cursor. Newest entry is at the back.
1338    pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1339        (&self.vim.change_list, self.vim.change_list_cursor)
1340    }
1341
1342    /// Replace the unnamed register without touching any other slot.
1343    /// For host-driven imports (e.g. system clipboard); operator
1344    /// code uses [`record_yank`] / [`record_delete`].
1345    pub fn set_yank(&mut self, text: impl Into<String>) {
1346        let text = text.into();
1347        let linewise = self.vim.yank_linewise;
1348        self.registers.unnamed = crate::registers::Slot { text, linewise };
1349    }
1350
1351    /// Record a yank into `"` and `"0`, plus the named target if the
1352    /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1353    /// paste path.
1354    pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1355        self.vim.yank_linewise = linewise;
1356        let target = self.vim.pending_register.take();
1357        self.registers.record_yank(text, linewise, target);
1358    }
1359
1360    /// Direct write to a named register slot — bypasses the unnamed
1361    /// `"` and `"0` updates that `record_yank` does. Used by the
1362    /// macro recorder so finishing a `q{reg}` recording doesn't
1363    /// pollute the user's last yank.
1364    pub(crate) fn set_named_register_text(&mut self, reg: char, text: String) {
1365        if let Some(slot) = match reg {
1366            'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1367            'A'..='Z' => {
1368                Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1369            }
1370            _ => None,
1371        } {
1372            slot.text = text;
1373            slot.linewise = false;
1374        }
1375    }
1376
1377    /// Record a delete / change into `"` and the `"1`–`"9` ring.
1378    /// Honours the active named-register prefix.
1379    pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1380        self.vim.yank_linewise = linewise;
1381        let target = self.vim.pending_register.take();
1382        self.registers.record_delete(text, linewise, target);
1383    }
1384
1385    /// Install styled syntax spans using the engine-native
1386    /// [`crate::types::Style`]. Always available, regardless of the
1387    /// `ratatui` feature. Hosts depending on ratatui can use the
1388    /// ratatui-flavoured [`Editor::install_ratatui_syntax_spans`].
1389    ///
1390    /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1391    /// 0.1.0 freeze the unprefixed name is the universally-available
1392    /// engine-native variant ("engine never imports ratatui").
1393    pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1394        let line_byte_lens: Vec<usize> = (0..buf_row_count(&self.buffer))
1395            .map(|r| buf_line(&self.buffer, r).map(str::len).unwrap_or(0))
1396            .collect();
1397        let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1398        #[cfg(feature = "ratatui")]
1399        let mut ratatui_spans: Vec<Vec<(usize, usize, ratatui::style::Style)>> =
1400            Vec::with_capacity(spans.len());
1401        for (row, row_spans) in spans.iter().enumerate() {
1402            let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
1403            let mut translated = Vec::with_capacity(row_spans.len());
1404            #[cfg(feature = "ratatui")]
1405            let mut translated_r = Vec::with_capacity(row_spans.len());
1406            for (start, end, style) in row_spans {
1407                let end_clamped = (*end).min(line_len);
1408                if end_clamped <= *start {
1409                    continue;
1410                }
1411                let id = self.intern_style(*style);
1412                translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1413                #[cfg(feature = "ratatui")]
1414                translated_r.push((*start, end_clamped, engine_style_to_ratatui(*style)));
1415            }
1416            by_row.push(translated);
1417            #[cfg(feature = "ratatui")]
1418            ratatui_spans.push(translated_r);
1419        }
1420        self.buffer_spans = by_row;
1421        #[cfg(feature = "ratatui")]
1422        {
1423            self.styled_spans = ratatui_spans;
1424        }
1425    }
1426
1427    /// Intern a `ratatui::style::Style` and return the opaque id used
1428    /// in `hjkl_buffer::Span::style`. The ratatui-flavoured variant of
1429    /// [`Editor::intern_style`]. Linear-scan dedup — the table grows
1430    /// only as new tree-sitter token kinds appear, so it stays tiny.
1431    /// Behind the `ratatui` feature.
1432    ///
1433    /// Renamed from `intern_style` in 0.0.32 — at 0.1.0 freeze the
1434    /// unprefixed name belongs to the engine-native variant.
1435    #[cfg(feature = "ratatui")]
1436    pub fn intern_ratatui_style(&mut self, style: ratatui::style::Style) -> u32 {
1437        if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1438            return idx as u32;
1439        }
1440        self.style_table.push(style);
1441        (self.style_table.len() - 1) as u32
1442    }
1443
1444    /// Read-only view of the style table — id `i` → `style_table[i]`.
1445    /// The render path passes a closure backed by this slice as the
1446    /// `StyleResolver` for `BufferView`. Behind the `ratatui` feature.
1447    #[cfg(feature = "ratatui")]
1448    pub fn style_table(&self) -> &[ratatui::style::Style] {
1449        &self.style_table
1450    }
1451
1452    /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1453    /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1454    /// per draw frame.
1455    ///
1456    /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1457    /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1458    /// caches spans; they live on the engine and route through the
1459    /// `Host::syntax_highlights` pipeline.
1460    pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1461        &self.buffer_spans
1462    }
1463
1464    /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1465    /// With the `ratatui` feature on, the id matches the one
1466    /// [`Editor::intern_ratatui_style`] would return for the equivalent
1467    /// `ratatui::Style` (both share the underlying table). With it off,
1468    /// the engine keeps a parallel `crate::types::Style`-keyed table
1469    /// — ids are still stable per-editor.
1470    ///
1471    /// Hosts that don't depend on ratatui (buffr, future GUI shells)
1472    /// reach this method to populate the table during syntax span
1473    /// installation.
1474    ///
1475    /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1476    /// the unprefixed name is the universally-available engine-native
1477    /// variant.
1478    pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1479        #[cfg(feature = "ratatui")]
1480        {
1481            let r = engine_style_to_ratatui(style);
1482            self.intern_ratatui_style(r)
1483        }
1484        #[cfg(not(feature = "ratatui"))]
1485        {
1486            if let Some(idx) = self.engine_style_table.iter().position(|s| *s == style) {
1487                return idx as u32;
1488            }
1489            self.engine_style_table.push(style);
1490            (self.engine_style_table.len() - 1) as u32
1491        }
1492    }
1493
1494    /// Look up an interned style by id and return it as a SPEC
1495    /// [`crate::types::Style`]. Returns `None` for ids past the end
1496    /// of the table.
1497    pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1498        #[cfg(feature = "ratatui")]
1499        {
1500            let r = self.style_table.get(id as usize).copied()?;
1501            Some(ratatui_style_to_engine(r))
1502        }
1503        #[cfg(not(feature = "ratatui"))]
1504        {
1505            self.engine_style_table.get(id as usize).copied()
1506        }
1507    }
1508
1509    /// Historical reverse-sync hook from when the textarea mirrored
1510    /// the buffer. Now that Buffer is the cursor authority this is a
1511    /// no-op; call sites can remain in place during the migration.
1512    pub fn push_buffer_cursor_to_textarea(&mut self) {}
1513
1514    /// Force the host viewport's top row without touching the
1515    /// cursor. Used by tests that simulate a scroll without the
1516    /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1517    /// apply.
1518    ///
1519    /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1520    /// instead of the (now-deleted) `Buffer::viewport_mut`.
1521    pub fn set_viewport_top(&mut self, row: usize) {
1522        let last = buf_row_count(&self.buffer).saturating_sub(1);
1523        let target = row.min(last);
1524        self.host.viewport_mut().top_row = target;
1525    }
1526
1527    /// Set the cursor to `(row, col)`, clamped to the buffer's
1528    /// content. Hosts use this for goto-line, jump-to-mark, and
1529    /// programmatic cursor placement.
1530    pub fn jump_cursor(&mut self, row: usize, col: usize) {
1531        buf_set_cursor_rc(&mut self.buffer, row, col);
1532    }
1533
1534    /// `(row, col)` cursor read sourced from the migration buffer.
1535    /// Equivalent to `self.textarea.cursor()` when the two are in
1536    /// sync — which is the steady state during Phase 7f because
1537    /// every step opens with `sync_buffer_content_from_textarea` and
1538    /// every ported motion pushes the result back. Prefer this over
1539    /// `self.textarea.cursor()` so call sites keep working unchanged
1540    /// once the textarea field is ripped.
1541    pub fn cursor(&self) -> (usize, usize) {
1542        buf_cursor_rc(&self.buffer)
1543    }
1544
1545    /// Drain any pending LSP intent raised by the last key. Returns
1546    /// `None` when no intent is armed.
1547    pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1548        self.pending_lsp.take()
1549    }
1550
1551    /// Drain every [`crate::types::FoldOp`] raised since the last
1552    /// call. Hosts that mirror the engine's fold storage (or that
1553    /// project folds onto a separate fold tree, LSP folding ranges,
1554    /// …) drain this each step and dispatch as their own
1555    /// [`crate::types::Host::Intent`] requires.
1556    ///
1557    /// The engine has already applied every op locally against the
1558    /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1559    /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1560    /// don't track folds independently can ignore the queue
1561    /// (or simply never call this drain).
1562    ///
1563    /// Introduced in 0.0.38 (Patch C-δ.4).
1564    pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1565        std::mem::take(&mut self.pending_fold_ops)
1566    }
1567
1568    /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1569    /// surface: queue it for host observation (drained by
1570    /// [`Editor::take_fold_ops`]) and apply it locally against the
1571    /// in-tree buffer fold storage via
1572    /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1573    /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1574    /// invalidation) route every fold mutation through this method.
1575    ///
1576    /// Introduced in 0.0.38 (Patch C-δ.4).
1577    pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1578        use crate::types::FoldProvider;
1579        self.pending_fold_ops.push(op);
1580        let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1581        provider.apply(op);
1582    }
1583
1584    /// Refresh the host viewport's height from the cached
1585    /// `viewport_height_value()`. Called from the per-step
1586    /// boilerplate; was the textarea → buffer mirror before Phase 7f
1587    /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1588    /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1589    /// `Host::viewport_mut`.
1590    pub(crate) fn sync_buffer_from_textarea(&mut self) {
1591        let height = self.viewport_height_value();
1592        self.host.viewport_mut().height = height;
1593    }
1594
1595    /// Was the full textarea → buffer content sync. Buffer is the
1596    /// content authority now; this remains as a no-op so the per-step
1597    /// call sites don't have to be ripped in the same patch.
1598    pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1599        self.sync_buffer_from_textarea();
1600    }
1601
1602    /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1603    /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1604    /// the cursor without going through the vim engine's motion
1605    /// machinery, where push_jump fires automatically.
1606    pub fn record_jump(&mut self, pos: (usize, usize)) {
1607        const JUMPLIST_MAX: usize = 100;
1608        self.vim.jump_back.push(pos);
1609        if self.vim.jump_back.len() > JUMPLIST_MAX {
1610            self.vim.jump_back.remove(0);
1611        }
1612        self.vim.jump_fwd.clear();
1613    }
1614
1615    /// Host apps call this each draw with the current text area height so
1616    /// scroll helpers can clamp the cursor without recomputing layout.
1617    pub fn set_viewport_height(&self, height: u16) {
1618        self.viewport_height.store(height, Ordering::Relaxed);
1619    }
1620
1621    /// Last height published by `set_viewport_height` (in rows).
1622    pub fn viewport_height_value(&self) -> u16 {
1623        self.viewport_height.load(Ordering::Relaxed)
1624    }
1625
1626    /// Apply `edit` against the buffer and return the inverse so the
1627    /// host can push it onto an undo stack. Side effects: dirty
1628    /// flag, change-list ring, mark / jump-list shifts, change_log
1629    /// append, fold invalidation around the touched rows.
1630    ///
1631    /// The primary edit funnel — both FSM operators and ex commands
1632    /// route mutations through here so the side effects fire
1633    /// uniformly.
1634    pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1635        // `:set readonly` short-circuits every mutation funnel: no
1636        // buffer change, no dirty flag, no undo entry, no change-log
1637        // emission. We swallow the requested `edit` and hand back a
1638        // self-inverse no-op (`InsertStr` of an empty string at the
1639        // current cursor) so callers that push the return value onto
1640        // an undo stack still get a structurally valid round trip.
1641        if self.settings.readonly {
1642            let _ = edit;
1643            return hjkl_buffer::Edit::InsertStr {
1644                at: buf_cursor_pos(&self.buffer),
1645                text: String::new(),
1646            };
1647        }
1648        let pre_row = buf_cursor_row(&self.buffer);
1649        let pre_rows = buf_row_count(&self.buffer);
1650        // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1651        // Vim's `:h '.` says "the position where the last change was made",
1652        // meaning the change-start, not the post-insert cursor. We snap it
1653        // here before `apply_buffer_edit` moves the cursor.
1654        let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1655        // Map the underlying buffer edit to a SPEC EditOp for
1656        // change-log emission before consuming it. Coarse — see
1657        // change_log field doc on the struct.
1658        self.change_log.extend(edit_to_editops(&edit));
1659        // Compute ContentEdit fan-out from the pre-edit buffer state.
1660        // Done before `apply_buffer_edit` consumes `edit` so we can
1661        // inspect the operation's fields and the buffer's pre-edit row
1662        // bytes (needed for byte_of_row / col_byte conversion). Edits
1663        // are pushed onto `pending_content_edits` for host drain.
1664        let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1665        self.pending_content_edits.extend(content_edits);
1666        // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1667        // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1668        // the 0.0.42 plan — see that fn's doc comment). The free fn
1669        // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1670        // no longer carries a `self.buffer.<inherent>` hop.
1671        let inverse = apply_buffer_edit(&mut self.buffer, edit);
1672        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1673        // Drop any folds the edit's range overlapped — vim opens the
1674        // surrounding fold automatically when you edit inside it. The
1675        // approximation here invalidates folds covering either the
1676        // pre-edit cursor row or the post-edit cursor row, which
1677        // catches the common single-line / multi-line edit shapes.
1678        let lo = pre_row.min(pos_row);
1679        let hi = pre_row.max(pos_row);
1680        self.apply_fold_op(crate::types::FoldOp::Invalidate {
1681            start_row: lo,
1682            end_row: hi,
1683        });
1684        // Dot mark records the PRE-edit position (change start), matching
1685        // vim's `:h '.` semantics. Previously this stored the post-edit
1686        // cursor, which diverged from nvim on `iX<Esc>j`.
1687        self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1688        // Append to the change-list ring (skip when the cursor sits on
1689        // the same cell as the last entry — back-to-back keystrokes on
1690        // one column shouldn't pollute the ring). A new edit while
1691        // walking the ring trims the forward half, vim style.
1692        let entry = (pos_row, pos_col);
1693        if self.vim.change_list.last() != Some(&entry) {
1694            if let Some(idx) = self.vim.change_list_cursor.take() {
1695                self.vim.change_list.truncate(idx + 1);
1696            }
1697            self.vim.change_list.push(entry);
1698            let len = self.vim.change_list.len();
1699            if len > crate::vim::CHANGE_LIST_MAX {
1700                self.vim
1701                    .change_list
1702                    .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1703            }
1704        }
1705        self.vim.change_list_cursor = None;
1706        // Shift / drop marks + jump-list entries to track the row
1707        // delta the edit produced. Without this, every line-changing
1708        // edit silently invalidates `'a`-style positions.
1709        let post_rows = buf_row_count(&self.buffer);
1710        let delta = post_rows as isize - pre_rows as isize;
1711        if delta != 0 {
1712            self.shift_marks_after_edit(pre_row, delta);
1713        }
1714        self.push_buffer_content_to_textarea();
1715        self.mark_content_dirty();
1716        inverse
1717    }
1718
1719    /// Migrate user marks + jumplist entries when an edit at row
1720    /// `edit_start` changes the buffer's row count by `delta` (positive
1721    /// for inserts, negative for deletes). Marks tied to a deleted row
1722    /// are dropped; marks past the affected band shift by `delta`.
1723    fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1724        if delta == 0 {
1725            return;
1726        }
1727        // Deleted-row band (only meaningful for delta < 0). Inclusive
1728        // start, exclusive end.
1729        let drop_end = if delta < 0 {
1730            edit_start.saturating_add((-delta) as usize)
1731        } else {
1732            edit_start
1733        };
1734        let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1735
1736        // 0.0.36: lowercase + uppercase marks share the unified
1737        // `marks` map; one pass migrates both.
1738        let mut to_drop: Vec<char> = Vec::new();
1739        for (c, (row, _col)) in self.marks.iter_mut() {
1740            if (edit_start..drop_end).contains(row) {
1741                to_drop.push(*c);
1742            } else if *row >= shift_threshold {
1743                *row = ((*row as isize) + delta).max(0) as usize;
1744            }
1745        }
1746        for c in to_drop {
1747            self.marks.remove(&c);
1748        }
1749
1750        let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1751            entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1752            for (row, _) in entries.iter_mut() {
1753                if *row >= shift_threshold {
1754                    *row = ((*row as isize) + delta).max(0) as usize;
1755                }
1756            }
1757        };
1758        shift_jumps(&mut self.vim.jump_back);
1759        shift_jumps(&mut self.vim.jump_fwd);
1760    }
1761
1762    /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1763    /// the textarea from the buffer's lines + cursor, preserving yank
1764    /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1765    /// textarea field disappears at the end of Phase 7f anyway.
1766    /// No-op since Buffer is the content authority. Retained as a
1767    /// shim so call sites in `mutate_edit` and friends don't have to
1768    /// be ripped in lockstep with the field removal.
1769    pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1770
1771    /// Single choke-point for "the buffer just changed". Sets the
1772    /// dirty flag and drops the cached `content_arc` snapshot so
1773    /// subsequent reads rebuild from the live textarea. Callers
1774    /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1775    /// path) must invoke this to keep the cache honest.
1776    pub fn mark_content_dirty(&mut self) {
1777        self.content_dirty = true;
1778        self.cached_content = None;
1779    }
1780
1781    /// Returns true if content changed since the last call, then clears the flag.
1782    pub fn take_dirty(&mut self) -> bool {
1783        let dirty = self.content_dirty;
1784        self.content_dirty = false;
1785        dirty
1786    }
1787
1788    /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1789    /// the last call. Each entry corresponds to a single buffer
1790    /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1791    /// fan out to one entry per row touched.
1792    ///
1793    /// Hosts call this each frame (after [`Editor::take_content_reset`])
1794    /// to fan edits into a tree-sitter parser via `Tree::edit`.
1795    pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1796        std::mem::take(&mut self.pending_content_edits)
1797    }
1798
1799    /// Returns `true` if a bulk buffer replacement happened since the
1800    /// last call (e.g. `set_content` / `restore` / undo restore), then
1801    /// clears the flag. When this returns `true`, hosts should drop
1802    /// any retained syntax tree before consuming
1803    /// [`Editor::take_content_edits`].
1804    pub fn take_content_reset(&mut self) -> bool {
1805        let r = self.pending_content_reset;
1806        self.pending_content_reset = false;
1807        r
1808    }
1809
1810    /// Pull-model coarse change observation. If content changed since
1811    /// the last call, returns `Some(Arc<String>)` with the new content
1812    /// and clears the dirty flag; otherwise returns `None`.
1813    ///
1814    /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1815    /// the character level) should diff against their own previous
1816    /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1817    /// once every edit path inside the engine is instrumented; this
1818    /// coarse form covers the pull-model use case in the meantime.
1819    pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1820        if !self.content_dirty {
1821            return None;
1822        }
1823        let arc = self.content_arc();
1824        self.content_dirty = false;
1825        Some(arc)
1826    }
1827
1828    /// Width in cells of the line-number gutter for the current buffer
1829    /// and settings. Matches what [`Editor::cursor_screen_pos`] reserves
1830    /// in front of the text column. Returns `0` when both `number` and
1831    /// `relativenumber` are off.
1832    pub fn lnum_width(&self) -> u16 {
1833        if self.settings.number || self.settings.relativenumber {
1834            let needed = buf_row_count(&self.buffer).to_string().len() + 1;
1835            needed.max(self.settings.numberwidth) as u16
1836        } else {
1837            0
1838        }
1839    }
1840
1841    /// Returns the cursor's row within the visible textarea (0-based), updating
1842    /// the stored viewport top so subsequent calls remain accurate.
1843    pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
1844        let cursor = buf_cursor_row(&self.buffer);
1845        let top = self.host.viewport().top_row;
1846        cursor.saturating_sub(top).min(height as usize - 1) as u16
1847    }
1848
1849    /// Returns the cursor's screen position `(x, y)` for the textarea
1850    /// described by `(area_x, area_y, area_width, area_height)`.
1851    /// Accounts for line-number gutter, viewport scroll, and any extra
1852    /// gutter width to the left of the number column (sign column, fold
1853    /// column). Returns `None` if the cursor is outside the visible
1854    /// viewport. Always available (engine-native; no ratatui dependency).
1855    ///
1856    /// `extra_gutter_width` is added to the number-column width before
1857    /// computing the cursor x position. Callers (e.g. `apps/hjkl/src/render.rs`)
1858    /// pass `sign_w + fold_w` here so the cursor lands on the correct cell
1859    /// when a dedicated sign or fold column is present.
1860    ///
1861    /// Renamed from `cursor_screen_pos_xywh` in 0.0.32 — the
1862    /// ratatui-flavoured `Rect` variant is now
1863    /// [`Editor::cursor_screen_pos_in_rect`] (cfg `ratatui`).
1864    pub fn cursor_screen_pos(
1865        &self,
1866        area_x: u16,
1867        area_y: u16,
1868        area_width: u16,
1869        area_height: u16,
1870        extra_gutter_width: u16,
1871    ) -> Option<(u16, u16)> {
1872        let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1873        let v = self.host.viewport();
1874        if pos_row < v.top_row || pos_col < v.top_col {
1875            return None;
1876        }
1877        let lnum_width = self.lnum_width();
1878        // Full offset from the left edge of the window to the first text cell.
1879        let gutter_total = lnum_width + extra_gutter_width;
1880        let dy = (pos_row - v.top_row) as u16;
1881        // Convert char column to visual column so cursor lands on the
1882        // correct cell when the line contains tabs (which the renderer
1883        // expands to TAB_WIDTH stops). Tab width must match the renderer.
1884        let line = self.buffer.line(pos_row).unwrap_or("");
1885        let tab_width = if v.tab_width == 0 {
1886            4
1887        } else {
1888            v.tab_width as usize
1889        };
1890        let visual_pos = visual_col_for_char(line, pos_col, tab_width);
1891        let visual_top = visual_col_for_char(line, v.top_col, tab_width);
1892        let dx = (visual_pos - visual_top) as u16;
1893        if dy >= area_height || dx + gutter_total >= area_width {
1894            return None;
1895        }
1896        Some((area_x + gutter_total + dx, area_y + dy))
1897    }
1898
1899    /// Ratatui [`Rect`]-flavoured wrapper around
1900    /// [`Editor::cursor_screen_pos`]. Behind the `ratatui` feature.
1901    ///
1902    /// Renamed from `cursor_screen_pos` in 0.0.32 — the unprefixed
1903    /// name now belongs to the engine-native variant.
1904    ///
1905    /// `extra_gutter_width` is the combined width of the sign column and
1906    /// fold column that sit to the left of the number column (and thus
1907    /// to the left of the text area). Pass `sign_w + fold_w` from the
1908    /// render layer.
1909    #[cfg(feature = "ratatui")]
1910    pub fn cursor_screen_pos_in_rect(
1911        &self,
1912        area: Rect,
1913        extra_gutter_width: u16,
1914    ) -> Option<(u16, u16)> {
1915        self.cursor_screen_pos(area.x, area.y, area.width, area.height, extra_gutter_width)
1916    }
1917
1918    /// Returns the current vim mode. Phase 6.3: reads from the stable
1919    /// `current_mode` field (kept in sync by both the FSM step loop and
1920    /// the Phase 6.3 primitive bridges) rather than deriving from the
1921    /// FSM-internal `mode` field via `public_mode()`.
1922    pub fn vim_mode(&self) -> VimMode {
1923        self.vim.current_mode
1924    }
1925
1926    /// Bounds of the active visual-block rectangle as
1927    /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
1928    /// `None` when we're not in VisualBlock mode.
1929    /// Read-only view of the live `/` or `?` prompt. `None` outside
1930    /// search-prompt mode.
1931    pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
1932        self.vim.search_prompt.as_ref()
1933    }
1934
1935    /// Most recent committed search pattern (persists across `n` / `N`
1936    /// and across prompt exits). `None` before the first search.
1937    pub fn last_search(&self) -> Option<&str> {
1938        self.vim.last_search.as_deref()
1939    }
1940
1941    /// Whether the last committed search was a forward `/` (`true`) or
1942    /// a backward `?` (`false`). `n` and `N` consult this to honour the
1943    /// direction the user committed.
1944    pub fn last_search_forward(&self) -> bool {
1945        self.vim.last_search_forward
1946    }
1947
1948    /// Set the most recent committed search text + direction. Used by
1949    /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
1950    /// outside the engine's vim FSM) so `n` / `N` repeat the host's
1951    /// most recent commit with the right direction. Pass `None` /
1952    /// `true` to clear.
1953    pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
1954        self.vim.last_search = text;
1955        self.vim.last_search_forward = forward;
1956    }
1957
1958    /// Start/end `(row, col)` of the active char-wise Visual selection
1959    /// (inclusive on both ends, positionally ordered). `None` when not
1960    /// in Visual mode.
1961    pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
1962        if self.vim_mode() != VimMode::Visual {
1963            return None;
1964        }
1965        let anchor = self.vim.visual_anchor;
1966        let cursor = self.cursor();
1967        let (start, end) = if anchor <= cursor {
1968            (anchor, cursor)
1969        } else {
1970            (cursor, anchor)
1971        };
1972        Some((start, end))
1973    }
1974
1975    /// Top/bottom rows of the active VisualLine selection (inclusive).
1976    /// `None` when we're not in VisualLine mode.
1977    pub fn line_highlight(&self) -> Option<(usize, usize)> {
1978        if self.vim_mode() != VimMode::VisualLine {
1979            return None;
1980        }
1981        let anchor = self.vim.visual_line_anchor;
1982        let cursor = buf_cursor_row(&self.buffer);
1983        Some((anchor.min(cursor), anchor.max(cursor)))
1984    }
1985
1986    pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
1987        if self.vim_mode() != VimMode::VisualBlock {
1988            return None;
1989        }
1990        let (ar, ac) = self.vim.block_anchor;
1991        let cr = buf_cursor_row(&self.buffer);
1992        let cc = self.vim.block_vcol;
1993        let top = ar.min(cr);
1994        let bot = ar.max(cr);
1995        let left = ac.min(cc);
1996        let right = ac.max(cc);
1997        Some((top, bot, left, right))
1998    }
1999
2000    /// Active selection in `hjkl_buffer::Selection` shape. `None` when
2001    /// not in a Visual mode. Phase 7d-i wiring — the host hands this
2002    /// straight to `BufferView` once render flips off textarea
2003    /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
2004    /// switch).
2005    pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
2006        use hjkl_buffer::{Position, Selection};
2007        match self.vim_mode() {
2008            VimMode::Visual => {
2009                let (ar, ac) = self.vim.visual_anchor;
2010                let head = buf_cursor_pos(&self.buffer);
2011                Some(Selection::Char {
2012                    anchor: Position::new(ar, ac),
2013                    head,
2014                })
2015            }
2016            VimMode::VisualLine => {
2017                let anchor_row = self.vim.visual_line_anchor;
2018                let head_row = buf_cursor_row(&self.buffer);
2019                Some(Selection::Line {
2020                    anchor_row,
2021                    head_row,
2022                })
2023            }
2024            VimMode::VisualBlock => {
2025                let (ar, ac) = self.vim.block_anchor;
2026                let cr = buf_cursor_row(&self.buffer);
2027                let cc = self.vim.block_vcol;
2028                Some(Selection::Block {
2029                    anchor: Position::new(ar, ac),
2030                    head: Position::new(cr, cc),
2031                })
2032            }
2033            _ => None,
2034        }
2035    }
2036
2037    /// Force back to normal mode (used when dismissing completions etc.)
2038    pub fn force_normal(&mut self) {
2039        self.vim.force_normal();
2040    }
2041
2042    pub fn content(&self) -> String {
2043        let n = buf_row_count(&self.buffer);
2044        let mut s = String::new();
2045        for r in 0..n {
2046            if r > 0 {
2047                s.push('\n');
2048            }
2049            s.push_str(crate::types::Query::line(&self.buffer, r as u32));
2050        }
2051        s.push('\n');
2052        s
2053    }
2054
2055    /// Same logical output as [`content`], but returns a cached
2056    /// `Arc<String>` so back-to-back reads within an un-mutated window
2057    /// are ref-count bumps instead of multi-MB joins. The cache is
2058    /// invalidated by every [`mark_content_dirty`] call.
2059    pub fn content_arc(&mut self) -> std::sync::Arc<String> {
2060        if let Some(arc) = &self.cached_content {
2061            return std::sync::Arc::clone(arc);
2062        }
2063        let arc = std::sync::Arc::new(self.content());
2064        self.cached_content = Some(std::sync::Arc::clone(&arc));
2065        arc
2066    }
2067
2068    pub fn set_content(&mut self, text: &str) {
2069        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
2070        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
2071            lines.pop();
2072        }
2073        if lines.is_empty() {
2074            lines.push(String::new());
2075        }
2076        let _ = lines;
2077        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2078        self.undo_stack.clear();
2079        self.redo_stack.clear();
2080        // Whole-buffer replace supersedes any queued ContentEdits.
2081        self.pending_content_edits.clear();
2082        self.pending_content_reset = true;
2083        self.mark_content_dirty();
2084    }
2085
2086    /// Whole-buffer replace that **preserves the undo history**.
2087    ///
2088    /// Equivalent to [`Editor::set_content`] but pushes the current buffer
2089    /// state onto the undo stack first, so a subsequent `u` walks back to
2090    /// the pre-replacement content. Use this for any operation the user
2091    /// expects to undo as a single step — e.g. external formatter output
2092    /// (`hjkl-mangler`) installed via the async [`crate::app::FormatWorker`].
2093    ///
2094    /// Like `push_undo`, this clears the redo stack (vim semantics: any
2095    /// new edit invalidates redo).
2096    pub fn set_content_undoable(&mut self, text: &str) {
2097        self.push_undo();
2098        let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
2099        while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
2100            lines.pop();
2101        }
2102        if lines.is_empty() {
2103            lines.push(String::new());
2104        }
2105        let _ = lines;
2106        crate::types::BufferEdit::replace_all(&mut self.buffer, text);
2107        // Whole-buffer replace supersedes any queued ContentEdits.
2108        self.pending_content_edits.clear();
2109        self.pending_content_reset = true;
2110        self.mark_content_dirty();
2111    }
2112
2113    /// Drain the pending change log produced by buffer mutations.
2114    ///
2115    /// Returns a `Vec<EditOp>` covering edits applied since the last
2116    /// call. Empty when no edits ran. Pull-model, complementary to
2117    /// [`Editor::take_content_change`] which gives back the new full
2118    /// content.
2119    ///
2120    /// Mapping coverage:
2121    /// - InsertChar / InsertStr → exact `EditOp` with empty range +
2122    ///   replacement.
2123    /// - DeleteRange (`Char` kind) → exact range + empty replacement.
2124    /// - Replace → exact range + new replacement.
2125    /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
2126    ///   InsertBlock, DeleteBlockChunks → best-effort placeholder
2127    ///   covering the touched range. Hosts wanting per-cell deltas
2128    ///   should diff their own `lines()` snapshot.
2129    pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
2130        std::mem::take(&mut self.change_log)
2131    }
2132
2133    /// Read the engine's current settings as a SPEC
2134    /// [`crate::types::Options`].
2135    ///
2136    /// Bridges between the legacy [`Settings`] (which carries fewer
2137    /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
2138    /// not present in `Settings` fall back to vim defaults (e.g.,
2139    /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
2140    /// Once trait extraction lands, this becomes the canonical config
2141    /// reader and `Settings` retires.
2142    pub fn current_options(&self) -> crate::types::Options {
2143        crate::types::Options {
2144            shiftwidth: self.settings.shiftwidth as u32,
2145            tabstop: self.settings.tabstop as u32,
2146            softtabstop: self.settings.softtabstop as u32,
2147            textwidth: self.settings.textwidth as u32,
2148            expandtab: self.settings.expandtab,
2149            ignorecase: self.settings.ignore_case,
2150            smartcase: self.settings.smartcase,
2151            wrapscan: self.settings.wrapscan,
2152            wrap: match self.settings.wrap {
2153                hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
2154                hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
2155                hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
2156            },
2157            readonly: self.settings.readonly,
2158            autoindent: self.settings.autoindent,
2159            smartindent: self.settings.smartindent,
2160            undo_levels: self.settings.undo_levels,
2161            undo_break_on_motion: self.settings.undo_break_on_motion,
2162            iskeyword: self.settings.iskeyword.clone(),
2163            timeout_len: self.settings.timeout_len,
2164            ..crate::types::Options::default()
2165        }
2166    }
2167
2168    /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
2169    /// Only the fields backed by today's [`Settings`] take effect;
2170    /// remaining options become live once trait extraction wires them
2171    /// through.
2172    pub fn apply_options(&mut self, opts: &crate::types::Options) {
2173        self.settings.shiftwidth = opts.shiftwidth as usize;
2174        self.settings.tabstop = opts.tabstop as usize;
2175        self.settings.softtabstop = opts.softtabstop as usize;
2176        self.settings.textwidth = opts.textwidth as usize;
2177        self.settings.expandtab = opts.expandtab;
2178        self.settings.ignore_case = opts.ignorecase;
2179        self.settings.smartcase = opts.smartcase;
2180        self.settings.wrapscan = opts.wrapscan;
2181        self.settings.wrap = match opts.wrap {
2182            crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
2183            crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
2184            crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
2185        };
2186        self.settings.readonly = opts.readonly;
2187        self.settings.autoindent = opts.autoindent;
2188        self.settings.smartindent = opts.smartindent;
2189        self.settings.undo_levels = opts.undo_levels;
2190        self.settings.undo_break_on_motion = opts.undo_break_on_motion;
2191        self.set_iskeyword(opts.iskeyword.clone());
2192        self.settings.timeout_len = opts.timeout_len;
2193        self.settings.number = opts.number;
2194        self.settings.relativenumber = opts.relativenumber;
2195        self.settings.numberwidth = opts.numberwidth;
2196        self.settings.cursorline = opts.cursorline;
2197        self.settings.cursorcolumn = opts.cursorcolumn;
2198        self.settings.signcolumn = opts.signcolumn;
2199        self.settings.foldcolumn = opts.foldcolumn;
2200        self.settings.colorcolumn = opts.colorcolumn.clone();
2201    }
2202
2203    /// Active visual selection as a SPEC [`crate::types::Highlight`]
2204    /// with [`crate::types::HighlightKind::Selection`].
2205    ///
2206    /// Returns `None` when the editor isn't in a Visual mode.
2207    /// Visual-line and visual-block selections collapse to the
2208    /// bounding char range of the selection — the SPEC `Selection`
2209    /// kind doesn't carry sub-line info today; hosts that need full
2210    /// line / block geometry continue to read [`buffer_selection`]
2211    /// (the legacy [`hjkl_buffer::Selection`] shape).
2212    pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2213        use crate::types::{Highlight, HighlightKind, Pos};
2214        let sel = self.buffer_selection()?;
2215        let (start, end) = match sel {
2216            hjkl_buffer::Selection::Char { anchor, head } => {
2217                let a = (anchor.row, anchor.col);
2218                let h = (head.row, head.col);
2219                if a <= h { (a, h) } else { (h, a) }
2220            }
2221            hjkl_buffer::Selection::Line {
2222                anchor_row,
2223                head_row,
2224            } => {
2225                let (top, bot) = if anchor_row <= head_row {
2226                    (anchor_row, head_row)
2227                } else {
2228                    (head_row, anchor_row)
2229                };
2230                let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2231                ((top, 0), (bot, last_col))
2232            }
2233            hjkl_buffer::Selection::Block { anchor, head } => {
2234                let (top, bot) = if anchor.row <= head.row {
2235                    (anchor.row, head.row)
2236                } else {
2237                    (head.row, anchor.row)
2238                };
2239                let (left, right) = if anchor.col <= head.col {
2240                    (anchor.col, head.col)
2241                } else {
2242                    (head.col, anchor.col)
2243                };
2244                ((top, left), (bot, right))
2245            }
2246        };
2247        Some(Highlight {
2248            range: Pos {
2249                line: start.0 as u32,
2250                col: start.1 as u32,
2251            }..Pos {
2252                line: end.0 as u32,
2253                col: end.1 as u32,
2254            },
2255            kind: HighlightKind::Selection,
2256        })
2257    }
2258
2259    /// SPEC-typed highlights for `line`.
2260    ///
2261    /// Two emission modes:
2262    ///
2263    /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2264    ///   `Editor::search_prompt` is `Some`. Live-preview matches of
2265    ///   the in-flight pattern surface as
2266    ///   [`crate::types::HighlightKind::IncSearch`].
2267    /// - **SearchMatch**: the prompt has been committed (or absent)
2268    ///   and the buffer's armed pattern is non-empty. Matches surface
2269    ///   as [`crate::types::HighlightKind::SearchMatch`].
2270    ///
2271    /// Selection / MatchParen / Syntax(id) variants land once the
2272    /// trait extraction routes the FSM's selection set + the host's
2273    /// syntax pipeline through the [`crate::types::Host`] trait.
2274    ///
2275    /// Returns an empty vec when there is nothing to highlight or
2276    /// `line` is out of bounds.
2277    pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2278        use crate::types::{Highlight, HighlightKind, Pos};
2279        let row = line as usize;
2280        if row >= buf_row_count(&self.buffer) {
2281            return Vec::new();
2282        }
2283
2284        // Live preview while the prompt is open beats the committed
2285        // pattern.
2286        if let Some(prompt) = self.search_prompt() {
2287            if prompt.text.is_empty() {
2288                return Vec::new();
2289            }
2290            let Ok(re) = regex::Regex::new(&prompt.text) else {
2291                return Vec::new();
2292            };
2293            let Some(haystack) = buf_line(&self.buffer, row) else {
2294                return Vec::new();
2295            };
2296            return re
2297                .find_iter(haystack)
2298                .map(|m| Highlight {
2299                    range: Pos {
2300                        line,
2301                        col: m.start() as u32,
2302                    }..Pos {
2303                        line,
2304                        col: m.end() as u32,
2305                    },
2306                    kind: HighlightKind::IncSearch,
2307                })
2308                .collect();
2309        }
2310
2311        if self.search_state.pattern.is_none() {
2312            return Vec::new();
2313        }
2314        let dgen = crate::types::Query::dirty_gen(&self.buffer);
2315        crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2316            .into_iter()
2317            .map(|(start, end)| Highlight {
2318                range: Pos {
2319                    line,
2320                    col: start as u32,
2321                }..Pos {
2322                    line,
2323                    col: end as u32,
2324                },
2325                kind: HighlightKind::SearchMatch,
2326            })
2327            .collect()
2328    }
2329
2330    /// Build the engine's [`crate::types::RenderFrame`] for the
2331    /// current state. Hosts call this once per redraw and diff
2332    /// across frames.
2333    ///
2334    /// Coarse today — covers mode + cursor + cursor shape + viewport
2335    /// top + line count. SPEC-target fields (selections, highlights,
2336    /// command line, search prompt, status line) land once trait
2337    /// extraction routes them through `SelectionSet` and the
2338    /// `Highlight` pipeline.
2339    pub fn render_frame(&self) -> crate::types::RenderFrame {
2340        use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2341        let (cursor_row, cursor_col) = self.cursor();
2342        let (mode, shape) = match self.vim_mode() {
2343            crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2344            crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2345            crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2346            crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2347            crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2348        };
2349        RenderFrame {
2350            mode,
2351            cursor_row: cursor_row as u32,
2352            cursor_col: cursor_col as u32,
2353            cursor_shape: shape,
2354            viewport_top: self.host.viewport().top_row as u32,
2355            line_count: crate::types::Query::line_count(&self.buffer),
2356        }
2357    }
2358
2359    /// Capture the editor's coarse state into a serde-friendly
2360    /// [`crate::types::EditorSnapshot`].
2361    ///
2362    /// Today's snapshot covers mode, cursor, lines, viewport top.
2363    /// Registers, marks, jump list, undo tree, and full options arrive
2364    /// once phase 5 trait extraction lands the generic
2365    /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2366    /// stays stable; only the snapshot's internal fields grow.
2367    ///
2368    /// Distinct from the internal `snapshot` used by undo (which
2369    /// returns `(Vec<String>, (usize, usize))`); host-facing
2370    /// persistence goes through this one.
2371    pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2372        use crate::types::{EditorSnapshot, SnapshotMode};
2373        let mode = match self.vim_mode() {
2374            crate::VimMode::Normal => SnapshotMode::Normal,
2375            crate::VimMode::Insert => SnapshotMode::Insert,
2376            crate::VimMode::Visual => SnapshotMode::Visual,
2377            crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2378            crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2379        };
2380        let cursor = self.cursor();
2381        let cursor = (cursor.0 as u32, cursor.1 as u32);
2382        let lines: Vec<String> = buf_lines_to_vec(&self.buffer);
2383        let viewport_top = self.host.viewport().top_row as u32;
2384        let marks = self
2385            .marks
2386            .iter()
2387            .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2388            .collect();
2389        EditorSnapshot {
2390            version: EditorSnapshot::VERSION,
2391            mode,
2392            cursor,
2393            lines,
2394            viewport_top,
2395            registers: self.registers.clone(),
2396            marks,
2397        }
2398    }
2399
2400    /// Restore editor state from an [`EditorSnapshot`]. Returns
2401    /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2402    /// `version` doesn't match [`EditorSnapshot::VERSION`].
2403    ///
2404    /// Mode is best-effort: `SnapshotMode` only round-trips the
2405    /// status-line summary, not the full FSM state. Visual / Insert
2406    /// mode entry happens through synthetic key dispatch when needed.
2407    pub fn restore_snapshot(
2408        &mut self,
2409        snap: crate::types::EditorSnapshot,
2410    ) -> Result<(), crate::EngineError> {
2411        use crate::types::EditorSnapshot;
2412        if snap.version != EditorSnapshot::VERSION {
2413            return Err(crate::EngineError::SnapshotVersion(
2414                snap.version,
2415                EditorSnapshot::VERSION,
2416            ));
2417        }
2418        let text = snap.lines.join("\n");
2419        self.set_content(&text);
2420        self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2421        self.host.viewport_mut().top_row = snap.viewport_top as usize;
2422        self.registers = snap.registers;
2423        self.marks = snap
2424            .marks
2425            .into_iter()
2426            .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2427            .collect();
2428        Ok(())
2429    }
2430
2431    /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2432    /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2433    /// shape their payload.
2434    pub fn seed_yank(&mut self, text: String) {
2435        let linewise = text.ends_with('\n');
2436        self.vim.yank_linewise = linewise;
2437        self.registers.unnamed = crate::registers::Slot { text, linewise };
2438    }
2439
2440    /// Scroll the viewport down by `rows`. The cursor stays on its
2441    /// absolute line (vim convention) unless the scroll would take it
2442    /// off-screen — in that case it's clamped to the first row still
2443    /// visible.
2444    pub fn scroll_down(&mut self, rows: i16) {
2445        self.scroll_viewport(rows);
2446    }
2447
2448    /// Scroll the viewport up by `rows`. Cursor stays unless it would
2449    /// fall off the bottom of the new viewport, then clamp to the
2450    /// bottom-most visible row.
2451    pub fn scroll_up(&mut self, rows: i16) {
2452        self.scroll_viewport(-rows);
2453    }
2454
2455    /// Scroll the viewport right by `cols` columns. Only the horizontal
2456    /// offset (`top_col`) moves — the cursor is NOT adjusted (matches
2457    /// vim's `zl` behaviour for horizontal scroll without wrap).
2458    pub fn scroll_right(&mut self, cols: i16) {
2459        let vp = self.host.viewport_mut();
2460        let cols_i = cols as isize;
2461        let new_top = (vp.top_col as isize + cols_i).max(0) as usize;
2462        vp.top_col = new_top;
2463    }
2464
2465    /// Scroll the viewport left by `cols` columns. Delegates to
2466    /// `scroll_right` with a negated argument so the floor-at-zero
2467    /// clamp is shared.
2468    pub fn scroll_left(&mut self, cols: i16) {
2469        self.scroll_right(-cols);
2470    }
2471
2472    /// Vim's `scrolloff` default — keep the cursor at least this many
2473    /// rows away from the top / bottom edge of the viewport while
2474    /// scrolling. Collapses to `height / 2` for tiny viewports.
2475    const SCROLLOFF: usize = 5;
2476
2477    /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2478    /// rows from each edge. Replaces the bare
2479    /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2480    /// don't park the cursor on the very last visible row.
2481    pub fn ensure_cursor_in_scrolloff(&mut self) {
2482        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2483        if height == 0 {
2484            // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2485            // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2486            // Disjoint-field borrow split: `self.buffer` (immutable via
2487            // `folds` snapshot + cursor) and `self.host` (mutable
2488            // viewport ref) live on distinct struct fields, so one
2489            // statement satisfies the borrow checker.
2490            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2491            crate::viewport_math::ensure_cursor_visible(
2492                &self.buffer,
2493                &folds,
2494                self.host.viewport_mut(),
2495            );
2496            return;
2497        }
2498        // Cap margin at (height - 1) / 2 so the upper + lower bands
2499        // can't overlap on tiny windows (margin=5 + height=10 would
2500        // otherwise produce contradictory clamp ranges).
2501        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2502        // Soft-wrap path: scrolloff math runs in *screen rows*, not
2503        // doc rows, since a wrapped doc row spans many visual lines.
2504        if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2505            self.ensure_scrolloff_wrap(height, margin);
2506            return;
2507        }
2508        let cursor_row = buf_cursor_row(&self.buffer);
2509        let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2510        let v = self.host.viewport_mut();
2511        // Top edge: cursor_row should sit at >= top_row + margin.
2512        if cursor_row < v.top_row + margin {
2513            v.top_row = cursor_row.saturating_sub(margin);
2514        }
2515        // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2516        let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2517        if cursor_row > v.top_row + max_bottom {
2518            v.top_row = cursor_row.saturating_sub(max_bottom);
2519        }
2520        // Clamp top_row so we never scroll past the buffer's bottom.
2521        let max_top = last_row.saturating_sub(height.saturating_sub(1));
2522        if v.top_row > max_top {
2523            v.top_row = max_top;
2524        }
2525        // Defer to Buffer for column-side scroll (no scrolloff for
2526        // horizontal scrolling — vim default `sidescrolloff = 0`).
2527        let cursor = buf_cursor_pos(&self.buffer);
2528        self.host.viewport_mut().ensure_visible(cursor);
2529    }
2530
2531    /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2532    /// at a time so the cursor's *screen* row stays inside
2533    /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2534    /// buffer's bottom never leaves blank rows below it.
2535    fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2536        let cursor_row = buf_cursor_row(&self.buffer);
2537        // Step 1 — cursor above viewport: snap top to cursor row,
2538        // then we'll fix up the margin below.
2539        if cursor_row < self.host.viewport().top_row {
2540            let v = self.host.viewport_mut();
2541            v.top_row = cursor_row;
2542            v.top_col = 0;
2543        }
2544        // Step 2 — push top forward until cursor's screen row is
2545        // within the bottom margin (`csr <= height - 1 - margin`).
2546        // 0.0.33 (Patch C-γ): fold-iteration goes through the
2547        // [`crate::types::FoldProvider`] surface via
2548        // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2549        // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2550        // a `&Viewport` parameter; the host owns the viewport, so the
2551        // disjoint `(self.host, self.buffer)` borrows split cleanly.
2552        let max_csr = height.saturating_sub(1).saturating_sub(margin);
2553        loop {
2554            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2555            let csr =
2556                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2557                    .unwrap_or(0);
2558            if csr <= max_csr {
2559                break;
2560            }
2561            let top = self.host.viewport().top_row;
2562            let row_count = buf_row_count(&self.buffer);
2563            let next = {
2564                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2565                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2566            };
2567            let Some(next) = next else {
2568                break;
2569            };
2570            // Don't walk past the cursor's row.
2571            if next > cursor_row {
2572                self.host.viewport_mut().top_row = cursor_row;
2573                break;
2574            }
2575            self.host.viewport_mut().top_row = next;
2576        }
2577        // Step 3 — pull top backward until cursor's screen row is
2578        // past the top margin (`csr >= margin`).
2579        loop {
2580            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2581            let csr =
2582                crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2583                    .unwrap_or(0);
2584            if csr >= margin {
2585                break;
2586            }
2587            let top = self.host.viewport().top_row;
2588            let prev = {
2589                let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2590                <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2591            };
2592            let Some(prev) = prev else {
2593                break;
2594            };
2595            self.host.viewport_mut().top_row = prev;
2596        }
2597        // Step 4 — clamp top so the buffer's bottom doesn't leave
2598        // blank rows below it. `max_top_for_height` walks segments
2599        // backward from the last row until it accumulates `height`
2600        // screen rows.
2601        let max_top = {
2602            let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2603            crate::viewport_math::max_top_for_height(
2604                &self.buffer,
2605                &folds,
2606                self.host.viewport(),
2607                height,
2608            )
2609        };
2610        if self.host.viewport().top_row > max_top {
2611            self.host.viewport_mut().top_row = max_top;
2612        }
2613        self.host.viewport_mut().top_col = 0;
2614    }
2615
2616    fn scroll_viewport(&mut self, delta: i16) {
2617        if delta == 0 {
2618            return;
2619        }
2620        // Bump the host viewport's top within bounds.
2621        let total_rows = buf_row_count(&self.buffer) as isize;
2622        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2623        let cur_top = self.host.viewport().top_row as isize;
2624        let new_top = (cur_top + delta as isize)
2625            .max(0)
2626            .min((total_rows - 1).max(0)) as usize;
2627        self.host.viewport_mut().top_row = new_top;
2628        // Mirror to textarea so its viewport reads (still consumed by
2629        // a couple of helpers) stay accurate.
2630        let _ = cur_top;
2631        if height == 0 {
2632            return;
2633        }
2634        // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2635        // from the visible viewport edges.
2636        let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2637        let margin = Self::SCROLLOFF.min(height / 2);
2638        let min_row = new_top + margin;
2639        let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2640        let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2641        if target_row != cursor_row {
2642            let line_len = buf_line(&self.buffer, target_row)
2643                .map(|l| l.chars().count())
2644                .unwrap_or(0);
2645            let target_col = cursor_col.min(line_len.saturating_sub(1));
2646            buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2647        }
2648    }
2649
2650    pub fn goto_line(&mut self, line: usize) {
2651        let row = line.saturating_sub(1);
2652        let max = buf_row_count(&self.buffer).saturating_sub(1);
2653        let target = row.min(max);
2654        buf_set_cursor_rc(&mut self.buffer, target, 0);
2655        // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2656        // the cursor lands off-screen and the user has to scroll
2657        // manually to see it.
2658        self.ensure_cursor_in_scrolloff();
2659    }
2660
2661    /// Scroll so the cursor row lands at the given viewport position:
2662    /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2663    /// Cursor stays on its absolute line; only the viewport moves.
2664    pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2665        let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2666        if height == 0 {
2667            return;
2668        }
2669        let cur_row = buf_cursor_row(&self.buffer);
2670        let cur_top = self.host.viewport().top_row;
2671        // Scrolloff awareness: `zt` lands the cursor at the top edge
2672        // of the viable area (top + margin), `zb` at the bottom edge
2673        // (top + height - 1 - margin). Match the cap used by
2674        // `ensure_cursor_in_scrolloff` so contradictory bounds are
2675        // impossible on tiny viewports.
2676        let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2677        let new_top = match pos {
2678            CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2679            CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2680            CursorScrollTarget::Bottom => {
2681                cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2682            }
2683        };
2684        if new_top == cur_top {
2685            return;
2686        }
2687        self.host.viewport_mut().top_row = new_top;
2688    }
2689
2690    /// Jump the cursor to the given 1-based line/column, clamped to the document.
2691    pub fn jump_to(&mut self, line: usize, col: usize) {
2692        let r = line.saturating_sub(1);
2693        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2694        let r = r.min(max_row);
2695        let line_len = buf_line(&self.buffer, r)
2696            .map(|l| l.chars().count())
2697            .unwrap_or(0);
2698        let c = col.saturating_sub(1).min(line_len);
2699        buf_set_cursor_rc(&mut self.buffer, r, c);
2700    }
2701
2702    // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
2703    //
2704    // These primitives operate on document (row, col) coordinates that the HOST
2705    // computes from its own layout knowledge (cell geometry for the TUI host,
2706    // pixel geometry for the future GUI host). The engine has no u16 terminal
2707    // assumption here — it just moves the cursor in doc-space.
2708
2709    /// Set the cursor to the given doc-space `(row, col)`, clamped to the
2710    /// document bounds. Hosts use this for programmatic cursor placement and
2711    /// as the building block for the mouse-click path.
2712    ///
2713    /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
2714    /// position); values beyond that are clamped to `char_count`.
2715    pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
2716        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2717        let r = row.min(max_row);
2718        let line_len = buf_line(&self.buffer, r)
2719            .map(|l| l.chars().count())
2720            .unwrap_or(0);
2721        let c = col.min(line_len);
2722        buf_set_cursor_rc(&mut self.buffer, r, c);
2723    }
2724
2725    /// Handle a left-button click at doc-space `(row, col)`.
2726    ///
2727    /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
2728    /// parity for `undo_break_on_motion`), then moves the cursor. The host
2729    /// performs cell→doc or pixel→doc translation before calling this.
2730    ///
2731    /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
2732    /// cursor lives on chars and never on the implicit `\n` — `col` is
2733    /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
2734    /// allows the one-past-EOL insert position (`col == chars().count()`).
2735    ///
2736    /// Resets `sticky_col` to the clicked column so the next `j`/`k`
2737    /// motion uses the clicked column as the intended visual column
2738    /// (otherwise the cursor would snap back to the keyboard-tracked
2739    /// column on the first vertical motion after a click).
2740    pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
2741        if self.vim.is_visual() {
2742            self.vim.force_normal();
2743        }
2744        // Mouse-position click counts as a motion — break the active
2745        // insert-mode undo group when the toggle is on (vim parity).
2746        crate::vim::break_undo_group_in_insert(self);
2747
2748        let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2749        let r = row.min(max_row);
2750        let line_len = buf_line(&self.buffer, r)
2751            .map(|l| l.chars().count())
2752            .unwrap_or(0);
2753        let cap = if self.vim.current_mode == crate::VimMode::Insert {
2754            line_len
2755        } else {
2756            line_len.saturating_sub(1)
2757        };
2758        let c = col.min(cap);
2759        buf_set_cursor_rc(&mut self.buffer, r, c);
2760        self.sticky_col = Some(c);
2761    }
2762
2763    /// Begin a mouse-drag selection: anchor at the current cursor and enter
2764    /// Visual-char mode. Idempotent if already in Visual-char mode.
2765    pub fn mouse_begin_drag(&mut self) {
2766        if !self.vim.is_visual_char() {
2767            vim::enter_visual_char_bridge(self);
2768        }
2769    }
2770
2771    /// Extend an in-progress mouse drag to doc-space `(row, col)`.
2772    ///
2773    /// Moves the live cursor; the Visual anchor stays where
2774    /// [`Editor::mouse_begin_drag`] set it. Call after the host has
2775    /// translated the drag position to doc coordinates.
2776    pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
2777        self.set_cursor_doc(row, col);
2778    }
2779
2780    pub fn insert_str(&mut self, text: &str) {
2781        let pos = crate::types::Cursor::cursor(&self.buffer);
2782        crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2783        self.push_buffer_content_to_textarea();
2784        self.mark_content_dirty();
2785    }
2786
2787    pub fn accept_completion(&mut self, completion: &str) {
2788        use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2789        let cursor_pos = CursorTrait::cursor(&self.buffer);
2790        let cursor_row = cursor_pos.line as usize;
2791        let cursor_col = cursor_pos.col as usize;
2792        let line = buf_line(&self.buffer, cursor_row).unwrap_or("").to_string();
2793        let chars: Vec<char> = line.chars().collect();
2794        let prefix_len = chars[..cursor_col.min(chars.len())]
2795            .iter()
2796            .rev()
2797            .take_while(|c| c.is_alphanumeric() || **c == '_')
2798            .count();
2799        if prefix_len > 0 {
2800            let start = Pos {
2801                line: cursor_row as u32,
2802                col: (cursor_col - prefix_len) as u32,
2803            };
2804            BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2805        }
2806        let cursor = CursorTrait::cursor(&self.buffer);
2807        BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2808        self.push_buffer_content_to_textarea();
2809        self.mark_content_dirty();
2810    }
2811
2812    pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
2813        let rc = buf_cursor_rc(&self.buffer);
2814        (buf_lines_to_vec(&self.buffer), rc)
2815    }
2816
2817    /// Walk one step back through the undo history. Equivalent to the
2818    /// user pressing `u` in normal mode. Drains the most recent undo
2819    /// entry and pushes it onto the redo stack.
2820    pub fn undo(&mut self) {
2821        crate::vim::do_undo(self);
2822    }
2823
2824    /// Walk one step forward through the redo history. Equivalent to
2825    /// `<C-r>` in normal mode.
2826    pub fn redo(&mut self) {
2827        crate::vim::do_redo(self);
2828    }
2829
2830    /// Snapshot current buffer state onto the undo stack and clear
2831    /// the redo stack. Bounded by `settings.undo_levels` — older
2832    /// entries pruned. Call before any group of buffer mutations the
2833    /// user might want to undo as a single step.
2834    pub fn push_undo(&mut self) {
2835        let snap = self.snapshot();
2836        self.undo_stack.push(snap);
2837        self.cap_undo();
2838        self.redo_stack.clear();
2839    }
2840
2841    /// Trim the undo stack down to `settings.undo_levels`, dropping
2842    /// the oldest entries. `undo_levels == 0` is treated as
2843    /// "unlimited" (vim's 0-means-no-undo semantics intentionally
2844    /// skipped — guarding with `> 0` is one line shorter than gating
2845    /// the cap path with an explicit zero-check above the call site).
2846    pub(crate) fn cap_undo(&mut self) {
2847        let cap = self.settings.undo_levels as usize;
2848        if cap > 0 && self.undo_stack.len() > cap {
2849            let diff = self.undo_stack.len() - cap;
2850            self.undo_stack.drain(..diff);
2851        }
2852    }
2853
2854    /// Test-only accessor for the undo stack length.
2855    #[doc(hidden)]
2856    pub fn undo_stack_len(&self) -> usize {
2857        self.undo_stack.len()
2858    }
2859
2860    /// Replace the buffer with `lines` joined by `\n` and set the
2861    /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
2862    /// paths. Marks the editor dirty.
2863    pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
2864        let text = lines.join("\n");
2865        crate::types::BufferEdit::replace_all(&mut self.buffer, &text);
2866        buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
2867        // Bulk replace — supersedes any queued ContentEdits.
2868        self.pending_content_edits.clear();
2869        self.pending_content_reset = true;
2870        self.mark_content_dirty();
2871    }
2872
2873    /// Returns true if the key was consumed by the editor.
2874    /// Replace the char under the cursor with `ch`, `count` times. Matches
2875    /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
2876    /// snapshot taken once at start. Promoted to public surface in 0.5.5
2877    /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
2878    /// re-entering the FSM.
2879    pub fn replace_char_at(&mut self, ch: char, count: usize) {
2880        vim::replace_char(self, ch, count);
2881    }
2882
2883    /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
2884    /// to the `count`-th occurrence of `ch` on the current line, respecting
2885    /// `forward` (direction) and `till` (stop one char before target).
2886    /// Records `last_find` so `;` / `,` repeat work.
2887    ///
2888    /// No-op if the target char isn't on the current line within range.
2889    /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
2890    pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
2891        vim::apply_find_char(self, ch, forward, till, count.max(1));
2892    }
2893
2894    /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
2895    /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
2896    /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
2897    ///
2898    /// Promoted to public surface in 0.5.10 so hjkl-vim's
2899    /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
2900    /// re-entering the engine FSM.
2901    pub fn after_g(&mut self, ch: char, count: usize) {
2902        vim::apply_after_g(self, ch, count);
2903    }
2904
2905    /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
2906    /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
2907    /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
2908    /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
2909    ///
2910    /// Promoted to public surface in 0.5.11 so hjkl-vim's
2911    /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
2912    /// re-entering the engine FSM.
2913    pub fn after_z(&mut self, ch: char, count: usize) {
2914        vim::apply_after_z(self, ch, count);
2915    }
2916
2917    /// Apply an operator over a single-key motion. `op` is the engine `Operator`
2918    /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
2919    /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
2920    /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
2921    /// then calls `apply_op_with_motion`. `total_count` is already the product of
2922    /// the prefix count and any inner count accumulated by the reducer.
2923    ///
2924    /// No-op when `motion_key` does not map to a known motion (engine silently
2925    /// cancels the operator, matching vim's behaviour on unknown motions).
2926    ///
2927    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2928    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
2929    /// re-entering the engine FSM.
2930    pub fn apply_op_motion(
2931        &mut self,
2932        op: crate::vim::Operator,
2933        motion_key: char,
2934        total_count: usize,
2935    ) {
2936        vim::apply_op_motion_key(self, op, motion_key, total_count);
2937    }
2938
2939    /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
2940    /// `total_count` is the product of prefix count and inner count.
2941    ///
2942    /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2943    /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
2944    /// re-entering the engine FSM.
2945    pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
2946        vim::apply_op_double(self, op, total_count);
2947    }
2948
2949    /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
2950    /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
2951    /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
2952    /// updates `last_change` when `op` is Change (for dot-repeat).
2953    ///
2954    /// `total_count` is the product of prefix count and any inner count
2955    /// accumulated by the reducer — already folded at transition time.
2956    ///
2957    /// Promoted to the public surface in 0.5.14 so the hjkl-vim
2958    /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
2959    /// re-entering the engine FSM. `handle_op_find_target` (used by the
2960    /// chord-init op path) delegates here to avoid logic duplication.
2961    pub fn apply_op_find(
2962        &mut self,
2963        op: crate::vim::Operator,
2964        ch: char,
2965        forward: bool,
2966        till: bool,
2967        total_count: usize,
2968    ) {
2969        vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
2970    }
2971
2972    /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
2973    /// Maps `ch` to a `TextObject` per the standard vim table, calls
2974    /// `apply_op_with_text_object`, and records `last_change` when `op` is
2975    /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
2976    /// matching the engine FSM's behaviour on unrecognised text-object chars.
2977    ///
2978    /// `total_count` is accepted for API symmetry with `apply_op_motion` /
2979    /// `apply_op_find` but is currently unused — text objects don't repeat in
2980    /// vim's current grammar. Kept for future-proofing.
2981    ///
2982    /// Promoted to the public surface in 0.5.15 so the hjkl-vim
2983    /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
2984    /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
2985    /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
2986    /// duplication.
2987    pub fn apply_op_text_obj(
2988        &mut self,
2989        op: crate::vim::Operator,
2990        ch: char,
2991        inner: bool,
2992        total_count: usize,
2993    ) {
2994        vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
2995    }
2996
2997    /// Apply an operator over a g-chord motion or case-op linewise form
2998    /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
2999    ///
3000    /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
3001    ///   letter (`U`/`u`/`~`), executes the line op (linewise form).
3002    /// - Otherwise maps `ch` to a motion:
3003    ///   - `'g'` → `Motion::FileTop` (gg)
3004    ///   - `'e'` → `Motion::WordEndBack` (ge)
3005    ///   - `'E'` → `Motion::BigWordEndBack` (gE)
3006    ///   - `'j'` → `Motion::ScreenDown` (gj)
3007    ///   - `'k'` → `Motion::ScreenUp` (gk)
3008    ///   - unknown → no-op (silently ignored, matching engine FSM behaviour)
3009    /// - Updates `last_change` for dot-repeat when `op` is a change operator.
3010    ///
3011    /// `total_count` is the already-folded product of prefix and inner counts.
3012    ///
3013    /// Promoted to the public surface in 0.5.16 so the hjkl-vim
3014    /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
3015    /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
3016    /// delegates to the shared `apply_op_g_inner` helper to avoid logic
3017    /// duplication.
3018    pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
3019        vim::apply_op_g_inner(self, op, ch, total_count);
3020    }
3021
3022    // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
3023
3024    /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
3025    /// row and the motion's target row. Used by the app layer to compute the
3026    /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
3027    /// format job.
3028    ///
3029    /// Returns `None` when `motion_key` does not map to a known motion (same
3030    /// condition that makes `apply_op_motion` a no-op).
3031    ///
3032    /// The cursor is restored to its original position after the probe —
3033    /// the buffer content is not touched.
3034    pub fn range_for_op_motion(
3035        &mut self,
3036        motion_key: char,
3037        total_count: usize,
3038    ) -> Option<(usize, usize)> {
3039        let start = self.cursor();
3040        // Reuse the same logic as apply_op_motion_key but only read the
3041        // target row — we parse the motion, apply it to move the cursor,
3042        // then immediately restore.
3043        let input = crate::input::Input {
3044            key: crate::input::Key::Char(motion_key),
3045            ctrl: false,
3046            alt: false,
3047            shift: false,
3048        };
3049        let motion = vim::parse_motion(&input)?;
3050        // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
3051        let motion = match motion {
3052            vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
3053                Some((ch, forward, till)) => vim::Motion::Find {
3054                    ch,
3055                    forward: if reverse { !forward } else { forward },
3056                    till,
3057                },
3058                None => return None,
3059            },
3060            m => m,
3061        };
3062        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3063        let end = self.cursor();
3064        // Restore cursor.
3065        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3066        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3067        Some((r0, r1))
3068    }
3069
3070    /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
3071    /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
3072    /// linewise forms that don't map to a row range.
3073    ///
3074    /// The cursor is restored after the probe.
3075    pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
3076        let start = self.cursor();
3077        let motion = match ch {
3078            'g' => vim::Motion::FileTop,
3079            'e' => vim::Motion::WordEndBack,
3080            'E' => vim::Motion::BigWordEndBack,
3081            'j' => vim::Motion::ScreenDown,
3082            'k' => vim::Motion::ScreenUp,
3083            _ => return None,
3084        };
3085        vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
3086        let end = self.cursor();
3087        buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
3088        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3089        Some((r0, r1))
3090    }
3091
3092    /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
3093    /// matched region. Returns `None` when `ch` is not a known text-object
3094    /// kind or the text object could not be resolved (e.g. no enclosing bracket).
3095    ///
3096    /// The buffer is not mutated.
3097    pub fn range_for_op_text_obj(
3098        &self,
3099        ch: char,
3100        inner: bool,
3101        _total_count: usize,
3102    ) -> Option<(usize, usize)> {
3103        let obj = match ch {
3104            'w' => vim::TextObject::Word { big: false },
3105            'W' => vim::TextObject::Word { big: true },
3106            '"' | '\'' | '`' => vim::TextObject::Quote(ch),
3107            '(' | ')' | 'b' => vim::TextObject::Bracket('('),
3108            '[' | ']' => vim::TextObject::Bracket('['),
3109            '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
3110            '<' | '>' => vim::TextObject::Bracket('<'),
3111            'p' => vim::TextObject::Paragraph,
3112            't' => vim::TextObject::XmlTag,
3113            's' => vim::TextObject::Sentence,
3114            _ => return None,
3115        };
3116        let (start, end, _kind) = vim::text_object_range(self, obj, inner)?;
3117        let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3118        Some((r0, r1))
3119    }
3120
3121    // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
3122    //
3123    // These do not consume input — the caller (hjkl-vim's visual-mode operator
3124    // path, chunk 4e) has already resolved the range from the visual selection
3125    // before calling in. Normal-mode op dispatch continues to use
3126    // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
3127
3128    /// Delete the region `[start, end)` and stash the removed text in
3129    /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
3130    /// select named registers.
3131    ///
3132    /// Pure range-mutation primitive — does not consume input. Called by
3133    /// hjkl-vim's visual-mode operator path which has already resolved the range
3134    /// from the visual selection.
3135    ///
3136    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3137    /// grammar migration (kryptic-sh/hjkl#70).
3138    pub fn delete_range(
3139        &mut self,
3140        start: (usize, usize),
3141        end: (usize, usize),
3142        kind: crate::vim::RangeKind,
3143        register: char,
3144    ) {
3145        vim::delete_range_bridge(self, start, end, kind, register);
3146    }
3147
3148    /// Yank (copy) the region `[start, end)` into `register` without mutating
3149    /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
3150    /// register; `'a'`–`'z'` select named registers.
3151    ///
3152    /// Pure range-mutation primitive — does not consume input. Called by
3153    /// hjkl-vim's visual-mode operator path which has already resolved the range
3154    /// from the visual selection.
3155    ///
3156    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3157    /// grammar migration (kryptic-sh/hjkl#70).
3158    pub fn yank_range(
3159        &mut self,
3160        start: (usize, usize),
3161        end: (usize, usize),
3162        kind: crate::vim::RangeKind,
3163        register: char,
3164    ) {
3165        vim::yank_range_bridge(self, start, end, kind, register);
3166    }
3167
3168    /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
3169    /// operator). The deleted text is stashed in `register`. On return the
3170    /// editor is in Insert mode; the caller must not issue further normal-mode
3171    /// ops until the insert session ends.
3172    ///
3173    /// Pure range-mutation primitive — does not consume input. Called by
3174    /// hjkl-vim's visual-mode operator path which has already resolved the range
3175    /// from the visual selection.
3176    ///
3177    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3178    /// grammar migration (kryptic-sh/hjkl#70).
3179    pub fn change_range(
3180        &mut self,
3181        start: (usize, usize),
3182        end: (usize, usize),
3183        kind: crate::vim::RangeKind,
3184        register: char,
3185    ) {
3186        vim::change_range_bridge(self, start, end, kind, register);
3187    }
3188
3189    /// Indent (`count > 0`) or outdent (`count < 0`) the row span
3190    /// `[start.0, end.0]`. Column components are ignored — indent is always
3191    /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
3192    /// this call; pass `0` to use the current editor setting. `count == 0` is a
3193    /// no-op.
3194    ///
3195    /// Pure range-mutation primitive — does not consume input. Called by
3196    /// hjkl-vim's visual-mode operator path which has already resolved the range
3197    /// from the visual selection.
3198    ///
3199    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3200    /// grammar migration (kryptic-sh/hjkl#70).
3201    pub fn indent_range(
3202        &mut self,
3203        start: (usize, usize),
3204        end: (usize, usize),
3205        count: i32,
3206        shiftwidth: u32,
3207    ) {
3208        vim::indent_range_bridge(self, start, end, count, shiftwidth);
3209    }
3210
3211    /// Apply a case transformation (`Operator::Uppercase` /
3212    /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3213    /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3214    /// Yanks registers are left untouched — vim's case operators do not write
3215    /// to registers.
3216    ///
3217    /// Pure range-mutation primitive — does not consume input. Called by
3218    /// hjkl-vim's visual-mode operator path which has already resolved the range
3219    /// from the visual selection.
3220    ///
3221    /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3222    /// grammar migration (kryptic-sh/hjkl#70).
3223    pub fn case_range(
3224        &mut self,
3225        start: (usize, usize),
3226        end: (usize, usize),
3227        kind: crate::vim::RangeKind,
3228        op: crate::vim::Operator,
3229    ) {
3230        vim::case_range_bridge(self, start, end, kind, op);
3231    }
3232
3233    // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3234    //
3235    // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3236    // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3237    // Ragged-edge handling (short lines not reaching `right_col`) matches the
3238    // engine FSM's `apply_block_operator` path — short lines lose only the
3239    // chars that exist.
3240    //
3241    // `register` is the target register; `'"'` selects the unnamed register.
3242
3243    /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3244    /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3245    /// bounds at the visual (display) column level. Ragged-edge handling
3246    /// matches engine FSM's VisualBlock op behavior — short lines that don't
3247    /// reach `right_col` lose only the chars that exist.
3248    ///
3249    /// `register` honors the user's pending register selection.
3250    ///
3251    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3252    pub fn delete_block(
3253        &mut self,
3254        top_row: usize,
3255        bot_row: usize,
3256        left_col: usize,
3257        right_col: usize,
3258        register: char,
3259    ) {
3260        vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3261    }
3262
3263    /// Yank a rectangular VisualBlock selection into `register` without
3264    /// mutating the buffer. `'"'` selects the unnamed register.
3265    ///
3266    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3267    pub fn yank_block(
3268        &mut self,
3269        top_row: usize,
3270        bot_row: usize,
3271        left_col: usize,
3272        right_col: usize,
3273        register: char,
3274    ) {
3275        vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3276    }
3277
3278    /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3279    /// operator). The deleted text is stashed in `register`. Mode is Insert
3280    /// on return; the caller must not issue further normal-mode ops until the
3281    /// insert session ends.
3282    ///
3283    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3284    pub fn change_block(
3285        &mut self,
3286        top_row: usize,
3287        bot_row: usize,
3288        left_col: usize,
3289        right_col: usize,
3290        register: char,
3291    ) {
3292        vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3293    }
3294
3295    /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3296    /// Column bounds are ignored — vim's block indent is always linewise.
3297    /// `count == 0` is a no-op.
3298    ///
3299    /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3300    pub fn indent_block(
3301        &mut self,
3302        top_row: usize,
3303        bot_row: usize,
3304        _left_col: usize,
3305        _right_col: usize,
3306        count: i32,
3307    ) {
3308        vim::indent_block_bridge(self, top_row, bot_row, count);
3309    }
3310
3311    /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3312    /// Column components are ignored — auto-indent is always linewise.
3313    ///
3314    /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3315    /// row 0 to compute the correct depth at `start.0`, then for each line in
3316    /// the target range strips existing leading whitespace and prepends
3317    /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3318    /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3319    /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3320    /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3321    ///
3322    /// After the operation the cursor lands on the first non-whitespace
3323    /// character of `start_row` (vim parity for `==`).
3324    ///
3325    /// **v1 limitation**: the bracket scan does not detect brackets inside
3326    /// string literals or comments. Code such as `let s = "{";` will increment
3327    /// the depth counter even though the brace is not a structural opener.
3328    /// Tree-sitter / LSP indentation is deferred to a follow-up.
3329    pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3330        vim::auto_indent_range_bridge(self, start, end);
3331    }
3332
3333    /// Drain the row range set by the most recent auto-indent operation.
3334    ///
3335    /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3336    /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3337    /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3338    /// uses this to arm a brief visual flash over the reindented rows.
3339    pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3340        self.last_indent_range.take()
3341    }
3342
3343    // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
3344    //
3345    // Pure functions — no cursor mutation, no mode change, no register write.
3346    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3347    // the existing `word_text_object` private resolver in vim.rs.
3348    //
3349    // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
3350    // before invoking a range-mutation primitive (`delete_range`, etc.).
3351    //
3352    // Return value: `Some((start, end))` where both positions are `(row, col)`
3353    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3354    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3355    // Returns `None` when the cursor is on an empty line or the resolver cannot
3356    // find a word boundary.
3357
3358    /// Resolve the range of `iw` (inner word) at the current cursor position.
3359    ///
3360    /// An inner word is the contiguous run of keyword characters (or punctuation
3361    /// characters if the cursor is on punctuation) under the cursor, without any
3362    /// surrounding whitespace. Whitespace-only positions return `None`.
3363    ///
3364    /// Pure function — does not move the cursor or change any editor state.
3365    /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
3366    /// invoking a range-mutation primitive (`delete_range`, etc.).
3367    ///
3368    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3369    /// migration (kryptic-sh/hjkl#70).
3370    pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
3371        vim::text_object_inner_word_bridge(self)
3372    }
3373
3374    /// Resolve the range of `aw` (around word) at the current cursor position.
3375    ///
3376    /// Like `iw` but extends the range to include trailing whitespace after the
3377    /// word. If no trailing whitespace exists, leading whitespace before the word
3378    /// is absorbed instead (vim `:help text-objects` behaviour).
3379    ///
3380    /// Pure function — does not move the cursor or change any editor state.
3381    ///
3382    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3383    /// migration (kryptic-sh/hjkl#70).
3384    pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
3385        vim::text_object_around_word_bridge(self)
3386    }
3387
3388    /// Resolve the range of `iW` (inner WORD) at the current cursor position.
3389    ///
3390    /// A WORD is any contiguous run of non-whitespace characters — punctuation
3391    /// is not treated as a word boundary. Returns the span of the WORD under the
3392    /// cursor, without surrounding whitespace.
3393    ///
3394    /// Pure function — does not move the cursor or change any editor state.
3395    ///
3396    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3397    /// migration (kryptic-sh/hjkl#70).
3398    pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3399        vim::text_object_inner_big_word_bridge(self)
3400    }
3401
3402    /// Resolve the range of `aW` (around WORD) at the current cursor position.
3403    ///
3404    /// Like `iW` but extends the range to include trailing whitespace after the
3405    /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
3406    /// is absorbed instead.
3407    ///
3408    /// Pure function — does not move the cursor or change any editor state.
3409    ///
3410    /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3411    /// migration (kryptic-sh/hjkl#70).
3412    pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3413        vim::text_object_around_big_word_bridge(self)
3414    }
3415
3416    // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
3417    //
3418    // Pure functions — no cursor mutation, no mode change, no register write.
3419    // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3420    // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
3421    // in vim.rs.
3422    //
3423    // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
3424    // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
3425    // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
3426    // hjkl-vim grammar layer normalises close→open before calling these methods.
3427    //
3428    // Return value: `Some((start, end))` where both positions are `(row, col)`
3429    // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3430    // on), matching the convention used by `delete_range` / `yank_range` / etc.
3431    // `bracket_text_object` internally distinguishes Linewise vs Exclusive
3432    // ranges for multi-line pairs; that tag is stripped here — callers receive
3433    // the same flat shape as all other text-object resolvers.
3434
3435    /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
3436    ///
3437    /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
3438    /// cursor's line contains fewer than two occurrences of `quote`, or when no
3439    /// matching pair can be found around or ahead of the cursor.
3440    ///
3441    /// Inner range excludes the quote characters themselves.
3442    ///
3443    /// Pure function — no cursor mutation.
3444    ///
3445    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3446    /// migration (kryptic-sh/hjkl#70).
3447    pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
3448        vim::text_object_inner_quote_bridge(self, quote)
3449    }
3450
3451    /// Resolve the range of `a<quote>` (around quote) at the cursor position.
3452    ///
3453    /// Like `i<quote>` but includes the quote characters themselves plus
3454    /// surrounding whitespace on one side: trailing whitespace after the closing
3455    /// quote if any exists; otherwise leading whitespace before the opening
3456    /// quote. This matches vim `:help text-objects` behaviour.
3457    ///
3458    /// Pure function — no cursor mutation.
3459    ///
3460    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3461    /// migration (kryptic-sh/hjkl#70).
3462    pub fn text_object_around_quote(
3463        &self,
3464        quote: char,
3465    ) -> Option<((usize, usize), (usize, usize))> {
3466        vim::text_object_around_quote_bridge(self, quote)
3467    }
3468
3469    /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
3470    ///
3471    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
3472    /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
3473    /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
3474    /// calling this method. Returns `None` when no enclosing pair is found.
3475    ///
3476    /// The cursor may be anywhere inside the pair or on a bracket character
3477    /// itself. When not inside any pair the resolver falls back to a forward
3478    /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
3479    ///
3480    /// Inner range excludes the bracket characters. Multi-line pairs are
3481    /// supported; the returned range spans the full content between the
3482    /// brackets.
3483    ///
3484    /// Pure function — no cursor mutation.
3485    ///
3486    /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
3487    /// handled here.
3488    ///
3489    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3490    /// migration (kryptic-sh/hjkl#70).
3491    pub fn text_object_inner_bracket(
3492        &self,
3493        open: char,
3494    ) -> Option<((usize, usize), (usize, usize))> {
3495        vim::text_object_inner_bracket_bridge(self, open)
3496    }
3497
3498    /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
3499    ///
3500    /// Like `i<bracket>` but includes the bracket characters themselves.
3501    /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
3502    ///
3503    /// Pure function — no cursor mutation.
3504    ///
3505    /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
3506    ///
3507    /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3508    /// migration (kryptic-sh/hjkl#70).
3509    pub fn text_object_around_bracket(
3510        &self,
3511        open: char,
3512    ) -> Option<((usize, usize), (usize, usize))> {
3513        vim::text_object_around_bracket_bridge(self, open)
3514    }
3515
3516    // ── Sentence text objects (is / as) ───────────────────────────────────
3517
3518    /// Resolve `is` (inner sentence) at the cursor position.
3519    ///
3520    /// Returns the range of the current sentence, excluding trailing
3521    /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
3522    /// `?` / `!` followed by whitespace or end-of-paragraph).
3523    ///
3524    /// Pure function — no cursor mutation.
3525    ///
3526    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3527    /// grammar migration (kryptic-sh/hjkl#70).
3528    pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3529        vim::text_object_inner_sentence_bridge(self)
3530    }
3531
3532    /// Resolve `as` (around sentence) at the cursor position.
3533    ///
3534    /// Like `is` but includes trailing whitespace after the sentence
3535    /// terminator.
3536    ///
3537    /// Pure function — no cursor mutation.
3538    ///
3539    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3540    /// grammar migration (kryptic-sh/hjkl#70).
3541    pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3542        vim::text_object_around_sentence_bridge(self)
3543    }
3544
3545    // ── Paragraph text objects (ip / ap) ──────────────────────────────────
3546
3547    /// Resolve `ip` (inner paragraph) at the cursor position.
3548    ///
3549    /// A paragraph is a block of non-blank lines bounded by blank lines or
3550    /// buffer edges. Returns `None` when the cursor is on a blank line.
3551    ///
3552    /// Pure function — no cursor mutation.
3553    ///
3554    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3555    /// grammar migration (kryptic-sh/hjkl#70).
3556    pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3557        vim::text_object_inner_paragraph_bridge(self)
3558    }
3559
3560    /// Resolve `ap` (around paragraph) at the cursor position.
3561    ///
3562    /// Like `ip` but includes one trailing blank line when present.
3563    ///
3564    /// Pure function — no cursor mutation.
3565    ///
3566    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3567    /// grammar migration (kryptic-sh/hjkl#70).
3568    pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3569        vim::text_object_around_paragraph_bridge(self)
3570    }
3571
3572    // ── Tag text objects (it / at) ────────────────────────────────────────
3573
3574    /// Resolve `it` (inner tag) at the cursor position.
3575    ///
3576    /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
3577    /// inner content between the open and close tags (excluding the tags
3578    /// themselves).
3579    ///
3580    /// Pure function — no cursor mutation.
3581    ///
3582    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3583    /// grammar migration (kryptic-sh/hjkl#70).
3584    pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3585        vim::text_object_inner_tag_bridge(self)
3586    }
3587
3588    /// Resolve `at` (around tag) at the cursor position.
3589    ///
3590    /// Like `it` but includes the open and close tag delimiters themselves.
3591    ///
3592    /// Pure function — no cursor mutation.
3593    ///
3594    /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3595    /// grammar migration (kryptic-sh/hjkl#70).
3596    pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3597        vim::text_object_around_tag_bridge(self)
3598    }
3599
3600    /// Execute a named cursor motion `kind` repeated `count` times.
3601    ///
3602    /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
3603    /// motion primitives, bypassing the engine FSM. Identical cursor semantics
3604    /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
3605    /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
3606    /// used by the FSM arms.
3607    ///
3608    /// Introduced in 0.6.1 as the host entry point for Phase 3a of
3609    /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
3610    /// calls this method rather than re-entering the engine FSM.
3611    ///
3612    /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
3613    /// intact for macro-replay coverage (macros re-feed raw keys through the
3614    /// FSM). This method is the keymap / controller path only.
3615    pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
3616        vim::apply_motion_kind(self, kind, count);
3617    }
3618
3619    /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
3620    /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
3621    /// chars are silently ignored (no-op), matching the engine FSM's
3622    /// `handle_select_register` behaviour.
3623    ///
3624    /// Promoted to the public surface in 0.5.17 so the hjkl-vim
3625    /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
3626    /// without re-entering the engine FSM. `handle_select_register` (engine FSM
3627    /// path for macro-replay / defensive coverage) delegates here to avoid
3628    /// logic duplication.
3629    pub fn set_pending_register(&mut self, reg: char) {
3630        if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
3631            self.vim.pending_register = Some(reg);
3632        }
3633        // Invalid chars silently no-op (matches engine FSM behavior).
3634    }
3635
3636    /// Record a mark named `ch` at the current cursor position.
3637    ///
3638    /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
3639    /// rules). Invalid chars are silently ignored (no-op), matching the engine
3640    /// FSM's `handle_set_mark` behaviour.
3641    ///
3642    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3643    /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
3644    /// without re-entering the engine FSM. `handle_set_mark` delegates here.
3645    pub fn set_mark_at_cursor(&mut self, ch: char) {
3646        vim::set_mark_at_cursor(self, ch);
3647    }
3648
3649    /// `.` dot-repeat: replay the last buffered change at the current cursor.
3650    /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
3651    /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
3652    ///
3653    /// Storage of `LastChange` stays inside engine for now; Phase 5c of
3654    /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
3655    /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
3656    /// FSM `.` arm stays for macro-replay defensive coverage.
3657    pub fn replay_last_change(&mut self, count: usize) {
3658        vim::replay_last_change(self, count);
3659    }
3660
3661    /// Jump to the mark named `ch`, linewise (row only; col snaps to first
3662    /// non-blank). Pushes the pre-jump position onto the jumplist if the
3663    /// cursor actually moved.
3664    ///
3665    /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
3666    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3667    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3668    /// are silently ignored (no-op), matching the engine FSM's
3669    /// `handle_goto_mark` behaviour.
3670    ///
3671    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3672    /// `PendingState::GotoMarkLine` reducer can dispatch
3673    /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
3674    pub fn goto_mark_line(&mut self, ch: char) {
3675        vim::goto_mark(self, ch, true);
3676    }
3677
3678    /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
3679    /// pre-jump position onto the jumplist if the cursor actually moved.
3680    ///
3681    /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
3682    /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3683    /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3684    /// are silently ignored (no-op), matching the engine FSM's
3685    /// `handle_goto_mark` behaviour.
3686    ///
3687    /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3688    /// `PendingState::GotoMarkChar` reducer can dispatch
3689    /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
3690    pub fn goto_mark_char(&mut self, ch: char) {
3691        vim::goto_mark(self, ch, false);
3692    }
3693
3694    // ── Macro controller API (Phase 5b) ──────────────────────────────────────
3695
3696    /// Begin recording keystrokes into register `reg`. The caller (app) is
3697    /// responsible for stopping the recording via `stop_macro_record` when the
3698    /// user presses bare `q`.
3699    ///
3700    /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
3701    ///   recording by pre-seeding `recording_keys` with the decoded text of the
3702    ///   matching lowercase register, matching vim's capital-register append
3703    ///   semantics.
3704    /// - Lowercase `reg` clears `recording_keys` (fresh recording).
3705    /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
3706    ///
3707    /// Promoted to the public surface in Phase 5b so the app's
3708    /// `route_chord_key` can start a recording without re-entering the engine
3709    /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
3710    /// defensive coverage) continues to use the same logic via delegation.
3711    pub fn start_macro_record(&mut self, reg: char) {
3712        if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
3713            return;
3714        }
3715        self.vim.recording_macro = Some(reg);
3716        if reg.is_ascii_uppercase() {
3717            // Seed recording_keys with the existing lowercase register's text
3718            // decoded back to inputs so capital-register append continues from
3719            // where the previous recording left off.
3720            let lower = reg.to_ascii_lowercase();
3721            let text = self
3722                .registers
3723                .read(lower)
3724                .map(|s| s.text.clone())
3725                .unwrap_or_default();
3726            self.vim.recording_keys = crate::input::decode_macro(&text);
3727        } else {
3728            self.vim.recording_keys.clear();
3729        }
3730    }
3731
3732    /// Finalize the active recording: encode `recording_keys` as text and write
3733    /// to the matching (lowercase) named register. Clears both `recording_macro`
3734    /// and `recording_keys`. No-ops if no recording is active.
3735    ///
3736    /// Promoted to the public surface in Phase 5b so the app's `QChord` action
3737    /// can stop a recording when the user presses bare `q` without re-entering
3738    /// the engine FSM.
3739    pub fn stop_macro_record(&mut self) {
3740        let Some(reg) = self.vim.recording_macro.take() else {
3741            return;
3742        };
3743        let keys = std::mem::take(&mut self.vim.recording_keys);
3744        let text = crate::input::encode_macro(&keys);
3745        self.set_named_register_text(reg.to_ascii_lowercase(), text);
3746    }
3747
3748    /// Returns `true` while a `q{reg}` recording is in progress.
3749    /// Hosts use this to show a "recording @r" status indicator and to decide
3750    /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
3751    /// chord.
3752    pub fn is_recording_macro(&self) -> bool {
3753        self.vim.recording_macro.is_some()
3754    }
3755
3756    /// Returns `true` while a macro is being replayed. The app sets this flag
3757    /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
3758    /// re-feed loop so the recorder hook can skip double-capture.
3759    pub fn is_replaying_macro(&self) -> bool {
3760        self.vim.replaying_macro
3761    }
3762
3763    /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
3764    /// prepare for replay, returning the inputs the app should re-feed through
3765    /// `route_chord_key`.
3766    ///
3767    /// Resolves `reg`:
3768    /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
3769    /// - Any other char → lowercase it, read the register, decode.
3770    ///
3771    /// Side-effects:
3772    /// - Sets `vim.last_macro` to the resolved register.
3773    /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
3774    ///   replay. The app calls `end_macro_replay` after the loop finishes.
3775    ///
3776    /// Returns an empty vec (and no side-effects for `'@'`) if the register is
3777    /// unset or empty.
3778    pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
3779        let resolved = if reg == '@' {
3780            match self.vim.last_macro {
3781                Some(r) => r,
3782                None => return vec![],
3783            }
3784        } else {
3785            reg.to_ascii_lowercase()
3786        };
3787        let text = match self.registers.read(resolved) {
3788            Some(slot) if !slot.text.is_empty() => slot.text.clone(),
3789            _ => return vec![],
3790        };
3791        let keys = crate::input::decode_macro(&text);
3792        self.vim.last_macro = Some(resolved);
3793        self.vim.replaying_macro = true;
3794        // Multiply by count (minimum 1).
3795        keys.repeat(count.max(1))
3796    }
3797
3798    /// Clear the `replaying_macro` flag. Called by the app after the
3799    /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
3800    pub fn end_macro_replay(&mut self) {
3801        self.vim.replaying_macro = false;
3802    }
3803
3804    /// Append `input` to the active recording (`recording_keys`) if and only
3805    /// if a recording is in progress AND we are not currently replaying.
3806    /// Called by the app's `route_chord_key` recorder hook so that user
3807    /// keystrokes captured through the app-level chord path are recorded
3808    /// (rather than relying solely on the engine FSM's in-step hook).
3809    pub fn record_input(&mut self, input: crate::input::Input) {
3810        if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
3811            self.vim.recording_keys.push(input);
3812        }
3813    }
3814
3815    // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
3816    //
3817    // Each method is the publicly callable form of one insert-mode action.
3818    // All logic lives in the corresponding `vim::*_bridge` free function;
3819    // these methods are thin delegators so the public surface stays on `Editor`.
3820    //
3821    // Invariants (enforced by the bridge fns):
3822    //   - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
3823    //   - Navigation keys call `break_undo_group_in_insert` when the FSM did.
3824    //   - `push_buffer_cursor_to_textarea` is called after every mutation
3825    //     (currently a no-op, kept for migration hygiene).
3826
3827    /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
3828    /// the cursor instead of inserting; at end-of-line, always appends. With
3829    /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
3830    /// dedent on an otherwise-whitespace line.
3831    ///
3832    /// Callers must ensure the editor is in Insert or Replace mode before
3833    /// calling this method.
3834    pub fn insert_char(&mut self, ch: char) {
3835        let mutated = vim::insert_char_bridge(self, ch);
3836        if mutated {
3837            self.mark_content_dirty();
3838            let (row, _) = self.cursor();
3839            self.vim.widen_insert_row(row);
3840        }
3841    }
3842
3843    /// Insert a newline at the cursor, applying autoindent / smartindent to
3844    /// prefix the new line with the appropriate leading whitespace.
3845    ///
3846    /// Callers must ensure the editor is in Insert mode before calling.
3847    pub fn insert_newline(&mut self) {
3848        let mutated = vim::insert_newline_bridge(self);
3849        if mutated {
3850            self.mark_content_dirty();
3851            let (row, _) = self.cursor();
3852            self.vim.widen_insert_row(row);
3853        }
3854    }
3855
3856    /// Insert a tab character (or spaces up to the next `softtabstop` boundary
3857    /// when `expandtab` is set).
3858    ///
3859    /// Callers must ensure the editor is in Insert mode before calling.
3860    pub fn insert_tab(&mut self) {
3861        let mutated = vim::insert_tab_bridge(self);
3862        if mutated {
3863            self.mark_content_dirty();
3864            let (row, _) = self.cursor();
3865            self.vim.widen_insert_row(row);
3866        }
3867    }
3868
3869    /// Delete the character before the cursor (Backspace). With `softtabstop`
3870    /// active, deletes the entire soft-tab run at an aligned boundary. Joins
3871    /// with the previous line when at column 0.
3872    ///
3873    /// Callers must ensure the editor is in Insert mode before calling.
3874    pub fn insert_backspace(&mut self) {
3875        let mutated = vim::insert_backspace_bridge(self);
3876        if mutated {
3877            self.mark_content_dirty();
3878            let (row, _) = self.cursor();
3879            self.vim.widen_insert_row(row);
3880        }
3881    }
3882
3883    /// Delete the character under the cursor (Delete key). Joins with the
3884    /// next line when at end-of-line.
3885    ///
3886    /// Callers must ensure the editor is in Insert mode before calling.
3887    pub fn insert_delete(&mut self) {
3888        let mutated = vim::insert_delete_bridge(self);
3889        if mutated {
3890            self.mark_content_dirty();
3891            let (row, _) = self.cursor();
3892            self.vim.widen_insert_row(row);
3893        }
3894    }
3895
3896    /// Move the cursor one step in `dir` (arrow key), breaking the undo group
3897    /// per `undo_break_on_motion`.
3898    ///
3899    /// Callers must ensure the editor is in Insert mode before calling.
3900    pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
3901        vim::insert_arrow_bridge(self, dir);
3902        let (row, _) = self.cursor();
3903        self.vim.widen_insert_row(row);
3904    }
3905
3906    /// Move the cursor to the start of the current line (Home key), breaking
3907    /// the undo group.
3908    ///
3909    /// Callers must ensure the editor is in Insert mode before calling.
3910    pub fn insert_home(&mut self) {
3911        vim::insert_home_bridge(self);
3912        let (row, _) = self.cursor();
3913        self.vim.widen_insert_row(row);
3914    }
3915
3916    /// Move the cursor to the end of the current line (End key), breaking the
3917    /// undo group.
3918    ///
3919    /// Callers must ensure the editor is in Insert mode before calling.
3920    pub fn insert_end(&mut self) {
3921        vim::insert_end_bridge(self);
3922        let (row, _) = self.cursor();
3923        self.vim.widen_insert_row(row);
3924    }
3925
3926    /// Scroll up one full viewport height (PageUp), moving the cursor with it.
3927    /// `viewport_h` is the current viewport height in rows; pass
3928    /// `self.viewport_height_value()` if the stored value is current.
3929    ///
3930    /// Callers must ensure the editor is in Insert mode before calling.
3931    pub fn insert_pageup(&mut self, viewport_h: u16) {
3932        vim::insert_pageup_bridge(self, viewport_h);
3933        let (row, _) = self.cursor();
3934        self.vim.widen_insert_row(row);
3935    }
3936
3937    /// Scroll down one full viewport height (PageDown), moving the cursor with
3938    /// it. `viewport_h` is the current viewport height in rows.
3939    ///
3940    /// Callers must ensure the editor is in Insert mode before calling.
3941    pub fn insert_pagedown(&mut self, viewport_h: u16) {
3942        vim::insert_pagedown_bridge(self, viewport_h);
3943        let (row, _) = self.cursor();
3944        self.vim.widen_insert_row(row);
3945    }
3946
3947    /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
3948    /// At column 0, joins with the previous line (vim `b`-motion semantics).
3949    ///
3950    /// Callers must ensure the editor is in Insert mode before calling.
3951    pub fn insert_ctrl_w(&mut self) {
3952        let mutated = vim::insert_ctrl_w_bridge(self);
3953        if mutated {
3954            self.mark_content_dirty();
3955            let (row, _) = self.cursor();
3956            self.vim.widen_insert_row(row);
3957        }
3958    }
3959
3960    /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
3961    /// No-op when already at column 0.
3962    ///
3963    /// Callers must ensure the editor is in Insert mode before calling.
3964    pub fn insert_ctrl_u(&mut self) {
3965        let mutated = vim::insert_ctrl_u_bridge(self);
3966        if mutated {
3967            self.mark_content_dirty();
3968            let (row, _) = self.cursor();
3969            self.vim.widen_insert_row(row);
3970        }
3971    }
3972
3973    /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
3974    /// insert mode. Joins with the previous line when at col 0.
3975    ///
3976    /// Callers must ensure the editor is in Insert mode before calling.
3977    pub fn insert_ctrl_h(&mut self) {
3978        let mutated = vim::insert_ctrl_h_bridge(self);
3979        if mutated {
3980            self.mark_content_dirty();
3981            let (row, _) = self.cursor();
3982            self.vim.widen_insert_row(row);
3983        }
3984    }
3985
3986    /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
3987    /// complete normal-mode command, then return to insert automatically.
3988    ///
3989    /// Callers must ensure the editor is in Insert mode before calling.
3990    pub fn insert_ctrl_o_arm(&mut self) {
3991        vim::insert_ctrl_o_bridge(self);
3992    }
3993
3994    /// Arm the register-paste selector (`Ctrl-R`). The next call to
3995    /// `insert_paste_register(reg)` will insert the register contents.
3996    /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
3997    /// the armed state and paste register `c`.
3998    ///
3999    /// Callers must ensure the editor is in Insert mode before calling.
4000    pub fn insert_ctrl_r_arm(&mut self) {
4001        vim::insert_ctrl_r_bridge(self);
4002    }
4003
4004    /// Indent the current line by one `shiftwidth` and shift the cursor right
4005    /// by the same amount (`Ctrl-T`).
4006    ///
4007    /// Callers must ensure the editor is in Insert mode before calling.
4008    pub fn insert_ctrl_t(&mut self) {
4009        let mutated = vim::insert_ctrl_t_bridge(self);
4010        if mutated {
4011            self.mark_content_dirty();
4012            let (row, _) = self.cursor();
4013            self.vim.widen_insert_row(row);
4014        }
4015    }
4016
4017    /// Outdent the current line by up to one `shiftwidth` and shift the cursor
4018    /// left by the amount stripped (`Ctrl-D`).
4019    ///
4020    /// Callers must ensure the editor is in Insert mode before calling.
4021    pub fn insert_ctrl_d(&mut self) {
4022        let mutated = vim::insert_ctrl_d_bridge(self);
4023        if mutated {
4024            self.mark_content_dirty();
4025            let (row, _) = self.cursor();
4026            self.vim.widen_insert_row(row);
4027        }
4028    }
4029
4030    /// Paste the contents of register `reg` at the cursor (the commit arm of
4031    /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
4032    ///
4033    /// Callers must ensure the editor is in Insert mode before calling.
4034    pub fn insert_paste_register(&mut self, reg: char) {
4035        vim::insert_paste_register_bridge(self, reg);
4036        let (row, _) = self.cursor();
4037        self.vim.widen_insert_row(row);
4038    }
4039
4040    /// Exit insert mode to Normal: finish the insert session, step the cursor
4041    /// one cell left (vim convention on Esc), record the `gi` target position,
4042    /// and update the sticky column.
4043    ///
4044    /// Callers must ensure the editor is in Insert mode before calling.
4045    pub fn leave_insert_to_normal(&mut self) {
4046        vim::leave_insert_to_normal_bridge(self);
4047    }
4048
4049    // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
4050    //
4051    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4052    // `vim.rs` following the same pattern as Phase 6.1. The FSM's
4053    // `handle_normal_only` now calls the same bridges so both paths are
4054    // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
4055
4056    /// `i` — transition to Insert mode at the current cursor position.
4057    /// `count` is stored in the insert session and replayed by dot-repeat
4058    /// as a repeat count on the inserted text.
4059    pub fn enter_insert_i(&mut self, count: usize) {
4060        vim::enter_insert_i_bridge(self, count);
4061    }
4062
4063    /// `I` — move to the first non-blank character on the line, then
4064    /// transition to Insert mode. `count` is stored for dot-repeat.
4065    pub fn enter_insert_shift_i(&mut self, count: usize) {
4066        vim::enter_insert_shift_i_bridge(self, count);
4067    }
4068
4069    /// `a` — advance the cursor one cell past the current position, then
4070    /// transition to Insert mode (append). `count` is stored for dot-repeat.
4071    pub fn enter_insert_a(&mut self, count: usize) {
4072        vim::enter_insert_a_bridge(self, count);
4073    }
4074
4075    /// `A` — move the cursor to the end of the line, then transition to
4076    /// Insert mode (append at end). `count` is stored for dot-repeat.
4077    pub fn enter_insert_shift_a(&mut self, count: usize) {
4078        vim::enter_insert_shift_a_bridge(self, count);
4079    }
4080
4081    /// `o` — open a new line below the current line with smart-indent, then
4082    /// transition to Insert mode. `count` is stored for dot-repeat replay.
4083    pub fn open_line_below(&mut self, count: usize) {
4084        vim::open_line_below_bridge(self, count);
4085    }
4086
4087    /// `O` — open a new line above the current line with smart-indent, then
4088    /// transition to Insert mode. `count` is stored for dot-repeat replay.
4089    pub fn open_line_above(&mut self, count: usize) {
4090        vim::open_line_above_bridge(self, count);
4091    }
4092
4093    /// `R` — enter Replace mode: subsequent typed characters overstrike the
4094    /// cell under the cursor rather than inserting. `count` is for replay.
4095    pub fn enter_replace_mode(&mut self, count: usize) {
4096        vim::enter_replace_mode_bridge(self, count);
4097    }
4098
4099    /// `x` — delete `count` characters forward from the cursor and write them
4100    /// to the unnamed register. No-op on an empty line. Records for `.`.
4101    pub fn delete_char_forward(&mut self, count: usize) {
4102        vim::delete_char_forward_bridge(self, count);
4103    }
4104
4105    /// `X` — delete `count` characters backward from the cursor and write
4106    /// them to the unnamed register. No-op at column 0. Records for `.`.
4107    pub fn delete_char_backward(&mut self, count: usize) {
4108        vim::delete_char_backward_bridge(self, count);
4109    }
4110
4111    /// `s` — substitute `count` characters: delete them (writing to the
4112    /// unnamed register) then enter Insert mode. Equivalent to `cl`.
4113    /// Records as `OpMotion { Change, Right }` for dot-repeat.
4114    pub fn substitute_char(&mut self, count: usize) {
4115        vim::substitute_char_bridge(self, count);
4116    }
4117
4118    /// `S` — substitute the current line: wipe its contents (writing to the
4119    /// unnamed register) then enter Insert mode. Equivalent to `cc`.
4120    /// Records as `LineOp { Change }` for dot-repeat.
4121    pub fn substitute_line(&mut self, count: usize) {
4122        vim::substitute_line_bridge(self, count);
4123    }
4124
4125    /// `D` — delete from the cursor to end-of-line, writing to the unnamed
4126    /// register. The cursor parks on the new last character. Records for `.`.
4127    pub fn delete_to_eol(&mut self) {
4128        vim::delete_to_eol_bridge(self);
4129    }
4130
4131    /// `C` — change from the cursor to end-of-line: delete to EOL then enter
4132    /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
4133    /// (the insert session records `DeleteToEol` on exit, like `c` motions).
4134    pub fn change_to_eol(&mut self) {
4135        vim::change_to_eol_bridge(self);
4136    }
4137
4138    /// `Y` — yank from the cursor to end-of-line into the unnamed register.
4139    /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
4140    pub fn yank_to_eol(&mut self, count: usize) {
4141        vim::yank_to_eol_bridge(self, count);
4142    }
4143
4144    /// `J` — join `count` lines (default 2) onto the current line, inserting
4145    /// a single space between each non-empty pair. Records for dot-repeat.
4146    pub fn join_line(&mut self, count: usize) {
4147        vim::join_line_bridge(self, count);
4148    }
4149
4150    /// `~` — toggle the case of `count` characters from the cursor, advancing
4151    /// right after each toggle. Records `ToggleCase` for dot-repeat.
4152    pub fn toggle_case_at_cursor(&mut self, count: usize) {
4153        vim::toggle_case_at_cursor_bridge(self, count);
4154    }
4155
4156    /// `p` — paste the unnamed register (or the register selected via `"r`)
4157    /// after the cursor. Linewise content opens a new line below; charwise
4158    /// content is inserted inline. Records `Paste { before: false }` for `.`.
4159    pub fn paste_after(&mut self, count: usize) {
4160        vim::paste_after_bridge(self, count);
4161    }
4162
4163    /// `P` — paste the unnamed register (or the `"r` register) before the
4164    /// cursor. Linewise content opens a new line above; charwise is inline.
4165    /// Records `Paste { before: true }` for dot-repeat.
4166    pub fn paste_before(&mut self, count: usize) {
4167        vim::paste_before_bridge(self, count);
4168    }
4169
4170    /// `<C-o>` — jump back `count` entries in the jumplist, saving the
4171    /// current position on the forward stack so `<C-i>` can return.
4172    pub fn jump_back(&mut self, count: usize) {
4173        vim::jump_back_bridge(self, count);
4174    }
4175
4176    /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
4177    /// saving the current position on the backward stack.
4178    pub fn jump_forward(&mut self, count: usize) {
4179        vim::jump_forward_bridge(self, count);
4180    }
4181
4182    /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
4183    /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
4184    /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
4185    pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
4186        vim::scroll_full_page_bridge(self, dir, count);
4187    }
4188
4189    /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
4190    /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
4191    pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
4192        vim::scroll_half_page_bridge(self, dir, count);
4193    }
4194
4195    /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
4196    /// the cursor (cursor is clamped to the new visible region if necessary).
4197    /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
4198    pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
4199        vim::scroll_line_bridge(self, dir, count);
4200    }
4201
4202    /// `n` — repeat the last `/` or `?` search `count` times in its original
4203    /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
4204    pub fn search_repeat(&mut self, forward: bool, count: usize) {
4205        vim::search_repeat_bridge(self, forward, count);
4206    }
4207
4208    /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
4209    /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
4210    /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
4211    pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
4212        vim::word_search_bridge(self, forward, whole_word, count);
4213    }
4214
4215    // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
4216    //
4217    // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4218    // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
4219    // and these wrappers write `current_mode` so `vim_mode()` returns correct
4220    // values regardless of which path performed the transition.
4221    // See kryptic-sh/hjkl#89 for the full promotion plan.
4222
4223    /// `v` from Normal — enter charwise Visual mode, anchoring the selection
4224    /// at the current cursor position.
4225    pub fn enter_visual_char(&mut self) {
4226        vim::enter_visual_char_bridge(self);
4227    }
4228
4229    /// `V` from Normal — enter linewise Visual mode, anchoring on the current
4230    /// line. Motions extend the selection by whole lines.
4231    pub fn enter_visual_line(&mut self) {
4232        vim::enter_visual_line_bridge(self);
4233    }
4234
4235    /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
4236    /// rectangle whose corners are the anchor and the live cursor.
4237    pub fn enter_visual_block(&mut self) {
4238        vim::enter_visual_block_bridge(self);
4239    }
4240
4241    /// Esc from any visual mode — set `<` / `>` marks, stash the selection
4242    /// for `gv` re-entry, then return to Normal mode.
4243    pub fn exit_visual_to_normal(&mut self) {
4244        vim::exit_visual_to_normal_bridge(self);
4245    }
4246
4247    /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
4248    /// so the user can extend the other end of the selection. Does NOT
4249    /// mutate the selection range; only the active endpoint changes.
4250    pub fn visual_o_toggle(&mut self) {
4251        vim::visual_o_toggle_bridge(self);
4252    }
4253
4254    /// `gv` — restore the last visual selection (mode + anchor + cursor
4255    /// position). No-op when no visual selection has been exited yet.
4256    pub fn reenter_last_visual(&mut self) {
4257        vim::reenter_last_visual_bridge(self);
4258    }
4259
4260    /// Direct mode-transition entry point. Sets both the internal FSM mode
4261    /// and the stable `current_mode` field read by [`Editor::vim_mode`].
4262    ///
4263    /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
4264    /// …) which also set up required bookkeeping (anchors, sessions, …).
4265    /// Use `set_mode` only when you need a raw mode flip without side-effects.
4266    pub fn set_mode(&mut self, mode: VimMode) {
4267        vim::set_mode_bridge(self, mode);
4268    }
4269}
4270
4271// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
4272//
4273// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
4274// `Editor` accessors and mutators defined in this block. Each method gets a
4275// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
4276// rather than individual getters + setters (e.g. `accumulate_count_digit`).
4277
4278/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
4279///
4280/// Treat as opaque — construct by calling `begin_step` and pass the
4281/// returned value directly into `end_step` without modification.
4282/// The fields capture per-step pre-dispatch state that the epilogue
4283/// needs to run its invariants correctly.
4284pub struct StepBookkeeping {
4285    /// True when the pending chord before this step was a macro-chord
4286    /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
4287    /// keys so that only the *payload* keys enter `recording_keys`.
4288    pub pending_was_macro_chord: bool,
4289    /// True when the mode was Insert *before* the FSM body ran. Used by
4290    /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
4291    /// back into Insert.
4292    pub was_insert: bool,
4293    /// Pre-dispatch visual snapshot. When the FSM body transitions out of
4294    /// a visual mode the epilogue uses this to set the `<`/`>` marks and
4295    /// store `last_visual` for `gv`.
4296    pub pre_visual_snapshot: Option<vim::LastVisual>,
4297}
4298
4299impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
4300    // ── Pending chord ─────────────────────────────────────────────────────────
4301
4302    /// Return a clone of the current pending chord state.
4303    pub fn pending(&self) -> vim::Pending {
4304        self.vim.pending.clone()
4305    }
4306
4307    /// Overwrite the pending chord state.
4308    pub fn set_pending(&mut self, p: vim::Pending) {
4309        self.vim.pending = p;
4310    }
4311
4312    /// Atomically take the pending chord, replacing it with `Pending::None`.
4313    pub fn take_pending(&mut self) -> vim::Pending {
4314        std::mem::take(&mut self.vim.pending)
4315    }
4316
4317    // ── Count prefix ──────────────────────────────────────────────────────────
4318
4319    /// Return the raw digit-prefix count (`0` = no prefix typed yet).
4320    pub fn count(&self) -> usize {
4321        self.vim.count
4322    }
4323
4324    /// Overwrite the digit-prefix count directly.
4325    pub fn set_count(&mut self, c: usize) {
4326        self.vim.count = c;
4327    }
4328
4329    /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
4330    pub fn accumulate_count_digit(&mut self, digit: usize) {
4331        self.vim.count = self.vim.count.saturating_mul(10) + digit;
4332    }
4333
4334    /// Reset the count prefix to zero (no pending count).
4335    pub fn reset_count(&mut self) {
4336        self.vim.count = 0;
4337    }
4338
4339    /// Consume the count and return it; resets to zero. Returns `1` when no
4340    /// prefix was typed (mirrors `take_count` in vim.rs).
4341    pub fn take_count(&mut self) -> usize {
4342        if self.vim.count > 0 {
4343            let n = self.vim.count;
4344            self.vim.count = 0;
4345            n
4346        } else {
4347            1
4348        }
4349    }
4350
4351    // ── Internal FSM mode ─────────────────────────────────────────────────────
4352
4353    /// Return the FSM-internal mode (Normal / Insert / Visual / …).
4354    pub fn fsm_mode(&self) -> vim::Mode {
4355        self.vim.mode
4356    }
4357
4358    /// Overwrite the FSM-internal mode without side-effects. Prefer the
4359    /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
4360    pub fn set_fsm_mode(&mut self, m: vim::Mode) {
4361        self.vim.mode = m;
4362        self.vim.current_mode = self.vim.public_mode();
4363    }
4364
4365    // ── Replaying flag ────────────────────────────────────────────────────────
4366
4367    /// `true` while the `.` dot-repeat replay is running.
4368    pub fn is_replaying(&self) -> bool {
4369        self.vim.replaying
4370    }
4371
4372    /// Set or clear the dot-replay flag.
4373    pub fn set_replaying(&mut self, v: bool) {
4374        self.vim.replaying = v;
4375    }
4376
4377    // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
4378
4379    /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
4380    /// to Insert after the next complete command.
4381    pub fn is_one_shot_normal(&self) -> bool {
4382        self.vim.one_shot_normal
4383    }
4384
4385    /// Set or clear the Ctrl-o one-shot-normal flag.
4386    pub fn set_one_shot_normal(&mut self, v: bool) {
4387        self.vim.one_shot_normal = v;
4388    }
4389
4390    // ── Last find (f/F/t/T target) ────────────────────────────────────────────
4391
4392    /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
4393    /// `None` before any find command was executed.
4394    pub fn last_find(&self) -> Option<(char, bool, bool)> {
4395        self.vim.last_find
4396    }
4397
4398    /// Overwrite the stored last-find target.
4399    pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
4400        self.vim.last_find = target;
4401    }
4402
4403    // ── Last change (dot-repeat payload) ─────────────────────────────────────
4404
4405    /// Return a clone of the last recorded mutating change, or `None` before
4406    /// any change has been made.
4407    pub fn last_change(&self) -> Option<vim::LastChange> {
4408        self.vim.last_change.clone()
4409    }
4410
4411    /// Overwrite the stored last-change record.
4412    pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
4413        self.vim.last_change = lc;
4414    }
4415
4416    /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
4417    /// field after the insert session completes).
4418    pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
4419        self.vim.last_change.as_mut()
4420    }
4421
4422    // ── Insert session ────────────────────────────────────────────────────────
4423
4424    /// Borrow the active insert session, or `None` when not in Insert mode.
4425    pub fn insert_session(&self) -> Option<&vim::InsertSession> {
4426        self.vim.insert_session.as_ref()
4427    }
4428
4429    /// Borrow the active insert session mutably.
4430    pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
4431        self.vim.insert_session.as_mut()
4432    }
4433
4434    /// Atomically take the insert session out, leaving `None`.
4435    pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
4436        self.vim.insert_session.take()
4437    }
4438
4439    /// Install a new insert session, replacing any existing one.
4440    pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
4441        self.vim.insert_session = s;
4442    }
4443
4444    // ── Visual anchors ────────────────────────────────────────────────────────
4445
4446    /// Return the charwise Visual-mode anchor `(row, col)`.
4447    pub fn visual_anchor(&self) -> (usize, usize) {
4448        self.vim.visual_anchor
4449    }
4450
4451    /// Overwrite the charwise Visual-mode anchor.
4452    pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
4453        self.vim.visual_anchor = anchor;
4454    }
4455
4456    /// Return the VisualLine anchor row.
4457    pub fn visual_line_anchor(&self) -> usize {
4458        self.vim.visual_line_anchor
4459    }
4460
4461    /// Overwrite the VisualLine anchor row.
4462    pub fn set_visual_line_anchor(&mut self, row: usize) {
4463        self.vim.visual_line_anchor = row;
4464    }
4465
4466    /// Return the VisualBlock anchor `(row, col)`.
4467    pub fn block_anchor(&self) -> (usize, usize) {
4468        self.vim.block_anchor
4469    }
4470
4471    /// Overwrite the VisualBlock anchor.
4472    pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
4473        self.vim.block_anchor = anchor;
4474    }
4475
4476    /// Return the VisualBlock virtual column used to survive j/k row clamping.
4477    pub fn block_vcol(&self) -> usize {
4478        self.vim.block_vcol
4479    }
4480
4481    /// Overwrite the VisualBlock virtual column.
4482    pub fn set_block_vcol(&mut self, vcol: usize) {
4483        self.vim.block_vcol = vcol;
4484    }
4485
4486    // ── Yank linewise flag ────────────────────────────────────────────────────
4487
4488    /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
4489    pub fn yank_linewise(&self) -> bool {
4490        self.vim.yank_linewise
4491    }
4492
4493    /// Set or clear the linewise-yank flag.
4494    pub fn set_yank_linewise(&mut self, v: bool) {
4495        self.vim.yank_linewise = v;
4496    }
4497
4498    // ── Pending register selector ─────────────────────────────────────────────
4499    // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
4500    // Only the mutators are new here.
4501
4502    /// Overwrite the pending register selector (Phase 6.6b mutator companion to
4503    /// the existing `pending_register()` getter).
4504    pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
4505        self.vim.pending_register = reg;
4506    }
4507
4508    /// Atomically take the pending register, returning `None` afterward.
4509    pub fn take_pending_register_raw(&mut self) -> Option<char> {
4510        self.vim.pending_register.take()
4511    }
4512
4513    // ── Macro recording ───────────────────────────────────────────────────────
4514
4515    /// Return the register currently being recorded into, or `None`.
4516    pub fn recording_macro(&self) -> Option<char> {
4517        self.vim.recording_macro
4518    }
4519
4520    /// Overwrite the recording-macro target register.
4521    pub fn set_recording_macro(&mut self, reg: Option<char>) {
4522        self.vim.recording_macro = reg;
4523    }
4524
4525    /// Append one input to the in-progress macro recording buffer.
4526    pub fn push_recording_key(&mut self, input: crate::input::Input) {
4527        self.vim.recording_keys.push(input);
4528    }
4529
4530    /// Atomically take the recorded key sequence, leaving an empty vec.
4531    pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
4532        std::mem::take(&mut self.vim.recording_keys)
4533    }
4534
4535    /// Overwrite the recording-keys buffer (e.g. to seed from a register).
4536    pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
4537        self.vim.recording_keys = keys;
4538    }
4539
4540    // ── Macro replay flag ─────────────────────────────────────────────────────
4541
4542    /// `true` while `@reg` macro replay is running (suppresses re-recording).
4543    pub fn is_replaying_macro_raw(&self) -> bool {
4544        self.vim.replaying_macro
4545    }
4546
4547    /// Set or clear the macro-replay-in-progress flag.
4548    pub fn set_replaying_macro_raw(&mut self, v: bool) {
4549        self.vim.replaying_macro = v;
4550    }
4551
4552    // ── Last macro register ───────────────────────────────────────────────────
4553
4554    /// Return the register of the most recently played macro (`@@` source).
4555    pub fn last_macro(&self) -> Option<char> {
4556        self.vim.last_macro
4557    }
4558
4559    /// Overwrite the last-played-macro register.
4560    pub fn set_last_macro(&mut self, reg: Option<char>) {
4561        self.vim.last_macro = reg;
4562    }
4563
4564    // ── Last insert position ──────────────────────────────────────────────────
4565
4566    /// Return the cursor position when Insert mode was last exited (for `gi`).
4567    pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
4568        self.vim.last_insert_pos
4569    }
4570
4571    /// Overwrite the stored last-insert position.
4572    pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
4573        self.vim.last_insert_pos = pos;
4574    }
4575
4576    // ── Last visual selection ─────────────────────────────────────────────────
4577
4578    /// Return the saved visual selection snapshot for `gv`, or `None`.
4579    pub fn last_visual(&self) -> Option<vim::LastVisual> {
4580        self.vim.last_visual
4581    }
4582
4583    /// Overwrite the saved visual selection snapshot.
4584    pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
4585        self.vim.last_visual = snap;
4586    }
4587
4588    // ── Viewport-pinned flag ──────────────────────────────────────────────────
4589
4590    /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
4591    /// the end-of-step scrolloff pass).
4592    pub fn viewport_pinned(&self) -> bool {
4593        self.vim.viewport_pinned
4594    }
4595
4596    /// Set or clear the viewport-pinned flag.
4597    pub fn set_viewport_pinned(&mut self, v: bool) {
4598        self.vim.viewport_pinned = v;
4599    }
4600
4601    // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
4602
4603    /// `true` while waiting for the register-name key after `Ctrl-R` in
4604    /// Insert mode.
4605    pub fn insert_pending_register(&self) -> bool {
4606        self.vim.insert_pending_register
4607    }
4608
4609    /// Set or clear the `Ctrl-R` register-wait flag.
4610    pub fn set_insert_pending_register(&mut self, v: bool) {
4611        self.vim.insert_pending_register = v;
4612    }
4613
4614    // ── Change-mark start ─────────────────────────────────────────────────────
4615
4616    /// Return the stashed `[` mark start for a Change operation, or `None`.
4617    pub fn change_mark_start(&self) -> Option<(usize, usize)> {
4618        self.vim.change_mark_start
4619    }
4620
4621    /// Atomically take the change-mark start, leaving `None`.
4622    pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
4623        self.vim.change_mark_start.take()
4624    }
4625
4626    /// Overwrite the change-mark start.
4627    pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
4628        self.vim.change_mark_start = pos;
4629    }
4630
4631    // ── Timeout tracking ──────────────────────────────────────────────────────
4632
4633    /// Return the wall-clock `Instant` of the last keystroke.
4634    pub fn last_input_at(&self) -> Option<std::time::Instant> {
4635        self.vim.last_input_at
4636    }
4637
4638    /// Overwrite the wall-clock last-input timestamp.
4639    pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
4640        self.vim.last_input_at = t;
4641    }
4642
4643    /// Return the `Host::now()` duration at the last keystroke.
4644    pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
4645        self.vim.last_input_host_at
4646    }
4647
4648    /// Overwrite the host-clock last-input timestamp.
4649    pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
4650        self.vim.last_input_host_at = d;
4651    }
4652
4653    // ── Search prompt ──────────────────────────────────────────────────────────
4654
4655    /// Borrow the live search prompt, or `None` when not in search-prompt mode.
4656    pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
4657        self.vim.search_prompt.as_ref()
4658    }
4659
4660    /// Borrow the live search prompt mutably.
4661    pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
4662        self.vim.search_prompt.as_mut()
4663    }
4664
4665    /// Atomically take the search prompt, leaving `None`.
4666    pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
4667        self.vim.search_prompt.take()
4668    }
4669
4670    /// Install a new search prompt (entering search-prompt mode).
4671    pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
4672        self.vim.search_prompt = prompt;
4673    }
4674
4675    // ── Last search pattern / direction ───────────────────────────────────────
4676    // Note: `last_search_forward()` getter already exists at line ~1909.
4677    // `set_last_search()` combined mutator exists at line ~1918.
4678    // Only new / complementary accessors are added here.
4679
4680    /// Return the most recently committed search pattern, or `None`.
4681    pub fn last_search_pattern(&self) -> Option<&str> {
4682        self.vim.last_search.as_deref()
4683    }
4684
4685    /// Overwrite the stored last-search pattern without changing direction
4686    /// (use the existing `set_last_search` for the combined update).
4687    pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
4688        self.vim.last_search = pattern;
4689    }
4690
4691    /// Overwrite only the last-search direction flag.
4692    pub fn set_last_search_forward_only(&mut self, forward: bool) {
4693        self.vim.last_search_forward = forward;
4694    }
4695
4696    // ── Search history ────────────────────────────────────────────────────────
4697
4698    /// Borrow the committed search-pattern history (oldest first).
4699    pub fn search_history(&self) -> &[String] {
4700        &self.vim.search_history
4701    }
4702
4703    /// Borrow the search history mutably (e.g. to push a new entry).
4704    pub fn search_history_mut(&mut self) -> &mut Vec<String> {
4705        &mut self.vim.search_history
4706    }
4707
4708    /// Return the current search-history navigation cursor index.
4709    pub fn search_history_cursor(&self) -> Option<usize> {
4710        self.vim.search_history_cursor
4711    }
4712
4713    /// Overwrite the search-history navigation cursor.
4714    pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
4715        self.vim.search_history_cursor = idx;
4716    }
4717
4718    // ── Jump lists ────────────────────────────────────────────────────────────
4719
4720    /// Borrow the back half of the jump list (entries Ctrl-o pops from).
4721    pub fn jump_back_list(&self) -> &[(usize, usize)] {
4722        &self.vim.jump_back
4723    }
4724
4725    /// Borrow the back jump list mutably (push / pop).
4726    pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4727        &mut self.vim.jump_back
4728    }
4729
4730    /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
4731    pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
4732        &self.vim.jump_fwd
4733    }
4734
4735    /// Borrow the forward jump list mutably (push / pop / clear).
4736    pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4737        &mut self.vim.jump_fwd
4738    }
4739
4740    // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
4741    //
4742    // `push_search_pattern`, `push_jump`, `record_search_history`, and
4743    // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
4744    // search-prompt and normal-mode FSM can call them via the public API.
4745
4746    /// Compile `pattern` into a regex and install it as the active search
4747    /// pattern. Respects `:set ignorecase` / `:set smartcase`. An empty or
4748    /// invalid pattern clears the highlight without raising an error.
4749    pub fn push_search_pattern(&mut self, pattern: &str) {
4750        let compiled = if pattern.is_empty() {
4751            None
4752        } else {
4753            let case_insensitive = self.settings().ignore_case
4754                && !(self.settings().smartcase && pattern.chars().any(|c| c.is_uppercase()));
4755            let effective: std::borrow::Cow<'_, str> = if case_insensitive {
4756                std::borrow::Cow::Owned(format!("(?i){pattern}"))
4757            } else {
4758                std::borrow::Cow::Borrowed(pattern)
4759            };
4760            regex::Regex::new(&effective).ok()
4761        };
4762        let wrap = self.settings().wrapscan;
4763        self.set_search_pattern(compiled);
4764        self.search_state_mut().wrap_around = wrap;
4765    }
4766
4767    /// Record a pre-jump cursor position onto the back jumplist. Called
4768    /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
4769    /// committed `/` or `?`, …). Branching off the history clears the
4770    /// forward half, matching vim's "redo-is-lost" semantics.
4771    pub fn push_jump(&mut self, from: (usize, usize)) {
4772        self.vim.jump_back.push(from);
4773        if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
4774            self.vim.jump_back.remove(0);
4775        }
4776        self.vim.jump_fwd.clear();
4777    }
4778
4779    /// Push `pattern` onto the committed search history. Skips if the
4780    /// most recent entry already matches (consecutive dedupe) and trims
4781    /// the oldest entries beyond the history cap.
4782    pub fn record_search_history(&mut self, pattern: &str) {
4783        if pattern.is_empty() {
4784            return;
4785        }
4786        if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
4787            return;
4788        }
4789        self.vim.search_history.push(pattern.to_string());
4790        let len = self.vim.search_history.len();
4791        if len > vim::SEARCH_HISTORY_MAX {
4792            self.vim
4793                .search_history
4794                .drain(0..len - vim::SEARCH_HISTORY_MAX);
4795        }
4796    }
4797
4798    /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
4799    /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
4800    /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
4801    /// active search prompt.
4802    pub fn walk_search_history(&mut self, dir: isize) {
4803        if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
4804            return;
4805        }
4806        let len = self.vim.search_history.len();
4807        let next_idx = match (self.vim.search_history_cursor, dir) {
4808            (None, -1) => Some(len - 1),
4809            (None, 1) => return,
4810            (Some(i), -1) => i.checked_sub(1),
4811            (Some(i), 1) if i + 1 < len => Some(i + 1),
4812            _ => None,
4813        };
4814        let Some(idx) = next_idx else {
4815            return;
4816        };
4817        self.vim.search_history_cursor = Some(idx);
4818        let text = self.vim.search_history[idx].clone();
4819        if let Some(prompt) = self.vim.search_prompt.as_mut() {
4820            prompt.cursor = text.chars().count();
4821            prompt.text = text.clone();
4822        }
4823        self.push_search_pattern(&text);
4824    }
4825
4826    // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
4827    //
4828    // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
4829    // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
4830
4831    /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
4832    ///
4833    /// Call this at the start of every step; pass the returned
4834    /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
4835    ///
4836    /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
4837    /// Returns `Err(consumed)` when the prelude itself handled the input
4838    /// (macro-stop chord); in that case skip the FSM body and do NOT call
4839    /// `end_step` — the macro-stop path is a true short-circuit with no
4840    /// epilogue needed.
4841    ///
4842    /// This method does NOT handle the search-prompt intercept — callers
4843    /// must check `search_prompt_state().is_some()` before calling `begin_step`
4844    /// and dispatch to the search-prompt FSM body directly.
4845    pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
4846        use crate::input::Key;
4847        use vim::{Mode, Pending};
4848        // ── Timestamps ───────────────────────────────────────────────────────
4849        // Phase 7f: sync buffer before motion handlers see it.
4850        self.sync_buffer_content_from_textarea();
4851        // `:set timeoutlen` chord-timeout handling.
4852        let now = std::time::Instant::now();
4853        let host_now = self.host.now();
4854        let timed_out = match self.vim.last_input_host_at {
4855            Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
4856            None => false,
4857        };
4858        if timed_out {
4859            let chord_in_flight = !matches!(self.vim.pending, Pending::None)
4860                || self.vim.count != 0
4861                || self.vim.pending_register.is_some()
4862                || self.vim.insert_pending_register;
4863            if chord_in_flight {
4864                self.vim.clear_pending_prefix();
4865            }
4866        }
4867        self.vim.last_input_at = Some(now);
4868        self.vim.last_input_host_at = Some(host_now);
4869        // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
4870        if self.vim.recording_macro.is_some()
4871            && !self.vim.replaying_macro
4872            && matches!(self.vim.pending, Pending::None)
4873            && self.vim.mode != Mode::Insert
4874            && input.key == Key::Char('q')
4875            && !input.ctrl
4876            && !input.alt
4877        {
4878            let reg = self.vim.recording_macro.take().unwrap();
4879            let keys = std::mem::take(&mut self.vim.recording_keys);
4880            let text = crate::input::encode_macro(&keys);
4881            self.set_named_register_text(reg.to_ascii_lowercase(), text);
4882            return Err(true);
4883        }
4884        // ── Snapshots for epilogue ────────────────────────────────────────────
4885        let pending_was_macro_chord = matches!(
4886            self.vim.pending,
4887            Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
4888        );
4889        let was_insert = self.vim.mode == Mode::Insert;
4890        let pre_visual_snapshot = match self.vim.mode {
4891            Mode::Visual => Some(vim::LastVisual {
4892                mode: Mode::Visual,
4893                anchor: self.vim.visual_anchor,
4894                cursor: self.cursor(),
4895                block_vcol: 0,
4896            }),
4897            Mode::VisualLine => Some(vim::LastVisual {
4898                mode: Mode::VisualLine,
4899                anchor: (self.vim.visual_line_anchor, 0),
4900                cursor: self.cursor(),
4901                block_vcol: 0,
4902            }),
4903            Mode::VisualBlock => Some(vim::LastVisual {
4904                mode: Mode::VisualBlock,
4905                anchor: self.vim.block_anchor,
4906                cursor: self.cursor(),
4907                block_vcol: self.vim.block_vcol,
4908            }),
4909            _ => None,
4910        };
4911        Ok(StepBookkeeping {
4912            pending_was_macro_chord,
4913            was_insert,
4914            pre_visual_snapshot,
4915        })
4916    }
4917
4918    /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
4919    ///
4920    /// `input` is the same input that was passed to `begin_step`.
4921    /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
4922    /// `consumed` is the return value of the FSM body; this method returns
4923    /// it after running all epilogue invariants.
4924    ///
4925    /// Must NOT be called when `begin_step` returned `Err(...)`.
4926    pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
4927        use crate::input::Key;
4928        use vim::{Mode, Pending};
4929        let StepBookkeeping {
4930            pending_was_macro_chord,
4931            was_insert,
4932            pre_visual_snapshot,
4933        } = bk;
4934        // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
4935        if let Some(snap) = pre_visual_snapshot
4936            && !matches!(
4937                self.vim.mode,
4938                Mode::Visual | Mode::VisualLine | Mode::VisualBlock
4939            )
4940        {
4941            let (lo, hi) = match snap.mode {
4942                Mode::Visual => {
4943                    if snap.anchor <= snap.cursor {
4944                        (snap.anchor, snap.cursor)
4945                    } else {
4946                        (snap.cursor, snap.anchor)
4947                    }
4948                }
4949                Mode::VisualLine => {
4950                    let r_lo = snap.anchor.0.min(snap.cursor.0);
4951                    let r_hi = snap.anchor.0.max(snap.cursor.0);
4952                    let last_col = self
4953                        .buffer()
4954                        .lines()
4955                        .get(r_hi)
4956                        .map(|l| l.chars().count().saturating_sub(1))
4957                        .unwrap_or(0);
4958                    ((r_lo, 0), (r_hi, last_col))
4959                }
4960                Mode::VisualBlock => {
4961                    let (r1, c1) = snap.anchor;
4962                    let (r2, c2) = snap.cursor;
4963                    ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
4964                }
4965                _ => {
4966                    if snap.anchor <= snap.cursor {
4967                        (snap.anchor, snap.cursor)
4968                    } else {
4969                        (snap.cursor, snap.anchor)
4970                    }
4971                }
4972            };
4973            self.set_mark('<', lo);
4974            self.set_mark('>', hi);
4975            self.vim.last_visual = Some(snap);
4976        }
4977        // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
4978        if !was_insert
4979            && self.vim.one_shot_normal
4980            && self.vim.mode == Mode::Normal
4981            && matches!(self.vim.pending, Pending::None)
4982        {
4983            self.vim.one_shot_normal = false;
4984            self.vim.mode = Mode::Insert;
4985        }
4986        // ── Content + viewport sync ───────────────────────────────────────────
4987        self.sync_buffer_content_from_textarea();
4988        if !self.vim.viewport_pinned {
4989            self.ensure_cursor_in_scrolloff();
4990        }
4991        self.vim.viewport_pinned = false;
4992        // ── Recorder hook ─────────────────────────────────────────────────────
4993        if self.vim.recording_macro.is_some()
4994            && !self.vim.replaying_macro
4995            && input.key != Key::Char('q')
4996            && !pending_was_macro_chord
4997        {
4998            self.vim.recording_keys.push(input);
4999        }
5000        // ── Phase 6.3: current_mode sync ─────────────────────────────────────
5001        self.vim.current_mode = self.vim.public_mode();
5002        consumed
5003    }
5004
5005    // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
5006
5007    /// `true` when the editor is in any visual mode (Visual / VisualLine /
5008    /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
5009    pub fn is_visual(&self) -> bool {
5010        matches!(
5011            self.vim.mode,
5012            vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
5013        )
5014    }
5015
5016    /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
5017    /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
5018    /// virtual column, which survives j/k clamping to shorter rows).
5019    ///
5020    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
5021    /// extents needed for VisualBlock `I` / `A` / `r` without accessing
5022    /// engine-private helpers.
5023    pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
5024        let (ar, ac) = self.vim.block_anchor;
5025        let (cr, _) = self.cursor();
5026        let cc = self.vim.block_vcol;
5027        let top = ar.min(cr);
5028        let bot = ar.max(cr);
5029        let left = ac.min(cc);
5030        let right = ac.max(cc);
5031        (top, bot, left, right)
5032    }
5033
5034    /// Return the character count (code-point count) of line `row`, or `0`
5035    /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
5036    /// I / A column computations.
5037    pub fn line_char_count(&self, row: usize) -> usize {
5038        buf_line_chars(&self.buffer, row)
5039    }
5040
5041    /// Apply operator over `motion` with `count` repetitions. The full
5042    /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
5043    ///
5044    /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
5045    /// relocated `handle_after_op` can call it directly with a parsed `Motion`
5046    /// without re-entering the engine FSM.
5047    pub fn apply_op_with_motion_direct(
5048        &mut self,
5049        op: crate::vim::Operator,
5050        motion: &crate::vim::Motion,
5051        count: usize,
5052    ) {
5053        vim::apply_op_with_motion(self, op, motion, count);
5054    }
5055
5056    /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
5057    /// `delta = 1` increments; `delta = -1` decrements; larger deltas
5058    /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
5059    /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
5060    pub fn adjust_number(&mut self, delta: i64) {
5061        vim::adjust_number(self, delta);
5062    }
5063
5064    /// Open the `/` or `?` search prompt. `forward = true` for `/`,
5065    /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
5066    /// dispatch `/` and `?` without re-entering the engine FSM.
5067    pub fn enter_search(&mut self, forward: bool) {
5068        vim::enter_search(self, forward);
5069    }
5070
5071    /// Enter Insert mode at the left edge of a VisualBlock selection for
5072    /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
5073    /// then begins an insert session with `InsertReason::BlockEdge`.
5074    ///
5075    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5076    /// VisualBlock `I` command without accessing engine-private helpers.
5077    pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
5078        self.jump_cursor(top, col);
5079        self.vim.mode = vim::Mode::Normal;
5080        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5081    }
5082
5083    /// Enter Insert mode at the right edge of a VisualBlock selection for
5084    /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
5085    /// then begins an insert session with `InsertReason::BlockEdge`.
5086    ///
5087    /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5088    /// VisualBlock `A` command without accessing engine-private helpers.
5089    pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
5090        self.jump_cursor(top, col);
5091        self.vim.mode = vim::Mode::Normal;
5092        vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5093    }
5094
5095    /// Execute a motion (cursor movement), push to the jumplist for big jumps,
5096    /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
5097    /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5098    pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
5099        vim::execute_motion(self, motion, count);
5100    }
5101
5102    /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
5103    /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
5104    /// non-h/l motions leave it alone so the intended column survives clamping
5105    /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5106    pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
5107        vim::update_block_vcol(self, motion);
5108    }
5109
5110    /// Apply `op` over the current visual selection (char-wise, linewise, or
5111    /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
5112    /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
5113    pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
5114        vim::apply_visual_operator(self, op);
5115    }
5116
5117    /// Replace each character cell in the current VisualBlock selection with
5118    /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
5119    /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
5120    pub fn replace_block_char(&mut self, ch: char) {
5121        vim::block_replace(self, ch);
5122    }
5123
5124    /// Extend the current visual selection to cover the text object identified
5125    /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
5126    /// via `text_object_range`, then updates the visual anchor and cursor.
5127    ///
5128    /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
5129    /// in `hjkl-vim::normal::handle_visual_text_obj`.
5130    pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
5131        use crate::vim::{Mode, TextObject};
5132        let obj = match ch {
5133            'w' => TextObject::Word { big: false },
5134            'W' => TextObject::Word { big: true },
5135            '"' | '\'' | '`' => TextObject::Quote(ch),
5136            '(' | ')' | 'b' => TextObject::Bracket('('),
5137            '[' | ']' => TextObject::Bracket('['),
5138            '{' | '}' | 'B' => TextObject::Bracket('{'),
5139            '<' | '>' => TextObject::Bracket('<'),
5140            'p' => TextObject::Paragraph,
5141            't' => TextObject::XmlTag,
5142            's' => TextObject::Sentence,
5143            _ => return,
5144        };
5145        let Some((start, end, kind)) = vim::text_object_range(self, obj, inner) else {
5146            return;
5147        };
5148        match kind {
5149            crate::vim::RangeKind::Linewise => {
5150                self.vim.visual_line_anchor = start.0;
5151                self.vim.mode = Mode::VisualLine;
5152                self.vim.current_mode = VimMode::VisualLine;
5153                self.jump_cursor(end.0, 0);
5154            }
5155            _ => {
5156                self.vim.mode = Mode::Visual;
5157                self.vim.current_mode = VimMode::Visual;
5158                self.vim.visual_anchor = (start.0, start.1);
5159                let (er, ec) = vim::retreat_one(self, end);
5160                self.jump_cursor(er, ec);
5161            }
5162        }
5163    }
5164}
5165
5166/// Visual column of the character at `char_col` in `line`, treating `\t`
5167/// as expansion to the next `tab_width` stop and every other char as
5168/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
5169/// the cursor math elsewhere also assumes single-cell chars.
5170fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
5171    let mut visual = 0usize;
5172    for (i, ch) in line.chars().enumerate() {
5173        if i >= char_col {
5174            break;
5175        }
5176        if ch == '\t' {
5177            visual += tab_width - (visual % tab_width);
5178        } else {
5179            visual += 1;
5180        }
5181    }
5182    visual
5183}
5184
5185#[cfg(feature = "crossterm")]
5186impl From<KeyEvent> for Input {
5187    fn from(key: KeyEvent) -> Self {
5188        let k = match key.code {
5189            KeyCode::Char(c) => Key::Char(c),
5190            KeyCode::Backspace => Key::Backspace,
5191            KeyCode::Delete => Key::Delete,
5192            KeyCode::Enter => Key::Enter,
5193            KeyCode::Left => Key::Left,
5194            KeyCode::Right => Key::Right,
5195            KeyCode::Up => Key::Up,
5196            KeyCode::Down => Key::Down,
5197            KeyCode::Home => Key::Home,
5198            KeyCode::End => Key::End,
5199            KeyCode::Tab => Key::Tab,
5200            KeyCode::Esc => Key::Esc,
5201            _ => Key::Null,
5202        };
5203        Input {
5204            key: k,
5205            ctrl: key.modifiers.contains(KeyModifiers::CONTROL),
5206            alt: key.modifiers.contains(KeyModifiers::ALT),
5207            shift: key.modifiers.contains(KeyModifiers::SHIFT),
5208        }
5209    }
5210}
5211
5212/// Crossterm `KeyEvent` → engine `Input`. Thin wrapper that delegates
5213/// to the [`From`] impl above; kept as a free fn for the in-tree
5214/// callers in the legacy ratatui-coupled paths.
5215#[cfg(feature = "crossterm")]
5216pub fn crossterm_to_input(key: KeyEvent) -> Input {
5217    Input::from(key)
5218}
5219
5220#[cfg(all(test, feature = "crossterm", feature = "ratatui"))]
5221mod tests {
5222    use super::*;
5223    use crate::types::Host;
5224    use crossterm::event::KeyEvent;
5225
5226    #[allow(dead_code)]
5227    fn key(code: KeyCode) -> KeyEvent {
5228        KeyEvent::new(code, KeyModifiers::NONE)
5229    }
5230    #[allow(dead_code)]
5231    fn shift_key(code: KeyCode) -> KeyEvent {
5232        KeyEvent::new(code, KeyModifiers::SHIFT)
5233    }
5234    #[allow(dead_code)]
5235    fn ctrl_key(code: KeyCode) -> KeyEvent {
5236        KeyEvent::new(code, KeyModifiers::CONTROL)
5237    }
5238
5239    #[test]
5240    fn intern_style_dedups_engine_native_styles() {
5241        use crate::types::{Attrs, Color, Style};
5242        let mut e = Editor::new(
5243            hjkl_buffer::Buffer::new(),
5244            crate::types::DefaultHost::new(),
5245            crate::types::Options::default(),
5246        );
5247        let s = Style {
5248            fg: Some(Color(255, 0, 0)),
5249            bg: None,
5250            attrs: Attrs::BOLD,
5251        };
5252        let id_a = e.intern_style(s);
5253        // Re-interning the same engine style returns the same id.
5254        let id_b = e.intern_style(s);
5255        assert_eq!(id_a, id_b);
5256        // Engine accessor returns the same style back.
5257        let back = e.engine_style_at(id_a).expect("interned");
5258        assert_eq!(back, s);
5259    }
5260
5261    #[test]
5262    fn engine_style_at_out_of_range_returns_none() {
5263        let e = Editor::new(
5264            hjkl_buffer::Buffer::new(),
5265            crate::types::DefaultHost::new(),
5266            crate::types::Options::default(),
5267        );
5268        assert!(e.engine_style_at(99).is_none());
5269    }
5270
5271    #[test]
5272    fn options_bridge_roundtrip() {
5273        let mut e = Editor::new(
5274            hjkl_buffer::Buffer::new(),
5275            crate::types::DefaultHost::new(),
5276            crate::types::Options::default(),
5277        );
5278        let opts = e.current_options();
5279        // 0.2.0: defaults flipped to modern editor norms — 4-space soft tabs.
5280        assert_eq!(opts.shiftwidth, 4);
5281        assert_eq!(opts.tabstop, 4);
5282
5283        let new_opts = crate::types::Options {
5284            shiftwidth: 4,
5285            tabstop: 2,
5286            ignorecase: true,
5287            ..crate::types::Options::default()
5288        };
5289        e.apply_options(&new_opts);
5290
5291        let after = e.current_options();
5292        assert_eq!(after.shiftwidth, 4);
5293        assert_eq!(after.tabstop, 2);
5294        assert!(after.ignorecase);
5295    }
5296
5297    #[test]
5298    fn selection_highlight_none_in_normal() {
5299        let mut e = Editor::new(
5300            hjkl_buffer::Buffer::new(),
5301            crate::types::DefaultHost::new(),
5302            crate::types::Options::default(),
5303        );
5304        e.set_content("hello");
5305        assert!(e.selection_highlight().is_none());
5306    }
5307
5308    #[test]
5309    fn highlights_emit_search_matches() {
5310        use crate::types::HighlightKind;
5311        let mut e = Editor::new(
5312            hjkl_buffer::Buffer::new(),
5313            crate::types::DefaultHost::new(),
5314            crate::types::Options::default(),
5315        );
5316        e.set_content("foo bar foo\nbaz qux\n");
5317        // 0.0.35: arm via the engine search state. The buffer
5318        // accessor still works (deprecated) but new code goes
5319        // through Editor.
5320        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
5321        let hs = e.highlights_for_line(0);
5322        assert_eq!(hs.len(), 2);
5323        for h in &hs {
5324            assert_eq!(h.kind, HighlightKind::SearchMatch);
5325            assert_eq!(h.range.start.line, 0);
5326            assert_eq!(h.range.end.line, 0);
5327        }
5328    }
5329
5330    #[test]
5331    fn highlights_empty_without_pattern() {
5332        let mut e = Editor::new(
5333            hjkl_buffer::Buffer::new(),
5334            crate::types::DefaultHost::new(),
5335            crate::types::Options::default(),
5336        );
5337        e.set_content("foo bar");
5338        assert!(e.highlights_for_line(0).is_empty());
5339    }
5340
5341    #[test]
5342    fn highlights_empty_for_out_of_range_line() {
5343        let mut e = Editor::new(
5344            hjkl_buffer::Buffer::new(),
5345            crate::types::DefaultHost::new(),
5346            crate::types::Options::default(),
5347        );
5348        e.set_content("foo");
5349        e.set_search_pattern(Some(regex::Regex::new("foo").unwrap()));
5350        assert!(e.highlights_for_line(99).is_empty());
5351    }
5352
5353    #[test]
5354    fn snapshot_roundtrips_through_restore() {
5355        use crate::types::SnapshotMode;
5356        let mut e = Editor::new(
5357            hjkl_buffer::Buffer::new(),
5358            crate::types::DefaultHost::new(),
5359            crate::types::Options::default(),
5360        );
5361        e.set_content("alpha\nbeta\ngamma");
5362        e.jump_cursor(2, 3);
5363        let snap = e.take_snapshot();
5364        assert_eq!(snap.mode, SnapshotMode::Normal);
5365        assert_eq!(snap.cursor, (2, 3));
5366        assert_eq!(snap.lines.len(), 3);
5367
5368        let mut other = Editor::new(
5369            hjkl_buffer::Buffer::new(),
5370            crate::types::DefaultHost::new(),
5371            crate::types::Options::default(),
5372        );
5373        other.restore_snapshot(snap).expect("restore");
5374        assert_eq!(other.cursor(), (2, 3));
5375        assert_eq!(other.buffer().lines().len(), 3);
5376    }
5377
5378    #[test]
5379    fn restore_snapshot_rejects_version_mismatch() {
5380        let mut e = Editor::new(
5381            hjkl_buffer::Buffer::new(),
5382            crate::types::DefaultHost::new(),
5383            crate::types::Options::default(),
5384        );
5385        let mut snap = e.take_snapshot();
5386        snap.version = 9999;
5387        match e.restore_snapshot(snap) {
5388            Err(crate::EngineError::SnapshotVersion(got, want)) => {
5389                assert_eq!(got, 9999);
5390                assert_eq!(want, crate::types::EditorSnapshot::VERSION);
5391            }
5392            other => panic!("expected SnapshotVersion err, got {other:?}"),
5393        }
5394    }
5395
5396    #[test]
5397    fn take_content_change_returns_some_on_first_dirty() {
5398        let mut e = Editor::new(
5399            hjkl_buffer::Buffer::new(),
5400            crate::types::DefaultHost::new(),
5401            crate::types::Options::default(),
5402        );
5403        e.set_content("hello");
5404        let first = e.take_content_change();
5405        assert!(first.is_some());
5406        let second = e.take_content_change();
5407        assert!(second.is_none());
5408    }
5409
5410    fn many_lines(n: usize) -> String {
5411        (0..n)
5412            .map(|i| format!("line{i}"))
5413            .collect::<Vec<_>>()
5414            .join("\n")
5415    }
5416
5417    #[allow(dead_code)]
5418    fn prime_viewport<H: Host>(e: &mut Editor<hjkl_buffer::Buffer, H>, height: u16) {
5419        e.set_viewport_height(height);
5420    }
5421
5422    /// Contract that the TUI drain relies on: `set_content` flags the
5423    /// editor dirty (so the next `take_dirty` call reports the change),
5424    /// and a second `take_dirty` returns `false` after consumption. The
5425    /// TUI drains this flag after every programmatic content load so
5426    /// opening a tab doesn't get mistaken for a user edit and mark the
5427    /// tab dirty (which would then trigger the quit-prompt on `:q`).
5428    #[test]
5429    fn set_content_dirties_then_take_dirty_clears() {
5430        let mut e = Editor::new(
5431            hjkl_buffer::Buffer::new(),
5432            crate::types::DefaultHost::new(),
5433            crate::types::Options::default(),
5434        );
5435        e.set_content("hello");
5436        assert!(
5437            e.take_dirty(),
5438            "set_content should leave content_dirty=true"
5439        );
5440        assert!(!e.take_dirty(), "take_dirty should clear the flag");
5441    }
5442
5443    #[test]
5444    fn content_arc_cache_invalidated_by_set_content() {
5445        let mut e = Editor::new(
5446            hjkl_buffer::Buffer::new(),
5447            crate::types::DefaultHost::new(),
5448            crate::types::Options::default(),
5449        );
5450        e.set_content("one");
5451        let a = e.content_arc();
5452        e.set_content("two");
5453        let b = e.content_arc();
5454        assert!(!std::sync::Arc::ptr_eq(&a, &b));
5455        assert!(b.starts_with("two"));
5456    }
5457
5458    // ── lnum_width ──────────────────────────────────────────────────────────
5459
5460    #[test]
5461    fn lnum_width_numberwidth_floor_enforced() {
5462        let mut e = Editor::new(
5463            hjkl_buffer::Buffer::new(),
5464            crate::types::DefaultHost::new(),
5465            crate::types::Options::default(),
5466        );
5467        // Default: number=true, numberwidth=4; buffer has 1 line → digits=1,
5468        // needed=2 which is less than floor of 4.
5469        e.set_content("single line");
5470        assert_eq!(e.lnum_width(), 4, "should be floored to numberwidth (4)");
5471    }
5472
5473    #[test]
5474    fn lnum_width_zero_when_both_flags_off() {
5475        let mut e = Editor::new(
5476            hjkl_buffer::Buffer::new(),
5477            crate::types::DefaultHost::new(),
5478            crate::types::Options {
5479                number: false,
5480                relativenumber: false,
5481                ..crate::types::Options::default()
5482            },
5483        );
5484        e.set_content("some content");
5485        assert_eq!(
5486            e.lnum_width(),
5487            0,
5488            "gutter should be 0 when number flags are off"
5489        );
5490    }
5491
5492    // ── doc-coord mouse primitives (Phase 1 — issue #114) ──────────────────
5493
5494    #[test]
5495    fn mouse_click_doc_moves_cursor_to_doc_coords() {
5496        let mut e = Editor::new(
5497            hjkl_buffer::Buffer::new(),
5498            crate::types::DefaultHost::new(),
5499            crate::types::Options::default(),
5500        );
5501        e.set_content("hello\nworld");
5502        e.mouse_click_doc(1, 2);
5503        assert_eq!(e.cursor(), (1, 2));
5504    }
5505
5506    #[test]
5507    fn mouse_click_doc_normal_mode_clamps_past_eol_to_last_char() {
5508        let mut e = Editor::new(
5509            hjkl_buffer::Buffer::new(),
5510            crate::types::DefaultHost::new(),
5511            crate::types::Options::default(),
5512        );
5513        e.set_content("hello");
5514        // Normal mode (default after construction): "hello" has 5 chars,
5515        // past-EOL click clamps to col=4 (last char 'o' — never on the
5516        // implicit \n, vim/neovim convention).
5517        e.mouse_click_doc(0, 99);
5518        assert_eq!(e.cursor(), (0, 4));
5519    }
5520
5521    #[test]
5522    fn mouse_click_doc_normal_mode_clamps_past_eol_multibyte() {
5523        let mut e = Editor::new(
5524            hjkl_buffer::Buffer::new(),
5525            crate::types::DefaultHost::new(),
5526            crate::types::Options::default(),
5527        );
5528        // 5 chars, 6 bytes — clamping must be char-counted, not byte-counted.
5529        e.set_content("héllo");
5530        e.mouse_click_doc(0, 99);
5531        assert_eq!(e.cursor(), (0, 4));
5532    }
5533
5534    #[test]
5535    fn mouse_click_doc_insert_mode_allows_one_past_eol() {
5536        let mut e = Editor::new(
5537            hjkl_buffer::Buffer::new(),
5538            crate::types::DefaultHost::new(),
5539            crate::types::Options::default(),
5540        );
5541        e.set_content("hello");
5542        e.enter_insert_i(1);
5543        // Insert mode allows the one-past-EOL position (col=5 for 5-char
5544        // line) — that's the canonical insert-here sentinel.
5545        e.mouse_click_doc(0, 99);
5546        assert_eq!(e.cursor(), (0, 5));
5547    }
5548
5549    #[test]
5550    fn mouse_click_doc_resets_sticky_col() {
5551        let mut e = Editor::new(
5552            hjkl_buffer::Buffer::new(),
5553            crate::types::DefaultHost::new(),
5554            crate::types::Options::default(),
5555        );
5556        e.set_content("aaaaa\nbb\naaaaa");
5557        // Pretend a previous keyboard motion put intended col at 4 (e.g.
5558        // user navigated $ on row 0).
5559        e.sticky_col = Some(4);
5560        // Click on row 1, col 1 (the second 'b' on a short line).
5561        e.mouse_click_doc(1, 1);
5562        assert_eq!(e.cursor(), (1, 1));
5563        assert_eq!(
5564            e.sticky_col,
5565            Some(1),
5566            "click must reset sticky_col so a subsequent j/k uses the clicked column \
5567             as the intended visual column (not the previous keyboard-tracked col)"
5568        );
5569    }
5570
5571    #[test]
5572    fn mouse_click_doc_exits_visual_mode() {
5573        use crate::VimMode;
5574        let mut e = Editor::new(
5575            hjkl_buffer::Buffer::new(),
5576            crate::types::DefaultHost::new(),
5577            crate::types::Options::default(),
5578        );
5579        e.set_content("hello");
5580        e.enter_visual_char();
5581        assert_eq!(e.vim_mode(), VimMode::Visual);
5582        e.mouse_click_doc(0, 2);
5583        assert_eq!(e.vim_mode(), VimMode::Normal);
5584        assert_eq!(e.cursor(), (0, 2));
5585    }
5586
5587    #[test]
5588    fn set_cursor_doc_clamps_past_last_row() {
5589        let mut e = Editor::new(
5590            hjkl_buffer::Buffer::new(),
5591            crate::types::DefaultHost::new(),
5592            crate::types::Options::default(),
5593        );
5594        e.set_content("one\ntwo");
5595        // doc has 2 rows (0 and 1); row 99 clamps to 1.
5596        e.set_cursor_doc(99, 0);
5597        assert_eq!(e.cursor(), (1, 0));
5598    }
5599
5600    #[test]
5601    fn mouse_begin_drag_enters_visual_char() {
5602        use crate::VimMode;
5603        let mut e = Editor::new(
5604            hjkl_buffer::Buffer::new(),
5605            crate::types::DefaultHost::new(),
5606            crate::types::Options::default(),
5607        );
5608        e.set_content("hello");
5609        e.mouse_begin_drag();
5610        assert_eq!(e.vim_mode(), VimMode::Visual);
5611    }
5612
5613    #[test]
5614    fn mouse_extend_drag_doc_moves_cursor_leaving_visual_anchor() {
5615        use crate::VimMode;
5616        let mut e = Editor::new(
5617            hjkl_buffer::Buffer::new(),
5618            crate::types::DefaultHost::new(),
5619            crate::types::Options::default(),
5620        );
5621        e.set_content("hello world");
5622        e.mouse_begin_drag(); // anchor at (0,0)
5623        e.mouse_extend_drag_doc(0, 5);
5624        assert_eq!(e.vim_mode(), VimMode::Visual);
5625        assert_eq!(e.cursor(), (0, 5));
5626    }
5627
5628    // ── Patch B (0.0.29): Host trait wired into Editor ──
5629
5630    #[test]
5631    fn host_clipboard_round_trip_via_default_host() {
5632        // DefaultHost stores write_clipboard in-memory; read_clipboard
5633        // returns the most recent payload.
5634        let mut e = Editor::new(
5635            hjkl_buffer::Buffer::new(),
5636            crate::types::DefaultHost::new(),
5637            crate::types::Options::default(),
5638        );
5639        e.host_mut().write_clipboard("payload".to_string());
5640        assert_eq!(e.host_mut().read_clipboard().as_deref(), Some("payload"));
5641    }
5642
5643    // ── ContentEdit emission ─────────────────────────────────────────
5644
5645    fn fresh_editor(initial: &str) -> Editor {
5646        let buffer = hjkl_buffer::Buffer::from_str(initial);
5647        Editor::new(
5648            buffer,
5649            crate::types::DefaultHost::new(),
5650            crate::types::Options::default(),
5651        )
5652    }
5653
5654    #[test]
5655    fn content_edit_insert_char_at_origin() {
5656        let mut e = fresh_editor("");
5657        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
5658            at: hjkl_buffer::Position::new(0, 0),
5659            ch: 'a',
5660        });
5661        let edits = e.take_content_edits();
5662        assert_eq!(edits.len(), 1);
5663        let ce = &edits[0];
5664        assert_eq!(ce.start_byte, 0);
5665        assert_eq!(ce.old_end_byte, 0);
5666        assert_eq!(ce.new_end_byte, 1);
5667        assert_eq!(ce.start_position, (0, 0));
5668        assert_eq!(ce.old_end_position, (0, 0));
5669        assert_eq!(ce.new_end_position, (0, 1));
5670    }
5671
5672    #[test]
5673    fn content_edit_insert_str_multiline() {
5674        // Buffer "x\ny" — insert "ab\ncd" at end of row 0.
5675        let mut e = fresh_editor("x\ny");
5676        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertStr {
5677            at: hjkl_buffer::Position::new(0, 1),
5678            text: "ab\ncd".into(),
5679        });
5680        let edits = e.take_content_edits();
5681        assert_eq!(edits.len(), 1);
5682        let ce = &edits[0];
5683        assert_eq!(ce.start_byte, 1);
5684        assert_eq!(ce.old_end_byte, 1);
5685        assert_eq!(ce.new_end_byte, 1 + 5);
5686        assert_eq!(ce.start_position, (0, 1));
5687        // Insertion contains one '\n', so row+1, col = bytes after last '\n' = 2.
5688        assert_eq!(ce.new_end_position, (1, 2));
5689    }
5690
5691    #[test]
5692    fn content_edit_delete_range_charwise() {
5693        // "abcdef" — delete chars 1..4 ("bcd").
5694        let mut e = fresh_editor("abcdef");
5695        let _ = e.mutate_edit(hjkl_buffer::Edit::DeleteRange {
5696            start: hjkl_buffer::Position::new(0, 1),
5697            end: hjkl_buffer::Position::new(0, 4),
5698            kind: hjkl_buffer::MotionKind::Char,
5699        });
5700        let edits = e.take_content_edits();
5701        assert_eq!(edits.len(), 1);
5702        let ce = &edits[0];
5703        assert_eq!(ce.start_byte, 1);
5704        assert_eq!(ce.old_end_byte, 4);
5705        assert_eq!(ce.new_end_byte, 1);
5706        assert!(ce.old_end_byte > ce.new_end_byte);
5707    }
5708
5709    #[test]
5710    fn content_edit_set_content_resets() {
5711        let mut e = fresh_editor("foo");
5712        let _ = e.mutate_edit(hjkl_buffer::Edit::InsertChar {
5713            at: hjkl_buffer::Position::new(0, 0),
5714            ch: 'X',
5715        });
5716        // set_content should clear queued edits and raise the reset
5717        // flag on the next take_content_reset.
5718        e.set_content("brand new");
5719        assert!(e.take_content_reset());
5720        // Subsequent call clears the flag.
5721        assert!(!e.take_content_reset());
5722        // Edits cleared on reset.
5723        assert!(e.take_content_edits().is_empty());
5724    }
5725
5726    #[test]
5727    fn content_edit_multiple_replaces_in_order() {
5728        // Three Replace edits applied left-to-right (mimics the
5729        // substitute path's per-match Replace fan-out). Verify each
5730        // mutation queues exactly one ContentEdit and they're drained
5731        // in source-order with structurally valid byte spans.
5732        let mut e = fresh_editor("xax xbx xcx");
5733        let _ = e.take_content_edits();
5734        let _ = e.take_content_reset();
5735        // Replace each "x" with "yy", left to right. After each replace,
5736        // the next match's char-col shifts by +1 (since "yy" is 1 char
5737        // longer than "x" but they're both ASCII so byte = char here).
5738        let positions = [(0usize, 0usize), (0, 4), (0, 8)];
5739        for (row, col) in positions {
5740            let _ = e.mutate_edit(hjkl_buffer::Edit::Replace {
5741                start: hjkl_buffer::Position::new(row, col),
5742                end: hjkl_buffer::Position::new(row, col + 1),
5743                with: "yy".into(),
5744            });
5745        }
5746        let edits = e.take_content_edits();
5747        assert_eq!(edits.len(), 3);
5748        for ce in &edits {
5749            assert!(ce.start_byte <= ce.old_end_byte);
5750            assert!(ce.start_byte <= ce.new_end_byte);
5751        }
5752        // Document order.
5753        for w in edits.windows(2) {
5754            assert!(w[0].start_byte <= w[1].start_byte);
5755        }
5756    }
5757
5758    #[test]
5759    fn replace_char_at_replaces_single_char_under_cursor() {
5760        // Matches vim's `rx` semantics: replace char under cursor.
5761        let mut e = fresh_editor("abc");
5762        e.jump_cursor(0, 1); // cursor on 'b'
5763        e.replace_char_at('X', 1);
5764        let got = e.content();
5765        let got = got.trim_end_matches('\n');
5766        assert_eq!(
5767            got, "aXc",
5768            "replace_char_at(X, 1) must replace 'b' with 'X'"
5769        );
5770        // Cursor stays on the replaced char.
5771        assert_eq!(e.cursor(), (0, 1));
5772    }
5773
5774    #[test]
5775    fn replace_char_at_count_replaces_multiple_chars() {
5776        // `3rx` in vim replaces 3 chars starting at cursor.
5777        let mut e = fresh_editor("abcde");
5778        e.jump_cursor(0, 0);
5779        e.replace_char_at('Z', 3);
5780        let got = e.content();
5781        let got = got.trim_end_matches('\n');
5782        assert_eq!(
5783            got, "ZZZde",
5784            "replace_char_at(Z, 3) must replace first 3 chars"
5785        );
5786    }
5787
5788    #[test]
5789    fn find_char_method_moves_to_target() {
5790        // buffer "abcabc", cursor (0,0), f<c> → cursor (0,2).
5791        let mut e = fresh_editor("abcabc");
5792        e.jump_cursor(0, 0);
5793        e.find_char('c', true, false, 1);
5794        assert_eq!(
5795            e.cursor(),
5796            (0, 2),
5797            "find_char('c', forward=true, till=false, count=1) must land on 'c' at col 2"
5798        );
5799    }
5800
5801    // ── after_g unit tests (Phase 2b-ii) ────────────────────────────────────
5802
5803    #[test]
5804    fn after_g_gg_jumps_to_top() {
5805        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
5806        let mut e = fresh_editor(&content);
5807        e.jump_cursor(15, 0);
5808        e.after_g('g', 1);
5809        assert_eq!(e.cursor().0, 0, "gg must move cursor to row 0");
5810    }
5811
5812    #[test]
5813    fn after_g_gg_with_count_jumps_line() {
5814        // 5gg → row 4 (0-indexed).
5815        let content: String = (0..20).map(|i| format!("line {i}\n")).collect();
5816        let mut e = fresh_editor(&content);
5817        e.jump_cursor(0, 0);
5818        e.after_g('g', 5);
5819        assert_eq!(e.cursor().0, 4, "5gg must land on row 4");
5820    }
5821
5822    #[test]
5823    fn after_g_gj_moves_down() {
5824        let mut e = fresh_editor("line0\nline1\nline2\n");
5825        e.jump_cursor(0, 0);
5826        e.after_g('j', 1);
5827        assert_eq!(e.cursor().0, 1, "gj must move down one display row");
5828    }
5829
5830    #[test]
5831    fn after_g_gu_sets_operator_pending() {
5832        // gU enters operator-pending with Uppercase op; next key applies it.
5833        let mut e = fresh_editor("hello\n");
5834        e.after_g('U', 1);
5835        // The engine should now be chord-pending (Pending::Op set).
5836        assert!(
5837            e.is_chord_pending(),
5838            "gU must set engine chord-pending (Pending::Op)"
5839        );
5840    }
5841
5842    #[test]
5843    fn after_g_g_star_searches_forward_non_whole_word() {
5844        // g* on word "foo" in "foobar" should find the match.
5845        let mut e = fresh_editor("foo foobar\n");
5846        e.jump_cursor(0, 0); // cursor on 'f' of "foo"
5847        e.after_g('*', 1);
5848        // After g* the cursor should have moved (ScreenDown motion is
5849        // not applicable here; WordAtCursor forward moves to next match).
5850        // At minimum: no panic and mode stays Normal.
5851        assert_eq!(e.vim_mode(), VimMode::Normal, "g* must stay in Normal mode");
5852    }
5853
5854    // ── apply_motion controller tests (Phase 3a) ────────────────────────────
5855
5856    #[test]
5857    fn apply_motion_char_left_moves_cursor() {
5858        let mut e = fresh_editor("hello\n");
5859        e.jump_cursor(0, 3);
5860        e.apply_motion(crate::MotionKind::CharLeft, 1);
5861        assert_eq!(e.cursor(), (0, 2), "CharLeft moves one col left");
5862    }
5863
5864    #[test]
5865    fn apply_motion_char_left_clamps_at_col_zero() {
5866        let mut e = fresh_editor("hello\n");
5867        e.jump_cursor(0, 0);
5868        e.apply_motion(crate::MotionKind::CharLeft, 1);
5869        assert_eq!(e.cursor(), (0, 0), "CharLeft at col 0 must not wrap");
5870    }
5871
5872    #[test]
5873    fn apply_motion_char_left_with_count() {
5874        let mut e = fresh_editor("hello\n");
5875        e.jump_cursor(0, 4);
5876        e.apply_motion(crate::MotionKind::CharLeft, 3);
5877        assert_eq!(e.cursor(), (0, 1), "CharLeft count=3 moves three cols left");
5878    }
5879
5880    #[test]
5881    fn apply_motion_char_right_moves_cursor() {
5882        let mut e = fresh_editor("hello\n");
5883        e.jump_cursor(0, 0);
5884        e.apply_motion(crate::MotionKind::CharRight, 1);
5885        assert_eq!(e.cursor(), (0, 1), "CharRight moves one col right");
5886    }
5887
5888    #[test]
5889    fn apply_motion_char_right_clamps_at_last_char() {
5890        let mut e = fresh_editor("hello\n");
5891        // "hello" has chars at 0..=4; normal mode clamps at 4.
5892        e.jump_cursor(0, 4);
5893        e.apply_motion(crate::MotionKind::CharRight, 1);
5894        assert_eq!(
5895            e.cursor(),
5896            (0, 4),
5897            "CharRight at end must not go past last char"
5898        );
5899    }
5900
5901    #[test]
5902    fn apply_motion_line_down_moves_cursor() {
5903        let mut e = fresh_editor("line0\nline1\nline2\n");
5904        e.jump_cursor(0, 0);
5905        e.apply_motion(crate::MotionKind::LineDown, 1);
5906        assert_eq!(e.cursor().0, 1, "LineDown moves one row down");
5907    }
5908
5909    #[test]
5910    fn apply_motion_line_down_with_count() {
5911        let mut e = fresh_editor("line0\nline1\nline2\n");
5912        e.jump_cursor(0, 0);
5913        e.apply_motion(crate::MotionKind::LineDown, 2);
5914        assert_eq!(e.cursor().0, 2, "LineDown count=2 moves two rows down");
5915    }
5916
5917    #[test]
5918    fn apply_motion_line_up_moves_cursor() {
5919        let mut e = fresh_editor("line0\nline1\nline2\n");
5920        e.jump_cursor(2, 0);
5921        e.apply_motion(crate::MotionKind::LineUp, 1);
5922        assert_eq!(e.cursor().0, 1, "LineUp moves one row up");
5923    }
5924
5925    #[test]
5926    fn apply_motion_line_up_clamps_at_top() {
5927        let mut e = fresh_editor("line0\nline1\n");
5928        e.jump_cursor(0, 0);
5929        e.apply_motion(crate::MotionKind::LineUp, 1);
5930        assert_eq!(e.cursor().0, 0, "LineUp at top must not go negative");
5931    }
5932
5933    #[test]
5934    fn apply_motion_first_non_blank_down_moves_and_lands_on_non_blank() {
5935        // Line 0: "  hello" (indent 2), line 1: "  world" (indent 2).
5936        let mut e = fresh_editor("  hello\n  world\n");
5937        e.jump_cursor(0, 0);
5938        e.apply_motion(crate::MotionKind::FirstNonBlankDown, 1);
5939        assert_eq!(e.cursor().0, 1, "FirstNonBlankDown must move to next row");
5940        assert_eq!(
5941            e.cursor().1,
5942            2,
5943            "FirstNonBlankDown must land on first non-blank col"
5944        );
5945    }
5946
5947    #[test]
5948    fn apply_motion_first_non_blank_up_moves_and_lands_on_non_blank() {
5949        let mut e = fresh_editor("  hello\n  world\n");
5950        e.jump_cursor(1, 4);
5951        e.apply_motion(crate::MotionKind::FirstNonBlankUp, 1);
5952        assert_eq!(e.cursor().0, 0, "FirstNonBlankUp must move to prev row");
5953        assert_eq!(
5954            e.cursor().1,
5955            2,
5956            "FirstNonBlankUp must land on first non-blank col"
5957        );
5958    }
5959
5960    #[test]
5961    fn apply_motion_count_zero_treated_as_one() {
5962        // count=0 must be normalised to 1 (count.max(1) in apply_motion_kind).
5963        let mut e = fresh_editor("hello\n");
5964        e.jump_cursor(0, 3);
5965        e.apply_motion(crate::MotionKind::CharLeft, 0);
5966        assert_eq!(e.cursor(), (0, 2), "count=0 treated as 1 for CharLeft");
5967    }
5968
5969    // ── apply_motion controller tests (Phase 3b) — word motions ─────────────
5970
5971    #[test]
5972    fn apply_motion_word_forward_moves_to_next_word() {
5973        // "hello world\n": 'w' from col 0 lands on 'w' of "world" at col 6.
5974        let mut e = fresh_editor("hello world\n");
5975        e.jump_cursor(0, 0);
5976        e.apply_motion(crate::MotionKind::WordForward, 1);
5977        assert_eq!(
5978            e.cursor(),
5979            (0, 6),
5980            "WordForward moves to start of next word"
5981        );
5982    }
5983
5984    #[test]
5985    fn apply_motion_word_forward_with_count() {
5986        // "one two three\n": 2w from col 0 → start of "three" at col 8.
5987        let mut e = fresh_editor("one two three\n");
5988        e.jump_cursor(0, 0);
5989        e.apply_motion(crate::MotionKind::WordForward, 2);
5990        assert_eq!(e.cursor(), (0, 8), "WordForward count=2 skips two words");
5991    }
5992
5993    #[test]
5994    fn apply_motion_big_word_forward_moves_to_next_big_word() {
5995        // "foo.bar baz\n": W from col 0 skips entire "foo.bar" (one WORD) to 'b' at col 8.
5996        let mut e = fresh_editor("foo.bar baz\n");
5997        e.jump_cursor(0, 0);
5998        e.apply_motion(crate::MotionKind::BigWordForward, 1);
5999        assert_eq!(e.cursor(), (0, 8), "BigWordForward skips the whole WORD");
6000    }
6001
6002    #[test]
6003    fn apply_motion_big_word_forward_with_count() {
6004        // "aa bb cc\n": 2W from col 0 → start of "cc" at col 6.
6005        let mut e = fresh_editor("aa bb cc\n");
6006        e.jump_cursor(0, 0);
6007        e.apply_motion(crate::MotionKind::BigWordForward, 2);
6008        assert_eq!(e.cursor(), (0, 6), "BigWordForward count=2 skips two WORDs");
6009    }
6010
6011    #[test]
6012    fn apply_motion_word_backward_moves_to_prev_word() {
6013        // "hello world\n": 'b' from col 6 ('w') lands back at col 0 ('h').
6014        let mut e = fresh_editor("hello world\n");
6015        e.jump_cursor(0, 6);
6016        e.apply_motion(crate::MotionKind::WordBackward, 1);
6017        assert_eq!(
6018            e.cursor(),
6019            (0, 0),
6020            "WordBackward moves to start of prev word"
6021        );
6022    }
6023
6024    #[test]
6025    fn apply_motion_word_backward_with_count() {
6026        // "one two three\n": 2b from col 8 ('t' of "three") → col 0 ('o' of "one").
6027        let mut e = fresh_editor("one two three\n");
6028        e.jump_cursor(0, 8);
6029        e.apply_motion(crate::MotionKind::WordBackward, 2);
6030        assert_eq!(
6031            e.cursor(),
6032            (0, 0),
6033            "WordBackward count=2 skips two words back"
6034        );
6035    }
6036
6037    #[test]
6038    fn apply_motion_big_word_backward_moves_to_prev_big_word() {
6039        // "foo.bar baz\n": B from col 8 ('b' of "baz") → col 0 (start of "foo.bar" WORD).
6040        let mut e = fresh_editor("foo.bar baz\n");
6041        e.jump_cursor(0, 8);
6042        e.apply_motion(crate::MotionKind::BigWordBackward, 1);
6043        assert_eq!(
6044            e.cursor(),
6045            (0, 0),
6046            "BigWordBackward jumps to start of prev WORD"
6047        );
6048    }
6049
6050    #[test]
6051    fn apply_motion_big_word_backward_with_count() {
6052        // "aa bb cc\n": 2B from col 6 ('c') → col 0 ('a').
6053        let mut e = fresh_editor("aa bb cc\n");
6054        e.jump_cursor(0, 6);
6055        e.apply_motion(crate::MotionKind::BigWordBackward, 2);
6056        assert_eq!(
6057            e.cursor(),
6058            (0, 0),
6059            "BigWordBackward count=2 skips two WORDs back"
6060        );
6061    }
6062
6063    #[test]
6064    fn apply_motion_word_end_moves_to_end_of_word() {
6065        // "hello world\n": 'e' from col 0 lands on 'o' of "hello" at col 4.
6066        let mut e = fresh_editor("hello world\n");
6067        e.jump_cursor(0, 0);
6068        e.apply_motion(crate::MotionKind::WordEnd, 1);
6069        assert_eq!(e.cursor(), (0, 4), "WordEnd moves to end of current word");
6070    }
6071
6072    #[test]
6073    fn apply_motion_word_end_with_count() {
6074        // "one two three\n": 2e from col 0 → end of "two" at col 6.
6075        let mut e = fresh_editor("one two three\n");
6076        e.jump_cursor(0, 0);
6077        e.apply_motion(crate::MotionKind::WordEnd, 2);
6078        assert_eq!(
6079            e.cursor(),
6080            (0, 6),
6081            "WordEnd count=2 lands on end of second word"
6082        );
6083    }
6084
6085    #[test]
6086    fn apply_motion_big_word_end_moves_to_end_of_big_word() {
6087        // "foo.bar baz\n": E from col 0 → end of "foo.bar" WORD at col 6.
6088        let mut e = fresh_editor("foo.bar baz\n");
6089        e.jump_cursor(0, 0);
6090        e.apply_motion(crate::MotionKind::BigWordEnd, 1);
6091        assert_eq!(e.cursor(), (0, 6), "BigWordEnd lands on end of WORD");
6092    }
6093
6094    #[test]
6095    fn apply_motion_big_word_end_with_count() {
6096        // "aa bb cc\n": 2E from col 0 → end of "bb" at col 4.
6097        let mut e = fresh_editor("aa bb cc\n");
6098        e.jump_cursor(0, 0);
6099        e.apply_motion(crate::MotionKind::BigWordEnd, 2);
6100        assert_eq!(
6101            e.cursor(),
6102            (0, 4),
6103            "BigWordEnd count=2 lands on end of second WORD"
6104        );
6105    }
6106
6107    // ── apply_motion controller tests (Phase 3c) — line-anchor motions ────────
6108
6109    #[test]
6110    fn apply_motion_line_start_lands_at_col_zero() {
6111        // "  foo bar  \n": `0` from col 5 → col 0 unconditionally.
6112        let mut e = fresh_editor("  foo bar  \n");
6113        e.jump_cursor(0, 5);
6114        e.apply_motion(crate::MotionKind::LineStart, 1);
6115        assert_eq!(e.cursor(), (0, 0), "LineStart lands at col 0");
6116    }
6117
6118    #[test]
6119    fn apply_motion_line_start_from_beginning_stays_at_col_zero() {
6120        // Already at col 0 — motion is a no-op but must not panic.
6121        let mut e = fresh_editor("  foo bar  \n");
6122        e.jump_cursor(0, 0);
6123        e.apply_motion(crate::MotionKind::LineStart, 1);
6124        assert_eq!(e.cursor(), (0, 0), "LineStart from col 0 stays at col 0");
6125    }
6126
6127    #[test]
6128    fn apply_motion_first_non_blank_lands_on_first_non_blank() {
6129        // "  foo bar  \n": `^` from col 0 → col 2 ('f').
6130        let mut e = fresh_editor("  foo bar  \n");
6131        e.jump_cursor(0, 0);
6132        e.apply_motion(crate::MotionKind::FirstNonBlank, 1);
6133        assert_eq!(
6134            e.cursor(),
6135            (0, 2),
6136            "FirstNonBlank lands on first non-blank char"
6137        );
6138    }
6139
6140    #[test]
6141    fn apply_motion_first_non_blank_on_blank_line_lands_at_zero() {
6142        // "   \n": all whitespace — `^` must land at col 0.
6143        let mut e = fresh_editor("   \n");
6144        e.jump_cursor(0, 2);
6145        e.apply_motion(crate::MotionKind::FirstNonBlank, 1);
6146        assert_eq!(
6147            e.cursor(),
6148            (0, 0),
6149            "FirstNonBlank on blank line stays at col 0"
6150        );
6151    }
6152
6153    #[test]
6154    fn apply_motion_line_end_lands_on_last_char() {
6155        // "  foo bar  \n": last char is the second space at col 10.
6156        let mut e = fresh_editor("  foo bar  \n");
6157        e.jump_cursor(0, 0);
6158        e.apply_motion(crate::MotionKind::LineEnd, 1);
6159        assert_eq!(e.cursor(), (0, 10), "LineEnd lands on last char of line");
6160    }
6161
6162    #[test]
6163    fn apply_motion_line_end_on_empty_line_stays_at_zero() {
6164        // "\n": empty line — `$` must stay at col 0.
6165        let mut e = fresh_editor("\n");
6166        e.jump_cursor(0, 0);
6167        e.apply_motion(crate::MotionKind::LineEnd, 1);
6168        assert_eq!(e.cursor(), (0, 0), "LineEnd on empty line stays at col 0");
6169    }
6170
6171    // ── apply_motion controller tests (Phase 3d) — doc-level motion ───────────
6172
6173    #[test]
6174    fn goto_line_count_1_lands_on_last_line() {
6175        // "foo\nbar\nbaz\n": bare `G` (count=1) → last content line (row 2).
6176        // Count convention: apply_motion_kind normalises 1 → execute_motion
6177        // with count=1 → FileBottom arm sees count <= 1 → move_bottom(0) =
6178        // last content row.
6179        let mut e = fresh_editor("foo\nbar\nbaz\n");
6180        e.jump_cursor(0, 0);
6181        e.apply_motion(crate::MotionKind::GotoLine, 1);
6182        assert_eq!(e.cursor(), (2, 0), "bare G lands on last content row");
6183    }
6184
6185    #[test]
6186    fn goto_line_count_5_lands_on_line_5() {
6187        // 6-line buffer (rows 0-5); `5G` → row 4 (1-based line 5).
6188        let mut e = fresh_editor("a\nb\nc\nd\ne\nf\n");
6189        e.jump_cursor(0, 0);
6190        e.apply_motion(crate::MotionKind::GotoLine, 5);
6191        assert_eq!(e.cursor(), (4, 0), "5G lands on row 4 (1-based line 5)");
6192    }
6193
6194    #[test]
6195    fn goto_line_count_past_buffer_clamps_to_last_line() {
6196        // "foo\nbar\nbaz\n": `100G` → last content line (row 2), clamped.
6197        let mut e = fresh_editor("foo\nbar\nbaz\n");
6198        e.jump_cursor(0, 0);
6199        e.apply_motion(crate::MotionKind::GotoLine, 100);
6200        assert_eq!(e.cursor(), (2, 0), "100G clamps to last content row");
6201    }
6202
6203    // ── FindRepeat / FindRepeatReverse controller tests (Phase 3e) ────────────
6204
6205    #[test]
6206    fn find_repeat_after_f_finds_next_occurrence() {
6207        // "abcabc", cursor at (0,0). `fc` lands on (0,2). `;` repeats → (0,5).
6208        let mut e = fresh_editor("abcabc");
6209        e.jump_cursor(0, 0);
6210        e.find_char('c', true, false, 1);
6211        assert_eq!(e.cursor(), (0, 2), "fc must land on first 'c'");
6212        e.apply_motion(crate::MotionKind::FindRepeat, 1);
6213        assert_eq!(
6214            e.cursor(),
6215            (0, 5),
6216            "find_repeat (;) must advance to second 'c'"
6217        );
6218    }
6219
6220    #[test]
6221    fn find_repeat_reverse_after_f_finds_prev_occurrence() {
6222        // "abcabc", cursor at (0,0). `fc` lands on (0,2). `;` → (0,5). `,` back → (0,2).
6223        let mut e = fresh_editor("abcabc");
6224        e.jump_cursor(0, 0);
6225        e.find_char('c', true, false, 1);
6226        assert_eq!(e.cursor(), (0, 2), "fc must land on first 'c'");
6227        e.apply_motion(crate::MotionKind::FindRepeat, 1);
6228        assert_eq!(e.cursor(), (0, 5), "; must advance to second 'c'");
6229        e.apply_motion(crate::MotionKind::FindRepeatReverse, 1);
6230        assert_eq!(
6231            e.cursor(),
6232            (0, 2),
6233            "find_repeat_reverse (,) must go back to first 'c'"
6234        );
6235    }
6236
6237    #[test]
6238    fn find_repeat_with_no_prior_find_is_noop() {
6239        // Fresh editor, no prior find — `;` must not move cursor.
6240        let mut e = fresh_editor("abcabc");
6241        e.jump_cursor(0, 3);
6242        e.apply_motion(crate::MotionKind::FindRepeat, 1);
6243        assert_eq!(
6244            e.cursor(),
6245            (0, 3),
6246            "find_repeat with no prior find must be a no-op"
6247        );
6248    }
6249
6250    #[test]
6251    fn find_repeat_with_count_advances_count_times() {
6252        // "aXaXaX", cursor (0,0). `fX` → (0,1). `3;` → repeats 3× → (0,5).
6253        let mut e = fresh_editor("aXaXaX");
6254        e.jump_cursor(0, 0);
6255        e.find_char('X', true, false, 1);
6256        assert_eq!(e.cursor(), (0, 1), "fX must land on first 'X' at col 1");
6257        e.apply_motion(crate::MotionKind::FindRepeat, 3);
6258        assert_eq!(
6259            e.cursor(),
6260            (0, 5),
6261            "3; must advance 3 times from col 1 to col 5"
6262        );
6263    }
6264
6265    // ── BracketMatch controller tests (Phase 3f) ───────────────────────────────
6266
6267    #[test]
6268    fn bracket_match_jumps_to_matching_close_paren() {
6269        // "(abc)", cursor at (0,0) on `(` — `%` must jump to `)` at (0,4).
6270        let mut e = fresh_editor("(abc)");
6271        e.jump_cursor(0, 0);
6272        e.apply_motion(crate::MotionKind::BracketMatch, 1);
6273        assert_eq!(
6274            e.cursor(),
6275            (0, 4),
6276            "% on '(' must land on matching ')' at col 4"
6277        );
6278    }
6279
6280    #[test]
6281    fn bracket_match_jumps_to_matching_open_paren() {
6282        // "(abc)", cursor at (0,4) on `)` — `%` must jump back to `(` at (0,0).
6283        let mut e = fresh_editor("(abc)");
6284        e.jump_cursor(0, 4);
6285        e.apply_motion(crate::MotionKind::BracketMatch, 1);
6286        assert_eq!(
6287            e.cursor(),
6288            (0, 0),
6289            "% on ')' must land on matching '(' at col 0"
6290        );
6291    }
6292
6293    #[test]
6294    fn bracket_match_with_no_match_on_line_is_noop_or_engine_behaviour() {
6295        // "abcd", cursor at (0,2) — no bracket under cursor; engine returns
6296        // false from matching_bracket, cursor must not move.
6297        let mut e = fresh_editor("abcd");
6298        e.jump_cursor(0, 2);
6299        e.apply_motion(crate::MotionKind::BracketMatch, 1);
6300        assert_eq!(
6301            e.cursor(),
6302            (0, 2),
6303            "% with no bracket under cursor must be a no-op"
6304        );
6305    }
6306
6307    // ── Scroll / viewport motion controller tests (Phase 3g) ──────────────────
6308
6309    /// Helper: build a 20-line buffer, set viewport to rows [5..14] (height=10).
6310    fn fresh_viewport_editor() -> Editor {
6311        let content = many_lines(20);
6312        let mut e = Editor::new(
6313            hjkl_buffer::Buffer::from_str(&content),
6314            crate::types::DefaultHost::new(),
6315            crate::types::Options::default(),
6316        );
6317        // height=10, top_row=5 → visible rows 5..14.
6318        // set_viewport_height stores to the atomic; sync_buffer_from_textarea
6319        // propagates it to host.viewport_mut().height so motion helpers see it.
6320        e.set_viewport_height(10);
6321        e.sync_buffer_from_textarea();
6322        e.host_mut().viewport_mut().top_row = 5;
6323        e
6324    }
6325
6326    #[test]
6327    fn viewport_top_lands_on_first_visible_row() {
6328        // Viewport top=5, height=10. H (count=1) should land on row 5
6329        // (the first visible row, offset = count-1 = 0).
6330        let mut e = fresh_viewport_editor();
6331        e.jump_cursor(10, 0);
6332        e.apply_motion(crate::MotionKind::ViewportTop, 1);
6333        assert_eq!(
6334            e.cursor().0,
6335            5,
6336            "H (count=1) must land on viewport top row (5)"
6337        );
6338    }
6339
6340    #[test]
6341    fn viewport_top_with_count_offsets_down() {
6342        // H with count=3 → viewport top + (3-1) = 5 + 2 = row 7.
6343        let mut e = fresh_viewport_editor();
6344        e.jump_cursor(12, 0);
6345        e.apply_motion(crate::MotionKind::ViewportTop, 3);
6346        assert_eq!(e.cursor().0, 7, "3H must land at viewport top + 2 = row 7");
6347    }
6348
6349    #[test]
6350    fn viewport_middle_lands_on_middle_visible_row() {
6351        // Viewport top=5, height=10 → last visible = 14, mid = 5 + (14-5)/2 = 9.
6352        let mut e = fresh_viewport_editor();
6353        e.jump_cursor(0, 0);
6354        e.apply_motion(crate::MotionKind::ViewportMiddle, 1);
6355        assert_eq!(e.cursor().0, 9, "M must land on middle visible row (9)");
6356    }
6357
6358    #[test]
6359    fn viewport_bottom_lands_on_last_visible_row() {
6360        // L (count=1) → viewport bottom, offset = count-1 = 0 → row 14.
6361        let mut e = fresh_viewport_editor();
6362        e.jump_cursor(5, 0);
6363        e.apply_motion(crate::MotionKind::ViewportBottom, 1);
6364        assert_eq!(
6365            e.cursor().0,
6366            14,
6367            "L (count=1) must land on viewport bottom row (14)"
6368        );
6369    }
6370
6371    #[test]
6372    fn half_page_down_moves_cursor_by_half_window() {
6373        // viewport height=10, so half=5. Cursor at row 0 → row 5 after C-d.
6374        let mut e = Editor::new(
6375            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6376            crate::types::DefaultHost::new(),
6377            crate::types::Options::default(),
6378        );
6379        e.set_viewport_height(10);
6380        e.jump_cursor(0, 0);
6381        e.apply_motion(crate::MotionKind::HalfPageDown, 1);
6382        assert_eq!(
6383            e.cursor().0,
6384            5,
6385            "<C-d> from row 0 with viewport height=10 must land on row 5"
6386        );
6387    }
6388
6389    #[test]
6390    fn half_page_up_moves_cursor_by_half_window_reverse() {
6391        // viewport height=10, half=5. Cursor at row 10 → row 5 after C-u.
6392        let mut e = Editor::new(
6393            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6394            crate::types::DefaultHost::new(),
6395            crate::types::Options::default(),
6396        );
6397        e.set_viewport_height(10);
6398        e.jump_cursor(10, 0);
6399        e.apply_motion(crate::MotionKind::HalfPageUp, 1);
6400        assert_eq!(
6401            e.cursor().0,
6402            5,
6403            "<C-u> from row 10 with viewport height=10 must land on row 5"
6404        );
6405    }
6406
6407    #[test]
6408    fn full_page_down_moves_cursor_by_full_window() {
6409        // viewport height=10, full = 10 - 2 = 8. Cursor at row 0 → row 8.
6410        let mut e = Editor::new(
6411            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6412            crate::types::DefaultHost::new(),
6413            crate::types::Options::default(),
6414        );
6415        e.set_viewport_height(10);
6416        e.jump_cursor(0, 0);
6417        e.apply_motion(crate::MotionKind::FullPageDown, 1);
6418        assert_eq!(
6419            e.cursor().0,
6420            8,
6421            "<C-f> from row 0 with viewport height=10 must land on row 8"
6422        );
6423    }
6424
6425    #[test]
6426    fn full_page_up_moves_cursor_by_full_window_reverse() {
6427        // viewport height=10, full=8. Cursor at row 10 → row 2.
6428        let mut e = Editor::new(
6429            hjkl_buffer::Buffer::from_str(&many_lines(30)),
6430            crate::types::DefaultHost::new(),
6431            crate::types::Options::default(),
6432        );
6433        e.set_viewport_height(10);
6434        e.jump_cursor(10, 0);
6435        e.apply_motion(crate::MotionKind::FullPageUp, 1);
6436        assert_eq!(
6437            e.cursor().0,
6438            2,
6439            "<C-b> from row 10 with viewport height=10 must land on row 2"
6440        );
6441    }
6442
6443    // ── set_mark_at_cursor unit tests ─────────────────────────────────────────
6444
6445    #[test]
6446    fn set_mark_at_cursor_alphabetic_records() {
6447        // `ma` at (0, 2) — mark 'a' must store (0, 2).
6448        let mut e = fresh_editor("hello");
6449        e.jump_cursor(0, 2);
6450        e.set_mark_at_cursor('a');
6451        assert_eq!(
6452            e.mark('a'),
6453            Some((0, 2)),
6454            "mark 'a' must record current pos"
6455        );
6456    }
6457
6458    #[test]
6459    fn set_mark_at_cursor_invalid_char_no_op() {
6460        // Invalid chars (digits, special) must not store a mark.
6461        let mut e = fresh_editor("hello");
6462        e.jump_cursor(0, 1);
6463        e.set_mark_at_cursor('1'); // digit — not alphanumeric in vim mark sense
6464        assert_eq!(e.mark('1'), None, "digit mark must be a no-op");
6465        e.set_mark_at_cursor('['); // special — only goto uses '[', not set_mark
6466        assert_eq!(
6467            e.mark('['),
6468            None,
6469            "bracket char must be a no-op for set_mark"
6470        );
6471    }
6472
6473    #[test]
6474    fn set_mark_at_cursor_special_left_bracket() {
6475        // Confirm '[' is NOT stored by set_mark_at_cursor (vim's `m[` is invalid).
6476        // The `[` mark is only set automatically by operator paths, not `m[`.
6477        let mut e = fresh_editor("hello");
6478        e.jump_cursor(0, 3);
6479        e.set_mark_at_cursor('[');
6480        assert_eq!(
6481            e.mark('['),
6482            None,
6483            "set_mark_at_cursor must reject '[' (vim: m[ is invalid)"
6484        );
6485    }
6486
6487    // ── goto_mark_line unit tests ─────────────────────────────────────────────
6488
6489    #[test]
6490    fn goto_mark_line_jumps_to_first_non_blank() {
6491        // Set mark 'a' at (1, 3), then jump back to (0, 0).
6492        // `'a` (linewise) must land on row 1, first non-blank column.
6493        let mut e = fresh_editor("hello\n  world\n");
6494        e.jump_cursor(1, 3);
6495        e.set_mark_at_cursor('a');
6496        e.jump_cursor(0, 0);
6497        e.goto_mark_line('a');
6498        assert_eq!(e.cursor().0, 1, "goto_mark_line must jump to mark row");
6499        // "  world" — first non-blank is col 2.
6500        assert_eq!(
6501            e.cursor().1,
6502            2,
6503            "goto_mark_line must land on first non-blank column"
6504        );
6505    }
6506
6507    #[test]
6508    fn goto_mark_line_unset_mark_no_op() {
6509        // Jumping to an unset mark must not move the cursor.
6510        let mut e = fresh_editor("hello\nworld\n");
6511        e.jump_cursor(1, 2);
6512        e.goto_mark_line('z'); // 'z' not set
6513        assert_eq!(e.cursor(), (1, 2), "unset mark jump must be a no-op");
6514    }
6515
6516    #[test]
6517    fn goto_mark_line_invalid_char_no_op() {
6518        // '!' is not a valid mark char — must not move cursor.
6519        let mut e = fresh_editor("hello\nworld\n");
6520        e.jump_cursor(0, 0);
6521        e.goto_mark_line('!');
6522        assert_eq!(e.cursor(), (0, 0), "invalid mark char must be a no-op");
6523    }
6524
6525    // ── goto_mark_char unit tests ─────────────────────────────────────────────
6526
6527    #[test]
6528    fn goto_mark_char_jumps_to_exact_pos() {
6529        // Set mark 'b' at (1, 4), then jump back to (0, 0).
6530        // `` `b `` (charwise) must land on (1, 4) exactly.
6531        let mut e = fresh_editor("hello\nworld\n");
6532        e.jump_cursor(1, 4);
6533        e.set_mark_at_cursor('b');
6534        e.jump_cursor(0, 0);
6535        e.goto_mark_char('b');
6536        assert_eq!(
6537            e.cursor(),
6538            (1, 4),
6539            "goto_mark_char must jump to exact mark position"
6540        );
6541    }
6542
6543    #[test]
6544    fn goto_mark_char_unset_mark_no_op() {
6545        // Jumping to an unset mark must not move the cursor.
6546        let mut e = fresh_editor("hello\nworld\n");
6547        e.jump_cursor(1, 1);
6548        e.goto_mark_char('x'); // 'x' not set
6549        assert_eq!(
6550            e.cursor(),
6551            (1, 1),
6552            "unset charwise mark jump must be a no-op"
6553        );
6554    }
6555
6556    #[test]
6557    fn goto_mark_char_invalid_char_no_op() {
6558        // '#' is not a valid mark char — must not move cursor.
6559        let mut e = fresh_editor("hello\nworld\n");
6560        e.jump_cursor(0, 2);
6561        e.goto_mark_char('#');
6562        assert_eq!(
6563            e.cursor(),
6564            (0, 2),
6565            "invalid charwise mark char must be a no-op"
6566        );
6567    }
6568
6569    // ── Macro controller API tests (Phase 5b) ─────────────────────────────────
6570
6571    #[test]
6572    fn start_macro_record_records_register() {
6573        let mut e = fresh_editor("hello");
6574        assert!(!e.is_recording_macro());
6575        e.start_macro_record('a');
6576        assert!(e.is_recording_macro());
6577        assert_eq!(e.recording_register(), Some('a'));
6578    }
6579
6580    #[test]
6581    fn start_macro_record_capital_seeds_existing() {
6582        // `qa` records "h", stop. Then `qA` should seed from existing 'a' reg.
6583        let mut e = fresh_editor("hello");
6584        e.start_macro_record('a');
6585        e.record_input(crate::input::Input {
6586            key: crate::input::Key::Char('h'),
6587            ..Default::default()
6588        });
6589        e.stop_macro_record();
6590        // Start capital 'A' — should seed from existing 'a' register.
6591        e.start_macro_record('A');
6592        // recording_keys should now contain 1 input (the seeded 'h').
6593        assert_eq!(
6594            e.vim.recording_keys.len(),
6595            1,
6596            "capital record must seed from existing lowercase reg"
6597        );
6598    }
6599
6600    #[test]
6601    fn stop_macro_record_writes_register() {
6602        let mut e = fresh_editor("hello");
6603        e.start_macro_record('a');
6604        e.record_input(crate::input::Input {
6605            key: crate::input::Key::Char('h'),
6606            ..Default::default()
6607        });
6608        e.record_input(crate::input::Input {
6609            key: crate::input::Key::Char('l'),
6610            ..Default::default()
6611        });
6612        e.stop_macro_record();
6613        assert!(!e.is_recording_macro());
6614        // Register 'a' should contain "hl".
6615        let text = e
6616            .registers()
6617            .read('a')
6618            .map(|s| s.text.clone())
6619            .unwrap_or_default();
6620        assert_eq!(
6621            text, "hl",
6622            "stop_macro_record must write encoded keys to register"
6623        );
6624    }
6625
6626    #[test]
6627    fn is_recording_macro_reflects_state() {
6628        let mut e = fresh_editor("hello");
6629        assert!(!e.is_recording_macro());
6630        e.start_macro_record('b');
6631        assert!(e.is_recording_macro());
6632        e.stop_macro_record();
6633        assert!(!e.is_recording_macro());
6634    }
6635
6636    #[test]
6637    fn play_macro_returns_decoded_inputs() {
6638        let mut e = fresh_editor("hello");
6639        // Write "jj" into register 'a'.
6640        e.set_named_register_text('a', "jj".to_string());
6641        let inputs = e.play_macro('a', 1);
6642        assert_eq!(inputs.len(), 2);
6643        assert_eq!(inputs[0].key, crate::input::Key::Char('j'));
6644        assert_eq!(inputs[1].key, crate::input::Key::Char('j'));
6645        assert!(e.is_replaying_macro(), "play_macro must set replaying flag");
6646        e.end_macro_replay();
6647        assert!(!e.is_replaying_macro());
6648    }
6649
6650    #[test]
6651    fn play_macro_at_uses_last_macro() {
6652        let mut e = fresh_editor("hello");
6653        e.set_named_register_text('a', "k".to_string());
6654        // Play 'a' first to set last_macro.
6655        let _ = e.play_macro('a', 1);
6656        e.end_macro_replay();
6657        // Now `@@` should replay 'a' again.
6658        let inputs = e.play_macro('@', 1);
6659        assert_eq!(inputs.len(), 1);
6660        assert_eq!(inputs[0].key, crate::input::Key::Char('k'));
6661        e.end_macro_replay();
6662    }
6663
6664    #[test]
6665    fn play_macro_with_count_repeats() {
6666        let mut e = fresh_editor("hello");
6667        e.set_named_register_text('a', "j".to_string());
6668        let inputs = e.play_macro('a', 3);
6669        assert_eq!(inputs.len(), 3, "3@a must produce 3 inputs");
6670        e.end_macro_replay();
6671    }
6672
6673    #[test]
6674    fn record_input_appends_when_recording() {
6675        let mut e = fresh_editor("hello");
6676        // Not recording: record_input is a no-op.
6677        e.record_input(crate::input::Input {
6678            key: crate::input::Key::Char('j'),
6679            ..Default::default()
6680        });
6681        assert_eq!(e.vim.recording_keys.len(), 0);
6682        // Start recording: record_input appends.
6683        e.start_macro_record('a');
6684        e.record_input(crate::input::Input {
6685            key: crate::input::Key::Char('j'),
6686            ..Default::default()
6687        });
6688        e.record_input(crate::input::Input {
6689            key: crate::input::Key::Char('k'),
6690            ..Default::default()
6691        });
6692        assert_eq!(e.vim.recording_keys.len(), 2);
6693        // During replay: record_input must NOT append.
6694        e.vim.replaying_macro = true;
6695        e.record_input(crate::input::Input {
6696            key: crate::input::Key::Char('l'),
6697            ..Default::default()
6698        });
6699        assert_eq!(
6700            e.vim.recording_keys.len(),
6701            2,
6702            "record_input must skip during replay"
6703        );
6704        e.vim.replaying_macro = false;
6705        e.stop_macro_record();
6706    }
6707
6708    // ── Phase 6.1 insert-mode primitive tests (kryptic-sh/hjkl#87) ────────────
6709
6710    /// Helper: enter insert mode via the public bridge, then call the method under test.
6711    fn enter_insert(e: &mut Editor) {
6712        e.enter_insert_i(1);
6713        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6714    }
6715
6716    #[test]
6717    fn insert_char_basic() {
6718        let mut e = fresh_editor("hello");
6719        enter_insert(&mut e);
6720        e.insert_char('X');
6721        assert_eq!(e.buffer().lines()[0], "Xhello");
6722        assert!(e.take_dirty());
6723    }
6724
6725    #[test]
6726    fn insert_newline_splits_line() {
6727        let mut e = fresh_editor("hello");
6728        // Move to col 3 so we split "hel" | "lo".
6729        e.jump_cursor(0, 3);
6730        enter_insert(&mut e);
6731        e.insert_newline();
6732        let lines = e.buffer().lines().to_vec();
6733        assert_eq!(lines[0], "hel");
6734        assert_eq!(lines[1], "lo");
6735    }
6736
6737    #[test]
6738    fn insert_tab_expandtab_inserts_spaces() {
6739        let mut e = fresh_editor("");
6740        // Default options: expandtab=true, softtabstop=4, tabstop=4.
6741        enter_insert(&mut e);
6742        e.insert_tab();
6743        // At col 0 with sts=4: 4 spaces inserted.
6744        assert_eq!(e.buffer().lines()[0], "    ");
6745    }
6746
6747    #[test]
6748    fn insert_tab_real_tab_when_noexpandtab() {
6749        let opts = crate::types::Options {
6750            expandtab: false,
6751            ..crate::types::Options::default()
6752        };
6753        let mut e = Editor::new(
6754            hjkl_buffer::Buffer::new(),
6755            crate::types::DefaultHost::new(),
6756            opts,
6757        );
6758        e.set_content("");
6759        enter_insert(&mut e);
6760        e.insert_tab();
6761        assert_eq!(e.buffer().lines()[0], "\t");
6762    }
6763
6764    #[test]
6765    fn insert_backspace_single_char() {
6766        // Cursor at col 3 in "hello", backspace deletes 'l'.
6767        let mut e = fresh_editor("hello");
6768        e.jump_cursor(0, 3);
6769        enter_insert(&mut e);
6770        e.insert_backspace();
6771        assert_eq!(e.buffer().lines()[0], "helo");
6772    }
6773
6774    #[test]
6775    fn insert_backspace_softtabstop() {
6776        // With sts=4, expandtab: 4 spaces at col 4 → one backspace deletes all 4.
6777        let mut e = fresh_editor("    hello");
6778        e.jump_cursor(0, 4);
6779        enter_insert(&mut e);
6780        e.insert_backspace();
6781        assert_eq!(e.buffer().lines()[0], "hello");
6782    }
6783
6784    #[test]
6785    fn insert_backspace_join_up() {
6786        // At col 0 on row 1, backspace joins with the previous line.
6787        let mut e = fresh_editor("foo\nbar");
6788        e.jump_cursor(1, 0);
6789        enter_insert(&mut e);
6790        e.insert_backspace();
6791        // Two rows merged into one.
6792        assert_eq!(e.buffer().lines().len(), 1);
6793        assert_eq!(e.buffer().lines()[0], "foobar");
6794    }
6795
6796    #[test]
6797    fn leave_insert_steps_back_col() {
6798        // Esc in insert mode should move the cursor one cell left (vim convention).
6799        let mut e = fresh_editor("hello");
6800        e.jump_cursor(0, 3);
6801        enter_insert(&mut e);
6802        // Type one char so cursor is at col 4, then call leave_insert_to_normal.
6803        e.insert_char('X');
6804        // cursor is now at col 4 (after the inserted 'X').
6805        let pre_col = e.cursor().1;
6806        e.leave_insert_to_normal();
6807        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6808        // Cursor stepped back one.
6809        assert_eq!(e.cursor().1, pre_col - 1);
6810    }
6811
6812    #[test]
6813    fn insert_ctrl_w_word_back() {
6814        // Ctrl-W deletes from cursor back to word start.
6815        // "hello world" — cursor at end of "world" (col 11).
6816        let mut e = fresh_editor("hello world");
6817        // Normal mode clamps cursor to col 10 (last char); jump_cursor doesn't clamp.
6818        e.jump_cursor(0, 11);
6819        enter_insert(&mut e);
6820        e.insert_ctrl_w();
6821        // "world" (5 chars) deleted, leaving "hello ".
6822        assert_eq!(e.buffer().lines()[0], "hello ");
6823    }
6824
6825    #[test]
6826    fn insert_ctrl_u_deletes_to_line_start() {
6827        let mut e = fresh_editor("hello world");
6828        e.jump_cursor(0, 5);
6829        enter_insert(&mut e);
6830        e.insert_ctrl_u();
6831        assert_eq!(e.buffer().lines()[0], " world");
6832    }
6833
6834    #[test]
6835    fn insert_ctrl_h_single_backspace() {
6836        // Ctrl-H is an alias for Backspace in insert mode.
6837        let mut e = fresh_editor("hello");
6838        e.jump_cursor(0, 3);
6839        enter_insert(&mut e);
6840        e.insert_ctrl_h();
6841        assert_eq!(e.buffer().lines()[0], "helo");
6842    }
6843
6844    #[test]
6845    fn insert_ctrl_h_join_up() {
6846        let mut e = fresh_editor("foo\nbar");
6847        e.jump_cursor(1, 0);
6848        enter_insert(&mut e);
6849        e.insert_ctrl_h();
6850        assert_eq!(e.buffer().lines().len(), 1);
6851        assert_eq!(e.buffer().lines()[0], "foobar");
6852    }
6853
6854    #[test]
6855    fn insert_ctrl_t_indents_current_line() {
6856        let mut e = Editor::new(
6857            hjkl_buffer::Buffer::new(),
6858            crate::types::DefaultHost::new(),
6859            crate::types::Options {
6860                shiftwidth: 4,
6861                ..crate::types::Options::default()
6862            },
6863        );
6864        e.set_content("hello");
6865        enter_insert(&mut e);
6866        e.insert_ctrl_t();
6867        assert_eq!(e.buffer().lines()[0], "    hello");
6868    }
6869
6870    #[test]
6871    fn insert_ctrl_d_outdents_current_line() {
6872        let mut e = Editor::new(
6873            hjkl_buffer::Buffer::new(),
6874            crate::types::DefaultHost::new(),
6875            crate::types::Options {
6876                shiftwidth: 4,
6877                ..crate::types::Options::default()
6878            },
6879        );
6880        e.set_content("    hello");
6881        enter_insert(&mut e);
6882        e.insert_ctrl_d();
6883        assert_eq!(e.buffer().lines()[0], "hello");
6884    }
6885
6886    #[test]
6887    fn insert_ctrl_o_arm_sets_one_shot_normal() {
6888        let mut e = fresh_editor("hello");
6889        enter_insert(&mut e);
6890        e.insert_ctrl_o_arm();
6891        // Mode should flip to Normal (one-shot).
6892        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
6893    }
6894
6895    #[test]
6896    fn insert_ctrl_r_arm_sets_pending_register() {
6897        let mut e = fresh_editor("hello");
6898        enter_insert(&mut e);
6899        e.insert_ctrl_r_arm();
6900        // pending register flag set; mode stays Insert.
6901        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
6902        assert!(e.vim.insert_pending_register);
6903    }
6904
6905    #[test]
6906    fn insert_delete_removes_char_under_cursor() {
6907        let mut e = fresh_editor("hello");
6908        e.jump_cursor(0, 2);
6909        enter_insert(&mut e);
6910        e.insert_delete();
6911        assert_eq!(e.buffer().lines()[0], "helo");
6912    }
6913
6914    #[test]
6915    fn insert_delete_joins_lines_at_eol() {
6916        let mut e = fresh_editor("foo\nbar");
6917        // Position at end of row 0 (col 3 = past last char).
6918        e.jump_cursor(0, 3);
6919        enter_insert(&mut e);
6920        e.insert_delete();
6921        assert_eq!(e.buffer().lines().len(), 1);
6922        assert_eq!(e.buffer().lines()[0], "foobar");
6923    }
6924
6925    #[test]
6926    fn insert_arrow_left_moves_cursor() {
6927        let mut e = fresh_editor("hello");
6928        e.jump_cursor(0, 3);
6929        enter_insert(&mut e);
6930        e.insert_arrow(crate::vim::InsertDir::Left);
6931        assert_eq!(e.cursor().1, 2);
6932    }
6933
6934    #[test]
6935    fn insert_arrow_right_moves_cursor() {
6936        let mut e = fresh_editor("hello");
6937        e.jump_cursor(0, 2);
6938        enter_insert(&mut e);
6939        e.insert_arrow(crate::vim::InsertDir::Right);
6940        assert_eq!(e.cursor().1, 3);
6941    }
6942
6943    #[test]
6944    fn insert_arrow_up_moves_cursor() {
6945        let mut e = fresh_editor("foo\nbar");
6946        e.jump_cursor(1, 0);
6947        enter_insert(&mut e);
6948        e.insert_arrow(crate::vim::InsertDir::Up);
6949        assert_eq!(e.cursor().0, 0);
6950    }
6951
6952    #[test]
6953    fn insert_arrow_down_moves_cursor() {
6954        let mut e = fresh_editor("foo\nbar");
6955        e.jump_cursor(0, 0);
6956        enter_insert(&mut e);
6957        e.insert_arrow(crate::vim::InsertDir::Down);
6958        assert_eq!(e.cursor().0, 1);
6959    }
6960
6961    #[test]
6962    fn insert_home_moves_to_line_start() {
6963        let mut e = fresh_editor("hello");
6964        e.jump_cursor(0, 4);
6965        enter_insert(&mut e);
6966        e.insert_home();
6967        assert_eq!(e.cursor().1, 0);
6968    }
6969
6970    #[test]
6971    fn insert_end_moves_to_line_end() {
6972        let mut e = fresh_editor("hello");
6973        e.jump_cursor(0, 0);
6974        enter_insert(&mut e);
6975        e.insert_end();
6976        // move_line_end lands on the last char (col 4) for "hello".
6977        assert_eq!(e.cursor().1, 4);
6978    }
6979
6980    #[test]
6981    fn insert_pageup_does_not_panic() {
6982        let mut e = fresh_editor("line1\nline2\nline3");
6983        e.jump_cursor(2, 0);
6984        enter_insert(&mut e);
6985        // Viewport height 0 → no crash (viewport_h saturates to 1 row effectively).
6986        e.insert_pageup(24);
6987    }
6988
6989    #[test]
6990    fn insert_pagedown_does_not_panic() {
6991        let mut e = fresh_editor("line1\nline2\nline3");
6992        e.jump_cursor(0, 0);
6993        enter_insert(&mut e);
6994        e.insert_pagedown(24);
6995    }
6996
6997    #[test]
6998    fn leave_insert_to_normal_exits_mode() {
6999        let mut e = fresh_editor("hello");
7000        enter_insert(&mut e);
7001        e.leave_insert_to_normal();
7002        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7003    }
7004
7005    #[test]
7006    fn insert_backspace_at_buffer_start_is_noop() {
7007        let mut e = fresh_editor("hello");
7008        e.jump_cursor(0, 0);
7009        enter_insert(&mut e);
7010        // No previous char and no previous row — should not panic.
7011        e.insert_backspace();
7012        assert_eq!(e.buffer().lines()[0], "hello");
7013    }
7014
7015    #[test]
7016    fn insert_delete_at_buffer_end_is_noop() {
7017        let mut e = fresh_editor("hello");
7018        // Cursor at col 5 (past last char index of 4), no next row.
7019        e.jump_cursor(0, 5);
7020        enter_insert(&mut e);
7021        // col 5 >= line_chars (5), no next row → no-op.
7022        e.insert_delete();
7023        assert_eq!(e.buffer().lines()[0], "hello");
7024    }
7025
7026    // ── Phase 6.2: normal-mode primitive tests (kryptic-sh/hjkl#88) ─────────
7027
7028    // Helper: set content and ensure we are in Normal mode.
7029    fn normal_editor(initial: &str) -> Editor {
7030        let e = fresh_editor(initial);
7031        // fresh_editor starts in Normal; this is just a readability alias.
7032        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7033        e
7034    }
7035
7036    // ── Insert-mode entry ────────────────────────────────────────────────────
7037
7038    #[test]
7039    fn enter_insert_i_lands_in_insert_at_cursor() {
7040        let mut e = normal_editor("hello");
7041        e.jump_cursor(0, 2);
7042        e.enter_insert_i(1);
7043        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7044        assert_eq!(e.cursor(), (0, 2));
7045    }
7046
7047    #[test]
7048    fn enter_insert_shift_i_moves_to_first_non_blank_then_insert() {
7049        let mut e = normal_editor("  hello");
7050        e.jump_cursor(0, 5);
7051        e.enter_insert_shift_i(1);
7052        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7053        // First non-blank of "  hello" is col 2.
7054        assert_eq!(e.cursor().1, 2);
7055    }
7056
7057    #[test]
7058    fn enter_insert_a_advances_one_then_insert() {
7059        let mut e = normal_editor("hello");
7060        e.jump_cursor(0, 0);
7061        e.enter_insert_a(1);
7062        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7063        assert_eq!(e.cursor().1, 1);
7064    }
7065
7066    #[test]
7067    fn enter_insert_shift_a_lands_at_eol() {
7068        let mut e = normal_editor("hello");
7069        e.enter_insert_shift_a(1);
7070        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7071        assert_eq!(e.cursor().1, 5);
7072    }
7073
7074    #[test]
7075    fn open_line_below_creates_new_line_and_insert() {
7076        let mut e = normal_editor("hello\nworld");
7077        e.open_line_below(1);
7078        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7079        assert_eq!(e.buffer().lines().len(), 3);
7080    }
7081
7082    #[test]
7083    fn open_line_above_creates_line_before_cursor() {
7084        let mut e = normal_editor("hello\nworld");
7085        e.jump_cursor(1, 0);
7086        e.open_line_above(1);
7087        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7088        assert_eq!(e.buffer().lines().len(), 3);
7089        assert_eq!(e.cursor().0, 1);
7090    }
7091
7092    #[test]
7093    fn open_line_above_at_row_0_creates_blank_first_line() {
7094        let mut e = normal_editor("hello");
7095        e.open_line_above(1);
7096        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7097        // New blank line is row 0; old "hello" is row 1.
7098        assert_eq!(e.cursor().0, 0);
7099        assert_eq!(e.buffer().lines()[1], "hello");
7100    }
7101
7102    #[test]
7103    fn enter_replace_mode_sets_insert_mode() {
7104        let mut e = normal_editor("hello");
7105        e.enter_replace_mode(1);
7106        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7107    }
7108
7109    // ── Char / line ops ──────────────────────────────────────────────────────
7110
7111    #[test]
7112    fn delete_char_forward_removes_one_char() {
7113        let mut e = normal_editor("hello");
7114        e.jump_cursor(0, 1);
7115        e.delete_char_forward(1);
7116        assert_eq!(e.buffer().lines()[0], "hllo");
7117    }
7118
7119    #[test]
7120    fn delete_char_forward_count_5_removes_five() {
7121        let mut e = normal_editor("hello world");
7122        e.delete_char_forward(5);
7123        assert_eq!(e.buffer().lines()[0], " world");
7124    }
7125
7126    #[test]
7127    fn delete_char_forward_noop_on_empty_line() {
7128        let mut e = normal_editor("");
7129        let before = e.content().to_string();
7130        e.delete_char_forward(1);
7131        // Empty buffer: no chars to delete, content unchanged.
7132        assert_eq!(e.content(), before.as_str());
7133    }
7134
7135    #[test]
7136    fn delete_char_backward_removes_char_before_cursor() {
7137        let mut e = normal_editor("hello");
7138        e.jump_cursor(0, 3);
7139        e.delete_char_backward(1);
7140        assert_eq!(e.buffer().lines()[0], "helo");
7141    }
7142
7143    #[test]
7144    fn delete_char_backward_noop_at_col_0() {
7145        let mut e = normal_editor("hello");
7146        e.jump_cursor(0, 0);
7147        e.delete_char_backward(1);
7148        assert_eq!(e.buffer().lines()[0], "hello");
7149    }
7150
7151    #[test]
7152    fn substitute_char_deletes_and_enters_insert() {
7153        let mut e = normal_editor("hello");
7154        e.jump_cursor(0, 0);
7155        e.substitute_char(1);
7156        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7157        assert_eq!(e.buffer().lines()[0], "ello");
7158    }
7159
7160    #[test]
7161    fn substitute_char_count_3_deletes_three() {
7162        let mut e = normal_editor("hello");
7163        e.substitute_char(3);
7164        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7165        assert_eq!(e.buffer().lines()[0], "lo");
7166    }
7167
7168    #[test]
7169    fn substitute_line_clears_content_and_enters_insert() {
7170        let mut e = normal_editor("hello world");
7171        e.substitute_line(1);
7172        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7173        assert_eq!(e.buffer().lines()[0], "");
7174    }
7175
7176    #[test]
7177    fn delete_to_eol_removes_from_cursor_to_end() {
7178        let mut e = normal_editor("hello world");
7179        e.jump_cursor(0, 5);
7180        e.delete_to_eol();
7181        // col 5 is ' ' — deletes " world", leaving "hello".
7182        assert_eq!(e.buffer().lines()[0], "hello");
7183    }
7184
7185    #[test]
7186    fn delete_to_eol_noop_when_cursor_past_end() {
7187        let mut e = normal_editor("hi");
7188        e.jump_cursor(0, 2);
7189        e.delete_to_eol();
7190        assert_eq!(e.buffer().lines()[0], "hi");
7191    }
7192
7193    #[test]
7194    fn change_to_eol_enters_insert() {
7195        let mut e = normal_editor("hello world");
7196        e.jump_cursor(0, 5);
7197        e.change_to_eol();
7198        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7199        // col 5 is ' ' — deletes " world", leaving "hello".
7200        assert_eq!(e.buffer().lines()[0], "hello");
7201    }
7202
7203    #[test]
7204    fn yank_to_eol_fills_register() {
7205        let mut e = normal_editor("hello world");
7206        e.jump_cursor(0, 6);
7207        e.yank_to_eol(1);
7208        // Yank does not change mode.
7209        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7210        // Unnamed register holds the yanked text (col 6 is 'w' in "world").
7211        assert!(
7212            e.registers().unnamed.text.starts_with("world")
7213                || e.registers().unnamed.text.contains("world")
7214        );
7215    }
7216
7217    #[test]
7218    fn join_line_merges_next_line_with_space() {
7219        let mut e = normal_editor("foo\nbar");
7220        e.join_line(1);
7221        assert_eq!(e.buffer().lines()[0], "foo bar");
7222    }
7223
7224    #[test]
7225    fn join_line_count_2_merges_three_lines() {
7226        let mut e = normal_editor("a\nb\nc");
7227        e.join_line(2);
7228        // Our bridge calls join_line() `count` times, each joining the
7229        // current line with the next → 2 iterations: "a b c".
7230        assert_eq!(e.buffer().lines()[0], "a b c");
7231    }
7232
7233    #[test]
7234    fn join_line_noop_on_last_line() {
7235        let mut e = normal_editor("only");
7236        e.join_line(1);
7237        assert_eq!(e.buffer().lines()[0], "only");
7238    }
7239
7240    #[test]
7241    fn toggle_case_at_cursor_flips_letter() {
7242        let mut e = normal_editor("hello");
7243        e.toggle_case_at_cursor(1);
7244        assert_eq!(e.buffer().lines()[0], "Hello");
7245    }
7246
7247    #[test]
7248    fn toggle_case_at_cursor_count_3_flips_three() {
7249        let mut e = normal_editor("hello");
7250        e.toggle_case_at_cursor(3);
7251        assert_eq!(e.buffer().lines()[0], "HELlo");
7252    }
7253
7254    // ── Undo / redo round-trip ───────────────────────────────────────────────
7255
7256    #[test]
7257    fn undo_redo_roundtrip_via_public_methods() {
7258        let mut e = normal_editor("hello");
7259        e.delete_char_forward(1);
7260        assert_eq!(e.buffer().lines()[0], "ello");
7261        e.undo();
7262        assert_eq!(e.buffer().lines()[0], "hello");
7263        e.redo();
7264        assert_eq!(e.buffer().lines()[0], "ello");
7265    }
7266
7267    // ── Jump / scroll ────────────────────────────────────────────────────────
7268
7269    #[test]
7270    fn jump_back_and_forward_roundtrip() {
7271        let mut e = fresh_editor("a\nb\nc\nd");
7272        e.set_viewport_height(10);
7273        e.jump_cursor(3, 0);
7274        // Push current pos onto jumplist (big motion done externally; use
7275        // `run_keys` shortcut: `gg` pushes jump then `G` jumps).
7276        // Simpler: just call jump_back with empty stack → no-op (shouldn't panic).
7277        e.jump_back(1);
7278        e.jump_forward(1);
7279    }
7280
7281    #[test]
7282    fn scroll_full_page_down_moves_cursor() {
7283        use crate::vim::ScrollDir;
7284        let lines = (0..30)
7285            .map(|i| format!("line{i}"))
7286            .collect::<Vec<_>>()
7287            .join("\n");
7288        let mut e = fresh_editor(&lines);
7289        e.set_viewport_height(10);
7290        let before = e.cursor().0;
7291        e.scroll_full_page(ScrollDir::Down, 1);
7292        assert!(e.cursor().0 > before);
7293    }
7294
7295    #[test]
7296    fn scroll_full_page_up_moves_cursor() {
7297        use crate::vim::ScrollDir;
7298        let lines = (0..30)
7299            .map(|i| format!("line{i}"))
7300            .collect::<Vec<_>>()
7301            .join("\n");
7302        let mut e = fresh_editor(&lines);
7303        e.set_viewport_height(10);
7304        e.jump_cursor(25, 0);
7305        let before = e.cursor().0;
7306        e.scroll_full_page(ScrollDir::Up, 1);
7307        assert!(e.cursor().0 < before);
7308    }
7309
7310    #[test]
7311    fn scroll_half_page_down_moves_cursor() {
7312        use crate::vim::ScrollDir;
7313        let lines = (0..30)
7314            .map(|i| format!("line{i}"))
7315            .collect::<Vec<_>>()
7316            .join("\n");
7317        let mut e = fresh_editor(&lines);
7318        e.set_viewport_height(10);
7319        let before = e.cursor().0;
7320        e.scroll_half_page(ScrollDir::Down, 1);
7321        assert!(e.cursor().0 > before);
7322    }
7323
7324    #[test]
7325    fn scroll_half_page_up_at_top_is_noop() {
7326        use crate::vim::ScrollDir;
7327        let lines = (0..30)
7328            .map(|i| format!("line{i}"))
7329            .collect::<Vec<_>>()
7330            .join("\n");
7331        let mut e = fresh_editor(&lines);
7332        e.set_viewport_height(10);
7333        // Already at top, scrolling up should not panic and cursor stays at 0.
7334        e.scroll_half_page(ScrollDir::Up, 1);
7335        assert_eq!(e.cursor().0, 0);
7336    }
7337
7338    #[test]
7339    fn scroll_line_down_shifts_viewport_without_moving_cursor() {
7340        use crate::vim::ScrollDir;
7341        let lines = (0..30)
7342            .map(|i| format!("line{i}"))
7343            .collect::<Vec<_>>()
7344            .join("\n");
7345        let mut e = fresh_editor(&lines);
7346        e.set_viewport_height(10);
7347        // Park cursor in the middle of a large buffer.
7348        e.jump_cursor(15, 0);
7349        e.set_viewport_top(10);
7350        let cursor_before = e.cursor().0;
7351        e.scroll_line(ScrollDir::Down, 1);
7352        // Viewport top advances; cursor stays.
7353        assert_eq!(e.cursor().0, cursor_before);
7354        assert_eq!(e.host().viewport().top_row, 11);
7355    }
7356
7357    #[test]
7358    fn scroll_line_up_shifts_viewport() {
7359        use crate::vim::ScrollDir;
7360        let lines = (0..30)
7361            .map(|i| format!("line{i}"))
7362            .collect::<Vec<_>>()
7363            .join("\n");
7364        let mut e = fresh_editor(&lines);
7365        e.set_viewport_height(10);
7366        e.jump_cursor(15, 0);
7367        e.set_viewport_top(10);
7368        let cursor_before = e.cursor().0;
7369        e.scroll_line(ScrollDir::Up, 1);
7370        assert_eq!(e.cursor().0, cursor_before);
7371        assert_eq!(e.host().viewport().top_row, 9);
7372    }
7373
7374    #[test]
7375    fn scroll_line_clamps_cursor_when_off_screen() {
7376        use crate::vim::ScrollDir;
7377        let lines = (0..30)
7378            .map(|i| format!("line{i}"))
7379            .collect::<Vec<_>>()
7380            .join("\n");
7381        let mut e = fresh_editor(&lines);
7382        e.set_viewport_height(10);
7383        // Cursor at viewport top; scrolling down pushes it off — must clamp.
7384        e.jump_cursor(5, 0);
7385        e.set_viewport_top(5);
7386        e.scroll_line(ScrollDir::Down, 3);
7387        // New top = 8; cursor was at 5, which is now off-screen (< 8).
7388        // Cursor clamped to new top.
7389        assert!(e.cursor().0 >= 8);
7390    }
7391
7392    #[test]
7393    fn scroll_doesnt_crash_at_buffer_edges() {
7394        use crate::vim::ScrollDir;
7395        let mut e = normal_editor("single line");
7396        e.set_viewport_height(10);
7397        // Should not panic on any of these at-the-edge scrolls.
7398        e.scroll_full_page(ScrollDir::Down, 99);
7399        e.scroll_full_page(ScrollDir::Up, 99);
7400        e.scroll_half_page(ScrollDir::Down, 99);
7401        e.scroll_half_page(ScrollDir::Up, 99);
7402        e.scroll_line(ScrollDir::Down, 99);
7403        e.scroll_line(ScrollDir::Up, 99);
7404    }
7405
7406    // ── Horizontal scroll ────────────────────────────────────────────────────
7407
7408    #[test]
7409    fn scroll_right_advances_top_col() {
7410        let mut e = fresh_editor("hello world");
7411        e.set_viewport_height(10);
7412        e.scroll_right(5);
7413        assert_eq!(e.host().viewport().top_col, 5);
7414    }
7415
7416    #[test]
7417    fn scroll_left_does_not_underflow() {
7418        let mut e = fresh_editor("hello world");
7419        e.set_viewport_height(10);
7420        e.scroll_right(2);
7421        e.scroll_left(10);
7422        assert_eq!(e.host().viewport().top_col, 0);
7423    }
7424
7425    #[test]
7426    fn scroll_left_then_right_roundtrip() {
7427        let mut e = fresh_editor("hello world");
7428        e.set_viewport_height(10);
7429        e.scroll_right(10);
7430        e.scroll_left(3);
7431        assert_eq!(e.host().viewport().top_col, 7);
7432    }
7433
7434    // ── Search ───────────────────────────────────────────────────────────────
7435
7436    #[test]
7437    fn search_repeat_advances_to_next_match() {
7438        let mut e = fresh_editor("foo bar foo baz");
7439        // Use word_search to seed the search state (no search prompt needed).
7440        // `*` on "foo" at col 0 finds the second "foo" and sets last_search.
7441        e.word_search(true, true, 1);
7442        // Repeating forward wraps and finds the first "foo" again at col 0.
7443        e.search_repeat(true, 1);
7444        // Just ensure no panic and search state is valid.
7445        assert!(e.cursor().0 < e.buffer().lines().len());
7446    }
7447
7448    #[test]
7449    fn search_repeat_no_pattern_is_noop() {
7450        let mut e = normal_editor("hello world");
7451        let before = e.cursor();
7452        // No search pattern loaded — should not panic.
7453        e.search_repeat(true, 1);
7454        assert_eq!(e.cursor(), before);
7455    }
7456
7457    #[test]
7458    fn word_search_finds_word_under_cursor() {
7459        let mut e = fresh_editor("foo bar foo");
7460        // cursor starts at col 0 on "foo"
7461        e.word_search(true, true, 1);
7462        // Should jump to the second "foo" at col 8.
7463        assert_eq!(e.cursor().1, 8);
7464    }
7465
7466    #[test]
7467    fn word_search_whole_word_false_extracts_word_under_cursor() {
7468        // `g*` on "foo" (no `\b`) — use two lines so wrap can find the next match.
7469        let mut e = fresh_editor("foobar\nfoo baz");
7470        // Cursor on second line "foo" at col 0.
7471        e.jump_cursor(1, 0);
7472        // g* with whole_word=false: pattern = "foo", advance forward (skip current).
7473        // Starting at (1, 0), skip "foo" at (1,0), wrap to (0, 0) which matches "foo"
7474        // inside "foobar".
7475        e.word_search(true, false, 1);
7476        // Cursor should land on "foo" at row 0, col 0.
7477        assert_eq!(e.cursor(), (0, 0));
7478    }
7479
7480    #[test]
7481    fn word_search_backward_finds_previous_match() {
7482        let mut e = fresh_editor("foo bar foo");
7483        e.jump_cursor(0, 8); // on second "foo"
7484        e.word_search(false, true, 1);
7485        // Cursor should land on col 0 (first "foo").
7486        assert_eq!(e.cursor().1, 0);
7487    }
7488
7489    // ── Edge cases ───────────────────────────────────────────────────────────
7490
7491    #[test]
7492    fn delete_char_forward_on_single_char_line() {
7493        let mut e = normal_editor("x");
7494        e.delete_char_forward(1);
7495        assert_eq!(e.buffer().lines()[0], "");
7496    }
7497
7498    #[test]
7499    fn substitute_char_on_empty_line_is_noop_for_delete() {
7500        let mut e = normal_editor("");
7501        e.substitute_char(1);
7502        // Nothing to delete — but should enter Insert mode.
7503        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7504    }
7505
7506    #[test]
7507    fn join_line_10_iterations_clamps_gracefully() {
7508        let mut e = normal_editor("a\nb");
7509        // Joining 10 times on a 2-line buffer should not panic.
7510        e.join_line(10);
7511        // After the first join succeeds, the rest are no-ops.
7512        assert_eq!(e.buffer().lines()[0], "a b");
7513    }
7514
7515    #[test]
7516    fn toggle_case_past_line_end_is_noop() {
7517        let mut e = normal_editor("ab");
7518        e.jump_cursor(0, 5); // way past end
7519        e.toggle_case_at_cursor(1);
7520        // Should not panic.
7521        assert_eq!(e.buffer().lines()[0], "ab");
7522    }
7523
7524    // ── Phase 6.3: visual-mode primitive tests (kryptic-sh/hjkl#89) ──────────
7525
7526    // ── Visual entry ─────────────────────────────────────────────────────────
7527
7528    #[test]
7529    fn enter_visual_char_lands_in_visual_at_cursor() {
7530        let mut e = normal_editor("hello world");
7531        e.jump_cursor(0, 3);
7532        e.enter_visual_char();
7533        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7534        // Anchor should be at the cursor position we entered from.
7535        assert_eq!(e.vim.visual_anchor, (0, 3));
7536    }
7537
7538    #[test]
7539    fn enter_visual_line_lands_in_visual_line() {
7540        let mut e = normal_editor("hello\nworld");
7541        e.jump_cursor(1, 2);
7542        e.enter_visual_line();
7543        assert_eq!(e.vim_mode(), crate::VimMode::VisualLine);
7544        // Line anchor should be the current row.
7545        assert_eq!(e.vim.visual_line_anchor, 1);
7546    }
7547
7548    #[test]
7549    fn enter_visual_block_lands_in_visual_block() {
7550        let mut e = normal_editor("hello\nworld");
7551        e.jump_cursor(0, 2);
7552        e.enter_visual_block();
7553        assert_eq!(e.vim_mode(), crate::VimMode::VisualBlock);
7554        // Block anchor and vcol should match the cursor column.
7555        assert_eq!(e.vim.block_anchor, (0, 2));
7556        assert_eq!(e.vim.block_vcol, 2);
7557    }
7558
7559    // ── Visual exit ──────────────────────────────────────────────────────────
7560
7561    #[test]
7562    fn exit_visual_to_normal_sets_marks_and_returns_to_normal() {
7563        let mut e = normal_editor("hello world");
7564        // Enter charwise visual at col 2, extend to col 5.
7565        e.jump_cursor(0, 2);
7566        e.enter_visual_char();
7567        e.jump_cursor(0, 5);
7568        e.exit_visual_to_normal();
7569        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7570        // `<` = (0, 2), `>` = (0, 5).
7571        assert_eq!(e.mark('<'), Some((0, 2)));
7572        assert_eq!(e.mark('>'), Some((0, 5)));
7573    }
7574
7575    #[test]
7576    fn exit_visual_to_normal_stores_last_visual() {
7577        let mut e = normal_editor("hello world");
7578        e.jump_cursor(0, 1);
7579        e.enter_visual_char();
7580        e.jump_cursor(0, 4);
7581        e.exit_visual_to_normal();
7582        // last_visual should be set so gv can restore it.
7583        assert!(e.vim.last_visual.is_some());
7584        let lv = e.vim.last_visual.unwrap();
7585        assert_eq!(lv.anchor, (0, 1));
7586        assert_eq!(lv.cursor, (0, 4));
7587    }
7588
7589    #[test]
7590    fn exit_visual_line_sets_marks_at_line_boundaries() {
7591        let mut e = normal_editor("alpha\nbeta\ngamma");
7592        e.enter_visual_line(); // row 0
7593        e.jump_cursor(1, 3);
7594        e.exit_visual_to_normal();
7595        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7596        // `<` snaps to (min_row, 0), `>` snaps to (max_row, last_col).
7597        assert_eq!(e.mark('<'), Some((0, 0)));
7598        let last_col_of_beta = "beta".chars().count() - 1;
7599        assert_eq!(e.mark('>'), Some((1, last_col_of_beta)));
7600    }
7601
7602    // ── visual_o_toggle ───────────────────────────────────────────────────────
7603
7604    #[test]
7605    fn visual_o_toggle_swaps_anchor_and_cursor_charwise() {
7606        let mut e = normal_editor("hello world");
7607        // Enter visual at col 0, extend to col 4.
7608        e.enter_visual_char(); // anchor = (0,0)
7609        e.jump_cursor(0, 4); // cursor at col 4
7610        // Selection bounds before toggle: anchor=0, cursor=4.
7611        let pre_anchor = e.vim.visual_anchor;
7612        let pre_cursor = e.cursor();
7613        e.visual_o_toggle();
7614        // After toggle: cursor jumps to old anchor, anchor = old cursor.
7615        assert_eq!(e.cursor(), pre_anchor, "cursor should move to old anchor");
7616        assert_eq!(
7617            e.vim.visual_anchor, pre_cursor,
7618            "anchor should take old cursor position"
7619        );
7620        // Mode is unchanged.
7621        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7622    }
7623
7624    #[test]
7625    fn visual_o_toggle_double_returns_to_start() {
7626        let mut e = normal_editor("hello world");
7627        e.enter_visual_char();
7628        e.jump_cursor(0, 4);
7629        let anchor0 = e.vim.visual_anchor;
7630        let cursor0 = e.cursor();
7631        e.visual_o_toggle();
7632        e.visual_o_toggle();
7633        // Two toggles restore original positions.
7634        assert_eq!(e.vim.visual_anchor, anchor0);
7635        assert_eq!(e.cursor(), cursor0);
7636    }
7637
7638    #[test]
7639    fn visual_o_toggle_linewise_swaps_anchor_row() {
7640        let mut e = normal_editor("alpha\nbeta\ngamma");
7641        e.enter_visual_line(); // anchor row = 0
7642        e.jump_cursor(2, 0); // cursor on row 2
7643        e.visual_o_toggle();
7644        // Cursor should jump to old anchor row.
7645        assert_eq!(e.cursor().0, 0, "cursor row should be old anchor row");
7646        // Anchor row should now be the old cursor row.
7647        assert_eq!(e.vim.visual_line_anchor, 2);
7648    }
7649
7650    // ── reenter_last_visual ───────────────────────────────────────────────────
7651
7652    #[test]
7653    fn reenter_last_visual_after_vdollar_esc_restores() {
7654        let mut e = normal_editor("hello world");
7655        // v$ then Esc via FSM to store a real last_visual.
7656        e.enter_visual_char(); // anchor = (0,0)
7657        e.jump_cursor(0, 5); // move cursor to col 5 to create a range
7658        e.exit_visual_to_normal();
7659        // Should be back to Normal.
7660        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7661        // gv — should restore Visual mode.
7662        e.reenter_last_visual();
7663        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7664        // Cursor should be at the stored last position (col 5).
7665        assert_eq!(e.cursor().1, 5);
7666    }
7667
7668    #[test]
7669    fn reenter_last_visual_noop_when_no_history() {
7670        let mut e = normal_editor("hello");
7671        // No prior visual — should be a no-op, not a panic.
7672        e.reenter_last_visual();
7673        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7674    }
7675
7676    // ── set_mode ─────────────────────────────────────────────────────────────
7677
7678    #[test]
7679    fn set_mode_insert_flips_vim_mode_to_insert() {
7680        let mut e = normal_editor("hello");
7681        e.set_mode(crate::VimMode::Insert);
7682        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7683    }
7684
7685    #[test]
7686    fn set_mode_roundtrip_normal_insert_normal() {
7687        let mut e = normal_editor("hello");
7688        e.set_mode(crate::VimMode::Insert);
7689        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7690        e.set_mode(crate::VimMode::Normal);
7691        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7692    }
7693
7694    #[test]
7695    fn set_mode_visual_variants() {
7696        let mut e = normal_editor("hello");
7697        e.set_mode(crate::VimMode::Visual);
7698        assert_eq!(e.vim_mode(), crate::VimMode::Visual);
7699        e.set_mode(crate::VimMode::VisualLine);
7700        assert_eq!(e.vim_mode(), crate::VimMode::VisualLine);
7701        e.set_mode(crate::VimMode::VisualBlock);
7702        assert_eq!(e.vim_mode(), crate::VimMode::VisualBlock);
7703        e.set_mode(crate::VimMode::Normal);
7704        assert_eq!(e.vim_mode(), crate::VimMode::Normal);
7705    }
7706
7707    // ── current_mode / vim_mode consistency ───────────────────────────────────
7708
7709    // ── Phase 6.6b: FSM state accessor smoke tests ────────────────────────────
7710
7711    #[test]
7712    fn pending_round_trips() {
7713        let mut e = normal_editor("hello");
7714        assert!(matches!(e.pending(), crate::vim::Pending::None));
7715        e.set_pending(crate::vim::Pending::G);
7716        assert!(matches!(e.pending(), crate::vim::Pending::G));
7717        let taken = e.take_pending();
7718        assert!(matches!(taken, crate::vim::Pending::G));
7719        assert!(matches!(e.pending(), crate::vim::Pending::None));
7720    }
7721
7722    #[test]
7723    fn count_round_trips() {
7724        let mut e = normal_editor("hello");
7725        assert_eq!(e.count(), 0);
7726        e.set_count(5);
7727        assert_eq!(e.count(), 5);
7728        e.accumulate_count_digit(3);
7729        assert_eq!(e.count(), 53);
7730        e.reset_count();
7731        assert_eq!(e.count(), 0);
7732    }
7733
7734    #[test]
7735    fn take_count_returns_one_when_zero() {
7736        let mut e = normal_editor("hello");
7737        assert_eq!(e.take_count(), 1);
7738    }
7739
7740    #[test]
7741    fn take_count_returns_value_and_resets() {
7742        let mut e = normal_editor("hello");
7743        e.set_count(7);
7744        assert_eq!(e.take_count(), 7);
7745        assert_eq!(e.count(), 0);
7746    }
7747
7748    #[test]
7749    fn fsm_mode_round_trips() {
7750        let mut e = normal_editor("hello");
7751        assert_eq!(e.fsm_mode(), crate::vim::Mode::Normal);
7752        e.set_fsm_mode(crate::vim::Mode::Insert);
7753        assert_eq!(e.fsm_mode(), crate::vim::Mode::Insert);
7754        assert_eq!(e.vim_mode(), crate::VimMode::Insert);
7755        e.set_fsm_mode(crate::vim::Mode::Normal);
7756        assert_eq!(e.fsm_mode(), crate::vim::Mode::Normal);
7757    }
7758
7759    #[test]
7760    fn replaying_flag_round_trips() {
7761        let mut e = normal_editor("hello");
7762        assert!(!e.is_replaying());
7763        e.set_replaying(true);
7764        assert!(e.is_replaying());
7765        e.set_replaying(false);
7766        assert!(!e.is_replaying());
7767    }
7768
7769    #[test]
7770    fn one_shot_normal_flag_round_trips() {
7771        let mut e = normal_editor("hello");
7772        assert!(!e.is_one_shot_normal());
7773        e.set_one_shot_normal(true);
7774        assert!(e.is_one_shot_normal());
7775        e.set_one_shot_normal(false);
7776        assert!(!e.is_one_shot_normal());
7777    }
7778
7779    #[test]
7780    fn last_find_round_trips() {
7781        let mut e = normal_editor("hello");
7782        assert_eq!(e.last_find(), None);
7783        e.set_last_find(Some(('x', true, false)));
7784        assert_eq!(e.last_find(), Some(('x', true, false)));
7785        e.set_last_find(None);
7786        assert_eq!(e.last_find(), None);
7787    }
7788
7789    #[test]
7790    fn last_change_round_trips() {
7791        let mut e = normal_editor("hello");
7792        assert!(e.last_change().is_none());
7793        e.set_last_change(Some(crate::vim::LastChange::ToggleCase { count: 2 }));
7794        let lc = e.last_change();
7795        assert!(matches!(
7796            lc,
7797            Some(crate::vim::LastChange::ToggleCase { count: 2 })
7798        ));
7799        e.set_last_change(None);
7800        assert!(e.last_change().is_none());
7801    }
7802
7803    #[test]
7804    fn last_change_mut_allows_in_place_edit() {
7805        let mut e = normal_editor("hello");
7806        e.set_last_change(Some(crate::vim::LastChange::ToggleCase { count: 1 }));
7807        if let Some(crate::vim::LastChange::ToggleCase { count }) = e.last_change_mut() {
7808            *count = 42;
7809        }
7810        assert!(matches!(
7811            e.last_change(),
7812            Some(crate::vim::LastChange::ToggleCase { count: 42 })
7813        ));
7814    }
7815
7816    #[test]
7817    fn insert_session_round_trips() {
7818        let mut e = normal_editor("hello");
7819        assert!(e.insert_session().is_none());
7820        e.set_insert_session(Some(crate::vim::InsertSession {
7821            count: 3,
7822            row_min: 0,
7823            row_max: 0,
7824            before_lines: vec!["hello".to_string()],
7825            reason: crate::vim::InsertReason::Enter(crate::vim::InsertEntry::I),
7826        }));
7827        assert_eq!(e.insert_session().map(|s| s.count), Some(3));
7828        let taken = e.take_insert_session();
7829        assert!(taken.is_some());
7830        assert!(e.insert_session().is_none());
7831    }
7832
7833    #[test]
7834    fn visual_anchor_round_trips() {
7835        let mut e = normal_editor("hello");
7836        e.set_visual_anchor((1, 3));
7837        assert_eq!(e.visual_anchor(), (1, 3));
7838    }
7839
7840    #[test]
7841    fn visual_line_anchor_round_trips() {
7842        let mut e = normal_editor("hello\nworld");
7843        e.set_visual_line_anchor(1);
7844        assert_eq!(e.visual_line_anchor(), 1);
7845    }
7846
7847    #[test]
7848    fn block_anchor_and_vcol_round_trip() {
7849        let mut e = normal_editor("hello");
7850        e.set_block_anchor((0, 2));
7851        e.set_block_vcol(4);
7852        assert_eq!(e.block_anchor(), (0, 2));
7853        assert_eq!(e.block_vcol(), 4);
7854    }
7855
7856    #[test]
7857    fn yank_linewise_round_trips() {
7858        let mut e = normal_editor("hello");
7859        assert!(!e.yank_linewise());
7860        e.set_yank_linewise(true);
7861        assert!(e.yank_linewise());
7862    }
7863
7864    #[test]
7865    fn pending_register_raw_round_trips() {
7866        let mut e = normal_editor("hello");
7867        assert_eq!(e.pending_register(), None);
7868        e.set_pending_register_raw(Some('a'));
7869        assert_eq!(e.pending_register(), Some('a'));
7870        let taken = e.take_pending_register_raw();
7871        assert_eq!(taken, Some('a'));
7872        assert_eq!(e.pending_register(), None);
7873    }
7874
7875    #[test]
7876    fn recording_macro_round_trips() {
7877        let mut e = normal_editor("hello");
7878        assert_eq!(e.recording_macro(), None);
7879        e.set_recording_macro(Some('q'));
7880        assert_eq!(e.recording_macro(), Some('q'));
7881        e.set_recording_macro(None);
7882        assert_eq!(e.recording_macro(), None);
7883    }
7884
7885    #[test]
7886    fn recording_keys_round_trips() {
7887        let mut e = normal_editor("hello");
7888        let input = crate::Input {
7889            key: crate::Key::Char('j'),
7890            ctrl: false,
7891            alt: false,
7892            shift: false,
7893        };
7894        e.push_recording_key(input);
7895        assert_eq!(e.take_recording_keys(), vec![input]);
7896        assert!(e.take_recording_keys().is_empty());
7897    }
7898
7899    #[test]
7900    fn replaying_macro_raw_round_trips() {
7901        let mut e = normal_editor("hello");
7902        assert!(!e.is_replaying_macro_raw());
7903        e.set_replaying_macro_raw(true);
7904        assert!(e.is_replaying_macro_raw());
7905        e.set_replaying_macro_raw(false);
7906        assert!(!e.is_replaying_macro_raw());
7907    }
7908
7909    #[test]
7910    fn last_macro_round_trips() {
7911        let mut e = normal_editor("hello");
7912        assert_eq!(e.last_macro(), None);
7913        e.set_last_macro(Some('m'));
7914        assert_eq!(e.last_macro(), Some('m'));
7915    }
7916
7917    #[test]
7918    fn last_insert_pos_round_trips() {
7919        let mut e = normal_editor("hello");
7920        assert_eq!(e.last_insert_pos(), None);
7921        e.set_last_insert_pos(Some((1, 2)));
7922        assert_eq!(e.last_insert_pos(), Some((1, 2)));
7923    }
7924
7925    #[test]
7926    fn last_visual_round_trips() {
7927        let mut e = normal_editor("hello");
7928        assert!(e.last_visual().is_none());
7929        let snap = crate::vim::LastVisual {
7930            mode: crate::vim::Mode::Visual,
7931            anchor: (0, 0),
7932            cursor: (0, 3),
7933            block_vcol: 0,
7934        };
7935        e.set_last_visual(Some(snap));
7936        assert!(e.last_visual().is_some());
7937        e.set_last_visual(None);
7938        assert!(e.last_visual().is_none());
7939    }
7940
7941    #[test]
7942    fn viewport_pinned_round_trips() {
7943        let mut e = normal_editor("hello");
7944        assert!(!e.viewport_pinned());
7945        e.set_viewport_pinned(true);
7946        assert!(e.viewport_pinned());
7947        e.set_viewport_pinned(false);
7948        assert!(!e.viewport_pinned());
7949    }
7950
7951    #[test]
7952    fn insert_pending_register_round_trips() {
7953        let mut e = normal_editor("hello");
7954        assert!(!e.insert_pending_register());
7955        e.set_insert_pending_register(true);
7956        assert!(e.insert_pending_register());
7957    }
7958
7959    #[test]
7960    fn change_mark_start_round_trips() {
7961        let mut e = normal_editor("hello");
7962        assert_eq!(e.change_mark_start(), None);
7963        e.set_change_mark_start(Some((2, 5)));
7964        assert_eq!(e.change_mark_start(), Some((2, 5)));
7965        let taken = e.take_change_mark_start();
7966        assert_eq!(taken, Some((2, 5)));
7967        assert_eq!(e.change_mark_start(), None);
7968    }
7969
7970    #[test]
7971    fn search_prompt_state_round_trips() {
7972        let mut e = normal_editor("hello");
7973        assert!(e.search_prompt_state().is_none());
7974        e.set_search_prompt_state(Some(crate::vim::SearchPrompt {
7975            text: "foo".to_string(),
7976            cursor: 3,
7977            forward: true,
7978        }));
7979        assert_eq!(
7980            e.search_prompt_state().map(|p| p.text.as_str()),
7981            Some("foo")
7982        );
7983        let taken = e.take_search_prompt_state();
7984        assert!(taken.is_some());
7985        assert!(e.search_prompt_state().is_none());
7986    }
7987
7988    #[test]
7989    fn last_search_pattern_and_direction_round_trips() {
7990        let mut e = normal_editor("hello");
7991        assert_eq!(e.last_search_pattern(), None);
7992        e.set_last_search_pattern_only(Some("world".to_string()));
7993        assert_eq!(e.last_search_pattern(), Some("world"));
7994        e.set_last_search_forward_only(false);
7995        assert!(!e.last_search_forward());
7996    }
7997
7998    #[test]
7999    fn search_history_round_trips() {
8000        let mut e = normal_editor("hello");
8001        assert!(e.search_history().is_empty());
8002        e.search_history_mut().push("pattern1".to_string());
8003        assert_eq!(e.search_history(), &["pattern1"]);
8004        e.set_search_history_cursor(Some(0));
8005        assert_eq!(e.search_history_cursor(), Some(0));
8006        e.set_search_history_cursor(None);
8007        assert_eq!(e.search_history_cursor(), None);
8008    }
8009
8010    #[test]
8011    fn jump_lists_round_trips() {
8012        let mut e = normal_editor("hello");
8013        assert!(e.jump_back_list().is_empty());
8014        assert!(e.jump_fwd_list().is_empty());
8015        e.jump_back_list_mut().push((1, 2));
8016        e.jump_fwd_list_mut().push((3, 4));
8017        assert_eq!(e.jump_back_list(), &[(1, 2)]);
8018        assert_eq!(e.jump_fwd_list(), &[(3, 4)]);
8019    }
8020
8021    #[test]
8022    fn last_input_timing_round_trips() {
8023        let mut e = normal_editor("hello");
8024        assert!(e.last_input_at().is_none());
8025        assert!(e.last_input_host_at().is_none());
8026        let now = std::time::Instant::now();
8027        e.set_last_input_at(Some(now));
8028        assert!(e.last_input_at().is_some());
8029        let dur = core::time::Duration::from_millis(100);
8030        e.set_last_input_host_at(Some(dur));
8031        assert_eq!(e.last_input_host_at(), Some(dur));
8032    }
8033
8034    // ── auto_indent_range tests ──────────────────────────────────────────────
8035
8036    /// Helper: build an editor with `expandtab=true` and the given shiftwidth.
8037    fn indent_editor(initial: &str, shiftwidth: usize, expandtab: bool) -> Editor {
8038        let mut e = fresh_editor(initial);
8039        e.settings_mut().shiftwidth = shiftwidth;
8040        e.settings_mut().expandtab = expandtab;
8041        e
8042    }
8043
8044    #[test]
8045    fn auto_indent_single_line_under_open_brace() {
8046        // `{\nfoo\n}` — "foo" is at depth 1 under the `{`.
8047        // With shiftwidth=4 expandtab=true it should become "    foo".
8048        let mut e = indent_editor("{\nfoo\n}", 4, true);
8049        // auto-indent only row 1 ("foo").
8050        e.auto_indent_range((1, 0), (1, 0));
8051        let lines = e.buffer().lines();
8052        assert_eq!(lines[1], "    foo", "foo should be indented by 4 spaces");
8053    }
8054
8055    #[test]
8056    fn auto_indent_close_brace_outdents() {
8057        // `{\n    inner\n}` — the `}` is at depth 1 but starts with a close
8058        // bracket so effective_depth = 0.
8059        let mut e = indent_editor("{\n    inner\n}", 4, true);
8060        e.auto_indent_range((2, 0), (2, 0));
8061        let lines = e.buffer().lines();
8062        assert_eq!(lines[2], "}", "`}}` should have zero indent");
8063    }
8064
8065    #[test]
8066    fn auto_indent_whole_buffer_normalizes_mixed_indent() {
8067        // Mixed-indent input: first line un-indented `{`, second line 1-tab
8068        // indented body, third line un-indented `}`.
8069        let src = "{\n\tbody\n}";
8070        let mut e = indent_editor(src, 4, true);
8071        let total = e.buffer().lines().len();
8072        e.auto_indent_range((0, 0), (total - 1, 0));
8073        let lines = e.buffer().lines();
8074        // `{` — depth 0 at start.
8075        assert_eq!(lines[0], "{");
8076        // `body` — depth 1 after `{`.
8077        assert_eq!(lines[1], "    body");
8078        // `}` — depth 1 but starts with close → effective_depth 0.
8079        assert_eq!(lines[2], "}");
8080    }
8081
8082    #[test]
8083    fn auto_indent_respects_expandtab_false_uses_tabs() {
8084        // Same buffer, but expandtab=false → indent unit is `\t`.
8085        let src = "{\nbody\n}";
8086        let mut e = indent_editor(src, 4, false);
8087        let total = e.buffer().lines().len();
8088        e.auto_indent_range((0, 0), (total - 1, 0));
8089        let lines = e.buffer().lines();
8090        assert_eq!(lines[0], "{");
8091        assert_eq!(lines[1], "\tbody");
8092        assert_eq!(lines[2], "}");
8093    }
8094
8095    #[test]
8096    fn auto_indent_empty_line_stays_empty() {
8097        // `{\n\nfoo\n}` — blank line in the middle should stay blank.
8098        let src = "{\n\nfoo\n}";
8099        let mut e = indent_editor(src, 4, true);
8100        let total = e.buffer().lines().len();
8101        e.auto_indent_range((0, 0), (total - 1, 0));
8102        let lines = e.buffer().lines();
8103        assert_eq!(lines[1], "", "blank line should stay blank");
8104        assert_eq!(lines[2], "    foo");
8105    }
8106
8107    #[test]
8108    fn auto_indent_cursor_lands_on_first_nonws_of_start_row() {
8109        // After `==` / `auto_indent_range` the cursor should be at the first
8110        // non-whitespace character of start_row (vim parity).
8111        let src = "{\nfoo\n}";
8112        let mut e = indent_editor(src, 4, true);
8113        // Reindent only row 1.
8114        e.auto_indent_range((1, 0), (1, 0));
8115        // Row 1 after reindent is "    foo"; first non-ws is col 4.
8116        let (row, col) = e.cursor();
8117        assert_eq!(row, 1, "cursor should stay on start_row");
8118        assert_eq!(col, 4, "cursor should land on first non-ws char (col 4)");
8119    }
8120
8121    #[test]
8122    fn auto_indent_sets_last_indent_range() {
8123        // After `auto_indent_range` the engine must store the touched row span.
8124        let src = "{\nfoo\nbar\n}";
8125        let mut e = indent_editor(src, 4, true);
8126        let total = e.buffer().lines().len();
8127        e.auto_indent_range((0, 0), (total - 1, 0));
8128        assert_eq!(
8129            e.take_last_indent_range(),
8130            Some((0, total - 1)),
8131            "take_last_indent_range must return Some with the touched rows"
8132        );
8133    }
8134
8135    #[test]
8136    fn take_last_indent_range_clears() {
8137        // A second call after draining must return None.
8138        let src = "{\nfoo\n}";
8139        let mut e = indent_editor(src, 4, true);
8140        e.auto_indent_range((0, 0), (2, 0));
8141        let _ = e.take_last_indent_range(); // drain
8142        assert_eq!(
8143            e.take_last_indent_range(),
8144            None,
8145            "second take_last_indent_range must return None"
8146        );
8147    }
8148
8149    // ── Diagnostic: auto_indent vs cargo fmt on a real source file ────────
8150    //
8151    // Loads `motions.rs` (~1400 LOC, mixed real-world Rust patterns: method
8152    // chains, multi-line fn args, match arms, where clauses, closures, nested
8153    // types) at compile time, runs `auto_indent_range` over every row, and
8154    // diffs per-line leading-whitespace counts against the cargo-fmt'd source
8155    // (the file is in the repo, fmt'd by CI on every commit).
8156    //
8157    // The test PRINTS divergences and only fails if more than `THRESHOLD`
8158    // lines disagree — the dumb shiftwidth+bracket algorithm is documented
8159    // to mishandle some patterns (chains, where clauses, etc.). A full
8160    // language-aware indenter is a v2 follow-up. The point of this test is
8161    // to surface the divergence list so we can decide which patterns the
8162    // dumb algo CAN be taught to handle without going full tree-sitter.
8163    //
8164    // To diagnose: run with `--nocapture` to see the full diff.
8165    #[test]
8166    #[ignore = "diagnostic — run with --ignored --nocapture to see auto-indent vs cargo fmt diffs"]
8167    fn auto_indent_vs_cargo_fmt_motions_diagnostic() {
8168        let original = include_str!("motions.rs");
8169
8170        let mut e = Editor::new(
8171            hjkl_buffer::Buffer::new(),
8172            crate::types::DefaultHost::new(),
8173            crate::types::Options {
8174                shiftwidth: 4,
8175                expandtab: true,
8176                tabstop: 4,
8177                ..crate::types::Options::default()
8178            },
8179        );
8180        e.set_content(original);
8181
8182        let row_count = buf_row_count(&e.buffer);
8183        e.auto_indent_range((0, 0), (row_count.saturating_sub(1), 0));
8184
8185        let after_lines: Vec<String> = (0..row_count)
8186            .filter_map(|r| buf_line(&e.buffer, r).map(str::to_owned))
8187            .collect();
8188        let original_lines: Vec<&str> = original.lines().collect();
8189
8190        let leading_ws = |s: &str| s.chars().take_while(|c| c.is_whitespace()).count();
8191
8192        let mut diffs: Vec<(usize, String, usize, usize)> = Vec::new();
8193        for (i, (orig, after)) in original_lines.iter().zip(after_lines.iter()).enumerate() {
8194            let want = leading_ws(orig);
8195            let got = leading_ws(after);
8196            if want != got {
8197                diffs.push((i + 1, orig.trim().chars().take(80).collect(), want, got));
8198            }
8199        }
8200
8201        // Print the first 50 divergences for diagnosis.
8202        eprintln!(
8203            "auto_indent_vs_cargo_fmt: {} lines differ out of {} ({}%)",
8204            diffs.len(),
8205            original_lines.len(),
8206            (diffs.len() * 100) / original_lines.len().max(1),
8207        );
8208        for (line_no, content, want, got) in diffs.iter().take(50) {
8209            eprintln!("  L{line_no:5} want={want:2} got={got:2}  {content}");
8210        }
8211        if diffs.len() > 50 {
8212            eprintln!("  ... and {} more", diffs.len() - 50);
8213        }
8214
8215        // Soft assertion — track divergence count over time. If the algo
8216        // gets smarter, this number should drop. If a regression makes it
8217        // jump, we'll notice. Set the cap generously above current baseline.
8218        let pct = (diffs.len() * 100) / original_lines.len().max(1);
8219        // 2026-05-16 baseline after fixing bracket scan + chain continuation:
8220        // 5 divergences / 1416 lines (<1%). Remaining lines are a single
8221        // \`let X = if {} else {};\` trailing-\`=\` continuation pattern —
8222        // documented v2 follow-up. Cap at 2% so any regression in the
8223        // bracket scan or chain detection trips the test.
8224        assert!(
8225            pct < 2,
8226            "auto_indent diverges from cargo fmt on {pct}% of lines — regression from <1% baseline"
8227        );
8228    }
8229}