Skip to main content

escriba_runtime/
lib.rs

1//! `escriba-runtime` — editor state machine.
2//!
3//! Wraps everything: `BufferSet`, `ModalState`, `Keymap`, `CommandRegistry`,
4//! `Layout`. Exposes `tick(input)` which advances one frame's worth of
5//! state given one input event. Pure — no rendering, no I/O beyond file
6//! save/load through `BufferSet`.
7
8extern crate self as escriba_runtime;
9
10mod plugin_host;
11pub use plugin_host::{LazyTrigger, PluginHost};
12
13mod operator_pending;
14pub mod status;
15
16pub use operator_pending::{OpState, OperatorPending};
17
18/// What one key meant to the operator-pending object layer.
19enum ObjectKey {
20    /// Swallowed — the key began an object and nothing runs yet.
21    Consumed,
22    /// The object is complete; run this.
23    Compose(Action),
24}
25pub use status::{PromptKind, StatusModel};
26
27use std::collections::HashMap;
28
29use awase::KeyRepeatGate;
30use escriba_buffer::BufferSet;
31use escriba_buffer::TextRev;
32use escriba_command::CommandRegistry;
33use escriba_core::{
34    Action, Anchored, Bound, BufferId, Cursors, Damage, Edit, EditGen, HighlightEffect, JumpList,
35    Mode, Motion, Operator, Position, Range, TextEffect, WindowId,
36};
37use escriba_input::{InputOutcome, translate_app_event};
38use escriba_keymap::{Key, Keymap};
39use escriba_madoguchi::{Negai, Outcome};
40use escriba_mode::ModalState;
41use escriba_search::{Direction as SearchDirection, MatchCount, SearchState};
42use escriba_ui::chrome::{ChromePalette, FleetTheme};
43use escriba_ui::splash::Splash;
44use escriba_ui::{Layout, Viewport, Window};
45use escriba_vm::{EditorSnapshot, EscribaHost, EscribaVm, VmError};
46use madori::AppEvent;
47use std::time::Instant;
48
49/// Full editor state — the single Rust value the binary hands to the
50/// renderer each frame.
51pub struct EditorState {
52    pub buffers: BufferSet,
53    pub modal: ModalState,
54    /// Search session — the committed pattern, its matches, the live `/`
55    /// prompt and history. Owns no buffer or cursor; it answers questions
56    /// about text and this runtime applies the answers.
57    pub search: SearchState,
58    pub keymap: Keymap,
59    pub commands: CommandRegistry,
60    pub layout: Layout,
61    pub active: BufferId,
62    /// The single typed home for cursor state. Phase-1 holds one primary
63    /// [`Position`]; reads go through [`Self::cursor`], writes through
64    /// [`Self::set_cursor`] → [`Cursors::set_primary`]. There is no loose
65    /// `Position` field beside an unused multi-caret type to desync.
66    cursors: Cursors,
67    pub quit_requested: bool,
68    /// Messages surfaced to the user (status line / `:messages`) — the
69    /// sink for the tatara-lisp `(message …)` effect and other feedback.
70    pub messages: Vec<String>,
71    /// Which match the cursor last landed on (0-based) — the `[3/17]`
72    /// numerator, ANCHORED to the text revision it was computed against.
73    ///
74    /// The anchor is what removes the manual invalidation this field used to
75    /// need. An ordinal indexes a match set; when the text changes the set
76    /// changes underneath it and the number silently means something else.
77    /// Reading through `Anchored::get(current_rev)` makes that a `None`, so
78    /// forgetting to clear is no longer a thing that can be forgotten.
79    search_at: Option<Anchored<usize, TextRev>>,
80    /// The last text change, for `.`.
81    ///
82    /// An action plus whatever was typed while it held Insert open. Both
83    /// halves are needed: `cw` alone is not a change, it is the FIRST HALF of
84    /// one — the text that followed is the rest, and replaying without it
85    /// would delete a word and leave the buffer in Insert.
86    last_change: Option<LastChange>,
87    /// True while an insert session belonging to `last_change` is open, so
88    /// typed characters are appended to it. Cleared on leaving Insert.
89    recording_insert: bool,
90
91    /// Where the cursor was before each far jump — `<C-o>` / `<C-i>`.
92    /// Search commits, `n`/`N` and `*`/`#` all record into it, which is what
93    /// makes a search a place you can come back from.
94    pub jumps: JumpList,
95    /// Generic editor option store (name → value). Written by the
96    /// tatara-lisp `(set-option …)` effect and the declarative
97    /// `defoption` apply path; typed accessors layer on top later.
98    pub options: HashMap<String, String>,
99    /// Cached embedded tatara-lisp runtime, built lazily on first
100    /// `run_lisp`. Caching avoids re-installing the ~175-definition full
101    /// stdlib on every call; the interpreter's top-level env also
102    /// persists across calls, giving REPL-like session semantics (an
103    /// earlier `(define …)` is visible to a later `run_lisp`).
104    lisp_vm: Option<EscribaVm>,
105    /// Keys accumulated for an in-progress multi-key sequence — e.g.
106    /// holding `[,, f]` while waiting for the final key of
107    /// `<leader>ff`. Empty when not mid-sequence. Lives on
108    /// `EditorState` (not `ModalState`) so `escriba-mode` needn't
109    /// depend on `escriba-keymap`'s `Key`.
110    pub pending_keys: Vec<Key>,
111    /// Per-key debouncer for OS key-repeat storms. Holding `j`/`l` makes
112    /// the windowing system deliver one `KeyDown` per repeat tick
113    /// (~30-50ms); without a gate those flood the motion path and thrash
114    /// the viewport. The gate lets ONE event per `min_interval` (80ms
115    /// default — ~12 intentional taps/sec still pass) reach the editor in
116    /// the navigation modes. The fleet primitive (`awase::KeyRepeatGate`,
117    /// the same one mado uses) is reused — not reinvented.
118    repeat_gate: KeyRepeatGate<Key>,
119    /// Runtime lazy-activation host for USER plugin caixas (the bundled
120    /// default catalog is applied eagerly at boot, not through here).
121    /// A command / filetype-open / event fires the matching plugins'
122    /// entries through the escriba-lisp apply paths. See [`PluginHost`].
123    pub plugin_host: PluginHost,
124    /// The unnamed register — the home for text an operator yanks or
125    /// deletes (`Operator::leaves_register`). `None` until the first
126    /// register-leaving operator runs. Phase-1 holds the single unnamed
127    /// register; named registers (`"ay`) layer on later.
128    register: Option<String>,
129    /// The operator-pending FSM (`d`/`c`/`y` then a motion → `dw`/`c$`/`y0`),
130    /// standing on the fleet `zenmai` Mealy-machine primitive. Every dispatched
131    /// action passes through it; only an operator-then-motion pair is rewritten
132    /// into an [`Action::ApplyOperator`].
133    op_pending: zenmai::Stateful<OperatorPending>,
134    /// Operator-pending OBJECT selection, held at the KEY layer.
135    ///
136    /// `Some(around)` means `d` + `i`/`a` have been pressed and the NEXT key
137    /// names the object. It lives here rather than in the operator FSM
138    /// because the FSM sees `Action`s and this decision needs the KEY: `a`
139    /// and every bracket are unbound in Normal, so they all arrive as
140    /// `Action::Pending` with the character already discarded. vim has a
141    /// whole operator-pending keymap for the same reason.
142    pending_object: Option<bool>,
143    /// Monotonic refresh-generation stamp — the root of the sealed refresh
144    /// tree (`theory/ESCRIBA.md` §Refresh-Seal). Bumped on every applied
145    /// action + resize; the renderer gates on it so an idle frame does zero
146    /// re-highlight / re-shape, and a stale frame is unreachable.
147    edit_gen: EditGen,
148    /// The accumulated dirty region since the renderer last drained it (M1).
149    /// Only ever widened via [`Damage::join`] at the mutation funnel, so it
150    /// always covers the changed region (`Damage ⊇ changed`); the renderer
151    /// drains it with [`take_damage`](Self::take_damage) to scope its work.
152    damage: Damage,
153    /// The theme every face paints with.
154    ///
155    /// ONE owner. Before this, `(deftheme :preset …)` parsed, validated,
156    /// resolved to a real `FleetTheme` — and then nothing consumed it,
157    /// because each renderer called `ChromePalette::prescribed()` at every
158    /// paint site. The declaration was honoured on paper only. Holding it
159    /// here means a face reads the operator's theme the same way it reads
160    /// the cursor: from the state, per frame.
161    theme: FleetTheme,
162    /// `theme` resolved to concrete colours — cached because it is a plain
163    /// `Copy` struct read many times per frame, and re-derived only in
164    /// [`set_theme`](Self::set_theme), so the two cannot disagree.
165    chrome: ChromePalette,
166    /// How deep the current command dispatch is nested.
167    ///
168    /// `Negai::RunCommand` lets a command invoke a command, which is useful
169    /// and which can also recurse forever. The budget makes the runaway
170    /// bounded and REPORTED rather than a stack overflow — the difference
171    /// between a typed refusal and the editor dying under the operator.
172    dispatch_depth: u8,
173    /// Every live result list — diagnostics, hunks, grep hits, TODOs.
174    ///
175    /// Public so a producer outside the runtime can publish into it once the
176    /// courier lands; today the only producer is the marker scan.
177    pub results: escriba_shirube::ListRegistry,
178    /// The open picker, if any.
179    ///
180    /// `Option<Picker>` on the state, exactly like `splash` — deliberately
181    /// NOT a `Mode` variant. A mode is a state keys are interpreted IN; this
182    /// is a surface that OWNS keys while it is up, which is a different
183    /// thing and composes differently with the keymap.
184    picker: Option<escriba_ui::picker::Picker>,
185    /// The git-index generation. See [`world`](Self::world) — every axis the
186    /// world can move is emitted unconditionally, so a producer anchoring on
187    /// one is not born permanently stale.
188    index_rev: escriba_shirube::IndexRev,
189    /// The external-session generation (LSP restart, debug session, test run).
190    session_gen: escriba_shirube::SessionGen,
191    /// Extension → language facts, populated from `(defmode …)`.
192    ///
193    /// The consumer `:commentstring` never had. Public so the binary's apply
194    /// pass can fill it the way it fills the keymap and the option store.
195    pub filetypes: escriba_core::FiletypeTable,
196    /// The start screen, while it is up.
197    ///
198    /// `Some` only between boot and the first keypress, and only when the
199    /// editor opened with no file. It is deliberately NOT a `Mode`: a mode
200    /// is a state keys are interpreted *in*, and the splash interprets
201    /// exactly one key before it is gone. Modelling it as `Option<Splash>`
202    /// keeps the modal state machine's variant set — and every exhaustive
203    /// match over it — untouched.
204    splash: Option<Splash>,
205}
206
207/// What the start screen did with a keypress.
208///
209/// Total, and matched exhaustively at its one call site, so a future
210/// outcome (a menu that opens a submenu, say) is a compile error rather
211/// than a key that silently falls through to the buffer.
212enum SplashKey {
213    /// No start screen is up — the key is the buffer's.
214    NotShowing,
215    /// The key selected a menu entry; run this.
216    Ran(Action),
217    /// The screen is gone and the key was not a menu key, so it still
218    /// means whatever it normally means. Anything else would make the
219    /// first keystroke after boot vanish.
220    Dismissed,
221}
222
223// ─── The counter, and the one place slips become mutations ───────────────
224
225/// `EditorState` read through the counter.
226///
227/// Borrowed, never copied: building it is free, so a command dispatch does
228/// not pay for a snapshot of the buffers.
229pub struct EditorWindow<'a> {
230    state: &'a EditorState,
231}
232
233impl escriba_madoguchi::CursorView for EditorWindow<'_> {
234    fn position(&self) -> Position {
235        self.state.cursor()
236    }
237    fn mode(&self) -> Mode {
238        self.state.modal.mode()
239    }
240}
241
242impl escriba_madoguchi::SyntaxView for EditorWindow<'_> {
243    fn filetype(&self) -> Option<&escriba_core::Filetype> {
244        let path = self.state.buffers.get(self.state.active)?.path.as_deref()?;
245        self.state.filetypes.resolve(path)
246    }
247}
248
249impl escriba_madoguchi::SearchView for EditorWindow<'_> {
250    fn pattern(&self) -> Option<&str> {
251        self.state.search.committed_pattern()
252    }
253    fn match_count(&self) -> Option<usize> {
254        // `None` means "nothing committed", which is not the same as zero
255        // matches — a distinction the status line already makes and that a
256        // handler must not have to re-derive.
257        self.state
258            .search
259            .committed_pattern()
260            .map(|_| self.state.search.match_count())
261    }
262    fn is_prompting(&self) -> bool {
263        self.state.search.is_prompting()
264    }
265}
266
267impl escriba_madoguchi::Snapshot for EditorWindow<'_> {
268    fn active(&self) -> Option<&dyn escriba_madoguchi::BufferView> {
269        self.buffer(self.state.active)
270    }
271    fn buffer(&self, id: BufferId) -> Option<&dyn escriba_madoguchi::BufferView> {
272        self.state
273            .buffers
274            .get(id)
275            .map(|b| b as &dyn escriba_madoguchi::BufferView)
276    }
277    fn buffer_ids(&self) -> Vec<BufferId> {
278        self.state.buffers.ids()
279    }
280    fn cursor(&self) -> &dyn escriba_madoguchi::CursorView {
281        self
282    }
283    fn option(&self, name: &str) -> Option<&str> {
284        self.state.options.get(name).map(String::as_str)
285    }
286    fn search(&self) -> &dyn escriba_madoguchi::SearchView {
287        self
288    }
289    fn syntax(&self) -> &dyn escriba_madoguchi::SyntaxView {
290        self
291    }
292}
293
294impl EditorState {
295    /// A read-only window onto this editor.
296    #[must_use]
297    pub fn window(&self) -> EditorWindow<'_> {
298        EditorWindow { state: self }
299    }
300
301    /// Honour an [`Outcome`] — the ONLY place slips become mutations.
302    ///
303    /// Every `&mut self` in the dispatch path lives here. A command cannot
304    /// reach editor state, so if the editor ends up in a state nobody
305    /// designed, this function is where it happened; that narrowing is the
306    /// whole return on the seam.
307    ///
308    /// A failed outcome's slips are DROPPED rather than half-applied: a
309    /// handler that reported failure has no business also mutating, and
310    /// applying part of what it asked for is how an editor reaches a state
311    /// nobody designed.
312    pub fn interpret(&mut self, outcome: Outcome) {
313        if let Some(m) = outcome.verdict.message() {
314            self.messages.push(m.to_string());
315            self.damage = self.damage.join(Damage::Viewport);
316            self.bump_gen();
317        }
318        if outcome.verdict.is_failure() {
319            return;
320        }
321        for slip in outcome.slips {
322            self.honour(slip);
323        }
324    }
325
326    /// Lower an [`Action`] to slips, when it has an exact slip equivalent.
327    ///
328    /// `None` means "editor mechanics" — 23 of the 30 variants are prompt
329    /// editing, the operator-pending FSM, motion resolution, the jumplist,
330    /// the dot register. Those are the KEYMAP's vocabulary, not the AUTHORED
331    /// one, and forcing them into `Negai` would put `PromptClearToStart` and
332    /// `SearchPreviewStep` in front of every plugin author and make the
333    /// capability question meaningless (what capability does a caret move
334    /// read?). One type serving two vocabularies is the mistake this avoids.
335    ///
336    /// The plan's M3 predicate was "apply_resolved contains zero `self.`
337    /// mutations", which would have forced exactly that. Amended: the
338    /// invariant worth having is ONE IMPLEMENTATION PER MUTATION, not one
339    /// vocabulary. See docs/backlog-plan.md §V Phase 1.
340    fn lower(action: &Action, active: BufferId) -> Option<Vec<Negai>> {
341        Some(match action {
342            Action::Quit => vec![Negai::Quit],
343            Action::ClearSearchHighlight => vec![Negai::ClearSearchHighlight],
344            Action::Save => vec![Negai::Save { buffer: active }],
345            Action::Undo => vec![Negai::Undo { buffer: active }],
346            Action::Redo => vec![Negai::Redo { buffer: active }],
347            // `apply_edit` was a STUB that did nothing, so this action was a
348            // silent no-op while `Negai::Edit` applied for real. Lowering it
349            // makes keymap-originated edits work for the first time — and
350            // nothing binds it today, so the duplication goes away at zero
351            // risk.
352            Action::Edit(edit) => vec![Negai::Edit {
353                buffer: active,
354                edit: edit.clone(),
355            }],
356            _ => return None,
357        })
358    }
359
360    /// What the world currently is, for freshness.
361    ///
362    /// One text axis per open buffer. A list sealed against this is fresh
363    /// exactly while the buffers it depends on are unchanged — and a buffer
364    /// that has since CLOSED drops out, which makes lists about it stale
365    /// rather than silently kept.
366    #[must_use]
367    pub fn world(&self) -> escriba_shirube::Anchor {
368        let mut a = escriba_shirube::Anchor::new();
369        for id in self.buffers.ids() {
370            if let Some(b) = self.buffers.get(id) {
371                a = a.on(escriba_shirube::Axis::Text(id, b.text_rev()));
372            }
373        }
374        // Every axis the world can move, ALWAYS present — not only the ones
375        // some producer happens to use today.
376        //
377        // `Anchor::is_fresh` treats an ABSENT axis as stale, deliberately:
378        // unknowable is not unchanged. The consequence, unnoticed until a
379        // recon pass went looking, is that a list anchored on an axis this
380        // function never emits is born PERMANENTLY stale — `]c` would answer
381        // "that list is out of date" forever, and nothing would say why. The
382        // two-axis model was built for git hunks and then only ever fed one
383        // axis.
384        //
385        // Emitting them unconditionally means a producer can anchor on any
386        // axis and get an honest answer. A counter that never moves reads as
387        // "unchanged", which is exactly right for a plane escriba does not
388        // track yet.
389        a = a.on(escriba_shirube::Axis::Index(self.index_rev));
390        a.on(escriba_shirube::Axis::Session(self.session_gen))
391    }
392
393    /// Where the cursor is, WITH the buffer it is in.
394    ///
395    /// Every jumplist push goes through this. A bare `Position` is what let
396    /// `<C-o>` return to the right line in the wrong file.
397    #[must_use]
398    pub fn spot(&self) -> escriba_core::Spot {
399        escriba_core::Spot::new(self.active, self.cursor())
400    }
401
402    /// Move to a `Spot`, switching buffer if it names another one.
403    ///
404    /// The read half of [`spot`](Self::spot). `<C-o>` and `<C-i>` both land
405    /// here so neither can forget the buffer.
406    fn goto_spot(&mut self, s: escriba_core::Spot) {
407        if s.buffer != self.active && self.buffers.get(s.buffer).is_some() {
408            self.active = s.buffer;
409        }
410        let clamped = self
411            .buffers
412            .get(self.active)
413            .map_or(s.pos, |b| b.clamp(s.pos));
414        self.set_cursor(clamped);
415    }
416
417    /// Advance the git-index generation — every list anchored on
418    /// `Axis::Index` goes stale.
419    ///
420    /// Not called yet; a git layer calls it after a stage/reset. Present so
421    /// the axis is WIRED rather than declared, because an axis nothing can
422    /// move is indistinguishable from an axis that does not exist.
423    pub fn bump_index_rev(&mut self) {
424        self.index_rev = escriba_shirube::IndexRev(self.index_rev.0.wrapping_add(1));
425    }
426
427    /// Advance the external-session generation — LSP restart, debug session,
428    /// test-runner invocation. See [`bump_index_rev`](Self::bump_index_rev).
429    pub fn bump_session_gen(&mut self) {
430        self.session_gen = escriba_shirube::SessionGen(self.session_gen.0.wrapping_add(1));
431    }
432
433    /// Move the cursor to the next/previous finding in `list`.
434    ///
435    /// Reports the wrap, because `n`/`N` do and a reader losing their place
436    /// in a long file is the same problem either way.
437    fn walk_list(&mut self, list: &str, forward: bool) {
438        let world = self.world();
439        let Some(result) = self.results.get(list) else {
440            let mut m = String::from("no list named ");
441            m.push_str(list);
442            self.messages.push(m);
443            return;
444        };
445        if result.is_stale(&world) {
446            self.messages
447                .push("that list is out of date — run it again".to_string());
448            return;
449        }
450        let here = (Some(self.active), self.cursor().line);
451        let Some(found) = result.step(&world, here, forward, escriba_shirube::Bound::Exclusive)
452        else {
453            let mut m = String::from("no entries in ");
454            m.push_str(list);
455            self.messages.push(m);
456            return;
457        };
458        let site = found.site.clone();
459        let msg = found.message.clone();
460        self.jump_to_site(&site);
461        self.messages.push(msg);
462    }
463
464    /// Move the cursor to a located finding's SITE — the one operation that
465    /// cannot drop the buffer half of a location.
466    ///
467    /// A `Site` is `(buffer, range)`. Every jumper before this re-derived the
468    /// move itself and clamped against `self.active`, so a finding in another
469    /// file landed on the right LINE in the WRONG file. `on_line` and
470    /// `worst_on_line` already filter by buffer, so the gutter and the walker
471    /// disagreed — latent only because the first producer scanned one buffer.
472    ///
473    /// Every future producer (diagnostics, hunks, grep hits, test failures)
474    /// is cross-file by nature, which is why this is a shared operation
475    /// rather than a fix at the one call site that has it wrong today.
476    ///
477    /// Always a FAR jump: it pushes the jumplist, so `<C-o>` returns from a
478    /// `]t` exactly as it returns from an `n`.
479    pub fn jump_to_site(&mut self, site: &escriba_shirube::Site) {
480        self.jumps.push(self.spot());
481        // Switch buffers FIRST — clamping against the wrong buffer is how the
482        // position gets silently mangled before anyone can notice.
483        if let Some(target) = site.buffer {
484            if target != self.active && self.buffers.get(target).is_some() {
485                self.active = target;
486                self.refollow_cursor();
487            }
488        }
489        let to = site.range.start;
490        let clamped = self.buffers.get(self.active).map_or(to, |b| b.clamp(to));
491        self.set_cursor(clamped);
492    }
493
494    /// Close a buffer, keeping "there is always an active buffer" true.
495    ///
496    /// The invariant is the whole reason this is not just
497    /// `self.buffers.close(id)`. `EditorState::active` is a `BufferId`, not
498    /// an `Option`, so a dangling active is not a degraded state — it is a
499    /// state where every read of the active buffer returns `None` and the
500    /// editor renders `<no buffer>` forever. Closing the last buffer opens a
501    /// scratch rather than emptying the set, which is what vim's `:bd` does
502    /// and what the type demands.
503    fn close_buffer(&mut self, id: BufferId) {
504        if self.buffers.close(id).is_none() {
505            self.messages.push("no such buffer".to_string());
506            return;
507        }
508        if self.active != id {
509            return;
510        }
511        // The active buffer went. Prefer the next one by id so repeated
512        // closes walk forward predictably rather than jumping around.
513        let next = self.buffers.ids().into_iter().find(|b| *b > id);
514        self.active = match next.or_else(|| self.buffers.ids().into_iter().next_back()) {
515            Some(b) => b,
516            None => self.buffers.scratch(""),
517        };
518        self.set_cursor(Position::ZERO);
519        if let Some(w) = self.layout.active_window_mut() {
520            w.buffer_id = self.active;
521        }
522    }
523
524    /// Move to the next or previous buffer, wrapping.
525    fn cycle_buffer(&mut self, forward: bool) {
526        let ids = self.buffers.ids();
527        if ids.len() < 2 {
528            self.messages.push("only one buffer".to_string());
529            return;
530        }
531        let at = ids.iter().position(|b| *b == self.active).unwrap_or(0);
532        let next = if forward {
533            (at + 1) % ids.len()
534        } else {
535            (at + ids.len() - 1) % ids.len()
536        };
537        self.active = ids[next];
538        self.set_cursor(Position::ZERO);
539        if let Some(w) = self.layout.active_window_mut() {
540            w.buffer_id = self.active;
541        }
542    }
543
544    /// Re-clamp the cursor and re-contain the viewport after a buffer
545    /// mutation.
546    ///
547    /// An undo can SHRINK the buffer under a cursor that was legal a moment
548    /// ago, leaving it out of bounds and its viewport scrolled past the end.
549    /// The Action executor has always done this (`self.set_cursor(self.cursor())`
550    /// after undo/redo/save); the M1 interpreter did NOT, so `u` re-followed
551    /// and `:undo` did not — two implementations of one operation, already
552    /// drifted within one milestone of being written. Naming it once is the
553    /// fix; lowering the Action arms onto the same slips is what keeps it
554    /// fixed.
555    fn refollow(&mut self) {
556        self.set_cursor(self.cursor());
557    }
558
559    /// Apply one slip and record what it damaged.
560    ///
561    /// The bookkeeping wrapper. The Action executor calls
562    /// [`honour_one`](Self::honour_one) directly because it does its own,
563    /// wider bookkeeping (the dot register, the S3 damage seal) around a
564    /// whole action.
565    fn honour(&mut self, slip: Negai) {
566        let touches_text = slip.touches_text();
567        self.honour_one(slip);
568        self.damage = self.damage.join(if touches_text {
569            Damage::Full
570        } else {
571            Damage::Viewport
572        });
573        self.bump_gen();
574    }
575
576    /// Apply one slip. THE single implementation of every mutation a slip
577    /// can ask for.
578    ///
579    /// Total over `Negai`: a new request variant is a compile error here
580    /// rather than a request silently ignored — the same failure Phase 0
581    /// removed one layer up.
582    fn honour_one(&mut self, slip: Negai) {
583        match slip {
584            Negai::Edit { buffer, edit } => {
585                if let Some(b) = self.buffers.get_mut(buffer) {
586                    let _ = b.apply(&edit);
587                }
588                self.refollow();
589            }
590            Negai::SetCursor { buffer, to } => {
591                // Clamping is the interpreter's job, exactly so that no
592                // handler has to re-implement it and get it wrong.
593                let clamped = self.buffers.get(buffer).map_or(to, |b| b.clamp(to));
594                self.set_cursor(clamped);
595            }
596            Negai::EnterMode(m) => self.modal.enter(m),
597            Negai::OpenPicker(source) => self.open_picker(source),
598            Negai::SplitWindow { stacked } => {
599                let axis = if stacked {
600                    escriba_ui::shikiri::Axis::Stacked
601                } else {
602                    escriba_ui::shikiri::Axis::SideBySide
603                };
604                self.layout.split_active(axis);
605                // The new pane is narrower/shorter than the old one, so the
606                // cursor can now be outside it. Every face re-reports its
607                // frame on the next draw, but the invariant must hold NOW —
608                // an operator who splits and immediately types should not be
609                // editing off-screen.
610                self.refollow_cursor();
611                self.damage = self.damage.join(Damage::Viewport);
612            }
613            Negai::CloseWindow => {
614                let id = self.layout.active();
615                if self.layout.close(id) {
616                    self.refollow_cursor();
617                    self.damage = self.damage.join(Damage::Viewport);
618                } else {
619                    // vim's E444, and the same refusal: the last window is
620                    // the editor. Closing it would mean "quit", which is a
621                    // different verb the operator did not type.
622                    self.messages
623                        .push("E444: Cannot close last window".to_string());
624                }
625            }
626            Negai::FocusDir { dx, dy } => {
627                use escriba_ui::Dir;
628                let dir = match (dx, dy) {
629                    (d, _) if d < 0 => Dir::Left,
630                    (d, _) if d > 0 => Dir::Right,
631                    (_, d) if d < 0 => Dir::Up,
632                    _ => Dir::Down,
633                };
634                if let Some(id) = self.layout.neighbour(dir) {
635                    self.layout.focus(id);
636                    // The window we moved to has its OWN buffer; the editor's
637                    // active buffer follows focus, or the next keystroke
638                    // would edit the file we just navigated away from.
639                    if let Some(w) = self.layout.active_window() {
640                        self.active = w.buffer_id;
641                    }
642                    self.refollow_cursor();
643                    self.damage = self.damage.join(Damage::Viewport);
644                }
645                // No neighbour is not an error — it is the edge of the
646                // layout, and vim says nothing there either.
647            }
648            Negai::GrepProject { pattern } => self.grep_project(&pattern),
649            Negai::CycleBuffer { forward } => self.cycle_buffer(forward),
650            Negai::FocusBuffer(id) => {
651                if self.buffers.get(id).is_some() {
652                    self.active = id;
653                }
654            }
655            Negai::OpenPath(path) => match self.buffers.open(&path) {
656                Ok(id) => self.active = id,
657                Err(e) => self.messages.push(e.to_string()),
658            },
659            Negai::CloseBuffer(id) => self.close_buffer(id),
660            Negai::Save { buffer } => {
661                if let Some(b) = self.buffers.get_mut(buffer) {
662                    if let Err(e) = b.save() {
663                        self.messages.push(e.to_string());
664                    }
665                }
666                self.refollow();
667            }
668            Negai::Undo { buffer } => {
669                if let Some(b) = self.buffers.get_mut(buffer) {
670                    let _ = b.undo();
671                }
672                self.refollow();
673            }
674            Negai::Redo { buffer } => {
675                if let Some(b) = self.buffers.get_mut(buffer) {
676                    let _ = b.redo();
677                }
678                self.refollow();
679            }
680            Negai::Yank { text, .. } => self.register = Some(text),
681            Negai::ClearSearchHighlight => self.search.clear_highlight(),
682            Negai::SetOption { name, value } => {
683                self.options.insert(name, value);
684            }
685            Negai::InsertText(text) => self.insert_text(&text),
686            Negai::RunCommand { name, args } => self.run_command(&name, &args),
687            Negai::PublishFindings { list, findings } => {
688                let world = self.world();
689                self.results
690                    .publish(list, escriba_shirube::ResultList::new(findings, world));
691            }
692            Negai::WalkList { list, forward } => self.walk_list(&list, forward),
693            Negai::Message(m) => self.messages.push(m),
694            Negai::Quit => self.quit_requested = true,
695            // Both suspend the dispatch and need machinery that does not
696            // exist yet — the courier (Phase 5) and the AwaitKey resume
697            // (M3). Announced, never silently dropped: a slip that vanishes
698            // is the class Phase 0 sealed.
699            Negai::Errand(_) | Negai::AwaitKey { .. } => {
700                self.messages
701                    .push("deferred work is not wired yet".to_string());
702            }
703        }
704    }
705}
706
707/// Outcome of feeding one key to the multi-key pending-stroke loop.
708enum SeqStep {
709    /// Key consumed into an in-progress sequence; wait for the next.
710    Pending,
711    /// A full bound sequence resolved — run this action.
712    Resolved(Action),
713    /// Key is not part of any sequence; hand it to single-key dispatch.
714    Passthrough,
715}
716
717/// Keys whose HELD repeat is a viewport storm, and which the repeat gate
718/// therefore exists to debounce.
719///
720/// This is an ALLOW-LIST, and it used to be the complement — an exception list
721/// of "discrete" keys that grew three times (`n`/`N`/`*`/`#`, then `.`/`u`/
722/// `<C-r>`, then `/`/`?`/`:`), each time because a key had been silently
723/// swallowed and someone noticed. The third growth is the signal that the
724/// default was backwards: almost every key in a modal editor is a discrete,
725/// deliberate press, and only a handful are ones you HOLD.
726///
727/// Inverting it makes the failure mode safe. Forgetting to list a key here now
728/// means it is ungated — one extra keypress honoured — instead of silently
729/// dropped, and a dropped key is indistinguishable from a dead one.
730///
731/// Measured cost of the old direction: `/foo<CR>` then `/<CR>` (vim's
732/// reuse-the-previous-pattern) lost the second `/` outright, because the gate
733/// is keyed by KEY and the two presses fell inside one debounce window.
734const fn is_repeat_storm_candidate(key: &Key) -> bool {
735    matches!(
736        key,
737        // The four navigation keys a user actually holds down. `h`/`l` and
738        // `j`/`k` flood the motion path and thrash the viewport; everything
739        // else is pressed once and meant once.
740        Key::Char('h')
741            | Key::Char('j')
742            | Key::Char('k')
743            | Key::Char('l')
744            | Key::Left
745            | Key::Right
746            | Key::Up
747            | Key::Down
748    )
749}
750
751/// Turn a command failure into the sentence an operator should read.
752///
753/// The two failures mean genuinely different things and must not be reported
754/// the same way:
755///
756/// - `:flurb` — the operator typed a name that does not exist. "command not
757///   found" is exactly right; it says *you* made a typo.
758/// - `<leader>ff` bound to `picker.files` — escriba's OWN shipped config
759///   declares this, `--list-rc` counts it, and it is not built yet. Telling
760///   the operator "command not found" blames them for a gap we shipped.
761///
762/// The discriminator is the dotted form. `:action` takes action SYMBOLS
763/// (`picker.files`), never command names — that boundary is already pinned by
764/// `action_naming_a_command_is_inert_not_recursive` in escriba-command. So a
765/// dotted name that reached dispatch and resolved to nothing is a declared
766/// capability with no implementation, which is precisely what the 85 entries
767/// in `escriba/tests/action_resolution.rs` are.
768fn describe_command_failure(name: &str, e: &escriba_command::CommandError) -> String {
769    use escriba_command::CommandError as E;
770    match e {
771        // Already the right words — the registry knew it was declared.
772        E::Unhandled(_) => e.to_string(),
773        E::NotFound(n) if n.contains('.') => {
774            let mut m = String::with_capacity(n.len() + 48);
775            m.push('`');
776            m.push_str(n);
777            m.push_str("` is declared but not implemented yet");
778            m
779        }
780        _ => {
781            let _ = name;
782            e.to_string()
783        }
784    }
785}
786
787/// What committing the open search prompt did.
788///
789/// The two commit paths — bare `/` and operated `d/` — used to own private
790/// copies of the whole sequence (read origin+skip, `accept`, three-arm
791/// match, `commit_step_skipping`), and they drifted: the operated one
792/// never reported the wrap, so `d/foo<CR>` that wrapped the file was
793/// silent where `/foo<CR>` printed "search hit BOTTOM, continuing at TOP".
794///
795/// Total, and matched exhaustively at BOTH call sites, so a new outcome is
796/// a compile error in two places rather than a case one path quietly
797/// forgets. It does not make divergence impossible — the two paths
798/// genuinely differ at the landing step — it makes FORGETTING A CASE
799/// impossible, which is the failure that actually happened.
800enum CommitOutcome {
801    /// The prompt committed and a match was found.
802    Landed {
803        origin: usize,
804        step: escriba_search::Step,
805    },
806    /// Committed, but nothing matched. E486 already reported.
807    NotFound,
808    /// Nothing typed and no previous pattern. E35 already reported.
809    NoPrevious,
810    /// No prompt was open.
811    NoPrompt,
812}
813
814/// A replayable text change.
815#[derive(Debug, Clone)]
816struct LastChange {
817    /// The action that began the change.
818    action: Action,
819    /// How many times it ran.
820    count: u32,
821    /// Characters typed while the change held Insert mode open.
822    inserted: String,
823}
824
825impl EditorState {
826    /// Build a fresh editor with one buffer (scratch or file-backed).
827    pub fn new_with_buffer(initial: BufferSet, active: BufferId) -> Self {
828        let window = Window {
829            id: WindowId(1),
830            buffer_id: active,
831            viewport: Viewport {
832                top_line: 0,
833                left_column: 0,
834                visible_lines: 40,
835                visible_columns: 160,
836            },
837        };
838        Self {
839            buffers: initial,
840            modal: ModalState::new(),
841            search: SearchState::new(escriba_search::CaseMode::Smart),
842            search_at: None,
843            last_change: None,
844            recording_insert: false,
845            jumps: JumpList::new(),
846            keymap: Keymap::default_vim(),
847            commands: CommandRegistry::default_set(),
848            layout: Layout::single(window),
849            active,
850            cursors: Cursors::single(Position::ZERO),
851            quit_requested: false,
852            register: None,
853            op_pending: zenmai::Stateful::new(OpState::Resting),
854            pending_object: None,
855            messages: Vec::new(),
856            options: HashMap::new(),
857            lisp_vm: None,
858            pending_keys: Vec::new(),
859            repeat_gate: KeyRepeatGate::new(),
860            plugin_host: PluginHost::default(),
861            edit_gen: EditGen::default(),
862            damage: Damage::None,
863            // The FLEET default until an rc says otherwise — never a
864            // hand-written theme name, so a fleet re-point lands for free.
865            dispatch_depth: 0,
866            filetypes: escriba_core::FiletypeTable::new(),
867            results: escriba_shirube::ListRegistry::new(),
868            picker: None,
869            index_rev: escriba_shirube::IndexRev::default(),
870            session_gen: escriba_shirube::SessionGen::default(),
871            theme: FleetTheme::prescribed_default(),
872            chrome: ChromePalette::prescribed(),
873            splash: None,
874        }
875    }
876
877    /// The theme this editor is set to.
878    #[must_use]
879    pub const fn theme(&self) -> FleetTheme {
880        self.theme
881    }
882
883    /// The colours every face paints with — read once per frame.
884    #[must_use]
885    pub const fn chrome(&self) -> ChromePalette {
886        self.chrome
887    }
888
889    /// Point the editor at a theme. The wiring that makes
890    /// `(deftheme :preset …)` real.
891    ///
892    /// Bumps the refresh generation, because a theme change repaints
893    /// everything: the GPU face caches its shaped buffer against that
894    /// generation and would otherwise keep the old colours until an
895    /// unrelated edit happened to invalidate it.
896    pub fn set_theme(&mut self, theme: FleetTheme) {
897        if self.theme == theme {
898            return;
899        }
900        self.theme = theme;
901        self.chrome = ChromePalette::for_theme(theme);
902        self.damage = self.damage.join(Damage::Viewport);
903        self.bump_gen();
904    }
905
906    /// The start screen, if one is up. Renderers paint this INSTEAD of the
907    /// buffer pane; `None` is the ordinary editor.
908    #[must_use]
909    pub fn splash(&self) -> Option<&Splash> {
910        self.splash.as_ref()
911    }
912
913    /// Raise the start screen. The binary calls this at boot when no file
914    /// was named; an empty splash is refused so a face never has to render
915    /// a blank screen over a perfectly good buffer.
916    pub fn set_splash(&mut self, splash: Splash) {
917        if splash.is_empty() {
918            return;
919        }
920        self.splash = Some(splash);
921        self.damage = self.damage.join(Damage::Viewport);
922        self.bump_gen();
923    }
924
925    /// Take the start screen down. Idempotent; bumps the refresh generation
926    /// only when something actually changed, so dismissing twice does not
927    /// cost a repaint.
928    pub fn dismiss_splash(&mut self) {
929        if self.splash.take().is_some() {
930            self.damage = self.damage.join(Damage::Viewport);
931            self.bump_gen();
932        }
933    }
934
935    /// Offer `key` to the start screen.
936    ///
937    /// A menu key runs its entry; ANY other key simply takes the screen
938    /// down and is then handled normally — so the first thing an operator
939    /// types is never swallowed.
940    /// The open picker, for a face to paint.
941    #[must_use]
942    pub fn picker(&self) -> Option<&escriba_ui::picker::Picker> {
943        self.picker.as_ref()
944    }
945
946    /// Give an open picker the key.
947    ///
948    /// Runs BEFORE the keymap, and before the sequence stepper: while a
949    /// picker is up it owns every key, including ones it has no meaning for.
950    /// An overlay that let unknown keys fall through would edit the file
951    /// behind itself.
952    fn consume_picker_key(&mut self, key: &Key) -> escriba_ui::picker::Consumed {
953        use escriba_ui::picker::Consumed;
954        let Some(p) = self.picker.as_mut() else {
955            return Consumed::NotShowing;
956        };
957        let outcome = p.on_key(key);
958        match &outcome {
959            Consumed::Dismissed | Consumed::Chose(_) => {
960                self.picker = None;
961                self.bump_gen();
962            }
963            Consumed::Held => self.bump_gen(),
964            Consumed::NotShowing => {}
965        }
966        outcome
967    }
968
969    /// Lower an accepted pick into the ONE interpreter.
970    ///
971    /// The whole reason `Choice` is a closed enum: a new source must decide
972    /// here, and the compiler says so.
973    fn honour_choice(&mut self, choice: escriba_ui::picker::Choice) {
974        use escriba_ui::picker::Choice;
975        let slip = match choice {
976            Choice::Buffer(id) => Negai::FocusBuffer(id),
977            Choice::Command(name) => Negai::RunCommand {
978                name,
979                args: Vec::new(),
980            },
981            Choice::OpenFile(path) => Negai::OpenPath(path),
982            Choice::Location { path, line } => {
983                // Open FIRST, then jump: the buffer may not exist yet, and
984                // `jump_to_site` needs a BufferId. Two slips, one interpret.
985                self.interpret(Outcome::did(vec![Negai::OpenPath(path)]));
986                let site = escriba_shirube::Site::in_buffer(
987                    self.active,
988                    escriba_core::Range::new(
989                        escriba_core::Position::new(line, 0),
990                        escriba_core::Position::new(line, 1),
991                    ),
992                );
993                self.jump_to_site(&site);
994                return;
995            }
996        };
997        self.interpret(Outcome::did(vec![slip]));
998    }
999
1000    /// How many files a project grep will read, and how many hits it keeps.
1001    ///
1002    /// BOUNDED, and the bound is here rather than hidden, because this is a
1003    /// SYNCHRONOUS scan on the editor's own thread. The interpreter already
1004    /// does synchronous filesystem I/O (`OpenPath`, `Save`), so the posture
1005    /// is not new — but those touch one file and this walks a tree, which is
1006    /// the first one big enough to freeze the editor.
1007    ///
1008    /// The DESTINATION is the courier: an errand that scans off-thread and
1009    /// delivers results as they arrive, with no ceiling at all. This bound is
1010    /// the interim that ships a working grep meanwhile, and it is a real
1011    /// limit — a match past the ceiling is NOT found, and the picker says so
1012    /// rather than presenting a truncated list as complete.
1013    const GREP_FILE_LIMIT: usize = 2_000;
1014    const GREP_HIT_LIMIT: usize = 500;
1015
1016    /// Walk the working directory, bounded, returning `(files, truncated)`.
1017    ///
1018    /// ONE walker. grep, files and project each need to enumerate the tree,
1019    /// and three copies of a bounded traversal is three places to get the
1020    /// ceiling, the skip-list, or the truncation report subtly different.
1021    ///
1022    /// Skips dotfiles, `target` and `node_modules`. That is NOT a gitignore
1023    /// implementation and does not pretend to be — a real ignore crate comes
1024    /// with the courier.
1025    fn walk_project(limit: usize) -> (Vec<std::path::PathBuf>, bool) {
1026        Self::walk_from(std::path::Path::new("."), limit)
1027    }
1028
1029    /// The same bounded walk, from an explicit root.
1030    ///
1031    /// `walk_project` is this with `.` — one traversal, two callers, rather
1032    /// than a second copy for "browse from somewhere else".
1033    fn walk_from(root: &std::path::Path, limit: usize) -> (Vec<std::path::PathBuf>, bool) {
1034        let mut out = Vec::new();
1035        let mut truncated = false;
1036        let mut stack = vec![root.to_path_buf()];
1037        while let Some(dir) = stack.pop() {
1038            let Ok(entries) = std::fs::read_dir(&dir) else {
1039                continue;
1040            };
1041            for entry in entries.flatten() {
1042                let name = entry.file_name();
1043                let name = name.to_string_lossy();
1044                if name.starts_with('.') || name == "target" || name == "node_modules" {
1045                    continue;
1046                }
1047                let path = entry.path();
1048                if entry.file_type().is_ok_and(|t| t.is_dir()) {
1049                    stack.push(path);
1050                    continue;
1051                }
1052                if out.len() >= limit {
1053                    truncated = true;
1054                    return (out, truncated);
1055                }
1056                out.push(path);
1057            }
1058        }
1059        (out, truncated)
1060    }
1061
1062    /// Say plainly when a bounded scan stopped short.
1063    ///
1064    /// A truncated list presented as complete is the failure this codebase
1065    /// keeps finding in itself; it does not get to ship one.
1066    fn report_truncation(&mut self, truncated: bool) {
1067        if truncated {
1068            self.messages
1069                .push("scan stopped at the limit — results are INCOMPLETE".to_string());
1070        }
1071    }
1072
1073    /// Scan the working directory for `pattern`, bounded.
1074    fn grep_project(&mut self, pattern: &str) {
1075        use escriba_ui::picker::{Choice, Picker, PickerItem, Source};
1076        if pattern.is_empty() {
1077            self.messages.push("grep: empty pattern".to_string());
1078            return;
1079        }
1080        let (files, mut truncated) = Self::walk_project(Self::GREP_FILE_LIMIT);
1081        let mut items: Vec<PickerItem<Choice>> = Vec::new();
1082        'outer: for path in files {
1083            let Ok(text) = std::fs::read_to_string(&path) else {
1084                continue; // binary or unreadable — not an error worth reporting
1085            };
1086            for (n, line) in text.lines().enumerate() {
1087                if !line.contains(pattern) {
1088                    continue;
1089                }
1090                if items.len() >= Self::GREP_HIT_LIMIT {
1091                    truncated = true;
1092                    break 'outer;
1093                }
1094                let Ok(n) = u32::try_from(n) else { break };
1095                let mut label = String::with_capacity(80);
1096                label.push_str(&path.to_string_lossy());
1097                label.push(':');
1098                label.push_str(&(n + 1).to_string());
1099                label.push_str("  ");
1100                label.push_str(line.trim());
1101                items.push(PickerItem::new(
1102                    Choice::Location {
1103                        path: path.clone(),
1104                        line: n,
1105                    },
1106                    label,
1107                ));
1108            }
1109        }
1110        if items.is_empty() {
1111            let mut m = String::from("grep: no matches for ");
1112            m.push_str(pattern);
1113            self.messages.push(m);
1114            return;
1115        }
1116        if truncated {
1117            // Stated, never silent. A truncated list presented as complete is
1118            // the failure this whole codebase keeps finding.
1119            self.messages
1120                .push("grep: stopped at the scan limit — results are INCOMPLETE".to_string());
1121        }
1122        self.picker = Some(Picker::open(Source::Grep, items));
1123        self.bump_gen();
1124    }
1125
1126    /// Where accepting a finding should take the operator.
1127    ///
1128    /// Returns `None` for a finding whose site names neither a path nor a
1129    /// live buffer — that is a finding about nowhere, and offering it would
1130    /// give the operator a row that does nothing when pressed.
1131    fn finding_choice(&self, f: &escriba_shirube::Finding) -> Option<escriba_ui::picker::Choice> {
1132        use escriba_ui::picker::Choice;
1133        let line = f.site.range.start.line;
1134        if let Some(p) = &f.site.path {
1135            return Some(Choice::Location {
1136                path: p.clone(),
1137                line,
1138            });
1139        }
1140        let id = f.site.buffer?;
1141        let b = self.buffers.get(id)?;
1142        // A buffer WITH a path becomes a Location, so the line survives. A
1143        // scratch buffer has no path to name, so the best available answer
1144        // is the buffer itself — and the line is lost. Stated rather than
1145        // hidden: a `Choice` that carried a BufferId AND a line would be
1146        // the fix, and it belongs with the picker, not here.
1147        b.path.as_ref().map_or(Some(Choice::Buffer(id)), |p| {
1148            Some(Choice::Location {
1149                path: p.clone(),
1150                line,
1151            })
1152        })
1153    }
1154
1155    /// How a finding's location reads in a list row.
1156    fn finding_label(&self, f: &escriba_shirube::Finding) -> String {
1157        if let Some(p) = &f.site.path {
1158            return p.to_string_lossy().into_owned();
1159        }
1160        f.site
1161            .buffer
1162            .and_then(|id| self.buffers.get(id))
1163            .and_then(|b| b.path.as_ref())
1164            .map_or_else(
1165                || String::from("[scratch]"),
1166                |p| p.to_string_lossy().into_owned(),
1167            )
1168    }
1169
1170    /// Picker rows for every file under `root`.
1171    ///
1172    /// `picker.files` and `files.open-parent` differ only in the root, so
1173    /// they share this rather than carrying two copies of the same body —
1174    /// which is what they did for one commit, and what the line-count lint
1175    /// correctly complained about.
1176    fn file_items(
1177        &mut self,
1178        root: &std::path::Path,
1179    ) -> Vec<escriba_ui::picker::PickerItem<escriba_ui::picker::Choice>> {
1180        use escriba_ui::picker::{Choice, PickerItem};
1181        let (files, truncated) = Self::walk_from(root, Self::GREP_FILE_LIMIT);
1182        self.report_truncation(truncated);
1183        files
1184            .into_iter()
1185            .map(|p| {
1186                let label = p.to_string_lossy().into_owned();
1187                PickerItem::new(Choice::OpenFile(p), label)
1188            })
1189            .collect()
1190    }
1191
1192    /// Picker rows for the located findings the `trouble.*` verbs show.
1193    ///
1194    /// Freshness is asked of the registry, not assumed: `fresh` filters
1195    /// against the CURRENT world, so a list anchored to a revision the
1196    /// buffer has moved past contributes nothing rather than offering a
1197    /// line that has since shifted.
1198    fn finding_items(
1199        &self,
1200        workspace: bool,
1201    ) -> Vec<escriba_ui::picker::PickerItem<escriba_ui::picker::Choice>> {
1202        use escriba_ui::picker::PickerItem;
1203        let world = self.world();
1204        let active = Some(self.active);
1205        let mut items = Vec::new();
1206        for name in self.results.names() {
1207            let Some(list) = self.results.get(name) else {
1208                continue;
1209            };
1210            for f in list.fresh(&world) {
1211                // `trouble.document` narrows to the buffer in front of the
1212                // operator. A finding that names only a path is
1213                // workspace-scoped by construction — it has no buffer to be
1214                // "this" one.
1215                if !workspace && f.site.buffer != active {
1216                    continue;
1217                }
1218                let Some(choice) = self.finding_choice(f) else {
1219                    continue;
1220                };
1221                let line = f.site.range.start.line;
1222                let mut label = String::with_capacity(64);
1223                label.push_str(f.severity.label());
1224                label.push_str("  ");
1225                label.push_str(&self.finding_label(f));
1226                label.push(':');
1227                label.push_str(&(line + 1).to_string());
1228                label.push_str("  ");
1229                label.push_str(&f.message);
1230                items.push(PickerItem::new(choice, label));
1231            }
1232        }
1233        items
1234    }
1235
1236    /// Build and open a picker over `source`.
1237    fn open_picker(&mut self, source: escriba_madoguchi::PickerSource) {
1238        use escriba_ui::picker::{Choice, Picker, PickerItem, Source};
1239        let (src, items) = match source {
1240            escriba_madoguchi::PickerSource::Buffers => (
1241                Source::Buffers,
1242                self.buffers
1243                    .ids()
1244                    .into_iter()
1245                    .filter_map(|id| {
1246                        let b = self.buffers.get(id)?;
1247                        let label = b.path.as_ref().map_or_else(
1248                            || String::from("[scratch]"),
1249                            |p| p.to_string_lossy().into_owned(),
1250                        );
1251                        Some(PickerItem::new(Choice::Buffer(id), label))
1252                    })
1253                    .collect::<Vec<_>>(),
1254            ),
1255            escriba_madoguchi::PickerSource::Help => (
1256                Source::Help,
1257                self.keymap
1258                    .entries_sorted()
1259                    .into_iter()
1260                    .map(|(mode, key, b)| {
1261                        // "NORMAL  gd   goto definition" — searchable by key,
1262                        // by mode, or by what it does, because a reader
1263                        // arrives from any of the three.
1264                        let mut label = String::with_capacity(48);
1265                        label.push_str(mode.as_str());
1266                        label.push_str("  ");
1267                        // `{key:?}` because there is no shared key FORMATTER
1268                        // in the fleet — awase owns the chord vocabulary but
1269                        // escriba-keymap's `Key` has no Display. That gap
1270                        // belongs to the keymap consolidation, not here, and
1271                        // inventing a fourth spelling would make it worse.
1272                        label.push_str(&format!("{key:?}"));
1273                        label.push_str("  ");
1274                        label.push_str(&b.description);
1275                        // Accepting runs the binding's action if it names a
1276                        // command; a typed Action has no name to run, so it
1277                        // reports rather than pretending.
1278                        let choice = match &b.action {
1279                            escriba_core::Action::Command { name, .. } => {
1280                                Choice::Command(name.clone())
1281                            }
1282                            other => Choice::Command(format!("{other:?}")),
1283                        };
1284                        PickerItem::new(choice, label)
1285                    })
1286                    .collect::<Vec<_>>(),
1287            ),
1288            escriba_madoguchi::PickerSource::Files => {
1289                (Source::Files, self.file_items(std::path::Path::new(".")))
1290            }
1291            escriba_madoguchi::PickerSource::Project => {
1292                // A project root is a directory carrying a marker. Derived
1293                // from the SAME walk rather than a second traversal — the
1294                // markers are files, so the walker already visited them.
1295                const MARKERS: &[&str] = &[
1296                    "Cargo.toml",
1297                    "flake.nix",
1298                    "package.json",
1299                    "go.mod",
1300                    "pyproject.toml",
1301                ];
1302                let (files, truncated) = Self::walk_project(Self::GREP_FILE_LIMIT);
1303                self.report_truncation(truncated);
1304                let mut roots: Vec<std::path::PathBuf> = files
1305                    .into_iter()
1306                    .filter(|p| {
1307                        p.file_name()
1308                            .is_some_and(|n| MARKERS.contains(&n.to_string_lossy().as_ref()))
1309                    })
1310                    .filter_map(|p| p.parent().map(std::path::Path::to_path_buf))
1311                    .collect();
1312                roots.sort();
1313                roots.dedup();
1314                (
1315                    Source::Project,
1316                    roots
1317                        .into_iter()
1318                        .map(|p| {
1319                            let label = p.to_string_lossy().into_owned();
1320                            PickerItem::new(Choice::OpenFile(p), label)
1321                        })
1322                        .collect::<Vec<_>>(),
1323                )
1324            }
1325            escriba_madoguchi::PickerSource::Commands => (
1326                Source::Commands,
1327                self.commands
1328                    .names()
1329                    .into_iter()
1330                    .map(|n| PickerItem::new(Choice::Command(n.to_string()), n.to_string()))
1331                    .collect::<Vec<_>>(),
1332            ),
1333            escriba_madoguchi::PickerSource::FilesUnder(root) => {
1334                (Source::Files, self.file_items(&root))
1335            }
1336            escriba_madoguchi::PickerSource::Findings { workspace } => {
1337                (Source::Findings, self.finding_items(workspace))
1338            }
1339        };
1340        if items.is_empty() {
1341            self.messages.push("nothing to pick from".to_string());
1342            return;
1343        }
1344        self.picker = Some(Picker::open(src, items));
1345        self.bump_gen();
1346    }
1347
1348    /// Read one key as operator-pending object selection.
1349    ///
1350    /// Returns `None` when the key is nothing to do with objects, so the
1351    /// ordinary path runs untouched.
1352    fn consume_object_key(&mut self, key: Key) -> Option<ObjectKey> {
1353        use escriba_core::TextObject as O;
1354        let Key::Char(c) = key else {
1355            // Esc (or anything non-printable) abandons a half-typed object
1356            // rather than leaving the editor silently armed.
1357            if self.pending_object.take().is_some() {
1358                self.op_pending
1359                    .dispatch((Action::ChangeMode(Mode::Normal), 1));
1360                return Some(ObjectKey::Consumed);
1361            }
1362            return None;
1363        };
1364
1365        // Second key: it names the object.
1366        if let Some(around) = self.pending_object.take() {
1367            let object = match c {
1368                'w' => Some(O::Word { around }),
1369                // vim's `b` and `B` aliases for the bracket pairs, plus the
1370                // brackets themselves in both directions.
1371                '(' | ')' | 'b' => Some(O::Delimited {
1372                    open: '(',
1373                    close: ')',
1374                    around,
1375                }),
1376                '{' | '}' | 'B' => Some(O::Delimited {
1377                    open: '{',
1378                    close: '}',
1379                    around,
1380                }),
1381                '[' | ']' => Some(O::Delimited {
1382                    open: '[',
1383                    close: ']',
1384                    around,
1385                }),
1386                '<' | '>' => Some(O::Delimited {
1387                    open: '<',
1388                    close: '>',
1389                    around,
1390                }),
1391                // Quotes: `open == close`, which is what tells the resolver
1392                // not to count nesting.
1393                '"' => Some(O::Delimited {
1394                    open: '"',
1395                    close: '"',
1396                    around,
1397                }),
1398                '\'' => Some(O::Delimited {
1399                    open: '\'',
1400                    close: '\'',
1401                    around,
1402                }),
1403                '`' => Some(O::Delimited {
1404                    open: '`',
1405                    close: '`',
1406                    around,
1407                }),
1408                _ => None,
1409            };
1410            let OpState::Awaiting { op, count } = *self.op_pending.state() else {
1411                return Some(ObjectKey::Consumed);
1412            };
1413            // Disarm either way: an unknown object key cancels the operator,
1414            // it does not leave it armed for the next unrelated keystroke.
1415            self.op_pending
1416                .dispatch((Action::ChangeMode(Mode::Normal), 1));
1417            let Some(object) = object else {
1418                return Some(ObjectKey::Consumed);
1419            };
1420            let composed = Action::ApplyOperatorObject { op, object };
1421            // `2diw` applies the object twice. The caller runs it once, so
1422            // the extra repeats happen here.
1423            for _ in 1..count {
1424                self.apply(&composed);
1425            }
1426            return Some(ObjectKey::Compose(composed));
1427        }
1428
1429        // First key: `i` or `a` while an operator waits.
1430        if matches!(c, 'i' | 'a') && matches!(self.op_pending.state(), OpState::Awaiting { .. }) {
1431            self.pending_object = Some(c == 'a');
1432            return Some(ObjectKey::Consumed);
1433        }
1434        None
1435    }
1436
1437    fn consume_splash_key(&mut self, key: &Key) -> SplashKey {
1438        let Some(splash) = self.splash.as_ref() else {
1439            return SplashKey::NotShowing;
1440        };
1441        let chosen = match key {
1442            Key::Char(c) => splash.entry_for(*c).map(|e| e.action.clone()),
1443            _ => None,
1444        };
1445        self.dismiss_splash();
1446        chosen.map_or(SplashKey::Dismissed, SplashKey::Ran)
1447    }
1448
1449    /// The current refresh generation. A renderer caches its products against
1450    /// this; equality is the freshness test (an unchanged generation ⇒ the
1451    /// last frame is still valid, so skip the re-highlight + re-shape).
1452    #[must_use]
1453    pub fn edit_gen(&self) -> EditGen {
1454        self.edit_gen
1455    }
1456
1457    /// Advance the refresh generation (a mutation happened).
1458    fn bump_gen(&mut self) {
1459        self.edit_gen = self.edit_gen.next();
1460    }
1461
1462    /// The accumulated dirty region (read-only). See [`take_damage`](Self::take_damage).
1463    #[must_use]
1464    pub fn damage(&self) -> Damage {
1465        self.damage
1466    }
1467
1468    /// Drain the accumulated dirty region, resetting to [`Damage::None`]. The
1469    /// renderer calls this once per frame to learn what to repaint, then the
1470    /// accumulator restarts — so damage never double-counts across frames.
1471    pub fn take_damage(&mut self) -> Damage {
1472        std::mem::replace(&mut self.damage, Damage::None)
1473    }
1474
1475    /// The line count of the active buffer (0 if none) — used to compute the
1476    /// [`Damage`] scope of a mutation.
1477    fn active_line_count(&self) -> u32 {
1478        self.buffers
1479            .get(self.active)
1480            .map_or(0, escriba_buffer::Buffer::line_count)
1481    }
1482
1483    /// Register a lazy USER plugin: its escriba entry is deferred until
1484    /// one of its `triggers` fires. Bundled defaults do NOT go through
1485    /// here — they are applied eagerly at boot. Empty `triggers` means
1486    /// the plugin never lazily activates (the binary applies eager
1487    /// plugins directly).
1488    pub fn register_lazy_plugin(
1489        &mut self,
1490        name: impl Into<String>,
1491        triggers: Vec<LazyTrigger>,
1492        entry_src: impl Into<String>,
1493    ) {
1494        self.plugin_host.register(name, triggers, entry_src);
1495    }
1496
1497    /// Apply a plugin entry's escriba-lisp to live state — the same
1498    /// keymap / command / option apply paths a user rc uses. Options are
1499    /// applied before keybinds so a plugin that sets `mapleader` resolves
1500    /// `<leader>` correctly. Returns the count of commands + keybinds it
1501    /// registered (best-effort; a malformed entry is skipped, not fatal).
1502    fn apply_plugin_entry(&mut self, entry_src: &str) -> usize {
1503        let Ok(plan) = escriba_lisp::apply_source(entry_src) else {
1504            return 0;
1505        };
1506        let cmd = escriba_lisp::apply_plan_to_commands(&plan, &mut self.commands);
1507        escriba_lisp::apply_plan_to_options(&plan, &mut self.options);
1508        if let Some(value) = self.options.get("mapleader") {
1509            if let Some(key) = escriba_lisp::parse_leader_key(value) {
1510                self.keymap.set_leader(key);
1511            }
1512        }
1513        let km = escriba_lisp::apply_plan_to_keymap(&plan, &mut self.keymap);
1514        (cmd.registered + km.keybinds_applied) as usize
1515    }
1516
1517    /// Fire any lazy plugin gated on a `FileType` trigger for `filetype`.
1518    /// Returns the number of plugins activated. Call when a buffer of a
1519    /// known filetype is opened.
1520    pub fn activate_filetype_plugins(&mut self, filetype: &str) -> usize {
1521        let pending = self.plugin_host.pending_for_filetype(filetype);
1522        let n = pending.len();
1523        for src in pending {
1524            self.apply_plugin_entry(&src);
1525        }
1526        n
1527    }
1528
1529    /// Fire any lazy plugin gated on an `Event` trigger for `event`.
1530    /// Returns the number of plugins activated.
1531    pub fn activate_event_plugins(&mut self, event: &str) -> usize {
1532        let pending = self.plugin_host.pending_for_event(event);
1533        let n = pending.len();
1534        for src in pending {
1535            self.apply_plugin_entry(&src);
1536        }
1537        n
1538    }
1539
1540    /// Advance one frame's worth of state given a raw madori event.
1541    ///
1542    /// Key events pass through the [`KeyRepeatGate`] first (see
1543    /// [`Self::tick_at`]); everything else is handled directly.
1544    pub fn tick(&mut self, event: &AppEvent) {
1545        self.tick_at(event, Instant::now());
1546    }
1547
1548    /// [`Self::tick`] with an explicit timestamp for the key-repeat gate —
1549    /// lets tests drive the debounce window without depending on the
1550    /// wall clock.
1551    pub fn tick_at(&mut self, event: &AppEvent, now: Instant) {
1552        match translate_app_event(event) {
1553            InputOutcome::Key(k) => {
1554                if self.gate_key(&k, now) {
1555                    self.on_key(&k);
1556                }
1557            }
1558            InputOutcome::Resized { .. } => {
1559                // Damage only. Each face owns its own geometry: the GPU
1560                // backend derives the grid in `RenderCallback::resize`, and
1561                // the ratatui face reads its area every frame. This arm used
1562                // to write `Window.rect`, which nothing ever read — so the
1563                // resize path was already doing no real work, it just looked
1564                // like it was.
1565                self.damage = self.damage.join(Damage::Viewport);
1566                self.bump_gen();
1567            }
1568            InputOutcome::Quit => self.quit_requested = true,
1569            InputOutcome::Focus(_) | InputOutcome::None => {}
1570        }
1571    }
1572
1573    /// Decide whether `key` survives the key-repeat gate at time `now`.
1574    ///
1575    /// Returns `true` when the key should be processed, `false` when it is
1576    /// an OS key-repeat storm tick that should be dropped. Gating applies
1577    /// ONLY in the navigation modes (Normal / Visual / VisualLine) — those
1578    /// are where a held `j`/`l` floods the motion path and thrashes the
1579    /// viewport. Insert and Command modes pass every key through ungated,
1580    /// because there "hold a key to repeat the character" is the intended
1581    /// behavior, not a storm to suppress.
1582    fn gate_key(&mut self, key: &Key, now: Instant) -> bool {
1583        match self.modal.mode() {
1584            Mode::Normal | Mode::Visual | Mode::VisualLine => {
1585                // The gate exists for HELD keys that flood the motion path and
1586                // thrash the viewport (`j`, `l`). It is wrong for the discrete
1587                // jumps: two `n` presses 10 ms apart mean two matches, and
1588                // swallowing the second is indistinguishable from a dead key —
1589                // the exact symptom the gate was added to prevent elsewhere.
1590                if is_repeat_storm_candidate(key) {
1591                    return self.repeat_gate.try_pass_at(*key, now);
1592                }
1593                true
1594            }
1595            Mode::Insert | Mode::Command => true,
1596        }
1597    }
1598
1599    /// Dispatch a single key through the keymap + apply the resulting action.
1600    pub fn on_key(&mut self, key: &Key) {
1601        // An open picker owns EVERY key while it is up — before the splash,
1602        // before the sequence stepper, before the keymap.
1603        match self.consume_picker_key(key) {
1604            escriba_ui::picker::Consumed::NotShowing => {}
1605            escriba_ui::picker::Consumed::Held | escriba_ui::picker::Consumed::Dismissed => return,
1606            escriba_ui::picker::Consumed::Chose(c) => {
1607                self.honour_choice(c);
1608                return;
1609            }
1610        }
1611        // The start screen owns the first keypress and nothing after it.
1612        match self.consume_splash_key(key) {
1613            SplashKey::NotShowing | SplashKey::Dismissed => {}
1614            SplashKey::Ran(action) => {
1615                self.apply(&action);
1616                return;
1617            }
1618        }
1619        // Operator-pending OBJECT selection runs before EVERYTHING, because
1620        // `di(` must not be read as `d` then `i` (insert) then `(`.
1621        if let Some(action) = self.consume_object_key(*key) {
1622            match action {
1623                ObjectKey::Consumed => return,
1624                ObjectKey::Compose(a) => {
1625                    self.apply(&a);
1626                    return;
1627                }
1628            }
1629        }
1630        // Multi-key sequence resolution runs first: a key that begins or
1631        // continues a bound sequence (`<leader>ff`, `gg`) is held or
1632        // resolved here before the single-key path sees it.
1633        match self.step_sequence(key) {
1634            SeqStep::Pending => return,
1635            SeqStep::Resolved(action) => {
1636                let count = self.modal.pending_count().unwrap_or(1);
1637                self.modal.clear_count();
1638                for _ in 0..count {
1639                    self.apply(&action);
1640                    if self.quit_requested {
1641                        return;
1642                    }
1643                }
1644                return;
1645            }
1646            SeqStep::Passthrough => {}
1647        }
1648        let counted = self.keymap.dispatch(&self.modal, key);
1649        // Count prefixes accumulate into modal state.
1650        if matches!(counted.action, Action::Pending) {
1651            if let Key::Char(c) = key {
1652                if c.is_ascii_digit() {
1653                    let d = u32::from(*c as u8 - b'0');
1654                    self.modal.append_count(d);
1655                }
1656            }
1657            return;
1658        }
1659        // The count flows through the operator-pending FSM (apply_counted), which
1660        // owns repetition: a bare motion runs count× , an operator captures its
1661        // count, and an operated motion multiplies the two. No naive outer loop.
1662        self.apply_counted(&counted.action, counted.count);
1663        // After applying, reset pending count.
1664        self.modal.clear_count();
1665    }
1666
1667    /// Advance the multi-key pending-stroke state machine for `key`.
1668    ///
1669    /// Sequences only apply in normal / visual modes — insert and
1670    /// command modes treat keys as literal text. Rules:
1671    /// - Mid-sequence: extend the pending prefix. Exact match →
1672    ///   [`SeqStep::Resolved`]; still a live prefix → [`SeqStep::Pending`];
1673    ///   otherwise abort the sequence and re-process this key fresh.
1674    /// - Not mid-sequence: if `key` begins a bound sequence AND is not
1675    ///   itself a complete single binding (single bindings win, so no
1676    ///   chord timeout is needed) → start pending. Otherwise
1677    ///   [`SeqStep::Passthrough`] to the single-key dispatcher.
1678    fn step_sequence(&mut self, key: &Key) -> SeqStep {
1679        let mode = self.modal.mode();
1680        if !matches!(mode, Mode::Normal | Mode::Visual | Mode::VisualLine) {
1681            return SeqStep::Passthrough;
1682        }
1683        if !self.pending_keys.is_empty() {
1684            let mut seq = self.pending_keys.clone();
1685            seq.push(key.clone());
1686            if let Some(b) = self.keymap.lookup_sequence(mode, &seq) {
1687                let action = b.action.clone();
1688                self.pending_keys.clear();
1689                return SeqStep::Resolved(action);
1690            }
1691            if self.keymap.is_sequence_prefix(mode, &seq) {
1692                self.pending_keys = seq;
1693                return SeqStep::Pending;
1694            }
1695            // The key broke the in-progress sequence — abort it and let
1696            // the key be re-processed as a fresh stroke below.
1697            self.pending_keys.clear();
1698        }
1699        let start = [key.clone()];
1700        if self.keymap.is_sequence_prefix(mode, &start) && self.keymap.lookup(mode, key).is_none() {
1701            self.pending_keys = start.to_vec();
1702            return SeqStep::Pending;
1703        }
1704        SeqStep::Passthrough
1705    }
1706
1707    /// The primary cursor position. The single read accessor — every
1708    /// renderer + motion path goes through it, so the underlying
1709    /// representation (today a single-cursor [`Cursors`]) can grow to
1710    /// multi-caret without changing read sites.
1711    #[must_use]
1712    pub fn cursor(&self) -> Position {
1713        self.cursors.primary()
1714    }
1715
1716    /// The **single** cursor-mutation path. Clamp the requested position to
1717    /// the active buffer's bounds, then scroll the active window's viewport
1718    /// to contain it on BOTH axes. Routing every cursor change through this
1719    /// (and through [`Cursors::set_primary`]) makes "cursor outside its
1720    /// viewport" an unrepresentable state, AND keeps cursor state in ONE
1721    /// typed home — there is no code path that advances the cursor without
1722    /// re-deriving the viewport from it, and no second `Position` field to
1723    /// fall out of sync.
1724    /// Re-assert the cursor-visibility invariant against the CURRENT
1725    /// viewport.
1726    ///
1727    /// A resize changes how much a face can show without moving the cursor,
1728    /// so nothing would otherwise re-run `scroll_to_contain` — the cursor
1729    /// would sit off-screen until the operator happened to move it. Every
1730    /// face calls this after telling the runtime its new size.
1731    pub fn refollow_cursor(&mut self) {
1732        self.set_cursor(self.cursors.primary());
1733    }
1734
1735    fn set_cursor(&mut self, pos: Position) {
1736        let clamped = if let Some(buf) = self.buffers.get(self.active) {
1737            buf.clamp(pos)
1738        } else {
1739            pos
1740        };
1741        self.cursors.set_primary(clamped);
1742        if let Some(w) = self.layout.active_window_mut() {
1743            w.viewport = w.viewport.scroll_to_contain(self.cursors.primary(), 2);
1744        }
1745    }
1746
1747    /// Dispatch one resolved action at count 1. See [`apply_counted`](Self::apply_counted).
1748    fn apply(&mut self, action: &Action) {
1749        self.apply_counted(action, 1);
1750    }
1751
1752    /// Dispatch one resolved action with its count. Routes `(action, count)`
1753    /// through the operator-pending FSM ([`OperatorPending`], on `zenmai`): most
1754    /// actions pass straight to [`apply_resolved`](Self::apply_resolved) carrying
1755    /// their count (so `5j` runs the motion 5×), an operator key is held, and an
1756    /// operator-then-motion pair is rewritten into a counted
1757    /// [`Action::ApplyOperator`] (so `3dw` deletes 3 words). The FSM owns count
1758    /// composition — there is no naive outer repeat loop.
1759    fn apply_counted(&mut self, action: &Action, count: u32) {
1760        // An uncompilable pattern must not reach the operator machine.
1761        //
1762        // `SearchState::accept` puts the prompt BACK on a compile error so the
1763        // typed text is not lost — but the FSM had already transitioned out of
1764        // `AwaitingSearch` on the way in, so the prompt survived and the
1765        // OPERATOR did not, with nothing said about it. The `d` was simply
1766        // gone, and the corrected pattern then ran as a bare search.
1767        //
1768        // The machine is a pure `(State, Event) -> (State, effects)` and
1769        // cannot observe the result of an effect, so it cannot decide this
1770        // itself. The fix is to stop handing it an event it has no business
1771        // deciding: the runtime classifies the submit first, from state it
1772        // already holds. `prompt_error` returns `None` for an EMPTY prompt, so
1773        // the bare-`/<CR>` reuse path is untouched.
1774        //
1775        // Tier-honest: parse-rejected at the boundary, not
1776        // truly-unrepresentable.
1777        if matches!(action, Action::SubmitCommand) {
1778            if let Some(e) = self.search.prompt_error() {
1779                let mut m = String::from("E383: Invalid search string: ");
1780                m.push_str(&e.to_string());
1781                self.messages.push(m);
1782                return;
1783            }
1784        }
1785
1786        for (resolved, times) in self.op_pending.dispatch((action.clone(), count)) {
1787            for _ in 0..times {
1788                self.apply_resolved(&resolved);
1789                if self.quit_requested {
1790                    return;
1791                }
1792            }
1793        }
1794    }
1795
1796    /// The active buffer's text. Search is a pure function of it.
1797    /// The active buffer's text revision — the token an offset measured
1798    /// against it should carry.
1799    #[must_use]
1800    fn text_rev(&self) -> TextRev {
1801        self.buffers
1802            .get(self.active)
1803            .map_or_else(TextRev::default, escriba_buffer::Buffer::text_rev)
1804    }
1805
1806    fn active_text(&self) -> String {
1807        self.buffers
1808            .get(self.active)
1809            .map(escriba_buffer::Buffer::to_string)
1810            .unwrap_or_default()
1811    }
1812
1813    /// The cursor as a char offset — the coordinate search speaks.
1814    fn cursor_char(&self) -> usize {
1815        self.buffers
1816            .get(self.active)
1817            .and_then(|b| b.position_to_char(self.cursor()).ok())
1818            .unwrap_or(0)
1819    }
1820
1821    /// Move the cursor onto a match and report a wrap the way vim does.
1822    /// The status line as data — what every face draws.
1823    ///
1824    /// One model, so the two faces can only disagree about styling. Before
1825    /// this existed the GPU face built its own line from a fixed `format!()`
1826    /// and drew neither the prompt nor any message, which made a fully
1827    /// working `/` look like a dead key on escriba's default renderer.
1828    #[must_use]
1829    pub fn status_model(&self) -> StatusModel<'_> {
1830        let cursor = self.cursor();
1831        let prompt = self.search.prompt();
1832
1833        let kind = match prompt.map(|p| p.direction) {
1834            Some(escriba_search::Direction::Forward) => PromptKind::SearchForward,
1835            Some(escriba_search::Direction::Backward) => PromptKind::SearchBackward,
1836            // Command mode with no search prompt open is an ex-command; the
1837            // typed `Option<Prompt>` is the discriminator, never a mode flag.
1838            None if self.modal.mode() == Mode::Command => PromptKind::Ex,
1839            None => PromptKind::None,
1840        };
1841
1842        StatusModel {
1843            mode: self.modal.mode(),
1844            line: cursor.line.saturating_add(1) as usize,
1845            column: cursor.column.saturating_add(1) as usize,
1846            prompt: kind,
1847            prompt_text: prompt
1848                .map_or_else(|| self.modal.minibuffer(), escriba_search::Prompt::text),
1849            prompt_caret: prompt.map_or_else(
1850                || self.modal.minibuffer_caret(),
1851                escriba_search::Prompt::caret,
1852            ),
1853            count: self.match_count(),
1854            message: self.messages.last().map(String::as_str),
1855        }
1856    }
1857
1858    /// `[3/17]` for the current pattern.
1859    ///
1860    /// While a prompt is open the count describes the PREVIEW — the answer to
1861    /// "what would Enter do", which is the question being asked mid-typing.
1862    /// Once committed it describes where the cursor actually is.
1863    #[must_use]
1864    fn match_count(&self) -> MatchCount {
1865        if self.search.is_prompting() {
1866            let text = self.active_text();
1867            // ONE scan, four outcomes. `Incomplete` and `NoMatch` used to be
1868            // the same `None`, so a half-typed character class reported
1869            // `[0/0]` — telling the user their pattern matches nothing while
1870            // they are still writing it.
1871            return match self.search.preview(&text) {
1872                escriba_search::Preview::Landed { step, total } => {
1873                    MatchCount::new(step.index, total)
1874                }
1875                escriba_search::Preview::NoMatch => MatchCount::None,
1876                escriba_search::Preview::Incomplete | escriba_search::Preview::Idle => {
1877                    MatchCount::Idle
1878                }
1879            };
1880        }
1881        if self.search.pattern().is_none() {
1882            return MatchCount::Idle;
1883        }
1884        let total = self.search.matches().len();
1885        // Read THROUGH the anchor: an ordinal computed against text that has
1886        // since changed reads as absent, so a stale count cannot be displayed.
1887        let rev = self.text_rev();
1888        self.search_at.as_ref().and_then(|a| a.get(rev)).map_or(
1889            if total == 0 {
1890                MatchCount::None
1891            } else {
1892                MatchCount::Idle
1893            },
1894            |&i| MatchCount::new(i, total),
1895        )
1896    }
1897
1898    /// `.` — replay the last change at the cursor.
1899    ///
1900    /// Two steps, because a change can be two: run the action, then re-type
1901    /// whatever followed it. `cgn` + `.` is exactly this — change the next
1902    /// match, then repeat that whole gesture on the one after.
1903    fn repeat_last_change(&mut self) {
1904        let Some(change) = self.last_change.clone() else {
1905            self.messages
1906                .push("E32: No previous change to repeat".to_string());
1907            return;
1908        };
1909
1910        for _ in 0..change.count.max(1) {
1911            self.apply_resolved(&change.action);
1912        }
1913        for c in change.inserted.chars() {
1914            self.apply_resolved(&Action::InsertChar(c));
1915        }
1916        if self.modal.mode() == Mode::Insert {
1917            // A replayed change must not leave the editor in Insert — the
1918            // original ended with an Esc the recording deliberately does not
1919            // store, since it is punctuation rather than part of the change.
1920            self.apply_resolved(&Action::ChangeMode(Mode::Normal));
1921        }
1922        // The replay wrote through `apply_resolved`, which re-records
1923        // `last_change` from the inner action. Put the ORIGINAL back so a
1924        // second `.` repeats the same change rather than a fragment of it.
1925        self.last_change = Some(change);
1926        self.recording_insert = false;
1927    }
1928
1929    /// Resolve a text object to the range it names.
1930    ///
1931    /// `gn` uses the INCLUSIVE step, so a cursor already sitting inside a
1932    /// match operates on THAT match rather than skipping to the next — which
1933    /// is what makes `cgn` then `.` walk matches one at a time instead of
1934    /// every other one.
1935    /// `dd` — the current line INCLUDING its terminator.
1936    ///
1937    /// Taking the newline is what makes `dd` remove a line rather than blank
1938    /// it. On the last line there is no following newline to take, so it
1939    /// falls back to the preceding one — otherwise `dd` on the final line
1940    /// leaves an empty line behind, which is the one case a naive
1941    /// "start-of-line to start-of-next-line" range gets wrong.
1942    fn object_line(&self) -> Option<Range> {
1943        let buf = self.buffers.get(self.active)?;
1944        let line = self.cursor().line;
1945        let last = buf.line_count().saturating_sub(1);
1946        if line < last {
1947            Some(Range::new(
1948                Position::new(line, 0),
1949                Position::new(line + 1, 0),
1950            ))
1951        } else if line > 0 {
1952            // Final line: swallow the PRECEDING newline instead.
1953            Some(Range::new(
1954                Position::new(line - 1, buf.line_len_chars(line - 1)),
1955                Position::new(line, buf.line_len_chars(line)),
1956            ))
1957        } else {
1958            // The only line in the buffer: clear it, keep the line itself.
1959            Some(Range::new(
1960                Position::new(0, 0),
1961                Position::new(0, buf.line_len_chars(0)),
1962            ))
1963        }
1964    }
1965
1966    /// `iw` / `aw` — the word under the cursor.
1967    ///
1968    /// vim's `w` classes are word / punctuation / whitespace, and a text
1969    /// object never crosses a line. `around` additionally takes the trailing
1970    /// whitespace run, falling back to LEADING whitespace when there is none
1971    /// after — which is what vim does at end of line.
1972    fn object_word(&self, around: bool) -> Option<Range> {
1973        let buf = self.buffers.get(self.active)?;
1974        let pos = self.cursor();
1975        let text: Vec<char> = buf.line(pos.line)?.chars().collect();
1976        if text.is_empty() {
1977            return None;
1978        }
1979        let col = (pos.column as usize).min(text.len().saturating_sub(1));
1980
1981        #[derive(PartialEq, Clone, Copy)]
1982        enum Class {
1983            Word,
1984            Punct,
1985            Space,
1986        }
1987        let class = |c: char| {
1988            if c.is_alphanumeric() || c == '_' {
1989                Class::Word
1990            } else if c.is_whitespace() {
1991                Class::Space
1992            } else {
1993                Class::Punct
1994            }
1995        };
1996
1997        let here = class(text[col]);
1998        let mut start = col;
1999        while start > 0 && class(text[start - 1]) == here {
2000            start -= 1;
2001        }
2002        let mut end = col + 1;
2003        while end < text.len() && class(text[end]) == here {
2004            end += 1;
2005        }
2006
2007        if around {
2008            let after = end;
2009            while end < text.len() && class(text[end]) == Class::Space {
2010                end += 1;
2011            }
2012            // No trailing run: take the leading one instead, as vim does.
2013            if end == after {
2014                while start > 0 && class(text[start - 1]) == Class::Space {
2015                    start -= 1;
2016                }
2017            }
2018        }
2019
2020        Some(Range::new(
2021            Position::new(pos.line, start as u32),
2022            Position::new(pos.line, end as u32),
2023        ))
2024    }
2025
2026    /// `i(` / `a"` … — the region between a matched pair, on one line.
2027    ///
2028    /// Brackets NEST and quotes do not, and that is the only difference:
2029    /// with `open == close` the scan cannot count depth, so it takes the
2030    /// nearest delimiter on each side instead.
2031    fn object_delimited(&self, open: char, close: char, around: bool) -> Option<Range> {
2032        let buf = self.buffers.get(self.active)?;
2033        let pos = self.cursor();
2034        let text: Vec<char> = buf.line(pos.line)?.chars().collect();
2035        if text.is_empty() {
2036            return None;
2037        }
2038        let col = (pos.column as usize).min(text.len().saturating_sub(1));
2039
2040        let (l, r) = if open == close {
2041            // Quotes: nearest on each side, no nesting to track.
2042            let l = (0..=col).rev().find(|&i| text[i] == open)?;
2043            let r = ((col.max(l) + 1)..text.len()).find(|&i| text[i] == close)?;
2044            (l, r)
2045        } else {
2046            // Brackets: walk out counting depth, so an inner pair does not
2047            // terminate the search for the enclosing one.
2048            let mut depth = 0i32;
2049            let l = (0..=col).rev().find(|&i| {
2050                if text[i] == close && i != col {
2051                    depth += 1;
2052                    false
2053                } else if text[i] == open {
2054                    if depth == 0 {
2055                        true
2056                    } else {
2057                        depth -= 1;
2058                        false
2059                    }
2060                } else {
2061                    false
2062                }
2063            })?;
2064            depth = 0;
2065            let r = ((l + 1)..text.len()).find(|&i| {
2066                if text[i] == open {
2067                    depth += 1;
2068                    false
2069                } else if text[i] == close {
2070                    if depth == 0 {
2071                        true
2072                    } else {
2073                        depth -= 1;
2074                        false
2075                    }
2076                } else {
2077                    false
2078                }
2079            })?;
2080            (l, r)
2081        };
2082
2083        // `i` is strictly between the delimiters; `a` includes them.
2084        let (s, e) = if around { (l, r + 1) } else { (l + 1, r) };
2085        Some(Range::new(
2086            Position::new(pos.line, s as u32),
2087            Position::new(pos.line, e as u32),
2088        ))
2089    }
2090
2091    fn resolve_object(&self, object: escriba_core::TextObject) -> Option<Range> {
2092        use escriba_core::TextObject as O;
2093
2094        // The text-scanning objects resolve against the BUFFER; the two
2095        // search objects resolve against the match set. Splitting here keeps
2096        // the search logic below exactly as it was rather than threading a
2097        // second concern through it.
2098        match object {
2099            O::Line => return self.object_line(),
2100            O::Word { around } => return self.object_word(around),
2101            O::Delimited {
2102                open,
2103                close,
2104                around,
2105            } => return self.object_delimited(open, close, around),
2106            O::NextMatch | O::PrevMatch => {}
2107        }
2108
2109        let at = self.cursor_char();
2110        let matches = self.search.matches();
2111
2112        // A match CONTAINING the cursor wins outright, whichever direction the
2113        // object names.
2114        //
2115        // Comparing only against `m.start` — which is what a `starts`-vector
2116        // plus `Bound::Inclusive` does — is right only when the cursor sits on
2117        // a match's FIRST character. One column further in, `start < at` and
2118        // the match is rejected, so `cgn` skipped the very instance the
2119        // operator was standing in and the rename silently missed it. vim
2120        // operates on the containing match from every interior column, and the
2121        // `starts`-only comparison cannot express "contains" because it never
2122        // looks at `m.end`.
2123        let idx = matches.iter().position(|m| m.contains(at)).or_else(|| {
2124            let starts: Vec<usize> = matches.iter().map(|m| m.start).collect();
2125            match object {
2126                O::PrevMatch => Bound::Inclusive.first_matching(&starts, at, false),
2127                // Every other variant returned above; `NextMatch` is the only
2128                // one that can reach here besides `PrevMatch`.
2129                _ => Bound::Inclusive.first_matching(&starts, at, true),
2130            }
2131        })?;
2132
2133        let m = matches.get(idx)?;
2134        let buf = self.buffers.get(self.active)?;
2135        Some(Range {
2136            start: buf.char_to_position(m.start),
2137            end: buf.char_to_position(m.end),
2138        })
2139    }
2140
2141    fn land_on(&mut self, step: escriba_search::Step) {
2142        if let Some(buf) = self.buffers.get(self.active) {
2143            let pos = buf.char_to_position(step.target.start);
2144            self.set_cursor(pos);
2145        }
2146        // The `[3/17]` numerator. `Step` has carried this index since the
2147        // engine was written — `engine.rs` even names the counter as the
2148        // reason it exists — and every consumer discarded it until now.
2149        self.search_at = Some(Anchored::new(step.index, self.text_rev()));
2150    }
2151
2152    /// vim's "search hit BOTTOM, continuing at TOP".
2153    ///
2154    /// One reporter, called by the two places a search can wrap: the shared
2155    /// commit and `n`/`N`. `land_on` deliberately does NOT report, or the bare
2156    /// commit would say it twice.
2157    fn report_wrap(&mut self, step: &escriba_search::Step) {
2158        if let Some(msg) = escriba_search::wrap_message(step.wrapped) {
2159            self.messages.push(msg.to_string());
2160        }
2161    }
2162
2163    /// `n` / `N`. Reports vim's E486 when the pattern matches nothing, rather
2164    /// than failing silently — a search that appears to do nothing is
2165    /// indistinguishable from a dropped keystroke.
2166    fn jump_search(&mut self, reverse: bool) {
2167        // Using the matches re-lights them: `n` after an auto-clear shows you
2168        // what you are walking through.
2169        self.search.relight();
2170        // `n` is a far jump — record where we leave from so `<C-o>` works.
2171        self.jumps.push(self.spot());
2172        let at = self.cursor_char();
2173        match self.search.repeat(at, reverse) {
2174            Some(step) => {
2175                // `n` wrapping the file says so, same as a commit does.
2176                self.report_wrap(&step);
2177                self.land_on(step);
2178            }
2179            None => {
2180                let msg = self.search.pattern().map_or_else(
2181                    || "E35: No previous regular expression".to_string(),
2182                    |p| {
2183                        let mut m = String::from("E486: Pattern not found: ");
2184                        m.push_str(p.raw());
2185                        m
2186                    },
2187                );
2188                self.messages.push(msg);
2189            }
2190        }
2191    }
2192
2193    /// Move the cursor to where the in-progress pattern would land, without
2194    /// committing anything. vim's `incsearch`.
2195    ///
2196    /// A pattern that does not compile yet (`/a[`, mid-typing) previews
2197    /// nothing and reports nothing — an error toast on every keystroke of a
2198    /// character class would be unusable.
2199    fn preview_search(&mut self) {
2200        let text = self.active_text();
2201        let Some(origin) = self.search.prompt().map(|p| p.origin) else {
2202            return;
2203        };
2204        let target = match self.search.preview(&text) {
2205            escriba_search::Preview::Landed { step, .. } => step.target.start,
2206            // Nothing to show: back to where the search started. Covers a
2207            // half-typed pattern and a pattern that finds nothing alike —
2208            // both mean "there is no match to preview".
2209            escriba_search::Preview::Idle
2210            | escriba_search::Preview::Incomplete
2211            | escriba_search::Preview::NoMatch => origin,
2212        };
2213        // A pattern that STOPS matching returns the cursor to the origin.
2214        //
2215        // Preview used to only ever move forward, so typing `ch` (a match) and
2216        // then `chz` (none) left the cursor parked on the `ch` match — a
2217        // preview showing a position the pattern no longer justifies, while
2218        // the count beside it read `[0/0]`. Restoring is also what makes
2219        // Escape's promise legible: at every keystroke the cursor is either on
2220        // a real match or back where you started, never on a stale one.
2221        if let Some(buf) = self.buffers.get(self.active) {
2222            let pos = buf.char_to_position(target);
2223            self.set_cursor(pos);
2224        }
2225    }
2226
2227    /// `d/foo<CR>` — commit the prompt and operate from the prompt's origin to
2228    /// where the search lands, as ONE action.
2229    ///
2230    /// Split from [`Self::submit_search`] rather than sharing it because the
2231    /// two want opposite things from the commit: the bare `/` MOVES the cursor
2232    /// to the match, and an operated `/` must NOT — the cursor is the
2233    /// operator's start point, and moving it first would leave the operator
2234    /// with a zero-width range.
2235    /// Commit the open search prompt. The ONE copy of the sequence.
2236    ///
2237    /// Reports its own failures (E486 / E35) so neither caller has to carry a
2238    /// third copy of the message strings. `Accepted::Invalid` cannot reach
2239    /// here — `apply_counted` rejects an uncompilable pattern at the dispatch
2240    /// boundary before the FSM or this method ever sees the submit.
2241    fn commit_search_prompt(&mut self) -> CommitOutcome {
2242        let text = self.active_text();
2243        let Some((origin, skip)) = self.search.prompt().map(|p| (p.origin, p.preview_skip()))
2244        else {
2245            return CommitOutcome::NoPrompt;
2246        };
2247
2248        match self.search.accept(&text) {
2249            escriba_search::Accepted::Committed | escriba_search::Accepted::ReusedPrevious => {
2250                self.modal.clear_minibuffer();
2251                self.modal.enter(Mode::Normal);
2252                match self.search.commit_step_skipping(origin, skip) {
2253                    Some(step) => {
2254                        // The wrap notice belongs HERE, once, for both commit
2255                        // paths. Reporting it in each caller is what let the
2256                        // operated path lose it in the first place — and my
2257                        // first attempt at this refactor duplicated it again
2258                        // rather than moving it, which the red proof caught.
2259                        self.report_wrap(&step);
2260                        CommitOutcome::Landed { origin, step }
2261                    }
2262                    None => {
2263                        self.report_pattern_not_found();
2264                        CommitOutcome::NotFound
2265                    }
2266                }
2267            }
2268            escriba_search::Accepted::NothingToRepeat => {
2269                self.modal.clear_minibuffer();
2270                self.modal.enter(Mode::Normal);
2271                self.messages
2272                    .push("E35: No previous regular expression".to_string());
2273                CommitOutcome::NoPrevious
2274            }
2275            // Unreachable: the boundary guard in `apply_counted` returns early
2276            // on an uncompilable pattern, leaving the prompt open. Reported
2277            // rather than `unreachable!()` — a panic in the editor's commit
2278            // path is a worse failure than a duplicate message.
2279            escriba_search::Accepted::Invalid(e) => {
2280                let mut m = String::from("E383: Invalid search string: ");
2281                m.push_str(&e.to_string());
2282                self.messages.push(m);
2283                CommitOutcome::NoPrompt
2284            }
2285        }
2286    }
2287
2288    /// vim's E486, with the pattern named. One place, so every path that fails
2289    /// to find reports identically.
2290    fn report_pattern_not_found(&mut self) {
2291        let mut m = String::from("E486: Pattern not found");
2292        if let Some(p) = self.search.pattern() {
2293            m.push_str(": ");
2294            m.push_str(p.raw());
2295        }
2296        self.messages.push(m);
2297    }
2298
2299    /// Bare `/foo<CR>` — commit and MOVE the cursor to the match.
2300    ///
2301    /// The only difference from the operated path is that this one lands;
2302    /// everything else lives in `commit_search_prompt`.
2303    fn submit_search(&mut self) {
2304        match self.commit_search_prompt() {
2305            CommitOutcome::Landed { origin, step } => {
2306                if let Some(buf) = self.buffers.get(self.active) {
2307                    let from = buf.char_to_position(origin);
2308                    self.jumps.push(escriba_core::Spot::new(self.active, from));
2309                }
2310                self.land_on(step);
2311            }
2312            CommitOutcome::NotFound | CommitOutcome::NoPrevious | CommitOutcome::NoPrompt => {}
2313        }
2314    }
2315
2316    /// `d/foo<CR>` — commit, then operate from the prompt's origin to where the
2317    /// search lands, as ONE action.
2318    ///
2319    /// The cursor must NOT move to the match first: it is the operator's start
2320    /// point. That is the whole reason this differs from the bare path, and
2321    /// now the only reason.
2322    fn submit_search_operated(&mut self, op: Operator) {
2323        match self.commit_search_prompt() {
2324            CommitOutcome::Landed { origin, step } => {
2325                if let Some(buf) = self.buffers.get(self.active) {
2326                    let from = buf.char_to_position(origin);
2327                    let target = buf.char_to_position(step.target.start);
2328                    // Operating over a search is itself a far jump.
2329                    self.jumps.push(escriba_core::Spot::new(self.active, from));
2330                    self.set_cursor(from);
2331                    self.apply_operator_to(op, target);
2332                }
2333            }
2334            CommitOutcome::NotFound | CommitOutcome::NoPrevious | CommitOutcome::NoPrompt => {}
2335        }
2336    }
2337
2338    fn apply_resolved(&mut self, action: &Action) {
2339        // Snapshot the scope inputs before the mutation so the resulting
2340        // Damage covers the changed region (the S3 seal — conservative widen).
2341        let lines_before = self.active_line_count();
2342        // Snapshot for the dot register: the only reliable witness that this
2343        // action changed text is that the buffer's revision moved.
2344        let rev_before = self.text_rev();
2345        let cline_before = self.cursor().line;
2346        match action {
2347            // Every action with an exact slip equivalent goes through the
2348            // interpreter, so "undo" has ONE implementation rather than one
2349            // per entry point. These had already drifted: the executor
2350            // re-followed the viewport after undo and the M1 interpreter did
2351            // not, so `u` and `:undo` behaved differently within a milestone
2352            // of each other.
2353            // Listed EXPLICITLY rather than behind a `if lower(..).is_some()`
2354            // guard: a guard arm does not count toward exhaustiveness, so the
2355            // guarded form silently gave up the total match — the compiler
2356            // said so, and it was right. `lowering_and_dispatch_agree` pins
2357            // that this list and `lower` stay the same set.
2358            Action::Quit
2359            | Action::ClearSearchHighlight
2360            | Action::Save
2361            | Action::Undo
2362            | Action::Redo
2363            | Action::Edit(_) => {
2364                for slip in Self::lower(action, self.active).unwrap_or_default() {
2365                    self.honour_one(slip);
2366                }
2367            }
2368            Action::Move(m) => self.apply_motion(*m),
2369            Action::SearchOpen(dir) => {
2370                // vim's `/` is the command-line with a different prompt char,
2371                // so we reuse Command mode; `search.prompt` is what tells a
2372                // later <CR> this is a search and not an ex-command.
2373                let origin = self.cursor_char();
2374                self.search.open(*dir, origin);
2375                self.modal.enter(Mode::Command);
2376            }
2377            Action::SearchRepeat { reverse } => self.jump_search(*reverse),
2378            Action::SearchWord { reverse } => {
2379                let dir = if *reverse {
2380                    SearchDirection::Backward
2381                } else {
2382                    SearchDirection::Forward
2383                };
2384                let (text, at) = (self.active_text(), self.cursor_char());
2385                // `*` jumps, so it records too.
2386                self.jumps.push(self.spot());
2387                match self.search.search_word(&text, at, dir) {
2388                    Some(step) => self.land_on(step),
2389                    // vim beeps and stays put when there is no word under the
2390                    // cursor; a silent no-op would look like a broken key.
2391                    None => self
2392                        .messages
2393                        .push("E348: No string under cursor".to_string()),
2394                }
2395            }
2396            Action::SearchSubmitOperated { op } => self.submit_search_operated(*op),
2397            Action::TextObject(object) => {
2398                // Bare `gn` moves onto the match. vim additionally starts a
2399                // Visual selection of it; escriba's Visual plumbing does not
2400                // carry a selection an operator can consume yet, so this
2401                // stops at the jump rather than faking a selection that
2402                // nothing would honour.
2403                if let Some(range) = self.resolve_object(*object) {
2404                    self.jumps.push(self.spot());
2405                    self.set_cursor(range.start);
2406                } else {
2407                    self.report_pattern_not_found();
2408                }
2409            }
2410            Action::ApplyOperatorObject { op, object } => match self.resolve_object(*object) {
2411                Some(range) => self.apply_operator_over(*op, range),
2412                None => self.report_pattern_not_found(),
2413            },
2414            Action::RepeatLastChange => self.repeat_last_change(),
2415            Action::JumpBack => {
2416                let here = self.spot();
2417                if let Some(spot) = self.jumps.back(here) {
2418                    self.goto_spot(spot);
2419                } else {
2420                    self.messages
2421                        .push("E662: At start of changelist".to_string());
2422                }
2423            }
2424            Action::JumpForward => {
2425                if let Some(spot) = self.jumps.forward() {
2426                    self.goto_spot(spot);
2427                } else {
2428                    self.messages.push("E663: At end of changelist".to_string());
2429                }
2430            }
2431            Action::ChangeMode(m) => {
2432                // Leaving the cmdline abandons any open search prompt and
2433                // returns the cursor home. The COMMITTED pattern survives —
2434                // cancelling a new search must not erase the old highlights.
2435                if *m == Mode::Normal && self.search.is_prompting() {
2436                    if let Some(origin) = self.search.cancel() {
2437                        if let Some(buf) = self.buffers.get(self.active) {
2438                            let pos = buf.char_to_position(origin);
2439                            self.set_cursor(pos);
2440                        }
2441                    }
2442                }
2443                self.modal.enter(*m);
2444            }
2445            Action::InsertChar(c) => self.insert_char(*c),
2446
2447            Action::SubmitCommand => {
2448                if self.search.is_prompting() {
2449                    self.submit_search();
2450                } else {
2451                    self.submit_command();
2452                }
2453            }
2454            Action::Command { name, args } => self.run_command(name, args),
2455            Action::ApplyOperator { op, motion } => self.apply_operator(*op, *motion),
2456            // The operator-pending FSM consumes Operator keys (begins pending);
2457            // they never reach the executor. Defensive no-op for exhaustiveness.
2458            Action::Operator(_) => {}
2459            Action::PromptCaret { to } => {
2460                // Both prompts have a caret now, and the same keys move it.
2461                if self.search.is_prompting() {
2462                    self.search.move_caret(*to);
2463                } else {
2464                    self.modal.move_minibuffer_caret(*to);
2465                }
2466            }
2467            Action::SearchPreviewStep { forward } => {
2468                if self.search.is_prompting() {
2469                    self.search.preview_step(*forward);
2470                    self.preview_search();
2471                }
2472            }
2473            Action::PromptDelete => {
2474                if self.search.is_prompting() {
2475                    self.search.delete_at_caret();
2476                    self.preview_search();
2477                } else {
2478                    self.modal.delete_minibuffer_at_caret();
2479                }
2480            }
2481            Action::PromptDeleteWord => {
2482                if self.search.is_prompting() {
2483                    self.search.delete_word_before_caret();
2484                    self.preview_search();
2485                }
2486            }
2487            Action::PromptClearToStart => {
2488                if self.search.is_prompting() {
2489                    self.search.clear_before_caret();
2490                    self.preview_search();
2491                }
2492            }
2493            Action::PromptBackspace => {
2494                self.prompt_backspace();
2495                // Shortening the pattern changes which matches exist, so the
2496                // preview must re-run — otherwise the cursor sits on a match
2497                // of a pattern that is no longer typed.
2498                if self.search.is_prompting() {
2499                    self.preview_search();
2500                }
2501            }
2502            Action::PromptHistory { back } => {
2503                if self.search.is_prompting() {
2504                    self.search.history_step(*back);
2505                    // No minibuffer resync: the shadow is the ex-line's store
2506                    // and nothing reads it while a search prompt is open, so
2507                    // rewriting it here was maintaining a copy for no reader.
2508                    self.preview_search();
2509                }
2510            }
2511            Action::Pending => {}
2512        }
2513        // Widen the dirty region by what this action touched (M1). Content
2514        // mutations that changed the line count run to end-of-document (every
2515        // line below shifted); an in-place edit or a cursor move is local;
2516        // arbitrary commands are conservatively Full. Never narrows.
2517        let lines_after = self.active_line_count();
2518        let cline_after = self.cursor().line;
2519        let d = match action {
2520            // A search repaints every highlight in the viewport, not just the
2521            // line the cursor left — so it must widen to Full. Treating it as a
2522            // cursor move would leave stale highlights on untouched lines.
2523            Action::SearchOpen(_)
2524            | Action::PromptHistory { .. }
2525            | Action::PromptBackspace
2526            | Action::PromptCaret { .. }
2527            | Action::SearchPreviewStep { .. }
2528            | Action::PromptDelete
2529            | Action::PromptDeleteWord
2530            | Action::PromptClearToStart
2531            | Action::SearchRepeat { .. }
2532            | Action::SearchWord { .. }
2533            | Action::ClearSearchHighlight
2534            | Action::SearchSubmitOperated { .. }
2535            // A replayed change can edit anywhere the original could, and a
2536            // match object can be anywhere in the document.
2537            | Action::RepeatLastChange
2538            | Action::TextObject(_)
2539            | Action::ApplyOperatorObject { .. }
2540            // A jump can land anywhere, so the viewport may scroll wholesale.
2541            | Action::JumpBack
2542            | Action::JumpForward => Damage::Full,
2543            Action::InsertChar(_)
2544            | Action::Edit(_)
2545            | Action::Undo
2546            | Action::Redo
2547            | Action::ApplyOperator { .. } => {
2548                if lines_after == lines_before {
2549                    Damage::span(cline_before, cline_after)
2550                } else {
2551                    Damage::Lines {
2552                        from: cline_before.min(cline_after),
2553                        to: u32::MAX,
2554                    }
2555                }
2556            }
2557            Action::Move(_) | Action::ChangeMode(_) => Damage::span(cline_before, cline_after),
2558            Action::Save => Damage::Viewport,
2559            Action::Command { .. } | Action::SubmitCommand => Damage::Full,
2560            Action::Quit | Action::Operator(_) | Action::Pending => Damage::None,
2561        };
2562        self.damage = self.damage.join(d);
2563        // Remember this change for `.`.
2564        //
2565        // Recorded from an OBSERVED MUTATION, not from the action's variant.
2566        // `text_effect()` is the wrong predicate here even though it looks
2567        // like the right one: it exists to decide cache invalidation, where
2568        // OVER-reporting is the safe direction, and the dot register needs the
2569        // opposite bias. Leaning on it meant `last_change` was set by actions
2570        // that changed no text at all, with two measured consequences:
2571        //
2572        //   `iZ<Esc>` then `/a<CR>` then `.`  — did nothing; the register held
2573        //       `SubmitCommand`, whose replay reads an already-cleared
2574        //       minibuffer.
2575        //   `iZ<Esc>` then `/q<Esc>` then `.` — TYPED `q` INTO THE BUFFER. An
2576        //       abandoned prompt left the register holding `InsertChar('q')`,
2577        //       and `.` in Normal mode routes that to the text. A corrupting
2578        //       register, not merely a lost one.
2579        //
2580        // Comparing the buffer's `TextRev` across the action answers the only
2581        // question that matters — did this actually change the text — and gets
2582        // the failed-operator case (`dgn` with no pattern) right for free.
2583        if self.recording_insert {
2584            match action {
2585                Action::InsertChar(c) => {
2586                    if let Some(lc) = self.last_change.as_mut() {
2587                        lc.inserted.push(*c);
2588                    }
2589                }
2590                // Leaving Insert ends the session; the change is now whole.
2591                Action::ChangeMode(m) if *m != Mode::Insert => self.recording_insert = false,
2592                _ => {}
2593            }
2594        } else if self.text_rev() != rev_before
2595            && !matches!(
2596                action,
2597                Action::RepeatLastChange | Action::Undo | Action::Redo
2598            )
2599        {
2600            self.last_change = Some(LastChange {
2601                action: action.clone(),
2602                count: 1,
2603                inserted: String::new(),
2604            });
2605            self.recording_insert = self.modal.mode() == Mode::Insert;
2606        }
2607
2608        // The search is over the moment you move on or edit — clear the
2609        // highlight rather than leaving the buffer as confetti until an
2610        // explicit `:noh`, which is the remap nearly every vimrc carries.
2611        // Clearing suppresses without forgetting, so `n` still works.
2612        if action.highlight_effect() == HighlightEffect::Clear {
2613            self.search.clear_highlight();
2614        }
2615        // Text changed ⇒ every match offset cached against the old text is
2616        // wrong. `SearchState::refresh` existed for exactly this and had ZERO
2617        // callers, so inserting four characters left both renderers painting
2618        // the highlight four columns off.
2619        //
2620        // Gated on the typed classifier rather than on `bump_gen` (which fires
2621        // for pure cursor moves too): re-scanning the document on every `j`
2622        // would be a per-keystroke full pass for no reason.
2623        if action.text_effect() == TextEffect::Mutates && self.search.pattern().is_some() {
2624            let text = self.active_text();
2625            self.search.refresh(&text);
2626            // NO manual invalidation of `search_at` here, deliberately. It is
2627            // `Anchored` to the text revision, so an ordinal computed against
2628            // the old text now reads as `None` on its own. This is the line
2629            // that used to have to be remembered.
2630        }
2631        // An action reached the executor ⇒ visible state may have changed.
2632        // Advance the refresh generation so the renderer repaints (and
2633        // re-highlights) exactly once. A gated-out key never reaches here, so
2634        // a key-repeat storm does not spin the renderer.
2635        self.bump_gen();
2636    }
2637
2638    /// Resolve a [`Motion`] from `from` to its target [`Position`] against the
2639    /// active buffer — **pure**: no cursor mutation, no side effects. This is
2640    /// the single motion-resolution source of truth that both [`apply_motion`]
2641    /// (move the cursor *to* the target) and [`apply_operator`] (use the target
2642    /// as the *other end* of an operated range) stand on. `None` only if there
2643    /// is no active buffer.
2644    ///
2645    /// [`apply_motion`]: Self::apply_motion
2646    /// [`apply_operator`]: Self::apply_operator
2647    fn resolve_motion(&self, from: Position, motion: Motion) -> Option<Position> {
2648        let buf = self.buffers.get(self.active)?;
2649        let pos = from;
2650        Some(match motion {
2651            // Search-as-motion: what makes `dn` / `d/foo<CR>` work. Resolved
2652            // against the committed match list, so it is `None` (motion fails,
2653            // operator aborts, buffer untouched) when nothing is committed —
2654            // never a silent move to 0, which would delete to the file start.
2655            Motion::SearchNext | Motion::SearchPrev => {
2656                let at = buf.position_to_char(pos).ok()?;
2657                let step = self
2658                    .search
2659                    .repeat(at, matches!(motion, Motion::SearchPrev))?;
2660                buf.char_to_position(step.target.start)
2661            }
2662            Motion::Left => Position::new(pos.line, pos.column.saturating_sub(1)),
2663            Motion::Right => Position::new(pos.line, pos.column.saturating_add(1)),
2664            Motion::Up => Position::new(pos.line.saturating_sub(1), pos.column),
2665            Motion::Down => Position::new(pos.line.saturating_add(1), pos.column),
2666            Motion::LineStart => Position::new(pos.line, 0),
2667            Motion::LineEnd => Position::new(pos.line, buf.line_len_chars(pos.line)),
2668            Motion::LineFirstNonBlank => first_non_blank(buf, pos.line),
2669            Motion::DocStart => Position::ZERO,
2670            Motion::DocEnd => Position::new(
2671                buf.line_count().saturating_sub(1),
2672                buf.line_len_chars(buf.line_count().saturating_sub(1)),
2673            ),
2674            Motion::WordStartNext | Motion::WordEndNext => word_next(buf, pos),
2675            Motion::WordStartPrev => word_prev(buf, pos),
2676            Motion::PageDown | Motion::HalfPageDown => {
2677                Position::new(pos.line.saturating_add(10), pos.column)
2678            }
2679            Motion::PageUp | Motion::HalfPageUp => {
2680                Position::new(pos.line.saturating_sub(10), pos.column)
2681            }
2682            Motion::GotoLine(n) => Position::new(n.saturating_sub(1), 0),
2683            // Structural Lisp motions — stubs for phase 1.B; full paredit
2684            // semantics land when caixa-ast is wired to the active buffer.
2685            Motion::ForwardSexp
2686            | Motion::BackwardSexp
2687            | Motion::UpList
2688            | Motion::DownList
2689            | Motion::BeginningOfDefun
2690            | Motion::EndOfDefun
2691            | Motion::BeginningOfSexp
2692            | Motion::EndOfSexp => pos,
2693        })
2694    }
2695
2696    fn apply_motion(&mut self, motion: Motion) {
2697        // A bare search motion is a FAR JUMP and it REPORTS — it records into
2698        // the jumplist, prints vim's "hit BOTTOM" on a wrap, and says E486
2699        // when nothing matches. `resolve_motion` can do none of that: it is
2700        // deliberately pure because the OPERATOR path calls it to find a range
2701        // without moving the cursor. So `n` routes to the one executor that
2702        // owns those side effects, and `Action::SearchRepeat` routes to the
2703        // same place — one code path, two spellings.
2704        if matches!(motion, Motion::SearchNext | Motion::SearchPrev) {
2705            self.jump_search(matches!(motion, Motion::SearchPrev));
2706            return;
2707        }
2708        let Some(pos) = self.resolve_motion(self.cursor(), motion) else {
2709            return;
2710        };
2711        // The single cursor-mutation path clamps to the buffer and scrolls
2712        // the viewport to contain the cursor on both axes.
2713        self.set_cursor(pos);
2714    }
2715
2716    /// Apply an operator over a motion — the vim `{operator}{motion}` verbs
2717    /// (`dw` delete-word, `c$` change-to-line-end, `y0` yank-to-line-start).
2718    /// Composition is explicit: the motion resolves a target via
2719    /// [`resolve_motion`](Self::resolve_motion); the operator acts over the
2720    /// `[cursor, target)` range. Register-leaving operators
2721    /// ([`Operator::leaves_register`]) capture the text first.
2722    fn apply_operator(&mut self, op: Operator, motion: Motion) {
2723        let from = self.cursor();
2724        let Some(to) = self.resolve_motion(from, motion) else {
2725            // A motion that cannot resolve aborts the operator with the buffer
2726            // untouched. A search motion says WHY — `dn` with no pattern armed
2727            // is otherwise indistinguishable from a dropped keystroke, which
2728            // is the same complaint that motivated E486 on the bare path.
2729            if matches!(motion, Motion::SearchNext | Motion::SearchPrev) {
2730                if self.search.pattern().is_none() {
2731                    self.messages
2732                        .push("E35: No previous regular expression".to_string());
2733                } else {
2734                    self.report_pattern_not_found();
2735                }
2736            }
2737            return;
2738        };
2739        self.apply_operator_to(op, to);
2740    }
2741
2742    /// Apply `op` over `[cursor, to)`.
2743    ///
2744    /// Split out of [`Self::apply_operator`] so the operated-search path can
2745    /// reach the same range machinery with a target it resolved itself — the
2746    /// alternative was a second copy of the delete/yank/register logic, which
2747    /// is how the two would drift.
2748    fn apply_operator_to(&mut self, op: Operator, to: Position) {
2749        let from = self.cursor();
2750        self.apply_operator_over(
2751            op,
2752            Range {
2753                start: from,
2754                end: to,
2755            },
2756        );
2757    }
2758
2759    /// Apply `op` over an explicit range.
2760    ///
2761    /// The object path needs this: `gn`'s extent need not begin at the cursor,
2762    /// so it cannot go through the `[cursor, target)` shape the motion path
2763    /// uses. One implementation of the delete/yank/register logic, reached two
2764    /// ways.
2765    fn apply_operator_over(&mut self, op: Operator, range: Range) {
2766        let range = range.normalized();
2767        if range.is_empty() {
2768            return;
2769        }
2770        // Capture the operated text (for the register) before mutating.
2771        let text = self
2772            .buffers
2773            .get(self.active)
2774            .and_then(|buf| buf.slice(range).ok());
2775        if op.leaves_register() {
2776            if let Some(t) = &text {
2777                self.register = Some(t.clone());
2778            }
2779        }
2780        match op {
2781            // Delete + Change remove the range; Change then enters Insert so
2782            // the operator pairs with immediate typing (`ciw`, `c$`).
2783            Operator::Delete | Operator::Change => {
2784                if let Some(buf) = self.buffers.get_mut(self.active) {
2785                    let _ = buf.apply(&Edit::delete(range));
2786                }
2787                self.set_cursor(range.start);
2788                if op == Operator::Change {
2789                    self.modal.enter(Mode::Insert);
2790                }
2791            }
2792            // Yank copies to the register without mutating the buffer; vim
2793            // leaves the cursor at the range start.
2794            Operator::Yank => {
2795                self.set_cursor(range.start);
2796            }
2797            // Indent/Format/structural operators are not yet wired — named,
2798            // not faked (no buffer mutation, register already captured for the
2799            // register-leaving ones above).
2800            _ => {
2801                self.messages
2802                    .push("operator not yet implemented".to_owned());
2803            }
2804        }
2805    }
2806
2807    /// The text last yanked or deleted into the unnamed register, if any.
2808    /// The future `p`/`P` paste reads this.
2809    #[must_use]
2810    pub fn register(&self) -> Option<&str> {
2811        self.register.as_deref()
2812    }
2813
2814    fn insert_char(&mut self, c: char) {
2815        if self.modal.mode() == Mode::Command {
2816            // A search prompt and an ex-command share Command mode (vim's
2817            // cmdline). `search.is_prompting()` is the typed discriminator —
2818            // it can only be true when `/` or `?` actually opened a prompt.
2819            if self.search.is_prompting() {
2820                // The search prompt is the SOLE store while it is open.
2821                //
2822                // This used to also `push_minibuffer(c)`, and the two stores
2823                // insert differently — `search.push` at the caret, the
2824                // minibuffer always at the end — so `/fo<Left>X` left them
2825                // reading `fXo` and `foX`. That was one of FIVE desync paths;
2826                // the caret moves, forward-delete, delete-word and
2827                // clear-to-start never touched the shadow at all.
2828                //
2829                // Deleting the write costs nothing because `status_model`
2830                // already selects the minibuffer only on the `prompt == None`
2831                // branch — the shadow is the EX-LINE's store, and while a
2832                // search prompt is open nothing reads it.
2833                self.search.push(c);
2834                self.preview_search();
2835            } else {
2836                self.modal.push_minibuffer(c);
2837            }
2838            return;
2839        }
2840        let cursor = self.cursor();
2841        let Some(buf) = self.buffers.get_mut(self.active) else {
2842            return;
2843        };
2844        let edit = Edit::insert(cursor, c.to_string());
2845        if buf.apply(&edit).is_ok() {
2846            let next = if c == '\n' {
2847                Position::new(cursor.line.saturating_add(1), 0)
2848            } else {
2849                cursor.shift_right(1)
2850            };
2851            // Route through the single cursor-mutation path so the viewport
2852            // follows the cursor (both axes) and the cursor stays clamped.
2853            self.set_cursor(next);
2854        }
2855    }
2856
2857    /// Backspace inside a prompt. Keeps the search buffer and the displayed
2858    /// minibuffer in lockstep — if only one shrank, the pattern submitted
2859    /// would differ from the text on screen.
2860    fn prompt_backspace(&mut self) -> bool {
2861        if self.modal.mode() != Mode::Command {
2862            return false;
2863        }
2864        if self.search.is_prompting() {
2865            // Backspacing past the `/` closes the prompt, as vim does. No
2866            // `pop_minibuffer` here for the same reason as `insert_char`: the
2867            // shadow is the ex-line's, and popping its TAIL when the caret is
2868            // mid-pattern was another desync path.
2869            if self.search.backspace() {
2870                self.modal.clear_minibuffer();
2871                self.modal.enter(Mode::Normal);
2872            }
2873            // Never `pop_minibuffer` on the search path: it pops the TAIL,
2874            // while `search.backspace()` removes the char before the CARET.
2875            return true;
2876        }
2877        self.modal.pop_minibuffer();
2878        true
2879    }
2880
2881    fn submit_command(&mut self) {
2882        // Read the command line BEFORE leaving Command mode — the minibuffer
2883        // exists only in the `Command` variant, so the escape must come
2884        // after the capture.
2885        let line = self.modal.minibuffer().to_string();
2886        self.modal.escape();
2887        let (name, args) = parse_command_line(&line);
2888        if name.is_empty() {
2889            return;
2890        }
2891        self.run_command(&name, &args);
2892    }
2893
2894    fn run_command(&mut self, name: &str, args: &[String]) {
2895        // Bound the command -> RunCommand slip -> command cycle. Refused and
2896        // reported, never a stack overflow: an editor that dies under the
2897        // operator loses their buffer, and a script that loops is a mistake
2898        // they should be told about, not punished for.
2899        if self.dispatch_depth >= Self::MAX_DISPATCH_DEPTH {
2900            let mut m = String::from("command recursion too deep at `");
2901            m.push_str(name);
2902            m.push_str("` — refusing");
2903            self.messages.push(m);
2904            self.damage = self.damage.join(Damage::Viewport);
2905            self.bump_gen();
2906            return;
2907        }
2908        self.dispatch_depth += 1;
2909        self.run_command_inner(name, args);
2910        self.dispatch_depth -= 1;
2911    }
2912
2913    /// How many nested command dispatches are allowed. Deep enough that no
2914    /// legitimate script notices, shallow enough to fail fast.
2915    const MAX_DISPATCH_DEPTH: u8 = 8;
2916
2917    fn run_command_inner(&mut self, name: &str, args: &[String]) {
2918        // Lazy-activation seam (lazy.nvim `cmd =` model): a user plugin
2919        // gated on `Command: <name>` has its entry applied the first time
2920        // that command runs, BEFORE dispatch — so the activated plugin
2921        // can register the very command being invoked and it resolves on
2922        // this same call.
2923        if self.plugin_host.pending() > 0 {
2924            let pending = self.plugin_host.pending_for_command(name);
2925            for src in pending {
2926                self.apply_plugin_entry(&src);
2927            }
2928        }
2929        // Read through the counter, then interpret. Two immutable borrows of
2930        // `self` (the window and the registry) coexist; the `&mut` comes
2931        // afterwards, once the outcome is owned. That sequencing IS the
2932        // seam: there is no moment where a command body and `&mut self` are
2933        // live at the same time.
2934        let outcome = {
2935            let window = self.window();
2936            self.commands.run(name, &window, args)
2937        };
2938        match outcome {
2939            Ok(o) => self.interpret(o),
2940            // Reported, never fatal (Phase 0). A failed command must not
2941            // take the editor down, but it must not be invisible either.
2942            Err(e) => {
2943                self.messages.push(describe_command_failure(name, &e));
2944                self.damage = self.damage.join(Damage::Viewport);
2945                self.bump_gen();
2946            }
2947        }
2948    }
2949
2950    // ── tatara-lisp runtime bridge (imperative programmability tier) ──
2951
2952    /// Capture a read snapshot of the editor for the tatara-lisp host.
2953    /// Lisp reads (`cursor-line`, `current-line`, …) answer from this.
2954    #[must_use]
2955    pub fn snapshot(&self) -> EditorSnapshot {
2956        let current_line = self
2957            .buffers
2958            .get(self.active)
2959            .and_then(|b| b.line(self.cursor().line))
2960            .map(|s| s.trim_end_matches('\n').to_string())
2961            .unwrap_or_default();
2962        let buffer_name = self
2963            .buffers
2964            .get(self.active)
2965            .and_then(|b| b.path.as_ref())
2966            .map(|p| p.display().to_string())
2967            .unwrap_or_else(|| "[scratch]".to_string());
2968        EditorSnapshot {
2969            cursor_line: i64::from(self.cursor().line),
2970            cursor_column: i64::from(self.cursor().column),
2971            current_line,
2972            mode: self.modal.mode().as_str().to_string(),
2973            buffer_name,
2974        }
2975    }
2976
2977    /// Evaluate tatara-lisp `src` against this editor: capture a
2978    /// snapshot, run it in the embedded VM, then apply the typed effects
2979    /// the program emitted. This is the imperative programmability tier
2980    /// — live Lisp that reads state and drives the editor through the
2981    /// sandboxed effect boundary.
2982    ///
2983    /// **Snapshot semantics:** the read snapshot is captured ONCE before
2984    /// eval, and effects are applied AFTER the program returns. So within
2985    /// a single `run_lisp` call a program cannot observe its own writes —
2986    /// `(insert "x") (cursor-column)` reads the pre-insert column. This
2987    /// snapshot-isolation is deliberate (it's what makes the effect
2988    /// boundary a clean sandbox seam); a program that must read its own
2989    /// effects splits the work across calls. The VM is cached
2990    /// ([`Self::lisp_vm`]) so the stdlib is installed once and top-level
2991    /// `define`s persist across calls (REPL-like).
2992    pub fn run_lisp(&mut self, src: &str) -> Result<(), VmError> {
2993        let mut host = EscribaHost::with_snapshot(self.snapshot());
2994        let vm = self.lisp_vm.get_or_insert_with(EscribaVm::new);
2995        vm.eval(src, &mut host)?;
2996        let effects = host.take_effects();
2997        self.apply_host_effects(effects);
2998        Ok(())
2999    }
3000
3001    /// Apply tatara-lisp effects to live editor state.
3002    ///
3003    /// A thin adapter now. It used to be `apply_host_effects`, a THIRD
3004    /// implementation of message-push / option-insert / insert-text beside
3005    /// the Action executor and the slip interpreter — the same duplication
3006    /// that let `u` and `:undo` drift apart in M3. The VM emits slips; this
3007    /// hands them to the one interpreter.
3008    pub fn apply_host_effects(&mut self, effects: Vec<Negai>) {
3009        self.interpret(Outcome::did(effects));
3010    }
3011
3012    /// Insert a (possibly multi-line) string at the cursor and advance
3013    /// the cursor past it. Used by the `(insert …)` effect.
3014    fn insert_text(&mut self, text: &str) {
3015        if text.is_empty() {
3016            return;
3017        }
3018        let cursor = self.cursor();
3019        let Some(buf) = self.buffers.get_mut(self.active) else {
3020            return;
3021        };
3022        let edit = Edit::insert(cursor, text.to_string());
3023        if buf.apply(&edit).is_ok() {
3024            let next = if let Some(nl) = text.rfind('\n') {
3025                let added_lines = u32::try_from(text.matches('\n').count()).unwrap_or(0);
3026                let last_line_len = u32::try_from(text[nl + 1..].chars().count()).unwrap_or(0);
3027                Position::new(cursor.line + added_lines, last_line_len)
3028            } else {
3029                let n = u32::try_from(text.chars().count()).unwrap_or(0);
3030                cursor.shift_right(n)
3031            };
3032            // Route through the single cursor-mutation path so the viewport
3033            // follows the cursor (both axes) and the cursor stays clamped.
3034            self.set_cursor(next);
3035        }
3036    }
3037}
3038
3039fn first_non_blank(buf: &escriba_buffer::Buffer, line: u32) -> Position {
3040    let Some(text) = buf.line(line) else {
3041        return Position::new(line, 0);
3042    };
3043    let col = text
3044        .chars()
3045        .take_while(|c| c.is_whitespace() && *c != '\n')
3046        .count();
3047    Position::new(line, u32::try_from(col).unwrap_or(0))
3048}
3049
3050fn word_next(buf: &escriba_buffer::Buffer, pos: Position) -> Position {
3051    let Some(text) = buf.line(pos.line) else {
3052        return pos;
3053    };
3054    let chars: Vec<char> = text.chars().collect();
3055    let start = pos.column as usize;
3056    let mut i = start;
3057    while i < chars.len() && !chars[i].is_whitespace() {
3058        i += 1;
3059    }
3060    while i < chars.len() && chars[i].is_whitespace() {
3061        i += 1;
3062    }
3063    if i >= chars.len() {
3064        // No more words on this line — jump to next line.
3065        if pos.line + 1 < buf.line_count() {
3066            return Position::new(pos.line + 1, 0);
3067        }
3068    }
3069    Position::new(pos.line, u32::try_from(i).unwrap_or(pos.column))
3070}
3071
3072fn word_prev(buf: &escriba_buffer::Buffer, pos: Position) -> Position {
3073    let Some(text) = buf.line(pos.line) else {
3074        return pos;
3075    };
3076    let chars: Vec<char> = text.chars().collect();
3077    let mut i = (pos.column as usize).min(chars.len());
3078    while i > 0 && chars[i - 1].is_whitespace() {
3079        i -= 1;
3080    }
3081    while i > 0 && !chars[i - 1].is_whitespace() {
3082        i -= 1;
3083    }
3084    Position::new(pos.line, u32::try_from(i).unwrap_or(0))
3085}
3086
3087fn parse_command_line(line: &str) -> (String, Vec<String>) {
3088    let mut parts = line.split_whitespace();
3089    let Some(first) = parts.next() else {
3090        return (String::new(), Vec::new());
3091    };
3092    let head = first.strip_prefix(':').unwrap_or(first);
3093    let name = match head {
3094        "w" => "save",
3095        "q" => "quit",
3096        "u" => "undo",
3097        other => other,
3098    };
3099    (name.to_string(), parts.map(str::to_string).collect())
3100}
3101
3102#[cfg(test)]
3103mod tests {
3104    use super::*;
3105    use madori::event::{KeyCode, KeyEvent, Modifiers};
3106
3107    // ── search wiring (escriba-search integration) ────────────────────
3108    //
3109    // The engine is proven in escriba-search's own 61 tests. These prove the
3110    // WIRING: that keys reach it, that the cursor lands where it says, and
3111    // that a search prompt and an ex-command can share Command mode without
3112    // being confused for one another.
3113
3114    fn type_search(st: &mut EditorState, dir: SearchDirection, pat: &str) {
3115        st.apply(&Action::SearchOpen(dir));
3116        for c in pat.chars() {
3117            st.apply(&Action::InsertChar(c));
3118        }
3119        st.apply(&Action::SubmitCommand);
3120    }
3121
3122    #[test]
3123    fn slash_search_moves_the_cursor_to_the_match() {
3124        let mut st = new_state_with("alpha\nbravo\ncharlie\n");
3125        type_search(&mut st, SearchDirection::Forward, "charlie");
3126        assert_eq!(st.cursor().line, 2, "cursor lands on the matching line");
3127        assert_eq!(st.modal.mode(), Mode::Normal, "prompt closes on submit");
3128        assert_eq!(st.search.matches().len(), 1);
3129    }
3130
3131    #[test]
3132    // `N` is a DIFFERENT vim key from `n` — see escriba-search.
3133    #[allow(non_snake_case)]
3134    fn n_and_N_walk_matches_in_both_directions() {
3135        let mut st = new_state_with("foo\nbar\nfoo\nbaz\nfoo\n");
3136        type_search(&mut st, SearchDirection::Forward, "foo");
3137        let first = st.cursor().line;
3138        st.apply(&Action::SearchRepeat { reverse: false });
3139        let second = st.cursor().line;
3140        assert!(second > first, "n advances ({first} -> {second})");
3141        st.apply(&Action::SearchRepeat { reverse: true });
3142        assert_eq!(st.cursor().line, first, "N comes back");
3143    }
3144
3145    #[test]
3146    fn star_searches_the_word_under_the_cursor() {
3147        let mut st = new_state_with("needle\nhaystack\nneedle\n");
3148        st.apply(&Action::SearchWord { reverse: false });
3149        assert_eq!(st.search.pattern().unwrap().raw(), r"\bneedle\b");
3150        assert_eq!(st.cursor().line, 2, "jumps to the other occurrence");
3151    }
3152
3153    #[test]
3154    fn escape_abandons_the_prompt_and_keeps_the_previous_search() {
3155        let mut st = new_state_with("foo\nbar\nfoo\n");
3156        type_search(&mut st, SearchDirection::Forward, "foo");
3157        let matches_before = st.search.matches().len();
3158
3159        st.apply(&Action::SearchOpen(SearchDirection::Forward));
3160        st.apply(&Action::InsertChar('z'));
3161        st.apply(&Action::ChangeMode(Mode::Normal));
3162
3163        assert!(!st.search.is_prompting(), "prompt gone");
3164        assert_eq!(
3165            st.search.pattern().unwrap().raw(),
3166            "foo",
3167            "old pattern survives"
3168        );
3169        assert_eq!(
3170            st.search.matches().len(),
3171            matches_before,
3172            "old highlights survive"
3173        );
3174    }
3175
3176    #[test]
3177    fn a_search_prompt_and_an_ex_command_are_not_confused() {
3178        let mut st = new_state_with("foo\n");
3179        // No `/` pressed: Command mode belongs to the ex-command line.
3180        st.apply(&Action::ChangeMode(Mode::Command));
3181        assert!(!st.search.is_prompting(), "`:` must not open a search");
3182        st.apply(&Action::InsertChar('w'));
3183        assert!(
3184            st.search.prompt().is_none(),
3185            "typed char went to the ex line"
3186        );
3187    }
3188
3189    #[test]
3190    fn a_missing_pattern_reports_instead_of_failing_silently() {
3191        let mut st = new_state_with("alpha\nbravo\n");
3192        type_search(&mut st, SearchDirection::Forward, "zzz");
3193        assert!(
3194            st.messages.iter().any(|m| m.contains("E486")),
3195            "must report not-found, got {:?}",
3196            st.messages
3197        );
3198    }
3199
3200    #[test]
3201    fn n_without_any_search_reports_rather_than_moving() {
3202        let mut st = new_state_with("alpha\nbravo\n");
3203        let before = st.cursor();
3204        st.apply(&Action::SearchRepeat { reverse: false });
3205        assert_eq!(st.cursor(), before, "cursor must not move");
3206        assert!(
3207            st.messages.iter().any(|m| m.contains("E35")),
3208            "got {:?}",
3209            st.messages
3210        );
3211    }
3212
3213    #[test]
3214    fn search_as_a_motion_composes_with_an_operator() {
3215        // The point of Motion::SearchNext: `d` + search deletes to the match.
3216        let mut st = new_state_with("alpha bravo charlie\n");
3217        type_search(&mut st, SearchDirection::Forward, "charlie");
3218        st.set_cursor(Position::new(0, 0));
3219        let target = st.resolve_motion(Position::new(0, 0), Motion::SearchNext);
3220        assert!(target.is_some(), "search must resolve as a motion");
3221        assert_eq!(target.unwrap().column, 12, "at `charlie`");
3222    }
3223
3224    #[test]
3225    fn search_motion_without_a_pattern_fails_the_motion_instead_of_moving_to_zero() {
3226        // A silent fallback to offset 0 would make `d` + search delete to the
3227        // start of the file — the worst possible failure for an operator.
3228        let st = new_state_with("alpha bravo\n");
3229        assert!(
3230            st.resolve_motion(Position::new(0, 5), Motion::SearchNext)
3231                .is_none()
3232        );
3233    }
3234
3235    #[test]
3236    fn clear_highlight_keeps_the_pattern_usable() {
3237        let mut st = new_state_with("foo\nbar\nfoo\n");
3238        type_search(&mut st, SearchDirection::Forward, "foo");
3239        st.apply(&Action::ClearSearchHighlight);
3240        assert!(st.search.highlights().is_empty(), "nothing lit");
3241        st.apply(&Action::SearchRepeat { reverse: false });
3242        assert!(st.search.pattern().is_some(), "but n still works");
3243    }
3244
3245    #[test]
3246    fn typing_previews_incrementally_before_commit() {
3247        let mut st = new_state_with("alpha\nbravo\ncharlie\n");
3248        st.apply(&Action::SearchOpen(SearchDirection::Forward));
3249        for c in "charlie".chars() {
3250            st.apply(&Action::InsertChar(c));
3251        }
3252        // incsearch: the cursor has already moved, with nothing committed.
3253        assert_eq!(st.cursor().line, 2, "preview moved the cursor");
3254        assert!(st.search.pattern().is_none(), "but nothing is committed");
3255    }
3256
3257    #[test]
3258    fn backspace_corrects_the_prompt_and_reruns_the_preview() {
3259        let mut st = new_state_with("alpha\nbravo\n");
3260        st.apply(&Action::SearchOpen(SearchDirection::Forward));
3261        for c in "bravox".chars() {
3262            st.apply(&Action::InsertChar(c));
3263        }
3264        assert_eq!(st.search.prompt().unwrap().text(), "bravox");
3265        st.apply(&Action::PromptBackspace);
3266        assert_eq!(
3267            st.search.prompt().unwrap().text(),
3268            "bravo",
3269            "typo corrected"
3270        );
3271        assert_eq!(
3272            st.status_model().prompt_text,
3273            "bravo",
3274            "the model reads the PROMPT — the minibuffer is the ex-line's store",
3275        );
3276        assert_eq!(st.cursor().line, 1, "preview re-ran and found it");
3277    }
3278
3279    #[test]
3280    fn backspacing_past_the_slash_closes_the_prompt() {
3281        let mut st = new_state_with("alpha\n");
3282        st.apply(&Action::SearchOpen(SearchDirection::Forward));
3283        st.apply(&Action::InsertChar('a'));
3284        st.apply(&Action::PromptBackspace);
3285        st.apply(&Action::PromptBackspace);
3286        assert!(!st.search.is_prompting(), "prompt closed");
3287        assert_eq!(st.modal.mode(), Mode::Normal);
3288    }
3289
3290    #[test]
3291    fn noh_clears_highlights_and_keeps_the_pattern() {
3292        let mut st = new_state_with("foo\nbar\nfoo\n");
3293        type_search(&mut st, SearchDirection::Forward, "foo");
3294        assert!(!st.search.highlights().is_empty());
3295        st.run_command("noh", &[]);
3296        assert!(st.search.highlights().is_empty(), ":noh turns them off");
3297        assert!(st.search.pattern().is_some(), "but n still works");
3298    }
3299
3300    #[test]
3301    fn noh_accepts_the_vim_aliases() {
3302        for name in ["noh", "nohl", "nohlsearch"] {
3303            let mut st = new_state_with("foo\nfoo\n");
3304            type_search(&mut st, SearchDirection::Forward, "foo");
3305            st.run_command(name, &[]);
3306            assert!(st.search.highlights().is_empty(), "{name} must clear");
3307        }
3308    }
3309
3310    #[test]
3311    fn backspace_on_the_ex_line_does_not_touch_search_state() {
3312        let mut st = new_state_with("foo\n");
3313        st.apply(&Action::ChangeMode(Mode::Command));
3314        st.apply(&Action::InsertChar('w'));
3315        st.apply(&Action::InsertChar('q'));
3316        st.apply(&Action::PromptBackspace);
3317        assert_eq!(st.status_model().prompt_text, "w");
3318        assert!(st.search.prompt().is_none(), "no search was involved");
3319    }
3320
3321    #[test]
3322    fn up_arrow_recalls_the_previous_search() {
3323        let mut st = new_state_with("alpha\nbravo\n");
3324        type_search(&mut st, SearchDirection::Forward, "bravo");
3325        st.apply(&Action::SearchOpen(SearchDirection::Forward));
3326        st.apply(&Action::PromptHistory { back: true });
3327        assert_eq!(st.search.prompt().unwrap().text(), "bravo");
3328        assert_eq!(
3329            st.status_model().prompt_text,
3330            "bravo",
3331            "display follows the prompt"
3332        );
3333    }
3334
3335    #[test]
3336    fn arrowing_back_down_restores_the_half_typed_pattern() {
3337        let mut st = new_state_with("alpha\nbravo\n");
3338        type_search(&mut st, SearchDirection::Forward, "bravo");
3339        st.apply(&Action::SearchOpen(SearchDirection::Forward));
3340        st.apply(&Action::InsertChar('a'));
3341        st.apply(&Action::PromptHistory { back: true });
3342        assert_eq!(st.search.prompt().unwrap().text(), "bravo");
3343        st.apply(&Action::PromptHistory { back: false });
3344        assert_eq!(
3345            st.search.prompt().unwrap().text(),
3346            "a",
3347            "the draft comes back"
3348        );
3349        assert_eq!(st.status_model().prompt_text, "a");
3350    }
3351
3352    #[test]
3353    fn history_arrows_do_nothing_on_the_ex_line() {
3354        let mut st = new_state_with("alpha\n");
3355        st.apply(&Action::ChangeMode(Mode::Command));
3356        st.apply(&Action::InsertChar('w'));
3357        st.apply(&Action::PromptHistory { back: true });
3358        assert_eq!(st.status_model().prompt_text, "w", "ex line untouched");
3359    }
3360
3361    // ── trouble.* — the findings view ────────────────────────────────
3362    //
3363    // These assert on the ROWS the picker would be built from, not on the
3364    // registry: the registry is already tested, and what could be wrong
3365    // here is the projection — scoping, freshness, and whether a row goes
3366    // anywhere when pressed.
3367
3368    fn finding_at(buffer: BufferId, line: u32, msg: &str) -> escriba_shirube::Finding {
3369        use escriba_core::{Position, Range};
3370        escriba_shirube::Finding::new(
3371            escriba_shirube::Site::in_buffer(
3372                buffer,
3373                Range::new(Position::new(line, 0), Position::new(line, 1)),
3374            ),
3375            escriba_shirube::Severity::Error,
3376            msg.to_string(),
3377            escriba_shirube::Origin::Text("test"),
3378        )
3379    }
3380
3381    #[test]
3382    fn published_findings_become_picker_rows() {
3383        let mut st = new_state_with("a\nb\nc\n");
3384        let world = st.world();
3385        st.results.publish(
3386            "test",
3387            escriba_shirube::ResultList::new(vec![finding_at(st.active, 1, "boom")], world),
3388        );
3389        let rows = st.finding_items(true);
3390        assert_eq!(rows.len(), 1, "the published finding produces a row");
3391        // The row must SAY something an operator can act on: severity,
3392        // 1-based line, and the message.
3393        let label = &rows[0].label;
3394        assert!(label.contains("ERROR"), "{label}");
3395        assert!(label.contains(":2"), "lines are 1-based on screen: {label}");
3396        assert!(label.contains("boom"), "{label}");
3397    }
3398
3399    #[test]
3400    fn a_stale_list_contributes_no_rows() {
3401        // THE load-bearing one. A list anchored to a revision the buffer has
3402        // moved past must vanish from the view rather than offer a line that
3403        // has since shifted — which is the whole reason findings carry an
3404        // anchor instead of just a position.
3405        let mut st = new_state_with("a\nb\nc\n");
3406        let world = st.world();
3407        st.results.publish(
3408            "test",
3409            escriba_shirube::ResultList::new(vec![finding_at(st.active, 1, "boom")], world),
3410        );
3411        assert_eq!(st.finding_items(true).len(), 1, "fresh to begin with");
3412
3413        st.apply(&Action::InsertChar('x'));
3414        assert!(
3415            st.finding_items(true).is_empty(),
3416            "an edit moved the text on; the list is stale and must not be shown"
3417        );
3418    }
3419
3420    #[test]
3421    fn document_scope_excludes_another_buffer() {
3422        // `trouble.document` vs `trouble.workspace` is one bool, so this is
3423        // the only thing that can distinguish them.
3424        let mut st = new_state_with("a\nb\n");
3425        let other = st.buffers.scratch("z\n");
3426        let world = st.world();
3427        st.results.publish(
3428            "test",
3429            escriba_shirube::ResultList::new(
3430                vec![
3431                    finding_at(st.active, 0, "mine"),
3432                    finding_at(other, 0, "theirs"),
3433                ],
3434                world,
3435            ),
3436        );
3437        let ws = st.finding_items(true);
3438        assert_eq!(ws.len(), 2, "workspace scope shows both");
3439        let doc = st.finding_items(false);
3440        assert_eq!(doc.len(), 1, "document scope shows only the active buffer");
3441        assert!(doc[0].label.contains("mine"), "{}", doc[0].label);
3442    }
3443
3444    #[test]
3445    fn files_under_a_root_produces_rows() {
3446        // `files.open-parent` differs from `files.open` only in the root, so
3447        // what must hold is that a root is actually honoured.
3448        let mut st = new_state_with("");
3449        let rows = st.file_items(std::path::Path::new("."));
3450        assert!(!rows.is_empty(), "the working directory has files");
3451    }
3452
3453    // ── vim text objects ─────────────────────────────────────────────
3454    //
3455    // Asserted through `apply` on real buffer text, so a wrong RANGE shows
3456    // up as wrong text rather than as a range that merely looks plausible.
3457
3458    fn after(text: &str, line: u32, col: u32, act: Action) -> String {
3459        let mut st = new_state_with(text);
3460        st.set_cursor(Position::new(line, col));
3461        st.apply(&act);
3462        st.buffers
3463            .get(st.active)
3464            .map(|b| b.to_string())
3465            .unwrap_or_default()
3466    }
3467
3468    fn del_obj(o: escriba_core::TextObject) -> Action {
3469        Action::ApplyOperatorObject {
3470            op: escriba_core::Operator::Delete,
3471            object: o,
3472        }
3473    }
3474
3475    #[test]
3476    fn dd_removes_the_line_not_just_its_contents() {
3477        // The distinction the newline makes: without it, `dd` blanks a line
3478        // and leaves it behind.
3479        let got = after("a\nb\nc\n", 1, 0, del_obj(escriba_core::TextObject::Line));
3480        assert_eq!(got, "a\nc\n");
3481    }
3482
3483    #[test]
3484    fn dd_on_the_last_line_leaves_no_blank_behind() {
3485        // The case a naive start-of-line..start-of-next range gets wrong:
3486        // there is no following newline to take, so it must take the
3487        // preceding one.
3488        let got = after("a\nb\nc\n", 2, 0, del_obj(escriba_core::TextObject::Line));
3489        assert_eq!(got, "a\nb\n", "no trailing empty line: {got:?}");
3490    }
3491
3492    #[test]
3493    fn dd_on_the_only_line_clears_it_but_keeps_the_line() {
3494        let got = after("solo\n", 0, 2, del_obj(escriba_core::TextObject::Line));
3495        assert!(got.starts_with('\n') || got.is_empty(), "{got:?}");
3496    }
3497
3498    #[test]
3499    fn diw_takes_the_word_and_daw_takes_its_trailing_space() {
3500        let inner = after(
3501            "one two three\n",
3502            0,
3503            5,
3504            del_obj(escriba_core::TextObject::Word { around: false }),
3505        );
3506        assert_eq!(inner, "one  three\n", "iw leaves both spaces");
3507        let around = after(
3508            "one two three\n",
3509            0,
3510            5,
3511            del_obj(escriba_core::TextObject::Word { around: true }),
3512        );
3513        assert_eq!(around, "one three\n", "aw takes the trailing space");
3514    }
3515
3516    #[test]
3517    fn iw_from_any_column_inside_the_word_takes_the_whole_word() {
3518        for col in 4..=6 {
3519            let got = after(
3520                "one two three\n",
3521                0,
3522                col,
3523                del_obj(escriba_core::TextObject::Word { around: false }),
3524            );
3525            assert_eq!(got, "one  three\n", "from column {col}");
3526        }
3527    }
3528
3529    #[test]
3530    fn iw_on_punctuation_takes_the_punctuation_run() {
3531        // vim's three classes: word / punctuation / whitespace. A `::` is a
3532        // run of punctuation, not part of either identifier.
3533        let got = after(
3534            "foo::bar\n",
3535            0,
3536            3,
3537            del_obj(escriba_core::TextObject::Word { around: false }),
3538        );
3539        assert_eq!(got, "foobar\n");
3540    }
3541
3542    #[test]
3543    fn i_paren_takes_the_inside_and_a_paren_takes_the_brackets_too() {
3544        let inner = after(
3545            "f(a, b)\n",
3546            0,
3547            3,
3548            del_obj(escriba_core::TextObject::Delimited {
3549                open: '(',
3550                close: ')',
3551                around: false,
3552            }),
3553        );
3554        assert_eq!(inner, "f()\n");
3555        let around = after(
3556            "f(a, b)\n",
3557            0,
3558            3,
3559            del_obj(escriba_core::TextObject::Delimited {
3560                open: '(',
3561                close: ')',
3562                around: true,
3563            }),
3564        );
3565        assert_eq!(around, "f\n");
3566    }
3567
3568    #[test]
3569    fn nested_brackets_resolve_to_the_enclosing_pair() {
3570        // THE reason the bracket scan counts depth: an inner pair must not
3571        // terminate the search for the one the cursor is actually inside.
3572        let got = after(
3573            "f(g(x), y)\n",
3574            0,
3575            8,
3576            del_obj(escriba_core::TextObject::Delimited {
3577                open: '(',
3578                close: ')',
3579                around: false,
3580            }),
3581        );
3582        assert_eq!(got, "f()\n", "took the outer pair");
3583    }
3584
3585    #[test]
3586    fn quotes_do_not_nest_so_the_nearest_pair_wins() {
3587        let got = after(
3588            r#"say "hi there" ok"#,
3589            0,
3590            7,
3591            del_obj(escriba_core::TextObject::Delimited {
3592                open: '"',
3593                close: '"',
3594                around: false,
3595            }),
3596        );
3597        assert_eq!(got, "say \"\" ok");
3598    }
3599
3600    #[test]
3601    fn an_unmatched_delimiter_resolves_to_nothing_rather_than_guessing() {
3602        let mut st = new_state_with("f(a, b\n");
3603        st.set_cursor(Position::new(0, 3));
3604        let before = st
3605            .buffers
3606            .get(st.active)
3607            .map(|b| b.to_string())
3608            .unwrap_or_default();
3609        st.apply(&del_obj(escriba_core::TextObject::Delimited {
3610            open: '(',
3611            close: ')',
3612            around: false,
3613        }));
3614        let got_after = st
3615            .buffers
3616            .get(st.active)
3617            .map(|b| b.to_string())
3618            .unwrap_or_default();
3619        assert_eq!(got_after, before, "no closing bracket: change nothing");
3620    }
3621
3622    fn new_state_with(text: &str) -> EditorState {
3623        let mut bufs = BufferSet::new();
3624        let id = bufs.scratch(text);
3625        EditorState::new_with_buffer(bufs, id)
3626    }
3627
3628    /// The refresh-seal driver (theory/ESCRIBA.md §Refresh-Seal): an applied
3629    /// action advances `edit_gen` (so the renderer repaints), and merely
3630    /// reading the generation does not. This is what lets `gpu.rs` gate the
3631    /// re-highlight/re-shape on a generation change — an idle frame observes an
3632    /// unchanged generation and reuses its cached buffer.
3633    #[test]
3634    fn edit_gen_advances_on_applied_action_not_on_read() {
3635        let mut s = new_state_with("hello\nworld\n");
3636        let g0 = s.edit_gen();
3637        s.apply(&Action::InsertChar('X'));
3638        assert_ne!(
3639            s.edit_gen(),
3640            g0,
3641            "an applied action must advance the refresh generation",
3642        );
3643        // Reading the generation is not a mutation — idle frames stay put.
3644        let g1 = s.edit_gen();
3645        assert_eq!(s.edit_gen(), g1, "reading edit_gen must not advance it");
3646    }
3647
3648    /// The M1 refresh node (theory/ESCRIBA.md §X): a mutation widens the typed
3649    /// `Damage` to cover exactly what changed — local for an in-place edit,
3650    /// to-end-of-document when the line count shifts — and the renderer drains
3651    /// it per frame. `Damage ⊇ changed` by construction; it never narrows.
3652    #[test]
3653    fn damage_tracks_edit_scope_and_drains() {
3654        let mut s = new_state_with("hello\nworld\n");
3655        assert!(s.damage().is_none(), "a fresh state has no damage");
3656
3657        s.apply(&Action::InsertChar('X')); // in-place edit on line 0
3658        assert_eq!(
3659            s.damage(),
3660            Damage::Lines { from: 0, to: 0 },
3661            "a local edit damages just its line",
3662        );
3663
3664        let drained = s.take_damage();
3665        assert_eq!(drained, Damage::Lines { from: 0, to: 0 });
3666        assert!(s.damage().is_none(), "take_damage drains to None");
3667
3668        s.apply(&Action::InsertChar('\n')); // splits line 0 → line count grows
3669        assert_eq!(
3670            s.damage(),
3671            Damage::Lines {
3672                from: 0,
3673                to: u32::MAX,
3674            },
3675            "a line-count change damages to end-of-document",
3676        );
3677    }
3678
3679    /// A state whose active window is a deliberately tiny viewport
3680    /// (`visible_lines` × `visible_columns`) so the scroll-to-contain
3681    /// invariant is exercised on small inputs.
3682    fn new_state_small_viewport(text: &str, vis_lines: u32, vis_cols: u32) -> EditorState {
3683        let mut s = new_state_with(text);
3684        for w in s.layout.windows_mut() {
3685            w.viewport.visible_lines = vis_lines;
3686            w.viewport.visible_columns = vis_cols;
3687        }
3688        s
3689    }
3690
3691    /// The core regression invariant: the active window's viewport CONTAINS
3692    /// the cursor on BOTH axes. This is the operator's exact complaint —
3693    /// "typing past the bottom (or right) leaves the cursor off-screen" —
3694    /// made into a checkable property.
3695    fn assert_cursor_in_viewport(s: &EditorState, ctx: &str) {
3696        let w = s.layout.active_window().expect("active window");
3697        let v = w.viewport;
3698        let c = s.cursor();
3699        assert!(
3700            v.top_line <= c.line && c.line < v.top_line + v.visible_lines,
3701            "[{ctx}] cursor line {} not in vertical window [{}, {}); viewport={v:?}",
3702            c.line,
3703            v.top_line,
3704            v.top_line + v.visible_lines,
3705        );
3706        assert!(
3707            v.left_column <= c.column && c.column < v.left_column + v.visible_columns,
3708            "[{ctx}] cursor column {} not in horizontal window [{}, {}); viewport={v:?}",
3709            c.column,
3710            v.left_column,
3711            v.left_column + v.visible_columns,
3712        );
3713    }
3714
3715    /// `dd` from the KEYBOARD, not from a synthesized action.
3716    ///
3717    /// The FSM composition is unit-tested, but what an operator actually
3718    /// does is press `d` twice — and that path goes through the keymap and
3719    /// the sequence stepper, either of which could swallow the second `d`.
3720    #[test]
3721    fn pressing_d_twice_deletes_the_line() {
3722        let mut st = new_state_with("alpha\nbeta\ngamma\n");
3723        st.set_cursor(Position::new(1, 0));
3724        st.tick(&press(KeyCode::Char('d')));
3725        st.tick(&press(KeyCode::Char('d')));
3726        let got = st
3727            .buffers
3728            .get(st.active)
3729            .map(|b| b.to_string())
3730            .unwrap_or_default();
3731        assert_eq!(got, "alpha\ngamma\n", "dd from the keyboard");
3732    }
3733
3734    #[test]
3735    fn pressing_2_d_d_deletes_two_lines() {
3736        let mut st = new_state_with("a\nb\nc\nd\n");
3737        st.set_cursor(Position::new(0, 0));
3738        for k in ['2', 'd', 'd'] {
3739            st.tick(&press(KeyCode::Char(k)));
3740        }
3741        let got = st
3742            .buffers
3743            .get(st.active)
3744            .map(|b| b.to_string())
3745            .unwrap_or_default();
3746        assert_eq!(got, "c\nd\n", "count applies to the doubled operator");
3747    }
3748
3749    /// Text objects FROM THE KEYBOARD — the layer the last commit said was
3750    /// missing. `i` is `ChangeMode(Insert)` in Normal and `a` and every
3751    /// bracket are unbound, so all of this had to be decided on the KEY.
3752
3753    fn keys(text: &str, line: u32, col: u32, seq: &str) -> String {
3754        let mut st = new_state_with(text);
3755        st.set_cursor(Position::new(line, col));
3756        for c in seq.chars() {
3757            st.tick(&press(KeyCode::Char(c)));
3758        }
3759        st.buffers
3760            .get(st.active)
3761            .map(|b| b.to_string())
3762            .unwrap_or_default()
3763    }
3764
3765    #[test]
3766    fn diw_from_the_keyboard() {
3767        assert_eq!(keys("one two three\n", 0, 5, "diw"), "one  three\n");
3768    }
3769
3770    #[test]
3771    fn daw_from_the_keyboard_takes_the_space() {
3772        assert_eq!(keys("one two three\n", 0, 5, "daw"), "one three\n");
3773    }
3774
3775    #[test]
3776    fn ciw_deletes_and_enters_insert() {
3777        let mut st = new_state_with("one two\n");
3778        st.set_cursor(Position::new(0, 5));
3779        for c in "ciw".chars() {
3780            st.tick(&press(KeyCode::Char(c)));
3781        }
3782        assert_eq!(st.modal.mode(), Mode::Insert, "change leaves you inserting");
3783        let got = st
3784            .buffers
3785            .get(st.active)
3786            .map(|b| b.to_string())
3787            .unwrap_or_default();
3788        assert_eq!(got, "one \n");
3789    }
3790
3791    #[test]
3792    fn di_paren_and_da_paren_from_the_keyboard() {
3793        assert_eq!(keys("f(a, b)\n", 0, 3, "di("), "f()\n");
3794        assert_eq!(keys("f(a, b)\n", 0, 3, "da("), "f\n");
3795    }
3796
3797    #[test]
3798    fn the_closing_bracket_and_b_are_aliases() {
3799        // vim accepts `i(`, `i)` and `ib` for the same object.
3800        for sel in ["di(", "di)", "dib"] {
3801            assert_eq!(keys("f(a, b)\n", 0, 3, sel), "f()\n", "{sel}");
3802        }
3803    }
3804
3805    #[test]
3806    fn di_quote_from_the_keyboard() {
3807        assert_eq!(keys("say \"hi\" ok\n", 0, 6, "di\""), "say \"\" ok\n");
3808    }
3809
3810    #[test]
3811    fn i_alone_still_enters_insert_when_no_operator_is_pending() {
3812        // The load-bearing negative: the object layer must not steal `i`
3813        // from ordinary use.
3814        let mut st = new_state_with("abc\n");
3815        st.tick(&press(KeyCode::Char('i')));
3816        assert_eq!(st.modal.mode(), Mode::Insert);
3817    }
3818
3819    #[test]
3820    fn an_unknown_object_key_cancels_rather_than_staying_armed() {
3821        // `diz` is not an object. The operator must disarm, and the buffer
3822        // must be untouched — not left waiting to eat the next keystroke.
3823        let mut st = new_state_with("one two\n");
3824        st.set_cursor(Position::new(0, 5));
3825        for c in "diz".chars() {
3826            st.tick(&press(KeyCode::Char(c)));
3827        }
3828        let got = st
3829            .buffers
3830            .get(st.active)
3831            .map(|b| b.to_string())
3832            .unwrap_or_default();
3833        assert_eq!(got, "one two\n", "nothing was deleted");
3834        assert_eq!(*st.op_pending.state(), OpState::Resting, "and it disarmed");
3835    }
3836
3837    fn press(kc: KeyCode) -> AppEvent {
3838        AppEvent::Key(KeyEvent {
3839            key: kc,
3840            pressed: true,
3841            modifiers: Modifiers::default(),
3842            text: None,
3843        })
3844    }
3845
3846    // ── operator-over-motion (the `dw`/`c$`/`y0` verbs) ──────────────
3847
3848    fn line0_len(s: &EditorState) -> u32 {
3849        s.buffers.get(s.active).unwrap().line_len_chars(0)
3850    }
3851
3852    #[test]
3853    fn delete_to_line_end_clears_line_and_fills_register() {
3854        let mut s = new_state_with("hello world");
3855        s.apply(&Action::ApplyOperator {
3856            op: Operator::Delete,
3857            motion: Motion::LineEnd,
3858        });
3859        assert_eq!(line0_len(&s), 0, "d$ deletes to end of line");
3860        assert_eq!(
3861            s.register(),
3862            Some("hello world"),
3863            "delete fills the register"
3864        );
3865        assert_eq!(
3866            s.cursor(),
3867            Position::ZERO,
3868            "cursor lands at the range start"
3869        );
3870    }
3871
3872    #[test]
3873    fn delete_over_right_motion_removes_one_char() {
3874        let mut s = new_state_with("abc");
3875        s.apply(&Action::ApplyOperator {
3876            op: Operator::Delete,
3877            motion: Motion::Right,
3878        });
3879        assert_eq!(
3880            s.buffers.get(s.active).unwrap().line(0).as_deref(),
3881            Some("bc")
3882        );
3883        assert_eq!(s.register(), Some("a"));
3884    }
3885
3886    #[test]
3887    fn change_to_line_end_deletes_and_enters_insert() {
3888        let mut s = new_state_with("hello world");
3889        assert_eq!(s.modal.mode(), Mode::Normal);
3890        s.apply(&Action::ApplyOperator {
3891            op: Operator::Change,
3892            motion: Motion::LineEnd,
3893        });
3894        assert_eq!(line0_len(&s), 0, "c$ deletes the range");
3895        assert_eq!(
3896            s.modal.mode(),
3897            Mode::Insert,
3898            "change enters Insert to type the replacement"
3899        );
3900        assert_eq!(
3901            s.register(),
3902            Some("hello world"),
3903            "change fills the register"
3904        );
3905    }
3906
3907    #[test]
3908    fn yank_to_line_end_fills_register_without_mutating() {
3909        let mut s = new_state_with("hello world");
3910        s.apply(&Action::ApplyOperator {
3911            op: Operator::Yank,
3912            motion: Motion::LineEnd,
3913        });
3914        assert_eq!(line0_len(&s), 11, "yank does not mutate the buffer");
3915        assert_eq!(s.register(), Some("hello world"), "yank fills the register");
3916        assert_eq!(s.modal.mode(), Mode::Normal, "yank stays in Normal");
3917    }
3918
3919    #[test]
3920    fn resolve_motion_is_the_shared_target_for_move_and_operator() {
3921        // The encapsulation proof: apply_motion (cursor move) and
3922        // apply_operator (range end) BOTH stand on resolve_motion — so a move
3923        // to LineEnd lands at exactly the position the operator deletes to.
3924        let mut s = new_state_with("hello world");
3925        let target = s.resolve_motion(Position::ZERO, Motion::LineEnd).unwrap();
3926        assert_eq!(target, Position::new(0, 11));
3927        s.apply_motion(Motion::LineEnd);
3928        assert_eq!(
3929            s.cursor(),
3930            target,
3931            "the move path resolves the same target the operator uses"
3932        );
3933    }
3934
3935    #[test]
3936    fn empty_motion_range_is_a_no_op() {
3937        // An operator over a zero-width motion (cursor already at line start)
3938        // mutates nothing and leaves the register untouched.
3939        let mut s = new_state_with("abc");
3940        s.apply(&Action::ApplyOperator {
3941            op: Operator::Delete,
3942            motion: Motion::LineStart,
3943        });
3944        assert_eq!(
3945            s.buffers.get(s.active).unwrap().line(0).as_deref(),
3946            Some("abc")
3947        );
3948        assert_eq!(s.register(), None);
3949    }
3950
3951    #[test]
3952    fn operator_then_motion_composes_through_the_pending_fsm() {
3953        // The full keymap→FSM→engine path: dispatching the `d` operator action
3954        // then a `$` motion composes `d$` via the zenmai operator-pending FSM —
3955        // the operator key alone does nothing until the motion arrives.
3956        let mut s = new_state_with("hello world");
3957        s.apply(&Action::Operator(Operator::Delete));
3958        assert_eq!(line0_len(&s), 11, "the operator key alone mutates nothing");
3959        s.apply(&Action::Move(Motion::LineEnd));
3960        assert_eq!(
3961            line0_len(&s),
3962            0,
3963            "d then $ composes d$ and deletes the line"
3964        );
3965        assert_eq!(s.register(), Some("hello world"));
3966    }
3967
3968    #[test]
3969    fn change_operator_through_fsm_enters_insert() {
3970        let mut s = new_state_with("hello world");
3971        s.apply(&Action::Operator(Operator::Change));
3972        s.apply(&Action::Move(Motion::LineEnd));
3973        assert_eq!(s.modal.mode(), Mode::Insert, "c$ deletes and enters Insert");
3974    }
3975
3976    #[test]
3977    fn lone_motion_after_no_operator_just_moves() {
3978        // Without a preceding operator the motion passes through unchanged.
3979        let mut s = new_state_with("hello world");
3980        s.apply(&Action::Move(Motion::LineEnd));
3981        assert_eq!(s.cursor(), Position::new(0, 11));
3982        assert_eq!(line0_len(&s), 11, "a bare motion never mutates");
3983    }
3984
3985    #[test]
3986    fn counted_operator_deletes_count_times() {
3987        // `3d` + a right-motion = `3dl` = delete 3 chars. The operator's count
3988        // flows through the FSM to the composed motion (the bug fix: previously
3989        // the count repeated the operator key and toggled the FSM).
3990        let mut s = new_state_with("abcdef");
3991        s.apply_counted(&Action::Operator(Operator::Delete), 3);
3992        assert_eq!(line0_len(&s), 6, "the operator key alone mutates nothing");
3993        s.apply(&Action::Move(Motion::Right));
3994        assert_eq!(
3995            s.buffers.get(s.active).unwrap().line(0).as_deref(),
3996            Some("def")
3997        );
3998    }
3999
4000    #[test]
4001    fn operator_and_motion_counts_multiply_end_to_end() {
4002        // `2d3l` = delete 2×3 = 6 chars.
4003        let mut s = new_state_with("abcdefgh");
4004        s.apply_counted(&Action::Operator(Operator::Delete), 2);
4005        s.apply_counted(&Action::Move(Motion::Right), 3);
4006        assert_eq!(
4007            s.buffers.get(s.active).unwrap().line(0).as_deref(),
4008            Some("gh")
4009        );
4010    }
4011
4012    #[test]
4013    fn bare_counted_motion_still_repeats_no_regression() {
4014        // `3j` still moves down 3 lines — the count passes through the FSM
4015        // unchanged when no operator is pending.
4016        let mut s = new_state_with("a\nb\nc\nd\ne");
4017        s.apply_counted(&Action::Move(Motion::Down), 3);
4018        assert_eq!(s.cursor().line, 3, "5j-style counted motion preserved");
4019    }
4020
4021    /// A monotonic clock for the key-repeat gate in tests — each `next()`
4022    /// jumps a full second past the previous, so every press it stamps is
4023    /// well outside the 80ms debounce window and therefore an INTENTIONAL
4024    /// press (never a storm tick). Used by tests that fire the *same*
4025    /// navigation key twice and assert editor logic, not debounce timing.
4026    struct SpacedClock(std::time::Instant);
4027    impl SpacedClock {
4028        fn new() -> Self {
4029            Self(std::time::Instant::now())
4030        }
4031        fn next(&mut self) -> std::time::Instant {
4032            self.0 += std::time::Duration::from_secs(1);
4033            self.0
4034        }
4035    }
4036
4037    #[test]
4038    fn hjkl_moves_cursor() {
4039        let mut s = new_state_with("hello\nworld");
4040        s.tick(&press(KeyCode::Char('l')));
4041        assert_eq!(s.cursor().column, 1);
4042        s.tick(&press(KeyCode::Char('j')));
4043        assert_eq!(s.cursor().line, 1);
4044        s.tick(&press(KeyCode::Char('h')));
4045        assert_eq!(s.cursor().column, 0);
4046    }
4047
4048    #[test]
4049    fn insert_mode_inserts_chars() {
4050        let mut s = new_state_with("");
4051        s.tick(&press(KeyCode::Char('i')));
4052        assert_eq!(s.modal.mode(), Mode::Insert);
4053        s.tick(&press(KeyCode::Char('h')));
4054        s.tick(&press(KeyCode::Char('i')));
4055        assert_eq!(s.buffers.get(s.active).unwrap().to_string(), "hi");
4056        assert_eq!(s.cursor().column, 2);
4057    }
4058
4059    #[test]
4060    fn esc_returns_to_normal() {
4061        let mut s = new_state_with("");
4062        s.tick(&press(KeyCode::Char('i')));
4063        s.tick(&press(KeyCode::Escape));
4064        assert_eq!(s.modal.mode(), Mode::Normal);
4065    }
4066
4067    #[test]
4068    fn count_prefix_repeats_motion() {
4069        let mut s = new_state_with("abcdefghij");
4070        s.tick(&press(KeyCode::Char('5')));
4071        s.tick(&press(KeyCode::Char('l')));
4072        assert_eq!(s.cursor().column, 5);
4073    }
4074
4075    #[test]
4076    fn close_event_requests_quit() {
4077        let mut s = new_state_with("");
4078        s.tick(&AppEvent::CloseRequested);
4079        assert!(s.quit_requested);
4080    }
4081
4082    #[test]
4083    fn word_next_jumps_past_whitespace() {
4084        let mut s = new_state_with("foo bar baz");
4085        // Two INTENTIONAL `w` presses, spaced past the key-repeat window so
4086        // the gate passes both (a real user's two taps are ≥80ms apart).
4087        let mut clk = SpacedClock::new();
4088        s.tick_at(&press(KeyCode::Char('w')), clk.next());
4089        assert_eq!(s.cursor().column, 4);
4090        s.tick_at(&press(KeyCode::Char('w')), clk.next());
4091        assert_eq!(s.cursor().column, 8);
4092    }
4093
4094    // ── Multi-key / leader pending-stroke ───────────────────────────
4095
4096    #[test]
4097    fn leader_sequence_holds_then_resolves() {
4098        let mut s = new_state_with("a\nbb\nccc");
4099        s.keymap.bind_sequence(
4100            Mode::Normal,
4101            vec![Key::Char(','), Key::Char('g')],
4102            Action::Move(Motion::DocEnd),
4103            "doc end",
4104        );
4105        // `,` begins the sequence — held pending, nothing applied yet.
4106        s.on_key(&Key::Char(','));
4107        assert_eq!(s.pending_keys, vec![Key::Char(',')]);
4108        assert_eq!(s.cursor(), Position::ZERO);
4109        // `g` completes `<leader>g` → DocEnd; pending clears.
4110        s.on_key(&Key::Char('g'));
4111        assert!(s.pending_keys.is_empty());
4112        assert_eq!(s.cursor().line, 2);
4113    }
4114
4115    #[test]
4116    fn two_key_gg_jumps_doc_start() {
4117        let mut s = new_state_with("a\nbb\nccc");
4118        s.keymap.bind_sequence(
4119            Mode::Normal,
4120            vec![Key::Char('g'), Key::Char('g')],
4121            Action::Move(Motion::DocStart),
4122            "doc start",
4123        );
4124        let mut clk = SpacedClock::new();
4125        s.tick_at(&press(KeyCode::Char('j')), clk.next());
4126        s.tick_at(&press(KeyCode::Char('j')), clk.next());
4127        assert_eq!(s.cursor().line, 2);
4128        s.on_key(&Key::Char('g')); // pending
4129        assert_eq!(s.pending_keys, vec![Key::Char('g')]);
4130        s.on_key(&Key::Char('g')); // resolve
4131        assert_eq!(s.cursor(), Position::ZERO);
4132    }
4133
4134    #[test]
4135    fn broken_sequence_aborts_and_clears_pending() {
4136        let mut s = new_state_with("hello");
4137        s.keymap.bind_sequence(
4138            Mode::Normal,
4139            vec![Key::Char('g'), Key::Char('g')],
4140            Action::Move(Motion::DocEnd),
4141            "doc end",
4142        );
4143        s.on_key(&Key::Char('g')); // pending [g]
4144        assert_eq!(s.pending_keys, vec![Key::Char('g')]);
4145        s.on_key(&Key::Char('x')); // breaks gg → abort; x is unbound → no-op
4146        assert!(s.pending_keys.is_empty());
4147        assert_eq!(s.cursor(), Position::ZERO);
4148    }
4149
4150    #[test]
4151    fn single_binding_wins_over_sequence_prefix() {
4152        // A key that is BOTH a complete single binding and the start of
4153        // a sequence fires the single binding immediately (no chord
4154        // timeout needed). Here `h` (move-left) also prefixes `hz`.
4155        let mut s = new_state_with("abcde");
4156        let mut clk = SpacedClock::new();
4157        s.tick_at(&press(KeyCode::Char('l')), clk.next());
4158        s.tick_at(&press(KeyCode::Char('l')), clk.next());
4159        assert_eq!(s.cursor().column, 2);
4160        s.keymap.bind_sequence(
4161            Mode::Normal,
4162            vec![Key::Char('h'), Key::Char('z')],
4163            Action::Move(Motion::DocEnd),
4164            "shadowed",
4165        );
4166        s.on_key(&Key::Char('h'));
4167        assert!(s.pending_keys.is_empty(), "single binding should not pend");
4168        assert_eq!(s.cursor().column, 1, "h moved left immediately");
4169    }
4170
4171    // ── tatara-lisp runtime bridge (imperative programmability) ─────
4172
4173    #[test]
4174    fn lisp_set_option_writes_live_options() {
4175        let mut s = new_state_with("");
4176        s.run_lisp(r#"(set-option "number" "true")"#).unwrap();
4177        assert_eq!(s.options.get("number").map(String::as_str), Some("true"));
4178    }
4179
4180    #[test]
4181    fn lisp_insert_modifies_buffer_and_advances_cursor() {
4182        let mut s = new_state_with("");
4183        s.run_lisp(r#"(insert "abc")"#).unwrap();
4184        assert_eq!(s.buffers.get(s.active).unwrap().to_string(), "abc");
4185        assert_eq!(s.cursor(), Position::new(0, 3));
4186    }
4187
4188    #[test]
4189    fn lisp_message_appends_to_messages() {
4190        let mut s = new_state_with("");
4191        s.run_lisp(r#"(message "hello from lisp")"#).unwrap();
4192        assert_eq!(s.messages, vec!["hello from lisp".to_string()]);
4193    }
4194
4195    #[test]
4196    fn lisp_reads_snapshot_and_branches_to_effect() {
4197        // Genuine programmability: Lisp reads the live cursor line and
4198        // an `if` decides which option to set.
4199        let mut s = new_state_with("one\ntwo\nthree");
4200        // cursor at line 0 → "top" branch
4201        s.run_lisp(r#"(if (= (cursor-line) 0) (set-option "pos" "top") (set-option "pos" "mid"))"#)
4202            .unwrap();
4203        assert_eq!(s.options.get("pos").map(String::as_str), Some("top"));
4204    }
4205
4206    #[test]
4207    fn lisp_run_command_effect_drives_registry() {
4208        // `(run-command "undo")` reaches the live command registry and
4209        // reverts a prior Lisp-driven insert — proving the RunCommand
4210        // effect dispatches through real editor commands.
4211        let mut s = new_state_with("");
4212        s.run_lisp(r#"(insert "abc")"#).unwrap();
4213        assert_eq!(s.buffers.get(s.active).unwrap().to_string(), "abc");
4214        s.run_lisp(r#"(run-command "undo")"#).unwrap();
4215        assert_eq!(s.buffers.get(s.active).unwrap().to_string(), "");
4216    }
4217
4218    #[test]
4219    fn lisp_run_command_quit_sets_quit_requested_via_typed_flag() {
4220        // The full imperative-quit path: (run-command "quit") routes
4221        // through the registry's typed `quit_requested` signal — no string
4222        // sentinel, and no minibuffer pollution (the editor stays in a
4223        // clean Normal state, which has no minibuffer at all).
4224        let mut s = new_state_with("");
4225        s.run_lisp(r#"(run-command "quit")"#).unwrap();
4226        assert!(s.quit_requested, "lisp-driven quit must set quit_requested");
4227        assert_eq!(
4228            s.modal.minibuffer(),
4229            "",
4230            "quit must not pollute any command line — Normal mode has no minibuffer",
4231        );
4232    }
4233
4234    // ── Lazy plugin activation (PluginHost) ────────────────────────
4235
4236    #[test]
4237    fn lazy_plugin_activates_on_command_trigger() {
4238        // A user plugin gated on `Command: LazyGo` has its entry applied
4239        // the first time that command runs — proving the lazy.nvim
4240        // `cmd =` model works end-to-end against live editor state.
4241        let mut s = new_state_with("");
4242        s.register_lazy_plugin(
4243            "user-lazy",
4244            vec![LazyTrigger::Command("LazyGo".into())],
4245            r#"(defoption :name "lazy-loaded" :value "yes")
4246               (defcmd :name "LazyGo" :description "noop" :action "editor.noop")"#,
4247        );
4248        assert_eq!(s.plugin_host.pending(), 1);
4249        assert!(
4250            s.options.get("lazy-loaded").is_none(),
4251            "entry not applied yet"
4252        );
4253
4254        // Drive the command through the public imperative path.
4255        s.run_lisp(r#"(run-command "LazyGo")"#).unwrap();
4256
4257        assert_eq!(
4258            s.options.get("lazy-loaded").map(String::as_str),
4259            Some("yes"),
4260            "the command trigger applied the plugin's entry",
4261        );
4262        assert_eq!(s.plugin_host.pending(), 0, "plugin activated exactly once");
4263    }
4264
4265    #[test]
4266    fn lazy_plugin_activates_on_filetype() {
4267        let mut s = new_state_with("");
4268        s.register_lazy_plugin(
4269            "user-rust",
4270            vec![LazyTrigger::FileType("rust".into())],
4271            r#"(defoption :name "rust-plugin" :value "on")"#,
4272        );
4273        let n = s.activate_filetype_plugins("rust");
4274        assert_eq!(n, 1);
4275        assert_eq!(s.options.get("rust-plugin").map(String::as_str), Some("on"));
4276        // A second open of the same filetype is a no-op (one-shot).
4277        assert_eq!(s.activate_filetype_plugins("rust"), 0);
4278    }
4279
4280    #[test]
4281    fn cached_vm_serves_multiple_run_lisp_calls() {
4282        let mut s = new_state_with("");
4283        s.run_lisp(r#"(message "one")"#).unwrap();
4284        assert!(
4285            s.lisp_vm.is_some(),
4286            "VM should be cached after first run_lisp"
4287        );
4288        s.run_lisp(r#"(message "two")"#).unwrap();
4289        assert_eq!(s.messages, vec!["one".to_string(), "two".to_string()]);
4290    }
4291
4292    #[test]
4293    fn lisp_define_persists_across_run_lisp_calls() {
4294        // The cached VM's top-level env persists across calls (REPL
4295        // semantics): a `define` in one call is visible in the next.
4296        let mut s = new_state_with("");
4297        s.run_lisp(r#"(define greeting "hi")"#).unwrap();
4298        s.run_lisp(r#"(message greeting)"#).unwrap();
4299        assert_eq!(s.messages, vec!["hi".to_string()]);
4300    }
4301
4302    #[test]
4303    fn snapshot_is_isolated_within_one_run_lisp_call_and_refreshes_across() {
4304        // Within ONE call a program cannot observe its own writes — the
4305        // read snapshot is captured before eval, effects apply after. A
4306        // later call sees the refreshed snapshot.
4307        let mut s = new_state_with("");
4308        s.run_lisp(
4309            r#"(insert "ab") (set-option "col" (if (= (cursor-column) 0) "stale-zero" "live"))"#,
4310        )
4311        .unwrap();
4312        assert_eq!(s.buffers.get(s.active).unwrap().to_string(), "ab");
4313        assert_eq!(
4314            s.options.get("col").map(String::as_str),
4315            Some("stale-zero"),
4316            "cursor-column within the same call reads the pre-eval snapshot",
4317        );
4318        // After the first call the cursor advanced to column 2; the next
4319        // call's snapshot reflects it.
4320        s.run_lisp(r#"(set-option "col2" (if (= (cursor-column) 2) "live-two" "other"))"#)
4321            .unwrap();
4322        assert_eq!(
4323            s.options.get("col2").map(String::as_str),
4324            Some("live-two"),
4325            "a later call sees the refreshed snapshot",
4326        );
4327    }
4328
4329    #[test]
4330    fn insert_text_effect_multiline_lands_cursor_on_last_line() {
4331        let mut s = new_state_with("");
4332        s.apply_host_effects(vec![Negai::InsertText("foo\nbar".to_string())]);
4333        assert_eq!(s.buffers.get(s.active).unwrap().to_string(), "foo\nbar");
4334        assert_eq!(s.cursor(), Position::new(1, 3));
4335    }
4336
4337    #[test]
4338    fn visual_mode_sequence_resolves() {
4339        let mut s = new_state_with("abc");
4340        s.modal.enter(Mode::Visual);
4341        s.keymap.bind_sequence(
4342            Mode::Visual,
4343            vec![Key::Char('g'), Key::Char('e')],
4344            Action::Move(Motion::DocEnd),
4345            "ge",
4346        );
4347        s.on_key(&Key::Char('g'));
4348        assert_eq!(s.pending_keys, vec![Key::Char('g')]);
4349        s.on_key(&Key::Char('e'));
4350        assert!(s.pending_keys.is_empty());
4351        assert_eq!(
4352            s.cursor().column,
4353            3,
4354            "ge resolved to doc-end in visual mode"
4355        );
4356    }
4357
4358    #[test]
4359    fn sequence_abort_with_bound_breaking_key_redispatches() {
4360        // gg is a sequence; `l` (move-right) is a bound single key. After
4361        // `g` pends, `l` breaks gg, aborts, and is re-dispatched fresh.
4362        let mut s = new_state_with("abcde");
4363        s.keymap.bind_sequence(
4364            Mode::Normal,
4365            vec![Key::Char('g'), Key::Char('g')],
4366            Action::Move(Motion::DocEnd),
4367            "gg",
4368        );
4369        s.on_key(&Key::Char('g'));
4370        assert_eq!(s.pending_keys, vec![Key::Char('g')]);
4371        s.on_key(&Key::Char('l'));
4372        assert!(s.pending_keys.is_empty());
4373        assert_eq!(
4374            s.cursor().column,
4375            1,
4376            "the breaking key l should re-dispatch as move-right",
4377        );
4378    }
4379
4380    // ── Viewport-follows-cursor invariant (both axes) ───────────────
4381
4382    #[test]
4383    fn viewport_contains_cursor_after_every_op() {
4384        // Tiny window: 5 visible lines × 10 visible columns. Drive a
4385        // representative scripted sequence and assert the viewport contains
4386        // the cursor after EVERY mutating step.
4387        let mut s = new_state_small_viewport("", 5, 10);
4388        assert_cursor_in_viewport(&s, "initial");
4389
4390        // Enter insert mode and type 30 newline-separated lines — this is
4391        // the exact "type past the bottom" complaint.
4392        s.tick(&press(KeyCode::Char('i')));
4393        assert_eq!(s.modal.mode(), Mode::Insert);
4394        for line in 0..30u32 {
4395            for c in "line".chars() {
4396                s.tick(&press(KeyCode::Char(c)));
4397                assert_cursor_in_viewport(&s, "typing chars");
4398            }
4399            s.tick(&press(KeyCode::Enter));
4400            assert_cursor_in_viewport(&s, &format!("newline after line {line}"));
4401        }
4402
4403        // Type a long (200-char) line — the "type past the right edge"
4404        // complaint. The cursor must stay horizontally visible the whole way.
4405        for i in 0..200u32 {
4406            s.tick(&press(KeyCode::Char('x')));
4407            assert_cursor_in_viewport(&s, &format!("long-line char {i}"));
4408        }
4409
4410        // Multi-line insert_text effect (the `(insert …)` Lisp path).
4411        s.insert_text("alpha\nbeta\ngamma delta epsilon zeta");
4412        assert_cursor_in_viewport(&s, "insert_text multiline");
4413
4414        // Back to normal mode and move in all directions / to extremes.
4415        s.tick(&press(KeyCode::Escape));
4416        assert_eq!(s.modal.mode(), Mode::Normal);
4417        for m in [
4418            Motion::DocStart,
4419            Motion::DocEnd,
4420            Motion::Down,
4421            Motion::Down,
4422            Motion::Up,
4423            Motion::Right,
4424            Motion::Right,
4425            Motion::Left,
4426            Motion::LineEnd,
4427            Motion::LineStart,
4428            Motion::GotoLine(1),
4429            Motion::GotoLine(40),
4430            Motion::PageDown,
4431            Motion::PageUp,
4432        ] {
4433            s.apply_motion(m);
4434            assert_cursor_in_viewport(&s, &format!("after motion {m:?}"));
4435        }
4436
4437        // Undo many times — the buffer shrinks; the viewport must re-follow
4438        // the (now clamped) cursor.
4439        for i in 0..50u32 {
4440            s.apply(&Action::Undo);
4441            assert_cursor_in_viewport(&s, &format!("undo {i}"));
4442        }
4443        // Redo back up.
4444        for i in 0..50u32 {
4445            s.apply(&Action::Redo);
4446            assert_cursor_in_viewport(&s, &format!("redo {i}"));
4447        }
4448    }
4449
4450    #[test]
4451    fn insert_at_eof_keeps_cursor_in_bounds() {
4452        // Inserting at the end of the buffer must leave the cursor clamped
4453        // to a valid position (and inside the viewport).
4454        let mut s = new_state_small_viewport("abc", 5, 10);
4455        s.apply_motion(Motion::DocEnd);
4456        s.tick(&press(KeyCode::Char('i')));
4457        s.tick(&press(KeyCode::Char('d')));
4458        let buf = s.buffers.get(s.active).unwrap();
4459        let clamped = buf.clamp(s.cursor());
4460        assert_eq!(
4461            s.cursor(),
4462            clamped,
4463            "cursor must be clamped in-bounds at EOF"
4464        );
4465        assert_cursor_in_viewport(&s, "insert at eof");
4466    }
4467
4468    #[test]
4469    fn count_prefix_then_sequence_repeats() {
4470        // `2` then `gj` (→ move-down) repeats the resolved action twice.
4471        let mut s = new_state_with("a\nb\nc\nd\ne");
4472        s.keymap.bind_sequence(
4473            Mode::Normal,
4474            vec![Key::Char('g'), Key::Char('j')],
4475            Action::Move(Motion::Down),
4476            "gj",
4477        );
4478        s.on_key(&Key::Char('2'));
4479        s.on_key(&Key::Char('g'));
4480        s.on_key(&Key::Char('j'));
4481        assert_eq!(s.cursor().line, 2, "count 2 should repeat the gj motion");
4482    }
4483
4484    // ── Key-repeat gate (awase::KeyRepeatGate) ──────────────────────────
4485
4486    #[test]
4487    fn held_key_repeat_storm_is_debounced_in_normal_mode() {
4488        // The audit's exact complaint: holding `j` floods motion events
4489        // and thrashes the viewport. Simulate an OS key-repeat storm — 20
4490        // identical `j` KeyDowns at 50ms intervals (typical repeat cadence)
4491        // — and assert only the gated subset (one per 80ms window) actually
4492        // moves the cursor.
4493        let mut s = new_state_with(&"x\n".repeat(40));
4494        let t0 = std::time::Instant::now();
4495        let mut delivered = 0u32;
4496        for i in 0..20u32 {
4497            let before = s.cursor().line;
4498            s.tick_at(
4499                &press(KeyCode::Char('j')),
4500                t0 + std::time::Duration::from_millis(u64::from(i) * 50),
4501            );
4502            if s.cursor().line != before {
4503                delivered += 1;
4504            }
4505        }
4506        // 20 events over ~1s at 50ms spacing, 80ms gate ⇒ ~13 pass — far
4507        // fewer than the 20 the ungated path would have applied.
4508        assert!(
4509            (10..=14).contains(&delivered),
4510            "expected the storm debounced to ~13 moves, got {delivered}",
4511        );
4512        assert!(
4513            delivered < 20,
4514            "the gate must drop SOME storm ticks, not pass all 20",
4515        );
4516    }
4517
4518    #[test]
4519    fn spaced_intentional_taps_all_pass() {
4520        // Intentional taps spaced past the debounce window must ALL reach
4521        // the editor — the gate filters storms, never deliberate input.
4522        let mut s = new_state_with(&"x\n".repeat(10));
4523        let t0 = std::time::Instant::now();
4524        for i in 0..5u32 {
4525            s.tick_at(
4526                &press(KeyCode::Char('j')),
4527                // 100ms apart — comfortably past the 80ms window.
4528                t0 + std::time::Duration::from_millis(u64::from(i) * 100),
4529            );
4530        }
4531        assert_eq!(s.cursor().line, 5, "all 5 spaced `j` taps moved the cursor");
4532    }
4533
4534    #[test]
4535    fn distinct_keys_have_independent_clocks() {
4536        // Holding `j` must not block a simultaneous `l` — the gate keys on
4537        // the Key, so independent keys have independent windows.
4538        let mut s = new_state_with("abc\ndef\nghi");
4539        let t = std::time::Instant::now();
4540        s.tick_at(&press(KeyCode::Char('j')), t);
4541        // `j` again within the window is dropped…
4542        s.tick_at(
4543            &press(KeyCode::Char('j')),
4544            t + std::time::Duration::from_millis(10),
4545        );
4546        assert_eq!(s.cursor().line, 1, "second `j` within window dropped");
4547        // …but `l` at the same instant passes (its own clock).
4548        s.tick_at(
4549            &press(KeyCode::Char('l')),
4550            t + std::time::Duration::from_millis(10),
4551        );
4552        assert_eq!(s.cursor().column, 1, "`l` is not blocked by `j`'s clock");
4553    }
4554
4555    // ── Cursors newtype is the single cursor home ──────────────────────
4556
4557    #[test]
4558    fn cursor_home_preserves_single_cursor_behavior() {
4559        // The typed `Cursors` wrapper behaves exactly like the old bare
4560        // `Position` field for single-cursor editing: the read accessor
4561        // tracks every mutation routed through `set_cursor`, and there is
4562        // exactly one caret.
4563        let mut s = new_state_with("hello\nworld\nthere");
4564        assert_eq!(s.cursor(), Position::ZERO);
4565        assert_eq!(s.cursors.count(), 1, "phase-1 holds exactly one caret");
4566
4567        s.apply_motion(Motion::Down);
4568        s.apply_motion(Motion::Right);
4569        s.apply_motion(Motion::Right);
4570        assert_eq!(s.cursor(), Position::new(1, 2));
4571        // Still a single caret after a sequence of motions.
4572        assert_eq!(s.cursors.count(), 1);
4573
4574        // The accessor is the SAME value the viewport-follow path read.
4575        let w = s.layout.active_window().unwrap();
4576        assert!(w.viewport.top_line <= s.cursor().line);
4577    }
4578
4579    #[test]
4580    fn insert_mode_is_ungated_so_repeat_typing_works() {
4581        // Holding a key to repeat-type a character is intended in Insert
4582        // mode — the gate must NOT suppress it. 10 rapid identical `x`
4583        // keystrokes at the same instant must all land as text.
4584        let mut s = new_state_with("");
4585        s.tick(&press(KeyCode::Char('i')));
4586        assert_eq!(s.modal.mode(), Mode::Insert);
4587        let t = std::time::Instant::now();
4588        for _ in 0..10 {
4589            s.tick_at(&press(KeyCode::Char('x')), t);
4590        }
4591        assert_eq!(
4592            s.buffers.get(s.active).unwrap().to_string(),
4593            "xxxxxxxxxx",
4594            "insert-mode repeat typing is ungated",
4595        );
4596    }
4597}