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