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}
187
188#[derive(Debug, Clone, PartialEq, Eq)]
189pub enum Motion {
190    Left,
191    Right,
192    Up,
193    Down,
194    WordFwd,
195    BigWordFwd,
196    WordBack,
197    BigWordBack,
198    WordEnd,
199    BigWordEnd,
200    /// `ge` — backward word end.
201    WordEndBack,
202    /// `gE` — backward WORD end.
203    BigWordEndBack,
204    LineStart,
205    FirstNonBlank,
206    LineEnd,
207    FileTop,
208    FileBottom,
209    Find {
210        ch: char,
211        forward: bool,
212        till: bool,
213    },
214    FindRepeat {
215        reverse: bool,
216    },
217    MatchBracket,
218    WordAtCursor {
219        forward: bool,
220        /// `*` / `#` use `\bword\b` boundaries; `g*` / `g#` drop them so
221        /// the search hits substrings (e.g. `foo` matches inside `foobar`).
222        whole_word: bool,
223    },
224    /// `n` / `N` — repeat the last `/` or `?` search.
225    SearchNext {
226        reverse: bool,
227    },
228    /// `H` — cursor to viewport top (plus `count - 1` rows down).
229    ViewportTop,
230    /// `M` — cursor to viewport middle.
231    ViewportMiddle,
232    /// `L` — cursor to viewport bottom (minus `count - 1` rows up).
233    ViewportBottom,
234    /// `g_` — last non-blank char on the line.
235    LastNonBlank,
236    /// `gM` — cursor to the middle char column of the current line
237    /// (`floor(chars / 2)`). Vim's variant ignoring screen wrap.
238    LineMiddle,
239    /// `{` — previous paragraph (preceding blank line, or top).
240    ParagraphPrev,
241    /// `}` — next paragraph (following blank line, or bottom).
242    ParagraphNext,
243    /// `(` — previous sentence boundary.
244    SentencePrev,
245    /// `)` — next sentence boundary.
246    SentenceNext,
247    /// `gj` — `count` visual rows down (one screen segment per step
248    /// under `:set wrap`; falls back to `Down` otherwise).
249    ScreenDown,
250    /// `gk` — `count` visual rows up; mirror of [`Motion::ScreenDown`].
251    ScreenUp,
252}
253
254#[derive(Debug, Clone, Copy, PartialEq, Eq)]
255pub enum TextObject {
256    Word {
257        big: bool,
258    },
259    Quote(char),
260    Bracket(char),
261    Paragraph,
262    /// `it` / `at` — XML/HTML-style tag pair. `inner = true` covers
263    /// content between `>` and `</`; `inner = false` covers the open
264    /// tag through the close tag inclusive.
265    XmlTag,
266    /// `is` / `as` — sentence: a run ending at `.`, `?`, or `!`
267    /// followed by whitespace or end-of-line. `inner = true` covers
268    /// the sentence text only; `inner = false` includes trailing
269    /// whitespace.
270    Sentence,
271}
272
273/// Classification determines how operators treat the range end.
274#[derive(Debug, Clone, Copy, PartialEq, Eq)]
275pub enum RangeKind {
276    /// Range end is exclusive (end column not included). Typical: h, l, w, 0, $.
277    Exclusive,
278    /// Range end is inclusive. Typical: e, f, t, %.
279    Inclusive,
280    /// Whole lines from top row to bottom row. Typical: j, k, gg, G.
281    Linewise,
282}
283
284// ─── Dot-repeat storage ────────────────────────────────────────────────────
285
286/// Information needed to replay a mutating change via `.`.
287#[derive(Debug, Clone)]
288pub enum LastChange {
289    /// Operator over a motion.
290    OpMotion {
291        op: Operator,
292        motion: Motion,
293        count: usize,
294        inserted: Option<String>,
295    },
296    /// Operator over a text-object.
297    OpTextObj {
298        op: Operator,
299        obj: TextObject,
300        inner: bool,
301        inserted: Option<String>,
302    },
303    /// `dd`, `cc`, `yy` with a count.
304    LineOp {
305        op: Operator,
306        count: usize,
307        inserted: Option<String>,
308    },
309    /// `x`, `X` with a count.
310    CharDel { forward: bool, count: usize },
311    /// `r<ch>` with a count.
312    ReplaceChar { ch: char, count: usize },
313    /// `~` with a count.
314    ToggleCase { count: usize },
315    /// `J` with a count.
316    JoinLine { count: usize },
317    /// `p` / `P` with a count.
318    Paste { before: bool, count: usize },
319    /// `D` (delete to EOL).
320    DeleteToEol { inserted: Option<String> },
321    /// `o` / `O` + the inserted text.
322    OpenLine { above: bool, inserted: String },
323    /// `i`/`I`/`a`/`A` + inserted text.
324    InsertAt {
325        entry: InsertEntry,
326        inserted: String,
327        count: usize,
328    },
329}
330
331#[derive(Debug, Clone, Copy, PartialEq, Eq)]
332pub enum InsertEntry {
333    I,
334    A,
335    ShiftI,
336    ShiftA,
337}
338
339// ─── VimState ──────────────────────────────────────────────────────────────
340
341#[derive(Default)]
342pub struct VimState {
343    /// Internal FSM mode. Kept in sync with `current_mode` after every
344    /// `step`. Phase 6.6b: promoted from private to `pub` so the FSM
345    /// body (moving to hjkl-vim in 6.6c–6.6g) can read/write it directly
346    /// until the migration is complete.
347    pub mode: Mode,
348    /// Two-key chord in progress. `Pending::None` when idle.
349    pub pending: Pending,
350    /// Digit prefix accumulated before an operator or motion. `0` means
351    /// no prefix was typed (treated as 1 by most commands).
352    pub count: usize,
353    /// Last `f`/`F`/`t`/`T` target, for `;` / `,` repeat.
354    pub last_find: Option<(char, bool, bool)>,
355    /// Most-recent mutating command for `.` dot-repeat.
356    pub last_change: Option<LastChange>,
357    /// Captured on insert-mode entry: count, buffer snapshot, entry kind.
358    pub insert_session: Option<InsertSession>,
359    /// (row, col) anchor for char-wise Visual mode. Set on entry, used
360    /// to compute the highlight range and the operator range without
361    /// relying on tui-textarea's live selection.
362    pub visual_anchor: (usize, usize),
363    /// Row anchor for VisualLine mode.
364    pub visual_line_anchor: usize,
365    /// (row, col) anchor for VisualBlock mode. The live cursor is the
366    /// opposite corner.
367    pub block_anchor: (usize, usize),
368    /// Intended "virtual" column for the block's active corner. j/k
369    /// clamp cursor.col to shorter rows, which would collapse the
370    /// block across ragged content — so we remember the desired column
371    /// separately and use it for block bounds / insert-column
372    /// computations. Updated by h/l only.
373    pub block_vcol: usize,
374    /// Track whether the last yank/cut was linewise (drives `p`/`P` layout).
375    pub yank_linewise: bool,
376    /// Active register selector — set by `"reg` prefix, consumed by
377    /// the next y / d / c / p. `None` falls back to the unnamed `"`.
378    pub pending_register: Option<char>,
379    /// Recording target — set by `q{reg}`, cleared by a bare `q`.
380    /// While `Some`, every consumed `Input` is appended to
381    /// `recording_keys`.
382    pub recording_macro: Option<char>,
383    /// Keys recorded into the in-progress macro. On `q` finish, these
384    /// are encoded via [`crate::input::encode_macro`] and written to
385    /// the matching named register slot, so macros and yanks share a
386    /// single store.
387    pub recording_keys: Vec<crate::input::Input>,
388    /// Set during `@reg` replay so the recorder doesn't capture the
389    /// replayed keystrokes a second time.
390    pub replaying_macro: bool,
391    /// Last register played via `@reg`. `@@` re-plays this one.
392    pub last_macro: Option<char>,
393    /// Position of the most recent buffer mutation. Surfaced via
394    /// the `'.` / `` `. `` marks for quick "back to last edit".
395    pub last_edit_pos: Option<(usize, usize)>,
396    /// Position where the cursor was when insert mode last exited (Esc).
397    /// Used by `gi` to return to the exact (row, col) where the user
398    /// last typed, matching vim's `:h gi`.
399    pub last_insert_pos: Option<(usize, usize)>,
400    /// Bounded ring of recent edit positions (newest at the back).
401    /// `g;` walks toward older entries, `g,` toward newer ones. Capped
402    /// at [`CHANGE_LIST_MAX`].
403    pub change_list: Vec<(usize, usize)>,
404    /// Index into `change_list` while walking. `None` outside a walk —
405    /// any new edit clears it (and trims forward entries past it).
406    pub change_list_cursor: Option<usize>,
407    /// Snapshot of the last visual selection for `gv` re-entry.
408    /// Stored on every Visual / VisualLine / VisualBlock exit.
409    pub last_visual: Option<LastVisual>,
410    /// `zz` / `zt` / `zb` set this so the end-of-step scrolloff
411    /// pass doesn't override the user's explicit viewport pinning.
412    /// Cleared every step.
413    pub viewport_pinned: bool,
414    /// Set while replaying `.` / last-change so we don't re-record it.
415    pub replaying: bool,
416    /// Entered Normal from Insert via `Ctrl-o`; after the next complete
417    /// normal-mode command we return to Insert.
418    pub one_shot_normal: bool,
419    /// Live `/` or `?` prompt. `None` outside search-prompt mode.
420    pub search_prompt: Option<SearchPrompt>,
421    /// Most recent committed search pattern. Surfaced to host apps via
422    /// [`Editor::last_search`] so their status line can render a hint
423    /// and so `n` / `N` have something to repeat.
424    pub last_search: Option<String>,
425    /// Direction of the last committed search. `n` repeats this; `N`
426    /// inverts it. Defaults to forward so a never-searched buffer's
427    /// `n` still walks downward.
428    pub last_search_forward: bool,
429    /// Back half of the jumplist — `Ctrl-o` pops from here. Populated
430    /// with the pre-motion cursor when a "big jump" motion fires
431    /// (`gg`/`G`, `%`, `*`/`#`, `n`/`N`, `H`/`M`/`L`, committed `/` or
432    /// `?`). Capped at 100 entries.
433    pub jump_back: Vec<(usize, usize)>,
434    /// Forward half — `Ctrl-i` pops from here. Cleared by any new big
435    /// jump, matching vim's "branch off trims forward history" rule.
436    pub jump_fwd: Vec<(usize, usize)>,
437    /// Set by `Ctrl-R` in insert mode while waiting for the register
438    /// selector. The next typed char names the register; its contents
439    /// are inserted inline at the cursor and the flag clears.
440    pub insert_pending_register: bool,
441    /// Stashed start position for the `[` mark on a Change operation.
442    /// Set to `top` before the cut in `run_operator_over_range` (Change
443    /// arm); consumed by `finish_insert_session` on Esc-from-insert
444    /// when the reason is `AfterChange`. Mirrors vim's `:h '[` / `:h ']`
445    /// rule that `[` = start of change, `]` = last typed char on exit.
446    pub change_mark_start: Option<(usize, usize)>,
447    /// Bounded history of committed `/` / `?` search patterns. Newest
448    /// entries are at the back; capped at [`SEARCH_HISTORY_MAX`] to
449    /// avoid unbounded growth on long sessions.
450    pub search_history: Vec<String>,
451    /// Index into `search_history` while the user walks past patterns
452    /// in the prompt via `Ctrl-P` / `Ctrl-N`. `None` outside that walk
453    /// — typing or backspacing in the prompt resets it so the next
454    /// `Ctrl-P` starts from the most recent entry again.
455    pub search_history_cursor: Option<usize>,
456    /// Wall-clock instant of the last keystroke. Drives the
457    /// `:set timeoutlen` multi-key timeout — if `now() - last_input_at`
458    /// exceeds the configured budget, any pending prefix is cleared
459    /// before the new key dispatches. `None` before the first key.
460    /// 0.0.29 (Patch B): `:set timeoutlen` math now reads
461    /// [`crate::types::Host::now`] via `last_input_host_at`. This
462    /// `Instant`-flavoured field stays for snapshot tests that still
463    /// observe it directly.
464    pub last_input_at: Option<std::time::Instant>,
465    /// `Host::now()` reading at the last keystroke. Drives
466    /// `:set timeoutlen` so macro replay / headless drivers stay
467    /// deterministic regardless of wall-clock skew.
468    pub last_input_host_at: Option<core::time::Duration>,
469    /// Canonical current mode. Mirrors `mode` (the FSM-internal field)
470    /// AND is written by every Phase 6.3 primitive (`set_mode`,
471    /// `enter_visual_char_bridge`, …). Once the FSM is gone this is the
472    /// sole source of truth; until then both fields are kept in sync.
473    /// Initialized to `Normal` via `#[derive(Default)]`.
474    pub(crate) current_mode: crate::VimMode,
475}
476
477pub(crate) const SEARCH_HISTORY_MAX: usize = 100;
478pub(crate) const CHANGE_LIST_MAX: usize = 100;
479
480/// Active `/` or `?` search prompt. Text mutations drive the textarea's
481/// live search pattern so matches highlight as the user types.
482#[derive(Debug, Clone)]
483pub struct SearchPrompt {
484    pub text: String,
485    pub cursor: usize,
486    pub forward: bool,
487}
488
489#[derive(Debug, Clone)]
490pub struct InsertSession {
491    pub count: usize,
492    /// Min/max row visited during this session. Widens on every key.
493    pub row_min: usize,
494    pub row_max: usize,
495    /// Snapshot of the full buffer at session entry. Used to diff the
496    /// affected row window at finish without being fooled by cursor
497    /// navigation through rows the user never edited.
498    pub before_lines: Vec<String>,
499    pub reason: InsertReason,
500}
501
502#[derive(Debug, Clone)]
503pub enum InsertReason {
504    /// Plain entry via i/I/a/A — recorded as `InsertAt`.
505    Enter(InsertEntry),
506    /// Entry via `o`/`O` — records OpenLine on Esc.
507    Open { above: bool },
508    /// Entry via an operator's change side-effect. Retro-fills the
509    /// stored last-change's `inserted` field on Esc.
510    AfterChange,
511    /// Entry via `C` (delete to EOL + insert).
512    DeleteToEol,
513    /// Entry via an insert triggered during dot-replay — don't touch
514    /// last_change because the outer replay will restore it.
515    ReplayOnly,
516    /// `I` or `A` from VisualBlock: insert the typed text at `col` on
517    /// every row in `top..=bot`. `col` is the start column for `I`, the
518    /// one-past-block-end column for `A`.
519    BlockEdge { top: usize, bot: usize, col: usize },
520    /// `c` from VisualBlock: block content deleted, then user types
521    /// replacement text replicated across all block rows on Esc. Cursor
522    /// advances to the last typed char after replication (unlike BlockEdge
523    /// which leaves cursor at the insertion column).
524    BlockChange { top: usize, bot: usize, col: usize },
525    /// `R` — Replace mode. Each typed char overwrites the cell under
526    /// the cursor instead of inserting; at end-of-line the session
527    /// falls through to insert (same as vim).
528    Replace,
529}
530
531/// Saved visual-mode anchor + cursor for `gv` (re-enters the last
532/// visual selection). `mode` carries which visual flavour to
533/// restore; `anchor` / `cursor` mean different things per flavour:
534///
535/// - `Visual`     — `anchor` is the char-wise visual anchor.
536/// - `VisualLine` — `anchor.0` is the `visual_line_anchor` row;
537///   `anchor.1` is unused.
538/// - `VisualBlock`— `anchor` is `block_anchor`, `block_vcol` is the
539///   sticky vcol that survives j/k clamping.
540#[derive(Debug, Clone, Copy)]
541pub struct LastVisual {
542    pub mode: Mode,
543    pub anchor: (usize, usize),
544    pub cursor: (usize, usize),
545    pub block_vcol: usize,
546}
547
548impl VimState {
549    pub fn public_mode(&self) -> VimMode {
550        match self.mode {
551            Mode::Normal => VimMode::Normal,
552            Mode::Insert => VimMode::Insert,
553            Mode::Visual => VimMode::Visual,
554            Mode::VisualLine => VimMode::VisualLine,
555            Mode::VisualBlock => VimMode::VisualBlock,
556        }
557    }
558
559    pub fn force_normal(&mut self) {
560        self.mode = Mode::Normal;
561        self.pending = Pending::None;
562        self.count = 0;
563        self.insert_session = None;
564        // Phase 6.3: keep current_mode in sync for callers that bypass step().
565        self.current_mode = crate::VimMode::Normal;
566    }
567
568    /// Reset every prefix-tracking field so the next keystroke starts
569    /// a fresh sequence. Drives `:set timeoutlen` — when the user
570    /// pauses past the configured budget, `hjkl_vim::dispatch_input` calls
571    /// this before dispatching the new key.
572    ///
573    /// Resets: `pending`, `count`, `pending_register`,
574    /// `insert_pending_register`. Does NOT touch `mode`,
575    /// `insert_session`, marks, jump list, or visual anchors —
576    /// those aren't part of the in-flight chord.
577    pub(crate) fn clear_pending_prefix(&mut self) {
578        self.pending = Pending::None;
579        self.count = 0;
580        self.pending_register = None;
581        self.insert_pending_register = false;
582    }
583
584    /// Widen the active insert session's row window to include `row`. Called
585    /// by the Phase 6.1 public `Editor::insert_*` methods after each
586    /// mutation so `finish_insert_session` diffs the right range on Esc.
587    /// No-op when no insert session is active (e.g. calling from Normal mode).
588    pub(crate) fn widen_insert_row(&mut self, row: usize) {
589        if let Some(ref mut session) = self.insert_session {
590            session.row_min = session.row_min.min(row);
591            session.row_max = session.row_max.max(row);
592        }
593    }
594
595    pub fn is_visual(&self) -> bool {
596        matches!(
597            self.mode,
598            Mode::Visual | Mode::VisualLine | Mode::VisualBlock
599        )
600    }
601
602    pub fn is_visual_char(&self) -> bool {
603        self.mode == Mode::Visual
604    }
605
606    pub fn enter_visual(&mut self, anchor: (usize, usize)) {
607        self.visual_anchor = anchor;
608        self.mode = Mode::Visual;
609    }
610
611    /// The pending repeat count (typed digits before a motion/operator),
612    /// or `None` when no digits are pending. Zero is treated as absent.
613    pub(crate) fn pending_count_val(&self) -> Option<u32> {
614        if self.count == 0 {
615            None
616        } else {
617            Some(self.count as u32)
618        }
619    }
620
621    /// `true` when an in-flight chord is awaiting more keys. Inverse of
622    /// `matches!(self.pending, Pending::None)`.
623    pub(crate) fn is_chord_pending(&self) -> bool {
624        !matches!(self.pending, Pending::None)
625    }
626
627    /// Return a single char representing the pending operator, if any.
628    /// Used by host apps (status line "showcmd" area) to display e.g.
629    /// `d`, `y`, `c` while waiting for a motion.
630    pub(crate) fn pending_op_char(&self) -> Option<char> {
631        let op = match &self.pending {
632            Pending::Op { op, .. }
633            | Pending::OpTextObj { op, .. }
634            | Pending::OpG { op, .. }
635            | Pending::OpFind { op, .. } => Some(*op),
636            _ => None,
637        };
638        op.map(|o| match o {
639            Operator::Delete => 'd',
640            Operator::Change => 'c',
641            Operator::Yank => 'y',
642            Operator::Uppercase => 'U',
643            Operator::Lowercase => 'u',
644            Operator::ToggleCase => '~',
645            Operator::Indent => '>',
646            Operator::Outdent => '<',
647            Operator::Fold => 'z',
648            Operator::Reflow => 'q',
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
2637fn 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    }
3558}
3559
3560// ─── Phase 4a pub range-mutation bridges ───────────────────────────────────
3561//
3562// These are `pub(crate)` entry points called by the five new pub methods on
3563// `Editor` (`delete_range`, `yank_range`, `change_range`, `indent_range`,
3564// `case_range`). They set `pending_register` from the caller-supplied char
3565// before delegating to the existing internal helpers so register semantics
3566// (unnamed `"`, named `"a`–`"z`, delete ring) are honoured exactly as in the
3567// FSM path.
3568//
3569// Do NOT call `run_operator_over_range` for Indent/Outdent or the three case
3570// operators — those share the FSM path but have dedicated parameter shapes
3571// (signed count, Operator-as-CaseOp) that map more cleanly to their own
3572// helpers.
3573
3574/// Delete the range `[start, end)` (interpretation determined by `kind`) and
3575/// stash the deleted text in `register`. `'"'` is the unnamed register.
3576pub(crate) fn delete_range_bridge<H: crate::types::Host>(
3577    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3578    start: (usize, usize),
3579    end: (usize, usize),
3580    kind: RangeKind,
3581    register: char,
3582) {
3583    ed.vim.pending_register = Some(register);
3584    run_operator_over_range(ed, Operator::Delete, start, end, kind);
3585}
3586
3587/// Yank (copy) the range `[start, end)` into `register` without mutating the
3588/// buffer. `'"'` is the unnamed register.
3589pub(crate) fn yank_range_bridge<H: crate::types::Host>(
3590    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3591    start: (usize, usize),
3592    end: (usize, usize),
3593    kind: RangeKind,
3594    register: char,
3595) {
3596    ed.vim.pending_register = Some(register);
3597    run_operator_over_range(ed, Operator::Yank, start, end, kind);
3598}
3599
3600/// Delete the range `[start, end)` and enter Insert mode (vim `c` operator).
3601/// The deleted text is stashed in `register`. Mode transitions to Insert on
3602/// return; the caller must not issue further normal-mode ops until the insert
3603/// session ends.
3604pub(crate) fn change_range_bridge<H: crate::types::Host>(
3605    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3606    start: (usize, usize),
3607    end: (usize, usize),
3608    kind: RangeKind,
3609    register: char,
3610) {
3611    ed.vim.pending_register = Some(register);
3612    run_operator_over_range(ed, Operator::Change, start, end, kind);
3613}
3614
3615/// Indent (`count > 0`) or outdent (`count < 0`) the row span `[start.0,
3616/// end.0]`. `shiftwidth` overrides the editor's `settings().shiftwidth` for
3617/// this call; pass `0` to use the editor setting. The column parts of `start`
3618/// / `end` are ignored — indent is always linewise.
3619pub(crate) fn indent_range_bridge<H: crate::types::Host>(
3620    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3621    start: (usize, usize),
3622    end: (usize, usize),
3623    count: i32,
3624    shiftwidth: u32,
3625) {
3626    if count == 0 {
3627        return;
3628    }
3629    let (top_row, bot_row) = if start.0 <= end.0 {
3630        (start.0, end.0)
3631    } else {
3632        (end.0, start.0)
3633    };
3634    // Temporarily override shiftwidth when the caller provides one.
3635    let original_sw = ed.settings().shiftwidth;
3636    if shiftwidth > 0 {
3637        ed.settings_mut().shiftwidth = shiftwidth as usize;
3638    }
3639    ed.push_undo();
3640    let abs_count = count.unsigned_abs() as usize;
3641    if count > 0 {
3642        indent_rows(ed, top_row, bot_row, abs_count);
3643    } else {
3644        outdent_rows(ed, top_row, bot_row, abs_count);
3645    }
3646    if shiftwidth > 0 {
3647        ed.settings_mut().shiftwidth = original_sw;
3648    }
3649    ed.vim.mode = Mode::Normal;
3650}
3651
3652/// Apply a case transformation (`Uppercase` / `Lowercase` / `ToggleCase`) to
3653/// the range `[start, end)`. Only the three case `Operator` variants are valid;
3654/// other variants are silently ignored (no-op).
3655pub(crate) fn case_range_bridge<H: crate::types::Host>(
3656    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3657    start: (usize, usize),
3658    end: (usize, usize),
3659    kind: RangeKind,
3660    op: Operator,
3661) {
3662    match op {
3663        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {}
3664        _ => return,
3665    }
3666    let (top, bot) = order(start, end);
3667    apply_case_op_to_selection(ed, op, top, bot, kind);
3668}
3669
3670// ─── Phase 4e pub block-shape range-mutation bridges ───────────────────────
3671//
3672// These are `pub(crate)` entry points called by the four new pub methods on
3673// `Editor` (`delete_block`, `yank_block`, `change_block`, `indent_block`).
3674// They set `pending_register` from the caller-supplied char then delegate to
3675// `apply_block_operator` (after temporarily installing the 4-corner block as
3676// the engine's virtual VisualBlock selection). The editor's VisualBlock state
3677// fields (`block_anchor`, `block_vcol`) are overwritten, the op fires, then
3678// the fields are restored to their pre-call values. This ensures the engine's
3679// register / undo / mode semantics are exercised without requiring the caller
3680// to already be in VisualBlock mode.
3681//
3682// `indent_block` is a separate helper — it does not use `apply_block_operator`
3683// because indent/outdent are always linewise for blocks (vim behaviour).
3684
3685/// Delete a rectangular VisualBlock selection. `top_row`/`bot_row` are
3686/// inclusive line bounds; `left_col`/`right_col` are inclusive char-column
3687/// bounds. Short lines that don't reach `right_col` lose only the chars
3688/// that exist (ragged-edge, matching engine FSM). `register` is honoured;
3689/// `'"'` selects the unnamed register.
3690pub(crate) fn delete_block_bridge<H: crate::types::Host>(
3691    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3692    top_row: usize,
3693    bot_row: usize,
3694    left_col: usize,
3695    right_col: usize,
3696    register: char,
3697) {
3698    ed.vim.pending_register = Some(register);
3699    let saved_anchor = ed.vim.block_anchor;
3700    let saved_vcol = ed.vim.block_vcol;
3701    ed.vim.block_anchor = (top_row, left_col);
3702    ed.vim.block_vcol = right_col;
3703    // Compute clamped col before the mutable borrow for buf_set_cursor_rc.
3704    let clamped = right_col.min(buf_line_chars(&ed.buffer, bot_row).saturating_sub(1));
3705    // Place cursor at bot_row / right_col so block_bounds resolves correctly.
3706    buf_set_cursor_rc(&mut ed.buffer, bot_row, clamped);
3707    apply_block_operator(ed, Operator::Delete);
3708    // Restore — block_anchor/vcol are only meaningful in VisualBlock mode;
3709    // after the op we're in Normal so restoring is a no-op for the user but
3710    // keeps state coherent if the caller inspects fields.
3711    ed.vim.block_anchor = saved_anchor;
3712    ed.vim.block_vcol = saved_vcol;
3713}
3714
3715/// Yank a rectangular VisualBlock selection into `register`.
3716pub(crate) fn yank_block_bridge<H: crate::types::Host>(
3717    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3718    top_row: usize,
3719    bot_row: usize,
3720    left_col: usize,
3721    right_col: usize,
3722    register: char,
3723) {
3724    ed.vim.pending_register = Some(register);
3725    let saved_anchor = ed.vim.block_anchor;
3726    let saved_vcol = ed.vim.block_vcol;
3727    ed.vim.block_anchor = (top_row, left_col);
3728    ed.vim.block_vcol = right_col;
3729    let clamped = right_col.min(buf_line_chars(&ed.buffer, bot_row).saturating_sub(1));
3730    buf_set_cursor_rc(&mut ed.buffer, bot_row, clamped);
3731    apply_block_operator(ed, Operator::Yank);
3732    ed.vim.block_anchor = saved_anchor;
3733    ed.vim.block_vcol = saved_vcol;
3734}
3735
3736/// Delete a rectangular VisualBlock selection and enter Insert mode (`c`).
3737/// The deleted text is stashed in `register`. Mode is Insert on return.
3738pub(crate) fn change_block_bridge<H: crate::types::Host>(
3739    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3740    top_row: usize,
3741    bot_row: usize,
3742    left_col: usize,
3743    right_col: usize,
3744    register: char,
3745) {
3746    ed.vim.pending_register = Some(register);
3747    let saved_anchor = ed.vim.block_anchor;
3748    let saved_vcol = ed.vim.block_vcol;
3749    ed.vim.block_anchor = (top_row, left_col);
3750    ed.vim.block_vcol = right_col;
3751    let clamped = right_col.min(buf_line_chars(&ed.buffer, bot_row).saturating_sub(1));
3752    buf_set_cursor_rc(&mut ed.buffer, bot_row, clamped);
3753    apply_block_operator(ed, Operator::Change);
3754    ed.vim.block_anchor = saved_anchor;
3755    ed.vim.block_vcol = saved_vcol;
3756}
3757
3758/// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3759/// Column bounds are ignored — vim's block indent is always linewise.
3760/// `count == 0` is a no-op.
3761pub(crate) fn indent_block_bridge<H: crate::types::Host>(
3762    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3763    top_row: usize,
3764    bot_row: usize,
3765    count: i32,
3766) {
3767    if count == 0 {
3768        return;
3769    }
3770    ed.push_undo();
3771    let abs = count.unsigned_abs() as usize;
3772    if count > 0 {
3773        indent_rows(ed, top_row, bot_row, abs);
3774    } else {
3775        outdent_rows(ed, top_row, bot_row, abs);
3776    }
3777    ed.vim.mode = Mode::Normal;
3778}
3779
3780// ─── Phase 4b pub text-object resolution bridges ───────────────────────────
3781//
3782// These are `pub(crate)` entry points called by the four new pub methods on
3783// `Editor` (`text_object_inner_word`, `text_object_around_word`,
3784// `text_object_inner_big_word`, `text_object_around_big_word`). They delegate
3785// to `word_text_object` — the existing private resolver — without touching any
3786// operator, register, or mode state. Pure functions: only `&Editor` required.
3787
3788/// Resolve the range of `iw` (inner word) at the current cursor position.
3789/// Returns `None` if no word exists at the cursor.
3790pub(crate) fn text_object_inner_word_bridge<H: crate::types::Host>(
3791    ed: &Editor<hjkl_buffer::Buffer, H>,
3792) -> Option<((usize, usize), (usize, usize))> {
3793    word_text_object(ed, true, false)
3794}
3795
3796/// Resolve the range of `aw` (around word) at the current cursor position.
3797/// Includes trailing whitespace (or leading whitespace if no trailing exists).
3798pub(crate) fn text_object_around_word_bridge<H: crate::types::Host>(
3799    ed: &Editor<hjkl_buffer::Buffer, H>,
3800) -> Option<((usize, usize), (usize, usize))> {
3801    word_text_object(ed, false, false)
3802}
3803
3804/// Resolve the range of `iW` (inner WORD) at the current cursor position.
3805/// A WORD is any run of non-whitespace characters (no punctuation splitting).
3806pub(crate) fn text_object_inner_big_word_bridge<H: crate::types::Host>(
3807    ed: &Editor<hjkl_buffer::Buffer, H>,
3808) -> Option<((usize, usize), (usize, usize))> {
3809    word_text_object(ed, true, true)
3810}
3811
3812/// Resolve the range of `aW` (around WORD) at the current cursor position.
3813/// Includes trailing whitespace (or leading whitespace if no trailing exists).
3814pub(crate) fn text_object_around_big_word_bridge<H: crate::types::Host>(
3815    ed: &Editor<hjkl_buffer::Buffer, H>,
3816) -> Option<((usize, usize), (usize, usize))> {
3817    word_text_object(ed, false, true)
3818}
3819
3820// ─── Phase 4c pub text-object resolution bridges (quote + bracket) ──────────
3821//
3822// `pub(crate)` entry points called by the four new pub methods on `Editor`
3823// (`text_object_inner_quote`, `text_object_around_quote`,
3824// `text_object_inner_bracket`, `text_object_around_bracket`). They delegate to
3825// `quote_text_object` / `bracket_text_object` — the existing private resolvers
3826// — without touching any operator, register, or mode state.
3827//
3828// `bracket_text_object` returns `Option<(Pos, Pos, RangeKind)>`; the bridges
3829// strip the `RangeKind` tag so callers see a uniform
3830// `Option<((usize,usize),(usize,usize))>` shape, consistent with 4b.
3831
3832/// Resolve the range of `i<quote>` (inner quote) at the current cursor
3833/// position. `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None`
3834/// when the cursor's line contains fewer than two occurrences of `quote`.
3835pub(crate) fn text_object_inner_quote_bridge<H: crate::types::Host>(
3836    ed: &Editor<hjkl_buffer::Buffer, H>,
3837    quote: char,
3838) -> Option<((usize, usize), (usize, usize))> {
3839    quote_text_object(ed, quote, true)
3840}
3841
3842/// Resolve the range of `a<quote>` (around quote) at the current cursor
3843/// position. Includes surrounding whitespace on one side per vim semantics.
3844pub(crate) fn text_object_around_quote_bridge<H: crate::types::Host>(
3845    ed: &Editor<hjkl_buffer::Buffer, H>,
3846    quote: char,
3847) -> Option<((usize, usize), (usize, usize))> {
3848    quote_text_object(ed, quote, false)
3849}
3850
3851/// Resolve the range of `i<bracket>` (inner bracket pair). `open` must be
3852/// one of `'('`, `'{'`, `'['`, `'<'`; the corresponding close is derived
3853/// internally. Returns `None` when no enclosing pair is found. The returned
3854/// range excludes the bracket characters themselves. Multi-line bracket pairs
3855/// whose content spans more than one line are reported as a charwise range
3856/// covering the first content character through the last content character
3857/// (RangeKind metadata is stripped — callers receive start/end only).
3858pub(crate) fn text_object_inner_bracket_bridge<H: crate::types::Host>(
3859    ed: &Editor<hjkl_buffer::Buffer, H>,
3860    open: char,
3861) -> Option<((usize, usize), (usize, usize))> {
3862    bracket_text_object(ed, open, true).map(|(s, e, _kind)| (s, e))
3863}
3864
3865/// Resolve the range of `a<bracket>` (around bracket pair). Includes the
3866/// bracket characters themselves. `open` must be one of `'('`, `'{'`, `'['`,
3867/// `'<'`.
3868pub(crate) fn text_object_around_bracket_bridge<H: crate::types::Host>(
3869    ed: &Editor<hjkl_buffer::Buffer, H>,
3870    open: char,
3871) -> Option<((usize, usize), (usize, usize))> {
3872    bracket_text_object(ed, open, false).map(|(s, e, _kind)| (s, e))
3873}
3874
3875// ── Sentence bridges (is / as) ─────────────────────────────────────────────
3876
3877/// Resolve the range of `is` (inner sentence) at the cursor. Excludes
3878/// trailing whitespace.
3879pub(crate) fn text_object_inner_sentence_bridge<H: crate::types::Host>(
3880    ed: &Editor<hjkl_buffer::Buffer, H>,
3881) -> Option<((usize, usize), (usize, usize))> {
3882    sentence_text_object(ed, true)
3883}
3884
3885/// Resolve the range of `as` (around sentence) at the cursor. Includes
3886/// trailing whitespace.
3887pub(crate) fn text_object_around_sentence_bridge<H: crate::types::Host>(
3888    ed: &Editor<hjkl_buffer::Buffer, H>,
3889) -> Option<((usize, usize), (usize, usize))> {
3890    sentence_text_object(ed, false)
3891}
3892
3893// ── Paragraph bridges (ip / ap) ────────────────────────────────────────────
3894
3895/// Resolve the range of `ip` (inner paragraph) at the cursor. A paragraph
3896/// is a block of non-blank lines bounded by blank lines or buffer edges.
3897pub(crate) fn text_object_inner_paragraph_bridge<H: crate::types::Host>(
3898    ed: &Editor<hjkl_buffer::Buffer, H>,
3899) -> Option<((usize, usize), (usize, usize))> {
3900    paragraph_text_object(ed, true)
3901}
3902
3903/// Resolve the range of `ap` (around paragraph) at the cursor. Includes one
3904/// trailing blank line when present.
3905pub(crate) fn text_object_around_paragraph_bridge<H: crate::types::Host>(
3906    ed: &Editor<hjkl_buffer::Buffer, H>,
3907) -> Option<((usize, usize), (usize, usize))> {
3908    paragraph_text_object(ed, false)
3909}
3910
3911// ── Tag bridges (it / at) ──────────────────────────────────────────────────
3912
3913/// Resolve the range of `it` (inner tag) at the cursor. Matches XML/HTML-style
3914/// `<tag>...</tag>` pairs; returns the range of inner content between the open
3915/// and close tags.
3916pub(crate) fn text_object_inner_tag_bridge<H: crate::types::Host>(
3917    ed: &Editor<hjkl_buffer::Buffer, H>,
3918) -> Option<((usize, usize), (usize, usize))> {
3919    tag_text_object(ed, true)
3920}
3921
3922/// Resolve the range of `at` (around tag) at the cursor. Includes the open
3923/// and close tag delimiters themselves.
3924pub(crate) fn text_object_around_tag_bridge<H: crate::types::Host>(
3925    ed: &Editor<hjkl_buffer::Buffer, H>,
3926) -> Option<((usize, usize), (usize, usize))> {
3927    tag_text_object(ed, false)
3928}
3929
3930/// Greedy word-wrap the rows in `[top, bot]` to `settings.textwidth`.
3931/// Splits on blank-line boundaries so paragraph structure is
3932/// preserved. Each paragraph's words are joined with single spaces
3933/// before re-wrapping.
3934fn reflow_rows<H: crate::types::Host>(
3935    ed: &mut Editor<hjkl_buffer::Buffer, H>,
3936    top: usize,
3937    bot: usize,
3938) {
3939    let width = ed.settings().textwidth.max(1);
3940    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
3941    let bot = bot.min(lines.len().saturating_sub(1));
3942    if top > bot {
3943        return;
3944    }
3945    let original = lines[top..=bot].to_vec();
3946    let mut wrapped: Vec<String> = Vec::new();
3947    let mut paragraph: Vec<String> = Vec::new();
3948    let flush = |para: &mut Vec<String>, out: &mut Vec<String>, width: usize| {
3949        if para.is_empty() {
3950            return;
3951        }
3952        let words = para.join(" ");
3953        let mut current = String::new();
3954        for word in words.split_whitespace() {
3955            let extra = if current.is_empty() {
3956                word.chars().count()
3957            } else {
3958                current.chars().count() + 1 + word.chars().count()
3959            };
3960            if extra > width && !current.is_empty() {
3961                out.push(std::mem::take(&mut current));
3962                current.push_str(word);
3963            } else if current.is_empty() {
3964                current.push_str(word);
3965            } else {
3966                current.push(' ');
3967                current.push_str(word);
3968            }
3969        }
3970        if !current.is_empty() {
3971            out.push(current);
3972        }
3973        para.clear();
3974    };
3975    for line in &original {
3976        if line.trim().is_empty() {
3977            flush(&mut paragraph, &mut wrapped, width);
3978            wrapped.push(String::new());
3979        } else {
3980            paragraph.push(line.clone());
3981        }
3982    }
3983    flush(&mut paragraph, &mut wrapped, width);
3984
3985    // Splice back. push_undo above means `u` reverses.
3986    let after: Vec<String> = lines.split_off(bot + 1);
3987    lines.truncate(top);
3988    lines.extend(wrapped);
3989    lines.extend(after);
3990    ed.restore(lines, (top, 0));
3991    ed.mark_content_dirty();
3992}
3993
3994/// Transform the range `[top, bot]` (vim `RangeKind`) in place with
3995/// the given case operator. Cursor lands on `top` afterward — vim
3996/// convention for `gU{motion}` / `gu{motion}` / `g~{motion}`.
3997/// Preserves the textarea yank buffer (vim's case operators don't
3998/// touch registers).
3999fn apply_case_op_to_selection<H: crate::types::Host>(
4000    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4001    op: Operator,
4002    top: (usize, usize),
4003    bot: (usize, usize),
4004    kind: RangeKind,
4005) {
4006    use hjkl_buffer::Edit;
4007    ed.push_undo();
4008    let saved_yank = ed.yank().to_string();
4009    let saved_yank_linewise = ed.vim.yank_linewise;
4010    let selection = cut_vim_range(ed, top, bot, kind);
4011    let transformed = match op {
4012        Operator::Uppercase => selection.to_uppercase(),
4013        Operator::Lowercase => selection.to_lowercase(),
4014        Operator::ToggleCase => toggle_case_str(&selection),
4015        _ => unreachable!(),
4016    };
4017    if !transformed.is_empty() {
4018        let cursor = buf_cursor_pos(&ed.buffer);
4019        ed.mutate_edit(Edit::InsertStr {
4020            at: cursor,
4021            text: transformed,
4022        });
4023    }
4024    buf_set_cursor_rc(&mut ed.buffer, top.0, top.1);
4025    ed.push_buffer_cursor_to_textarea();
4026    ed.set_yank(saved_yank);
4027    ed.vim.yank_linewise = saved_yank_linewise;
4028    ed.vim.mode = Mode::Normal;
4029}
4030
4031/// Prepend `count * shiftwidth` spaces to each row in `[top, bot]`.
4032/// Rows that are empty are skipped (vim leaves blank lines alone when
4033/// indenting). `shiftwidth` is read from `editor.settings()` so
4034/// `:set shiftwidth=N` takes effect on the next operation.
4035fn indent_rows<H: crate::types::Host>(
4036    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4037    top: usize,
4038    bot: usize,
4039    count: usize,
4040) {
4041    ed.sync_buffer_content_from_textarea();
4042    let width = ed.settings().shiftwidth * count.max(1);
4043    let pad: String = " ".repeat(width);
4044    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4045    let bot = bot.min(lines.len().saturating_sub(1));
4046    for line in lines.iter_mut().take(bot + 1).skip(top) {
4047        if !line.is_empty() {
4048            line.insert_str(0, &pad);
4049        }
4050    }
4051    // Restore cursor to first non-blank of the top row so the next
4052    // vertical motion aims sensibly — matches vim's `>>` convention.
4053    ed.restore(lines, (top, 0));
4054    move_first_non_whitespace(ed);
4055}
4056
4057/// Remove up to `count * shiftwidth` leading spaces (or tabs) from
4058/// each row in `[top, bot]`. Rows with less leading whitespace have
4059/// all their indent stripped, not clipped to zero length.
4060fn outdent_rows<H: crate::types::Host>(
4061    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4062    top: usize,
4063    bot: usize,
4064    count: usize,
4065) {
4066    ed.sync_buffer_content_from_textarea();
4067    let width = ed.settings().shiftwidth * count.max(1);
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        let strip: usize = line
4072            .chars()
4073            .take(width)
4074            .take_while(|c| *c == ' ' || *c == '\t')
4075            .count();
4076        if strip > 0 {
4077            let byte_len: usize = line.chars().take(strip).map(|c| c.len_utf8()).sum();
4078            line.drain(..byte_len);
4079        }
4080    }
4081    ed.restore(lines, (top, 0));
4082    move_first_non_whitespace(ed);
4083}
4084
4085fn toggle_case_str(s: &str) -> String {
4086    s.chars()
4087        .map(|c| {
4088            if c.is_lowercase() {
4089                c.to_uppercase().next().unwrap_or(c)
4090            } else if c.is_uppercase() {
4091                c.to_lowercase().next().unwrap_or(c)
4092            } else {
4093                c
4094            }
4095        })
4096        .collect()
4097}
4098
4099fn order(a: (usize, usize), b: (usize, usize)) -> ((usize, usize), (usize, usize)) {
4100    if a <= b { (a, b) } else { (b, a) }
4101}
4102
4103/// Clamp the buffer cursor to normal-mode valid position: col may not
4104/// exceed `line.chars().count().saturating_sub(1)` (or 0 on an empty
4105/// line). Vim applies this clamp on every return to Normal mode after an
4106/// operator or Esc-from-insert.
4107fn clamp_cursor_to_normal_mode<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
4108    let (row, col) = ed.cursor();
4109    let line_chars = buf_line_chars(&ed.buffer, row);
4110    let max_col = line_chars.saturating_sub(1);
4111    if col > max_col {
4112        buf_set_cursor_rc(&mut ed.buffer, row, max_col);
4113        ed.push_buffer_cursor_to_textarea();
4114    }
4115}
4116
4117// ─── dd/cc/yy ──────────────────────────────────────────────────────────────
4118
4119fn execute_line_op<H: crate::types::Host>(
4120    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4121    op: Operator,
4122    count: usize,
4123) {
4124    let (row, col) = ed.cursor();
4125    let total = buf_row_count(&ed.buffer);
4126    let end_row = (row + count.saturating_sub(1)).min(total.saturating_sub(1));
4127
4128    match op {
4129        Operator::Yank => {
4130            // yy must not move the cursor.
4131            let text = read_vim_range(ed, (row, col), (end_row, 0), RangeKind::Linewise);
4132            if !text.is_empty() {
4133                ed.record_yank_to_host(text.clone());
4134                ed.record_yank(text, true);
4135            }
4136            // Vim `:h '[` / `:h ']`: yy/Nyy — linewise yank; `[` =
4137            // (top_row, 0), `]` = (bot_row, last_col).
4138            let last_col = buf_line_chars(&ed.buffer, end_row).saturating_sub(1);
4139            ed.set_mark('[', (row, 0));
4140            ed.set_mark(']', (end_row, last_col));
4141            buf_set_cursor_rc(&mut ed.buffer, row, col);
4142            ed.push_buffer_cursor_to_textarea();
4143            ed.vim.mode = Mode::Normal;
4144        }
4145        Operator::Delete => {
4146            ed.push_undo();
4147            let deleted_through_last = end_row + 1 >= total;
4148            cut_vim_range(ed, (row, col), (end_row, 0), RangeKind::Linewise);
4149            // Vim's `dd` / `Ndd` leaves the cursor on the *first
4150            // non-blank* of the line that now occupies `row` — or, if
4151            // the deletion consumed the last line, the line above it.
4152            let total_after = buf_row_count(&ed.buffer);
4153            let raw_target = if deleted_through_last {
4154                row.saturating_sub(1).min(total_after.saturating_sub(1))
4155            } else {
4156                row.min(total_after.saturating_sub(1))
4157            };
4158            // Clamp off the trailing phantom empty row that arises from a
4159            // buffer with a trailing newline (stored as ["...", ""]). If
4160            // the target row is the trailing empty row and there is a real
4161            // content row above it, use that instead — matching vim's view
4162            // that the trailing `\n` is a terminator, not a separator.
4163            let target_row = if raw_target > 0
4164                && raw_target + 1 == total_after
4165                && buf_line(&ed.buffer, raw_target)
4166                    .map(str::is_empty)
4167                    .unwrap_or(false)
4168            {
4169                raw_target - 1
4170            } else {
4171                raw_target
4172            };
4173            buf_set_cursor_rc(&mut ed.buffer, target_row, 0);
4174            ed.push_buffer_cursor_to_textarea();
4175            move_first_non_whitespace(ed);
4176            ed.sticky_col = Some(ed.cursor().1);
4177            ed.vim.mode = Mode::Normal;
4178            // Vim `:h '[` / `:h ']`: dd/Ndd — both marks park at the
4179            // post-delete cursor position (the join point).
4180            let pos = ed.cursor();
4181            ed.set_mark('[', pos);
4182            ed.set_mark(']', pos);
4183        }
4184        Operator::Change => {
4185            // `cc` / `3cc`: wipe contents of the covered lines but leave
4186            // a single blank line so insert-mode opens on it. Done as two
4187            // edits: drop rows past the first, then clear row `row`.
4188            use hjkl_buffer::{Edit, MotionKind as BufKind, Position};
4189            // Vim `:h '[`: stash change start for `]` deferral on insert-exit.
4190            ed.vim.change_mark_start = Some((row, 0));
4191            ed.push_undo();
4192            ed.sync_buffer_content_from_textarea();
4193            // Read the cut payload first so yank reflects every line.
4194            let payload = read_vim_range(ed, (row, col), (end_row, 0), RangeKind::Linewise);
4195            if end_row > row {
4196                ed.mutate_edit(Edit::DeleteRange {
4197                    start: Position::new(row + 1, 0),
4198                    end: Position::new(end_row, 0),
4199                    kind: BufKind::Line,
4200                });
4201            }
4202            let line_chars = buf_line_chars(&ed.buffer, row);
4203            if line_chars > 0 {
4204                ed.mutate_edit(Edit::DeleteRange {
4205                    start: Position::new(row, 0),
4206                    end: Position::new(row, line_chars),
4207                    kind: BufKind::Char,
4208                });
4209            }
4210            if !payload.is_empty() {
4211                ed.record_yank_to_host(payload.clone());
4212                ed.record_delete(payload, true);
4213            }
4214            buf_set_cursor_rc(&mut ed.buffer, row, 0);
4215            ed.push_buffer_cursor_to_textarea();
4216            begin_insert_noundo(ed, 1, InsertReason::AfterChange);
4217        }
4218        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4219            // `gUU` / `guu` / `g~~` — linewise case transform over
4220            // [row, end_row]. Preserve cursor on `row` (first non-blank
4221            // lines up with vim's behaviour).
4222            apply_case_op_to_selection(ed, op, (row, col), (end_row, 0), RangeKind::Linewise);
4223            // After case-op on a linewise range vim puts the cursor on
4224            // the first non-blank of the starting line.
4225            move_first_non_whitespace(ed);
4226        }
4227        Operator::Indent | Operator::Outdent => {
4228            // `>>` / `N>>` / `<<` / `N<<` — linewise indent / outdent.
4229            ed.push_undo();
4230            if op == Operator::Indent {
4231                indent_rows(ed, row, end_row, 1);
4232            } else {
4233                outdent_rows(ed, row, end_row, 1);
4234            }
4235            ed.sticky_col = Some(ed.cursor().1);
4236            ed.vim.mode = Mode::Normal;
4237        }
4238        // No doubled form — `zfzf` is two consecutive `zf` chords.
4239        Operator::Fold => unreachable!("Fold has no line-op double"),
4240        Operator::Reflow => {
4241            // `gqq` / `Ngqq` — reflow `count` rows starting at the cursor.
4242            ed.push_undo();
4243            reflow_rows(ed, row, end_row);
4244            move_first_non_whitespace(ed);
4245            ed.sticky_col = Some(ed.cursor().1);
4246            ed.vim.mode = Mode::Normal;
4247        }
4248    }
4249}
4250
4251// ─── Visual mode operators ─────────────────────────────────────────────────
4252
4253pub(crate) fn apply_visual_operator<H: crate::types::Host>(
4254    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4255    op: Operator,
4256) {
4257    match ed.vim.mode {
4258        Mode::VisualLine => {
4259            let cursor_row = buf_cursor_pos(&ed.buffer).row;
4260            let top = cursor_row.min(ed.vim.visual_line_anchor);
4261            let bot = cursor_row.max(ed.vim.visual_line_anchor);
4262            ed.vim.yank_linewise = true;
4263            match op {
4264                Operator::Yank => {
4265                    let text = read_vim_range(ed, (top, 0), (bot, 0), RangeKind::Linewise);
4266                    if !text.is_empty() {
4267                        ed.record_yank_to_host(text.clone());
4268                        ed.record_yank(text, true);
4269                    }
4270                    buf_set_cursor_rc(&mut ed.buffer, top, 0);
4271                    ed.push_buffer_cursor_to_textarea();
4272                    ed.vim.mode = Mode::Normal;
4273                }
4274                Operator::Delete => {
4275                    ed.push_undo();
4276                    cut_vim_range(ed, (top, 0), (bot, 0), RangeKind::Linewise);
4277                    ed.vim.mode = Mode::Normal;
4278                }
4279                Operator::Change => {
4280                    // Vim `Vc`: wipe the line contents but leave a blank
4281                    // line in place so insert-mode starts on an empty row.
4282                    use hjkl_buffer::{Edit, MotionKind as BufKind, Position};
4283                    ed.push_undo();
4284                    ed.sync_buffer_content_from_textarea();
4285                    let payload = read_vim_range(ed, (top, 0), (bot, 0), RangeKind::Linewise);
4286                    if bot > top {
4287                        ed.mutate_edit(Edit::DeleteRange {
4288                            start: Position::new(top + 1, 0),
4289                            end: Position::new(bot, 0),
4290                            kind: BufKind::Line,
4291                        });
4292                    }
4293                    let line_chars = buf_line_chars(&ed.buffer, top);
4294                    if line_chars > 0 {
4295                        ed.mutate_edit(Edit::DeleteRange {
4296                            start: Position::new(top, 0),
4297                            end: Position::new(top, line_chars),
4298                            kind: BufKind::Char,
4299                        });
4300                    }
4301                    if !payload.is_empty() {
4302                        ed.record_yank_to_host(payload.clone());
4303                        ed.record_delete(payload, true);
4304                    }
4305                    buf_set_cursor_rc(&mut ed.buffer, top, 0);
4306                    ed.push_buffer_cursor_to_textarea();
4307                    begin_insert_noundo(ed, 1, InsertReason::AfterChange);
4308                }
4309                Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4310                    let bot = buf_cursor_pos(&ed.buffer)
4311                        .row
4312                        .max(ed.vim.visual_line_anchor);
4313                    apply_case_op_to_selection(ed, op, (top, 0), (bot, 0), RangeKind::Linewise);
4314                    move_first_non_whitespace(ed);
4315                }
4316                Operator::Indent | Operator::Outdent => {
4317                    ed.push_undo();
4318                    let (cursor_row, _) = ed.cursor();
4319                    let bot = cursor_row.max(ed.vim.visual_line_anchor);
4320                    if op == Operator::Indent {
4321                        indent_rows(ed, top, bot, 1);
4322                    } else {
4323                        outdent_rows(ed, top, bot, 1);
4324                    }
4325                    ed.vim.mode = Mode::Normal;
4326                }
4327                Operator::Reflow => {
4328                    ed.push_undo();
4329                    let (cursor_row, _) = ed.cursor();
4330                    let bot = cursor_row.max(ed.vim.visual_line_anchor);
4331                    reflow_rows(ed, top, bot);
4332                    ed.vim.mode = Mode::Normal;
4333                }
4334                // Visual `zf` is handled inline in `handle_after_z`,
4335                // never routed through this dispatcher.
4336                Operator::Fold => unreachable!("Visual zf takes its own path"),
4337            }
4338        }
4339        Mode::Visual => {
4340            ed.vim.yank_linewise = false;
4341            let anchor = ed.vim.visual_anchor;
4342            let cursor = ed.cursor();
4343            let (top, bot) = order(anchor, cursor);
4344            match op {
4345                Operator::Yank => {
4346                    let text = read_vim_range(ed, top, bot, RangeKind::Inclusive);
4347                    if !text.is_empty() {
4348                        ed.record_yank_to_host(text.clone());
4349                        ed.record_yank(text, false);
4350                    }
4351                    buf_set_cursor_rc(&mut ed.buffer, top.0, top.1);
4352                    ed.push_buffer_cursor_to_textarea();
4353                    ed.vim.mode = Mode::Normal;
4354                }
4355                Operator::Delete => {
4356                    ed.push_undo();
4357                    cut_vim_range(ed, top, bot, RangeKind::Inclusive);
4358                    ed.vim.mode = Mode::Normal;
4359                }
4360                Operator::Change => {
4361                    ed.push_undo();
4362                    cut_vim_range(ed, top, bot, RangeKind::Inclusive);
4363                    begin_insert_noundo(ed, 1, InsertReason::AfterChange);
4364                }
4365                Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4366                    // Anchor stays where the visual selection started.
4367                    let anchor = ed.vim.visual_anchor;
4368                    let cursor = ed.cursor();
4369                    let (top, bot) = order(anchor, cursor);
4370                    apply_case_op_to_selection(ed, op, top, bot, RangeKind::Inclusive);
4371                }
4372                Operator::Indent | Operator::Outdent => {
4373                    ed.push_undo();
4374                    let anchor = ed.vim.visual_anchor;
4375                    let cursor = ed.cursor();
4376                    let (top, bot) = order(anchor, cursor);
4377                    if op == Operator::Indent {
4378                        indent_rows(ed, top.0, bot.0, 1);
4379                    } else {
4380                        outdent_rows(ed, top.0, bot.0, 1);
4381                    }
4382                    ed.vim.mode = Mode::Normal;
4383                }
4384                Operator::Reflow => {
4385                    ed.push_undo();
4386                    let anchor = ed.vim.visual_anchor;
4387                    let cursor = ed.cursor();
4388                    let (top, bot) = order(anchor, cursor);
4389                    reflow_rows(ed, top.0, bot.0);
4390                    ed.vim.mode = Mode::Normal;
4391                }
4392                Operator::Fold => unreachable!("Visual zf takes its own path"),
4393            }
4394        }
4395        Mode::VisualBlock => apply_block_operator(ed, op),
4396        _ => {}
4397    }
4398}
4399
4400/// Compute `(top_row, bot_row, left_col, right_col)` for the current
4401/// VisualBlock selection. Columns are inclusive on both ends. Uses the
4402/// tracked virtual column (updated by h/l, preserved across j/k) so
4403/// ragged / empty rows don't collapse the block's width.
4404fn block_bounds<H: crate::types::Host>(
4405    ed: &Editor<hjkl_buffer::Buffer, H>,
4406) -> (usize, usize, usize, usize) {
4407    let (ar, ac) = ed.vim.block_anchor;
4408    let (cr, _) = ed.cursor();
4409    let cc = ed.vim.block_vcol;
4410    let top = ar.min(cr);
4411    let bot = ar.max(cr);
4412    let left = ac.min(cc);
4413    let right = ac.max(cc);
4414    (top, bot, left, right)
4415}
4416
4417/// Update the virtual column after a motion in VisualBlock mode.
4418/// Horizontal motions sync `block_vcol` to the new cursor column;
4419/// vertical / non-h/l motions leave it alone so the intended column
4420/// survives clamping to shorter lines.
4421pub(crate) fn update_block_vcol<H: crate::types::Host>(
4422    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4423    motion: &Motion,
4424) {
4425    match motion {
4426        Motion::Left
4427        | Motion::Right
4428        | Motion::WordFwd
4429        | Motion::BigWordFwd
4430        | Motion::WordBack
4431        | Motion::BigWordBack
4432        | Motion::WordEnd
4433        | Motion::BigWordEnd
4434        | Motion::WordEndBack
4435        | Motion::BigWordEndBack
4436        | Motion::LineStart
4437        | Motion::FirstNonBlank
4438        | Motion::LineEnd
4439        | Motion::Find { .. }
4440        | Motion::FindRepeat { .. }
4441        | Motion::MatchBracket => {
4442            ed.vim.block_vcol = ed.cursor().1;
4443        }
4444        // Up / Down / FileTop / FileBottom / Search — preserve vcol.
4445        _ => {}
4446    }
4447}
4448
4449/// Yank / delete / change / replace a rectangular selection. Yanked text
4450/// is stored as one string per row joined with `\n` so pasting reproduces
4451/// the block as sequential lines. (Vim's true block-paste reinserts as
4452/// columns; we render the content with our char-wise paste path.)
4453fn apply_block_operator<H: crate::types::Host>(
4454    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4455    op: Operator,
4456) {
4457    let (top, bot, left, right) = block_bounds(ed);
4458    // Snapshot the block text for yank / clipboard.
4459    let yank = block_yank(ed, top, bot, left, right);
4460
4461    match op {
4462        Operator::Yank => {
4463            if !yank.is_empty() {
4464                ed.record_yank_to_host(yank.clone());
4465                ed.record_yank(yank, false);
4466            }
4467            ed.vim.mode = Mode::Normal;
4468            ed.jump_cursor(top, left);
4469        }
4470        Operator::Delete => {
4471            ed.push_undo();
4472            delete_block_contents(ed, top, bot, left, right);
4473            if !yank.is_empty() {
4474                ed.record_yank_to_host(yank.clone());
4475                ed.record_delete(yank, false);
4476            }
4477            ed.vim.mode = Mode::Normal;
4478            ed.jump_cursor(top, left);
4479        }
4480        Operator::Change => {
4481            ed.push_undo();
4482            delete_block_contents(ed, top, bot, left, right);
4483            if !yank.is_empty() {
4484                ed.record_yank_to_host(yank.clone());
4485                ed.record_delete(yank, false);
4486            }
4487            ed.jump_cursor(top, left);
4488            begin_insert_noundo(
4489                ed,
4490                1,
4491                InsertReason::BlockChange {
4492                    top,
4493                    bot,
4494                    col: left,
4495                },
4496            );
4497        }
4498        Operator::Uppercase | Operator::Lowercase | Operator::ToggleCase => {
4499            ed.push_undo();
4500            transform_block_case(ed, op, top, bot, left, right);
4501            ed.vim.mode = Mode::Normal;
4502            ed.jump_cursor(top, left);
4503        }
4504        Operator::Indent | Operator::Outdent => {
4505            // VisualBlock `>` / `<` falls back to linewise indent over
4506            // the block's row range — vim does the same (column-wise
4507            // indent/outdent doesn't make sense).
4508            ed.push_undo();
4509            if op == Operator::Indent {
4510                indent_rows(ed, top, bot, 1);
4511            } else {
4512                outdent_rows(ed, top, bot, 1);
4513            }
4514            ed.vim.mode = Mode::Normal;
4515        }
4516        Operator::Fold => unreachable!("Visual zf takes its own path"),
4517        Operator::Reflow => {
4518            // Reflow over the block falls back to linewise reflow over
4519            // the row range — column slicing for `gq` doesn't make
4520            // sense.
4521            ed.push_undo();
4522            reflow_rows(ed, top, bot);
4523            ed.vim.mode = Mode::Normal;
4524        }
4525    }
4526}
4527
4528/// In-place case transform over the rectangular block
4529/// `(top..=bot, left..=right)`. Rows shorter than `left` are left
4530/// untouched — vim behaves the same way (ragged blocks).
4531fn transform_block_case<H: crate::types::Host>(
4532    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4533    op: Operator,
4534    top: usize,
4535    bot: usize,
4536    left: usize,
4537    right: usize,
4538) {
4539    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4540    for r in top..=bot.min(lines.len().saturating_sub(1)) {
4541        let chars: Vec<char> = lines[r].chars().collect();
4542        if left >= chars.len() {
4543            continue;
4544        }
4545        let end = (right + 1).min(chars.len());
4546        let head: String = chars[..left].iter().collect();
4547        let mid: String = chars[left..end].iter().collect();
4548        let tail: String = chars[end..].iter().collect();
4549        let transformed = match op {
4550            Operator::Uppercase => mid.to_uppercase(),
4551            Operator::Lowercase => mid.to_lowercase(),
4552            Operator::ToggleCase => toggle_case_str(&mid),
4553            _ => mid,
4554        };
4555        lines[r] = format!("{head}{transformed}{tail}");
4556    }
4557    let saved_yank = ed.yank().to_string();
4558    let saved_linewise = ed.vim.yank_linewise;
4559    ed.restore(lines, (top, left));
4560    ed.set_yank(saved_yank);
4561    ed.vim.yank_linewise = saved_linewise;
4562}
4563
4564fn block_yank<H: crate::types::Host>(
4565    ed: &Editor<hjkl_buffer::Buffer, H>,
4566    top: usize,
4567    bot: usize,
4568    left: usize,
4569    right: usize,
4570) -> String {
4571    let lines = buf_lines_to_vec(&ed.buffer);
4572    let mut rows: Vec<String> = Vec::new();
4573    for r in top..=bot {
4574        let line = match lines.get(r) {
4575            Some(l) => l,
4576            None => break,
4577        };
4578        let chars: Vec<char> = line.chars().collect();
4579        let end = (right + 1).min(chars.len());
4580        if left >= chars.len() {
4581            rows.push(String::new());
4582        } else {
4583            rows.push(chars[left..end].iter().collect());
4584        }
4585    }
4586    rows.join("\n")
4587}
4588
4589fn delete_block_contents<H: crate::types::Host>(
4590    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4591    top: usize,
4592    bot: usize,
4593    left: usize,
4594    right: usize,
4595) {
4596    use hjkl_buffer::{Edit, MotionKind, Position};
4597    ed.sync_buffer_content_from_textarea();
4598    let last_row = bot.min(buf_row_count(&ed.buffer).saturating_sub(1));
4599    if last_row < top {
4600        return;
4601    }
4602    ed.mutate_edit(Edit::DeleteRange {
4603        start: Position::new(top, left),
4604        end: Position::new(last_row, right),
4605        kind: MotionKind::Block,
4606    });
4607    ed.push_buffer_cursor_to_textarea();
4608}
4609
4610/// Replace each character cell in the block with `ch`.
4611pub(crate) fn block_replace<H: crate::types::Host>(
4612    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4613    ch: char,
4614) {
4615    let (top, bot, left, right) = block_bounds(ed);
4616    ed.push_undo();
4617    ed.sync_buffer_content_from_textarea();
4618    let mut lines: Vec<String> = buf_lines_to_vec(&ed.buffer);
4619    for r in top..=bot.min(lines.len().saturating_sub(1)) {
4620        let chars: Vec<char> = lines[r].chars().collect();
4621        if left >= chars.len() {
4622            continue;
4623        }
4624        let end = (right + 1).min(chars.len());
4625        let before: String = chars[..left].iter().collect();
4626        let middle: String = std::iter::repeat_n(ch, end - left).collect();
4627        let after: String = chars[end..].iter().collect();
4628        lines[r] = format!("{before}{middle}{after}");
4629    }
4630    reset_textarea_lines(ed, lines);
4631    ed.vim.mode = Mode::Normal;
4632    ed.jump_cursor(top, left);
4633}
4634
4635/// Replace buffer content with `lines` while preserving the cursor.
4636/// Used by indent / outdent / block_replace to wholesale rewrite
4637/// rows without going through the per-edit funnel.
4638fn reset_textarea_lines<H: crate::types::Host>(
4639    ed: &mut Editor<hjkl_buffer::Buffer, H>,
4640    lines: Vec<String>,
4641) {
4642    let cursor = ed.cursor();
4643    crate::types::BufferEdit::replace_all(&mut ed.buffer, &lines.join("\n"));
4644    buf_set_cursor_rc(&mut ed.buffer, cursor.0, cursor.1);
4645    ed.mark_content_dirty();
4646}
4647
4648// ─── Visual-line helpers ───────────────────────────────────────────────────
4649
4650// ─── Text-object range computation ─────────────────────────────────────────
4651
4652/// Cursor position as `(row, col)`.
4653type Pos = (usize, usize);
4654
4655/// Returns `(start, end, kind)` where `end` is *exclusive* (one past the
4656/// last character to act on). `kind` is `Linewise` for line-oriented text
4657/// objects like paragraphs and `Exclusive` otherwise.
4658pub(crate) fn text_object_range<H: crate::types::Host>(
4659    ed: &Editor<hjkl_buffer::Buffer, H>,
4660    obj: TextObject,
4661    inner: bool,
4662) -> Option<(Pos, Pos, RangeKind)> {
4663    match obj {
4664        TextObject::Word { big } => {
4665            word_text_object(ed, inner, big).map(|(s, e)| (s, e, RangeKind::Exclusive))
4666        }
4667        TextObject::Quote(q) => {
4668            quote_text_object(ed, q, inner).map(|(s, e)| (s, e, RangeKind::Exclusive))
4669        }
4670        TextObject::Bracket(open) => bracket_text_object(ed, open, inner),
4671        TextObject::Paragraph => {
4672            paragraph_text_object(ed, inner).map(|(s, e)| (s, e, RangeKind::Linewise))
4673        }
4674        TextObject::XmlTag => tag_text_object(ed, inner).map(|(s, e)| (s, e, RangeKind::Exclusive)),
4675        TextObject::Sentence => {
4676            sentence_text_object(ed, inner).map(|(s, e)| (s, e, RangeKind::Exclusive))
4677        }
4678    }
4679}
4680
4681/// `(` / `)` — walk to the next sentence boundary in `forward` direction.
4682/// Returns `(row, col)` of the boundary's first non-whitespace cell, or
4683/// `None` when already at the buffer's edge in that direction.
4684fn sentence_boundary<H: crate::types::Host>(
4685    ed: &Editor<hjkl_buffer::Buffer, H>,
4686    forward: bool,
4687) -> Option<(usize, usize)> {
4688    let lines = buf_lines_to_vec(&ed.buffer);
4689    if lines.is_empty() {
4690        return None;
4691    }
4692    let pos_to_idx = |pos: (usize, usize)| -> usize {
4693        let mut idx = 0;
4694        for line in lines.iter().take(pos.0) {
4695            idx += line.chars().count() + 1;
4696        }
4697        idx + pos.1
4698    };
4699    let idx_to_pos = |mut idx: usize| -> (usize, usize) {
4700        for (r, line) in lines.iter().enumerate() {
4701            let len = line.chars().count();
4702            if idx <= len {
4703                return (r, idx);
4704            }
4705            idx -= len + 1;
4706        }
4707        let last = lines.len().saturating_sub(1);
4708        (last, lines[last].chars().count())
4709    };
4710    let mut chars: Vec<char> = Vec::new();
4711    for (r, line) in lines.iter().enumerate() {
4712        chars.extend(line.chars());
4713        if r + 1 < lines.len() {
4714            chars.push('\n');
4715        }
4716    }
4717    if chars.is_empty() {
4718        return None;
4719    }
4720    let total = chars.len();
4721    let cursor_idx = pos_to_idx(ed.cursor()).min(total - 1);
4722    let is_terminator = |c: char| matches!(c, '.' | '?' | '!');
4723
4724    if forward {
4725        // Walk forward looking for a terminator run followed by
4726        // whitespace; land on the first non-whitespace cell after.
4727        let mut i = cursor_idx + 1;
4728        while i < total {
4729            if is_terminator(chars[i]) {
4730                while i + 1 < total && is_terminator(chars[i + 1]) {
4731                    i += 1;
4732                }
4733                if i + 1 >= total {
4734                    return None;
4735                }
4736                if chars[i + 1].is_whitespace() {
4737                    let mut j = i + 1;
4738                    while j < total && chars[j].is_whitespace() {
4739                        j += 1;
4740                    }
4741                    if j >= total {
4742                        return None;
4743                    }
4744                    return Some(idx_to_pos(j));
4745                }
4746            }
4747            i += 1;
4748        }
4749        None
4750    } else {
4751        // Walk backward to find the start of the current sentence (if
4752        // we're already at the start, jump to the previous sentence's
4753        // start instead).
4754        let find_start = |from: usize| -> Option<usize> {
4755            let mut start = from;
4756            while start > 0 {
4757                let prev = chars[start - 1];
4758                if prev.is_whitespace() {
4759                    let mut k = start - 1;
4760                    while k > 0 && chars[k - 1].is_whitespace() {
4761                        k -= 1;
4762                    }
4763                    if k > 0 && is_terminator(chars[k - 1]) {
4764                        break;
4765                    }
4766                }
4767                start -= 1;
4768            }
4769            while start < total && chars[start].is_whitespace() {
4770                start += 1;
4771            }
4772            (start < total).then_some(start)
4773        };
4774        let current_start = find_start(cursor_idx)?;
4775        if current_start < cursor_idx {
4776            return Some(idx_to_pos(current_start));
4777        }
4778        // Already at the sentence start — step over the boundary into
4779        // the previous sentence and find its start.
4780        let mut k = current_start;
4781        while k > 0 && chars[k - 1].is_whitespace() {
4782            k -= 1;
4783        }
4784        if k == 0 {
4785            return None;
4786        }
4787        let prev_start = find_start(k - 1)?;
4788        Some(idx_to_pos(prev_start))
4789    }
4790}
4791
4792/// `is` / `as` — sentence: text up to and including the next sentence
4793/// terminator (`.`, `?`, `!`). Vim treats `.`/`?`/`!` followed by
4794/// whitespace (or end-of-line) as a boundary; runs of consecutive
4795/// terminators stay attached to the same sentence. `as` extends to
4796/// include trailing whitespace; `is` does not.
4797fn sentence_text_object<H: crate::types::Host>(
4798    ed: &Editor<hjkl_buffer::Buffer, H>,
4799    inner: bool,
4800) -> Option<((usize, usize), (usize, usize))> {
4801    let lines = buf_lines_to_vec(&ed.buffer);
4802    if lines.is_empty() {
4803        return None;
4804    }
4805    // Flatten the buffer so a sentence can span lines (vim's behaviour).
4806    // Newlines count as whitespace for boundary detection.
4807    let pos_to_idx = |pos: (usize, usize)| -> usize {
4808        let mut idx = 0;
4809        for line in lines.iter().take(pos.0) {
4810            idx += line.chars().count() + 1;
4811        }
4812        idx + pos.1
4813    };
4814    let idx_to_pos = |mut idx: usize| -> (usize, usize) {
4815        for (r, line) in lines.iter().enumerate() {
4816            let len = line.chars().count();
4817            if idx <= len {
4818                return (r, idx);
4819            }
4820            idx -= len + 1;
4821        }
4822        let last = lines.len().saturating_sub(1);
4823        (last, lines[last].chars().count())
4824    };
4825    let mut chars: Vec<char> = Vec::new();
4826    for (r, line) in lines.iter().enumerate() {
4827        chars.extend(line.chars());
4828        if r + 1 < lines.len() {
4829            chars.push('\n');
4830        }
4831    }
4832    if chars.is_empty() {
4833        return None;
4834    }
4835
4836    let cursor_idx = pos_to_idx(ed.cursor()).min(chars.len() - 1);
4837    let is_terminator = |c: char| matches!(c, '.' | '?' | '!');
4838
4839    // Walk backward from cursor to find the start of the current
4840    // sentence. A boundary is: whitespace immediately after a run of
4841    // terminators (or start-of-buffer).
4842    let mut start = cursor_idx;
4843    while start > 0 {
4844        let prev = chars[start - 1];
4845        if prev.is_whitespace() {
4846            // Check if the whitespace follows a terminator — if so,
4847            // we've crossed a sentence boundary; the sentence begins
4848            // at the first non-whitespace cell *after* this run.
4849            let mut k = start - 1;
4850            while k > 0 && chars[k - 1].is_whitespace() {
4851                k -= 1;
4852            }
4853            if k > 0 && is_terminator(chars[k - 1]) {
4854                break;
4855            }
4856        }
4857        start -= 1;
4858    }
4859    // Skip leading whitespace (vim doesn't include it in the
4860    // sentence body).
4861    while start < chars.len() && chars[start].is_whitespace() {
4862        start += 1;
4863    }
4864    if start >= chars.len() {
4865        return None;
4866    }
4867
4868    // Walk forward to the sentence end (last terminator before the
4869    // next whitespace boundary).
4870    let mut end = start;
4871    while end < chars.len() {
4872        if is_terminator(chars[end]) {
4873            // Consume any consecutive terminators (e.g. `?!`).
4874            while end + 1 < chars.len() && is_terminator(chars[end + 1]) {
4875                end += 1;
4876            }
4877            // If followed by whitespace or end-of-buffer, that's the
4878            // boundary.
4879            if end + 1 >= chars.len() || chars[end + 1].is_whitespace() {
4880                break;
4881            }
4882        }
4883        end += 1;
4884    }
4885    // Inclusive end → exclusive end_idx.
4886    let end_idx = (end + 1).min(chars.len());
4887
4888    let final_end = if inner {
4889        end_idx
4890    } else {
4891        // `as`: include trailing whitespace (but stop before the next
4892        // newline so we don't gobble a paragraph break — vim keeps
4893        // sentences within a paragraph for the trailing-ws extension).
4894        let mut e = end_idx;
4895        while e < chars.len() && chars[e].is_whitespace() && chars[e] != '\n' {
4896            e += 1;
4897        }
4898        e
4899    };
4900
4901    Some((idx_to_pos(start), idx_to_pos(final_end)))
4902}
4903
4904/// `it` / `at` — XML tag pair text object. Builds a flat char index of
4905/// the buffer, walks `<...>` tokens to pair tags via a stack, and
4906/// returns the innermost pair containing the cursor.
4907fn tag_text_object<H: crate::types::Host>(
4908    ed: &Editor<hjkl_buffer::Buffer, H>,
4909    inner: bool,
4910) -> Option<((usize, usize), (usize, usize))> {
4911    let lines = buf_lines_to_vec(&ed.buffer);
4912    if lines.is_empty() {
4913        return None;
4914    }
4915    // Flatten char positions so we can compare cursor against tag
4916    // ranges without per-row arithmetic. `\n` between lines counts as
4917    // a single char.
4918    let pos_to_idx = |pos: (usize, usize)| -> usize {
4919        let mut idx = 0;
4920        for line in lines.iter().take(pos.0) {
4921            idx += line.chars().count() + 1;
4922        }
4923        idx + pos.1
4924    };
4925    let idx_to_pos = |mut idx: usize| -> (usize, usize) {
4926        for (r, line) in lines.iter().enumerate() {
4927            let len = line.chars().count();
4928            if idx <= len {
4929                return (r, idx);
4930            }
4931            idx -= len + 1;
4932        }
4933        let last = lines.len().saturating_sub(1);
4934        (last, lines[last].chars().count())
4935    };
4936    let mut chars: Vec<char> = Vec::new();
4937    for (r, line) in lines.iter().enumerate() {
4938        chars.extend(line.chars());
4939        if r + 1 < lines.len() {
4940            chars.push('\n');
4941        }
4942    }
4943    let cursor_idx = pos_to_idx(ed.cursor());
4944
4945    // Walk `<...>` tokens. Track open tags on a stack; on a matching
4946    // close pop and consider the pair a candidate when the cursor lies
4947    // inside its content range. Innermost wins (replace whenever a
4948    // tighter range turns up). Also track the first complete pair that
4949    // starts at or after the cursor so we can fall back to a forward
4950    // scan (targets.vim-style) when the cursor isn't inside any tag.
4951    let mut stack: Vec<(usize, usize, String)> = Vec::new(); // (open_start, content_start, name)
4952    let mut innermost: Option<(usize, usize, usize, usize)> = None;
4953    let mut next_after: Option<(usize, usize, usize, usize)> = None;
4954    let mut i = 0;
4955    while i < chars.len() {
4956        if chars[i] != '<' {
4957            i += 1;
4958            continue;
4959        }
4960        let mut j = i + 1;
4961        while j < chars.len() && chars[j] != '>' {
4962            j += 1;
4963        }
4964        if j >= chars.len() {
4965            break;
4966        }
4967        let inside: String = chars[i + 1..j].iter().collect();
4968        let close_end = j + 1;
4969        let trimmed = inside.trim();
4970        if trimmed.starts_with('!') || trimmed.starts_with('?') {
4971            i = close_end;
4972            continue;
4973        }
4974        if let Some(rest) = trimmed.strip_prefix('/') {
4975            let name = rest.split_whitespace().next().unwrap_or("").to_string();
4976            if !name.is_empty()
4977                && let Some(stack_idx) = stack.iter().rposition(|(_, _, n)| *n == name)
4978            {
4979                let (open_start, content_start, _) = stack[stack_idx].clone();
4980                stack.truncate(stack_idx);
4981                let content_end = i;
4982                let candidate = (open_start, content_start, content_end, close_end);
4983                if cursor_idx >= content_start && cursor_idx <= content_end {
4984                    innermost = match innermost {
4985                        Some((_, cs, ce, _)) if cs <= content_start && content_end <= ce => {
4986                            Some(candidate)
4987                        }
4988                        None => Some(candidate),
4989                        existing => existing,
4990                    };
4991                } else if open_start >= cursor_idx && next_after.is_none() {
4992                    next_after = Some(candidate);
4993                }
4994            }
4995        } else if !trimmed.ends_with('/') {
4996            let name: String = trimmed
4997                .split(|c: char| c.is_whitespace() || c == '/')
4998                .next()
4999                .unwrap_or("")
5000                .to_string();
5001            if !name.is_empty() {
5002                stack.push((i, close_end, name));
5003            }
5004        }
5005        i = close_end;
5006    }
5007
5008    let (open_start, content_start, content_end, close_end) = innermost.or(next_after)?;
5009    if inner {
5010        Some((idx_to_pos(content_start), idx_to_pos(content_end)))
5011    } else {
5012        Some((idx_to_pos(open_start), idx_to_pos(close_end)))
5013    }
5014}
5015
5016fn is_wordchar(c: char) -> bool {
5017    c.is_alphanumeric() || c == '_'
5018}
5019
5020// `is_keyword_char` lives in hjkl-buffer (used by word motions);
5021// engine re-uses it via `hjkl_buffer::is_keyword_char` so there's
5022// one parser, one default, one bug surface.
5023pub(crate) use hjkl_buffer::is_keyword_char;
5024
5025fn word_text_object<H: crate::types::Host>(
5026    ed: &Editor<hjkl_buffer::Buffer, H>,
5027    inner: bool,
5028    big: bool,
5029) -> Option<((usize, usize), (usize, usize))> {
5030    let (row, col) = ed.cursor();
5031    let line = buf_line(&ed.buffer, row)?;
5032    let chars: Vec<char> = line.chars().collect();
5033    if chars.is_empty() {
5034        return None;
5035    }
5036    let at = col.min(chars.len().saturating_sub(1));
5037    let classify = |c: char| -> u8 {
5038        if c.is_whitespace() {
5039            0
5040        } else if big || is_wordchar(c) {
5041            1
5042        } else {
5043            2
5044        }
5045    };
5046    let cls = classify(chars[at]);
5047    let mut start = at;
5048    while start > 0 && classify(chars[start - 1]) == cls {
5049        start -= 1;
5050    }
5051    let mut end = at;
5052    while end + 1 < chars.len() && classify(chars[end + 1]) == cls {
5053        end += 1;
5054    }
5055    // Byte-offset helpers.
5056    let char_byte = |i: usize| {
5057        if i >= chars.len() {
5058            line.len()
5059        } else {
5060            line.char_indices().nth(i).map(|(b, _)| b).unwrap_or(0)
5061        }
5062    };
5063    let mut start_col = char_byte(start);
5064    // Exclusive end: byte index of char AFTER the last-included char.
5065    let mut end_col = char_byte(end + 1);
5066    if !inner {
5067        // `aw` — include trailing whitespace; if there's no trailing ws, absorb leading ws.
5068        let mut t = end + 1;
5069        let mut included_trailing = false;
5070        while t < chars.len() && chars[t].is_whitespace() {
5071            included_trailing = true;
5072            t += 1;
5073        }
5074        if included_trailing {
5075            end_col = char_byte(t);
5076        } else {
5077            let mut s = start;
5078            while s > 0 && chars[s - 1].is_whitespace() {
5079                s -= 1;
5080            }
5081            start_col = char_byte(s);
5082        }
5083    }
5084    Some(((row, start_col), (row, end_col)))
5085}
5086
5087fn quote_text_object<H: crate::types::Host>(
5088    ed: &Editor<hjkl_buffer::Buffer, H>,
5089    q: char,
5090    inner: bool,
5091) -> Option<((usize, usize), (usize, usize))> {
5092    let (row, col) = ed.cursor();
5093    let line = buf_line(&ed.buffer, row)?;
5094    let bytes = line.as_bytes();
5095    let q_byte = q as u8;
5096    // Find opening and closing quote on the same line.
5097    let mut positions: Vec<usize> = Vec::new();
5098    for (i, &b) in bytes.iter().enumerate() {
5099        if b == q_byte {
5100            positions.push(i);
5101        }
5102    }
5103    if positions.len() < 2 {
5104        return None;
5105    }
5106    let mut open_idx: Option<usize> = None;
5107    let mut close_idx: Option<usize> = None;
5108    for pair in positions.chunks(2) {
5109        if pair.len() < 2 {
5110            break;
5111        }
5112        if col >= pair[0] && col <= pair[1] {
5113            open_idx = Some(pair[0]);
5114            close_idx = Some(pair[1]);
5115            break;
5116        }
5117        if col < pair[0] {
5118            open_idx = Some(pair[0]);
5119            close_idx = Some(pair[1]);
5120            break;
5121        }
5122    }
5123    let open = open_idx?;
5124    let close = close_idx?;
5125    // End columns are *exclusive* — one past the last character to act on.
5126    if inner {
5127        if close <= open + 1 {
5128            return None;
5129        }
5130        Some(((row, open + 1), (row, close)))
5131    } else {
5132        // `da<q>` — "around" includes the surrounding whitespace on one
5133        // side: trailing whitespace if any exists after the closing quote;
5134        // otherwise leading whitespace before the opening quote. This
5135        // matches vim's `:help text-objects` behaviour and avoids leaving
5136        // a double-space when the quoted span sits mid-sentence.
5137        let after_close = close + 1; // byte index after closing quote
5138        if after_close < bytes.len() && bytes[after_close].is_ascii_whitespace() {
5139            // Eat trailing whitespace run.
5140            let mut end = after_close;
5141            while end < bytes.len() && bytes[end].is_ascii_whitespace() {
5142                end += 1;
5143            }
5144            Some(((row, open), (row, end)))
5145        } else if open > 0 && bytes[open - 1].is_ascii_whitespace() {
5146            // Eat leading whitespace run.
5147            let mut start = open;
5148            while start > 0 && bytes[start - 1].is_ascii_whitespace() {
5149                start -= 1;
5150            }
5151            Some(((row, start), (row, close + 1)))
5152        } else {
5153            Some(((row, open), (row, close + 1)))
5154        }
5155    }
5156}
5157
5158fn bracket_text_object<H: crate::types::Host>(
5159    ed: &Editor<hjkl_buffer::Buffer, H>,
5160    open: char,
5161    inner: bool,
5162) -> Option<(Pos, Pos, RangeKind)> {
5163    let close = match open {
5164        '(' => ')',
5165        '[' => ']',
5166        '{' => '}',
5167        '<' => '>',
5168        _ => return None,
5169    };
5170    let (row, col) = ed.cursor();
5171    let lines = buf_lines_to_vec(&ed.buffer);
5172    let lines = lines.as_slice();
5173    // Walk backward from cursor to find unbalanced opening. When the
5174    // cursor isn't inside any pair, fall back to scanning forward for
5175    // the next opening bracket (targets.vim-style: `ci(` works when
5176    // cursor is before the `(` on the same line or below).
5177    let open_pos = find_open_bracket(lines, row, col, open, close)
5178        .or_else(|| find_next_open(lines, row, col, open))?;
5179    let close_pos = find_close_bracket(lines, open_pos.0, open_pos.1 + 1, open, close)?;
5180    // End positions are *exclusive*.
5181    if inner {
5182        // Multi-line `iB` / `i{` etc: vim deletes the full lines between
5183        // the braces (linewise), preserving the `{` and `}` lines
5184        // themselves and the newlines that directly abut them. E.g.:
5185        //   {\n    body\n}\n  →  {\n}\n    (cursor on `}` line)
5186        // Single-line `i{` falls back to charwise exclusive.
5187        if close_pos.0 > open_pos.0 + 1 {
5188            // There is at least one line strictly between open and close.
5189            let inner_row_start = open_pos.0 + 1;
5190            let inner_row_end = close_pos.0 - 1;
5191            let end_col = lines
5192                .get(inner_row_end)
5193                .map(|l| l.chars().count())
5194                .unwrap_or(0);
5195            return Some((
5196                (inner_row_start, 0),
5197                (inner_row_end, end_col),
5198                RangeKind::Linewise,
5199            ));
5200        }
5201        let inner_start = advance_pos(lines, open_pos);
5202        if inner_start.0 > close_pos.0
5203            || (inner_start.0 == close_pos.0 && inner_start.1 >= close_pos.1)
5204        {
5205            return None;
5206        }
5207        Some((inner_start, close_pos, RangeKind::Exclusive))
5208    } else {
5209        Some((
5210            open_pos,
5211            advance_pos(lines, close_pos),
5212            RangeKind::Exclusive,
5213        ))
5214    }
5215}
5216
5217fn find_open_bracket(
5218    lines: &[String],
5219    row: usize,
5220    col: usize,
5221    open: char,
5222    close: char,
5223) -> Option<(usize, usize)> {
5224    let mut depth: i32 = 0;
5225    let mut r = row;
5226    let mut c = col as isize;
5227    loop {
5228        let cur = &lines[r];
5229        let chars: Vec<char> = cur.chars().collect();
5230        // Clamp `c` to the line length: callers may seed `col` past
5231        // EOL on virtual-cursor lines (e.g., insert mode after `o`)
5232        // so direct indexing would panic on empty / short lines.
5233        if (c as usize) >= chars.len() {
5234            c = chars.len() as isize - 1;
5235        }
5236        while c >= 0 {
5237            let ch = chars[c as usize];
5238            if ch == close {
5239                depth += 1;
5240            } else if ch == open {
5241                if depth == 0 {
5242                    return Some((r, c as usize));
5243                }
5244                depth -= 1;
5245            }
5246            c -= 1;
5247        }
5248        if r == 0 {
5249            return None;
5250        }
5251        r -= 1;
5252        c = lines[r].chars().count() as isize - 1;
5253    }
5254}
5255
5256fn find_close_bracket(
5257    lines: &[String],
5258    row: usize,
5259    start_col: usize,
5260    open: char,
5261    close: char,
5262) -> Option<(usize, usize)> {
5263    let mut depth: i32 = 0;
5264    let mut r = row;
5265    let mut c = start_col;
5266    loop {
5267        let cur = &lines[r];
5268        let chars: Vec<char> = cur.chars().collect();
5269        while c < chars.len() {
5270            let ch = chars[c];
5271            if ch == open {
5272                depth += 1;
5273            } else if ch == close {
5274                if depth == 0 {
5275                    return Some((r, c));
5276                }
5277                depth -= 1;
5278            }
5279            c += 1;
5280        }
5281        if r + 1 >= lines.len() {
5282            return None;
5283        }
5284        r += 1;
5285        c = 0;
5286    }
5287}
5288
5289/// Forward scan from `(row, col)` for the next occurrence of `open`.
5290/// Multi-line. Used by bracket text objects to support targets.vim-style
5291/// "search forward when not currently inside a pair" behaviour.
5292fn find_next_open(lines: &[String], row: usize, col: usize, open: char) -> Option<(usize, usize)> {
5293    let mut r = row;
5294    let mut c = col;
5295    while r < lines.len() {
5296        let chars: Vec<char> = lines[r].chars().collect();
5297        while c < chars.len() {
5298            if chars[c] == open {
5299                return Some((r, c));
5300            }
5301            c += 1;
5302        }
5303        r += 1;
5304        c = 0;
5305    }
5306    None
5307}
5308
5309fn advance_pos(lines: &[String], pos: (usize, usize)) -> (usize, usize) {
5310    let (r, c) = pos;
5311    let line_len = lines[r].chars().count();
5312    if c < line_len {
5313        (r, c + 1)
5314    } else if r + 1 < lines.len() {
5315        (r + 1, 0)
5316    } else {
5317        pos
5318    }
5319}
5320
5321fn paragraph_text_object<H: crate::types::Host>(
5322    ed: &Editor<hjkl_buffer::Buffer, H>,
5323    inner: bool,
5324) -> Option<((usize, usize), (usize, usize))> {
5325    let (row, _) = ed.cursor();
5326    let lines = buf_lines_to_vec(&ed.buffer);
5327    if lines.is_empty() {
5328        return None;
5329    }
5330    // A paragraph is a run of non-blank lines.
5331    let is_blank = |r: usize| lines.get(r).map(|s| s.trim().is_empty()).unwrap_or(true);
5332    if is_blank(row) {
5333        return None;
5334    }
5335    let mut top = row;
5336    while top > 0 && !is_blank(top - 1) {
5337        top -= 1;
5338    }
5339    let mut bot = row;
5340    while bot + 1 < lines.len() && !is_blank(bot + 1) {
5341        bot += 1;
5342    }
5343    // For `ap`, include one trailing blank line if present.
5344    if !inner && bot + 1 < lines.len() && is_blank(bot + 1) {
5345        bot += 1;
5346    }
5347    let end_col = lines[bot].chars().count();
5348    Some(((top, 0), (bot, end_col)))
5349}
5350
5351// ─── Individual commands ───────────────────────────────────────────────────
5352
5353/// Read the text in a vim-shaped range without mutating. Used by
5354/// `Operator::Yank` so we can pipe the same range translation as
5355/// [`cut_vim_range`] but skip the delete + inverse extraction.
5356fn read_vim_range<H: crate::types::Host>(
5357    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5358    start: (usize, usize),
5359    end: (usize, usize),
5360    kind: RangeKind,
5361) -> String {
5362    let (top, bot) = order(start, end);
5363    ed.sync_buffer_content_from_textarea();
5364    let lines = buf_lines_to_vec(&ed.buffer);
5365    match kind {
5366        RangeKind::Linewise => {
5367            let lo = top.0;
5368            let hi = bot.0.min(lines.len().saturating_sub(1));
5369            let mut text = lines[lo..=hi].join("\n");
5370            text.push('\n');
5371            text
5372        }
5373        RangeKind::Inclusive | RangeKind::Exclusive => {
5374            let inclusive = matches!(kind, RangeKind::Inclusive);
5375            // Walk row-by-row collecting chars in `[top, end_exclusive)`.
5376            let mut out = String::new();
5377            for row in top.0..=bot.0 {
5378                let line = lines.get(row).map(String::as_str).unwrap_or("");
5379                let lo = if row == top.0 { top.1 } else { 0 };
5380                let hi_unclamped = if row == bot.0 {
5381                    if inclusive { bot.1 + 1 } else { bot.1 }
5382                } else {
5383                    line.chars().count() + 1
5384                };
5385                let row_chars: Vec<char> = line.chars().collect();
5386                let hi = hi_unclamped.min(row_chars.len());
5387                if lo < hi {
5388                    out.push_str(&row_chars[lo..hi].iter().collect::<String>());
5389                }
5390                if row < bot.0 {
5391                    out.push('\n');
5392                }
5393            }
5394            out
5395        }
5396    }
5397}
5398
5399/// Cut a vim-shaped range through the Buffer edit funnel and return
5400/// the deleted text. Translates vim's `RangeKind`
5401/// (Linewise/Inclusive/Exclusive) into the buffer's
5402/// `hjkl_buffer::MotionKind` (Line/Char) and applies the right end-
5403/// position adjustment so inclusive motions actually include the bot
5404/// cell. Pushes the cut text into both `last_yank` and the textarea
5405/// yank buffer (still observed by `p`/`P` until the paste path is
5406/// ported), and updates `yank_linewise` for linewise cuts.
5407fn cut_vim_range<H: crate::types::Host>(
5408    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5409    start: (usize, usize),
5410    end: (usize, usize),
5411    kind: RangeKind,
5412) -> String {
5413    use hjkl_buffer::{Edit, MotionKind as BufKind, Position};
5414    let (top, bot) = order(start, end);
5415    ed.sync_buffer_content_from_textarea();
5416    let (buf_start, buf_end, buf_kind) = match kind {
5417        RangeKind::Linewise => (
5418            Position::new(top.0, 0),
5419            Position::new(bot.0, 0),
5420            BufKind::Line,
5421        ),
5422        RangeKind::Inclusive => {
5423            let line_chars = buf_line_chars(&ed.buffer, bot.0);
5424            // Advance one cell past `bot` so the buffer's exclusive
5425            // `cut_chars` actually drops the inclusive endpoint. Wrap
5426            // to the next row when bot already sits on the last char.
5427            let next = if bot.1 < line_chars {
5428                Position::new(bot.0, bot.1 + 1)
5429            } else if bot.0 + 1 < buf_row_count(&ed.buffer) {
5430                Position::new(bot.0 + 1, 0)
5431            } else {
5432                Position::new(bot.0, line_chars)
5433            };
5434            (Position::new(top.0, top.1), next, BufKind::Char)
5435        }
5436        RangeKind::Exclusive => (
5437            Position::new(top.0, top.1),
5438            Position::new(bot.0, bot.1),
5439            BufKind::Char,
5440        ),
5441    };
5442    let inverse = ed.mutate_edit(Edit::DeleteRange {
5443        start: buf_start,
5444        end: buf_end,
5445        kind: buf_kind,
5446    });
5447    let text = match inverse {
5448        Edit::InsertStr { text, .. } => text,
5449        _ => String::new(),
5450    };
5451    if !text.is_empty() {
5452        ed.record_yank_to_host(text.clone());
5453        ed.record_delete(text.clone(), matches!(kind, RangeKind::Linewise));
5454    }
5455    ed.push_buffer_cursor_to_textarea();
5456    text
5457}
5458
5459/// `D` / `C` — delete from cursor to end of line through the edit
5460/// funnel. Mirrors the deleted text into both `ed.last_yank` and the
5461/// textarea's yank buffer (still observed by `p`/`P` until the paste
5462/// path is ported). Cursor lands at the deletion start so the caller
5463/// can decide whether to step it left (`D`) or open insert mode (`C`).
5464fn delete_to_eol<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5465    use hjkl_buffer::{Edit, MotionKind, Position};
5466    ed.sync_buffer_content_from_textarea();
5467    let cursor = buf_cursor_pos(&ed.buffer);
5468    let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5469    if cursor.col >= line_chars {
5470        return;
5471    }
5472    let inverse = ed.mutate_edit(Edit::DeleteRange {
5473        start: cursor,
5474        end: Position::new(cursor.row, line_chars),
5475        kind: MotionKind::Char,
5476    });
5477    if let Edit::InsertStr { text, .. } = inverse
5478        && !text.is_empty()
5479    {
5480        ed.record_yank_to_host(text.clone());
5481        ed.vim.yank_linewise = false;
5482        ed.set_yank(text);
5483    }
5484    buf_set_cursor_pos(&mut ed.buffer, cursor);
5485    ed.push_buffer_cursor_to_textarea();
5486}
5487
5488fn do_char_delete<H: crate::types::Host>(
5489    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5490    forward: bool,
5491    count: usize,
5492) {
5493    use hjkl_buffer::{Edit, MotionKind, Position};
5494    ed.push_undo();
5495    ed.sync_buffer_content_from_textarea();
5496    // Collect deleted chars so we can write them to the unnamed register
5497    // (vim's `x`/`X` populate `"` so that `xp` round-trips the char).
5498    let mut deleted = String::new();
5499    for _ in 0..count {
5500        let cursor = buf_cursor_pos(&ed.buffer);
5501        let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5502        if forward {
5503            // `x` — delete the char under the cursor. Vim no-ops on
5504            // an empty line; the buffer would drop a row otherwise.
5505            if cursor.col >= line_chars {
5506                continue;
5507            }
5508            let inverse = ed.mutate_edit(Edit::DeleteRange {
5509                start: cursor,
5510                end: Position::new(cursor.row, cursor.col + 1),
5511                kind: MotionKind::Char,
5512            });
5513            if let Edit::InsertStr { text, .. } = inverse {
5514                deleted.push_str(&text);
5515            }
5516        } else {
5517            // `X` — delete the char before the cursor.
5518            if cursor.col == 0 {
5519                continue;
5520            }
5521            let inverse = ed.mutate_edit(Edit::DeleteRange {
5522                start: Position::new(cursor.row, cursor.col - 1),
5523                end: cursor,
5524                kind: MotionKind::Char,
5525            });
5526            if let Edit::InsertStr { text, .. } = inverse {
5527                // X deletes backwards; prepend so the register text
5528                // matches reading order (first deleted char first).
5529                deleted = text + &deleted;
5530            }
5531        }
5532    }
5533    if !deleted.is_empty() {
5534        ed.record_yank_to_host(deleted.clone());
5535        ed.record_delete(deleted, false);
5536    }
5537    ed.push_buffer_cursor_to_textarea();
5538}
5539
5540/// Vim `Ctrl-a` / `Ctrl-x` — find the next decimal number at or after the
5541/// cursor on the current line, add `delta`, leave the cursor on the last
5542/// digit of the result. No-op if the line has no digits to the right.
5543pub(crate) fn adjust_number<H: crate::types::Host>(
5544    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5545    delta: i64,
5546) -> bool {
5547    use hjkl_buffer::{Edit, MotionKind, Position};
5548    ed.sync_buffer_content_from_textarea();
5549    let cursor = buf_cursor_pos(&ed.buffer);
5550    let row = cursor.row;
5551    let chars: Vec<char> = match buf_line(&ed.buffer, row) {
5552        Some(l) => l.chars().collect(),
5553        None => return false,
5554    };
5555    let Some(digit_start) = (cursor.col..chars.len()).find(|&i| chars[i].is_ascii_digit()) else {
5556        return false;
5557    };
5558    let span_start = if digit_start > 0 && chars[digit_start - 1] == '-' {
5559        digit_start - 1
5560    } else {
5561        digit_start
5562    };
5563    let mut span_end = digit_start;
5564    while span_end < chars.len() && chars[span_end].is_ascii_digit() {
5565        span_end += 1;
5566    }
5567    let s: String = chars[span_start..span_end].iter().collect();
5568    let Ok(n) = s.parse::<i64>() else {
5569        return false;
5570    };
5571    let new_s = n.saturating_add(delta).to_string();
5572
5573    ed.push_undo();
5574    let span_start_pos = Position::new(row, span_start);
5575    let span_end_pos = Position::new(row, span_end);
5576    ed.mutate_edit(Edit::DeleteRange {
5577        start: span_start_pos,
5578        end: span_end_pos,
5579        kind: MotionKind::Char,
5580    });
5581    ed.mutate_edit(Edit::InsertStr {
5582        at: span_start_pos,
5583        text: new_s.clone(),
5584    });
5585    let new_len = new_s.chars().count();
5586    buf_set_cursor_rc(&mut ed.buffer, row, span_start + new_len.saturating_sub(1));
5587    ed.push_buffer_cursor_to_textarea();
5588    true
5589}
5590
5591pub(crate) fn replace_char<H: crate::types::Host>(
5592    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5593    ch: char,
5594    count: usize,
5595) {
5596    use hjkl_buffer::{Edit, MotionKind, Position};
5597    ed.push_undo();
5598    ed.sync_buffer_content_from_textarea();
5599    for _ in 0..count {
5600        let cursor = buf_cursor_pos(&ed.buffer);
5601        let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5602        if cursor.col >= line_chars {
5603            break;
5604        }
5605        ed.mutate_edit(Edit::DeleteRange {
5606            start: cursor,
5607            end: Position::new(cursor.row, cursor.col + 1),
5608            kind: MotionKind::Char,
5609        });
5610        ed.mutate_edit(Edit::InsertChar { at: cursor, ch });
5611    }
5612    // Vim leaves the cursor on the last replaced char.
5613    crate::motions::move_left(&mut ed.buffer, 1);
5614    ed.push_buffer_cursor_to_textarea();
5615}
5616
5617fn toggle_case_at_cursor<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5618    use hjkl_buffer::{Edit, MotionKind, Position};
5619    ed.sync_buffer_content_from_textarea();
5620    let cursor = buf_cursor_pos(&ed.buffer);
5621    let Some(c) = buf_line(&ed.buffer, cursor.row).and_then(|l| l.chars().nth(cursor.col)) else {
5622        return;
5623    };
5624    let toggled = if c.is_uppercase() {
5625        c.to_lowercase().next().unwrap_or(c)
5626    } else {
5627        c.to_uppercase().next().unwrap_or(c)
5628    };
5629    ed.mutate_edit(Edit::DeleteRange {
5630        start: cursor,
5631        end: Position::new(cursor.row, cursor.col + 1),
5632        kind: MotionKind::Char,
5633    });
5634    ed.mutate_edit(Edit::InsertChar {
5635        at: cursor,
5636        ch: toggled,
5637    });
5638}
5639
5640fn join_line<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5641    use hjkl_buffer::{Edit, Position};
5642    ed.sync_buffer_content_from_textarea();
5643    let row = buf_cursor_pos(&ed.buffer).row;
5644    if row + 1 >= buf_row_count(&ed.buffer) {
5645        return;
5646    }
5647    let cur_line = buf_line(&ed.buffer, row).unwrap_or("").to_string();
5648    let next_raw = buf_line(&ed.buffer, row + 1).unwrap_or("").to_string();
5649    let next_trimmed = next_raw.trim_start();
5650    let cur_chars = cur_line.chars().count();
5651    let next_chars = next_raw.chars().count();
5652    // `J` inserts a single space iff both sides are non-empty after
5653    // stripping the next line's leading whitespace.
5654    let separator = if !cur_line.is_empty() && !next_trimmed.is_empty() {
5655        " "
5656    } else {
5657        ""
5658    };
5659    let joined = format!("{cur_line}{separator}{next_trimmed}");
5660    ed.mutate_edit(Edit::Replace {
5661        start: Position::new(row, 0),
5662        end: Position::new(row + 1, next_chars),
5663        with: joined,
5664    });
5665    // Vim parks the cursor on the inserted space — or at the join
5666    // point when no space went in (which is the same column either
5667    // way, since the space sits exactly at `cur_chars`).
5668    buf_set_cursor_rc(&mut ed.buffer, row, cur_chars);
5669    ed.push_buffer_cursor_to_textarea();
5670}
5671
5672/// `gJ` — join the next line onto the current one without inserting a
5673/// separating space or stripping leading whitespace.
5674fn join_line_raw<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5675    use hjkl_buffer::Edit;
5676    ed.sync_buffer_content_from_textarea();
5677    let row = buf_cursor_pos(&ed.buffer).row;
5678    if row + 1 >= buf_row_count(&ed.buffer) {
5679        return;
5680    }
5681    let join_col = buf_line_chars(&ed.buffer, row);
5682    ed.mutate_edit(Edit::JoinLines {
5683        row,
5684        count: 1,
5685        with_space: false,
5686    });
5687    // Vim leaves the cursor at the join point (end of original line).
5688    buf_set_cursor_rc(&mut ed.buffer, row, join_col);
5689    ed.push_buffer_cursor_to_textarea();
5690}
5691
5692fn do_paste<H: crate::types::Host>(
5693    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5694    before: bool,
5695    count: usize,
5696) {
5697    use hjkl_buffer::{Edit, Position};
5698    ed.push_undo();
5699    // Resolve the source register: `"reg` prefix (consumed) or the
5700    // unnamed register otherwise. Read text + linewise from the
5701    // selected slot rather than the global `vim.yank_linewise` so
5702    // pasting from `"0` after a delete still uses the yank's layout.
5703    let selector = ed.vim.pending_register.take();
5704    let (yank, linewise) = match selector.and_then(|c| ed.registers().read(c)) {
5705        Some(slot) => (slot.text.clone(), slot.linewise),
5706        // Read both fields from the unnamed slot rather than mixing the
5707        // slot's text with `vim.yank_linewise`. The cached vim flag is
5708        // per-editor, so a register imported from another editor (e.g.
5709        // cross-buffer yank/paste) carried the wrong linewise without
5710        // this — pasting a linewise yank inserted at the char cursor.
5711        None => {
5712            let s = &ed.registers().unnamed;
5713            (s.text.clone(), s.linewise)
5714        }
5715    };
5716    // Vim `:h '[` / `:h ']`: after paste `[` = first inserted char of
5717    // the final paste, `]` = last inserted char of the final paste.
5718    // We track (lo, hi) across iterations; the last value wins.
5719    let mut paste_mark: Option<((usize, usize), (usize, usize))> = None;
5720    for _ in 0..count {
5721        ed.sync_buffer_content_from_textarea();
5722        let yank = yank.clone();
5723        if yank.is_empty() {
5724            continue;
5725        }
5726        if linewise {
5727            // Linewise paste: insert payload as fresh row(s) above
5728            // (`P`) or below (`p`) the cursor's row. Cursor lands on
5729            // the first non-blank of the first pasted line.
5730            let text = yank.trim_matches('\n').to_string();
5731            let row = buf_cursor_pos(&ed.buffer).row;
5732            let target_row = if before {
5733                ed.mutate_edit(Edit::InsertStr {
5734                    at: Position::new(row, 0),
5735                    text: format!("{text}\n"),
5736                });
5737                row
5738            } else {
5739                let line_chars = buf_line_chars(&ed.buffer, row);
5740                ed.mutate_edit(Edit::InsertStr {
5741                    at: Position::new(row, line_chars),
5742                    text: format!("\n{text}"),
5743                });
5744                row + 1
5745            };
5746            buf_set_cursor_rc(&mut ed.buffer, target_row, 0);
5747            crate::motions::move_first_non_blank(&mut ed.buffer);
5748            ed.push_buffer_cursor_to_textarea();
5749            // Linewise: `[` = (target_row, 0), `]` = (bot_row, last_col).
5750            let payload_lines = text.lines().count().max(1);
5751            let bot_row = target_row + payload_lines - 1;
5752            let bot_last_col = buf_line_chars(&ed.buffer, bot_row).saturating_sub(1);
5753            paste_mark = Some(((target_row, 0), (bot_row, bot_last_col)));
5754        } else {
5755            // Charwise paste. `P` inserts at cursor (shifting cell
5756            // right); `p` inserts after cursor (advance one cell
5757            // first, clamped to the end of the line).
5758            let cursor = buf_cursor_pos(&ed.buffer);
5759            let at = if before {
5760                cursor
5761            } else {
5762                let line_chars = buf_line_chars(&ed.buffer, cursor.row);
5763                Position::new(cursor.row, (cursor.col + 1).min(line_chars))
5764            };
5765            ed.mutate_edit(Edit::InsertStr {
5766                at,
5767                text: yank.clone(),
5768            });
5769            // Vim parks the cursor on the last char of the pasted
5770            // text (do_insert_str leaves it one past the end).
5771            crate::motions::move_left(&mut ed.buffer, 1);
5772            ed.push_buffer_cursor_to_textarea();
5773            // Charwise: `[` = insert start, `]` = cursor (last pasted char).
5774            let lo = (at.row, at.col);
5775            let hi = ed.cursor();
5776            paste_mark = Some((lo, hi));
5777        }
5778    }
5779    if let Some((lo, hi)) = paste_mark {
5780        ed.set_mark('[', lo);
5781        ed.set_mark(']', hi);
5782    }
5783    // Any paste re-anchors the sticky column to the new cursor position.
5784    ed.sticky_col = Some(buf_cursor_pos(&ed.buffer).col);
5785}
5786
5787pub(crate) fn do_undo<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5788    if let Some((lines, cursor)) = ed.undo_stack.pop() {
5789        let current = ed.snapshot();
5790        ed.redo_stack.push(current);
5791        ed.restore(lines, cursor);
5792    }
5793    ed.vim.mode = Mode::Normal;
5794    // The restored cursor came from a snapshot taken in insert mode
5795    // (before the insert started) and may be past the last valid
5796    // normal-mode column. Clamp it now, same as Esc-from-insert does.
5797    clamp_cursor_to_normal_mode(ed);
5798}
5799
5800pub(crate) fn do_redo<H: crate::types::Host>(ed: &mut Editor<hjkl_buffer::Buffer, H>) {
5801    if let Some((lines, cursor)) = ed.redo_stack.pop() {
5802        let current = ed.snapshot();
5803        ed.undo_stack.push(current);
5804        ed.cap_undo();
5805        ed.restore(lines, cursor);
5806    }
5807    ed.vim.mode = Mode::Normal;
5808}
5809
5810// ─── Dot repeat ────────────────────────────────────────────────────────────
5811
5812/// Replay-side helper: insert `text` at the cursor through the
5813/// edit funnel, then leave insert mode (the original change ended
5814/// with Esc, so the dot-repeat must end the same way — including
5815/// the cursor step-back vim does on Esc-from-insert).
5816fn replay_insert_and_finish<H: crate::types::Host>(
5817    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5818    text: &str,
5819) {
5820    use hjkl_buffer::{Edit, Position};
5821    let cursor = ed.cursor();
5822    ed.mutate_edit(Edit::InsertStr {
5823        at: Position::new(cursor.0, cursor.1),
5824        text: text.to_string(),
5825    });
5826    if ed.vim.insert_session.take().is_some() {
5827        if ed.cursor().1 > 0 {
5828            crate::motions::move_left(&mut ed.buffer, 1);
5829            ed.push_buffer_cursor_to_textarea();
5830        }
5831        ed.vim.mode = Mode::Normal;
5832    }
5833}
5834
5835pub(crate) fn replay_last_change<H: crate::types::Host>(
5836    ed: &mut Editor<hjkl_buffer::Buffer, H>,
5837    outer_count: usize,
5838) {
5839    let Some(change) = ed.vim.last_change.clone() else {
5840        return;
5841    };
5842    ed.vim.replaying = true;
5843    let scale = if outer_count > 0 { outer_count } else { 1 };
5844    match change {
5845        LastChange::OpMotion {
5846            op,
5847            motion,
5848            count,
5849            inserted,
5850        } => {
5851            let total = count.max(1) * scale;
5852            apply_op_with_motion(ed, op, &motion, total);
5853            if let Some(text) = inserted {
5854                replay_insert_and_finish(ed, &text);
5855            }
5856        }
5857        LastChange::OpTextObj {
5858            op,
5859            obj,
5860            inner,
5861            inserted,
5862        } => {
5863            apply_op_with_text_object(ed, op, obj, inner);
5864            if let Some(text) = inserted {
5865                replay_insert_and_finish(ed, &text);
5866            }
5867        }
5868        LastChange::LineOp {
5869            op,
5870            count,
5871            inserted,
5872        } => {
5873            let total = count.max(1) * scale;
5874            execute_line_op(ed, op, total);
5875            if let Some(text) = inserted {
5876                replay_insert_and_finish(ed, &text);
5877            }
5878        }
5879        LastChange::CharDel { forward, count } => {
5880            do_char_delete(ed, forward, count * scale);
5881        }
5882        LastChange::ReplaceChar { ch, count } => {
5883            replace_char(ed, ch, count * scale);
5884        }
5885        LastChange::ToggleCase { count } => {
5886            for _ in 0..count * scale {
5887                ed.push_undo();
5888                toggle_case_at_cursor(ed);
5889            }
5890        }
5891        LastChange::JoinLine { count } => {
5892            for _ in 0..count * scale {
5893                ed.push_undo();
5894                join_line(ed);
5895            }
5896        }
5897        LastChange::Paste { before, count } => {
5898            do_paste(ed, before, count * scale);
5899        }
5900        LastChange::DeleteToEol { inserted } => {
5901            use hjkl_buffer::{Edit, Position};
5902            ed.push_undo();
5903            delete_to_eol(ed);
5904            if let Some(text) = inserted {
5905                let cursor = ed.cursor();
5906                ed.mutate_edit(Edit::InsertStr {
5907                    at: Position::new(cursor.0, cursor.1),
5908                    text,
5909                });
5910            }
5911        }
5912        LastChange::OpenLine { above, inserted } => {
5913            use hjkl_buffer::{Edit, Position};
5914            ed.push_undo();
5915            ed.sync_buffer_content_from_textarea();
5916            let row = buf_cursor_pos(&ed.buffer).row;
5917            if above {
5918                ed.mutate_edit(Edit::InsertStr {
5919                    at: Position::new(row, 0),
5920                    text: "\n".to_string(),
5921                });
5922                let folds = crate::buffer_impl::SnapshotFoldProvider::from_buffer(&ed.buffer);
5923                crate::motions::move_up(&mut ed.buffer, &folds, 1, &mut ed.sticky_col);
5924            } else {
5925                let line_chars = buf_line_chars(&ed.buffer, row);
5926                ed.mutate_edit(Edit::InsertStr {
5927                    at: Position::new(row, line_chars),
5928                    text: "\n".to_string(),
5929                });
5930            }
5931            ed.push_buffer_cursor_to_textarea();
5932            let cursor = ed.cursor();
5933            ed.mutate_edit(Edit::InsertStr {
5934                at: Position::new(cursor.0, cursor.1),
5935                text: inserted,
5936            });
5937        }
5938        LastChange::InsertAt {
5939            entry,
5940            inserted,
5941            count,
5942        } => {
5943            use hjkl_buffer::{Edit, Position};
5944            ed.push_undo();
5945            match entry {
5946                InsertEntry::I => {}
5947                InsertEntry::ShiftI => move_first_non_whitespace(ed),
5948                InsertEntry::A => {
5949                    crate::motions::move_right_to_end(&mut ed.buffer, 1);
5950                    ed.push_buffer_cursor_to_textarea();
5951                }
5952                InsertEntry::ShiftA => {
5953                    crate::motions::move_line_end(&mut ed.buffer);
5954                    crate::motions::move_right_to_end(&mut ed.buffer, 1);
5955                    ed.push_buffer_cursor_to_textarea();
5956                }
5957            }
5958            for _ in 0..count.max(1) {
5959                let cursor = ed.cursor();
5960                ed.mutate_edit(Edit::InsertStr {
5961                    at: Position::new(cursor.0, cursor.1),
5962                    text: inserted.clone(),
5963                });
5964            }
5965        }
5966    }
5967    ed.vim.replaying = false;
5968}
5969
5970// ─── Extracting inserted text for replay ───────────────────────────────────
5971
5972fn extract_inserted(before: &str, after: &str) -> String {
5973    let before_chars: Vec<char> = before.chars().collect();
5974    let after_chars: Vec<char> = after.chars().collect();
5975    if after_chars.len() <= before_chars.len() {
5976        return String::new();
5977    }
5978    let prefix = before_chars
5979        .iter()
5980        .zip(after_chars.iter())
5981        .take_while(|(a, b)| a == b)
5982        .count();
5983    let max_suffix = before_chars.len() - prefix;
5984    let suffix = before_chars
5985        .iter()
5986        .rev()
5987        .zip(after_chars.iter().rev())
5988        .take(max_suffix)
5989        .take_while(|(a, b)| a == b)
5990        .count();
5991    after_chars[prefix..after_chars.len() - suffix]
5992        .iter()
5993        .collect()
5994}
5995
5996// ─── Tests ────────────────────────────────────────────────────────────────