Skip to main content

hjkl_engine/
vim.rs

1//! Vim-mode engine.
2//!
3//! Implements a command grammar of the form
4//!
5//! ```text
6//! Command := count? (operator count? (motion | text-object)
7//!                   | motion
8//!                   | insert-entry
9//!                   | misc)
10//! ```
11//!
12//! The parser is a small state machine driven by one `Input` at a time.
13//! Motions and text objects produce a [`Range`] (with inclusive/exclusive
14//! / linewise classification). A single [`Operator`] implementation
15//! applies a range — so `dw`, `d$`, `daw`, and visual `d` all go through
16//! the same code path.
17//!
18//! The most recent mutating command is stored in
19//! [`VimState::last_change`] so `.` can replay it.
20//!
21//! # Roadmap
22//!
23//! Tracked in the original plan at
24//! `~/.claude/plans/look-at-the-vim-curried-fern.md`. Phases still
25//! outstanding — each one can land as an isolated PR.
26//!
27//! ## P3 — Registers & marks
28//!
29//! - TODO: `RegisterBank` indexed by char:
30//!     - unnamed `""`, last-yank `"0`, small-delete `"-`
31//!     - named `"a-"z` (uppercase `"A-"Z` appends instead of overwriting)
32//!     - blackhole `"_`
33//!     - system clipboard `"+` / `"*` (wire to `crate::clipboard::Clipboard`)
34//!     - read-only `":`, `".`, `"%` — surface in `:reg` output
35//! - TODO: route every yank / cut / paste through the bank. Parser needs
36//!   a `"{reg}` prefix state that captures the target register before a
37//!   count / operator.
38//! - TODO: `m{a-z}` sets a mark in a `HashMap<char, (buffer_id, row, col)>`;
39//!   `'x` jumps to the line (FirstNonBlank), `` `x `` to the exact cell.
40//!   Uppercase marks are global across tabs; lowercase are per-buffer.
41//! - TODO: `''` and `` `` `` jump to the last-jump position; `'[` `']`
42//!   `'<` `'>` bound the last change / visual region.
43//! - TODO: `:reg` and `:marks` ex commands.
44//!
45//! ## P4 — Macros
46//!
47//! - TODO: `q{a-z}` starts recording raw `Input`s into the register;
48//!   next `q` stops.
49//! - TODO: `@{a-z}` replays the register by re-feeding inputs through
50//!   `step`. `@@` repeats the last macro. Nested macros need a sane
51//!   depth cap (e.g. 100) to avoid runaway loops.
52//! - TODO: ensure recording doesn't capture the initial `q{a-z}` itself.
53//!
54//! ## P6 — Polish (still outstanding)
55//!
56//! - TODO: indent operators `>` / `<` (with line + text-object targets).
57//! - TODO: format operator `=` — map to whatever SQL formatter we wire
58//!   up; for now stub that returns the range unchanged with a toast.
59//! - TODO: case operators `gU` / `gu` / `g~` on a range (already have
60//!   single-char `~`).
61//! - TODO: screen motions `H` / `M` / `L` once we track the render
62//!   viewport height inside Editor.
63//! - TODO: scroll-to-cursor motions `zz` / `zt` / `zb`.
64//!
65//! ## Known substrate / divergence notes
66//!
67//! - TODO: insert-mode indent helpers — `Ctrl-t` / `Ctrl-d` (increase /
68//!   decrease indent on current line) and `Ctrl-r <reg>` (paste from a
69//!   register). `Ctrl-r` needs the `RegisterBank` from P3 to be useful.
70//! - TODO: `/` and `?` search prompts still live in `the host/src/lib.rs`.
71//!   The plan calls for moving them into the editor (so the editor owns
72//!   `last_search_pattern` rather than the TUI loop). Safe to defer.
73
74use crate::VimMode;
75use crate::input::{Input, Key};
76
77use crate::buf_helpers::{
78    buf_cursor_pos, buf_line, buf_line_bytes, buf_line_chars, buf_lines_to_vec, buf_row_count,
79    buf_set_cursor_pos, buf_set_cursor_rc,
80};
81use crate::editor::Editor;
82
83// ─── Modes & parser state ───────────────────────────────────────────────────
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
86pub enum Mode {
87    #[default]
88    Normal,
89    Insert,
90    Visual,
91    VisualLine,
92    /// Column-oriented selection (`Ctrl-V`). Unlike the other visual
93    /// modes this one doesn't use tui-textarea's single-range selection
94    /// — the block corners live in [`VimState::block_anchor`] and the
95    /// live cursor. Operators read the rectangle off those two points.
96    VisualBlock,
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Default)]
100pub enum Pending {
101    #[default]
102    None,
103    /// Operator seen; still waiting for a motion / text-object / double-op.
104    /// `count1` is any count pressed before the operator.
105    Op { op: Operator, count1: usize },
106    /// Operator + 'i' or 'a' seen; waiting for the text-object character.
107    OpTextObj {
108        op: Operator,
109        count1: usize,
110        inner: bool,
111    },
112    /// Operator + 'g' seen (for `dgg`).
113    OpG { op: Operator, count1: usize },
114    /// Bare `g` seen in normal/visual — looking for `g`, `e`, `E`, …
115    G,
116    /// Bare `f`/`F`/`t`/`T` — looking for the target char.
117    Find { forward: bool, till: bool },
118    /// Operator + `f`/`F`/`t`/`T` — looking for target char.
119    OpFind {
120        op: Operator,
121        count1: usize,
122        forward: bool,
123        till: bool,
124    },
125    /// `r` pressed — waiting for the replacement char.
126    Replace,
127    /// Visual mode + `i` or `a` pressed — waiting for the text-object
128    /// character to extend the selection over.
129    VisualTextObj { inner: bool },
130    /// Bare `z` seen — looking for `z` (center), `t` (top), `b` (bottom).
131    Z,
132    /// `m` pressed — waiting for the mark letter to set.
133    SetMark,
134    /// `'` pressed — waiting for the mark letter to jump to its line
135    /// (lands on first non-blank, linewise for operators).
136    GotoMarkLine,
137    /// `` ` `` pressed — waiting for the mark letter to jump to the
138    /// exact `(row, col)` stored at set time (charwise for operators).
139    GotoMarkChar,
140    /// `"` pressed — waiting for the register selector. The next char
141    /// (`a`–`z`, `A`–`Z`, `0`–`9`, or `"`) sets `pending_register`.
142    SelectRegister,
143    /// `q` pressed (not currently recording) — waiting for the macro
144    /// register name. The macro records every key after the chord
145    /// resolves, until a bare `q` ends the recording.
146    RecordMacroTarget,
147    /// `@` pressed — waiting for the macro register name to play.
148    /// `count` is the prefix multiplier (`3@a` plays the macro 3
149    /// times); 0 means "no prefix" and is treated as 1.
150    PlayMacroTarget { count: usize },
151}
152
153// ─── Operator / Motion / TextObject ────────────────────────────────────────
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum Operator {
157    Delete,
158    Change,
159    Yank,
160    /// `gU{motion}` — uppercase the range. Entered via the `g` prefix
161    /// in normal mode or `U` in visual mode.
162    Uppercase,
163    /// `gu{motion}` — lowercase the range. `u` in visual mode.
164    Lowercase,
165    /// `g~{motion}` — toggle case of the range. `~` in visual mode
166    /// (character at the cursor for the single-char `~` command stays
167    /// its own code path in normal mode).
168    ToggleCase,
169    /// `>{motion}` — indent the line range by `shiftwidth` spaces.
170    /// Always linewise, even when the motion is char-wise — mirrors
171    /// vim's behaviour where `>w` indents the current line, not the
172    /// word on it.
173    Indent,
174    /// `<{motion}` — outdent the line range (remove up to
175    /// `shiftwidth` leading spaces per line).
176    Outdent,
177    /// `zf{motion}` / `zf{textobj}` / Visual `zf` — create a closed
178    /// fold spanning the row range. Doesn't mutate the buffer text;
179    /// cursor restores to the operator's start position.
180    Fold,
181    /// `gq{motion}` — reflow the row range to `settings.textwidth`.
182    /// Greedy word-wrap: collapses each paragraph (blank-line-bounded
183    /// run) into space-separated words, then re-emits lines whose
184    /// width stays under `textwidth`. Always linewise, like indent.
185    Reflow,
186    /// `={motion}` — auto-indent the line range using shiftwidth-based
187    /// bracket depth counting (v1 dumb reindent). Always linewise.
188    /// See `auto_indent_range` for the algorithm and its limitations.
189    AutoIndent,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
193pub enum Motion {
194    Left,
195    Right,
196    Up,
197    Down,
198    WordFwd,
199    BigWordFwd,
200    WordBack,
201    BigWordBack,
202    WordEnd,
203    BigWordEnd,
204    /// `ge` — backward word end.
205    WordEndBack,
206    /// `gE` — backward WORD end.
207    BigWordEndBack,
208    LineStart,
209    FirstNonBlank,
210    LineEnd,
211    FileTop,
212    FileBottom,
213    Find {
214        ch: char,
215        forward: bool,
216        till: bool,
217    },
218    FindRepeat {
219        reverse: bool,
220    },
221    MatchBracket,
222    WordAtCursor {
223        forward: bool,
224        /// `*` / `#` use `\bword\b` boundaries; `g*` / `g#` drop them so
225        /// the search hits substrings (e.g. `foo` matches inside `foobar`).
226        whole_word: bool,
227    },
228    /// `n` / `N` — repeat the last `/` or `?` search.
229    SearchNext {
230        reverse: bool,
231    },
232    /// `H` — cursor to viewport top (plus `count - 1` rows down).
233    ViewportTop,
234    /// `M` — cursor to viewport middle.
235    ViewportMiddle,
236    /// `L` — cursor to viewport bottom (minus `count - 1` rows up).
237    ViewportBottom,
238    /// `g_` — last non-blank char on the line.
239    LastNonBlank,
240    /// `gM` — cursor to the middle char column of the current line
241    /// (`floor(chars / 2)`). Vim's variant ignoring screen wrap.
242    LineMiddle,
243    /// `{` — previous paragraph (preceding blank line, or top).
244    ParagraphPrev,
245    /// `}` — next paragraph (following blank line, or bottom).
246    ParagraphNext,
247    /// `(` — previous sentence boundary.
248    SentencePrev,
249    /// `)` — next sentence boundary.
250    SentenceNext,
251    /// `gj` — `count` visual rows down (one screen segment per step
252    /// under `:set wrap`; falls back to `Down` otherwise).
253    ScreenDown,
254    /// `gk` — `count` visual rows up; mirror of [`Motion::ScreenDown`].
255    ScreenUp,
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq)]
259pub enum TextObject {
260    Word {
261        big: bool,
262    },
263    Quote(char),
264    Bracket(char),
265    Paragraph,
266    /// `it` / `at` — XML/HTML-style tag pair. `inner = true` covers
267    /// content between `>` and `</`; `inner = false` covers the open
268    /// tag through the close tag inclusive.
269    XmlTag,
270    /// `is` / `as` — sentence: a run ending at `.`, `?`, or `!`
271    /// followed by whitespace or end-of-line. `inner = true` covers
272    /// the sentence text only; `inner = false` includes trailing
273    /// whitespace.
274    Sentence,
275}
276
277/// Classification determines how operators treat the range end.
278#[derive(Debug, Clone, Copy, PartialEq, Eq)]
279pub enum RangeKind {
280    /// Range end is exclusive (end column not included). Typical: h, l, w, 0, $.
281    Exclusive,
282    /// Range end is inclusive. Typical: e, f, t, %.
283    Inclusive,
284    /// Whole lines from top row to bottom row. Typical: j, k, gg, G.
285    Linewise,
286}
287
288// ─── Dot-repeat storage ────────────────────────────────────────────────────
289
290/// Information needed to replay a mutating change via `.`.
291#[derive(Debug, Clone)]
292pub enum LastChange {
293    /// Operator over a motion.
294    OpMotion {
295        op: Operator,
296        motion: Motion,
297        count: usize,
298        inserted: Option<String>,
299    },
300    /// Operator over a text-object.
301    OpTextObj {
302        op: Operator,
303        obj: TextObject,
304        inner: bool,
305        inserted: Option<String>,
306    },
307    /// `dd`, `cc`, `yy` with a count.
308    LineOp {
309        op: Operator,
310        count: usize,
311        inserted: Option<String>,
312    },
313    /// `x`, `X` with a count.
314    CharDel { forward: bool, count: usize },
315    /// `r<ch>` with a count.
316    ReplaceChar { ch: char, count: usize },
317    /// `~` with a count.
318    ToggleCase { count: usize },
319    /// `J` with a count.
320    JoinLine { count: usize },
321    /// `p` / `P` with a count.
322    Paste { before: bool, count: usize },
323    /// `D` (delete to EOL).
324    DeleteToEol { inserted: Option<String> },
325    /// `o` / `O` + the inserted text.
326    OpenLine { above: bool, inserted: String },
327    /// `i`/`I`/`a`/`A` + inserted text.
328    InsertAt {
329        entry: InsertEntry,
330        inserted: String,
331        count: usize,
332    },
333}
334
335#[derive(Debug, Clone, Copy, PartialEq, Eq)]
336pub enum InsertEntry {
337    I,
338    A,
339    ShiftI,
340    ShiftA,
341}
342
343// ─── VimState ──────────────────────────────────────────────────────────────
344
345#[derive(Default)]
346pub struct VimState {
347    /// Internal FSM mode. Kept in sync with `current_mode` after every
348    /// `step`. Phase 6.6b: promoted from private to `pub` so the FSM
349    /// body (moving to hjkl-vim in 6.6c–6.6g) can read/write it directly
350    /// until the migration is complete.
351    pub mode: Mode,
352    /// Two-key chord in progress. `Pending::None` when idle.
353    pub pending: Pending,
354    /// Digit prefix accumulated before an operator or motion. `0` means
355    /// no prefix was typed (treated as 1 by most commands).
356    pub count: usize,
357    /// Last `f`/`F`/`t`/`T` target, for `;` / `,` repeat.
358    pub last_find: Option<(char, bool, bool)>,
359    /// Most-recent mutating command for `.` dot-repeat.
360    pub last_change: Option<LastChange>,
361    /// Captured on insert-mode entry: count, buffer snapshot, entry kind.
362    pub insert_session: Option<InsertSession>,
363    /// (row, col) anchor for char-wise Visual mode. Set on entry, used
364    /// to compute the highlight range and the operator range without
365    /// relying on tui-textarea's live selection.
366    pub visual_anchor: (usize, usize),
367    /// Row anchor for VisualLine mode.
368    pub visual_line_anchor: usize,
369    /// (row, col) anchor for VisualBlock mode. The live cursor is the
370    /// opposite corner.
371    pub block_anchor: (usize, usize),
372    /// Intended "virtual" column for the block's active corner. j/k
373    /// clamp cursor.col to shorter rows, which would collapse the
374    /// block across ragged content — so we remember the desired column
375    /// separately and use it for block bounds / insert-column
376    /// computations. Updated by h/l only.
377    pub block_vcol: usize,
378    /// Track whether the last yank/cut was linewise (drives `p`/`P` layout).
379    pub yank_linewise: bool,
380    /// Active register selector — set by `"reg` prefix, consumed by
381    /// the next y / d / c / p. `None` falls back to the unnamed `"`.
382    pub pending_register: Option<char>,
383    /// Recording target — set by `q{reg}`, cleared by a bare `q`.
384    /// While `Some`, every consumed `Input` is appended to
385    /// `recording_keys`.
386    pub recording_macro: Option<char>,
387    /// Keys recorded into the in-progress macro. On `q` finish, these
388    /// are encoded via [`crate::input::encode_macro`] and written to
389    /// the matching named register slot, so macros and yanks share a
390    /// single store.
391    pub recording_keys: Vec<crate::input::Input>,
392    /// Set during `@reg` replay so the recorder doesn't capture the
393    /// replayed keystrokes a second time.
394    pub replaying_macro: bool,
395    /// Last register played via `@reg`. `@@` re-plays this one.
396    pub last_macro: Option<char>,
397    /// Position of the most recent buffer mutation. Surfaced via
398    /// the `'.` / `` `. `` marks for quick "back to last edit".
399    pub last_edit_pos: Option<(usize, usize)>,
400    /// Position where the cursor was when insert mode last exited (Esc).
401    /// Used by `gi` to return to the exact (row, col) where the user
402    /// last typed, matching vim's `:h gi`.
403    pub last_insert_pos: Option<(usize, usize)>,
404    /// Bounded ring of recent edit positions (newest at the back).
405    /// `g;` walks toward older entries, `g,` toward newer ones. Capped
406    /// at [`CHANGE_LIST_MAX`].
407    pub change_list: Vec<(usize, usize)>,
408    /// Index into `change_list` while walking. `None` outside a walk —
409    /// any new edit clears it (and trims forward entries past it).
410    pub change_list_cursor: Option<usize>,
411    /// Snapshot of the last visual selection for `gv` re-entry.
412    /// Stored on every Visual / VisualLine / VisualBlock exit.
413    pub last_visual: Option<LastVisual>,
414    /// `zz` / `zt` / `zb` set this so the end-of-step scrolloff
415    /// pass doesn't override the user's explicit viewport pinning.
416    /// Cleared every step.
417    pub viewport_pinned: bool,
418    /// Set while replaying `.` / last-change so we don't re-record it.
419    pub replaying: bool,
420    /// Entered Normal from Insert via `Ctrl-o`; after the next complete
421    /// normal-mode command we return to Insert.
422    pub one_shot_normal: bool,
423    /// Live `/` or `?` prompt. `None` outside search-prompt mode.
424    pub search_prompt: Option<SearchPrompt>,
425    /// Most recent committed search pattern. Surfaced to host apps via
426    /// [`Editor::last_search`] so their status line can render a hint
427    /// and so `n` / `N` have something to repeat.
428    pub last_search: Option<String>,
429    /// Direction of the last committed search. `n` repeats this; `N`
430    /// inverts it. Defaults to forward so a never-searched buffer's
431    /// `n` still walks downward.
432    pub last_search_forward: bool,
433    /// Back half of the jumplist — `Ctrl-o` pops from here. Populated
434    /// with the pre-motion cursor when a "big jump" motion fires
435    /// (`gg`/`G`, `%`, `*`/`#`, `n`/`N`, `H`/`M`/`L`, committed `/` or
436    /// `?`). Capped at 100 entries.
437    pub jump_back: Vec<(usize, usize)>,
438    /// Forward half — `Ctrl-i` pops from here. Cleared by any new big
439    /// jump, matching vim's "branch off trims forward history" rule.
440    pub jump_fwd: Vec<(usize, usize)>,
441    /// Set by `Ctrl-R` in insert mode while waiting for the register
442    /// selector. The next typed char names the register; its contents
443    /// are inserted inline at the cursor and the flag clears.
444    pub insert_pending_register: bool,
445    /// Stashed start position for the `[` mark on a Change operation.
446    /// Set to `top` before the cut in `run_operator_over_range` (Change
447    /// arm); consumed by `finish_insert_session` on Esc-from-insert
448    /// when the reason is `AfterChange`. Mirrors vim's `:h '[` / `:h ']`
449    /// rule that `[` = start of change, `]` = last typed char on exit.
450    pub change_mark_start: Option<(usize, usize)>,
451    /// Bounded history of committed `/` / `?` search patterns. Newest
452    /// entries are at the back; capped at [`SEARCH_HISTORY_MAX`] to
453    /// avoid unbounded growth on long sessions.
454    pub search_history: Vec<String>,
455    /// Index into `search_history` while the user walks past patterns
456    /// in the prompt via `Ctrl-P` / `Ctrl-N`. `None` outside that walk
457    /// — typing or backspacing in the prompt resets it so the next
458    /// `Ctrl-P` starts from the most recent entry again.
459    pub search_history_cursor: Option<usize>,
460    /// Wall-clock instant of the last keystroke. Drives the
461    /// `:set timeoutlen` multi-key timeout — if `now() - last_input_at`
462    /// exceeds the configured budget, any pending prefix is cleared
463    /// before the new key dispatches. `None` before the first key.
464    /// 0.0.29 (Patch B): `:set timeoutlen` math now reads
465    /// [`crate::types::Host::now`] via `last_input_host_at`. This
466    /// `Instant`-flavoured field stays for snapshot tests that still
467    /// observe it directly.
468    pub last_input_at: Option<std::time::Instant>,
469    /// `Host::now()` reading at the last keystroke. Drives
470    /// `:set timeoutlen` so macro replay / headless drivers stay
471    /// deterministic regardless of wall-clock skew.
472    pub last_input_host_at: Option<core::time::Duration>,
473    /// Canonical current mode. Mirrors `mode` (the FSM-internal field)
474    /// AND is written by every Phase 6.3 primitive (`set_mode`,
475    /// `enter_visual_char_bridge`, …). Once the FSM is gone this is the
476    /// sole source of truth; until then both fields are kept in sync.
477    /// Initialized to `Normal` via `#[derive(Default)]`.
478    pub(crate) current_mode: crate::VimMode,
479}
480
481pub(crate) const SEARCH_HISTORY_MAX: usize = 100;
482pub(crate) const CHANGE_LIST_MAX: usize = 100;
483
484/// Active `/` or `?` search prompt. Text mutations drive the textarea's
485/// live search pattern so matches highlight as the user types.
486#[derive(Debug, Clone)]
487pub struct SearchPrompt {
488    pub text: String,
489    pub cursor: usize,
490    pub forward: bool,
491}
492
493#[derive(Debug, Clone)]
494pub struct InsertSession {
495    pub count: usize,
496    /// Min/max row visited during this session. Widens on every key.
497    pub row_min: usize,
498    pub row_max: usize,
499    /// Snapshot of the full buffer at session entry. Used to diff the
500    /// affected row window at finish without being fooled by cursor
501    /// navigation through rows the user never edited.
502    pub before_lines: Vec<String>,
503    pub reason: InsertReason,
504}
505
506#[derive(Debug, Clone)]
507pub enum InsertReason {
508    /// Plain entry via i/I/a/A — recorded as `InsertAt`.
509    Enter(InsertEntry),
510    /// Entry via `o`/`O` — records OpenLine on Esc.
511    Open { above: bool },
512    /// Entry via an operator's change side-effect. Retro-fills the
513    /// stored last-change's `inserted` field on Esc.
514    AfterChange,
515    /// Entry via `C` (delete to EOL + insert).
516    DeleteToEol,
517    /// Entry via an insert triggered during dot-replay — don't touch
518    /// last_change because the outer replay will restore it.
519    ReplayOnly,
520    /// `I` or `A` from VisualBlock: insert the typed text at `col` on
521    /// every row in `top..=bot`. `col` is the start column for `I`, the
522    /// one-past-block-end column for `A`.
523    BlockEdge { top: usize, bot: usize, col: usize },
524    /// `c` from VisualBlock: block content deleted, then user types
525    /// replacement text replicated across all block rows on Esc. Cursor
526    /// advances to the last typed char after replication (unlike BlockEdge
527    /// which leaves cursor at the insertion column).
528    BlockChange { top: usize, bot: usize, col: usize },
529    /// `R` — Replace mode. Each typed char overwrites the cell under
530    /// the cursor instead of inserting; at end-of-line the session
531    /// falls through to insert (same as vim).
532    Replace,
533}
534
535/// Saved visual-mode anchor + cursor for `gv` (re-enters the last
536/// visual selection). `mode` carries which visual flavour to
537/// restore; `anchor` / `cursor` mean different things per flavour:
538///
539/// - `Visual`     — `anchor` is the char-wise visual anchor.
540/// - `VisualLine` — `anchor.0` is the `visual_line_anchor` row;
541///   `anchor.1` is unused.
542/// - `VisualBlock`— `anchor` is `block_anchor`, `block_vcol` is the
543///   sticky vcol that survives j/k clamping.
544#[derive(Debug, Clone, Copy)]
545pub struct LastVisual {
546    pub mode: Mode,
547    pub anchor: (usize, usize),
548    pub cursor: (usize, usize),
549    pub block_vcol: usize,
550}
551
552impl VimState {
553    pub fn public_mode(&self) -> VimMode {
554        match self.mode {
555            Mode::Normal => VimMode::Normal,
556            Mode::Insert => VimMode::Insert,
557            Mode::Visual => VimMode::Visual,
558            Mode::VisualLine => VimMode::VisualLine,
559            Mode::VisualBlock => VimMode::VisualBlock,
560        }
561    }
562
563    pub fn force_normal(&mut self) {
564        self.mode = Mode::Normal;
565        self.pending = Pending::None;
566        self.count = 0;
567        self.insert_session = None;
568        // Phase 6.3: keep current_mode in sync for callers that bypass step().
569        self.current_mode = crate::VimMode::Normal;
570    }
571
572    /// Reset every prefix-tracking field so the next keystroke starts
573    /// a fresh sequence. Drives `:set timeoutlen` — when the user
574    /// pauses past the configured budget, `hjkl_vim::dispatch_input` calls
575    /// this before dispatching the new key.
576    ///
577    /// Resets: `pending`, `count`, `pending_register`,
578    /// `insert_pending_register`. Does NOT touch `mode`,
579    /// `insert_session`, marks, jump list, or visual anchors —
580    /// those aren't part of the in-flight chord.
581    pub(crate) fn clear_pending_prefix(&mut self) {
582        self.pending = Pending::None;
583        self.count = 0;
584        self.pending_register = None;
585        self.insert_pending_register = false;
586    }
587
588    /// Widen the active insert session's row window to include `row`. Called
589    /// by the Phase 6.1 public `Editor::insert_*` methods after each
590    /// mutation so `finish_insert_session` diffs the right range on Esc.
591    /// No-op when no insert session is active (e.g. calling from Normal mode).
592    pub(crate) fn widen_insert_row(&mut self, row: usize) {
593        if let Some(ref mut session) = self.insert_session {
594            session.row_min = session.row_min.min(row);
595            session.row_max = session.row_max.max(row);
596        }
597    }
598
599    pub fn is_visual(&self) -> bool {
600        matches!(
601            self.mode,
602            Mode::Visual | Mode::VisualLine | Mode::VisualBlock
603        )
604    }
605
606    pub fn is_visual_char(&self) -> bool {
607        self.mode == Mode::Visual
608    }
609
610    /// The pending repeat count (typed digits before a motion/operator),
611    /// or `None` when no digits are pending. Zero is treated as absent.
612    pub(crate) fn pending_count_val(&self) -> Option<u32> {
613        if self.count == 0 {
614            None
615        } else {
616            Some(self.count as u32)
617        }
618    }
619
620    /// `true` when an in-flight chord is awaiting more keys. Inverse of
621    /// `matches!(self.pending, Pending::None)`.
622    pub(crate) fn is_chord_pending(&self) -> bool {
623        !matches!(self.pending, Pending::None)
624    }
625
626    /// Return a single char representing the pending operator, if any.
627    /// Used by host apps (status line "showcmd" area) to display e.g.
628    /// `d`, `y`, `c` while waiting for a motion.
629    pub(crate) fn pending_op_char(&self) -> Option<char> {
630        let op = match &self.pending {
631            Pending::Op { op, .. }
632            | Pending::OpTextObj { op, .. }
633            | Pending::OpG { op, .. }
634            | Pending::OpFind { op, .. } => Some(*op),
635            _ => None,
636        };
637        op.map(|o| match o {
638            Operator::Delete => 'd',
639            Operator::Change => 'c',
640            Operator::Yank => 'y',
641            Operator::Uppercase => 'U',
642            Operator::Lowercase => 'u',
643            Operator::ToggleCase => '~',
644            Operator::Indent => '>',
645            Operator::Outdent => '<',
646            Operator::Fold => 'z',
647            Operator::Reflow => 'q',
648            Operator::AutoIndent => '=',
649        })
650    }
651}
652
653// ─── Entry point ───────────────────────────────────────────────────────────
654
655/// Open the `/` (forward) or `?` (backward) search prompt. Clears any
656/// live search highlight until the user commits a query. `last_search`
657/// is preserved so an empty `<CR>` can re-run the previous pattern.
658pub(crate) fn enter_search<H: crate::types::Host>(
659    ed: &mut Editor<hjkl_buffer::Buffer, H>,
660    forward: bool,
661) {
662    ed.vim.search_prompt = Some(SearchPrompt {
663        text: String::new(),
664        cursor: 0,
665        forward,
666    });
667    ed.vim.search_history_cursor = None;
668    // 0.0.37: clear via the engine search state (the buffer-side
669    // bridge from 0.0.35 was removed in this patch — the `BufferView`
670    // renderer reads the pattern from `Editor::search_state()`).
671    ed.set_search_pattern(None);
672}
673
674/// `g;` / `g,` body. `dir = -1` walks toward older entries (g;),
675/// `dir = 1` toward newer (g,). `count` repeats the step. Stops at
676/// the ends of the ring; off-ring positions are silently ignored.
677fn walk_change_list<H: crate::types::Host>(
678    ed: &mut Editor<hjkl_buffer::Buffer, H>,
679    dir: isize,
680    count: usize,
681) {
682    if ed.vim.change_list.is_empty() {
683        return;
684    }
685    let len = ed.vim.change_list.len();
686    let mut idx: isize = match (ed.vim.change_list_cursor, dir) {
687        (None, -1) => len as isize - 1,
688        (None, 1) => return, // already past the newest entry
689        (Some(i), -1) => i as isize - 1,
690        (Some(i), 1) => i as isize + 1,
691        _ => return,
692    };
693    for _ in 1..count {
694        let next = idx + dir;
695        if next < 0 || next >= len as isize {
696            break;
697        }
698        idx = next;
699    }
700    if idx < 0 || idx >= len as isize {
701        return;
702    }
703    let idx = idx as usize;
704    ed.vim.change_list_cursor = Some(idx);
705    let (row, col) = ed.vim.change_list[idx];
706    ed.jump_cursor(row, col);
707}
708
709/// `Ctrl-R {reg}` body — insert the named register's contents at the
710/// cursor as charwise text. Embedded newlines split lines naturally via
711/// `Edit::InsertStr`. Unknown selectors and empty slots are no-ops so
712/// stray keystrokes don't mutate the buffer.
713fn insert_register_text<H: crate::types::Host>(
714    ed: &mut Editor<hjkl_buffer::Buffer, H>,
715    selector: char,
716) {
717    use hjkl_buffer::Edit;
718    let text = match ed.registers().read(selector) {
719        Some(slot) if !slot.text.is_empty() => slot.text.clone(),
720        _ => return,
721    };
722    ed.sync_buffer_content_from_textarea();
723    let cursor = buf_cursor_pos(&ed.buffer);
724    ed.mutate_edit(Edit::InsertStr {
725        at: cursor,
726        text: text.clone(),
727    });
728    // Advance cursor to the end of the inserted payload — multi-line
729    // pastes land on the last inserted row at the post-text column.
730    let mut row = cursor.row;
731    let mut col = cursor.col;
732    for ch in text.chars() {
733        if ch == '\n' {
734            row += 1;
735            col = 0;
736        } else {
737            col += 1;
738        }
739    }
740    buf_set_cursor_rc(&mut ed.buffer, row, col);
741    ed.push_buffer_cursor_to_textarea();
742    ed.mark_content_dirty();
743    if let Some(ref mut session) = ed.vim.insert_session {
744        session.row_min = session.row_min.min(row);
745        session.row_max = session.row_max.max(row);
746    }
747}
748
749/// Compute the indent string to insert at the start of a new line
750/// after Enter is pressed at `cursor`. Walks the smartindent rules:
751///
752/// - autoindent off → empty string
753/// - autoindent on  → copy prev line's leading whitespace
754/// - smartindent on → bump one `shiftwidth` if prev line's last
755///   non-whitespace char is `{` / `(` / `[`
756///
757/// Indent unit (used for the smartindent bump):
758///
759/// - `expandtab && softtabstop > 0` → `softtabstop` spaces
760/// - `expandtab` → `shiftwidth` spaces
761/// - `!expandtab` → one literal `\t`
762///
763/// This is the placeholder for a future tree-sitter indent provider:
764/// when a language has an `indents.scm` query, the engine will route
765/// the same call through that provider and only fall back to this
766/// heuristic when no query matches.
767pub(super) fn compute_enter_indent(settings: &crate::editor::Settings, prev_line: &str) -> String {
768    if !settings.autoindent {
769        return String::new();
770    }
771    // Copy the prev line's leading whitespace (autoindent base).
772    let base: String = prev_line
773        .chars()
774        .take_while(|c| *c == ' ' || *c == '\t')
775        .collect();
776
777    if settings.smartindent {
778        // If the last non-whitespace character is an open bracket, bump
779        // indent by one unit. This is the heuristic seam: a tree-sitter
780        // `indents.scm` provider would replace this branch.
781        let last_non_ws = prev_line.chars().rev().find(|c| !c.is_whitespace());
782        if matches!(last_non_ws, Some('{' | '(' | '[')) {
783            let unit = if settings.expandtab {
784                if settings.softtabstop > 0 {
785                    " ".repeat(settings.softtabstop)
786                } else {
787                    " ".repeat(settings.shiftwidth)
788                }
789            } else {
790                "\t".to_string()
791            };
792            return format!("{base}{unit}");
793        }
794    }
795
796    base
797}
798
799/// Strip one indent unit from the beginning of `line` and insert `ch`
800/// instead. Returns `true` when it consumed the keystroke (dedent +
801/// insert), `false` when the caller should insert normally.
802///
803/// Dedent fires when:
804///   - `smartindent` is on
805///   - `ch` is `}` / `)` / `]`
806///   - all bytes BEFORE the cursor on the current line are whitespace
807///   - there is at least one full indent unit of leading whitespace
808fn try_dedent_close_bracket<H: crate::types::Host>(
809    ed: &mut Editor<hjkl_buffer::Buffer, H>,
810    cursor: hjkl_buffer::Position,
811    ch: char,
812) -> bool {
813    use hjkl_buffer::{Edit, MotionKind, Position};
814
815    if !ed.settings.smartindent {
816        return false;
817    }
818    if !matches!(ch, '}' | ')' | ']') {
819        return false;
820    }
821
822    let line = match buf_line(&ed.buffer, cursor.row) {
823        Some(l) => l.to_string(),
824        None => return false,
825    };
826
827    // All chars before cursor must be whitespace.
828    let before: String = line.chars().take(cursor.col).collect();
829    if !before.chars().all(|c| c == ' ' || c == '\t') {
830        return false;
831    }
832    if before.is_empty() {
833        // Nothing to strip — just insert normally (cursor at col 0).
834        return false;
835    }
836
837    // Compute indent unit.
838    let unit_len: usize = if ed.settings.expandtab {
839        if ed.settings.softtabstop > 0 {
840            ed.settings.softtabstop
841        } else {
842            ed.settings.shiftwidth
843        }
844    } else {
845        // Tab: one literal tab character.
846        1
847    };
848
849    // Check there's at least one full unit to strip.
850    let strip_len = if ed.settings.expandtab {
851        // Count leading spaces; need at least `unit_len`.
852        let spaces = before.chars().filter(|c| *c == ' ').count();
853        if spaces < unit_len {
854            return false;
855        }
856        unit_len
857    } else {
858        // noexpandtab: strip one leading tab.
859        if !before.starts_with('\t') {
860            return false;
861        }
862        1
863    };
864
865    // Delete the leading `strip_len` chars of the current line.
866    ed.mutate_edit(Edit::DeleteRange {
867        start: Position::new(cursor.row, 0),
868        end: Position::new(cursor.row, strip_len),
869        kind: MotionKind::Char,
870    });
871    // Insert the close bracket at column 0 (after the delete the cursor
872    // is still positioned at the end of the remaining whitespace; the
873    // delete moved the text so the cursor is now at col = before.len() -
874    // strip_len).
875    let new_col = cursor.col.saturating_sub(strip_len);
876    ed.mutate_edit(Edit::InsertChar {
877        at: Position::new(cursor.row, new_col),
878        ch,
879    });
880    true
881}
882
883fn finish_insert_session<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
884    let Some(session) = ed.vim.insert_session.take() else {
885        return;
886    };
887    let lines = buf_lines_to_vec(&ed.buffer);
888    // Clamp both slices to their respective bounds — the buffer may have
889    // grown (Enter splits rows) or shrunk (Backspace joins rows) during
890    // the session, so row_max can overshoot either side.
891    let after_end = session.row_max.min(lines.len().saturating_sub(1));
892    let before_end = session
893        .row_max
894        .min(session.before_lines.len().saturating_sub(1));
895    let before = if before_end >= session.row_min && session.row_min < session.before_lines.len() {
896        session.before_lines[session.row_min..=before_end].join("\n")
897    } else {
898        String::new()
899    };
900    let after = if after_end >= session.row_min && session.row_min < lines.len() {
901        lines[session.row_min..=after_end].join("\n")
902    } else {
903        String::new()
904    };
905    let inserted = extract_inserted(&before, &after);
906    if !inserted.is_empty() && session.count > 1 && !ed.vim.replaying {
907        use hjkl_buffer::{Edit, Position};
908        for _ in 0..session.count - 1 {
909            let (row, col) = ed.cursor();
910            ed.mutate_edit(Edit::InsertStr {
911                at: Position::new(row, col),
912                text: inserted.clone(),
913            });
914        }
915    }
916    // Helper: replicate `inserted` text across block rows top+1..=bot at `col`,
917    // padding short rows to reach `col` first. Returns without touching the
918    // cursor — callers position the cursor afterward according to their needs.
919    fn replicate_block_text<H: crate::types::Host>(
920        ed: &mut Editor<hjkl_buffer::Buffer, H>,
921        inserted: &str,
922        top: usize,
923        bot: usize,
924        col: usize,
925    ) {
926        use hjkl_buffer::{Edit, Position};
927        for r in (top + 1)..=bot {
928            let line_len = buf_line_chars(&ed.buffer, r);
929            if col > line_len {
930                let pad: String = std::iter::repeat_n(' ', col - line_len).collect();
931                ed.mutate_edit(Edit::InsertStr {
932                    at: Position::new(r, line_len),
933                    text: pad,
934                });
935            }
936            ed.mutate_edit(Edit::InsertStr {
937                at: Position::new(r, col),
938                text: inserted.to_string(),
939            });
940        }
941    }
942
943    if let InsertReason::BlockEdge { top, bot, col } = session.reason {
944        // `I` / `A` from VisualBlock: replicate text across rows; cursor
945        // stays at the block-start column (vim leaves cursor there).
946        if !inserted.is_empty() && top < bot && !ed.vim.replaying {
947            replicate_block_text(ed, &inserted, top, bot, col);
948            buf_set_cursor_rc(&mut ed.buffer, top, col);
949            ed.push_buffer_cursor_to_textarea();
950        }
951        return;
952    }
953    if let InsertReason::BlockChange { top, bot, col } = session.reason {
954        // `c` from VisualBlock: replicate text across rows; cursor advances
955        // to `col + ins_chars` (pre-step-back) so the Esc step-back lands
956        // on the last typed char (col + ins_chars - 1), matching nvim.
957        if !inserted.is_empty() && top < bot && !ed.vim.replaying {
958            replicate_block_text(ed, &inserted, top, bot, col);
959            let ins_chars = inserted.chars().count();
960            let line_len = buf_line_chars(&ed.buffer, top);
961            let target_col = (col + ins_chars).min(line_len);
962            buf_set_cursor_rc(&mut ed.buffer, top, target_col);
963            ed.push_buffer_cursor_to_textarea();
964        }
965        return;
966    }
967    if ed.vim.replaying {
968        return;
969    }
970    match session.reason {
971        InsertReason::Enter(entry) => {
972            ed.vim.last_change = Some(LastChange::InsertAt {
973                entry,
974                inserted,
975                count: session.count,
976            });
977        }
978        InsertReason::Open { above } => {
979            ed.vim.last_change = Some(LastChange::OpenLine { above, inserted });
980        }
981        InsertReason::AfterChange => {
982            if let Some(
983                LastChange::OpMotion { inserted: ins, .. }
984                | LastChange::OpTextObj { inserted: ins, .. }
985                | LastChange::LineOp { inserted: ins, .. },
986            ) = ed.vim.last_change.as_mut()
987            {
988                *ins = Some(inserted);
989            }
990            // Vim `:h '[` / `:h ']`: on change, `[` = start of the
991            // changed range (stashed before the cut), `]` = the cursor
992            // at Esc time (last inserted char, before the step-back).
993            // When nothing was typed cursor still sits at the change
994            // start, satisfying vim's "both at start" parity for `c<m><Esc>`.
995            if let Some(start) = ed.vim.change_mark_start.take() {
996                let end = ed.cursor();
997                ed.set_mark('[', start);
998                ed.set_mark(']', end);
999            }
1000        }
1001        InsertReason::DeleteToEol => {
1002            ed.vim.last_change = Some(LastChange::DeleteToEol {
1003                inserted: Some(inserted),
1004            });
1005        }
1006        InsertReason::ReplayOnly => {}
1007        InsertReason::BlockEdge { .. } => unreachable!("handled above"),
1008        InsertReason::BlockChange { .. } => unreachable!("handled above"),
1009        InsertReason::Replace => {
1010            // Record overstrike sessions as DeleteToEol-style — replay
1011            // re-types each character but doesn't try to restore prior
1012            // content (vim's R has its own replay path; this is the
1013            // pragmatic approximation).
1014            ed.vim.last_change = Some(LastChange::DeleteToEol {
1015                inserted: Some(inserted),
1016            });
1017        }
1018    }
1019}
1020
1021pub(crate) fn begin_insert<H: crate::types::Host>(
1022    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1023    count: usize,
1024    reason: InsertReason,
1025) {
1026    let record = !matches!(reason, InsertReason::ReplayOnly);
1027    if record {
1028        ed.push_undo();
1029    }
1030    let reason = if ed.vim.replaying {
1031        InsertReason::ReplayOnly
1032    } else {
1033        reason
1034    };
1035    let (row, _) = ed.cursor();
1036    ed.vim.insert_session = Some(InsertSession {
1037        count,
1038        row_min: row,
1039        row_max: row,
1040        before_lines: buf_lines_to_vec(&ed.buffer),
1041        reason,
1042    });
1043    ed.vim.mode = Mode::Insert;
1044    // Phase 6.3: keep current_mode in sync for callers that bypass step().
1045    ed.vim.current_mode = crate::VimMode::Insert;
1046}
1047
1048/// `:set undobreak` semantics for insert-mode motions. When the
1049/// toggle is on, a non-character keystroke that moves the cursor
1050/// (arrow keys, Home/End, mouse click) ends the current undo group
1051/// and starts a new one mid-session. After this, a subsequent `u`
1052/// in normal mode reverts only the post-break run, leaving the
1053/// pre-break edits in place — matching vim's behaviour.
1054///
1055/// Implementation: snapshot the current buffer onto the undo stack
1056/// (the new break point) and reset the active `InsertSession`'s
1057/// `before_lines` so `finish_insert_session`'s diff window only
1058/// captures the post-break run for `last_change` / dot-repeat.
1059///
1060/// During replay we skip the break — replay shouldn't pollute the
1061/// undo stack with intra-replay snapshots.
1062pub(crate) fn break_undo_group_in_insert<H: crate::types::Host>(
1063    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1064) {
1065    if !ed.settings.undo_break_on_motion {
1066        return;
1067    }
1068    if ed.vim.replaying {
1069        return;
1070    }
1071    if ed.vim.insert_session.is_none() {
1072        return;
1073    }
1074    ed.push_undo();
1075    let n = crate::types::Query::line_count(&ed.buffer) as usize;
1076    let mut lines: Vec<String> = Vec::with_capacity(n);
1077    for r in 0..n {
1078        lines.push(crate::types::Query::line(&ed.buffer, r as u32).to_string());
1079    }
1080    let row = crate::types::Cursor::cursor(&ed.buffer).line as usize;
1081    if let Some(ref mut session) = ed.vim.insert_session {
1082        session.before_lines = lines;
1083        session.row_min = row;
1084        session.row_max = row;
1085    }
1086}
1087
1088// ─── Phase 6.1: public insert-mode primitives ──────────────────────────────
1089//
1090// Each `pub(crate)` free function below implements one insert-mode action.
1091// hjkl-vim's insert dispatcher calls them through `Editor::insert_*` methods.
1092// External callers can also invoke the public Editor methods directly.
1093//
1094// Invariants every function upholds:
1095//   - Opens with `ed.sync_buffer_content_from_textarea()` (no-op, kept for
1096//     forward compatibility once textarea is gone).
1097//   - All buffer mutations go through `ed.mutate_edit(...)` so dirty flag,
1098//     undo, change-list, content-edit fan-out all fire uniformly.
1099//   - Navigation-only functions call `break_undo_group_in_insert` when the
1100//     FSM did so, then return `false` (no mutation).
1101//   - After mutations, `ed.push_buffer_cursor_to_textarea()` is called
1102//     (currently a no-op but kept for migration hygiene).
1103//   - Returns `true` when the buffer was mutated, `false` otherwise.
1104
1105/// Insert a single character at the cursor. Handles replace-mode overstrike
1106/// (when `InsertSession::reason` is `Replace`) and smart-indent dedent of
1107/// closing brackets (}/)]/). Returns `true`.
1108pub(crate) fn insert_char_bridge<H: crate::types::Host>(
1109    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1110    ch: char,
1111) -> bool {
1112    use hjkl_buffer::{Edit, MotionKind, Position};
1113    ed.sync_buffer_content_from_textarea();
1114    let cursor = buf_cursor_pos(&ed.buffer);
1115    let line_chars = buf_line_chars(&ed.buffer, cursor.row);
1116    let in_replace = matches!(
1117        ed.vim.insert_session.as_ref().map(|s| &s.reason),
1118        Some(InsertReason::Replace)
1119    );
1120    if in_replace && cursor.col < line_chars {
1121        ed.mutate_edit(Edit::DeleteRange {
1122            start: cursor,
1123            end: Position::new(cursor.row, cursor.col + 1),
1124            kind: MotionKind::Char,
1125        });
1126        ed.mutate_edit(Edit::InsertChar { at: cursor, ch });
1127    } else if !try_dedent_close_bracket(ed, cursor, ch) {
1128        ed.mutate_edit(Edit::InsertChar { at: cursor, ch });
1129    }
1130    ed.push_buffer_cursor_to_textarea();
1131    true
1132}
1133
1134/// Insert a newline at the cursor, applying autoindent / smartindent.
1135/// Returns `true`.
1136pub(crate) fn insert_newline_bridge<H: crate::types::Host>(
1137    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1138) -> bool {
1139    use hjkl_buffer::Edit;
1140    ed.sync_buffer_content_from_textarea();
1141    let cursor = buf_cursor_pos(&ed.buffer);
1142    let prev_line = buf_line(&ed.buffer, cursor.row)
1143        .unwrap_or_default()
1144        .to_string();
1145    let indent = compute_enter_indent(&ed.settings, &prev_line);
1146    let text = format!("\n{indent}");
1147    ed.mutate_edit(Edit::InsertStr { at: cursor, text });
1148    ed.push_buffer_cursor_to_textarea();
1149    true
1150}
1151
1152/// Insert a tab character (or spaces up to the next softtabstop boundary when
1153/// `expandtab` is set). Returns `true`.
1154pub(crate) fn insert_tab_bridge<H: crate::types::Host>(
1155    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1156) -> bool {
1157    use hjkl_buffer::Edit;
1158    ed.sync_buffer_content_from_textarea();
1159    let cursor = buf_cursor_pos(&ed.buffer);
1160    if ed.settings.expandtab {
1161        let sts = ed.settings.softtabstop;
1162        let n = if sts > 0 {
1163            sts - (cursor.col % sts)
1164        } else {
1165            ed.settings.tabstop.max(1)
1166        };
1167        ed.mutate_edit(Edit::InsertStr {
1168            at: cursor,
1169            text: " ".repeat(n),
1170        });
1171    } else {
1172        ed.mutate_edit(Edit::InsertChar {
1173            at: cursor,
1174            ch: '\t',
1175        });
1176    }
1177    ed.push_buffer_cursor_to_textarea();
1178    true
1179}
1180
1181/// Delete the character before the cursor (vim Backspace / `^H`). With
1182/// `softtabstop` active, deletes the entire soft-tab run at an aligned
1183/// boundary. Joins with the previous line when at column 0. Returns
1184/// `true` when something was deleted, `false` at the very start of the
1185/// buffer.
1186pub(crate) fn insert_backspace_bridge<H: crate::types::Host>(
1187    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1188) -> bool {
1189    use hjkl_buffer::{Edit, MotionKind, Position};
1190    ed.sync_buffer_content_from_textarea();
1191    let cursor = buf_cursor_pos(&ed.buffer);
1192    let sts = ed.settings.softtabstop;
1193    if sts > 0 && cursor.col >= sts && cursor.col.is_multiple_of(sts) {
1194        let line = buf_line(&ed.buffer, cursor.row).unwrap_or_default();
1195        let chars: Vec<char> = line.chars().collect();
1196        let run_start = cursor.col - sts;
1197        if (run_start..cursor.col).all(|i| chars.get(i).copied() == Some(' ')) {
1198            ed.mutate_edit(Edit::DeleteRange {
1199                start: Position::new(cursor.row, run_start),
1200                end: cursor,
1201                kind: MotionKind::Char,
1202            });
1203            ed.push_buffer_cursor_to_textarea();
1204            return true;
1205        }
1206    }
1207    let result = if cursor.col > 0 {
1208        ed.mutate_edit(Edit::DeleteRange {
1209            start: Position::new(cursor.row, cursor.col - 1),
1210            end: cursor,
1211            kind: MotionKind::Char,
1212        });
1213        true
1214    } else if cursor.row > 0 {
1215        let prev_row = cursor.row - 1;
1216        let prev_chars = buf_line_chars(&ed.buffer, prev_row);
1217        ed.mutate_edit(Edit::JoinLines {
1218            row: prev_row,
1219            count: 1,
1220            with_space: false,
1221        });
1222        buf_set_cursor_rc(&mut ed.buffer, prev_row, prev_chars);
1223        true
1224    } else {
1225        false
1226    };
1227    ed.push_buffer_cursor_to_textarea();
1228    result
1229}
1230
1231/// Delete the character under the cursor (vim `Delete`). Joins with the
1232/// next line when at end-of-line. Returns `true` when something was deleted.
1233pub(crate) fn insert_delete_bridge<H: crate::types::Host>(
1234    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1235) -> bool {
1236    use hjkl_buffer::{Edit, MotionKind, Position};
1237    ed.sync_buffer_content_from_textarea();
1238    let cursor = buf_cursor_pos(&ed.buffer);
1239    let line_chars = buf_line_chars(&ed.buffer, cursor.row);
1240    let result = if cursor.col < line_chars {
1241        ed.mutate_edit(Edit::DeleteRange {
1242            start: cursor,
1243            end: Position::new(cursor.row, cursor.col + 1),
1244            kind: MotionKind::Char,
1245        });
1246        buf_set_cursor_pos(&mut ed.buffer, cursor);
1247        true
1248    } else if cursor.row + 1 < buf_row_count(&ed.buffer) {
1249        ed.mutate_edit(Edit::JoinLines {
1250            row: cursor.row,
1251            count: 1,
1252            with_space: false,
1253        });
1254        buf_set_cursor_pos(&mut ed.buffer, cursor);
1255        true
1256    } else {
1257        false
1258    };
1259    ed.push_buffer_cursor_to_textarea();
1260    result
1261}
1262
1263/// Direction for insert-mode arrow movement.
1264#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1265pub enum InsertDir {
1266    Left,
1267    Right,
1268    Up,
1269    Down,
1270}
1271
1272/// Move the cursor one step in `dir`, breaking the undo group per
1273/// `undo_break_on_motion`. Returns `false` (no mutation).
1274pub(crate) fn insert_arrow_bridge<H: crate::types::Host>(
1275    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1276    dir: InsertDir,
1277) -> bool {
1278    ed.sync_buffer_content_from_textarea();
1279    match dir {
1280        InsertDir::Left => {
1281            crate::motions::move_left(&mut ed.buffer, 1);
1282        }
1283        InsertDir::Right => {
1284            crate::motions::move_right_to_end(&mut ed.buffer, 1);
1285        }
1286        InsertDir::Up => {
1287            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
1288            crate::motions::move_up(&mut ed.buffer, &folds, 1, &mut ed.sticky_col);
1289        }
1290        InsertDir::Down => {
1291            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
1292            crate::motions::move_down(&mut ed.buffer, &folds, 1, &mut ed.sticky_col);
1293        }
1294    }
1295    break_undo_group_in_insert(ed);
1296    ed.push_buffer_cursor_to_textarea();
1297    false
1298}
1299
1300/// Move the cursor to the start of the current line, breaking the undo group.
1301/// Returns `false` (no mutation).
1302pub(crate) fn insert_home_bridge<H: crate::types::Host>(
1303    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1304) -> bool {
1305    ed.sync_buffer_content_from_textarea();
1306    crate::motions::move_line_start(&mut ed.buffer);
1307    break_undo_group_in_insert(ed);
1308    ed.push_buffer_cursor_to_textarea();
1309    false
1310}
1311
1312/// Move the cursor to the end of the current line, breaking the undo group.
1313/// Returns `false` (no mutation).
1314pub(crate) fn insert_end_bridge<H: crate::types::Host>(
1315    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1316) -> bool {
1317    ed.sync_buffer_content_from_textarea();
1318    crate::motions::move_line_end(&mut ed.buffer);
1319    break_undo_group_in_insert(ed);
1320    ed.push_buffer_cursor_to_textarea();
1321    false
1322}
1323
1324/// Scroll up one full viewport height, moving the cursor with it.
1325/// Breaks the undo group. Returns `false` (no mutation).
1326pub(crate) fn insert_pageup_bridge<H: crate::types::Host>(
1327    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1328    viewport_h: u16,
1329) -> bool {
1330    let rows = viewport_h.saturating_sub(2).max(1) as isize;
1331    scroll_cursor_rows(ed, -rows);
1332    false
1333}
1334
1335/// Scroll down one full viewport height, moving the cursor with it.
1336/// Breaks the undo group. Returns `false` (no mutation).
1337pub(crate) fn insert_pagedown_bridge<H: crate::types::Host>(
1338    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1339    viewport_h: u16,
1340) -> bool {
1341    let rows = viewport_h.saturating_sub(2).max(1) as isize;
1342    scroll_cursor_rows(ed, rows);
1343    false
1344}
1345
1346/// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
1347/// At col 0, joins with the previous line (vim semantics). Returns `true`
1348/// when something was deleted.
1349pub(crate) fn insert_ctrl_w_bridge<H: crate::types::Host>(
1350    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1351) -> bool {
1352    use hjkl_buffer::{Edit, MotionKind};
1353    ed.sync_buffer_content_from_textarea();
1354    let cursor = buf_cursor_pos(&ed.buffer);
1355    if cursor.row == 0 && cursor.col == 0 {
1356        return true;
1357    }
1358    crate::motions::move_word_back(&mut ed.buffer, false, 1, &ed.settings.iskeyword);
1359    let word_start = buf_cursor_pos(&ed.buffer);
1360    if word_start == cursor {
1361        return true;
1362    }
1363    buf_set_cursor_pos(&mut ed.buffer, cursor);
1364    ed.mutate_edit(Edit::DeleteRange {
1365        start: word_start,
1366        end: cursor,
1367        kind: MotionKind::Char,
1368    });
1369    ed.push_buffer_cursor_to_textarea();
1370    true
1371}
1372
1373/// Delete from the cursor back to the start of the current line (`Ctrl-U`).
1374/// No-op when already at column 0. Returns `true` when something was deleted.
1375pub(crate) fn insert_ctrl_u_bridge<H: crate::types::Host>(
1376    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1377) -> bool {
1378    use hjkl_buffer::{Edit, MotionKind, Position};
1379    ed.sync_buffer_content_from_textarea();
1380    let cursor = buf_cursor_pos(&ed.buffer);
1381    if cursor.col > 0 {
1382        ed.mutate_edit(Edit::DeleteRange {
1383            start: Position::new(cursor.row, 0),
1384            end: cursor,
1385            kind: MotionKind::Char,
1386        });
1387        ed.push_buffer_cursor_to_textarea();
1388    }
1389    true
1390}
1391
1392/// Delete one character backwards (`Ctrl-H`) — alias for Backspace in insert
1393/// mode. Joins with the previous line when at col 0. Returns `true` when
1394/// something was deleted.
1395pub(crate) fn insert_ctrl_h_bridge<H: crate::types::Host>(
1396    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1397) -> bool {
1398    use hjkl_buffer::{Edit, MotionKind, Position};
1399    ed.sync_buffer_content_from_textarea();
1400    let cursor = buf_cursor_pos(&ed.buffer);
1401    if cursor.col > 0 {
1402        ed.mutate_edit(Edit::DeleteRange {
1403            start: Position::new(cursor.row, cursor.col - 1),
1404            end: cursor,
1405            kind: MotionKind::Char,
1406        });
1407    } else if cursor.row > 0 {
1408        let prev_row = cursor.row - 1;
1409        let prev_chars = buf_line_chars(&ed.buffer, prev_row);
1410        ed.mutate_edit(Edit::JoinLines {
1411            row: prev_row,
1412            count: 1,
1413            with_space: false,
1414        });
1415        buf_set_cursor_rc(&mut ed.buffer, prev_row, prev_chars);
1416    }
1417    ed.push_buffer_cursor_to_textarea();
1418    true
1419}
1420
1421/// Indent the current line by one `shiftwidth` and shift the cursor right by
1422/// the same amount (`Ctrl-T`). Returns `true`.
1423pub(crate) fn insert_ctrl_t_bridge<H: crate::types::Host>(
1424    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1425) -> bool {
1426    let (row, col) = ed.cursor();
1427    let sw = ed.settings().shiftwidth;
1428    indent_rows(ed, row, row, 1);
1429    ed.jump_cursor(row, col + sw);
1430    true
1431}
1432
1433/// Outdent the current line by up to one `shiftwidth` and shift the cursor
1434/// left by the amount stripped (`Ctrl-D`). Returns `true`.
1435pub(crate) fn insert_ctrl_d_bridge<H: crate::types::Host>(
1436    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1437) -> bool {
1438    let (row, col) = ed.cursor();
1439    let before_len = buf_line_bytes(&ed.buffer, row);
1440    outdent_rows(ed, row, row, 1);
1441    let after_len = buf_line_bytes(&ed.buffer, row);
1442    let stripped = before_len.saturating_sub(after_len);
1443    let new_col = col.saturating_sub(stripped);
1444    ed.jump_cursor(row, new_col);
1445    true
1446}
1447
1448/// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
1449/// complete normal-mode command, then return to insert. Returns `false`
1450/// (no buffer mutation — only mode state changes).
1451pub(crate) fn insert_ctrl_o_bridge<H: crate::types::Host>(
1452    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1453) -> bool {
1454    ed.vim.one_shot_normal = true;
1455    ed.vim.mode = Mode::Normal;
1456    // Phase 6.3: keep current_mode in sync for callers that bypass step().
1457    ed.vim.current_mode = crate::VimMode::Normal;
1458    false
1459}
1460
1461/// Arm the register-paste selector (`Ctrl-R`): the next typed character
1462/// names the register whose text will be inserted inline. Returns `false`
1463/// (no buffer mutation yet — mutation happens when the register char arrives).
1464pub(crate) fn insert_ctrl_r_bridge<H: crate::types::Host>(
1465    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1466) -> bool {
1467    ed.vim.insert_pending_register = true;
1468    false
1469}
1470
1471/// Paste the contents of `reg` at the cursor (the body of `Ctrl-R {reg}`).
1472/// Unknown or empty registers are a no-op. Returns `true` when text was
1473/// inserted.
1474pub(crate) fn insert_paste_register_bridge<H: crate::types::Host>(
1475    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1476    reg: char,
1477) -> bool {
1478    insert_register_text(ed, reg);
1479    // insert_register_text already calls mark_content_dirty internally;
1480    // return true to signal that the session row window should be widened.
1481    true
1482}
1483
1484/// Exit insert mode to Normal: finish the insert session, step the cursor one
1485/// cell left (vim convention), record the `gi` target, and update the sticky
1486/// column. Returns `true` (always consumed — even if no buffer mutation, the
1487/// mode change itself is a meaningful step).
1488pub(crate) fn leave_insert_to_normal_bridge<H: crate::types::Host>(
1489    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1490) -> bool {
1491    finish_insert_session(ed);
1492    ed.vim.mode = Mode::Normal;
1493    // Phase 6.3: keep current_mode in sync for callers that bypass step().
1494    ed.vim.current_mode = crate::VimMode::Normal;
1495    let col = ed.cursor().1;
1496    ed.vim.last_insert_pos = Some(ed.cursor());
1497    if col > 0 {
1498        crate::motions::move_left(&mut ed.buffer, 1);
1499        ed.push_buffer_cursor_to_textarea();
1500    }
1501    ed.sticky_col = Some(ed.cursor().1);
1502    true
1503}
1504
1505// ─── Phase 6.2: normal-mode primitive bridges ──────────────────────────────
1506
1507/// Scroll direction for `scroll_full_page`, `scroll_half_page`, and
1508/// `scroll_line` controller methods.
1509#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1510pub enum ScrollDir {
1511    /// Move forward / downward (toward end of buffer).
1512    Down,
1513    /// Move backward / upward (toward start of buffer).
1514    Up,
1515}
1516
1517// ── Insert-mode entry bridges ──────────────────────────────────────────────
1518
1519/// `i` — begin Insert at the cursor. `count` is stored in the session for
1520/// insert-exit replay. Returns `true`.
1521pub(crate) fn enter_insert_i_bridge<H: crate::types::Host>(
1522    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1523    count: usize,
1524) {
1525    begin_insert(ed, count.max(1), InsertReason::Enter(InsertEntry::I));
1526}
1527
1528/// `I` — move to first non-blank then begin Insert. `count` stored for replay.
1529pub(crate) fn enter_insert_shift_i_bridge<H: crate::types::Host>(
1530    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1531    count: usize,
1532) {
1533    move_first_non_whitespace(ed);
1534    begin_insert(ed, count.max(1), InsertReason::Enter(InsertEntry::ShiftI));
1535}
1536
1537/// `a` — advance past the cursor char then begin Insert. `count` for replay.
1538pub(crate) fn enter_insert_a_bridge<H: crate::types::Host>(
1539    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1540    count: usize,
1541) {
1542    crate::motions::move_right_to_end(&mut ed.buffer, 1);
1543    ed.push_buffer_cursor_to_textarea();
1544    begin_insert(ed, count.max(1), InsertReason::Enter(InsertEntry::A));
1545}
1546
1547/// `A` — move to end-of-line then begin Insert. `count` for replay.
1548pub(crate) fn enter_insert_shift_a_bridge<H: crate::types::Host>(
1549    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1550    count: usize,
1551) {
1552    crate::motions::move_line_end(&mut ed.buffer);
1553    crate::motions::move_right_to_end(&mut ed.buffer, 1);
1554    ed.push_buffer_cursor_to_textarea();
1555    begin_insert(ed, count.max(1), InsertReason::Enter(InsertEntry::ShiftA));
1556}
1557
1558/// `o` — open a new line below the cursor and begin Insert.
1559pub(crate) fn open_line_below_bridge<H: crate::types::Host>(
1560    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1561    count: usize,
1562) {
1563    use hjkl_buffer::{Edit, Position};
1564    ed.push_undo();
1565    begin_insert_noundo(ed, count.max(1), InsertReason::Open { above: false });
1566    ed.sync_buffer_content_from_textarea();
1567    let row = buf_cursor_pos(&ed.buffer).row;
1568    let line_chars = buf_line_chars(&ed.buffer, row);
1569    let prev_line = buf_line(&ed.buffer, row).unwrap_or_default();
1570    let indent = compute_enter_indent(&ed.settings, prev_line);
1571    ed.mutate_edit(Edit::InsertStr {
1572        at: Position::new(row, line_chars),
1573        text: format!("\n{indent}"),
1574    });
1575    ed.push_buffer_cursor_to_textarea();
1576}
1577
1578/// `O` — open a new line above the cursor and begin Insert.
1579pub(crate) fn open_line_above_bridge<H: crate::types::Host>(
1580    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1581    count: usize,
1582) {
1583    use hjkl_buffer::{Edit, Position};
1584    ed.push_undo();
1585    begin_insert_noundo(ed, count.max(1), InsertReason::Open { above: true });
1586    ed.sync_buffer_content_from_textarea();
1587    let row = buf_cursor_pos(&ed.buffer).row;
1588    let indent = if row > 0 {
1589        let above = buf_line(&ed.buffer, row - 1).unwrap_or_default();
1590        compute_enter_indent(&ed.settings, above)
1591    } else {
1592        let cur = buf_line(&ed.buffer, row).unwrap_or_default();
1593        cur.chars()
1594            .take_while(|c| *c == ' ' || *c == '\t')
1595            .collect::<String>()
1596    };
1597    ed.mutate_edit(Edit::InsertStr {
1598        at: Position::new(row, 0),
1599        text: format!("{indent}\n"),
1600    });
1601    let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
1602    crate::motions::move_up(&mut ed.buffer, &folds, 1, &mut ed.sticky_col);
1603    let new_row = buf_cursor_pos(&ed.buffer).row;
1604    buf_set_cursor_rc(&mut ed.buffer, new_row, indent.chars().count());
1605    ed.push_buffer_cursor_to_textarea();
1606}
1607
1608/// `R` — enter Replace mode (overstrike). `count` stored for replay.
1609pub(crate) fn enter_replace_mode_bridge<H: crate::types::Host>(
1610    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1611    count: usize,
1612) {
1613    begin_insert(ed, count.max(1), InsertReason::Replace);
1614}
1615
1616// ── Char / line ops ────────────────────────────────────────────────────────
1617
1618/// `x` — delete `count` chars forward from the cursor, writing to the unnamed
1619/// register. Records `LastChange::CharDel` for dot-repeat.
1620pub(crate) fn delete_char_forward_bridge<H: crate::types::Host>(
1621    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1622    count: usize,
1623) {
1624    do_char_delete(ed, true, count.max(1));
1625    if !ed.vim.replaying {
1626        ed.vim.last_change = Some(LastChange::CharDel {
1627            forward: true,
1628            count: count.max(1),
1629        });
1630    }
1631}
1632
1633/// `X` — delete `count` chars backward from the cursor, writing to the unnamed
1634/// register. Records `LastChange::CharDel` for dot-repeat.
1635pub(crate) fn delete_char_backward_bridge<H: crate::types::Host>(
1636    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1637    count: usize,
1638) {
1639    do_char_delete(ed, false, count.max(1));
1640    if !ed.vim.replaying {
1641        ed.vim.last_change = Some(LastChange::CharDel {
1642            forward: false,
1643            count: count.max(1),
1644        });
1645    }
1646}
1647
1648/// `s` — substitute `count` chars (delete then enter Insert). Equivalent to
1649/// `cl`. Records `LastChange::OpMotion` for dot-repeat.
1650pub(crate) fn substitute_char_bridge<H: crate::types::Host>(
1651    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1652    count: usize,
1653) {
1654    use hjkl_buffer::{Edit, MotionKind, Position};
1655    ed.push_undo();
1656    ed.sync_buffer_content_from_textarea();
1657    for _ in 0..count.max(1) {
1658        let cursor = buf_cursor_pos(&ed.buffer);
1659        let line_chars = buf_line_chars(&ed.buffer, cursor.row);
1660        if cursor.col >= line_chars {
1661            break;
1662        }
1663        ed.mutate_edit(Edit::DeleteRange {
1664            start: cursor,
1665            end: Position::new(cursor.row, cursor.col + 1),
1666            kind: MotionKind::Char,
1667        });
1668    }
1669    ed.push_buffer_cursor_to_textarea();
1670    begin_insert_noundo(ed, 1, InsertReason::AfterChange);
1671    if !ed.vim.replaying {
1672        ed.vim.last_change = Some(LastChange::OpMotion {
1673            op: Operator::Change,
1674            motion: Motion::Right,
1675            count: count.max(1),
1676            inserted: None,
1677        });
1678    }
1679}
1680
1681/// `S` — substitute the whole line (delete line contents then enter Insert).
1682/// Equivalent to `cc`. Records `LastChange::LineOp` for dot-repeat.
1683pub(crate) fn substitute_line_bridge<H: crate::types::Host>(
1684    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1685    count: usize,
1686) {
1687    execute_line_op(ed, Operator::Change, count.max(1));
1688    if !ed.vim.replaying {
1689        ed.vim.last_change = Some(LastChange::LineOp {
1690            op: Operator::Change,
1691            count: count.max(1),
1692            inserted: None,
1693        });
1694    }
1695}
1696
1697/// `D` — delete from the cursor to end-of-line, writing to the unnamed
1698/// register. Cursor parks on the new last char. Records for dot-repeat.
1699pub(crate) fn delete_to_eol_bridge<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
1700    ed.push_undo();
1701    delete_to_eol(ed);
1702    crate::motions::move_left(&mut ed.buffer, 1);
1703    ed.push_buffer_cursor_to_textarea();
1704    if !ed.vim.replaying {
1705        ed.vim.last_change = Some(LastChange::DeleteToEol { inserted: None });
1706    }
1707}
1708
1709/// `C` — change from the cursor to end-of-line (delete then enter Insert).
1710/// Equivalent to `c$`. Shares the delete path with `D`.
1711pub(crate) fn change_to_eol_bridge<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
1712    ed.push_undo();
1713    delete_to_eol(ed);
1714    begin_insert_noundo(ed, 1, InsertReason::DeleteToEol);
1715}
1716
1717/// `Y` — yank from the cursor to end-of-line (same as `y$` in Vim 8 default).
1718pub(crate) fn yank_to_eol_bridge<H: crate::types::Host>(
1719    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1720    count: usize,
1721) {
1722    apply_op_with_motion(ed, Operator::Yank, &Motion::LineEnd, count.max(1));
1723}
1724
1725/// `J` — join `count` lines (default 2) onto the current one, inserting a
1726/// single space between each pair (vim semantics). Records for dot-repeat.
1727pub(crate) fn join_line_bridge<H: crate::types::Host>(
1728    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1729    count: usize,
1730) {
1731    for _ in 0..count.max(1) {
1732        ed.push_undo();
1733        join_line(ed);
1734    }
1735    if !ed.vim.replaying {
1736        ed.vim.last_change = Some(LastChange::JoinLine {
1737            count: count.max(1),
1738        });
1739    }
1740}
1741
1742/// `~` — toggle the case of `count` chars from the cursor, advancing right.
1743/// Records `LastChange::ToggleCase` for dot-repeat.
1744pub(crate) fn toggle_case_at_cursor_bridge<H: crate::types::Host>(
1745    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1746    count: usize,
1747) {
1748    for _ in 0..count.max(1) {
1749        ed.push_undo();
1750        toggle_case_at_cursor(ed);
1751    }
1752    if !ed.vim.replaying {
1753        ed.vim.last_change = Some(LastChange::ToggleCase {
1754            count: count.max(1),
1755        });
1756    }
1757}
1758
1759/// `p` — paste the unnamed register (or `"reg` register) after the cursor.
1760/// Linewise yanks open a new line below; charwise pastes inline.
1761/// Records `LastChange::Paste` for dot-repeat.
1762pub(crate) fn paste_after_bridge<H: crate::types::Host>(
1763    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1764    count: usize,
1765) {
1766    do_paste(ed, false, count.max(1));
1767    if !ed.vim.replaying {
1768        ed.vim.last_change = Some(LastChange::Paste {
1769            before: false,
1770            count: count.max(1),
1771        });
1772    }
1773}
1774
1775/// `P` — paste the unnamed register (or `"reg` register) before the cursor.
1776/// Linewise yanks open a new line above; charwise pastes inline.
1777/// Records `LastChange::Paste` for dot-repeat.
1778pub(crate) fn paste_before_bridge<H: crate::types::Host>(
1779    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1780    count: usize,
1781) {
1782    do_paste(ed, true, count.max(1));
1783    if !ed.vim.replaying {
1784        ed.vim.last_change = Some(LastChange::Paste {
1785            before: true,
1786            count: count.max(1),
1787        });
1788    }
1789}
1790
1791// ── Jump bridges ───────────────────────────────────────────────────────────
1792
1793/// `<C-o>` — jump back `count` entries in the jumplist, saving the current
1794/// position on the forward stack so `<C-i>` can return.
1795pub(crate) fn jump_back_bridge<H: crate::types::Host>(
1796    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1797    count: usize,
1798) {
1799    for _ in 0..count.max(1) {
1800        jump_back(ed);
1801    }
1802}
1803
1804/// `<C-i>` / `Tab` — redo `count` jumps on the forward stack, saving the
1805/// current position on the backward stack.
1806pub(crate) fn jump_forward_bridge<H: crate::types::Host>(
1807    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1808    count: usize,
1809) {
1810    for _ in 0..count.max(1) {
1811        jump_forward(ed);
1812    }
1813}
1814
1815// ── Scroll bridges ─────────────────────────────────────────────────────────
1816
1817/// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
1818/// (`h - 2` rows to preserve two-line overlap). `count` multiplies.
1819pub(crate) fn scroll_full_page_bridge<H: crate::types::Host>(
1820    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1821    dir: ScrollDir,
1822    count: usize,
1823) {
1824    let rows = viewport_full_rows(ed, count) as isize;
1825    match dir {
1826        ScrollDir::Down => scroll_cursor_rows(ed, rows),
1827        ScrollDir::Up => scroll_cursor_rows(ed, -rows),
1828    }
1829}
1830
1831/// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
1832/// `count` multiplies.
1833pub(crate) fn scroll_half_page_bridge<H: crate::types::Host>(
1834    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1835    dir: ScrollDir,
1836    count: usize,
1837) {
1838    let rows = viewport_half_rows(ed, count) as isize;
1839    match dir {
1840        ScrollDir::Down => scroll_cursor_rows(ed, rows),
1841        ScrollDir::Up => scroll_cursor_rows(ed, -rows),
1842    }
1843}
1844
1845/// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving the
1846/// cursor (cursor is clamped to the new visible region if it would go
1847/// off-screen). `<C-e>` scrolls down; `<C-y>` scrolls up.
1848pub(crate) fn scroll_line_bridge<H: crate::types::Host>(
1849    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1850    dir: ScrollDir,
1851    count: usize,
1852) {
1853    let n = count.max(1);
1854    let total = buf_row_count(&ed.buffer);
1855    let last = total.saturating_sub(1);
1856    let h = ed.viewport_height_value() as usize;
1857    let vp = ed.host().viewport();
1858    let cur_top = vp.top_row;
1859    let new_top = match dir {
1860        ScrollDir::Down => (cur_top + n).min(last),
1861        ScrollDir::Up => cur_top.saturating_sub(n),
1862    };
1863    ed.set_viewport_top(new_top);
1864    // Clamp cursor to stay within the new visible region.
1865    let (row, col) = ed.cursor();
1866    let bot = (new_top + h).saturating_sub(1).min(last);
1867    let clamped = row.max(new_top).min(bot);
1868    if clamped != row {
1869        buf_set_cursor_rc(&mut ed.buffer, clamped, col);
1870        ed.push_buffer_cursor_to_textarea();
1871    }
1872}
1873
1874// ── Search bridges ─────────────────────────────────────────────────────────
1875
1876/// `n` / `N` — repeat the last search `count` times. `forward = true` means
1877/// repeat in the original search direction; `false` inverts it (like `N`).
1878pub(crate) fn search_repeat_bridge<H: crate::types::Host>(
1879    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1880    forward: bool,
1881    count: usize,
1882) {
1883    if let Some(pattern) = ed.vim.last_search.clone() {
1884        ed.push_search_pattern(&pattern);
1885    }
1886    if ed.search_state().pattern.is_none() {
1887        return;
1888    }
1889    let go_forward = ed.vim.last_search_forward == forward;
1890    for _ in 0..count.max(1) {
1891        if go_forward {
1892            ed.search_advance_forward(true);
1893        } else {
1894            ed.search_advance_backward(true);
1895        }
1896    }
1897    ed.push_buffer_cursor_to_textarea();
1898}
1899
1900/// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
1901/// `forward` picks search direction; `whole_word` wraps in `\b...\b`.
1902/// `count` repeats the advance.
1903pub(crate) fn word_search_bridge<H: crate::types::Host>(
1904    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1905    forward: bool,
1906    whole_word: bool,
1907    count: usize,
1908) {
1909    word_at_cursor_search(ed, forward, whole_word, count.max(1));
1910}
1911
1912// ── Undo / redo confirmation wrappers (already public on Editor) ───────────
1913
1914/// `u` bridge — identical to `do_undo`; retained for Phase 6.6b audit.
1915/// The FSM now calls `ed.undo()` directly (Phase 6.6a).
1916#[allow(dead_code)]
1917#[inline]
1918pub(crate) fn do_undo_bridge<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
1919    do_undo(ed);
1920}
1921
1922// ─── Phase 6.3: visual-mode primitive bridges ──────────────────────────────
1923//
1924// Each `pub(crate)` free function is the extractable body of one visual-mode
1925// transition. These bridges set `vim.mode` directly AND write `current_mode`
1926// so that `Editor::vim_mode()` can read from the stable field without going
1927// through `public_mode()`.
1928//
1929// Pattern identical to Phase 6.1 / 6.2:
1930//   - Bridge fn is `pub(crate) fn *_bridge<H: Host>(ed, …)` in this file.
1931//   - Public wrapper is `pub fn *(&mut self, …)` in `editor.rs` with rustdoc.
1932
1933/// Helper — set both the FSM-internal `mode` and the stable `current_mode`
1934/// field in one call. Every Phase 6.3 bridge that changes mode calls this so
1935/// `vim_mode()` stays correct without going through the FSM's `step()` loop.
1936#[inline]
1937pub(crate) fn set_vim_mode_bridge<H: crate::types::Host>(
1938    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1939    mode: Mode,
1940) {
1941    ed.vim.mode = mode;
1942    ed.vim.current_mode = ed.vim.public_mode();
1943}
1944
1945/// `v` from Normal — enter charwise Visual mode. Anchors at the current
1946/// cursor position; the cursor IS the live end of the selection.
1947pub(crate) fn enter_visual_char_bridge<H: crate::types::Host>(
1948    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1949) {
1950    let cur = ed.cursor();
1951    ed.vim.visual_anchor = cur;
1952    set_vim_mode_bridge(ed, Mode::Visual);
1953}
1954
1955/// `V` from Normal — enter linewise Visual mode. Anchors the whole line
1956/// containing the current cursor; `o` still swaps the anchor row.
1957pub(crate) fn enter_visual_line_bridge<H: crate::types::Host>(
1958    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1959) {
1960    let (row, _) = ed.cursor();
1961    ed.vim.visual_line_anchor = row;
1962    set_vim_mode_bridge(ed, Mode::VisualLine);
1963}
1964
1965/// `<C-v>` from Normal — enter Visual-block mode. Anchors at the current
1966/// cursor; `block_vcol` is seeded from the cursor column so h/l navigation
1967/// preserves the desired virtual column.
1968pub(crate) fn enter_visual_block_bridge<H: crate::types::Host>(
1969    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1970) {
1971    let cur = ed.cursor();
1972    ed.vim.block_anchor = cur;
1973    ed.vim.block_vcol = cur.1;
1974    set_vim_mode_bridge(ed, Mode::VisualBlock);
1975}
1976
1977/// Esc from any visual mode — set `<` / `>` marks (per `:h v_:`), stash the
1978/// selection for `gv` re-entry, and return to Normal. Replicates the
1979/// `pre_visual_snapshot` logic in `step()` so callers outside the FSM get
1980/// identical behaviour.
1981pub(crate) fn exit_visual_to_normal_bridge<H: crate::types::Host>(
1982    ed: &mut Editor<hjkl_buffer::Buffer, H>,
1983) {
1984    // Build the same snapshot that `step()` captures at pre-step time.
1985    let snap: Option<LastVisual> = match ed.vim.mode {
1986        Mode::Visual => Some(LastVisual {
1987            mode: Mode::Visual,
1988            anchor: ed.vim.visual_anchor,
1989            cursor: ed.cursor(),
1990            block_vcol: 0,
1991        }),
1992        Mode::VisualLine => Some(LastVisual {
1993            mode: Mode::VisualLine,
1994            anchor: (ed.vim.visual_line_anchor, 0),
1995            cursor: ed.cursor(),
1996            block_vcol: 0,
1997        }),
1998        Mode::VisualBlock => Some(LastVisual {
1999            mode: Mode::VisualBlock,
2000            anchor: ed.vim.block_anchor,
2001            cursor: ed.cursor(),
2002            block_vcol: ed.vim.block_vcol,
2003        }),
2004        _ => None,
2005    };
2006    // Transition to Normal first (matches FSM order).
2007    ed.vim.pending = Pending::None;
2008    ed.vim.count = 0;
2009    ed.vim.insert_session = None;
2010    set_vim_mode_bridge(ed, Mode::Normal);
2011    // Set `<` / `>` marks and stash `last_visual` — mirrors the post-step
2012    // logic in `step()` that fires when a visual → non-visual transition
2013    // is detected.
2014    if let Some(snap) = snap {
2015        let (lo, hi) = match snap.mode {
2016            Mode::Visual => {
2017                if snap.anchor <= snap.cursor {
2018                    (snap.anchor, snap.cursor)
2019                } else {
2020                    (snap.cursor, snap.anchor)
2021                }
2022            }
2023            Mode::VisualLine => {
2024                let r_lo = snap.anchor.0.min(snap.cursor.0);
2025                let r_hi = snap.anchor.0.max(snap.cursor.0);
2026                let last_col = ed
2027                    .buffer()
2028                    .lines()
2029                    .get(r_hi)
2030                    .map(|l| l.chars().count().saturating_sub(1))
2031                    .unwrap_or(0);
2032                ((r_lo, 0), (r_hi, last_col))
2033            }
2034            Mode::VisualBlock => {
2035                let (r1, c1) = snap.anchor;
2036                let (r2, c2) = snap.cursor;
2037                ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
2038            }
2039            _ => {
2040                if snap.anchor <= snap.cursor {
2041                    (snap.anchor, snap.cursor)
2042                } else {
2043                    (snap.cursor, snap.anchor)
2044                }
2045            }
2046        };
2047        ed.set_mark('<', lo);
2048        ed.set_mark('>', hi);
2049        ed.vim.last_visual = Some(snap);
2050    }
2051}
2052
2053/// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
2054/// without mutating the selection range. In charwise mode the cursor jumps
2055/// to the old anchor and the anchor takes the old cursor. In linewise mode
2056/// the anchor *row* swaps with the current cursor row. In block mode the
2057/// block corners swap.
2058pub(crate) fn visual_o_toggle_bridge<H: crate::types::Host>(
2059    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2060) {
2061    match ed.vim.mode {
2062        Mode::Visual => {
2063            let cur = ed.cursor();
2064            let anchor = ed.vim.visual_anchor;
2065            ed.vim.visual_anchor = cur;
2066            ed.jump_cursor(anchor.0, anchor.1);
2067        }
2068        Mode::VisualLine => {
2069            let cur_row = ed.cursor().0;
2070            let anchor_row = ed.vim.visual_line_anchor;
2071            ed.vim.visual_line_anchor = cur_row;
2072            ed.jump_cursor(anchor_row, 0);
2073        }
2074        Mode::VisualBlock => {
2075            let cur = ed.cursor();
2076            let anchor = ed.vim.block_anchor;
2077            ed.vim.block_anchor = cur;
2078            ed.vim.block_vcol = anchor.1;
2079            ed.jump_cursor(anchor.0, anchor.1);
2080        }
2081        _ => {}
2082    }
2083}
2084
2085/// `gv` — restore the last visual selection (mode + anchor + cursor).
2086/// No-op if no selection was ever stored. Mirrors the `gv` arm in
2087/// `handle_normal_g`.
2088pub(crate) fn reenter_last_visual_bridge<H: crate::types::Host>(
2089    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2090) {
2091    if let Some(snap) = ed.vim.last_visual {
2092        match snap.mode {
2093            Mode::Visual => {
2094                ed.vim.visual_anchor = snap.anchor;
2095                set_vim_mode_bridge(ed, Mode::Visual);
2096            }
2097            Mode::VisualLine => {
2098                ed.vim.visual_line_anchor = snap.anchor.0;
2099                set_vim_mode_bridge(ed, Mode::VisualLine);
2100            }
2101            Mode::VisualBlock => {
2102                ed.vim.block_anchor = snap.anchor;
2103                ed.vim.block_vcol = snap.block_vcol;
2104                set_vim_mode_bridge(ed, Mode::VisualBlock);
2105            }
2106            _ => {}
2107        }
2108        ed.jump_cursor(snap.cursor.0, snap.cursor.1);
2109    }
2110}
2111
2112/// Direct mode-transition entry point for external controllers (e.g.
2113/// hjkl-vim). Sets both the FSM-internal `mode` and the stable
2114/// `current_mode`. Use sparingly — prefer the semantic primitives
2115/// (`enter_visual_char_bridge`, `enter_insert_i_bridge`, …) which also
2116/// set up the required bookkeeping (anchors, sessions, …).
2117pub(crate) fn set_mode_bridge<H: crate::types::Host>(
2118    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2119    mode: crate::VimMode,
2120) {
2121    let internal = match mode {
2122        crate::VimMode::Normal => Mode::Normal,
2123        crate::VimMode::Insert => Mode::Insert,
2124        crate::VimMode::Visual => Mode::Visual,
2125        crate::VimMode::VisualLine => Mode::VisualLine,
2126        crate::VimMode::VisualBlock => Mode::VisualBlock,
2127    };
2128    ed.vim.mode = internal;
2129    ed.vim.current_mode = mode;
2130}
2131
2132// ─── Normal / Visual / Operator-pending dispatcher removed in Phase 6.6g.3 ──
2133//
2134// `step_normal` and all private dispatch helpers (handle_after_op,
2135// handle_after_g, handle_after_z, handle_normal_only, etc.) were deleted.
2136// The canonical FSM body lives in `hjkl-vim::normal`. Use
2137// `hjkl_vim::dispatch_input` as the entry point.
2138//
2139// DELETED FUNCTION SIGNATURE (for archaeology):
2140// pub(crate) fn step_normal<H: crate::types::Host>(ed: ..., input: Input) -> bool {
2141
2142/// `m{ch}` — public controller entry point. Validates `ch` (must be
2143/// alphanumeric to match vim's mark-name rules) and records the current
2144/// cursor position under that name. Promoted to the public surface in 0.6.7
2145/// so the hjkl-vim `PendingState::SetMark` reducer can dispatch
2146/// `EngineCmd::SetMark` without re-entering the engine FSM.
2147/// `handle_set_mark` delegates here to avoid logic duplication.
2148pub(crate) fn set_mark_at_cursor<H: crate::types::Host>(
2149    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2150    ch: char,
2151) {
2152    if ch.is_ascii_lowercase() || ch.is_ascii_uppercase() {
2153        // 0.0.36: lowercase + uppercase marks share the unified
2154        // `Editor::marks` map. Uppercase entries survive
2155        // `set_content` so they persist across tab swaps within the
2156        // same Editor (the map lives on the Editor, not the buffer).
2157        let pos = ed.cursor();
2158        ed.set_mark(ch, pos);
2159    }
2160    // Invalid chars silently no-op (mirrors handle_set_mark behaviour).
2161}
2162
2163/// `'<ch>` / `` `<ch> `` — public controller entry point. Validates `ch`
2164/// against the set of legal mark names (lowercase, uppercase, special:
2165/// `'`/`` ` ``/`.`/`[`/`]`/`<`/`>`), resolves the target position, and
2166/// jumps the cursor. `linewise = true` → row only, col snaps to first
2167/// non-blank; `linewise = false` → exact (row, col). Called by
2168/// `Editor::goto_mark_line` / `Editor::goto_mark_char` so that hjkl-vim's
2169/// `PendingState::GotoMarkLine` / `GotoMarkChar` reducers can dispatch
2170/// without re-entering the engine FSM.
2171pub(crate) fn goto_mark<H: crate::types::Host>(
2172    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2173    ch: char,
2174    linewise: bool,
2175) {
2176    let target = match ch {
2177        'a'..='z' | 'A'..='Z' => ed.mark(ch),
2178        '\'' | '`' => ed.vim.jump_back.last().copied(),
2179        '.' => ed.vim.last_edit_pos,
2180        '[' | ']' | '<' | '>' => ed.mark(ch),
2181        _ => None,
2182    };
2183    let Some((row, col)) = target else {
2184        return;
2185    };
2186    let pre = ed.cursor();
2187    let (r, c_clamped) = clamp_pos(ed, (row, col));
2188    if linewise {
2189        buf_set_cursor_rc(&mut ed.buffer, r, 0);
2190        ed.push_buffer_cursor_to_textarea();
2191        move_first_non_whitespace(ed);
2192    } else {
2193        buf_set_cursor_rc(&mut ed.buffer, r, c_clamped);
2194        ed.push_buffer_cursor_to_textarea();
2195    }
2196    if ed.cursor() != pre {
2197        ed.push_jump(pre);
2198    }
2199    ed.sticky_col = Some(ed.cursor().1);
2200}
2201
2202/// `true` when `op` records a `last_change` entry for dot-repeat purposes.
2203/// Promoted to `pub` in Phase 6.6e so `hjkl-vim::normal` can use it without
2204/// duplicating the logic.
2205pub fn op_is_change(op: Operator) -> bool {
2206    matches!(op, Operator::Delete | Operator::Change)
2207}
2208
2209// ─── Jumplist (Ctrl-o / Ctrl-i) ────────────────────────────────────────────
2210
2211/// Max jumplist depth. Matches vim default.
2212pub(crate) const JUMPLIST_MAX: usize = 100;
2213
2214/// `Ctrl-o` — jump back to the most recent pre-jump position. Saves
2215/// the current cursor onto the forward stack so `Ctrl-i` can return.
2216fn jump_back<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
2217    let Some(target) = ed.vim.jump_back.pop() else {
2218        return;
2219    };
2220    let cur = ed.cursor();
2221    ed.vim.jump_fwd.push(cur);
2222    let (r, c) = clamp_pos(ed, target);
2223    ed.jump_cursor(r, c);
2224    ed.sticky_col = Some(c);
2225}
2226
2227/// `Ctrl-i` / `Tab` — redo the last `Ctrl-o`. Saves the current cursor
2228/// onto the back stack.
2229fn jump_forward<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
2230    let Some(target) = ed.vim.jump_fwd.pop() else {
2231        return;
2232    };
2233    let cur = ed.cursor();
2234    ed.vim.jump_back.push(cur);
2235    if ed.vim.jump_back.len() > JUMPLIST_MAX {
2236        ed.vim.jump_back.remove(0);
2237    }
2238    let (r, c) = clamp_pos(ed, target);
2239    ed.jump_cursor(r, c);
2240    ed.sticky_col = Some(c);
2241}
2242
2243/// Clamp a stored `(row, col)` to the live buffer in case edits
2244/// shrunk the document between push and pop.
2245fn clamp_pos<H: crate::types::Host>(
2246    ed: &Editor<hjkl_buffer::Buffer, H>,
2247    pos: (usize, usize),
2248) -> (usize, usize) {
2249    let last_row = buf_row_count(&ed.buffer).saturating_sub(1);
2250    let r = pos.0.min(last_row);
2251    let line_len = buf_line_chars(&ed.buffer, r);
2252    let c = pos.1.min(line_len.saturating_sub(1));
2253    (r, c)
2254}
2255
2256/// True for motions that vim treats as jumps (pushed onto the jumplist).
2257fn is_big_jump(motion: &Motion) -> bool {
2258    matches!(
2259        motion,
2260        Motion::FileTop
2261            | Motion::FileBottom
2262            | Motion::MatchBracket
2263            | Motion::WordAtCursor { .. }
2264            | Motion::SearchNext { .. }
2265            | Motion::ViewportTop
2266            | Motion::ViewportMiddle
2267            | Motion::ViewportBottom
2268    )
2269}
2270
2271// ─── Scroll helpers (Ctrl-d / Ctrl-u / Ctrl-f / Ctrl-b) ────────────────────
2272
2273/// Half-viewport row count, with a floor of 1 so tiny / un-rendered
2274/// viewports still step by a single row. `count` multiplies.
2275fn viewport_half_rows<H: crate::types::Host>(
2276    ed: &Editor<hjkl_buffer::Buffer, H>,
2277    count: usize,
2278) -> usize {
2279    let h = ed.viewport_height_value() as usize;
2280    (h / 2).max(1).saturating_mul(count.max(1))
2281}
2282
2283/// Full-viewport row count. Vim conventionally keeps 2 lines of overlap
2284/// between successive `Ctrl-f` pages; we approximate with `h - 2`.
2285fn viewport_full_rows<H: crate::types::Host>(
2286    ed: &Editor<hjkl_buffer::Buffer, H>,
2287    count: usize,
2288) -> usize {
2289    let h = ed.viewport_height_value() as usize;
2290    h.saturating_sub(2).max(1).saturating_mul(count.max(1))
2291}
2292
2293/// Move the cursor by `delta` rows (positive = down, negative = up),
2294/// clamp to the document, then land at the first non-blank on the new
2295/// row. The textarea viewport auto-scrolls to keep the cursor visible
2296/// when the cursor pushes off-screen.
2297fn scroll_cursor_rows<H: crate::types::Host>(
2298    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2299    delta: isize,
2300) {
2301    if delta == 0 {
2302        return;
2303    }
2304    ed.sync_buffer_content_from_textarea();
2305    let (row, _) = ed.cursor();
2306    let last_row = buf_row_count(&ed.buffer).saturating_sub(1);
2307    let target = (row as isize + delta).max(0).min(last_row as isize) as usize;
2308    buf_set_cursor_rc(&mut ed.buffer, target, 0);
2309    crate::motions::move_first_non_blank(&mut ed.buffer);
2310    ed.push_buffer_cursor_to_textarea();
2311    ed.sticky_col = Some(buf_cursor_pos(&ed.buffer).col);
2312}
2313
2314// ─── Motion parsing ────────────────────────────────────────────────────────
2315
2316/// Parse the first key of a normal/visual-mode motion. Returns `None` for
2317/// keys that don't start a motion (operator keys, command keys, etc.).
2318/// Promoted to `pub` in Phase 6.6e so `hjkl-vim::normal` can call it.
2319pub fn parse_motion(input: &Input) -> Option<Motion> {
2320    if input.ctrl {
2321        return None;
2322    }
2323    match input.key {
2324        Key::Char('h') | Key::Backspace | Key::Left => Some(Motion::Left),
2325        Key::Char('l') | Key::Right => Some(Motion::Right),
2326        Key::Char('j') | Key::Down | Key::Enter => Some(Motion::Down),
2327        Key::Char('k') | Key::Up => Some(Motion::Up),
2328        Key::Char('w') => Some(Motion::WordFwd),
2329        Key::Char('W') => Some(Motion::BigWordFwd),
2330        Key::Char('b') => Some(Motion::WordBack),
2331        Key::Char('B') => Some(Motion::BigWordBack),
2332        Key::Char('e') => Some(Motion::WordEnd),
2333        Key::Char('E') => Some(Motion::BigWordEnd),
2334        Key::Char('0') | Key::Home => Some(Motion::LineStart),
2335        Key::Char('^') => Some(Motion::FirstNonBlank),
2336        Key::Char('$') | Key::End => Some(Motion::LineEnd),
2337        Key::Char('G') => Some(Motion::FileBottom),
2338        Key::Char('%') => Some(Motion::MatchBracket),
2339        Key::Char(';') => Some(Motion::FindRepeat { reverse: false }),
2340        Key::Char(',') => Some(Motion::FindRepeat { reverse: true }),
2341        Key::Char('*') => Some(Motion::WordAtCursor {
2342            forward: true,
2343            whole_word: true,
2344        }),
2345        Key::Char('#') => Some(Motion::WordAtCursor {
2346            forward: false,
2347            whole_word: true,
2348        }),
2349        Key::Char('n') => Some(Motion::SearchNext { reverse: false }),
2350        Key::Char('N') => Some(Motion::SearchNext { reverse: true }),
2351        Key::Char('H') => Some(Motion::ViewportTop),
2352        Key::Char('M') => Some(Motion::ViewportMiddle),
2353        Key::Char('L') => Some(Motion::ViewportBottom),
2354        Key::Char('{') => Some(Motion::ParagraphPrev),
2355        Key::Char('}') => Some(Motion::ParagraphNext),
2356        Key::Char('(') => Some(Motion::SentencePrev),
2357        Key::Char(')') => Some(Motion::SentenceNext),
2358        _ => None,
2359    }
2360}
2361
2362// ─── Motion execution ──────────────────────────────────────────────────────
2363
2364pub(crate) fn execute_motion<H: crate::types::Host>(
2365    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2366    motion: Motion,
2367    count: usize,
2368) {
2369    let count = count.max(1);
2370    // FindRepeat needs the stored direction.
2371    let motion = match motion {
2372        Motion::FindRepeat { reverse } => match ed.vim.last_find {
2373            Some((ch, forward, till)) => Motion::Find {
2374                ch,
2375                forward: if reverse { !forward } else { forward },
2376                till,
2377            },
2378            None => return,
2379        },
2380        other => other,
2381    };
2382    let pre_pos = ed.cursor();
2383    let pre_col = pre_pos.1;
2384    apply_motion_cursor(ed, &motion, count);
2385    let post_pos = ed.cursor();
2386    if is_big_jump(&motion) && pre_pos != post_pos {
2387        ed.push_jump(pre_pos);
2388    }
2389    apply_sticky_col(ed, &motion, pre_col);
2390    // Phase 7b: keep the migration buffer's cursor + viewport in
2391    // lockstep with the textarea after every motion. Once 7c lands
2392    // (motions ported onto the buffer's API), this flips: the
2393    // buffer becomes authoritative and the textarea mirrors it.
2394    ed.sync_buffer_from_textarea();
2395}
2396
2397// ─── Keymap-layer motion controller ────────────────────────────────────────
2398
2399/// Wrapper around `execute_motion` that also syncs `block_vcol` when in
2400/// VisualBlock mode. The engine FSM's `step()` already does this (line ~2001);
2401/// the keymap path (`apply_motion_kind`) must do the same so VisualBlock h/l
2402/// extend the highlighted region correctly.
2403///
2404/// `update_block_vcol` is only a no-op for vertical / non-horizontal motions
2405/// (Up, Down, FileTop, FileBottom, Search), so passing every motion through is
2406/// safe — the function's own match arm handles the no-op case.
2407fn execute_motion_with_block_vcol<H: crate::types::Host>(
2408    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2409    motion: Motion,
2410    count: usize,
2411) {
2412    let motion_copy = motion.clone();
2413    execute_motion(ed, motion, count);
2414    if ed.vim.mode == Mode::VisualBlock {
2415        update_block_vcol(ed, &motion_copy);
2416    }
2417}
2418
2419/// Execute a `crate::MotionKind` cursor motion. Called by the host's
2420/// `Editor::apply_motion` controller method — the keymap dispatch path for
2421/// Phase 3a of kryptic-sh/hjkl#69.
2422///
2423/// Maps each variant to the same internal primitives used by the engine FSM
2424/// so cursor, sticky column, scroll, and sync semantics are identical.
2425///
2426/// # Visual-mode post-motion sync audit (2026-05-13)
2427///
2428/// After `execute_motion`, two things are conditional on visual mode:
2429///
2430/// 1. **VisualBlock `block_vcol` sync** — `update_block_vcol(ed, &motion)` is
2431///    called when `mode == Mode::VisualBlock`.  This is replicated here via
2432///    `execute_motion_with_block_vcol` for every motion variant below.
2433///
2434/// 2. **`last_find` update** — `Motion::Find` is dispatched through
2435///    `Pending::Find → apply_find_char` (in hjkl-vim), which writes `last_find`
2436///    itself.  A post-motion `last_find` write here would be dead code.  The keymap
2437///    path writes `last_find` in `apply_find_char` (called from
2438///    `Editor::find_char`), so no gap exists here.
2439///
2440/// No VisualLine-specific or Visual-specific post-motion work exists in the
2441/// FSM: anchors (`visual_anchor`, `visual_line_anchor`, `block_anchor`) are
2442/// only written on mode-entry or `o`-swap, never on motion.  The `<`/`>`
2443/// mark update in `step()` fires only on visual→normal transition, not after
2444/// each motion.  There are **no further sync gaps** beyond the `block_vcol`
2445/// fix already applied above.
2446pub(crate) fn apply_motion_kind<H: crate::types::Host>(
2447    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2448    kind: crate::MotionKind,
2449    count: usize,
2450) {
2451    let count = count.max(1);
2452    match kind {
2453        crate::MotionKind::CharLeft => {
2454            execute_motion_with_block_vcol(ed, Motion::Left, count);
2455        }
2456        crate::MotionKind::CharRight => {
2457            execute_motion_with_block_vcol(ed, Motion::Right, count);
2458        }
2459        crate::MotionKind::LineDown => {
2460            execute_motion_with_block_vcol(ed, Motion::Down, count);
2461        }
2462        crate::MotionKind::LineUp => {
2463            execute_motion_with_block_vcol(ed, Motion::Up, count);
2464        }
2465        crate::MotionKind::FirstNonBlankDown => {
2466            // `+`: move down `count` lines then land on first non-blank.
2467            // Not a big-jump (no jump-list entry), sticky col set to the
2468            // landed column (first non-blank). Mirrors scroll_cursor_rows
2469            // semantics but goes through the fold-aware buffer motion path.
2470            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
2471            crate::motions::move_down(&mut ed.buffer, &folds, count, &mut ed.sticky_col);
2472            crate::motions::move_first_non_blank(&mut ed.buffer);
2473            ed.push_buffer_cursor_to_textarea();
2474            ed.sticky_col = Some(buf_cursor_pos(&ed.buffer).col);
2475            ed.sync_buffer_from_textarea();
2476        }
2477        crate::MotionKind::FirstNonBlankUp => {
2478            // `-`: move up `count` lines then land on first non-blank.
2479            // Same pattern as FirstNonBlankDown, direction reversed.
2480            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
2481            crate::motions::move_up(&mut ed.buffer, &folds, count, &mut ed.sticky_col);
2482            crate::motions::move_first_non_blank(&mut ed.buffer);
2483            ed.push_buffer_cursor_to_textarea();
2484            ed.sticky_col = Some(buf_cursor_pos(&ed.buffer).col);
2485            ed.sync_buffer_from_textarea();
2486        }
2487        crate::MotionKind::WordForward => {
2488            execute_motion_with_block_vcol(ed, Motion::WordFwd, count);
2489        }
2490        crate::MotionKind::BigWordForward => {
2491            execute_motion_with_block_vcol(ed, Motion::BigWordFwd, count);
2492        }
2493        crate::MotionKind::WordBackward => {
2494            execute_motion_with_block_vcol(ed, Motion::WordBack, count);
2495        }
2496        crate::MotionKind::BigWordBackward => {
2497            execute_motion_with_block_vcol(ed, Motion::BigWordBack, count);
2498        }
2499        crate::MotionKind::WordEnd => {
2500            execute_motion_with_block_vcol(ed, Motion::WordEnd, count);
2501        }
2502        crate::MotionKind::BigWordEnd => {
2503            execute_motion_with_block_vcol(ed, Motion::BigWordEnd, count);
2504        }
2505        crate::MotionKind::LineStart => {
2506            // `0` / `<Home>`: first column of the current line.
2507            // count is ignored — matches vim `0` semantics.
2508            execute_motion_with_block_vcol(ed, Motion::LineStart, 1);
2509        }
2510        crate::MotionKind::FirstNonBlank => {
2511            // `^`: first non-blank column on the current line.
2512            // count is ignored — matches vim `^` semantics.
2513            execute_motion_with_block_vcol(ed, Motion::FirstNonBlank, 1);
2514        }
2515        crate::MotionKind::GotoLine => {
2516            // `G`: bare `G` → last line; `count G` → jump to line `count`.
2517            // apply_motion_kind normalises the raw count to count.max(1)
2518            // above, so count == 1 means "bare G" (last line) and count > 1
2519            // means "go to line N". execute_motion's FileBottom arm applies
2520            // the same `count > 1` check before calling move_bottom, so the
2521            // convention aligns: pass count straight through.
2522            // FileBottom is vertical — update_block_vcol is a no-op here
2523            // (preserves vcol), so the helper is safe to use.
2524            execute_motion_with_block_vcol(ed, Motion::FileBottom, count);
2525        }
2526        crate::MotionKind::LineEnd => {
2527            // `$` / `<End>`: last character on the current line.
2528            // count is ignored at the keymap-path level (vim `N$` moves
2529            // down N-1 lines then lands at line-end; not yet wired).
2530            execute_motion_with_block_vcol(ed, Motion::LineEnd, 1);
2531        }
2532        crate::MotionKind::FindRepeat => {
2533            // `;` — repeat last f/F/t/T in the same direction.
2534            // execute_motion resolves FindRepeat via ed.vim.last_find;
2535            // no-op if no prior find exists (None arm returns early).
2536            execute_motion_with_block_vcol(ed, Motion::FindRepeat { reverse: false }, count);
2537        }
2538        crate::MotionKind::FindRepeatReverse => {
2539            // `,` — repeat last f/F/t/T in the reverse direction.
2540            // execute_motion resolves FindRepeat via ed.vim.last_find;
2541            // no-op if no prior find exists (None arm returns early).
2542            execute_motion_with_block_vcol(ed, Motion::FindRepeat { reverse: true }, count);
2543        }
2544        crate::MotionKind::BracketMatch => {
2545            // `%` — jump to the matching bracket.
2546            // count is passed through; engine-side matching_bracket handles
2547            // the no-match case as a no-op (cursor stays). Engine FSM arm
2548            // for `%` in parse_motion is kept intact for macro-replay.
2549            execute_motion_with_block_vcol(ed, Motion::MatchBracket, count);
2550        }
2551        crate::MotionKind::ViewportTop => {
2552            // `H` — cursor to top of visible viewport, then count-1 rows down.
2553            // Engine FSM arm for `H` in parse_motion is kept intact for macro-replay.
2554            execute_motion_with_block_vcol(ed, Motion::ViewportTop, count);
2555        }
2556        crate::MotionKind::ViewportMiddle => {
2557            // `M` — cursor to middle of visible viewport; count ignored.
2558            // Engine FSM arm for `M` in parse_motion is kept intact for macro-replay.
2559            execute_motion_with_block_vcol(ed, Motion::ViewportMiddle, count);
2560        }
2561        crate::MotionKind::ViewportBottom => {
2562            // `L` — cursor to bottom of visible viewport, then count-1 rows up.
2563            // Engine FSM arm for `L` in parse_motion is kept intact for macro-replay.
2564            execute_motion_with_block_vcol(ed, Motion::ViewportBottom, count);
2565        }
2566        crate::MotionKind::HalfPageDown => {
2567            // `<C-d>` — half page down, count multiplies the distance.
2568            // Calls scroll_cursor_rows directly rather than adding a Motion enum
2569            // variant, keeping engine Motion churn minimal.
2570            scroll_cursor_rows(ed, viewport_half_rows(ed, count) as isize);
2571        }
2572        crate::MotionKind::HalfPageUp => {
2573            // `<C-u>` — half page up, count multiplies the distance.
2574            // Direct call mirrors the FSM Ctrl-u arm. No new Motion variant.
2575            scroll_cursor_rows(ed, -(viewport_half_rows(ed, count) as isize));
2576        }
2577        crate::MotionKind::FullPageDown => {
2578            // `<C-f>` — full page down (2-line overlap), count multiplies.
2579            // Direct call mirrors the FSM Ctrl-f arm. No new Motion variant.
2580            scroll_cursor_rows(ed, viewport_full_rows(ed, count) as isize);
2581        }
2582        crate::MotionKind::FullPageUp => {
2583            // `<C-b>` — full page up (2-line overlap), count multiplies.
2584            // Direct call mirrors the FSM Ctrl-b arm. No new Motion variant.
2585            scroll_cursor_rows(ed, -(viewport_full_rows(ed, count) as isize));
2586        }
2587    }
2588}
2589
2590/// Restore the cursor to the sticky column after vertical motions and
2591/// sync the sticky column to the current column after horizontal ones.
2592/// `pre_col` is the cursor column captured *before* the motion — used
2593/// to bootstrap the sticky value on the very first motion.
2594fn apply_sticky_col<H: crate::types::Host>(
2595    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2596    motion: &Motion,
2597    pre_col: usize,
2598) {
2599    if is_vertical_motion(motion) {
2600        let want = ed.sticky_col.unwrap_or(pre_col);
2601        // Record the desired column so the next vertical motion sees
2602        // it even if we currently clamped to a shorter row.
2603        ed.sticky_col = Some(want);
2604        let (row, _) = ed.cursor();
2605        let line_len = buf_line_chars(&ed.buffer, row);
2606        // Clamp to the last char on non-empty lines (vim normal-mode
2607        // never parks the cursor one past end of line). Empty lines
2608        // collapse to col 0.
2609        let max_col = line_len.saturating_sub(1);
2610        let target = want.min(max_col);
2611        ed.jump_cursor(row, target);
2612    } else {
2613        // Horizontal motion or non-motion: sticky column tracks the
2614        // new cursor column so the *next* vertical motion aims there.
2615        ed.sticky_col = Some(ed.cursor().1);
2616    }
2617}
2618
2619fn is_vertical_motion(motion: &Motion) -> bool {
2620    // Only j / k preserve the sticky column. Everything else (search,
2621    // gg / G, word jumps, etc.) lands at the match's own column so the
2622    // sticky value should sync to the new cursor column.
2623    matches!(
2624        motion,
2625        Motion::Up | Motion::Down | Motion::ScreenUp | Motion::ScreenDown
2626    )
2627}
2628
2629fn apply_motion_cursor<H: crate::types::Host>(
2630    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2631    motion: &Motion,
2632    count: usize,
2633) {
2634    apply_motion_cursor_ctx(ed, motion, count, false)
2635}
2636
2637pub(crate) fn apply_motion_cursor_ctx<H: crate::types::Host>(
2638    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2639    motion: &Motion,
2640    count: usize,
2641    as_operator: bool,
2642) {
2643    match motion {
2644        Motion::Left => {
2645            // `h` — Buffer clamps at col 0 (no wrap), matching vim.
2646            crate::motions::move_left(&mut ed.buffer, count);
2647            ed.push_buffer_cursor_to_textarea();
2648        }
2649        Motion::Right => {
2650            // `l` — operator-motion context (`dl`/`cl`/`yl`) is allowed
2651            // one past the last char so the range includes it; cursor
2652            // context clamps at the last char.
2653            if as_operator {
2654                crate::motions::move_right_to_end(&mut ed.buffer, count);
2655            } else {
2656                crate::motions::move_right_in_line(&mut ed.buffer, count);
2657            }
2658            ed.push_buffer_cursor_to_textarea();
2659        }
2660        Motion::Up => {
2661            // Final col is set by `apply_sticky_col` below — push the
2662            // post-move row to the textarea and let sticky tracking
2663            // finish the work.
2664            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
2665            crate::motions::move_up(&mut ed.buffer, &folds, count, &mut ed.sticky_col);
2666            ed.push_buffer_cursor_to_textarea();
2667        }
2668        Motion::Down => {
2669            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
2670            crate::motions::move_down(&mut ed.buffer, &folds, count, &mut ed.sticky_col);
2671            ed.push_buffer_cursor_to_textarea();
2672        }
2673        Motion::ScreenUp => {
2674            let v = *ed.host.viewport();
2675            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
2676            crate::motions::move_screen_up(&mut ed.buffer, &folds, &v, count, &mut ed.sticky_col);
2677            ed.push_buffer_cursor_to_textarea();
2678        }
2679        Motion::ScreenDown => {
2680            let v = *ed.host.viewport();
2681            let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
2682            crate::motions::move_screen_down(&mut ed.buffer, &folds, &v, count, &mut ed.sticky_col);
2683            ed.push_buffer_cursor_to_textarea();
2684        }
2685        Motion::WordFwd => {
2686            crate::motions::move_word_fwd(&mut ed.buffer, false, count, &ed.settings.iskeyword);
2687            ed.push_buffer_cursor_to_textarea();
2688        }
2689        Motion::WordBack => {
2690            crate::motions::move_word_back(&mut ed.buffer, false, count, &ed.settings.iskeyword);
2691            ed.push_buffer_cursor_to_textarea();
2692        }
2693        Motion::WordEnd => {
2694            crate::motions::move_word_end(&mut ed.buffer, false, count, &ed.settings.iskeyword);
2695            ed.push_buffer_cursor_to_textarea();
2696        }
2697        Motion::BigWordFwd => {
2698            crate::motions::move_word_fwd(&mut ed.buffer, true, count, &ed.settings.iskeyword);
2699            ed.push_buffer_cursor_to_textarea();
2700        }
2701        Motion::BigWordBack => {
2702            crate::motions::move_word_back(&mut ed.buffer, true, count, &ed.settings.iskeyword);
2703            ed.push_buffer_cursor_to_textarea();
2704        }
2705        Motion::BigWordEnd => {
2706            crate::motions::move_word_end(&mut ed.buffer, true, count, &ed.settings.iskeyword);
2707            ed.push_buffer_cursor_to_textarea();
2708        }
2709        Motion::WordEndBack => {
2710            crate::motions::move_word_end_back(
2711                &mut ed.buffer,
2712                false,
2713                count,
2714                &ed.settings.iskeyword,
2715            );
2716            ed.push_buffer_cursor_to_textarea();
2717        }
2718        Motion::BigWordEndBack => {
2719            crate::motions::move_word_end_back(&mut ed.buffer, true, count, &ed.settings.iskeyword);
2720            ed.push_buffer_cursor_to_textarea();
2721        }
2722        Motion::LineStart => {
2723            crate::motions::move_line_start(&mut ed.buffer);
2724            ed.push_buffer_cursor_to_textarea();
2725        }
2726        Motion::FirstNonBlank => {
2727            crate::motions::move_first_non_blank(&mut ed.buffer);
2728            ed.push_buffer_cursor_to_textarea();
2729        }
2730        Motion::LineEnd => {
2731            // Vim normal-mode `$` lands on the last char, not one past it.
2732            crate::motions::move_line_end(&mut ed.buffer);
2733            ed.push_buffer_cursor_to_textarea();
2734        }
2735        Motion::FileTop => {
2736            // `count gg` jumps to line `count` (first non-blank);
2737            // bare `gg` lands at the top.
2738            if count > 1 {
2739                crate::motions::move_bottom(&mut ed.buffer, count);
2740            } else {
2741                crate::motions::move_top(&mut ed.buffer);
2742            }
2743            ed.push_buffer_cursor_to_textarea();
2744        }
2745        Motion::FileBottom => {
2746            // `count G` jumps to line `count`; bare `G` lands at
2747            // the buffer bottom (`Buffer::move_bottom(0)`).
2748            if count > 1 {
2749                crate::motions::move_bottom(&mut ed.buffer, count);
2750            } else {
2751                crate::motions::move_bottom(&mut ed.buffer, 0);
2752            }
2753            ed.push_buffer_cursor_to_textarea();
2754        }
2755        Motion::Find { ch, forward, till } => {
2756            for _ in 0..count {
2757                if !find_char_on_line(ed, *ch, *forward, *till) {
2758                    break;
2759                }
2760            }
2761        }
2762        Motion::FindRepeat { .. } => {} // already resolved upstream
2763        Motion::MatchBracket => {
2764            let _ = matching_bracket(ed);
2765        }
2766        Motion::WordAtCursor {
2767            forward,
2768            whole_word,
2769        } => {
2770            word_at_cursor_search(ed, *forward, *whole_word, count);
2771        }
2772        Motion::SearchNext { reverse } => {
2773            // Re-push the last query so the buffer's search state is
2774            // correct even if the host happened to clear it (e.g. while
2775            // a Visual mode draw was in progress).
2776            if let Some(pattern) = ed.vim.last_search.clone() {
2777                ed.push_search_pattern(&pattern);
2778            }
2779            if ed.search_state().pattern.is_none() {
2780                return;
2781            }
2782            // `n` repeats the last search in its committed direction;
2783            // `N` inverts. So a `?` search makes `n` walk backward and
2784            // `N` walk forward.
2785            let forward = ed.vim.last_search_forward != *reverse;
2786            for _ in 0..count.max(1) {
2787                if forward {
2788                    ed.search_advance_forward(true);
2789                } else {
2790                    ed.search_advance_backward(true);
2791                }
2792            }
2793            ed.push_buffer_cursor_to_textarea();
2794        }
2795        Motion::ViewportTop => {
2796            let v = *ed.host().viewport();
2797            crate::motions::move_viewport_top(&mut ed.buffer, &v, count.saturating_sub(1));
2798            ed.push_buffer_cursor_to_textarea();
2799        }
2800        Motion::ViewportMiddle => {
2801            let v = *ed.host().viewport();
2802            crate::motions::move_viewport_middle(&mut ed.buffer, &v);
2803            ed.push_buffer_cursor_to_textarea();
2804        }
2805        Motion::ViewportBottom => {
2806            let v = *ed.host().viewport();
2807            crate::motions::move_viewport_bottom(&mut ed.buffer, &v, count.saturating_sub(1));
2808            ed.push_buffer_cursor_to_textarea();
2809        }
2810        Motion::LastNonBlank => {
2811            crate::motions::move_last_non_blank(&mut ed.buffer);
2812            ed.push_buffer_cursor_to_textarea();
2813        }
2814        Motion::LineMiddle => {
2815            let row = ed.cursor().0;
2816            let line_chars = buf_line_chars(&ed.buffer, row);
2817            // Vim's `gM`: column = floor(chars / 2). Empty / single-char
2818            // lines stay at col 0.
2819            let target = line_chars / 2;
2820            ed.jump_cursor(row, target);
2821        }
2822        Motion::ParagraphPrev => {
2823            crate::motions::move_paragraph_prev(&mut ed.buffer, count);
2824            ed.push_buffer_cursor_to_textarea();
2825        }
2826        Motion::ParagraphNext => {
2827            crate::motions::move_paragraph_next(&mut ed.buffer, count);
2828            ed.push_buffer_cursor_to_textarea();
2829        }
2830        Motion::SentencePrev => {
2831            for _ in 0..count.max(1) {
2832                if let Some((row, col)) = sentence_boundary(ed, false) {
2833                    ed.jump_cursor(row, col);
2834                }
2835            }
2836        }
2837        Motion::SentenceNext => {
2838            for _ in 0..count.max(1) {
2839                if let Some((row, col)) = sentence_boundary(ed, true) {
2840                    ed.jump_cursor(row, col);
2841                }
2842            }
2843        }
2844    }
2845}
2846
2847fn move_first_non_whitespace<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
2848    // Some call sites invoke this right after `dd` / `<<` / `>>` etc
2849    // mutates the textarea content, so the migration buffer hasn't
2850    // seen the new lines OR new cursor yet. Mirror the full content
2851    // across before delegating, then push the result back so the
2852    // textarea reflects the resolved column too.
2853    ed.sync_buffer_content_from_textarea();
2854    crate::motions::move_first_non_blank(&mut ed.buffer);
2855    ed.push_buffer_cursor_to_textarea();
2856}
2857
2858fn find_char_on_line<H: crate::types::Host>(
2859    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2860    ch: char,
2861    forward: bool,
2862    till: bool,
2863) -> bool {
2864    let moved = crate::motions::find_char_on_line(&mut ed.buffer, ch, forward, till);
2865    if moved {
2866        ed.push_buffer_cursor_to_textarea();
2867    }
2868    moved
2869}
2870
2871fn matching_bracket<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) -> bool {
2872    let moved = crate::motions::match_bracket(&mut ed.buffer);
2873    if moved {
2874        ed.push_buffer_cursor_to_textarea();
2875    }
2876    moved
2877}
2878
2879fn word_at_cursor_search<H: crate::types::Host>(
2880    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2881    forward: bool,
2882    whole_word: bool,
2883    count: usize,
2884) {
2885    let (row, col) = ed.cursor();
2886    let line: String = buf_line(&ed.buffer, row).unwrap_or("").to_string();
2887    let chars: Vec<char> = line.chars().collect();
2888    if chars.is_empty() {
2889        return;
2890    }
2891    // Expand around cursor to a word boundary.
2892    let spec = ed.settings().iskeyword.clone();
2893    let is_word = |c: char| is_keyword_char(c, &spec);
2894    let mut start = col.min(chars.len().saturating_sub(1));
2895    while start > 0 && is_word(chars[start - 1]) {
2896        start -= 1;
2897    }
2898    let mut end = start;
2899    while end < chars.len() && is_word(chars[end]) {
2900        end += 1;
2901    }
2902    if end <= start {
2903        return;
2904    }
2905    let word: String = chars[start..end].iter().collect();
2906    let escaped = regex_escape(&word);
2907    let pattern = if whole_word {
2908        format!(r"\b{escaped}\b")
2909    } else {
2910        escaped
2911    };
2912    ed.push_search_pattern(&pattern);
2913    if ed.search_state().pattern.is_none() {
2914        return;
2915    }
2916    // Remember the query so `n` / `N` keep working after the jump.
2917    ed.vim.last_search = Some(pattern);
2918    ed.vim.last_search_forward = forward;
2919    for _ in 0..count.max(1) {
2920        if forward {
2921            ed.search_advance_forward(true);
2922        } else {
2923            ed.search_advance_backward(true);
2924        }
2925    }
2926    ed.push_buffer_cursor_to_textarea();
2927}
2928
2929fn regex_escape(s: &str) -> String {
2930    let mut out = String::with_capacity(s.len());
2931    for c in s.chars() {
2932        if matches!(
2933            c,
2934            '.' | '+' | '*' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | '^' | '$' | '\\'
2935        ) {
2936            out.push('\\');
2937        }
2938        out.push(c);
2939    }
2940    out
2941}
2942
2943// ─── Operator application ──────────────────────────────────────────────────
2944
2945/// Public(crate) entry: apply operator over the motion identified by a raw
2946/// char key. Called by `Editor::apply_op_motion` (the public controller API)
2947/// so the hjkl-vim pending-state reducer can dispatch `ApplyOpMotion` without
2948/// re-entering the FSM.
2949///
2950/// Applies standard vim quirks:
2951/// - `cw` / `cW` → `ce` / `cE`
2952/// - `FindRepeat` → resolves against `last_find`
2953/// - Updates `last_find` and `last_change` per existing conventions.
2954///
2955/// No-op when `motion_key` does not produce a known motion.
2956pub(crate) fn apply_op_motion_key<H: crate::types::Host>(
2957    ed: &mut Editor<hjkl_buffer::Buffer, H>,
2958    op: Operator,
2959    motion_key: char,
2960    total_count: usize,
2961) {
2962    let input = Input {
2963        key: Key::Char(motion_key),
2964        ctrl: false,
2965        alt: false,
2966        shift: false,
2967    };
2968    let Some(motion) = parse_motion(&input) else {
2969        return;
2970    };
2971    let motion = match motion {
2972        Motion::FindRepeat { reverse } => match ed.vim.last_find {
2973            Some((ch, forward, till)) => Motion::Find {
2974                ch,
2975                forward: if reverse { !forward } else { forward },
2976                till,
2977            },
2978            None => return,
2979        },
2980        // Vim quirk: `cw` / `cW` → `ce` / `cE`.
2981        Motion::WordFwd if op == Operator::Change => Motion::WordEnd,
2982        Motion::BigWordFwd if op == Operator::Change => Motion::BigWordEnd,
2983        m => m,
2984    };
2985    apply_op_with_motion(ed, op, &motion, total_count);
2986    if let Motion::Find { ch, forward, till } = &motion {
2987        ed.vim.last_find = Some((*ch, *forward, *till));
2988    }
2989    if !ed.vim.replaying && op_is_change(op) {
2990        ed.vim.last_change = Some(LastChange::OpMotion {
2991            op,
2992            motion,
2993            count: total_count,
2994            inserted: None,
2995        });
2996    }
2997}
2998
2999/// Public(crate) entry: apply doubled-letter line op (`dd`/`yy`/`cc`/`>>`/`<<`).
3000/// Called by `Editor::apply_op_double` (the public controller API).
3001pub(crate) fn apply_op_double<H: crate::types::Host>(
3002    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3003    op: Operator,
3004    total_count: usize,
3005) {
3006    execute_line_op(ed, op, total_count);
3007    if !ed.vim.replaying {
3008        ed.vim.last_change = Some(LastChange::LineOp {
3009            op,
3010            count: total_count,
3011            inserted: None,
3012        });
3013    }
3014}
3015
3016/// Shared implementation: apply operator over a g-chord motion or case-op
3017/// linewise form. Called by `Editor::apply_op_g` (the public controller API)
3018/// so the hjkl-vim reducer can dispatch `ApplyOpG` without re-entering the FSM.
3019///
3020/// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's char
3021///   (`U`/`u`/`~`): executes the line op and updates `last_change`.
3022/// - Otherwise, maps `ch` to a motion (`g`→FileTop, `e`→WordEndBack,
3023///   `E`→BigWordEndBack, `j`→ScreenDown, `k`→ScreenUp) and applies. Unknown
3024///   chars are silently ignored (no-op), matching the engine FSM's behaviour.
3025pub(crate) fn apply_op_g_inner<H: crate::types::Host>(
3026    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3027    op: Operator,
3028    ch: char,
3029    total_count: usize,
3030) {
3031    // Case-op linewise form: `gUgU`, `gugu`, `g~g~` — same effect as
3032    // `gUU` / `guu` / `g~~`.
3033    if matches!(
3034        op,
3035        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase
3036    ) {
3037        let op_char = match op {
3038            Operator::Uppercase => 'U',
3039            Operator::Lowercase => 'u',
3040            Operator::ToggleCase => '~',
3041            _ => unreachable!(),
3042        };
3043        if ch == op_char {
3044            execute_line_op(ed, op, total_count);
3045            if !ed.vim.replaying {
3046                ed.vim.last_change = Some(LastChange::LineOp {
3047                    op,
3048                    count: total_count,
3049                    inserted: None,
3050                });
3051            }
3052            return;
3053        }
3054    }
3055    let motion = match ch {
3056        'g' => Motion::FileTop,
3057        'e' => Motion::WordEndBack,
3058        'E' => Motion::BigWordEndBack,
3059        'j' => Motion::ScreenDown,
3060        'k' => Motion::ScreenUp,
3061        _ => return, // Unknown char — no-op.
3062    };
3063    apply_op_with_motion(ed, op, &motion, total_count);
3064    if !ed.vim.replaying && op_is_change(op) {
3065        ed.vim.last_change = Some(LastChange::OpMotion {
3066            op,
3067            motion,
3068            count: total_count,
3069            inserted: None,
3070        });
3071    }
3072}
3073
3074/// Public(crate) entry point for bare `g<x>`. Applies the g-chord effect
3075/// given the char `ch` and pre-captured `count`. Called by `Editor::after_g`
3076/// (the public controller API) so the hjkl-vim pending-state reducer can
3077/// dispatch `AfterGChord` without re-entering the FSM.
3078pub(crate) fn apply_after_g<H: crate::types::Host>(
3079    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3080    ch: char,
3081    count: usize,
3082) {
3083    match ch {
3084        'g' => {
3085            // gg — top / jump to line count.
3086            let pre = ed.cursor();
3087            if count > 1 {
3088                ed.jump_cursor(count - 1, 0);
3089            } else {
3090                ed.jump_cursor(0, 0);
3091            }
3092            move_first_non_whitespace(ed);
3093            if ed.cursor() != pre {
3094                ed.push_jump(pre);
3095            }
3096        }
3097        'e' => execute_motion(ed, Motion::WordEndBack, count),
3098        'E' => execute_motion(ed, Motion::BigWordEndBack, count),
3099        // `g_` — last non-blank on the line.
3100        '_' => execute_motion(ed, Motion::LastNonBlank, count),
3101        // `gM` — middle char column of the current line.
3102        'M' => execute_motion(ed, Motion::LineMiddle, count),
3103        // `gv` — re-enter the last visual selection.
3104        // Phase 6.6a: drive through the public Editor API.
3105        'v' => ed.reenter_last_visual(),
3106        // `gj` / `gk` — display-line down / up. Walks one screen
3107        // segment at a time under `:set wrap`; falls back to `j`/`k`
3108        // when wrap is off (Buffer::move_screen_* handles the branch).
3109        'j' => execute_motion(ed, Motion::ScreenDown, count),
3110        'k' => execute_motion(ed, Motion::ScreenUp, count),
3111        // Case operators: `gU` / `gu` / `g~`. Enter operator-pending
3112        // so the next input is treated as the motion / text object /
3113        // shorthand double (`gUU`, `guu`, `g~~`).
3114        'U' => {
3115            ed.vim.pending = Pending::Op {
3116                op: Operator::Uppercase,
3117                count1: count,
3118            };
3119        }
3120        'u' => {
3121            ed.vim.pending = Pending::Op {
3122                op: Operator::Lowercase,
3123                count1: count,
3124            };
3125        }
3126        '~' => {
3127            ed.vim.pending = Pending::Op {
3128                op: Operator::ToggleCase,
3129                count1: count,
3130            };
3131        }
3132        'q' => {
3133            // `gq{motion}` — text reflow operator. Subsequent motion
3134            // / textobj rides the same operator pipeline.
3135            ed.vim.pending = Pending::Op {
3136                op: Operator::Reflow,
3137                count1: count,
3138            };
3139        }
3140        'J' => {
3141            // `gJ` — join line below without inserting a space.
3142            for _ in 0..count.max(1) {
3143                ed.push_undo();
3144                join_line_raw(ed);
3145            }
3146            if !ed.vim.replaying {
3147                ed.vim.last_change = Some(LastChange::JoinLine {
3148                    count: count.max(1),
3149                });
3150            }
3151        }
3152        'd' => {
3153            // `gd` — goto definition. hjkl-engine doesn't run an LSP
3154            // itself; raise an intent the host drains and routes to
3155            // `sqls`. The cursor stays put here — the host moves it
3156            // once it has the target location.
3157            ed.pending_lsp = Some(crate::editor::LspIntent::GotoDefinition);
3158        }
3159        // `gi` — go to last-insert position and re-enter insert mode.
3160        // Matches vim's `:h gi`: moves to the `'^` mark position (the
3161        // cursor where insert mode was last active, before Esc step-back)
3162        // and enters insert mode there.
3163        'i' => {
3164            if let Some((row, col)) = ed.vim.last_insert_pos {
3165                ed.jump_cursor(row, col);
3166            }
3167            begin_insert(ed, count.max(1), InsertReason::Enter(InsertEntry::I));
3168        }
3169        // `g;` / `g,` — walk the change list. `g;` toward older
3170        // entries, `g,` toward newer.
3171        ';' => walk_change_list(ed, -1, count.max(1)),
3172        ',' => walk_change_list(ed, 1, count.max(1)),
3173        // `g*` / `g#` — like `*` / `#` but match substrings (no `\b`
3174        // boundary anchors), so the cursor on `foo` finds it inside
3175        // `foobar` too.
3176        '*' => execute_motion(
3177            ed,
3178            Motion::WordAtCursor {
3179                forward: true,
3180                whole_word: false,
3181            },
3182            count,
3183        ),
3184        '#' => execute_motion(
3185            ed,
3186            Motion::WordAtCursor {
3187                forward: false,
3188                whole_word: false,
3189            },
3190            count,
3191        ),
3192        _ => {}
3193    }
3194}
3195
3196/// Public(crate) entry point for bare `z<x>`. Applies the z-chord effect
3197/// given the char `ch` and pre-captured `count`. Called by `Editor::after_z`
3198/// (the public controller API) so the hjkl-vim pending-state reducer can
3199/// dispatch `AfterZChord` without re-entering the engine FSM.
3200pub(crate) fn apply_after_z<H: crate::types::Host>(
3201    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3202    ch: char,
3203    count: usize,
3204) {
3205    use crate::editor::CursorScrollTarget;
3206    let row = ed.cursor().0;
3207    match ch {
3208        'z' => {
3209            ed.scroll_cursor_to(CursorScrollTarget::Center);
3210            ed.vim.viewport_pinned = true;
3211        }
3212        't' => {
3213            ed.scroll_cursor_to(CursorScrollTarget::Top);
3214            ed.vim.viewport_pinned = true;
3215        }
3216        'b' => {
3217            ed.scroll_cursor_to(CursorScrollTarget::Bottom);
3218            ed.vim.viewport_pinned = true;
3219        }
3220        // Folds — operate on the fold under the cursor (or the
3221        // whole buffer for `R` / `M`). Routed through
3222        // [`Editor::apply_fold_op`] (0.0.38 Patch C-δ.4) so the host
3223        // can observe / veto each op via [`Editor::take_fold_ops`].
3224        'o' => {
3225            ed.apply_fold_op(crate::types::FoldOp::OpenAt(row));
3226        }
3227        'c' => {
3228            ed.apply_fold_op(crate::types::FoldOp::CloseAt(row));
3229        }
3230        'a' => {
3231            ed.apply_fold_op(crate::types::FoldOp::ToggleAt(row));
3232        }
3233        'R' => {
3234            ed.apply_fold_op(crate::types::FoldOp::OpenAll);
3235        }
3236        'M' => {
3237            ed.apply_fold_op(crate::types::FoldOp::CloseAll);
3238        }
3239        'E' => {
3240            ed.apply_fold_op(crate::types::FoldOp::ClearAll);
3241        }
3242        'd' => {
3243            ed.apply_fold_op(crate::types::FoldOp::RemoveAt(row));
3244        }
3245        'f' => {
3246            if matches!(
3247                ed.vim.mode,
3248                Mode::Visual | Mode::VisualLine | Mode::VisualBlock
3249            ) {
3250                // `zf` over a Visual selection creates a fold spanning
3251                // anchor → cursor.
3252                let anchor_row = match ed.vim.mode {
3253                    Mode::VisualLine => ed.vim.visual_line_anchor,
3254                    Mode::VisualBlock => ed.vim.block_anchor.0,
3255                    _ => ed.vim.visual_anchor.0,
3256                };
3257                let cur = ed.cursor().0;
3258                let top = anchor_row.min(cur);
3259                let bot = anchor_row.max(cur);
3260                ed.apply_fold_op(crate::types::FoldOp::Add {
3261                    start_row: top,
3262                    end_row: bot,
3263                    closed: true,
3264                });
3265                ed.vim.mode = Mode::Normal;
3266            } else {
3267                // `zf{motion}` / `zf{textobj}` — route through the
3268                // operator pipeline. `Operator::Fold` reuses every
3269                // motion / text-object / `g`-prefix branch the other
3270                // operators get.
3271                ed.vim.pending = Pending::Op {
3272                    op: Operator::Fold,
3273                    count1: count,
3274                };
3275            }
3276        }
3277        _ => {}
3278    }
3279}
3280
3281/// Public(crate) entry point for bare `f<x>` / `F<x>` / `t<x>` / `T<x>`.
3282/// Applies the motion and records `last_find` for `;` / `,` repeat.
3283/// Called by `Editor::find_char` (the public controller API) so the
3284/// hjkl-vim pending-state reducer can dispatch `FindChar` without
3285/// re-entering the FSM.
3286pub(crate) fn apply_find_char<H: crate::types::Host>(
3287    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3288    ch: char,
3289    forward: bool,
3290    till: bool,
3291    count: usize,
3292) {
3293    execute_motion(ed, Motion::Find { ch, forward, till }, count.max(1));
3294    ed.vim.last_find = Some((ch, forward, till));
3295}
3296
3297/// Public(crate) entry: apply operator over a find motion (`df<x>` etc.).
3298/// Called by `Editor::apply_op_find` (the public controller API) so the
3299/// hjkl-vim `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
3300/// re-entering the FSM. `handle_op_find_target` now delegates here to avoid
3301/// logic duplication.
3302pub(crate) fn apply_op_find_motion<H: crate::types::Host>(
3303    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3304    op: Operator,
3305    ch: char,
3306    forward: bool,
3307    till: bool,
3308    total_count: usize,
3309) {
3310    let motion = Motion::Find { ch, forward, till };
3311    apply_op_with_motion(ed, op, &motion, total_count);
3312    ed.vim.last_find = Some((ch, forward, till));
3313    if !ed.vim.replaying && op_is_change(op) {
3314        ed.vim.last_change = Some(LastChange::OpMotion {
3315            op,
3316            motion,
3317            count: total_count,
3318            inserted: None,
3319        });
3320    }
3321}
3322
3323/// Shared implementation: map `ch` to `TextObject`, apply the operator, and
3324/// record `last_change`. Returns `false` when `ch` is not a known text-object
3325/// kind (caller should treat as a no-op). Called by `Editor::apply_op_text_obj`
3326/// (the public controller API) so hjkl-vim can dispatch without re-entering the FSM.
3327///
3328/// `_total_count` is accepted for API symmetry with `apply_op_find_motion` /
3329/// `apply_op_motion_key` but is currently unused — text objects don't repeat
3330/// in vim's current grammar. Kept for future-proofing.
3331pub(crate) fn apply_op_text_obj_inner<H: crate::types::Host>(
3332    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3333    op: Operator,
3334    ch: char,
3335    inner: bool,
3336    _total_count: usize,
3337) -> bool {
3338    // total_count unused — text objects don't repeat in vim's current grammar.
3339    // Kept for API symmetry with apply_op_motion / apply_op_find.
3340    let obj = match ch {
3341        'w' => TextObject::Word { big: false },
3342        'W' => TextObject::Word { big: true },
3343        '"' | '\'' | '`' => TextObject::Quote(ch),
3344        '(' | ')' | 'b' => TextObject::Bracket('('),
3345        '[' | ']' => TextObject::Bracket('['),
3346        '{' | '}' | 'B' => TextObject::Bracket('{'),
3347        '<' | '>' => TextObject::Bracket('<'),
3348        'p' => TextObject::Paragraph,
3349        't' => TextObject::XmlTag,
3350        's' => TextObject::Sentence,
3351        _ => return false,
3352    };
3353    apply_op_with_text_object(ed, op, obj, inner);
3354    if !ed.vim.replaying && op_is_change(op) {
3355        ed.vim.last_change = Some(LastChange::OpTextObj {
3356            op,
3357            obj,
3358            inner,
3359            inserted: None,
3360        });
3361    }
3362    true
3363}
3364
3365/// Move `pos` back by one character, clamped to (0, 0).
3366pub(crate) fn retreat_one<H: crate::types::Host>(
3367    ed: &Editor<hjkl_buffer::Buffer, H>,
3368    pos: (usize, usize),
3369) -> (usize, usize) {
3370    let (r, c) = pos;
3371    if c > 0 {
3372        (r, c - 1)
3373    } else if r > 0 {
3374        let prev_len = buf_line_bytes(&ed.buffer, r - 1);
3375        (r - 1, prev_len)
3376    } else {
3377        (0, 0)
3378    }
3379}
3380
3381/// Variant of begin_insert that doesn't push_undo (caller already did).
3382fn begin_insert_noundo<H: crate::types::Host>(
3383    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3384    count: usize,
3385    reason: InsertReason,
3386) {
3387    let reason = if ed.vim.replaying {
3388        InsertReason::ReplayOnly
3389    } else {
3390        reason
3391    };
3392    let (row, _) = ed.cursor();
3393    ed.vim.insert_session = Some(InsertSession {
3394        count,
3395        row_min: row,
3396        row_max: row,
3397        before_lines: buf_lines_to_vec(&ed.buffer),
3398        reason,
3399    });
3400    ed.vim.mode = Mode::Insert;
3401    // Phase 6.3: keep current_mode in sync for callers that bypass step().
3402    ed.vim.current_mode = crate::VimMode::Insert;
3403}
3404
3405// ─── Operator × Motion application ─────────────────────────────────────────
3406
3407pub(crate) fn apply_op_with_motion<H: crate::types::Host>(
3408    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3409    op: Operator,
3410    motion: &Motion,
3411    count: usize,
3412) {
3413    let start = ed.cursor();
3414    // Tentatively apply motion to find the endpoint. Operator context
3415    // so `l` on the last char advances past-last (standard vim
3416    // exclusive-motion endpoint behaviour), enabling `dl` / `cl` /
3417    // `yl` to cover the final char.
3418    apply_motion_cursor_ctx(ed, motion, count, true);
3419    let end = ed.cursor();
3420    let kind = motion_kind(motion);
3421    // Restore cursor before selecting (so Yank leaves cursor at start).
3422    ed.jump_cursor(start.0, start.1);
3423    run_operator_over_range(ed, op, start, end, kind);
3424}
3425
3426fn apply_op_with_text_object<H: crate::types::Host>(
3427    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3428    op: Operator,
3429    obj: TextObject,
3430    inner: bool,
3431) {
3432    let Some((start, end, kind)) = text_object_range(ed, obj, inner) else {
3433        return;
3434    };
3435    ed.jump_cursor(start.0, start.1);
3436    run_operator_over_range(ed, op, start, end, kind);
3437}
3438
3439fn motion_kind(motion: &Motion) -> RangeKind {
3440    match motion {
3441        Motion::Up | Motion::Down | Motion::ScreenUp | Motion::ScreenDown => RangeKind::Linewise,
3442        Motion::FileTop | Motion::FileBottom => RangeKind::Linewise,
3443        Motion::ViewportTop | Motion::ViewportMiddle | Motion::ViewportBottom => {
3444            RangeKind::Linewise
3445        }
3446        Motion::WordEnd | Motion::BigWordEnd | Motion::WordEndBack | Motion::BigWordEndBack => {
3447            RangeKind::Inclusive
3448        }
3449        Motion::Find { .. } => RangeKind::Inclusive,
3450        Motion::MatchBracket => RangeKind::Inclusive,
3451        // `$` now lands on the last char — operator ranges include it.
3452        Motion::LineEnd => RangeKind::Inclusive,
3453        _ => RangeKind::Exclusive,
3454    }
3455}
3456
3457fn run_operator_over_range<H: crate::types::Host>(
3458    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3459    op: Operator,
3460    start: (usize, usize),
3461    end: (usize, usize),
3462    kind: RangeKind,
3463) {
3464    let (top, bot) = order(start, end);
3465    // Charwise empty range (same position) — nothing to act on. For Linewise
3466    // the range `top == bot` means "operate on this one line" which is
3467    // perfectly valid (e.g. `Vd` on a single-line VisualLine selection).
3468    if top == bot && !matches!(kind, RangeKind::Linewise) {
3469        return;
3470    }
3471
3472    match op {
3473        Operator::Yank => {
3474            let text = read_vim_range(ed, top, bot, kind);
3475            if !text.is_empty() {
3476                ed.record_yank_to_host(text.clone());
3477                ed.record_yank(text, matches!(kind, RangeKind::Linewise));
3478            }
3479            // Vim `:h '[` / `:h ']`: after a yank `[` = first yanked char,
3480            // `]` = last yanked char. Mode-aware: linewise snaps to line
3481            // edges; charwise uses the actual inclusive endpoint.
3482            let rbr = match kind {
3483                RangeKind::Linewise => {
3484                    let last_col = buf_line_chars(&ed.buffer, bot.0).saturating_sub(1);
3485                    (bot.0, last_col)
3486                }
3487                RangeKind::Inclusive => (bot.0, bot.1),
3488                RangeKind::Exclusive => (bot.0, bot.1.saturating_sub(1)),
3489            };
3490            ed.set_mark('[', top);
3491            ed.set_mark(']', rbr);
3492            buf_set_cursor_rc(&mut ed.buffer, top.0, top.1);
3493            ed.push_buffer_cursor_to_textarea();
3494        }
3495        Operator::Delete => {
3496            ed.push_undo();
3497            cut_vim_range(ed, top, bot, kind);
3498            // After a charwise / inclusive delete the buffer cursor is
3499            // placed at `start` by the edit path. In Normal mode the
3500            // cursor max col is `line_len - 1`; clamp it here so e.g.
3501            // `d$` doesn't leave the cursor one past the new line end.
3502            if !matches!(kind, RangeKind::Linewise) {
3503                clamp_cursor_to_normal_mode(ed);
3504            }
3505            ed.vim.mode = Mode::Normal;
3506            // Vim `:h '[` / `:h ']`: after a delete both marks park at
3507            // the cursor position where the deletion collapsed (the join
3508            // point). Set after the cut and clamp so the position is final.
3509            let pos = ed.cursor();
3510            ed.set_mark('[', pos);
3511            ed.set_mark(']', pos);
3512        }
3513        Operator::Change => {
3514            // Vim `:h '[`: `[` is set to the start of the changed range
3515            // before the cut. `]` is deferred to insert-exit (AfterChange
3516            // path in finish_insert_session) where the cursor sits on the
3517            // last inserted char.
3518            ed.vim.change_mark_start = Some(top);
3519            ed.push_undo();
3520            cut_vim_range(ed, top, bot, kind);
3521            begin_insert_noundo(ed, 1, InsertReason::AfterChange);
3522        }
3523        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
3524            apply_case_op_to_selection(ed, op, top, bot, kind);
3525        }
3526        Operator::Indent | Operator::Outdent => {
3527            // Indent / outdent are always linewise even when triggered
3528            // by a char-wise motion (e.g. `>w` indents the whole line).
3529            ed.push_undo();
3530            if op == Operator::Indent {
3531                indent_rows(ed, top.0, bot.0, 1);
3532            } else {
3533                outdent_rows(ed, top.0, bot.0, 1);
3534            }
3535            ed.vim.mode = Mode::Normal;
3536        }
3537        Operator::Fold => {
3538            // Always linewise — fold the spanned rows regardless of the
3539            // motion's natural kind. Cursor lands on `top.0` to mirror
3540            // the visual `zf` path.
3541            if bot.0 >= top.0 {
3542                ed.apply_fold_op(crate::types::FoldOp::Add {
3543                    start_row: top.0,
3544                    end_row: bot.0,
3545                    closed: true,
3546                });
3547            }
3548            buf_set_cursor_rc(&mut ed.buffer, top.0, top.1);
3549            ed.push_buffer_cursor_to_textarea();
3550            ed.vim.mode = Mode::Normal;
3551        }
3552        Operator::Reflow => {
3553            ed.push_undo();
3554            reflow_rows(ed, top.0, bot.0);
3555            ed.vim.mode = Mode::Normal;
3556        }
3557        Operator::AutoIndent => {
3558            // Always linewise — like Indent/Outdent.
3559            ed.push_undo();
3560            auto_indent_rows(ed, top.0, bot.0);
3561            ed.vim.mode = Mode::Normal;
3562        }
3563    }
3564}
3565
3566// ─── Phase 4a pub range-mutation bridges ───────────────────────────────────
3567//
3568// These are `pub(crate)` entry points called by the five new pub methods on
3569// `Editor` (`delete_range`, `yank_range`, `change_range`, `indent_range`,
3570// `case_range`). They set `pending_register` from the caller-supplied char
3571// before delegating to the existing internal helpers so register semantics
3572// (unnamed `"`, named `"a`–`"z`, delete ring) are honoured exactly as in the
3573// FSM path.
3574//
3575// Do NOT call `run_operator_over_range` for Indent/Outdent or the three case
3576// operators — those share the FSM path but have dedicated parameter shapes
3577// (signed count, Operator-as-CaseOp) that map more cleanly to their own
3578// helpers.
3579
3580/// Delete the range `[start, end)` (interpretation determined by `kind`) and
3581/// stash the deleted text in `register`. `'"'` is the unnamed register.
3582pub(crate) fn delete_range_bridge<H: crate::types::Host>(
3583    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3584    start: (usize, usize),
3585    end: (usize, usize),
3586    kind: RangeKind,
3587    register: char,
3588) {
3589    ed.vim.pending_register = Some(register);
3590    run_operator_over_range(ed, Operator::Delete, start, end, kind);
3591}
3592
3593/// Yank (copy) the range `[start, end)` into `register` without mutating the
3594/// buffer. `'"'` is the unnamed register.
3595pub(crate) fn yank_range_bridge<H: crate::types::Host>(
3596    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3597    start: (usize, usize),
3598    end: (usize, usize),
3599    kind: RangeKind,
3600    register: char,
3601) {
3602    ed.vim.pending_register = Some(register);
3603    run_operator_over_range(ed, Operator::Yank, start, end, kind);
3604}
3605
3606/// Delete the range `[start, end)` and enter Insert mode (vim `c` operator).
3607/// The deleted text is stashed in `register`. Mode transitions to Insert on
3608/// return; the caller must not issue further normal-mode ops until the insert
3609/// session ends.
3610pub(crate) fn change_range_bridge<H: crate::types::Host>(
3611    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3612    start: (usize, usize),
3613    end: (usize, usize),
3614    kind: RangeKind,
3615    register: char,
3616) {
3617    ed.vim.pending_register = Some(register);
3618    run_operator_over_range(ed, Operator::Change, start, end, kind);
3619}
3620
3621/// Indent (`count > 0`) or outdent (`count < 0`) the row span `[start.0,
3622/// end.0]`. `shiftwidth` overrides the editor's `settings().shiftwidth` for
3623/// this call; pass `0` to use the editor setting. The column parts of `start`
3624/// / `end` are ignored — indent is always linewise.
3625pub(crate) fn indent_range_bridge<H: crate::types::Host>(
3626    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3627    start: (usize, usize),
3628    end: (usize, usize),
3629    count: i32,
3630    shiftwidth: u32,
3631) {
3632    if count == 0 {
3633        return;
3634    }
3635    let (top_row, bot_row) = if start.0 <= end.0 {
3636        (start.0, end.0)
3637    } else {
3638        (end.0, start.0)
3639    };
3640    // Temporarily override shiftwidth when the caller provides one.
3641    let original_sw = ed.settings().shiftwidth;
3642    if shiftwidth > 0 {
3643        ed.settings_mut().shiftwidth = shiftwidth as usize;
3644    }
3645    ed.push_undo();
3646    let abs_count = count.unsigned_abs() as usize;
3647    if count > 0 {
3648        indent_rows(ed, top_row, bot_row, abs_count);
3649    } else {
3650        outdent_rows(ed, top_row, bot_row, abs_count);
3651    }
3652    if shiftwidth > 0 {
3653        ed.settings_mut().shiftwidth = original_sw;
3654    }
3655    ed.vim.mode = Mode::Normal;
3656}
3657
3658/// Apply a case transformation (`Uppercase` / `Lowercase` / `ToggleCase`) to
3659/// the range `[start, end)`. Only the three case `Operator` variants are valid;
3660/// other variants are silently ignored (no-op).
3661pub(crate) fn case_range_bridge<H: crate::types::Host>(
3662    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3663    start: (usize, usize),
3664    end: (usize, usize),
3665    kind: RangeKind,
3666    op: Operator,
3667) {
3668    match op {
3669        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {}
3670        _ => return,
3671    }
3672    let (top, bot) = order(start, end);
3673    apply_case_op_to_selection(ed, op, top, bot, kind);
3674}
3675
3676// ─── Phase 4e pub block-shape range-mutation bridges ───────────────────────
3677//
3678// These are `pub(crate)` entry points called by the four new pub methods on
3679// `Editor` (`delete_block`, `yank_block`, `change_block`, `indent_block`).
3680// They set `pending_register` from the caller-supplied char then delegate to
3681// `apply_block_operator` (after temporarily installing the 4-corner block as
3682// the engine's virtual VisualBlock selection). The editor's VisualBlock state
3683// fields (`block_anchor`, `block_vcol`) are overwritten, the op fires, then
3684// the fields are restored to their pre-call values. This ensures the engine's
3685// register / undo / mode semantics are exercised without requiring the caller
3686// to already be in VisualBlock mode.
3687//
3688// `indent_block` is a separate helper — it does not use `apply_block_operator`
3689// because indent/outdent are always linewise for blocks (vim behaviour).
3690
3691/// Delete a rectangular VisualBlock selection. `top_row`/`bot_row` are
3692/// inclusive line bounds; `left_col`/`right_col` are inclusive char-column
3693/// bounds. Short lines that don't reach `right_col` lose only the chars
3694/// that exist (ragged-edge, matching engine FSM). `register` is honoured;
3695/// `'"'` selects the unnamed register.
3696pub(crate) fn delete_block_bridge<H: crate::types::Host>(
3697    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3698    top_row: usize,
3699    bot_row: usize,
3700    left_col: usize,
3701    right_col: usize,
3702    register: char,
3703) {
3704    ed.vim.pending_register = Some(register);
3705    let saved_anchor = ed.vim.block_anchor;
3706    let saved_vcol = ed.vim.block_vcol;
3707    ed.vim.block_anchor = (top_row, left_col);
3708    ed.vim.block_vcol = right_col;
3709    // Compute clamped col before the mutable borrow for buf_set_cursor_rc.
3710    let clamped = right_col.min(buf_line_chars(&ed.buffer, bot_row).saturating_sub(1));
3711    // Place cursor at bot_row / right_col so block_bounds resolves correctly.
3712    buf_set_cursor_rc(&mut ed.buffer, bot_row, clamped);
3713    apply_block_operator(ed, Operator::Delete);
3714    // Restore — block_anchor/vcol are only meaningful in VisualBlock mode;
3715    // after the op we're in Normal so restoring is a no-op for the user but
3716    // keeps state coherent if the caller inspects fields.
3717    ed.vim.block_anchor = saved_anchor;
3718    ed.vim.block_vcol = saved_vcol;
3719}
3720
3721/// Yank a rectangular VisualBlock selection into `register`.
3722pub(crate) fn yank_block_bridge<H: crate::types::Host>(
3723    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3724    top_row: usize,
3725    bot_row: usize,
3726    left_col: usize,
3727    right_col: usize,
3728    register: char,
3729) {
3730    ed.vim.pending_register = Some(register);
3731    let saved_anchor = ed.vim.block_anchor;
3732    let saved_vcol = ed.vim.block_vcol;
3733    ed.vim.block_anchor = (top_row, left_col);
3734    ed.vim.block_vcol = right_col;
3735    let clamped = right_col.min(buf_line_chars(&ed.buffer, bot_row).saturating_sub(1));
3736    buf_set_cursor_rc(&mut ed.buffer, bot_row, clamped);
3737    apply_block_operator(ed, Operator::Yank);
3738    ed.vim.block_anchor = saved_anchor;
3739    ed.vim.block_vcol = saved_vcol;
3740}
3741
3742/// Delete a rectangular VisualBlock selection and enter Insert mode (`c`).
3743/// The deleted text is stashed in `register`. Mode is Insert on return.
3744pub(crate) fn change_block_bridge<H: crate::types::Host>(
3745    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3746    top_row: usize,
3747    bot_row: usize,
3748    left_col: usize,
3749    right_col: usize,
3750    register: char,
3751) {
3752    ed.vim.pending_register = Some(register);
3753    let saved_anchor = ed.vim.block_anchor;
3754    let saved_vcol = ed.vim.block_vcol;
3755    ed.vim.block_anchor = (top_row, left_col);
3756    ed.vim.block_vcol = right_col;
3757    let clamped = right_col.min(buf_line_chars(&ed.buffer, bot_row).saturating_sub(1));
3758    buf_set_cursor_rc(&mut ed.buffer, bot_row, clamped);
3759    apply_block_operator(ed, Operator::Change);
3760    ed.vim.block_anchor = saved_anchor;
3761    ed.vim.block_vcol = saved_vcol;
3762}
3763
3764/// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3765/// Column bounds are ignored — vim's block indent is always linewise.
3766/// `count == 0` is a no-op.
3767pub(crate) fn indent_block_bridge<H: crate::types::Host>(
3768    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3769    top_row: usize,
3770    bot_row: usize,
3771    count: i32,
3772) {
3773    if count == 0 {
3774        return;
3775    }
3776    ed.push_undo();
3777    let abs = count.unsigned_abs() as usize;
3778    if count > 0 {
3779        indent_rows(ed, top_row, bot_row, abs);
3780    } else {
3781        outdent_rows(ed, top_row, bot_row, abs);
3782    }
3783    ed.vim.mode = Mode::Normal;
3784}
3785
3786/// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`. Column
3787/// parts are ignored — auto-indent is always linewise. See
3788/// `auto_indent_rows` for the algorithm and its v1 limitations.
3789pub(crate) fn auto_indent_range_bridge<H: crate::types::Host>(
3790    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3791    start: (usize, usize),
3792    end: (usize, usize),
3793) {
3794    let (top_row, bot_row) = if start.0 <= end.0 {
3795        (start.0, end.0)
3796    } else {
3797        (end.0, start.0)
3798    };
3799    ed.push_undo();
3800    auto_indent_rows(ed, top_row, bot_row);
3801    ed.vim.mode = Mode::Normal;
3802}
3803
3804// ─── Phase 4b pub text-object resolution bridges ───────────────────────────
3805//
3806// These are `pub(crate)` entry points called by the four new pub methods on
3807// `Editor` (`text_object_inner_word`, `text_object_around_word`,
3808// `text_object_inner_big_word`, `text_object_around_big_word`). They delegate
3809// to `word_text_object` — the existing private resolver — without touching any
3810// operator, register, or mode state. Pure functions: only `&Editor` required.
3811
3812/// Resolve the range of `iw` (inner word) at the current cursor position.
3813/// Returns `None` if no word exists at the cursor.
3814pub(crate) fn text_object_inner_word_bridge<H: crate::types::Host>(
3815    ed: &Editor<hjkl_buffer::Buffer, H>,
3816) -> Option<((usize, usize), (usize, usize))> {
3817    word_text_object(ed, true, false)
3818}
3819
3820/// Resolve the range of `aw` (around word) at the current cursor position.
3821/// Includes trailing whitespace (or leading whitespace if no trailing exists).
3822pub(crate) fn text_object_around_word_bridge<H: crate::types::Host>(
3823    ed: &Editor<hjkl_buffer::Buffer, H>,
3824) -> Option<((usize, usize), (usize, usize))> {
3825    word_text_object(ed, false, false)
3826}
3827
3828/// Resolve the range of `iW` (inner WORD) at the current cursor position.
3829/// A WORD is any run of non-whitespace characters (no punctuation splitting).
3830pub(crate) fn text_object_inner_big_word_bridge<H: crate::types::Host>(
3831    ed: &Editor<hjkl_buffer::Buffer, H>,
3832) -> Option<((usize, usize), (usize, usize))> {
3833    word_text_object(ed, true, true)
3834}
3835
3836/// Resolve the range of `aW` (around WORD) at the current cursor position.
3837/// Includes trailing whitespace (or leading whitespace if no trailing exists).
3838pub(crate) fn text_object_around_big_word_bridge<H: crate::types::Host>(
3839    ed: &Editor<hjkl_buffer::Buffer, H>,
3840) -> Option<((usize, usize), (usize, usize))> {
3841    word_text_object(ed, false, true)
3842}
3843
3844// ─── Phase 4c pub text-object resolution bridges (quote + bracket) ──────────
3845//
3846// `pub(crate)` entry points called by the four new pub methods on `Editor`
3847// (`text_object_inner_quote`, `text_object_around_quote`,
3848// `text_object_inner_bracket`, `text_object_around_bracket`). They delegate to
3849// `quote_text_object` / `bracket_text_object` — the existing private resolvers
3850// — without touching any operator, register, or mode state.
3851//
3852// `bracket_text_object` returns `Option<(Pos, Pos, RangeKind)>`; the bridges
3853// strip the `RangeKind` tag so callers see a uniform
3854// `Option<((usize,usize),(usize,usize))>` shape, consistent with 4b.
3855
3856/// Resolve the range of `i<quote>` (inner quote) at the current cursor
3857/// position. `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None`
3858/// when the cursor's line contains fewer than two occurrences of `quote`.
3859pub(crate) fn text_object_inner_quote_bridge<H: crate::types::Host>(
3860    ed: &Editor<hjkl_buffer::Buffer, H>,
3861    quote: char,
3862) -> Option<((usize, usize), (usize, usize))> {
3863    quote_text_object(ed, quote, true)
3864}
3865
3866/// Resolve the range of `a<quote>` (around quote) at the current cursor
3867/// position. Includes surrounding whitespace on one side per vim semantics.
3868pub(crate) fn text_object_around_quote_bridge<H: crate::types::Host>(
3869    ed: &Editor<hjkl_buffer::Buffer, H>,
3870    quote: char,
3871) -> Option<((usize, usize), (usize, usize))> {
3872    quote_text_object(ed, quote, false)
3873}
3874
3875/// Resolve the range of `i<bracket>` (inner bracket pair). `open` must be
3876/// one of `'('`, `'{'`, `'['`, `'<'`; the corresponding close is derived
3877/// internally. Returns `None` when no enclosing pair is found. The returned
3878/// range excludes the bracket characters themselves. Multi-line bracket pairs
3879/// whose content spans more than one line are reported as a charwise range
3880/// covering the first content character through the last content character
3881/// (RangeKind metadata is stripped — callers receive start/end only).
3882pub(crate) fn text_object_inner_bracket_bridge<H: crate::types::Host>(
3883    ed: &Editor<hjkl_buffer::Buffer, H>,
3884    open: char,
3885) -> Option<((usize, usize), (usize, usize))> {
3886    bracket_text_object(ed, open, true).map(|(s, e, _kind)| (s, e))
3887}
3888
3889/// Resolve the range of `a<bracket>` (around bracket pair). Includes the
3890/// bracket characters themselves. `open` must be one of `'('`, `'{'`, `'['`,
3891/// `'<'`.
3892pub(crate) fn text_object_around_bracket_bridge<H: crate::types::Host>(
3893    ed: &Editor<hjkl_buffer::Buffer, H>,
3894    open: char,
3895) -> Option<((usize, usize), (usize, usize))> {
3896    bracket_text_object(ed, open, false).map(|(s, e, _kind)| (s, e))
3897}
3898
3899// ── Sentence bridges (is / as) ─────────────────────────────────────────────
3900
3901/// Resolve the range of `is` (inner sentence) at the cursor. Excludes
3902/// trailing whitespace.
3903pub(crate) fn text_object_inner_sentence_bridge<H: crate::types::Host>(
3904    ed: &Editor<hjkl_buffer::Buffer, H>,
3905) -> Option<((usize, usize), (usize, usize))> {
3906    sentence_text_object(ed, true)
3907}
3908
3909/// Resolve the range of `as` (around sentence) at the cursor. Includes
3910/// trailing whitespace.
3911pub(crate) fn text_object_around_sentence_bridge<H: crate::types::Host>(
3912    ed: &Editor<hjkl_buffer::Buffer, H>,
3913) -> Option<((usize, usize), (usize, usize))> {
3914    sentence_text_object(ed, false)
3915}
3916
3917// ── Paragraph bridges (ip / ap) ────────────────────────────────────────────
3918
3919/// Resolve the range of `ip` (inner paragraph) at the cursor. A paragraph
3920/// is a block of non-blank lines bounded by blank lines or buffer edges.
3921pub(crate) fn text_object_inner_paragraph_bridge<H: crate::types::Host>(
3922    ed: &Editor<hjkl_buffer::Buffer, H>,
3923) -> Option<((usize, usize), (usize, usize))> {
3924    paragraph_text_object(ed, true)
3925}
3926
3927/// Resolve the range of `ap` (around paragraph) at the cursor. Includes one
3928/// trailing blank line when present.
3929pub(crate) fn text_object_around_paragraph_bridge<H: crate::types::Host>(
3930    ed: &Editor<hjkl_buffer::Buffer, H>,
3931) -> Option<((usize, usize), (usize, usize))> {
3932    paragraph_text_object(ed, false)
3933}
3934
3935// ── Tag bridges (it / at) ──────────────────────────────────────────────────
3936
3937/// Resolve the range of `it` (inner tag) at the cursor. Matches XML/HTML-style
3938/// `<tag>...</tag>` pairs; returns the range of inner content between the open
3939/// and close tags.
3940pub(crate) fn text_object_inner_tag_bridge<H: crate::types::Host>(
3941    ed: &Editor<hjkl_buffer::Buffer, H>,
3942) -> Option<((usize, usize), (usize, usize))> {
3943    tag_text_object(ed, true)
3944}
3945
3946/// Resolve the range of `at` (around tag) at the cursor. Includes the open
3947/// and close tag delimiters themselves.
3948pub(crate) fn text_object_around_tag_bridge<H: crate::types::Host>(
3949    ed: &Editor<hjkl_buffer::Buffer, H>,
3950) -> Option<((usize, usize), (usize, usize))> {
3951    tag_text_object(ed, false)
3952}
3953
3954/// Greedy word-wrap the rows in `[top, bot]` to `settings.textwidth`.
3955/// Splits on blank-line boundaries so paragraph structure is
3956/// preserved. Each paragraph's words are joined with single spaces
3957/// before re-wrapping.
3958fn reflow_rows<H: crate::types::Host>(
3959    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3960    top: usize,
3961    bot: usize,
3962) {
3963    let width = ed.settings().textwidth.max(1);
3964    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
3965    let bot = bot.min(lines.len().saturating_sub(1));
3966    if top > bot {
3967        return;
3968    }
3969    let original = lines[top..=bot].to_vec();
3970    let mut wrapped: Vec<String> = Vec::new();
3971    let mut paragraph: Vec<String> = Vec::new();
3972    let flush = |para: &mut Vec<String>, out: &mut Vec<String>, width: usize| {
3973        if para.is_empty() {
3974            return;
3975        }
3976        let words = para.join(" ");
3977        let mut current = String::new();
3978        for word in words.split_whitespace() {
3979            let extra = if current.is_empty() {
3980                word.chars().count()
3981            } else {
3982                current.chars().count() + 1 + word.chars().count()
3983            };
3984            if extra > width && !current.is_empty() {
3985                out.push(std::mem::take(&mut current));
3986                current.push_str(word);
3987            } else if current.is_empty() {
3988                current.push_str(word);
3989            } else {
3990                current.push(' ');
3991                current.push_str(word);
3992            }
3993        }
3994        if !current.is_empty() {
3995            out.push(current);
3996        }
3997        para.clear();
3998    };
3999    for line in &original {
4000        if line.trim().is_empty() {
4001            flush(&mut paragraph, &mut wrapped, width);
4002            wrapped.push(String::new());
4003        } else {
4004            paragraph.push(line.clone());
4005        }
4006    }
4007    flush(&mut paragraph, &mut wrapped, width);
4008
4009    // Splice back. push_undo above means `u` reverses.
4010    let after: Vec<String> = lines.split_off(bot + 1);
4011    lines.truncate(top);
4012    lines.extend(wrapped);
4013    lines.extend(after);
4014    ed.restore(lines, (top, 0));
4015    ed.mark_content_dirty();
4016}
4017
4018/// Transform the range `[top, bot]` (vim `RangeKind`) in place with
4019/// the given case operator. Cursor lands on `top` afterward — vim
4020/// convention for `gU{motion}` / `gu{motion}` / `g~{motion}`.
4021/// Preserves the textarea yank buffer (vim's case operators don't
4022/// touch registers).
4023fn apply_case_op_to_selection<H: crate::types::Host>(
4024    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4025    op: Operator,
4026    top: (usize, usize),
4027    bot: (usize, usize),
4028    kind: RangeKind,
4029) {
4030    use hjkl_buffer::Edit;
4031    ed.push_undo();
4032    let saved_yank = ed.yank().to_string();
4033    let saved_yank_linewise = ed.vim.yank_linewise;
4034    let selection = cut_vim_range(ed, top, bot, kind);
4035    let transformed = match op {
4036        Operator::Uppercase => selection.to_uppercase(),
4037        Operator::Lowercase => selection.to_lowercase(),
4038        Operator::ToggleCase => toggle_case_str(&selection),
4039        _ => unreachable!(),
4040    };
4041    if !transformed.is_empty() {
4042        let cursor = buf_cursor_pos(&ed.buffer);
4043        ed.mutate_edit(Edit::InsertStr {
4044            at: cursor,
4045            text: transformed,
4046        });
4047    }
4048    buf_set_cursor_rc(&mut ed.buffer, top.0, top.1);
4049    ed.push_buffer_cursor_to_textarea();
4050    ed.set_yank(saved_yank);
4051    ed.vim.yank_linewise = saved_yank_linewise;
4052    ed.vim.mode = Mode::Normal;
4053}
4054
4055/// Prepend `count * shiftwidth` spaces to each row in `[top, bot]`.
4056/// Rows that are empty are skipped (vim leaves blank lines alone when
4057/// indenting). `shiftwidth` is read from `editor.settings()` so
4058/// `:set shiftwidth=N` takes effect on the next operation.
4059fn indent_rows<H: crate::types::Host>(
4060    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4061    top: usize,
4062    bot: usize,
4063    count: usize,
4064) {
4065    ed.sync_buffer_content_from_textarea();
4066    let width = ed.settings().shiftwidth * count.max(1);
4067    let pad: String = " ".repeat(width);
4068    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4069    let bot = bot.min(lines.len().saturating_sub(1));
4070    for line in lines.iter_mut().take(bot + 1).skip(top) {
4071        if !line.is_empty() {
4072            line.insert_str(0, &pad);
4073        }
4074    }
4075    // Restore cursor to first non-blank of the top row so the next
4076    // vertical motion aims sensibly — matches vim's `>>` convention.
4077    ed.restore(lines, (top, 0));
4078    move_first_non_whitespace(ed);
4079}
4080
4081/// Remove up to `count * shiftwidth` leading spaces (or tabs) from
4082/// each row in `[top, bot]`. Rows with less leading whitespace have
4083/// all their indent stripped, not clipped to zero length.
4084fn outdent_rows<H: crate::types::Host>(
4085    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4086    top: usize,
4087    bot: usize,
4088    count: usize,
4089) {
4090    ed.sync_buffer_content_from_textarea();
4091    let width = ed.settings().shiftwidth * count.max(1);
4092    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4093    let bot = bot.min(lines.len().saturating_sub(1));
4094    for line in lines.iter_mut().take(bot + 1).skip(top) {
4095        let strip: usize = line
4096            .chars()
4097            .take(width)
4098            .take_while(|c| *c == ' ' || *c == '\t')
4099            .count();
4100        if strip > 0 {
4101            let byte_len: usize = line.chars().take(strip).map(|c| c.len_utf8()).sum();
4102            line.drain(..byte_len);
4103        }
4104    }
4105    ed.restore(lines, (top, 0));
4106    move_first_non_whitespace(ed);
4107}
4108
4109/// Count the number of open/close bracket pairs on a single line for the
4110/// auto-indent depth scanner. Only bare bracket scanning — does NOT handle
4111/// string literals or comments (v1 limitation, documented on
4112/// `auto_indent_range_bridge`).
4113/// Net bracket count `(open - close)` for a single line, skipping
4114/// brackets inside `//` line comments, `"..."` string literals, and
4115/// `'X'` char literals.
4116///
4117/// String / char escapes (`\"`, `\'`, `\\`) are honored so the closing
4118/// quote isn't missed when the literal contains a backslash.
4119///
4120/// Limitations:
4121/// - Block comments `/* ... */` are NOT tracked across lines (a single
4122///   line `/* foo { bar } */` is correctly skipped only because the
4123///   `/*` and `*/` are on the same line and we'd see `{` after `/*`).
4124///   For v1 we leave this since block comments mid-code are rare.
4125/// - Raw string literals `r"..."` / `r#"..."#` are NOT special-cased.
4126/// - Lifetime annotations like `'a` look like an unterminated char
4127///   literal — handled by the heuristic that a char literal MUST close
4128///   within the line; if the closing `'` isn't found, treat the `'` as
4129///   a normal character (lifetime).
4130///
4131/// Pre-fix the scan was naive — `//! ... }` on a doc comment
4132/// decremented depth, cascading wrong indentation through the rest of
4133/// the file. This caused ~19% of lines to mis-indent on a real Rust
4134/// source diagnostic.
4135fn bracket_net(line: &str) -> i32 {
4136    let mut net: i32 = 0;
4137    let mut chars = line.chars().peekable();
4138    while let Some(ch) = chars.next() {
4139        match ch {
4140            // `//` → rest of line is a comment, stop.
4141            '/' if chars.peek() == Some(&'/') => return net,
4142            '"' => {
4143                // String literal — consume until unescaped closing `"`.
4144                while let Some(c) = chars.next() {
4145                    match c {
4146                        '\\' => {
4147                            chars.next();
4148                        } // skip escape byte
4149                        '"' => break,
4150                        _ => {}
4151                    }
4152                }
4153            }
4154            '\'' => {
4155                // Char literal OR lifetime. A char literal closes within
4156                // a few chars (one or two for escapes). A lifetime is
4157                // `'ident` with no closing quote.
4158                //
4159                // Strategy: peek ahead for a closing `'`. If found
4160                // within ~4 chars, consume as char literal. Otherwise
4161                // treat the `'` as the start of a lifetime — leave the
4162                // remaining chars to be scanned normally.
4163                let saved: Vec<char> = chars.clone().take(5).collect();
4164                let close_idx = if saved.first() == Some(&'\\') {
4165                    saved.iter().skip(2).position(|&c| c == '\'').map(|p| p + 2)
4166                } else {
4167                    saved.iter().skip(1).position(|&c| c == '\'').map(|p| p + 1)
4168                };
4169                if let Some(idx) = close_idx {
4170                    for _ in 0..=idx {
4171                        chars.next();
4172                    }
4173                }
4174                // If no close found, leave chars alone — lifetime path.
4175            }
4176            '{' | '(' | '[' => net += 1,
4177            '}' | ')' | ']' => net -= 1,
4178            _ => {}
4179        }
4180    }
4181    net
4182}
4183
4184/// Reindent rows `[top, bot]` using shiftwidth-based bracket-depth counting.
4185///
4186/// The indent for each line is computed as follows:
4187/// 1. Scan all rows from 0 up to the target row, accumulating a bracket depth
4188///    (`depth`) from net open − close brackets per line. The scan starts at row
4189///    0 to give correct depth for code that appears mid-buffer.
4190/// 2. For the target line, peek at its first non-whitespace character:
4191///    if it is a close bracket (`}`, `)`, `]`) then `effective_depth =
4192///    depth.saturating_sub(1)`; otherwise `effective_depth = depth`.
4193/// 3. Strip the line's existing leading whitespace and prepend
4194///    `effective_depth × indent_unit` where `indent_unit` is `"\t"` when
4195///    `expandtab == false` or `" " × shiftwidth` when `expandtab == true`.
4196/// 4. Empty / whitespace-only lines are left empty (no trailing whitespace).
4197/// 5. After computing the new line, advance `depth` by the line's bracket
4198///    net count (open − close), where the leading close-bracket already
4199///    contributed `−1` to the net of its own line.
4200///
4201/// **v1 limitation**: the bracket scan is naive — it does not skip brackets
4202/// inside string literals (`"{"`, `'['`) or comments (`// {`). Code with
4203/// such patterns will produce incorrect indent depths. Tree-sitter / LSP
4204/// indentation is deferred to a follow-up.
4205fn auto_indent_rows<H: crate::types::Host>(
4206    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4207    top: usize,
4208    bot: usize,
4209) {
4210    ed.sync_buffer_content_from_textarea();
4211    let shiftwidth = ed.settings().shiftwidth;
4212    let expandtab = ed.settings().expandtab;
4213    let indent_unit: String = if expandtab {
4214        " ".repeat(shiftwidth)
4215    } else {
4216        "\t".to_string()
4217    };
4218
4219    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4220    let bot = bot.min(lines.len().saturating_sub(1));
4221
4222    // Accumulate bracket depth from row 0 up to `top - 1` so we start with
4223    // the correct depth for the first line of the target range.
4224    let mut depth: i32 = 0;
4225    for line in lines.iter().take(top) {
4226        depth += bracket_net(line);
4227        if depth < 0 {
4228            depth = 0;
4229        }
4230    }
4231
4232    for line in lines.iter_mut().take(bot + 1).skip(top) {
4233        let trimmed_owned = line.trim_start().to_owned();
4234        // Empty / whitespace-only lines stay empty.
4235        if trimmed_owned.is_empty() {
4236            *line = String::new();
4237            // depth contribution from an empty line is zero; no bracket scan needed.
4238            continue;
4239        }
4240
4241        // Detect leading close-bracket for effective depth.
4242        let starts_with_close = trimmed_owned
4243            .chars()
4244            .next()
4245            .is_some_and(|c| matches!(c, '}' | ')' | ']'));
4246        // Chain continuation: a line starting with `.` (e.g. `.foo()`)
4247        // hangs off the previous expression and gets one extra indent
4248        // level, matching cargo fmt / clang-format conventions for
4249        // method chains like:
4250        //   let x = foo()
4251        //       .bar()
4252        //       .baz();
4253        // Range expressions (`..`) and try-chains (`?.`) are out of
4254        // scope for v1 — single leading `.` is the common case.
4255        let starts_with_dot = trimmed_owned.starts_with('.')
4256            && !trimmed_owned.starts_with("..")
4257            && !trimmed_owned.starts_with(".;");
4258        let effective_depth = if starts_with_close {
4259            depth.saturating_sub(1)
4260        } else if starts_with_dot {
4261            depth.saturating_add(1)
4262        } else {
4263            depth
4264        } as usize;
4265
4266        // Build new line: indent × depth + stripped content.
4267        let new_line = format!("{}{}", indent_unit.repeat(effective_depth), trimmed_owned);
4268
4269        // Advance depth by this line's net bracket count (scan trimmed content).
4270        depth += bracket_net(&trimmed_owned);
4271        if depth < 0 {
4272            depth = 0;
4273        }
4274
4275        *line = new_line;
4276    }
4277
4278    // Restore cursor to the first non-blank of `top` (vim parity for `==`).
4279    ed.restore(lines, (top, 0));
4280    move_first_non_whitespace(ed);
4281    // Record the touched row range so the host can display a visual flash.
4282    ed.last_indent_range = Some((top, bot));
4283}
4284
4285fn toggle_case_str(s: &str) -> String {
4286    s.chars()
4287        .map(|c| {
4288            if c.is_lowercase() {
4289                c.to_uppercase().next().unwrap_or(c)
4290            } else if c.is_uppercase() {
4291                c.to_lowercase().next().unwrap_or(c)
4292            } else {
4293                c
4294            }
4295        })
4296        .collect()
4297}
4298
4299fn order(a: (usize, usize), b: (usize, usize)) -> ((usize, usize), (usize, usize)) {
4300    if a <= b { (a, b) } else { (b, a) }
4301}
4302
4303/// Clamp the buffer cursor to normal-mode valid position: col may not
4304/// exceed `line.chars().count().saturating_sub(1)` (or 0 on an empty
4305/// line). Vim applies this clamp on every return to Normal mode after an
4306/// operator or Esc-from-insert.
4307fn clamp_cursor_to_normal_mode<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
4308    let (row, col) = ed.cursor();
4309    let line_chars = buf_line_chars(&ed.buffer, row);
4310    let max_col = line_chars.saturating_sub(1);
4311    if col > max_col {
4312        buf_set_cursor_rc(&mut ed.buffer, row, max_col);
4313        ed.push_buffer_cursor_to_textarea();
4314    }
4315}
4316
4317// ─── dd/cc/yy ──────────────────────────────────────────────────────────────
4318
4319fn execute_line_op<H: crate::types::Host>(
4320    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4321    op: Operator,
4322    count: usize,
4323) {
4324    let (row, col) = ed.cursor();
4325    let total = buf_row_count(&ed.buffer);
4326    let end_row = (row + count.saturating_sub(1)).min(total.saturating_sub(1));
4327
4328    match op {
4329        Operator::Yank => {
4330            // yy must not move the cursor.
4331            let text = read_vim_range(ed, (row, col), (end_row, 0), RangeKind::Linewise);
4332            if !text.is_empty() {
4333                ed.record_yank_to_host(text.clone());
4334                ed.record_yank(text, true);
4335            }
4336            // Vim `:h '[` / `:h ']`: yy/Nyy — linewise yank; `[` =
4337            // (top_row, 0), `]` = (bot_row, last_col).
4338            let last_col = buf_line_chars(&ed.buffer, end_row).saturating_sub(1);
4339            ed.set_mark('[', (row, 0));
4340            ed.set_mark(']', (end_row, last_col));
4341            buf_set_cursor_rc(&mut ed.buffer, row, col);
4342            ed.push_buffer_cursor_to_textarea();
4343            ed.vim.mode = Mode::Normal;
4344        }
4345        Operator::Delete => {
4346            ed.push_undo();
4347            let deleted_through_last = end_row + 1 >= total;
4348            cut_vim_range(ed, (row, col), (end_row, 0), RangeKind::Linewise);
4349            // Vim's `dd` / `Ndd` leaves the cursor on the *first
4350            // non-blank* of the line that now occupies `row` — or, if
4351            // the deletion consumed the last line, the line above it.
4352            let total_after = buf_row_count(&ed.buffer);
4353            let raw_target = if deleted_through_last {
4354                row.saturating_sub(1).min(total_after.saturating_sub(1))
4355            } else {
4356                row.min(total_after.saturating_sub(1))
4357            };
4358            // Clamp off the trailing phantom empty row that arises from a
4359            // buffer with a trailing newline (stored as ["...", ""]). If
4360            // the target row is the trailing empty row and there is a real
4361            // content row above it, use that instead — matching vim's view
4362            // that the trailing `\n` is a terminator, not a separator.
4363            let target_row = if raw_target > 0
4364                && raw_target + 1 == total_after
4365                && buf_line(&ed.buffer, raw_target)
4366                    .map(str::is_empty)
4367                    .unwrap_or(false)
4368            {
4369                raw_target - 1
4370            } else {
4371                raw_target
4372            };
4373            buf_set_cursor_rc(&mut ed.buffer, target_row, 0);
4374            ed.push_buffer_cursor_to_textarea();
4375            move_first_non_whitespace(ed);
4376            ed.sticky_col = Some(ed.cursor().1);
4377            ed.vim.mode = Mode::Normal;
4378            // Vim `:h '[` / `:h ']`: dd/Ndd — both marks park at the
4379            // post-delete cursor position (the join point).
4380            let pos = ed.cursor();
4381            ed.set_mark('[', pos);
4382            ed.set_mark(']', pos);
4383        }
4384        Operator::Change => {
4385            // `cc` / `3cc`: wipe contents of the covered lines but leave
4386            // a single blank line so insert-mode opens on it. Done as two
4387            // edits: drop rows past the first, then clear row `row`.
4388            use hjkl_buffer::{Edit, MotionKind as BufKind, Position};
4389            // Vim `:h '[`: stash change start for `]` deferral on insert-exit.
4390            ed.vim.change_mark_start = Some((row, 0));
4391            ed.push_undo();
4392            ed.sync_buffer_content_from_textarea();
4393            // Read the cut payload first so yank reflects every line.
4394            let payload = read_vim_range(ed, (row, col), (end_row, 0), RangeKind::Linewise);
4395            if end_row > row {
4396                ed.mutate_edit(Edit::DeleteRange {
4397                    start: Position::new(row + 1, 0),
4398                    end: Position::new(end_row, 0),
4399                    kind: BufKind::Line,
4400                });
4401            }
4402            let line_chars = buf_line_chars(&ed.buffer, row);
4403            if line_chars > 0 {
4404                ed.mutate_edit(Edit::DeleteRange {
4405                    start: Position::new(row, 0),
4406                    end: Position::new(row, line_chars),
4407                    kind: BufKind::Char,
4408                });
4409            }
4410            if !payload.is_empty() {
4411                ed.record_yank_to_host(payload.clone());
4412                ed.record_delete(payload, true);
4413            }
4414            buf_set_cursor_rc(&mut ed.buffer, row, 0);
4415            ed.push_buffer_cursor_to_textarea();
4416            begin_insert_noundo(ed, 1, InsertReason::AfterChange);
4417        }
4418        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4419            // `gUU` / `guu` / `g~~` — linewise case transform over
4420            // [row, end_row]. Preserve cursor on `row` (first non-blank
4421            // lines up with vim's behaviour).
4422            apply_case_op_to_selection(ed, op, (row, col), (end_row, 0), RangeKind::Linewise);
4423            // After case-op on a linewise range vim puts the cursor on
4424            // the first non-blank of the starting line.
4425            move_first_non_whitespace(ed);
4426        }
4427        Operator::Indent | Operator::Outdent => {
4428            // `>>` / `N>>` / `<<` / `N<<` — linewise indent / outdent.
4429            ed.push_undo();
4430            if op == Operator::Indent {
4431                indent_rows(ed, row, end_row, 1);
4432            } else {
4433                outdent_rows(ed, row, end_row, 1);
4434            }
4435            ed.sticky_col = Some(ed.cursor().1);
4436            ed.vim.mode = Mode::Normal;
4437        }
4438        // No doubled form — `zfzf` is two consecutive `zf` chords.
4439        Operator::Fold => unreachable!("Fold has no line-op double"),
4440        Operator::Reflow => {
4441            // `gqq` / `Ngqq` — reflow `count` rows starting at the cursor.
4442            ed.push_undo();
4443            reflow_rows(ed, row, end_row);
4444            move_first_non_whitespace(ed);
4445            ed.sticky_col = Some(ed.cursor().1);
4446            ed.vim.mode = Mode::Normal;
4447        }
4448        Operator::AutoIndent => {
4449            // `==` / `N==` — auto-indent `count` rows starting at cursor.
4450            ed.push_undo();
4451            auto_indent_rows(ed, row, end_row);
4452            ed.sticky_col = Some(ed.cursor().1);
4453            ed.vim.mode = Mode::Normal;
4454        }
4455    }
4456}
4457
4458// ─── Visual mode operators ─────────────────────────────────────────────────
4459
4460pub(crate) fn apply_visual_operator<H: crate::types::Host>(
4461    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4462    op: Operator,
4463) {
4464    match ed.vim.mode {
4465        Mode::VisualLine => {
4466            let cursor_row = buf_cursor_pos(&ed.buffer).row;
4467            let top = cursor_row.min(ed.vim.visual_line_anchor);
4468            let bot = cursor_row.max(ed.vim.visual_line_anchor);
4469            ed.vim.yank_linewise = true;
4470            match op {
4471                Operator::Yank => {
4472                    let text = read_vim_range(ed, (top, 0), (bot, 0), RangeKind::Linewise);
4473                    if !text.is_empty() {
4474                        ed.record_yank_to_host(text.clone());
4475                        ed.record_yank(text, true);
4476                    }
4477                    buf_set_cursor_rc(&mut ed.buffer, top, 0);
4478                    ed.push_buffer_cursor_to_textarea();
4479                    ed.vim.mode = Mode::Normal;
4480                }
4481                Operator::Delete => {
4482                    ed.push_undo();
4483                    cut_vim_range(ed, (top, 0), (bot, 0), RangeKind::Linewise);
4484                    ed.vim.mode = Mode::Normal;
4485                }
4486                Operator::Change => {
4487                    // Vim `Vc`: wipe the line contents but leave a blank
4488                    // line in place so insert-mode starts on an empty row.
4489                    use hjkl_buffer::{Edit, MotionKind as BufKind, Position};
4490                    ed.push_undo();
4491                    ed.sync_buffer_content_from_textarea();
4492                    let payload = read_vim_range(ed, (top, 0), (bot, 0), RangeKind::Linewise);
4493                    if bot > top {
4494                        ed.mutate_edit(Edit::DeleteRange {
4495                            start: Position::new(top + 1, 0),
4496                            end: Position::new(bot, 0),
4497                            kind: BufKind::Line,
4498                        });
4499                    }
4500                    let line_chars = buf_line_chars(&ed.buffer, top);
4501                    if line_chars > 0 {
4502                        ed.mutate_edit(Edit::DeleteRange {
4503                            start: Position::new(top, 0),
4504                            end: Position::new(top, line_chars),
4505                            kind: BufKind::Char,
4506                        });
4507                    }
4508                    if !payload.is_empty() {
4509                        ed.record_yank_to_host(payload.clone());
4510                        ed.record_delete(payload, true);
4511                    }
4512                    buf_set_cursor_rc(&mut ed.buffer, top, 0);
4513                    ed.push_buffer_cursor_to_textarea();
4514                    begin_insert_noundo(ed, 1, InsertReason::AfterChange);
4515                }
4516                Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4517                    let bot = buf_cursor_pos(&ed.buffer)
4518                        .row
4519                        .max(ed.vim.visual_line_anchor);
4520                    apply_case_op_to_selection(ed, op, (top, 0), (bot, 0), RangeKind::Linewise);
4521                    move_first_non_whitespace(ed);
4522                }
4523                Operator::Indent | Operator::Outdent => {
4524                    ed.push_undo();
4525                    let (cursor_row, _) = ed.cursor();
4526                    let bot = cursor_row.max(ed.vim.visual_line_anchor);
4527                    if op == Operator::Indent {
4528                        indent_rows(ed, top, bot, 1);
4529                    } else {
4530                        outdent_rows(ed, top, bot, 1);
4531                    }
4532                    ed.vim.mode = Mode::Normal;
4533                }
4534                Operator::Reflow => {
4535                    ed.push_undo();
4536                    let (cursor_row, _) = ed.cursor();
4537                    let bot = cursor_row.max(ed.vim.visual_line_anchor);
4538                    reflow_rows(ed, top, bot);
4539                    ed.vim.mode = Mode::Normal;
4540                }
4541                Operator::AutoIndent => {
4542                    ed.push_undo();
4543                    let (cursor_row, _) = ed.cursor();
4544                    let bot = cursor_row.max(ed.vim.visual_line_anchor);
4545                    auto_indent_rows(ed, top, bot);
4546                    ed.vim.mode = Mode::Normal;
4547                }
4548                // Visual `zf` is handled inline in `handle_after_z`,
4549                // never routed through this dispatcher.
4550                Operator::Fold => unreachable!("Visual zf takes its own path"),
4551            }
4552        }
4553        Mode::Visual => {
4554            ed.vim.yank_linewise = false;
4555            let anchor = ed.vim.visual_anchor;
4556            let cursor = ed.cursor();
4557            let (top, bot) = order(anchor, cursor);
4558            match op {
4559                Operator::Yank => {
4560                    let text = read_vim_range(ed, top, bot, RangeKind::Inclusive);
4561                    if !text.is_empty() {
4562                        ed.record_yank_to_host(text.clone());
4563                        ed.record_yank(text, false);
4564                    }
4565                    buf_set_cursor_rc(&mut ed.buffer, top.0, top.1);
4566                    ed.push_buffer_cursor_to_textarea();
4567                    ed.vim.mode = Mode::Normal;
4568                }
4569                Operator::Delete => {
4570                    ed.push_undo();
4571                    cut_vim_range(ed, top, bot, RangeKind::Inclusive);
4572                    ed.vim.mode = Mode::Normal;
4573                }
4574                Operator::Change => {
4575                    ed.push_undo();
4576                    cut_vim_range(ed, top, bot, RangeKind::Inclusive);
4577                    begin_insert_noundo(ed, 1, InsertReason::AfterChange);
4578                }
4579                Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4580                    // Anchor stays where the visual selection started.
4581                    let anchor = ed.vim.visual_anchor;
4582                    let cursor = ed.cursor();
4583                    let (top, bot) = order(anchor, cursor);
4584                    apply_case_op_to_selection(ed, op, top, bot, RangeKind::Inclusive);
4585                }
4586                Operator::Indent | Operator::Outdent => {
4587                    ed.push_undo();
4588                    let anchor = ed.vim.visual_anchor;
4589                    let cursor = ed.cursor();
4590                    let (top, bot) = order(anchor, cursor);
4591                    if op == Operator::Indent {
4592                        indent_rows(ed, top.0, bot.0, 1);
4593                    } else {
4594                        outdent_rows(ed, top.0, bot.0, 1);
4595                    }
4596                    ed.vim.mode = Mode::Normal;
4597                }
4598                Operator::Reflow => {
4599                    ed.push_undo();
4600                    let anchor = ed.vim.visual_anchor;
4601                    let cursor = ed.cursor();
4602                    let (top, bot) = order(anchor, cursor);
4603                    reflow_rows(ed, top.0, bot.0);
4604                    ed.vim.mode = Mode::Normal;
4605                }
4606                Operator::AutoIndent => {
4607                    ed.push_undo();
4608                    let anchor = ed.vim.visual_anchor;
4609                    let cursor = ed.cursor();
4610                    let (top, bot) = order(anchor, cursor);
4611                    auto_indent_rows(ed, top.0, bot.0);
4612                    ed.vim.mode = Mode::Normal;
4613                }
4614                Operator::Fold => unreachable!("Visual zf takes its own path"),
4615            }
4616        }
4617        Mode::VisualBlock => apply_block_operator(ed, op),
4618        _ => {}
4619    }
4620}
4621
4622/// Compute `(top_row, bot_row, left_col, right_col)` for the current
4623/// VisualBlock selection. Columns are inclusive on both ends. Uses the
4624/// tracked virtual column (updated by h/l, preserved across j/k) so
4625/// ragged / empty rows don't collapse the block's width.
4626fn block_bounds<H: crate::types::Host>(
4627    ed: &Editor<hjkl_buffer::Buffer, H>,
4628) -> (usize, usize, usize, usize) {
4629    let (ar, ac) = ed.vim.block_anchor;
4630    let (cr, _) = ed.cursor();
4631    let cc = ed.vim.block_vcol;
4632    let top = ar.min(cr);
4633    let bot = ar.max(cr);
4634    let left = ac.min(cc);
4635    let right = ac.max(cc);
4636    (top, bot, left, right)
4637}
4638
4639/// Update the virtual column after a motion in VisualBlock mode.
4640/// Horizontal motions sync `block_vcol` to the new cursor column;
4641/// vertical / non-h/l motions leave it alone so the intended column
4642/// survives clamping to shorter lines.
4643pub(crate) fn update_block_vcol<H: crate::types::Host>(
4644    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4645    motion: &Motion,
4646) {
4647    match motion {
4648        Motion::Left
4649        | Motion::Right
4650        | Motion::WordFwd
4651        | Motion::BigWordFwd
4652        | Motion::WordBack
4653        | Motion::BigWordBack
4654        | Motion::WordEnd
4655        | Motion::BigWordEnd
4656        | Motion::WordEndBack
4657        | Motion::BigWordEndBack
4658        | Motion::LineStart
4659        | Motion::FirstNonBlank
4660        | Motion::LineEnd
4661        | Motion::Find { .. }
4662        | Motion::FindRepeat { .. }
4663        | Motion::MatchBracket => {
4664            ed.vim.block_vcol = ed.cursor().1;
4665        }
4666        // Up / Down / FileTop / FileBottom / Search — preserve vcol.
4667        _ => {}
4668    }
4669}
4670
4671/// Yank / delete / change / replace a rectangular selection. Yanked text
4672/// is stored as one string per row joined with `\n` so pasting reproduces
4673/// the block as sequential lines. (Vim's true block-paste reinserts as
4674/// columns; we render the content with our char-wise paste path.)
4675fn apply_block_operator<H: crate::types::Host>(
4676    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4677    op: Operator,
4678) {
4679    let (top, bot, left, right) = block_bounds(ed);
4680    // Snapshot the block text for yank / clipboard.
4681    let yank = block_yank(ed, top, bot, left, right);
4682
4683    match op {
4684        Operator::Yank => {
4685            if !yank.is_empty() {
4686                ed.record_yank_to_host(yank.clone());
4687                ed.record_yank(yank, false);
4688            }
4689            ed.vim.mode = Mode::Normal;
4690            ed.jump_cursor(top, left);
4691        }
4692        Operator::Delete => {
4693            ed.push_undo();
4694            delete_block_contents(ed, top, bot, left, right);
4695            if !yank.is_empty() {
4696                ed.record_yank_to_host(yank.clone());
4697                ed.record_delete(yank, false);
4698            }
4699            ed.vim.mode = Mode::Normal;
4700            ed.jump_cursor(top, left);
4701        }
4702        Operator::Change => {
4703            ed.push_undo();
4704            delete_block_contents(ed, top, bot, left, right);
4705            if !yank.is_empty() {
4706                ed.record_yank_to_host(yank.clone());
4707                ed.record_delete(yank, false);
4708            }
4709            ed.jump_cursor(top, left);
4710            begin_insert_noundo(
4711                ed,
4712                1,
4713                InsertReason::BlockChange {
4714                    top,
4715                    bot,
4716                    col: left,
4717                },
4718            );
4719        }
4720        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4721            ed.push_undo();
4722            transform_block_case(ed, op, top, bot, left, right);
4723            ed.vim.mode = Mode::Normal;
4724            ed.jump_cursor(top, left);
4725        }
4726        Operator::Indent | Operator::Outdent => {
4727            // VisualBlock `>` / `<` falls back to linewise indent over
4728            // the block's row range — vim does the same (column-wise
4729            // indent/outdent doesn't make sense).
4730            ed.push_undo();
4731            if op == Operator::Indent {
4732                indent_rows(ed, top, bot, 1);
4733            } else {
4734                outdent_rows(ed, top, bot, 1);
4735            }
4736            ed.vim.mode = Mode::Normal;
4737        }
4738        Operator::Fold => unreachable!("Visual zf takes its own path"),
4739        Operator::Reflow => {
4740            // Reflow over the block falls back to linewise reflow over
4741            // the row range — column slicing for `gq` doesn't make
4742            // sense.
4743            ed.push_undo();
4744            reflow_rows(ed, top, bot);
4745            ed.vim.mode = Mode::Normal;
4746        }
4747        Operator::AutoIndent => {
4748            // AutoIndent over the block falls back to linewise
4749            // auto-indent over the row range.
4750            ed.push_undo();
4751            auto_indent_rows(ed, top, bot);
4752            ed.vim.mode = Mode::Normal;
4753        }
4754    }
4755}
4756
4757/// In-place case transform over the rectangular block
4758/// `(top..=bot, left..=right)`. Rows shorter than `left` are left
4759/// untouched — vim behaves the same way (ragged blocks).
4760fn transform_block_case<H: crate::types::Host>(
4761    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4762    op: Operator,
4763    top: usize,
4764    bot: usize,
4765    left: usize,
4766    right: usize,
4767) {
4768    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4769    for r in top..=bot.min(lines.len().saturating_sub(1)) {
4770        let chars: Vec<char> = lines[r].chars().collect();
4771        if left >= chars.len() {
4772            continue;
4773        }
4774        let end = (right + 1).min(chars.len());
4775        let head: String = chars[..left].iter().collect();
4776        let mid: String = chars[left..end].iter().collect();
4777        let tail: String = chars[end..].iter().collect();
4778        let transformed = match op {
4779            Operator::Uppercase => mid.to_uppercase(),
4780            Operator::Lowercase => mid.to_lowercase(),
4781            Operator::ToggleCase => toggle_case_str(&mid),
4782            _ => mid,
4783        };
4784        lines[r] = format!("{head}{transformed}{tail}");
4785    }
4786    let saved_yank = ed.yank().to_string();
4787    let saved_linewise = ed.vim.yank_linewise;
4788    ed.restore(lines, (top, left));
4789    ed.set_yank(saved_yank);
4790    ed.vim.yank_linewise = saved_linewise;
4791}
4792
4793fn block_yank<H: crate::types::Host>(
4794    ed: &Editor<hjkl_buffer::Buffer, H>,
4795    top: usize,
4796    bot: usize,
4797    left: usize,
4798    right: usize,
4799) -> String {
4800    let lines = buf_lines_to_vec(&ed.buffer);
4801    let mut rows: Vec<String> = Vec::new();
4802    for r in top..=bot {
4803        let line = match lines.get(r) {
4804            Some(l) => l,
4805            None => break,
4806        };
4807        let chars: Vec<char> = line.chars().collect();
4808        let end = (right + 1).min(chars.len());
4809        if left >= chars.len() {
4810            rows.push(String::new());
4811        } else {
4812            rows.push(chars[left..end].iter().collect());
4813        }
4814    }
4815    rows.join("\n")
4816}
4817
4818fn delete_block_contents<H: crate::types::Host>(
4819    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4820    top: usize,
4821    bot: usize,
4822    left: usize,
4823    right: usize,
4824) {
4825    use hjkl_buffer::{Edit, MotionKind, Position};
4826    ed.sync_buffer_content_from_textarea();
4827    let last_row = bot.min(buf_row_count(&ed.buffer).saturating_sub(1));
4828    if last_row < top {
4829        return;
4830    }
4831    ed.mutate_edit(Edit::DeleteRange {
4832        start: Position::new(top, left),
4833        end: Position::new(last_row, right),
4834        kind: MotionKind::Block,
4835    });
4836    ed.push_buffer_cursor_to_textarea();
4837}
4838
4839/// Replace each character cell in the block with `ch`.
4840pub(crate) fn block_replace<H: crate::types::Host>(
4841    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4842    ch: char,
4843) {
4844    let (top, bot, left, right) = block_bounds(ed);
4845    ed.push_undo();
4846    ed.sync_buffer_content_from_textarea();
4847    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4848    for r in top..=bot.min(lines.len().saturating_sub(1)) {
4849        let chars: Vec<char> = lines[r].chars().collect();
4850        if left >= chars.len() {
4851            continue;
4852        }
4853        let end = (right + 1).min(chars.len());
4854        let before: String = chars[..left].iter().collect();
4855        let middle: String = std::iter::repeat_n(ch, end - left).collect();
4856        let after: String = chars[end..].iter().collect();
4857        lines[r] = format!("{before}{middle}{after}");
4858    }
4859    reset_textarea_lines(ed, lines);
4860    ed.vim.mode = Mode::Normal;
4861    ed.jump_cursor(top, left);
4862}
4863
4864/// Replace buffer content with `lines` while preserving the cursor.
4865/// Used by indent / outdent / block_replace to wholesale rewrite
4866/// rows without going through the per-edit funnel.
4867fn reset_textarea_lines<H: crate::types::Host>(
4868    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4869    lines: Vec<String>,
4870) {
4871    let cursor = ed.cursor();
4872    crate::types::BufferEdit::replace_all(&mut ed.buffer, &lines.join("\n"));
4873    buf_set_cursor_rc(&mut ed.buffer, cursor.0, cursor.1);
4874    ed.mark_content_dirty();
4875}
4876
4877// ─── Visual-line helpers ───────────────────────────────────────────────────
4878
4879// ─── Text-object range computation ─────────────────────────────────────────
4880
4881/// Cursor position as `(row, col)`.
4882type Pos = (usize, usize);
4883
4884/// Returns `(start, end, kind)` where `end` is *exclusive* (one past the
4885/// last character to act on). `kind` is `Linewise` for line-oriented text
4886/// objects like paragraphs and `Exclusive` otherwise.
4887pub(crate) fn text_object_range<H: crate::types::Host>(
4888    ed: &Editor<hjkl_buffer::Buffer, H>,
4889    obj: TextObject,
4890    inner: bool,
4891) -> Option<(Pos, Pos, RangeKind)> {
4892    match obj {
4893        TextObject::Word { big } => {
4894            word_text_object(ed, inner, big).map(|(s, e)| (s, e, RangeKind::Exclusive))
4895        }
4896        TextObject::Quote(q) => {
4897            quote_text_object(ed, q, inner).map(|(s, e)| (s, e, RangeKind::Exclusive))
4898        }
4899        TextObject::Bracket(open) => bracket_text_object(ed, open, inner),
4900        TextObject::Paragraph => {
4901            paragraph_text_object(ed, inner).map(|(s, e)| (s, e, RangeKind::Linewise))
4902        }
4903        TextObject::XmlTag => tag_text_object(ed, inner).map(|(s, e)| (s, e, RangeKind::Exclusive)),
4904        TextObject::Sentence => {
4905            sentence_text_object(ed, inner).map(|(s, e)| (s, e, RangeKind::Exclusive))
4906        }
4907    }
4908}
4909
4910/// `(` / `)` — walk to the next sentence boundary in `forward` direction.
4911/// Returns `(row, col)` of the boundary's first non-whitespace cell, or
4912/// `None` when already at the buffer's edge in that direction.
4913fn sentence_boundary<H: crate::types::Host>(
4914    ed: &Editor<hjkl_buffer::Buffer, H>,
4915    forward: bool,
4916) -> Option<(usize, usize)> {
4917    let lines = buf_lines_to_vec(&ed.buffer);
4918    if lines.is_empty() {
4919        return None;
4920    }
4921    let pos_to_idx = |pos: (usize, usize)| -> usize {
4922        let mut idx = 0;
4923        for line in lines.iter().take(pos.0) {
4924            idx += line.chars().count() + 1;
4925        }
4926        idx + pos.1
4927    };
4928    let idx_to_pos = |mut idx: usize| -> (usize, usize) {
4929        for (r, line) in lines.iter().enumerate() {
4930            let len = line.chars().count();
4931            if idx <= len {
4932                return (r, idx);
4933            }
4934            idx -= len + 1;
4935        }
4936        let last = lines.len().saturating_sub(1);
4937        (last, lines[last].chars().count())
4938    };
4939    let mut chars: Vec<char> = Vec::new();
4940    for (r, line) in lines.iter().enumerate() {
4941        chars.extend(line.chars());
4942        if r + 1 < lines.len() {
4943            chars.push('\n');
4944        }
4945    }
4946    if chars.is_empty() {
4947        return None;
4948    }
4949    let total = chars.len();
4950    let cursor_idx = pos_to_idx(ed.cursor()).min(total - 1);
4951    let is_terminator = |c: char| matches!(c, '.' | '?' | '!');
4952
4953    if forward {
4954        // Walk forward looking for a terminator run followed by
4955        // whitespace; land on the first non-whitespace cell after.
4956        let mut i = cursor_idx + 1;
4957        while i < total {
4958            if is_terminator(chars[i]) {
4959                while i + 1 < total && is_terminator(chars[i + 1]) {
4960                    i += 1;
4961                }
4962                if i + 1 >= total {
4963                    return None;
4964                }
4965                if chars[i + 1].is_whitespace() {
4966                    let mut j = i + 1;
4967                    while j < total && chars[j].is_whitespace() {
4968                        j += 1;
4969                    }
4970                    if j >= total {
4971                        return None;
4972                    }
4973                    return Some(idx_to_pos(j));
4974                }
4975            }
4976            i += 1;
4977        }
4978        None
4979    } else {
4980        // Walk backward to find the start of the current sentence (if
4981        // we're already at the start, jump to the previous sentence's
4982        // start instead).
4983        let find_start = |from: usize| -> Option<usize> {
4984            let mut start = from;
4985            while start > 0 {
4986                let prev = chars[start - 1];
4987                if prev.is_whitespace() {
4988                    let mut k = start - 1;
4989                    while k > 0 && chars[k - 1].is_whitespace() {
4990                        k -= 1;
4991                    }
4992                    if k > 0 && is_terminator(chars[k - 1]) {
4993                        break;
4994                    }
4995                }
4996                start -= 1;
4997            }
4998            while start < total && chars[start].is_whitespace() {
4999                start += 1;
5000            }
5001            (start < total).then_some(start)
5002        };
5003        let current_start = find_start(cursor_idx)?;
5004        if current_start < cursor_idx {
5005            return Some(idx_to_pos(current_start));
5006        }
5007        // Already at the sentence start — step over the boundary into
5008        // the previous sentence and find its start.
5009        let mut k = current_start;
5010        while k > 0 && chars[k - 1].is_whitespace() {
5011            k -= 1;
5012        }
5013        if k == 0 {
5014            return None;
5015        }
5016        let prev_start = find_start(k - 1)?;
5017        Some(idx_to_pos(prev_start))
5018    }
5019}
5020
5021/// `is` / `as` — sentence: text up to and including the next sentence
5022/// terminator (`.`, `?`, `!`). Vim treats `.`/`?`/`!` followed by
5023/// whitespace (or end-of-line) as a boundary; runs of consecutive
5024/// terminators stay attached to the same sentence. `as` extends to
5025/// include trailing whitespace; `is` does not.
5026fn sentence_text_object<H: crate::types::Host>(
5027    ed: &Editor<hjkl_buffer::Buffer, H>,
5028    inner: bool,
5029) -> Option<((usize, usize), (usize, usize))> {
5030    let lines = buf_lines_to_vec(&ed.buffer);
5031    if lines.is_empty() {
5032        return None;
5033    }
5034    // Flatten the buffer so a sentence can span lines (vim's behaviour).
5035    // Newlines count as whitespace for boundary detection.
5036    let pos_to_idx = |pos: (usize, usize)| -> usize {
5037        let mut idx = 0;
5038        for line in lines.iter().take(pos.0) {
5039            idx += line.chars().count() + 1;
5040        }
5041        idx + pos.1
5042    };
5043    let idx_to_pos = |mut idx: usize| -> (usize, usize) {
5044        for (r, line) in lines.iter().enumerate() {
5045            let len = line.chars().count();
5046            if idx <= len {
5047                return (r, idx);
5048            }
5049            idx -= len + 1;
5050        }
5051        let last = lines.len().saturating_sub(1);
5052        (last, lines[last].chars().count())
5053    };
5054    let mut chars: Vec<char> = Vec::new();
5055    for (r, line) in lines.iter().enumerate() {
5056        chars.extend(line.chars());
5057        if r + 1 < lines.len() {
5058            chars.push('\n');
5059        }
5060    }
5061    if chars.is_empty() {
5062        return None;
5063    }
5064
5065    let cursor_idx = pos_to_idx(ed.cursor()).min(chars.len() - 1);
5066    let is_terminator = |c: char| matches!(c, '.' | '?' | '!');
5067
5068    // Walk backward from cursor to find the start of the current
5069    // sentence. A boundary is: whitespace immediately after a run of
5070    // terminators (or start-of-buffer).
5071    let mut start = cursor_idx;
5072    while start > 0 {
5073        let prev = chars[start - 1];
5074        if prev.is_whitespace() {
5075            // Check if the whitespace follows a terminator — if so,
5076            // we've crossed a sentence boundary; the sentence begins
5077            // at the first non-whitespace cell *after* this run.
5078            let mut k = start - 1;
5079            while k > 0 && chars[k - 1].is_whitespace() {
5080                k -= 1;
5081            }
5082            if k > 0 && is_terminator(chars[k - 1]) {
5083                break;
5084            }
5085        }
5086        start -= 1;
5087    }
5088    // Skip leading whitespace (vim doesn't include it in the
5089    // sentence body).
5090    while start < chars.len() && chars[start].is_whitespace() {
5091        start += 1;
5092    }
5093    if start >= chars.len() {
5094        return None;
5095    }
5096
5097    // Walk forward to the sentence end (last terminator before the
5098    // next whitespace boundary).
5099    let mut end = start;
5100    while end < chars.len() {
5101        if is_terminator(chars[end]) {
5102            // Consume any consecutive terminators (e.g. `?!`).
5103            while end + 1 < chars.len() && is_terminator(chars[end + 1]) {
5104                end += 1;
5105            }
5106            // If followed by whitespace or end-of-buffer, that's the
5107            // boundary.
5108            if end + 1 >= chars.len() || chars[end + 1].is_whitespace() {
5109                break;
5110            }
5111        }
5112        end += 1;
5113    }
5114    // Inclusive end → exclusive end_idx.
5115    let end_idx = (end + 1).min(chars.len());
5116
5117    let final_end = if inner {
5118        end_idx
5119    } else {
5120        // `as`: include trailing whitespace (but stop before the next
5121        // newline so we don't gobble a paragraph break — vim keeps
5122        // sentences within a paragraph for the trailing-ws extension).
5123        let mut e = end_idx;
5124        while e < chars.len() && chars[e].is_whitespace() && chars[e] != '\n' {
5125            e += 1;
5126        }
5127        e
5128    };
5129
5130    Some((idx_to_pos(start), idx_to_pos(final_end)))
5131}
5132
5133/// `it` / `at` — XML tag pair text object. Builds a flat char index of
5134/// the buffer, walks `<...>` tokens to pair tags via a stack, and
5135/// returns the innermost pair containing the cursor.
5136fn tag_text_object<H: crate::types::Host>(
5137    ed: &Editor<hjkl_buffer::Buffer, H>,
5138    inner: bool,
5139) -> Option<((usize, usize), (usize, usize))> {
5140    let lines = buf_lines_to_vec(&ed.buffer);
5141    if lines.is_empty() {
5142        return None;
5143    }
5144    // Flatten char positions so we can compare cursor against tag
5145    // ranges without per-row arithmetic. `\n` between lines counts as
5146    // a single char.
5147    let pos_to_idx = |pos: (usize, usize)| -> usize {
5148        let mut idx = 0;
5149        for line in lines.iter().take(pos.0) {
5150            idx += line.chars().count() + 1;
5151        }
5152        idx + pos.1
5153    };
5154    let idx_to_pos = |mut idx: usize| -> (usize, usize) {
5155        for (r, line) in lines.iter().enumerate() {
5156            let len = line.chars().count();
5157            if idx <= len {
5158                return (r, idx);
5159            }
5160            idx -= len + 1;
5161        }
5162        let last = lines.len().saturating_sub(1);
5163        (last, lines[last].chars().count())
5164    };
5165    let mut chars: Vec<char> = Vec::new();
5166    for (r, line) in lines.iter().enumerate() {
5167        chars.extend(line.chars());
5168        if r + 1 < lines.len() {
5169            chars.push('\n');
5170        }
5171    }
5172    let cursor_idx = pos_to_idx(ed.cursor());
5173
5174    // Walk `<...>` tokens. Track open tags on a stack; on a matching
5175    // close pop and consider the pair a candidate when the cursor lies
5176    // inside its content range. Innermost wins (replace whenever a
5177    // tighter range turns up). Also track the first complete pair that
5178    // starts at or after the cursor so we can fall back to a forward
5179    // scan (targets.vim-style) when the cursor isn't inside any tag.
5180    let mut stack: Vec<(usize, usize, String)> = Vec::new(); // (open_start, content_start, name)
5181    let mut innermost: Option<(usize, usize, usize, usize)> = None;
5182    let mut next_after: Option<(usize, usize, usize, usize)> = None;
5183    let mut i = 0;
5184    while i < chars.len() {
5185        if chars[i] != '<' {
5186            i += 1;
5187            continue;
5188        }
5189        let mut j = i + 1;
5190        while j < chars.len() && chars[j] != '>' {
5191            j += 1;
5192        }
5193        if j >= chars.len() {
5194            break;
5195        }
5196        let inside: String = chars[i + 1..j].iter().collect();
5197        let close_end = j + 1;
5198        let trimmed = inside.trim();
5199        if trimmed.starts_with('!') || trimmed.starts_with('?') {
5200            i = close_end;
5201            continue;
5202        }
5203        if let Some(rest) = trimmed.strip_prefix('/') {
5204            let name = rest.split_whitespace().next().unwrap_or("").to_string();
5205            if !name.is_empty()
5206                && let Some(stack_idx) = stack.iter().rposition(|(_, _, n)| *n == name)
5207            {
5208                let (open_start, content_start, _) = stack[stack_idx].clone();
5209                stack.truncate(stack_idx);
5210                let content_end = i;
5211                let candidate = (open_start, content_start, content_end, close_end);
5212                if cursor_idx >= content_start && cursor_idx <= content_end {
5213                    innermost = match innermost {
5214                        Some((_, cs, ce, _)) if cs <= content_start && content_end <= ce => {
5215                            Some(candidate)
5216                        }
5217                        None => Some(candidate),
5218                        existing => existing,
5219                    };
5220                } else if open_start >= cursor_idx && next_after.is_none() {
5221                    next_after = Some(candidate);
5222                }
5223            }
5224        } else if !trimmed.ends_with('/') {
5225            let name: String = trimmed
5226                .split(|c: char| c.is_whitespace() || c == '/')
5227                .next()
5228                .unwrap_or("")
5229                .to_string();
5230            if !name.is_empty() {
5231                stack.push((i, close_end, name));
5232            }
5233        }
5234        i = close_end;
5235    }
5236
5237    let (open_start, content_start, content_end, close_end) = innermost.or(next_after)?;
5238    if inner {
5239        Some((idx_to_pos(content_start), idx_to_pos(content_end)))
5240    } else {
5241        Some((idx_to_pos(open_start), idx_to_pos(close_end)))
5242    }
5243}
5244
5245fn is_wordchar(c: char) -> bool {
5246    c.is_alphanumeric() || c == '_'
5247}
5248
5249// `is_keyword_char` lives in hjkl-buffer (used by word motions);
5250// engine re-uses it via `hjkl_buffer::is_keyword_char` so there's
5251// one parser, one default, one bug surface.
5252pub(crate) use hjkl_buffer::is_keyword_char;
5253
5254fn word_text_object<H: crate::types::Host>(
5255    ed: &Editor<hjkl_buffer::Buffer, H>,
5256    inner: bool,
5257    big: bool,
5258) -> Option<((usize, usize), (usize, usize))> {
5259    let (row, col) = ed.cursor();
5260    let line = buf_line(&ed.buffer, row)?;
5261    let chars: Vec<char> = line.chars().collect();
5262    if chars.is_empty() {
5263        return None;
5264    }
5265    let at = col.min(chars.len().saturating_sub(1));
5266    let classify = |c: char| -> u8 {
5267        if c.is_whitespace() {
5268            0
5269        } else if big || is_wordchar(c) {
5270            1
5271        } else {
5272            2
5273        }
5274    };
5275    let cls = classify(chars[at]);
5276    let mut start = at;
5277    while start > 0 && classify(chars[start - 1]) == cls {
5278        start -= 1;
5279    }
5280    let mut end = at;
5281    while end + 1 < chars.len() && classify(chars[end + 1]) == cls {
5282        end += 1;
5283    }
5284    // Byte-offset helpers.
5285    let char_byte = |i: usize| {
5286        if i >= chars.len() {
5287            line.len()
5288        } else {
5289            line.char_indices().nth(i).map(|(b, _)| b).unwrap_or(0)
5290        }
5291    };
5292    let mut start_col = char_byte(start);
5293    // Exclusive end: byte index of char AFTER the last-included char.
5294    let mut end_col = char_byte(end + 1);
5295    if !inner {
5296        // `aw` — include trailing whitespace; if there's no trailing ws, absorb leading ws.
5297        let mut t = end + 1;
5298        let mut included_trailing = false;
5299        while t < chars.len() && chars[t].is_whitespace() {
5300            included_trailing = true;
5301            t += 1;
5302        }
5303        if included_trailing {
5304            end_col = char_byte(t);
5305        } else {
5306            let mut s = start;
5307            while s > 0 && chars[s - 1].is_whitespace() {
5308                s -= 1;
5309            }
5310            start_col = char_byte(s);
5311        }
5312    }
5313    Some(((row, start_col), (row, end_col)))
5314}
5315
5316fn quote_text_object<H: crate::types::Host>(
5317    ed: &Editor<hjkl_buffer::Buffer, H>,
5318    q: char,
5319    inner: bool,
5320) -> Option<((usize, usize), (usize, usize))> {
5321    let (row, col) = ed.cursor();
5322    let line = buf_line(&ed.buffer, row)?;
5323    let bytes = line.as_bytes();
5324    let q_byte = q as u8;
5325    // Find opening and closing quote on the same line.
5326    let mut positions: Vec<usize> = Vec::new();
5327    for (i, &b) in bytes.iter().enumerate() {
5328        if b == q_byte {
5329            positions.push(i);
5330        }
5331    }
5332    if positions.len() < 2 {
5333        return None;
5334    }
5335    let mut open_idx: Option<usize> = None;
5336    let mut close_idx: Option<usize> = None;
5337    for pair in positions.chunks(2) {
5338        if pair.len() < 2 {
5339            break;
5340        }
5341        if col >= pair[0] && col <= pair[1] {
5342            open_idx = Some(pair[0]);
5343            close_idx = Some(pair[1]);
5344            break;
5345        }
5346        if col < pair[0] {
5347            open_idx = Some(pair[0]);
5348            close_idx = Some(pair[1]);
5349            break;
5350        }
5351    }
5352    let open = open_idx?;
5353    let close = close_idx?;
5354    // End columns are *exclusive* — one past the last character to act on.
5355    if inner {
5356        if close <= open + 1 {
5357            return None;
5358        }
5359        Some(((row, open + 1), (row, close)))
5360    } else {
5361        // `da<q>` — "around" includes the surrounding whitespace on one
5362        // side: trailing whitespace if any exists after the closing quote;
5363        // otherwise leading whitespace before the opening quote. This
5364        // matches vim's `:help text-objects` behaviour and avoids leaving
5365        // a double-space when the quoted span sits mid-sentence.
5366        let after_close = close + 1; // byte index after closing quote
5367        if after_close < bytes.len() && bytes[after_close].is_ascii_whitespace() {
5368            // Eat trailing whitespace run.
5369            let mut end = after_close;
5370            while end < bytes.len() && bytes[end].is_ascii_whitespace() {
5371                end += 1;
5372            }
5373            Some(((row, open), (row, end)))
5374        } else if open > 0 && bytes[open - 1].is_ascii_whitespace() {
5375            // Eat leading whitespace run.
5376            let mut start = open;
5377            while start > 0 && bytes[start - 1].is_ascii_whitespace() {
5378                start -= 1;
5379            }
5380            Some(((row, start), (row, close + 1)))
5381        } else {
5382            Some(((row, open), (row, close + 1)))
5383        }
5384    }
5385}
5386
5387fn bracket_text_object<H: crate::types::Host>(
5388    ed: &Editor<hjkl_buffer::Buffer, H>,
5389    open: char,
5390    inner: bool,
5391) -> Option<(Pos, Pos, RangeKind)> {
5392    let close = match open {
5393        '(' => ')',
5394        '[' => ']',
5395        '{' => '}',
5396        '<' => '>',
5397        _ => return None,
5398    };
5399    let (row, col) = ed.cursor();
5400    let lines = buf_lines_to_vec(&ed.buffer);
5401    let lines = lines.as_slice();
5402    // Walk backward from cursor to find unbalanced opening. When the
5403    // cursor isn't inside any pair, fall back to scanning forward for
5404    // the next opening bracket (targets.vim-style: `ci(` works when
5405    // cursor is before the `(` on the same line or below).
5406    let open_pos = find_open_bracket(lines, row, col, open, close)
5407        .or_else(|| find_next_open(lines, row, col, open))?;
5408    let close_pos = find_close_bracket(lines, open_pos.0, open_pos.1 + 1, open, close)?;
5409    // End positions are *exclusive*.
5410    if inner {
5411        // Multi-line `iB` / `i{` etc: vim deletes the full lines between
5412        // the braces (linewise), preserving the `{` and `}` lines
5413        // themselves and the newlines that directly abut them. E.g.:
5414        //   {\n    body\n}\n  →  {\n}\n    (cursor on `}` line)
5415        // Single-line `i{` falls back to charwise exclusive.
5416        if close_pos.0 > open_pos.0 + 1 {
5417            // There is at least one line strictly between open and close.
5418            let inner_row_start = open_pos.0 + 1;
5419            let inner_row_end = close_pos.0 - 1;
5420            let end_col = lines
5421                .get(inner_row_end)
5422                .map(|l| l.chars().count())
5423                .unwrap_or(0);
5424            return Some((
5425                (inner_row_start, 0),
5426                (inner_row_end, end_col),
5427                RangeKind::Linewise,
5428            ));
5429        }
5430        let inner_start = advance_pos(lines, open_pos);
5431        if inner_start.0 > close_pos.0
5432            || (inner_start.0 == close_pos.0 && inner_start.1 >= close_pos.1)
5433        {
5434            return None;
5435        }
5436        Some((inner_start, close_pos, RangeKind::Exclusive))
5437    } else {
5438        Some((
5439            open_pos,
5440            advance_pos(lines, close_pos),
5441            RangeKind::Exclusive,
5442        ))
5443    }
5444}
5445
5446fn find_open_bracket(
5447    lines: &[String],
5448    row: usize,
5449    col: usize,
5450    open: char,
5451    close: char,
5452) -> Option<(usize, usize)> {
5453    let mut depth: i32 = 0;
5454    let mut r = row;
5455    let mut c = col as isize;
5456    loop {
5457        let cur = &lines[r];
5458        let chars: Vec<char> = cur.chars().collect();
5459        // Clamp `c` to the line length: callers may seed `col` past
5460        // EOL on virtual-cursor lines (e.g., insert mode after `o`)
5461        // so direct indexing would panic on empty / short lines.
5462        if (c as usize) >= chars.len() {
5463            c = chars.len() as isize - 1;
5464        }
5465        while c >= 0 {
5466            let ch = chars[c as usize];
5467            if ch == close {
5468                depth += 1;
5469            } else if ch == open {
5470                if depth == 0 {
5471                    return Some((r, c as usize));
5472                }
5473                depth -= 1;
5474            }
5475            c -= 1;
5476        }
5477        if r == 0 {
5478            return None;
5479        }
5480        r -= 1;
5481        c = lines[r].chars().count() as isize - 1;
5482    }
5483}
5484
5485fn find_close_bracket(
5486    lines: &[String],
5487    row: usize,
5488    start_col: usize,
5489    open: char,
5490    close: char,
5491) -> Option<(usize, usize)> {
5492    let mut depth: i32 = 0;
5493    let mut r = row;
5494    let mut c = start_col;
5495    loop {
5496        let cur = &lines[r];
5497        let chars: Vec<char> = cur.chars().collect();
5498        while c < chars.len() {
5499            let ch = chars[c];
5500            if ch == open {
5501                depth += 1;
5502            } else if ch == close {
5503                if depth == 0 {
5504                    return Some((r, c));
5505                }
5506                depth -= 1;
5507            }
5508            c += 1;
5509        }
5510        if r + 1 >= lines.len() {
5511            return None;
5512        }
5513        r += 1;
5514        c = 0;
5515    }
5516}
5517
5518/// Forward scan from `(row, col)` for the next occurrence of `open`.
5519/// Multi-line. Used by bracket text objects to support targets.vim-style
5520/// "search forward when not currently inside a pair" behaviour.
5521fn find_next_open(lines: &[String], row: usize, col: usize, open: char) -> Option<(usize, usize)> {
5522    let mut r = row;
5523    let mut c = col;
5524    while r < lines.len() {
5525        let chars: Vec<char> = lines[r].chars().collect();
5526        while c < chars.len() {
5527            if chars[c] == open {
5528                return Some((r, c));
5529            }
5530            c += 1;
5531        }
5532        r += 1;
5533        c = 0;
5534    }
5535    None
5536}
5537
5538fn advance_pos(lines: &[String], pos: (usize, usize)) -> (usize, usize) {
5539    let (r, c) = pos;
5540    let line_len = lines[r].chars().count();
5541    if c < line_len {
5542        (r, c + 1)
5543    } else if r + 1 < lines.len() {
5544        (r + 1, 0)
5545    } else {
5546        pos
5547    }
5548}
5549
5550fn paragraph_text_object<H: crate::types::Host>(
5551    ed: &Editor<hjkl_buffer::Buffer, H>,
5552    inner: bool,
5553) -> Option<((usize, usize), (usize, usize))> {
5554    let (row, _) = ed.cursor();
5555    let lines = buf_lines_to_vec(&ed.buffer);
5556    if lines.is_empty() {
5557        return None;
5558    }
5559    // A paragraph is a run of non-blank lines.
5560    let is_blank = |r: usize| lines.get(r).map(|s| s.trim().is_empty()).unwrap_or(true);
5561    if is_blank(row) {
5562        return None;
5563    }
5564    let mut top = row;
5565    while top > 0 && !is_blank(top - 1) {
5566        top -= 1;
5567    }
5568    let mut bot = row;
5569    while bot + 1 < lines.len() && !is_blank(bot + 1) {
5570        bot += 1;
5571    }
5572    // For `ap`, include one trailing blank line if present.
5573    if !inner && bot + 1 < lines.len() && is_blank(bot + 1) {
5574        bot += 1;
5575    }
5576    let end_col = lines[bot].chars().count();
5577    Some(((top, 0), (bot, end_col)))
5578}
5579
5580// ─── Individual commands ───────────────────────────────────────────────────
5581
5582/// Read the text in a vim-shaped range without mutating. Used by
5583/// `Operator::Yank` so we can pipe the same range translation as
5584/// [`cut_vim_range`] but skip the delete + inverse extraction.
5585fn read_vim_range<H: crate::types::Host>(
5586    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5587    start: (usize, usize),
5588    end: (usize, usize),
5589    kind: RangeKind,
5590) -> String {
5591    let (top, bot) = order(start, end);
5592    ed.sync_buffer_content_from_textarea();
5593    let lines = buf_lines_to_vec(&ed.buffer);
5594    match kind {
5595        RangeKind::Linewise => {
5596            let lo = top.0;
5597            let hi = bot.0.min(lines.len().saturating_sub(1));
5598            let mut text = lines[lo..=hi].join("\n");
5599            text.push('\n');
5600            text
5601        }
5602        RangeKind::Inclusive | RangeKind::Exclusive => {
5603            let inclusive = matches!(kind, RangeKind::Inclusive);
5604            // Walk row-by-row collecting chars in `[top, end_exclusive)`.
5605            let mut out = String::new();
5606            for row in top.0..=bot.0 {
5607                let line = lines.get(row).map(String::as_str).unwrap_or("");
5608                let lo = if row == top.0 { top.1 } else { 0 };
5609                let hi_unclamped = if row == bot.0 {
5610                    if inclusive { bot.1 + 1 } else { bot.1 }
5611                } else {
5612                    line.chars().count() + 1
5613                };
5614                let row_chars: Vec<char> = line.chars().collect();
5615                let hi = hi_unclamped.min(row_chars.len());
5616                if lo < hi {
5617                    out.push_str(&row_chars[lo..hi].iter().collect::<String>());
5618                }
5619                if row < bot.0 {
5620                    out.push('\n');
5621                }
5622            }
5623            out
5624        }
5625    }
5626}
5627
5628/// Cut a vim-shaped range through the Buffer edit funnel and return
5629/// the deleted text. Translates vim's `RangeKind`
5630/// (Linewise/Inclusive/Exclusive) into the buffer's
5631/// `hjkl_buffer::MotionKind` (Line/Char) and applies the right end-
5632/// position adjustment so inclusive motions actually include the bot
5633/// cell. Pushes the cut text into both `last_yank` and the textarea
5634/// yank buffer (still observed by `p`/`P` until the paste path is
5635/// ported), and updates `yank_linewise` for linewise cuts.
5636fn cut_vim_range<H: crate::types::Host>(
5637    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5638    start: (usize, usize),
5639    end: (usize, usize),
5640    kind: RangeKind,
5641) -> String {
5642    use hjkl_buffer::{Edit, MotionKind as BufKind, Position};
5643    let (top, bot) = order(start, end);
5644    ed.sync_buffer_content_from_textarea();
5645    let (buf_start, buf_end, buf_kind) = match kind {
5646        RangeKind::Linewise => (
5647            Position::new(top.0, 0),
5648            Position::new(bot.0, 0),
5649            BufKind::Line,
5650        ),
5651        RangeKind::Inclusive => {
5652            let line_chars = buf_line_chars(&ed.buffer, bot.0);
5653            // Advance one cell past `bot` so the buffer's exclusive
5654            // `cut_chars` actually drops the inclusive endpoint. Wrap
5655            // to the next row when bot already sits on the last char.
5656            let next = if bot.1 < line_chars {
5657                Position::new(bot.0, bot.1 + 1)
5658            } else if bot.0 + 1 < buf_row_count(&ed.buffer) {
5659                Position::new(bot.0 + 1, 0)
5660            } else {
5661                Position::new(bot.0, line_chars)
5662            };
5663            (Position::new(top.0, top.1), next, BufKind::Char)
5664        }
5665        RangeKind::Exclusive => (
5666            Position::new(top.0, top.1),
5667            Position::new(bot.0, bot.1),
5668            BufKind::Char,
5669        ),
5670    };
5671    let inverse = ed.mutate_edit(Edit::DeleteRange {
5672        start: buf_start,
5673        end: buf_end,
5674        kind: buf_kind,
5675    });
5676    let text = match inverse {
5677        Edit::InsertStr { text, .. } => text,
5678        _ => String::new(),
5679    };
5680    if !text.is_empty() {
5681        ed.record_yank_to_host(text.clone());
5682        ed.record_delete(text.clone(), matches!(kind, RangeKind::Linewise));
5683    }
5684    ed.push_buffer_cursor_to_textarea();
5685    text
5686}
5687
5688/// `D` / `C` — delete from cursor to end of line through the edit
5689/// funnel. Mirrors the deleted text into both `ed.last_yank` and the
5690/// textarea's yank buffer (still observed by `p`/`P` until the paste
5691/// path is ported). Cursor lands at the deletion start so the caller
5692/// can decide whether to step it left (`D`) or open insert mode (`C`).
5693fn delete_to_eol<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5694    use hjkl_buffer::{Edit, MotionKind, Position};
5695    ed.sync_buffer_content_from_textarea();
5696    let cursor = buf_cursor_pos(&ed.buffer);
5697    let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5698    if cursor.col >= line_chars {
5699        return;
5700    }
5701    let inverse = ed.mutate_edit(Edit::DeleteRange {
5702        start: cursor,
5703        end: Position::new(cursor.row, line_chars),
5704        kind: MotionKind::Char,
5705    });
5706    if let Edit::InsertStr { text, .. } = inverse
5707        && !text.is_empty()
5708    {
5709        ed.record_yank_to_host(text.clone());
5710        ed.vim.yank_linewise = false;
5711        ed.set_yank(text);
5712    }
5713    buf_set_cursor_pos(&mut ed.buffer, cursor);
5714    ed.push_buffer_cursor_to_textarea();
5715}
5716
5717fn do_char_delete<H: crate::types::Host>(
5718    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5719    forward: bool,
5720    count: usize,
5721) {
5722    use hjkl_buffer::{Edit, MotionKind, Position};
5723    ed.push_undo();
5724    ed.sync_buffer_content_from_textarea();
5725    // Collect deleted chars so we can write them to the unnamed register
5726    // (vim's `x`/`X` populate `"` so that `xp` round-trips the char).
5727    let mut deleted = String::new();
5728    for _ in 0..count {
5729        let cursor = buf_cursor_pos(&ed.buffer);
5730        let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5731        if forward {
5732            // `x` — delete the char under the cursor. Vim no-ops on
5733            // an empty line; the buffer would drop a row otherwise.
5734            if cursor.col >= line_chars {
5735                continue;
5736            }
5737            let inverse = ed.mutate_edit(Edit::DeleteRange {
5738                start: cursor,
5739                end: Position::new(cursor.row, cursor.col + 1),
5740                kind: MotionKind::Char,
5741            });
5742            if let Edit::InsertStr { text, .. } = inverse {
5743                deleted.push_str(&text);
5744            }
5745        } else {
5746            // `X` — delete the char before the cursor.
5747            if cursor.col == 0 {
5748                continue;
5749            }
5750            let inverse = ed.mutate_edit(Edit::DeleteRange {
5751                start: Position::new(cursor.row, cursor.col - 1),
5752                end: cursor,
5753                kind: MotionKind::Char,
5754            });
5755            if let Edit::InsertStr { text, .. } = inverse {
5756                // X deletes backwards; prepend so the register text
5757                // matches reading order (first deleted char first).
5758                deleted = text + &deleted;
5759            }
5760        }
5761    }
5762    if !deleted.is_empty() {
5763        ed.record_yank_to_host(deleted.clone());
5764        ed.record_delete(deleted, false);
5765    }
5766    ed.push_buffer_cursor_to_textarea();
5767}
5768
5769/// Vim `Ctrl-a` / `Ctrl-x` — find the next decimal number at or after the
5770/// cursor on the current line, add `delta`, leave the cursor on the last
5771/// digit of the result. No-op if the line has no digits to the right.
5772pub(crate) fn adjust_number<H: crate::types::Host>(
5773    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5774    delta: i64,
5775) -> bool {
5776    use hjkl_buffer::{Edit, MotionKind, Position};
5777    ed.sync_buffer_content_from_textarea();
5778    let cursor = buf_cursor_pos(&ed.buffer);
5779    let row = cursor.row;
5780    let chars: Vec<char> = match buf_line(&ed.buffer, row) {
5781        Some(l) => l.chars().collect(),
5782        None => return false,
5783    };
5784    let Some(digit_start) = (cursor.col..chars.len()).find(|&i| chars[i].is_ascii_digit()) else {
5785        return false;
5786    };
5787    let span_start = if digit_start > 0 && chars[digit_start - 1] == '-' {
5788        digit_start - 1
5789    } else {
5790        digit_start
5791    };
5792    let mut span_end = digit_start;
5793    while span_end < chars.len() && chars[span_end].is_ascii_digit() {
5794        span_end += 1;
5795    }
5796    let s: String = chars[span_start..span_end].iter().collect();
5797    let Ok(n) = s.parse::<i64>() else {
5798        return false;
5799    };
5800    let new_s = n.saturating_add(delta).to_string();
5801
5802    ed.push_undo();
5803    let span_start_pos = Position::new(row, span_start);
5804    let span_end_pos = Position::new(row, span_end);
5805    ed.mutate_edit(Edit::DeleteRange {
5806        start: span_start_pos,
5807        end: span_end_pos,
5808        kind: MotionKind::Char,
5809    });
5810    ed.mutate_edit(Edit::InsertStr {
5811        at: span_start_pos,
5812        text: new_s.clone(),
5813    });
5814    let new_len = new_s.chars().count();
5815    buf_set_cursor_rc(&mut ed.buffer, row, span_start + new_len.saturating_sub(1));
5816    ed.push_buffer_cursor_to_textarea();
5817    true
5818}
5819
5820pub(crate) fn replace_char<H: crate::types::Host>(
5821    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5822    ch: char,
5823    count: usize,
5824) {
5825    use hjkl_buffer::{Edit, MotionKind, Position};
5826    ed.push_undo();
5827    ed.sync_buffer_content_from_textarea();
5828    for _ in 0..count {
5829        let cursor = buf_cursor_pos(&ed.buffer);
5830        let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5831        if cursor.col >= line_chars {
5832            break;
5833        }
5834        ed.mutate_edit(Edit::DeleteRange {
5835            start: cursor,
5836            end: Position::new(cursor.row, cursor.col + 1),
5837            kind: MotionKind::Char,
5838        });
5839        ed.mutate_edit(Edit::InsertChar { at: cursor, ch });
5840    }
5841    // Vim leaves the cursor on the last replaced char.
5842    crate::motions::move_left(&mut ed.buffer, 1);
5843    ed.push_buffer_cursor_to_textarea();
5844}
5845
5846fn toggle_case_at_cursor<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5847    use hjkl_buffer::{Edit, MotionKind, Position};
5848    ed.sync_buffer_content_from_textarea();
5849    let cursor = buf_cursor_pos(&ed.buffer);
5850    let Some(c) = buf_line(&ed.buffer, cursor.row).and_then(|l| l.chars().nth(cursor.col)) else {
5851        return;
5852    };
5853    let toggled = if c.is_uppercase() {
5854        c.to_lowercase().next().unwrap_or(c)
5855    } else {
5856        c.to_uppercase().next().unwrap_or(c)
5857    };
5858    ed.mutate_edit(Edit::DeleteRange {
5859        start: cursor,
5860        end: Position::new(cursor.row, cursor.col + 1),
5861        kind: MotionKind::Char,
5862    });
5863    ed.mutate_edit(Edit::InsertChar {
5864        at: cursor,
5865        ch: toggled,
5866    });
5867}
5868
5869fn join_line<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5870    use hjkl_buffer::{Edit, Position};
5871    ed.sync_buffer_content_from_textarea();
5872    let row = buf_cursor_pos(&ed.buffer).row;
5873    if row + 1 >= buf_row_count(&ed.buffer) {
5874        return;
5875    }
5876    let cur_line = buf_line(&ed.buffer, row).unwrap_or("").to_string();
5877    let next_raw = buf_line(&ed.buffer, row + 1).unwrap_or("").to_string();
5878    let next_trimmed = next_raw.trim_start();
5879    let cur_chars = cur_line.chars().count();
5880    let next_chars = next_raw.chars().count();
5881    // `J` inserts a single space iff both sides are non-empty after
5882    // stripping the next line's leading whitespace.
5883    let separator = if !cur_line.is_empty() && !next_trimmed.is_empty() {
5884        " "
5885    } else {
5886        ""
5887    };
5888    let joined = format!("{cur_line}{separator}{next_trimmed}");
5889    ed.mutate_edit(Edit::Replace {
5890        start: Position::new(row, 0),
5891        end: Position::new(row + 1, next_chars),
5892        with: joined,
5893    });
5894    // Vim parks the cursor on the inserted space — or at the join
5895    // point when no space went in (which is the same column either
5896    // way, since the space sits exactly at `cur_chars`).
5897    buf_set_cursor_rc(&mut ed.buffer, row, cur_chars);
5898    ed.push_buffer_cursor_to_textarea();
5899}
5900
5901/// `gJ` — join the next line onto the current one without inserting a
5902/// separating space or stripping leading whitespace.
5903fn join_line_raw<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5904    use hjkl_buffer::Edit;
5905    ed.sync_buffer_content_from_textarea();
5906    let row = buf_cursor_pos(&ed.buffer).row;
5907    if row + 1 >= buf_row_count(&ed.buffer) {
5908        return;
5909    }
5910    let join_col = buf_line_chars(&ed.buffer, row);
5911    ed.mutate_edit(Edit::JoinLines {
5912        row,
5913        count: 1,
5914        with_space: false,
5915    });
5916    // Vim leaves the cursor at the join point (end of original line).
5917    buf_set_cursor_rc(&mut ed.buffer, row, join_col);
5918    ed.push_buffer_cursor_to_textarea();
5919}
5920
5921fn do_paste<H: crate::types::Host>(
5922    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5923    before: bool,
5924    count: usize,
5925) {
5926    use hjkl_buffer::{Edit, Position};
5927    ed.push_undo();
5928    // Resolve the source register: `"reg` prefix (consumed) or the
5929    // unnamed register otherwise. Read text + linewise from the
5930    // selected slot rather than the global `vim.yank_linewise` so
5931    // pasting from `"0` after a delete still uses the yank's layout.
5932    let selector = ed.vim.pending_register.take();
5933    let (yank, linewise) = match selector.and_then(|c| ed.registers().read(c)) {
5934        Some(slot) => (slot.text.clone(), slot.linewise),
5935        // Read both fields from the unnamed slot rather than mixing the
5936        // slot's text with `vim.yank_linewise`. The cached vim flag is
5937        // per-editor, so a register imported from another editor (e.g.
5938        // cross-buffer yank/paste) carried the wrong linewise without
5939        // this — pasting a linewise yank inserted at the char cursor.
5940        None => {
5941            let s = &ed.registers().unnamed;
5942            (s.text.clone(), s.linewise)
5943        }
5944    };
5945    // Vim `:h '[` / `:h ']`: after paste `[` = first inserted char of
5946    // the final paste, `]` = last inserted char of the final paste.
5947    // We track (lo, hi) across iterations; the last value wins.
5948    let mut paste_mark: Option<((usize, usize), (usize, usize))> = None;
5949    for _ in 0..count {
5950        ed.sync_buffer_content_from_textarea();
5951        let yank = yank.clone();
5952        if yank.is_empty() {
5953            continue;
5954        }
5955        if linewise {
5956            // Linewise paste: insert payload as fresh row(s) above
5957            // (`P`) or below (`p`) the cursor's row. Cursor lands on
5958            // the first non-blank of the first pasted line.
5959            let text = yank.trim_matches('\n').to_string();
5960            let row = buf_cursor_pos(&ed.buffer).row;
5961            let target_row = if before {
5962                ed.mutate_edit(Edit::InsertStr {
5963                    at: Position::new(row, 0),
5964                    text: format!("{text}\n"),
5965                });
5966                row
5967            } else {
5968                let line_chars = buf_line_chars(&ed.buffer, row);
5969                ed.mutate_edit(Edit::InsertStr {
5970                    at: Position::new(row, line_chars),
5971                    text: format!("\n{text}"),
5972                });
5973                row + 1
5974            };
5975            buf_set_cursor_rc(&mut ed.buffer, target_row, 0);
5976            crate::motions::move_first_non_blank(&mut ed.buffer);
5977            ed.push_buffer_cursor_to_textarea();
5978            // Linewise: `[` = (target_row, 0), `]` = (bot_row, last_col).
5979            let payload_lines = text.lines().count().max(1);
5980            let bot_row = target_row + payload_lines - 1;
5981            let bot_last_col = buf_line_chars(&ed.buffer, bot_row).saturating_sub(1);
5982            paste_mark = Some(((target_row, 0), (bot_row, bot_last_col)));
5983        } else {
5984            // Charwise paste. `P` inserts at cursor (shifting cell
5985            // right); `p` inserts after cursor (advance one cell
5986            // first, clamped to the end of the line).
5987            let cursor = buf_cursor_pos(&ed.buffer);
5988            let at = if before {
5989                cursor
5990            } else {
5991                let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5992                Position::new(cursor.row, (cursor.col + 1).min(line_chars))
5993            };
5994            ed.mutate_edit(Edit::InsertStr {
5995                at,
5996                text: yank.clone(),
5997            });
5998            // Vim parks the cursor on the last char of the pasted
5999            // text (do_insert_str leaves it one past the end).
6000            crate::motions::move_left(&mut ed.buffer, 1);
6001            ed.push_buffer_cursor_to_textarea();
6002            // Charwise: `[` = insert start, `]` = cursor (last pasted char).
6003            let lo = (at.row, at.col);
6004            let hi = ed.cursor();
6005            paste_mark = Some((lo, hi));
6006        }
6007    }
6008    if let Some((lo, hi)) = paste_mark {
6009        ed.set_mark('[', lo);
6010        ed.set_mark(']', hi);
6011    }
6012    // Any paste re-anchors the sticky column to the new cursor position.
6013    ed.sticky_col = Some(buf_cursor_pos(&ed.buffer).col);
6014}
6015
6016pub(crate) fn do_undo<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
6017    if let Some((lines, cursor)) = ed.undo_stack.pop() {
6018        let current = ed.snapshot();
6019        ed.redo_stack.push(current);
6020        ed.restore(lines, cursor);
6021    }
6022    ed.vim.mode = Mode::Normal;
6023    // The restored cursor came from a snapshot taken in insert mode
6024    // (before the insert started) and may be past the last valid
6025    // normal-mode column. Clamp it now, same as Esc-from-insert does.
6026    clamp_cursor_to_normal_mode(ed);
6027}
6028
6029pub(crate) fn do_redo<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
6030    if let Some((lines, cursor)) = ed.redo_stack.pop() {
6031        let current = ed.snapshot();
6032        ed.undo_stack.push(current);
6033        ed.cap_undo();
6034        ed.restore(lines, cursor);
6035    }
6036    ed.vim.mode = Mode::Normal;
6037}
6038
6039// ─── Dot repeat ────────────────────────────────────────────────────────────
6040
6041/// Replay-side helper: insert `text` at the cursor through the
6042/// edit funnel, then leave insert mode (the original change ended
6043/// with Esc, so the dot-repeat must end the same way — including
6044/// the cursor step-back vim does on Esc-from-insert).
6045fn replay_insert_and_finish<H: crate::types::Host>(
6046    ed: &mut Editor<hjkl_buffer::Buffer, H>,
6047    text: &str,
6048) {
6049    use hjkl_buffer::{Edit, Position};
6050    let cursor = ed.cursor();
6051    ed.mutate_edit(Edit::InsertStr {
6052        at: Position::new(cursor.0, cursor.1),
6053        text: text.to_string(),
6054    });
6055    if ed.vim.insert_session.take().is_some() {
6056        if ed.cursor().1 > 0 {
6057            crate::motions::move_left(&mut ed.buffer, 1);
6058            ed.push_buffer_cursor_to_textarea();
6059        }
6060        ed.vim.mode = Mode::Normal;
6061    }
6062}
6063
6064pub(crate) fn replay_last_change<H: crate::types::Host>(
6065    ed: &mut Editor<hjkl_buffer::Buffer, H>,
6066    outer_count: usize,
6067) {
6068    let Some(change) = ed.vim.last_change.clone() else {
6069        return;
6070    };
6071    ed.vim.replaying = true;
6072    let scale = if outer_count > 0 { outer_count } else { 1 };
6073    match change {
6074        LastChange::OpMotion {
6075            op,
6076            motion,
6077            count,
6078            inserted,
6079        } => {
6080            let total = count.max(1) * scale;
6081            apply_op_with_motion(ed, op, &motion, total);
6082            if let Some(text) = inserted {
6083                replay_insert_and_finish(ed, &text);
6084            }
6085        }
6086        LastChange::OpTextObj {
6087            op,
6088            obj,
6089            inner,
6090            inserted,
6091        } => {
6092            apply_op_with_text_object(ed, op, obj, inner);
6093            if let Some(text) = inserted {
6094                replay_insert_and_finish(ed, &text);
6095            }
6096        }
6097        LastChange::LineOp {
6098            op,
6099            count,
6100            inserted,
6101        } => {
6102            let total = count.max(1) * scale;
6103            execute_line_op(ed, op, total);
6104            if let Some(text) = inserted {
6105                replay_insert_and_finish(ed, &text);
6106            }
6107        }
6108        LastChange::CharDel { forward, count } => {
6109            do_char_delete(ed, forward, count * scale);
6110        }
6111        LastChange::ReplaceChar { ch, count } => {
6112            replace_char(ed, ch, count * scale);
6113        }
6114        LastChange::ToggleCase { count } => {
6115            for _ in 0..count * scale {
6116                ed.push_undo();
6117                toggle_case_at_cursor(ed);
6118            }
6119        }
6120        LastChange::JoinLine { count } => {
6121            for _ in 0..count * scale {
6122                ed.push_undo();
6123                join_line(ed);
6124            }
6125        }
6126        LastChange::Paste { before, count } => {
6127            do_paste(ed, before, count * scale);
6128        }
6129        LastChange::DeleteToEol { inserted } => {
6130            use hjkl_buffer::{Edit, Position};
6131            ed.push_undo();
6132            delete_to_eol(ed);
6133            if let Some(text) = inserted {
6134                let cursor = ed.cursor();
6135                ed.mutate_edit(Edit::InsertStr {
6136                    at: Position::new(cursor.0, cursor.1),
6137                    text,
6138                });
6139            }
6140        }
6141        LastChange::OpenLine { above, inserted } => {
6142            use hjkl_buffer::{Edit, Position};
6143            ed.push_undo();
6144            ed.sync_buffer_content_from_textarea();
6145            let row = buf_cursor_pos(&ed.buffer).row;
6146            if above {
6147                ed.mutate_edit(Edit::InsertStr {
6148                    at: Position::new(row, 0),
6149                    text: "\n".to_string(),
6150                });
6151                let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
6152                crate::motions::move_up(&mut ed.buffer, &folds, 1, &mut ed.sticky_col);
6153            } else {
6154                let line_chars = buf_line_chars(&ed.buffer, row);
6155                ed.mutate_edit(Edit::InsertStr {
6156                    at: Position::new(row, line_chars),
6157                    text: "\n".to_string(),
6158                });
6159            }
6160            ed.push_buffer_cursor_to_textarea();
6161            let cursor = ed.cursor();
6162            ed.mutate_edit(Edit::InsertStr {
6163                at: Position::new(cursor.0, cursor.1),
6164                text: inserted,
6165            });
6166        }
6167        LastChange::InsertAt {
6168            entry,
6169            inserted,
6170            count,
6171        } => {
6172            use hjkl_buffer::{Edit, Position};
6173            ed.push_undo();
6174            match entry {
6175                InsertEntry::I => {}
6176                InsertEntry::ShiftI => move_first_non_whitespace(ed),
6177                InsertEntry::A => {
6178                    crate::motions::move_right_to_end(&mut ed.buffer, 1);
6179                    ed.push_buffer_cursor_to_textarea();
6180                }
6181                InsertEntry::ShiftA => {
6182                    crate::motions::move_line_end(&mut ed.buffer);
6183                    crate::motions::move_right_to_end(&mut ed.buffer, 1);
6184                    ed.push_buffer_cursor_to_textarea();
6185                }
6186            }
6187            for _ in 0..count.max(1) {
6188                let cursor = ed.cursor();
6189                ed.mutate_edit(Edit::InsertStr {
6190                    at: Position::new(cursor.0, cursor.1),
6191                    text: inserted.clone(),
6192                });
6193            }
6194        }
6195    }
6196    ed.vim.replaying = false;
6197}
6198
6199// ─── Extracting inserted text for replay ───────────────────────────────────
6200
6201fn extract_inserted(before: &str, after: &str) -> String {
6202    let before_chars: Vec<char> = before.chars().collect();
6203    let after_chars: Vec<char> = after.chars().collect();
6204    if after_chars.len() <= before_chars.len() {
6205        return String::new();
6206    }
6207    let prefix = before_chars
6208        .iter()
6209        .zip(after_chars.iter())
6210        .take_while(|(a, b)| a == b)
6211        .count();
6212    let max_suffix = before_chars.len() - prefix;
6213    let suffix = before_chars
6214        .iter()
6215        .rev()
6216        .zip(after_chars.iter().rev())
6217        .take(max_suffix)
6218        .take_while(|(a, b)| a == b)
6219        .count();
6220    after_chars[prefix..after_chars.len() - suffix]
6221        .iter()
6222        .collect()
6223}
6224
6225// ─── Tests ────────────────────────────────────────────────────────────────