hjkl_engine/editor.rs
1//! Editor — the public sqeel-vim type, layered over `hjkl_buffer::Buffer`.
2//!
3//! This file owns the public Editor API — construction, content access,
4//! mouse and goto helpers, the (buffer-level) undo stack, and insert-mode
5//! session bookkeeping. All vim-specific keyboard handling lives in
6//! [`vim`] and communicates with Editor through a small internal API
7//! exposed via `pub(super)` fields and helper methods.
8
9use crate::input::Input;
10use crate::vim::{self, VimState};
11use crate::{KeybindingMode, VimMode};
12use std::sync::atomic::{AtomicU16, Ordering};
13
14/// Map a [`hjkl_buffer::Edit`] to one or more SPEC
15/// [`crate::types::Edit`] (`EditOp`) records.
16///
17/// Most buffer edits map to a single EditOp. Block ops
18/// ([`hjkl_buffer::Edit::InsertBlock`] /
19/// [`hjkl_buffer::Edit::DeleteBlockChunks`]) emit one EditOp per row
20/// touched — they edit non-contiguous cells and a single
21/// `range..range` can't represent the rectangle.
22///
23/// Returns an empty vec when the edit isn't representable (no buffer
24/// variant currently fails this check).
25fn edit_to_editops(edit: &hjkl_buffer::Edit) -> Vec<crate::types::Edit> {
26 use crate::types::{Edit as Op, Pos};
27 use hjkl_buffer::Edit as B;
28 let to_pos = |p: hjkl_buffer::Position| Pos {
29 line: p.row as u32,
30 col: p.col as u32,
31 };
32 match edit {
33 B::InsertChar { at, ch } => vec![Op {
34 range: to_pos(*at)..to_pos(*at),
35 replacement: ch.to_string(),
36 }],
37 B::InsertStr { at, text } => vec![Op {
38 range: to_pos(*at)..to_pos(*at),
39 replacement: text.clone(),
40 }],
41 B::DeleteRange { start, end, .. } => vec![Op {
42 range: to_pos(*start)..to_pos(*end),
43 replacement: String::new(),
44 }],
45 B::Replace { start, end, with } => vec![Op {
46 range: to_pos(*start)..to_pos(*end),
47 replacement: with.clone(),
48 }],
49 B::JoinLines {
50 row,
51 count,
52 with_space,
53 } => {
54 // Joining `count` rows after `row` collapses
55 // [(row+1, 0) .. (row+count, EOL)] into the joined
56 // sentinel. The replacement is either an empty string
57 // (gJ) or " " between segments (J).
58 let start = Pos {
59 line: *row as u32 + 1,
60 col: 0,
61 };
62 let end = Pos {
63 line: (*row + *count) as u32,
64 col: u32::MAX, // covers to EOL of the last source row
65 };
66 vec![Op {
67 range: start..end,
68 replacement: if *with_space {
69 " ".into()
70 } else {
71 String::new()
72 },
73 }]
74 }
75 B::SplitLines {
76 row,
77 cols,
78 inserted_space: _,
79 } => {
80 // SplitLines reverses a JoinLines: insert a `\n`
81 // (and optional dropped space) at each col on `row`.
82 cols.iter()
83 .map(|c| {
84 let p = Pos {
85 line: *row as u32,
86 col: *c as u32,
87 };
88 Op {
89 range: p..p,
90 replacement: "\n".into(),
91 }
92 })
93 .collect()
94 }
95 B::InsertBlock { at, chunks } => {
96 // One EditOp per row in the block — non-contiguous edits.
97 chunks
98 .iter()
99 .enumerate()
100 .map(|(i, chunk)| {
101 let p = Pos {
102 line: at.row as u32 + i as u32,
103 col: at.col as u32,
104 };
105 Op {
106 range: p..p,
107 replacement: chunk.clone(),
108 }
109 })
110 .collect()
111 }
112 B::DeleteBlockChunks { at, widths } => {
113 // One EditOp per row, deleting `widths[i]` chars at
114 // `(at.row + i, at.col)`.
115 widths
116 .iter()
117 .enumerate()
118 .map(|(i, w)| {
119 let start = Pos {
120 line: at.row as u32 + i as u32,
121 col: at.col as u32,
122 };
123 let end = Pos {
124 line: at.row as u32 + i as u32,
125 col: at.col as u32 + *w as u32,
126 };
127 Op {
128 range: start..end,
129 replacement: String::new(),
130 }
131 })
132 .collect()
133 }
134 }
135}
136
137/// Sum of bytes from the start of the buffer to the start of `row`.
138/// Walks lines + their separating `\n` bytes — matches the canonical
139/// `lines().join("\n")` byte rendering used by syntax tooling.
140#[inline]
141fn buffer_byte_of_row(buf: &hjkl_buffer::Buffer, row: usize) -> usize {
142 let n = buf.row_count();
143 let row = row.min(n);
144 let mut acc = 0usize;
145 for r in 0..row {
146 acc += buf.line(r).map(|s| s.len()).unwrap_or(0);
147 if r + 1 < n {
148 acc += 1; // separator '\n'
149 }
150 }
151 acc
152}
153
154/// Convert an `hjkl_buffer::Position` (char-indexed col) into byte
155/// coordinates `(byte_within_buffer, (row, col_byte))` against the
156/// **pre-edit** buffer.
157fn position_to_byte_coords(
158 buf: &hjkl_buffer::Buffer,
159 pos: hjkl_buffer::Position,
160) -> (usize, (u32, u32)) {
161 let row = pos.row.min(buf.row_count().saturating_sub(1));
162 let line = buf.line(row).unwrap_or_default();
163 let col_byte = pos.byte_offset(&line);
164 let byte = buffer_byte_of_row(buf, row) + col_byte;
165 (byte, (row as u32, col_byte as u32))
166}
167
168/// Compute the byte position after inserting `text` starting at
169/// `start_byte` / `start_pos`. Returns `(end_byte, end_position)`.
170fn advance_by_text(text: &str, start_byte: usize, start_pos: (u32, u32)) -> (usize, (u32, u32)) {
171 let new_end_byte = start_byte + text.len();
172 let newlines = text.bytes().filter(|&b| b == b'\n').count();
173 let end_pos = if newlines == 0 {
174 (start_pos.0, start_pos.1 + text.len() as u32)
175 } else {
176 // Bytes after the last newline determine the trailing column.
177 let last_nl = text.rfind('\n').unwrap();
178 let tail_bytes = (text.len() - last_nl - 1) as u32;
179 (start_pos.0 + newlines as u32, tail_bytes)
180 };
181 (new_end_byte, end_pos)
182}
183
184/// Translate a single `hjkl_buffer::Edit` into one or more
185/// [`crate::types::ContentEdit`] records using the **pre-edit** buffer
186/// state for byte/position lookups. Block ops fan out to one entry per
187/// touched row (matches `edit_to_editops`).
188fn content_edits_from_buffer_edit(
189 buf: &hjkl_buffer::Buffer,
190 edit: &hjkl_buffer::Edit,
191) -> Vec<crate::types::ContentEdit> {
192 use hjkl_buffer::Edit as B;
193 use hjkl_buffer::Position;
194
195 let mut out: Vec<crate::types::ContentEdit> = Vec::new();
196
197 match edit {
198 B::InsertChar { at, ch } => {
199 let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
200 let new_end_byte = start_byte + ch.len_utf8();
201 let new_end_pos = (start_pos.0, start_pos.1 + ch.len_utf8() as u32);
202 out.push(crate::types::ContentEdit {
203 start_byte,
204 old_end_byte: start_byte,
205 new_end_byte,
206 start_position: start_pos,
207 old_end_position: start_pos,
208 new_end_position: new_end_pos,
209 });
210 }
211 B::InsertStr { at, text } => {
212 let (start_byte, start_pos) = position_to_byte_coords(buf, *at);
213 let (new_end_byte, new_end_pos) = advance_by_text(text, start_byte, start_pos);
214 out.push(crate::types::ContentEdit {
215 start_byte,
216 old_end_byte: start_byte,
217 new_end_byte,
218 start_position: start_pos,
219 old_end_position: start_pos,
220 new_end_position: new_end_pos,
221 });
222 }
223 B::DeleteRange { start, end, kind } => {
224 let (start, end) = if start <= end {
225 (*start, *end)
226 } else {
227 (*end, *start)
228 };
229 match kind {
230 hjkl_buffer::MotionKind::Char => {
231 let (start_byte, start_pos) = position_to_byte_coords(buf, start);
232 let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
233 out.push(crate::types::ContentEdit {
234 start_byte,
235 old_end_byte,
236 new_end_byte: start_byte,
237 start_position: start_pos,
238 old_end_position: old_end_pos,
239 new_end_position: start_pos,
240 });
241 }
242 hjkl_buffer::MotionKind::Line => {
243 // Linewise delete drops rows [start.row..=end.row]. Map
244 // to a span from start of `start.row` through start of
245 // (end.row + 1). The buffer's own `do_delete_range`
246 // collapses to row `start.row` after dropping.
247 let lo = start.row;
248 let hi = end.row.min(buf.row_count().saturating_sub(1));
249 let start_byte = buffer_byte_of_row(buf, lo);
250 let next_row_byte = if hi + 1 < buf.row_count() {
251 buffer_byte_of_row(buf, hi + 1)
252 } else {
253 // No row after; clamp to end-of-buffer byte.
254 buffer_byte_of_row(buf, buf.row_count())
255 + buf
256 .line(buf.row_count().saturating_sub(1))
257 .map(|s| s.len())
258 .unwrap_or(0)
259 };
260 out.push(crate::types::ContentEdit {
261 start_byte,
262 old_end_byte: next_row_byte,
263 new_end_byte: start_byte,
264 start_position: (lo as u32, 0),
265 old_end_position: ((hi + 1) as u32, 0),
266 new_end_position: (lo as u32, 0),
267 });
268 }
269 hjkl_buffer::MotionKind::Block => {
270 // Block delete removes a rectangle of chars per row.
271 // Fan out to one ContentEdit per row.
272 let (left_col, right_col) = (start.col.min(end.col), start.col.max(end.col));
273 for row in start.row..=end.row {
274 let row_start_pos = Position::new(row, left_col);
275 let row_end_pos = Position::new(row, right_col + 1);
276 let (sb, sp) = position_to_byte_coords(buf, row_start_pos);
277 let (eb, ep) = position_to_byte_coords(buf, row_end_pos);
278 if eb <= sb {
279 continue;
280 }
281 out.push(crate::types::ContentEdit {
282 start_byte: sb,
283 old_end_byte: eb,
284 new_end_byte: sb,
285 start_position: sp,
286 old_end_position: ep,
287 new_end_position: sp,
288 });
289 }
290 }
291 }
292 }
293 B::Replace { start, end, with } => {
294 let (start, end) = if start <= end {
295 (*start, *end)
296 } else {
297 (*end, *start)
298 };
299 let (start_byte, start_pos) = position_to_byte_coords(buf, start);
300 let (old_end_byte, old_end_pos) = position_to_byte_coords(buf, end);
301 let (new_end_byte, new_end_pos) = advance_by_text(with, start_byte, start_pos);
302 out.push(crate::types::ContentEdit {
303 start_byte,
304 old_end_byte,
305 new_end_byte,
306 start_position: start_pos,
307 old_end_position: old_end_pos,
308 new_end_position: new_end_pos,
309 });
310 }
311 B::JoinLines {
312 row,
313 count,
314 with_space,
315 } => {
316 // Joining `count` rows after `row` collapses the bytes
317 // between EOL of `row` and EOL of `row + count` into either
318 // an empty string (gJ) or a single space per join (J — but
319 // only when both sides are non-empty; we approximate with
320 // a single space for simplicity).
321 let row = (*row).min(buf.row_count().saturating_sub(1));
322 let last_join_row = (row + count).min(buf.row_count().saturating_sub(1));
323 let line = buf.line(row).unwrap_or_default();
324 let row_eol_byte = buffer_byte_of_row(buf, row) + line.len();
325 let row_eol_col = line.len() as u32;
326 let next_row_after = last_join_row + 1;
327 let old_end_byte = if next_row_after < buf.row_count() {
328 buffer_byte_of_row(buf, next_row_after).saturating_sub(1)
329 } else {
330 buffer_byte_of_row(buf, buf.row_count())
331 + buf
332 .line(buf.row_count().saturating_sub(1))
333 .map(|s| s.len())
334 .unwrap_or(0)
335 };
336 let last_line = buf.line(last_join_row).unwrap_or_default();
337 let old_end_pos = (last_join_row as u32, last_line.len() as u32);
338 let replacement_len = if *with_space { 1 } else { 0 };
339 let new_end_byte = row_eol_byte + replacement_len;
340 let new_end_pos = (row as u32, row_eol_col + replacement_len as u32);
341 out.push(crate::types::ContentEdit {
342 start_byte: row_eol_byte,
343 old_end_byte,
344 new_end_byte,
345 start_position: (row as u32, row_eol_col),
346 old_end_position: old_end_pos,
347 new_end_position: new_end_pos,
348 });
349 }
350 B::SplitLines {
351 row,
352 cols,
353 inserted_space,
354 } => {
355 // Splits insert "\n" (or "\n " inverse) at each col on `row`.
356 // The buffer applies all splits left-to-right via the
357 // do_split_lines path; we emit one ContentEdit per col,
358 // each treated as an insert at that col on `row`. Note: the
359 // buffer state during emission is *pre-edit*, so all cols
360 // index into the same pre-edit row.
361 let row = (*row).min(buf.row_count().saturating_sub(1));
362 let line = buf.line(row).unwrap_or_default();
363 let row_byte = buffer_byte_of_row(buf, row);
364 let insert = if *inserted_space { "\n " } else { "\n" };
365 for &c in cols {
366 let pos = Position::new(row, c);
367 let col_byte = pos.byte_offset(&line);
368 let start_byte = row_byte + col_byte;
369 let start_pos = (row as u32, col_byte as u32);
370 let (new_end_byte, new_end_pos) = advance_by_text(insert, start_byte, start_pos);
371 out.push(crate::types::ContentEdit {
372 start_byte,
373 old_end_byte: start_byte,
374 new_end_byte,
375 start_position: start_pos,
376 old_end_position: start_pos,
377 new_end_position: new_end_pos,
378 });
379 }
380 }
381 B::InsertBlock { at, chunks } => {
382 // One ContentEdit per chunk; each lands at `(at.row + i,
383 // at.col)` in the pre-edit buffer.
384 for (i, chunk) in chunks.iter().enumerate() {
385 let pos = Position::new(at.row + i, at.col);
386 let (start_byte, start_pos) = position_to_byte_coords(buf, pos);
387 let (new_end_byte, new_end_pos) = advance_by_text(chunk, start_byte, start_pos);
388 out.push(crate::types::ContentEdit {
389 start_byte,
390 old_end_byte: start_byte,
391 new_end_byte,
392 start_position: start_pos,
393 old_end_position: start_pos,
394 new_end_position: new_end_pos,
395 });
396 }
397 }
398 B::DeleteBlockChunks { at, widths } => {
399 for (i, w) in widths.iter().enumerate() {
400 let row = at.row + i;
401 let start_pos = Position::new(row, at.col);
402 let end_pos = Position::new(row, at.col + *w);
403 let (sb, sp) = position_to_byte_coords(buf, start_pos);
404 let (eb, ep) = position_to_byte_coords(buf, end_pos);
405 if eb <= sb {
406 continue;
407 }
408 out.push(crate::types::ContentEdit {
409 start_byte: sb,
410 old_end_byte: eb,
411 new_end_byte: sb,
412 start_position: sp,
413 old_end_position: ep,
414 new_end_position: sp,
415 });
416 }
417 }
418 }
419
420 out
421}
422
423/// Where the cursor should land in the viewport after a `z`-family
424/// scroll (`zz` / `zt` / `zb`).
425#[derive(Debug, Clone, Copy, PartialEq, Eq)]
426pub(super) enum CursorScrollTarget {
427 Center,
428 Top,
429 Bottom,
430}
431
432// ── Trait-surface cast helpers ────────────────────────────────────
433//
434// 0.0.42 (Patch C-δ.7): the helpers introduced in 0.0.41 were
435// promoted to [`crate::buf_helpers`] so `vim.rs` free fns can route
436// their reaches through the same primitives. Re-import via
437// `use` so the editor body keeps its terse call shape.
438
439use crate::buf_helpers::{
440 apply_buffer_edit, buf_cursor_pos, buf_cursor_rc, buf_cursor_row, buf_line, buf_line_chars,
441 buf_lines_to_vec, buf_row_count, buf_set_cursor_rc,
442};
443
444/// Return value from the engine's `try_goto_mark_*` methods. Tells the
445/// caller (app layer) whether a cross-buffer switch is required.
446///
447/// - `SameBuffer` — cursor moved (or mark was unset → no-op) within the
448/// same buffer; no buffer switch needed.
449/// - `CrossBuffer` — the mark lives in a different buffer. The app must
450/// switch to the slot whose `buffer_id` matches, then position the cursor
451/// at `(row, col)` using `Editor::jump_cursor`.
452/// - `Unset` — mark not set; no action needed.
453#[derive(Debug, Clone, PartialEq, Eq)]
454pub enum MarkJump {
455 SameBuffer,
456 CrossBuffer {
457 buffer_id: u64,
458 row: usize,
459 col: usize,
460 },
461 Unset,
462}
463
464pub struct Editor<
465 B: crate::types::Buffer = hjkl_buffer::Buffer,
466 H: crate::types::Host = crate::types::DefaultHost,
467> {
468 pub keybinding_mode: KeybindingMode,
469 /// Set when the user yanks/cuts; caller drains this to write to OS clipboard.
470 pub last_yank: Option<String>,
471 /// All vim-specific state (mode, pending operator, count, dot-repeat, ...).
472 /// Internal — exposed via Editor accessor methods
473 /// ([`Editor::buffer_mark`], [`Editor::last_jump_back`],
474 /// [`Editor::last_edit_pos`], [`Editor::take_lsp_intent`], …).
475 pub(crate) vim: VimState,
476 /// Undo history: each entry is (lines, cursor) before the edit.
477 /// Internal — managed by [`Editor::push_undo`] / [`Editor::restore`]
478 /// / [`Editor::pop_last_undo`].
479 pub(crate) undo_stack: Vec<(Vec<String>, (usize, usize))>,
480 /// Redo history: entries pushed when undoing.
481 pub(super) redo_stack: Vec<(Vec<String>, (usize, usize))>,
482 /// Set whenever the buffer content changes; cleared by `take_dirty`.
483 pub(super) content_dirty: bool,
484 /// Cached snapshot of `lines().join("\n") + "\n"` wrapped in an Arc
485 /// so repeated `content_arc()` calls within the same un-mutated
486 /// window are free (ref-count bump instead of a full-buffer join).
487 /// Invalidated by every [`mark_content_dirty`] call.
488 pub(super) cached_content: Option<std::sync::Arc<String>>,
489 /// Last rendered viewport height (text rows only, no chrome). Written
490 /// by the draw path via [`set_viewport_height`] so the scroll helpers
491 /// can clamp the cursor to stay visible without plumbing the height
492 /// through every call.
493 pub(super) viewport_height: AtomicU16,
494 /// Pending LSP intent set by a normal-mode chord (e.g. `gd` for
495 /// goto-definition). The host app drains this each step and fires
496 /// the matching request against its own LSP client.
497 pub(super) pending_lsp: Option<LspIntent>,
498 /// Pending [`crate::types::FoldOp`]s raised by `z…` keystrokes,
499 /// the `:fold*` Ex commands, or the edit pipeline's
500 /// "edits-inside-a-fold open it" invalidation. Drained by hosts
501 /// via [`Editor::take_fold_ops`]; the engine also applies each op
502 /// locally through [`crate::buffer_impl::BufferFoldProviderMut`]
503 /// so the in-tree buffer fold storage stays in sync without host
504 /// cooperation. Introduced in 0.0.38 (Patch C-δ.4).
505 pub(super) pending_fold_ops: Vec<crate::types::FoldOp>,
506 /// Buffer storage.
507 ///
508 /// 0.1.0 (Patch C-δ): generic over `B: Buffer` per SPEC §"Editor
509 /// surface". Default `B = hjkl_buffer::Buffer`. The vim FSM body
510 /// and `Editor::mutate_edit` are concrete on `hjkl_buffer::Buffer`
511 /// for 0.1.0 — see `crate::buf_helpers::apply_buffer_edit`.
512 pub(super) buffer: B,
513 /// Engine-native style intern table. Opaque `Span::style` ids index
514 /// into this table; the render path resolves ids back to
515 /// [`crate::types::Style`]. Ratatui hosts convert at the boundary via
516 /// `hjkl_engine_tui::style_to_ratatui`. Always present — no cfg-mutex.
517 pub(super) style_table: Vec<crate::types::Style>,
518 /// Vim-style register bank — `"`, `"0`–`"9`, `"a`–`"z`. Sources
519 /// every `p` / `P` via the active selector (default unnamed).
520 /// Internal — read via [`Editor::registers`]; mutated by yank /
521 /// delete / paste FSM paths and by [`Editor::seed_yank`].
522 pub(crate) registers: crate::registers::Registers,
523 /// Per-row syntax styling in engine-native form. Always present —
524 /// populated by [`Editor::install_syntax_spans`]. Ratatui hosts use
525 /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`.
526 pub styled_spans: Vec<Vec<(usize, usize, crate::types::Style)>>,
527 /// Per-editor settings tweakable via `:set`. Exposed by reference
528 /// so handlers (indent, search) read the live value rather than a
529 /// snapshot taken at startup. Read via [`Editor::settings`];
530 /// mutate via [`Editor::settings_mut`].
531 pub(crate) settings: Settings,
532 /// Unified named-marks map. Lowercase letters (`'a`–`'z`) are
533 /// per-Editor / "buffer-scope-equivalent" — set by `m{a-z}`, read
534 /// by `'{a-z}` / `` `{a-z} ``. Uppercase letters (`'A`–`'Z`) are
535 /// "file marks" that survive [`Editor::set_content`] calls so
536 /// they persist across tab swaps within the same Editor.
537 ///
538 /// 0.0.36: consolidated from three former storages:
539 /// - `hjkl_buffer::Buffer::marks` (deleted; was unused dead code).
540 /// - `vim::VimState::marks` (lowercase) (deleted).
541 /// - `Editor::file_marks` (uppercase) (replaced by this map).
542 ///
543 /// `BTreeMap` so iteration is deterministic for snapshot tests
544 /// and the `:marks` ex command. Mark-shift on edits is handled
545 /// by [`Editor::shift_marks_after_edit`].
546 pub(crate) marks: std::collections::BTreeMap<char, (usize, usize)>,
547 /// Global (uppercase) marks that carry a `buffer_id` so they can jump
548 /// across buffers. Keyed by `'A'`–`'Z'`; values are
549 /// `(buffer_id, row, col)`. Set by `m{A-Z}`, resolved by
550 /// `try_goto_mark_line` / `try_goto_mark_char`.
551 pub(crate) global_marks: std::collections::BTreeMap<char, (u64, usize, usize)>,
552 /// The `buffer_id` this editor instance is currently attached to.
553 /// Updated by the host app on every `switch_to` / slot creation so
554 /// global-mark writes record the correct id without requiring the app
555 /// to pass the id on every keystroke.
556 pub(crate) current_buffer_id: u64,
557 /// Block ranges (`(start_row, end_row)` inclusive) the host has
558 /// extracted from a syntax tree. `:foldsyntax` reads these to
559 /// populate folds. The host refreshes them on every re-parse via
560 /// [`Editor::set_syntax_fold_ranges`]; ex commands read them via
561 /// [`Editor::syntax_fold_ranges`].
562 pub(crate) syntax_fold_ranges: Vec<(usize, usize)>,
563 /// Pending edit log drained by [`Editor::take_changes`]. Each entry
564 /// is a SPEC [`crate::types::Edit`] mapped from the underlying
565 /// `hjkl_buffer::Edit` operation. Compound ops (JoinLines,
566 /// SplitLines, InsertBlock, DeleteBlockChunks) emit a single
567 /// best-effort EditOp covering the touched range; hosts wanting
568 /// per-cell deltas should diff their own snapshot of `lines()`.
569 /// Sealed at 0.1.0 trait extraction.
570 /// Drained by [`Editor::take_changes`].
571 pub(crate) change_log: Vec<crate::types::Edit>,
572 /// Vim's "sticky column" (curswant). `None` before the first
573 /// motion — the next vertical motion bootstraps from the live
574 /// cursor column. Horizontal motions refresh this to the new
575 /// column; vertical motions read it back so bouncing through a
576 /// shorter row doesn't drag the cursor to col 0. Hoisted out of
577 /// `hjkl_buffer::Buffer` (and `VimState`) in 0.0.28 — Editor is
578 /// the single owner now. Buffer motion methods that need it
579 /// take a `&mut Option<usize>` parameter.
580 pub(crate) sticky_col: Option<usize>,
581 /// Host adapter for clipboard, cursor-shape, time, viewport, and
582 /// search-prompt / cancellation side-channels.
583 ///
584 /// 0.1.0 (Patch C-δ): generic over `H: Host` per SPEC §"Editor
585 /// surface". Default `H = DefaultHost`. The pre-0.1.0 `EngineHost`
586 /// dyn-shim is gone — every method now dispatches through `H`'s
587 /// `Host` trait surface directly.
588 pub(crate) host: H,
589 /// Last public mode the cursor-shape emitter saw. Drives
590 /// [`Editor::emit_cursor_shape_if_changed`] so `Host::emit_cursor_shape`
591 /// fires exactly once per mode transition without sprinkling the
592 /// call across every `vim.mode = ...` site.
593 pub(crate) last_emitted_mode: crate::VimMode,
594 /// Search FSM state (pattern + per-row match cache + wrapscan).
595 /// 0.0.35: relocated out of `hjkl_buffer::Buffer` per
596 /// `DESIGN_33_METHOD_CLASSIFICATION.md` step 1.
597 /// 0.0.37: the buffer-side bridge (`Buffer::search_pattern`) is
598 /// gone; `BufferView` now takes the active regex as a `&Regex`
599 /// parameter, sourced from `Editor::search_state().pattern`.
600 pub(crate) search_state: crate::search::SearchState,
601 /// Per-row syntax span overlay. Source of truth for the host's
602 /// renderer ([`hjkl_buffer::BufferView::spans`]). Populated by
603 /// [`Editor::install_syntax_spans`] (ratatui hosts use
604 /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`)
605 /// and, in due course, by `Host::syntax_highlights` once the engine
606 /// drives that path directly.
607 ///
608 /// 0.0.37: lifted out of `hjkl_buffer::Buffer` per step 3 of
609 /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer-side cache +
610 /// `Buffer::set_spans` / `Buffer::spans` accessors are gone.
611 pub(crate) buffer_spans: Vec<Vec<hjkl_buffer::Span>>,
612 /// Pending `ContentEdit` records emitted by `mutate_edit`. Drained by
613 /// hosts via [`Editor::take_content_edits`] for fan-in to a syntax
614 /// tree (or any other content-change observer that needs byte-level
615 /// position deltas). Edges are byte-indexed and `(row, col_byte)`.
616 pub(crate) pending_content_edits: Vec<crate::types::ContentEdit>,
617 /// Pending "reset" flag set when the entire buffer is replaced
618 /// (e.g. `set_content` / `restore`). Supersedes any queued
619 /// `pending_content_edits` on the same frame: hosts call
620 /// [`Editor::take_content_reset`] before draining edits.
621 pub(crate) pending_content_reset: bool,
622 /// Row range touched by the most recent `auto_indent_rows` call.
623 /// `(top_row, bot_row)` inclusive. Set by the engine after every
624 /// auto-indent operation; drained (and cleared) by the host via
625 /// [`Editor::take_last_indent_range`] so it can display a brief
626 /// visual flash over the reindented rows.
627 pub(crate) last_indent_range: Option<(usize, usize)>,
628}
629
630/// Vim-style options surfaced by `:set`. New fields land here as
631/// individual ex commands gain `:set` plumbing.
632#[derive(Debug, Clone)]
633pub struct Settings {
634 /// Spaces per shift step for `>>` / `<<` / `Ctrl-T` / `Ctrl-D`.
635 pub shiftwidth: usize,
636 /// Visual width of a `\t` character. Stored for future render
637 /// hookup; not yet consumed by the buffer renderer.
638 pub tabstop: usize,
639 /// When true, `/` / `?` patterns and `:s/.../.../` ignore case
640 /// without an explicit `i` flag.
641 pub ignore_case: bool,
642 /// When true *and* `ignore_case` is true, an uppercase letter in
643 /// the pattern flips that search back to case-sensitive. Matches
644 /// vim's `:set smartcase`. Default `false`.
645 pub smartcase: bool,
646 /// Wrap searches past buffer ends. Matches vim's `:set wrapscan`.
647 /// Default `true`.
648 pub wrapscan: bool,
649 /// Wrap column for `gq{motion}` text reflow. Vim's default is 79.
650 pub textwidth: usize,
651 /// When `true`, the Tab key in insert mode inserts `tabstop` spaces
652 /// instead of a literal `\t`. Matches vim's `:set expandtab`.
653 /// Default `false`.
654 pub expandtab: bool,
655 /// Soft tab stop in spaces. When `> 0`, Tab inserts spaces to the
656 /// next softtabstop boundary (when `expandtab`), and Backspace at the
657 /// end of a softtabstop-aligned space run deletes the entire run as
658 /// if it were one tab. `0` disables. Matches vim's `:set softtabstop`.
659 pub softtabstop: usize,
660 /// Soft-wrap mode the renderer + scroll math + `gj` / `gk` use.
661 /// Default is [`hjkl_buffer::Wrap::None`] — long lines extend
662 /// past the right edge and `top_col` clips the left side.
663 /// `:set wrap` flips to char-break wrap; `:set linebreak` flips
664 /// to word-break wrap; `:set nowrap` resets.
665 pub wrap: hjkl_buffer::Wrap,
666 /// When true, the engine drops every edit before it touches the
667 /// buffer — undo, dirty flag, and change log all stay clean.
668 /// Matches vim's `:set readonly` / `:set ro`. Default `false`.
669 pub readonly: bool,
670 /// When `true`, pressing Enter in insert mode copies the leading
671 /// whitespace of the current line onto the new line. Matches vim's
672 /// `:set autoindent`. Default `true` (vim parity).
673 pub autoindent: bool,
674 /// When `true`, bumps indent by one `shiftwidth` after a line ending
675 /// in `{` / `(` / `[`, and strips one indent unit when the user types
676 /// `}` / `)` / `]` on a whitespace-only line. See `compute_enter_indent`
677 /// in `vim.rs` for the tree-sitter plug-in seam. Default `true`.
678 pub smartindent: bool,
679 /// Cap on undo-stack length. Older entries are pruned past this
680 /// bound. `0` means unlimited. Matches vim's `:set undolevels`.
681 /// Default `1000`.
682 pub undo_levels: u32,
683 /// When `true`, cursor motions inside insert mode break the
684 /// current undo group (so a single `u` only reverses the run of
685 /// keystrokes that preceded the motion). Default `true`.
686 /// Currently a no-op — engine doesn't yet break the undo group
687 /// on insert-mode motions; field is wired through `:set
688 /// undobreak` for forward compatibility.
689 pub undo_break_on_motion: bool,
690 /// Vim-flavoured "what counts as a word" character class.
691 /// Comma-separated tokens: `@` = `is_alphabetic()`, `_` = literal
692 /// `_`, `48-57` = decimal char range, bare integer = single char
693 /// code, single ASCII punctuation = literal. Default
694 /// `"@,48-57,_,192-255"` matches vim.
695 pub iskeyword: String,
696 /// Multi-key sequence timeout (e.g. `gg`, `dd`). When the user
697 /// pauses longer than this between keys, any pending prefix is
698 /// abandoned and the next key starts a fresh sequence. Matches
699 /// vim's `:set timeoutlen` / `:set tm` (millis). Default 1000ms.
700 pub timeout_len: core::time::Duration,
701 /// When true, render absolute line numbers in the gutter. Matches
702 /// vim's `:set number` / `:set nu`. Default `true`.
703 pub number: bool,
704 /// When true, render line numbers as offsets from the cursor row.
705 /// Combined with `number`, the cursor row shows its absolute number
706 /// while other rows show the relative offset (vim's `nu+rnu` hybrid).
707 /// Matches vim's `:set relativenumber` / `:set rnu`. Default `false`.
708 pub relativenumber: bool,
709 /// Minimum gutter width in cells for the line-number column.
710 /// Width grows past this to fit the largest displayed number.
711 /// Matches vim's `:set numberwidth` / `:set nuw`. Default `4`.
712 /// Range 1..=20.
713 pub numberwidth: usize,
714 /// Highlight the row where the cursor sits. Matches vim's `:set cursorline`.
715 /// Default `false`.
716 pub cursorline: bool,
717 /// Highlight the column where the cursor sits. Matches vim's `:set cursorcolumn`.
718 /// Default `false`.
719 pub cursorcolumn: bool,
720 /// Sign-column display mode. Matches vim's `:set signcolumn`.
721 /// Default [`crate::types::SignColumnMode::Auto`].
722 pub signcolumn: crate::types::SignColumnMode,
723 /// Number of cells reserved for a fold-marker gutter.
724 /// Matches vim's `:set foldcolumn`. Default `0`.
725 pub foldcolumn: u32,
726 /// Comma-separated 1-based column indices for vertical rulers.
727 /// Matches vim's `:set colorcolumn`. Default `""`.
728 pub colorcolumn: String,
729}
730
731impl Default for Settings {
732 fn default() -> Self {
733 Self {
734 shiftwidth: 4,
735 tabstop: 4,
736 softtabstop: 4,
737 ignore_case: false,
738 smartcase: false,
739 wrapscan: true,
740 textwidth: 79,
741 expandtab: true,
742 wrap: hjkl_buffer::Wrap::None,
743 readonly: false,
744 autoindent: true,
745 smartindent: true,
746 undo_levels: 1000,
747 undo_break_on_motion: true,
748 iskeyword: "@,48-57,_,192-255".to_string(),
749 timeout_len: core::time::Duration::from_millis(1000),
750 number: true,
751 relativenumber: false,
752 numberwidth: 4,
753 cursorline: false,
754 cursorcolumn: false,
755 signcolumn: crate::types::SignColumnMode::Auto,
756 foldcolumn: 0,
757 colorcolumn: String::new(),
758 }
759 }
760}
761
762/// Translate a SPEC [`crate::types::Options`] into the engine's
763/// internal [`Settings`] representation. Field-by-field map; the
764/// shapes are isomorphic except for type widths
765/// (`u32` vs `usize`, [`crate::types::WrapMode`] vs
766/// [`hjkl_buffer::Wrap`]). 0.1.0 (Patch C-δ) collapses both into one
767/// type once the `Editor<B, H>::new(buffer, host, options)` constructor
768/// is the canonical entry point.
769fn settings_from_options(o: &crate::types::Options) -> Settings {
770 Settings {
771 shiftwidth: o.shiftwidth as usize,
772 tabstop: o.tabstop as usize,
773 softtabstop: o.softtabstop as usize,
774 ignore_case: o.ignorecase,
775 smartcase: o.smartcase,
776 wrapscan: o.wrapscan,
777 textwidth: o.textwidth as usize,
778 expandtab: o.expandtab,
779 wrap: match o.wrap {
780 crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
781 crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
782 crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
783 },
784 readonly: o.readonly,
785 autoindent: o.autoindent,
786 smartindent: o.smartindent,
787 undo_levels: o.undo_levels,
788 undo_break_on_motion: o.undo_break_on_motion,
789 iskeyword: o.iskeyword.clone(),
790 timeout_len: o.timeout_len,
791 number: o.number,
792 relativenumber: o.relativenumber,
793 numberwidth: o.numberwidth,
794 cursorline: o.cursorline,
795 cursorcolumn: o.cursorcolumn,
796 signcolumn: o.signcolumn,
797 foldcolumn: o.foldcolumn,
798 colorcolumn: o.colorcolumn.clone(),
799 }
800}
801
802/// Host-observable LSP requests triggered by editor bindings. The
803/// hjkl-engine crate doesn't talk to an LSP itself — it just raises an
804/// intent that the TUI layer picks up and routes to `sqls`.
805#[derive(Debug, Clone, Copy, PartialEq, Eq)]
806pub enum LspIntent {
807 /// `gd` — textDocument/definition at the cursor.
808 GotoDefinition,
809}
810
811impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
812 /// Build an [`Editor`] from a buffer, host adapter, and SPEC options.
813 ///
814 /// 0.1.0 (Patch C-δ): canonical, frozen constructor per SPEC §"Editor
815 /// surface". Replaces the pre-0.1.0 `Editor::new(KeybindingMode)` /
816 /// `with_host` / `with_options` triad — there is no shim.
817 ///
818 /// Consumers that don't need a custom host pass
819 /// [`crate::types::DefaultHost::new()`]; consumers that don't need
820 /// custom options pass [`crate::types::Options::default()`].
821 pub fn new(buffer: hjkl_buffer::Buffer, host: H, options: crate::types::Options) -> Self {
822 let settings = settings_from_options(&options);
823 Self {
824 keybinding_mode: KeybindingMode::Vim,
825 last_yank: None,
826 vim: VimState::default(),
827 undo_stack: Vec::new(),
828 redo_stack: Vec::new(),
829 content_dirty: false,
830 cached_content: None,
831 viewport_height: AtomicU16::new(0),
832 pending_lsp: None,
833 pending_fold_ops: Vec::new(),
834 buffer,
835 style_table: Vec::new(),
836 registers: crate::registers::Registers::default(),
837 styled_spans: Vec::new(),
838 settings,
839 marks: std::collections::BTreeMap::new(),
840 global_marks: std::collections::BTreeMap::new(),
841 current_buffer_id: 0,
842 syntax_fold_ranges: Vec::new(),
843 change_log: Vec::new(),
844 sticky_col: None,
845 host,
846 last_emitted_mode: crate::VimMode::Normal,
847 search_state: crate::search::SearchState::new(),
848 buffer_spans: Vec::new(),
849 pending_content_edits: Vec::new(),
850 pending_content_reset: false,
851 last_indent_range: None,
852 }
853 }
854}
855
856impl<B: crate::types::Buffer, H: crate::types::Host> Editor<B, H> {
857 /// Borrow the buffer (typed `&B`). Host renders through this via
858 /// `hjkl_buffer::BufferView` when `B = hjkl_buffer::Buffer`.
859 pub fn buffer(&self) -> &B {
860 &self.buffer
861 }
862
863 /// Mutably borrow the buffer (typed `&mut B`).
864 pub fn buffer_mut(&mut self) -> &mut B {
865 &mut self.buffer
866 }
867
868 /// Borrow the host adapter directly (typed `&H`).
869 pub fn host(&self) -> &H {
870 &self.host
871 }
872
873 /// Mutably borrow the host adapter (typed `&mut H`).
874 pub fn host_mut(&mut self) -> &mut H {
875 &mut self.host
876 }
877}
878
879impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
880 /// Update the active `iskeyword` spec for word motions
881 /// (`w`/`b`/`e`/`ge` and engine-side `*`/`#` pickup). 0.0.28
882 /// hoisted iskeyword storage out of `Buffer` — `Editor` is the
883 /// single owner now. Equivalent to assigning
884 /// `settings_mut().iskeyword` directly; the dedicated setter is
885 /// retained for source-compatibility with 0.0.27 callers.
886 pub fn set_iskeyword(&mut self, spec: impl Into<String>) {
887 self.settings.iskeyword = spec.into();
888 }
889
890 /// Emit `Host::emit_cursor_shape` if the public mode has changed
891 /// since the last emit. Engine calls this at the end of every input
892 /// step so mode transitions surface to the host without sprinkling
893 /// the call across every `vim.mode = ...` site.
894 pub fn emit_cursor_shape_if_changed(&mut self) {
895 let mode = self.vim_mode();
896 if mode == self.last_emitted_mode {
897 return;
898 }
899 let shape = match mode {
900 crate::VimMode::Insert => crate::types::CursorShape::Bar,
901 _ => crate::types::CursorShape::Block,
902 };
903 self.host.emit_cursor_shape(shape);
904 self.last_emitted_mode = mode;
905 }
906
907 /// Record a yank/cut payload. Writes both the legacy
908 /// [`Editor::last_yank`] field (drained directly by 0.0.28-era
909 /// hosts) and the new [`crate::types::Host::write_clipboard`]
910 /// side-channel (Patch B). Consumers should migrate to a `Host`
911 /// impl whose `write_clipboard` queues the platform-clipboard
912 /// write; the `last_yank` mirror will be removed at 0.1.0.
913 pub(crate) fn record_yank_to_host(&mut self, text: String) {
914 self.host.write_clipboard(text.clone());
915 self.last_yank = Some(text);
916 }
917
918 /// Vim's sticky column (curswant). `None` before the first motion;
919 /// hosts shouldn't normally need to read this directly — it's
920 /// surfaced for migration off `Buffer::sticky_col` and for
921 /// snapshot tests.
922 pub fn sticky_col(&self) -> Option<usize> {
923 self.sticky_col
924 }
925
926 /// Replace the sticky column. Hosts should rarely touch this —
927 /// motion code maintains it through the standard horizontal /
928 /// vertical motion paths.
929 pub fn set_sticky_col(&mut self, col: Option<usize>) {
930 self.sticky_col = col;
931 }
932
933 /// Host hook: replace the cached syntax-derived block ranges that
934 /// `:foldsyntax` consumes. the host calls this on every re-parse;
935 /// the cost is just a `Vec` swap.
936 /// Look up a named mark by character. Returns `(row, col)` if
937 /// set; `None` otherwise. Both lowercase (`'a`–`'z`) and
938 /// uppercase (`'A`–`'Z`) marks live in the same unified
939 /// [`Editor::marks`] map as of 0.0.36.
940 pub fn mark(&self, c: char) -> Option<(usize, usize)> {
941 self.marks.get(&c).copied()
942 }
943
944 /// Set the named mark `c` to `(row, col)`. Used by the FSM's
945 /// `m{a-zA-Z}` keystroke and by [`Editor::restore_snapshot`].
946 pub fn set_mark(&mut self, c: char, pos: (usize, usize)) {
947 self.marks.insert(c, pos);
948 }
949
950 /// Remove the named mark `c` (no-op if unset).
951 pub fn clear_mark(&mut self, c: char) {
952 self.marks.remove(&c);
953 }
954
955 /// Look up an uppercase global mark by letter. Returns
956 /// `(buffer_id, row, col)` if set; `None` otherwise.
957 pub fn global_mark(&self, c: char) -> Option<(u64, usize, usize)> {
958 self.global_marks.get(&c).copied()
959 }
960
961 /// Set an uppercase global mark `c` to `(buffer_id, row, col)`.
962 pub fn set_global_mark(&mut self, c: char, buffer_id: u64, pos: (usize, usize)) {
963 self.global_marks.insert(c, (buffer_id, pos.0, pos.1));
964 }
965
966 /// Return the `buffer_id` this editor is currently attached to.
967 pub fn current_buffer_id(&self) -> u64 {
968 self.current_buffer_id
969 }
970
971 /// Update the `buffer_id` this editor is attached to. Called by the
972 /// app on every `switch_to` so global-mark sets record the correct id.
973 pub fn set_current_buffer_id(&mut self, id: u64) {
974 self.current_buffer_id = id;
975 }
976
977 /// Iterate all global marks (`'A'`–`'Z'`), yielding
978 /// `(mark_char, buffer_id, row, col)`.
979 pub fn global_marks_iter(&self) -> impl Iterator<Item = (char, u64, usize, usize)> + '_ {
980 self.global_marks
981 .iter()
982 .map(|(c, &(bid, r, col))| (*c, bid, r, col))
983 }
984
985 /// Look up a buffer-local lowercase mark (`'a`–`'z`). Kept as a
986 /// thin wrapper over [`Editor::mark`] for source compatibility
987 /// with pre-0.0.36 callers; new code should call
988 /// [`Editor::mark`] directly.
989 #[deprecated(
990 since = "0.0.36",
991 note = "use Editor::mark — lowercase + uppercase marks now live in a single map"
992 )]
993 pub fn buffer_mark(&self, c: char) -> Option<(usize, usize)> {
994 self.mark(c)
995 }
996
997 /// Discard the most recent undo entry. Used by ex commands that
998 /// pre-emptively pushed an undo state (`:s`, `:r`) but ended up
999 /// matching nothing — popping prevents a no-op undo step from
1000 /// polluting the user's history.
1001 ///
1002 /// Returns `true` if an entry was discarded.
1003 pub fn pop_last_undo(&mut self) -> bool {
1004 self.undo_stack.pop().is_some()
1005 }
1006
1007 /// Read all named marks set this session — both lowercase
1008 /// (`'a`–`'z`) and uppercase (`'A`–`'Z`). Iteration is
1009 /// deterministic (BTreeMap-ordered) so snapshot / `:marks`
1010 /// output is stable.
1011 pub fn marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1012 self.marks.iter().map(|(c, p)| (*c, *p))
1013 }
1014
1015 /// Read all buffer-local lowercase marks. Kept for source
1016 /// compatibility with pre-0.0.36 callers (e.g. `:marks` ex
1017 /// command); new code should use [`Editor::marks`] which
1018 /// iterates the unified map.
1019 #[deprecated(
1020 since = "0.0.36",
1021 note = "use Editor::marks — lowercase + uppercase marks now live in a single map"
1022 )]
1023 pub fn buffer_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1024 self.marks
1025 .iter()
1026 .filter(|(c, _)| c.is_ascii_lowercase())
1027 .map(|(c, p)| (*c, *p))
1028 }
1029
1030 /// Position the cursor was at when the user last jumped via
1031 /// `<C-o>` / `g;` / similar. `None` before any jump.
1032 pub fn last_jump_back(&self) -> Option<(usize, usize)> {
1033 self.vim.jump_back.last().copied()
1034 }
1035
1036 /// Position of the last edit (where `.` would replay). `None` if
1037 /// no edit has happened yet in this session.
1038 pub fn last_edit_pos(&self) -> Option<(usize, usize)> {
1039 self.vim.last_edit_pos
1040 }
1041
1042 /// Read-only view of the file-marks table — uppercase / "file"
1043 /// marks (`'A`–`'Z`) the host has set this session. Returns an
1044 /// iterator of `(mark_char, (row, col))` pairs.
1045 ///
1046 /// Mutate via the FSM (`m{A-Z}` keystroke) or via
1047 /// [`Editor::restore_snapshot`].
1048 ///
1049 /// 0.0.36: file marks now live in the unified [`Editor::marks`]
1050 /// map; this accessor is kept for source compatibility and
1051 /// filters the unified map to uppercase entries.
1052 pub fn file_marks(&self) -> impl Iterator<Item = (char, (usize, usize))> + '_ {
1053 self.marks
1054 .iter()
1055 .filter(|(c, _)| c.is_ascii_uppercase())
1056 .map(|(c, p)| (*c, *p))
1057 }
1058
1059 /// Read-only view of the cached syntax-derived block ranges that
1060 /// `:foldsyntax` consumes. Returns the slice the host last
1061 /// installed via [`Editor::set_syntax_fold_ranges`]; empty when
1062 /// no syntax integration is active.
1063 pub fn syntax_fold_ranges(&self) -> &[(usize, usize)] {
1064 &self.syntax_fold_ranges
1065 }
1066
1067 pub fn set_syntax_fold_ranges(&mut self, ranges: Vec<(usize, usize)>) {
1068 self.syntax_fold_ranges = ranges;
1069 }
1070
1071 /// Live settings (read-only). `:set` mutates these via
1072 /// [`Editor::settings_mut`].
1073 pub fn settings(&self) -> &Settings {
1074 &self.settings
1075 }
1076
1077 /// Live settings (mutable). `:set` flows through here to mutate
1078 /// shiftwidth / tabstop / textwidth / ignore_case / wrap. Hosts
1079 /// configuring at startup typically construct a [`Settings`]
1080 /// snapshot and overwrite via `*editor.settings_mut() = …`.
1081 pub fn settings_mut(&mut self) -> &mut Settings {
1082 &mut self.settings
1083 }
1084
1085 /// Returns `true` when `:set readonly` is active. Convenience
1086 /// accessor for hosts that cannot import the internal [`Settings`]
1087 /// type. Phase 5 binary uses this to gate `:w` writes.
1088 pub fn is_readonly(&self) -> bool {
1089 self.settings.readonly
1090 }
1091
1092 /// Borrow the engine search state. Hosts inspecting the
1093 /// committed `/` / `?` pattern (e.g. for status-line display) or
1094 /// feeding the active regex into `BufferView::search_pattern`
1095 /// read it from here.
1096 pub fn search_state(&self) -> &crate::search::SearchState {
1097 &self.search_state
1098 }
1099
1100 /// Mutable engine search state. Hosts driving search
1101 /// programmatically (test fixtures, scripted demos) write the
1102 /// pattern through here.
1103 pub fn search_state_mut(&mut self) -> &mut crate::search::SearchState {
1104 &mut self.search_state
1105 }
1106
1107 /// Install `pattern` as the active search regex on the engine
1108 /// state and clear the cached row matches. Pass `None` to clear.
1109 /// 0.0.37: dropped the buffer-side mirror that 0.0.35 introduced
1110 /// — `BufferView` now takes the regex through its `search_pattern`
1111 /// field per step 3 of `DESIGN_33_METHOD_CLASSIFICATION.md`.
1112 pub fn set_search_pattern(&mut self, pattern: Option<regex::Regex>) {
1113 self.search_state.set_pattern(pattern);
1114 }
1115
1116 /// Drive `n` (or the `/` commit equivalent) — advance the cursor
1117 /// to the next match of `search_state.pattern` from the cursor's
1118 /// current position. Returns `true` when a match was found.
1119 /// `skip_current = true` excludes a match the cursor sits on.
1120 pub fn search_advance_forward(&mut self, skip_current: bool) -> bool {
1121 crate::search::search_forward(&mut self.buffer, &mut self.search_state, skip_current)
1122 }
1123
1124 /// Drive `N` — symmetric counterpart of [`Editor::search_advance_forward`].
1125 pub fn search_advance_backward(&mut self, skip_current: bool) -> bool {
1126 crate::search::search_backward(&mut self.buffer, &mut self.search_state, skip_current)
1127 }
1128
1129 /// Snapshot of the unnamed register (the default `p` / `P` source).
1130 pub fn yank(&self) -> &str {
1131 &self.registers.unnamed.text
1132 }
1133
1134 /// Borrow the full register bank — `"`, `"0`–`"9`, `"a`–`"z`.
1135 pub fn registers(&self) -> &crate::registers::Registers {
1136 &self.registers
1137 }
1138
1139 /// Mutably borrow the full register bank. Hosts that share registers
1140 /// across multiple editors (e.g. multi-buffer `yy` / `p`) overwrite
1141 /// the slots here on buffer switch.
1142 pub fn registers_mut(&mut self) -> &mut crate::registers::Registers {
1143 &mut self.registers
1144 }
1145
1146 /// Host hook: load the OS clipboard's contents into the `"+` / `"*`
1147 /// register slot. the host calls this before letting vim consume a
1148 /// paste so `"*p` / `"+p` reflect the live clipboard rather than a
1149 /// stale snapshot from the last yank.
1150 pub fn sync_clipboard_register(&mut self, text: String, linewise: bool) {
1151 self.registers.set_clipboard(text, linewise);
1152 }
1153
1154 /// Return the user's pending register selection (set via `"<reg>` chord
1155 /// before an operator). `None` if no register was selected — caller should
1156 /// use the unnamed register `"`.
1157 ///
1158 /// Read-only — does not consume / clear the pending selection. The
1159 /// register is cleared by the engine after the next operator fires.
1160 ///
1161 /// Promoted in 0.6.X for Phase 4e to let the App's visual-op dispatch arm
1162 /// honor `"a` + visual op chord sequences.
1163 pub fn pending_register(&self) -> Option<char> {
1164 self.vim.pending_register
1165 }
1166
1167 /// True when the user's pending register selector is `+` or `*`.
1168 /// the host peeks this so it can refresh `sync_clipboard_register`
1169 /// only when a clipboard read is actually about to happen.
1170 pub fn pending_register_is_clipboard(&self) -> bool {
1171 matches!(self.vim.pending_register, Some('+') | Some('*'))
1172 }
1173
1174 /// Register currently being recorded into via `q{reg}`. `None` when
1175 /// no recording is active. Hosts use this to surface a "recording @r"
1176 /// indicator in the status line.
1177 pub fn recording_register(&self) -> Option<char> {
1178 self.vim.recording_macro
1179 }
1180
1181 /// Pending repeat count the user has typed but not yet resolved
1182 /// (e.g. pressing `5` before `d`). `None` when nothing is pending.
1183 /// Hosts surface this in a "showcmd" area.
1184 pub fn pending_count(&self) -> Option<u32> {
1185 self.vim.pending_count_val()
1186 }
1187
1188 /// The operator character for any in-flight operator that is waiting
1189 /// for a motion (e.g. `d` after the user types `d` but before a
1190 /// motion). Returns `None` when no operator is pending.
1191 pub fn pending_op(&self) -> Option<char> {
1192 self.vim.pending_op_char()
1193 }
1194
1195 /// `true` when the engine is in any pending chord state — waiting for
1196 /// the next key to complete a command (e.g. `r<char>` replace,
1197 /// `f<char>` find, `m<a>` set-mark, `'<a>` goto-mark, operator-pending
1198 /// after `d` / `c` / `y`, `g`-prefix continuation, `z`-prefix continuation,
1199 /// register selection `"<reg>`, macro recording target, etc).
1200 ///
1201 /// Hosts use this to bypass their own chord dispatch (keymap tries, etc.)
1202 /// and forward keys directly to the engine so in-flight commands can
1203 /// complete without the host eating their continuation keys.
1204 pub fn is_chord_pending(&self) -> bool {
1205 self.vim.is_chord_pending()
1206 }
1207
1208 /// `true` when `insert_ctrl_r_arm()` has been called and the dispatcher
1209 /// is waiting for the next typed character to name the register to paste.
1210 /// The dispatcher should call `insert_paste_register(c)` instead of
1211 /// `insert_char(c)` for the next printable key, then the flag auto-clears.
1212 ///
1213 /// Phase 6.5: exposed so the app-level `dispatch_insert_key` can branch
1214 /// without having to drive the full FSM.
1215 pub fn is_insert_register_pending(&self) -> bool {
1216 self.vim.insert_pending_register
1217 }
1218
1219 /// Clear the `Ctrl-R` register-paste pending flag. Call this immediately
1220 /// before `insert_paste_register(c)` in app-level dispatchers so that the
1221 /// flag does not persist into the next key. Call before
1222 /// `insert_paste_register_bridge` (which `hjkl_vim::insert` does).
1223 ///
1224 /// Phase 6.5: used by `dispatch_insert_key` in the app crate.
1225 pub fn clear_insert_register_pending(&mut self) {
1226 self.vim.insert_pending_register = false;
1227 }
1228
1229 /// Read-only view of the jump-back list (positions pushed on "big"
1230 /// motions). Newest entry is at the back — `Ctrl-o` pops from there.
1231 #[allow(clippy::type_complexity)]
1232 pub fn jump_list(&self) -> (&[(usize, usize)], &[(usize, usize)]) {
1233 (&self.vim.jump_back, &self.vim.jump_fwd)
1234 }
1235
1236 /// Read-only view of the change list (positions of recent edits) plus
1237 /// the current walk cursor. Newest entry is at the back.
1238 pub fn change_list(&self) -> (&[(usize, usize)], Option<usize>) {
1239 (&self.vim.change_list, self.vim.change_list_cursor)
1240 }
1241
1242 /// Replace the unnamed register without touching any other slot.
1243 /// For host-driven imports (e.g. system clipboard); operator
1244 /// code uses [`record_yank`] / [`record_delete`].
1245 pub fn set_yank(&mut self, text: impl Into<String>) {
1246 let text = text.into();
1247 let linewise = self.vim.yank_linewise;
1248 self.registers.unnamed = crate::registers::Slot { text, linewise };
1249 }
1250
1251 /// Record a yank into `"` and `"0`, plus the named target if the
1252 /// user prefixed `"reg`. Updates `vim.yank_linewise` for the
1253 /// paste path.
1254 pub(crate) fn record_yank(&mut self, text: String, linewise: bool) {
1255 self.vim.yank_linewise = linewise;
1256 let target = self.vim.pending_register.take();
1257 self.registers.record_yank(text, linewise, target);
1258 }
1259
1260 /// Direct write to a named register slot — bypasses the unnamed
1261 /// `"` and `"0` updates that `record_yank` does. Used by the
1262 /// macro recorder so finishing a `q{reg}` recording doesn't
1263 /// pollute the user's last yank.
1264 pub fn set_named_register_text(&mut self, reg: char, text: String) {
1265 if let Some(slot) = match reg {
1266 'a'..='z' => Some(&mut self.registers.named[(reg as u8 - b'a') as usize]),
1267 'A'..='Z' => {
1268 Some(&mut self.registers.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize])
1269 }
1270 _ => None,
1271 } {
1272 slot.text = text;
1273 slot.linewise = false;
1274 }
1275 }
1276
1277 /// Record a delete / change into `"` and the `"1`–`"9` ring.
1278 /// Honours the active named-register prefix.
1279 pub(crate) fn record_delete(&mut self, text: String, linewise: bool) {
1280 self.vim.yank_linewise = linewise;
1281 let target = self.vim.pending_register.take();
1282 self.registers.record_delete(text, linewise, target);
1283 }
1284
1285 /// Install styled syntax spans using the engine-native
1286 /// [`crate::types::Style`]. Always available — engine is ratatui-free.
1287 /// Ratatui hosts use
1288 /// `hjkl_engine_tui::EditorRatatuiExt::install_ratatui_syntax_spans`
1289 /// which converts at the boundary and delegates here.
1290 ///
1291 /// Renamed from `install_engine_syntax_spans` in 0.0.32 — at the
1292 /// 0.1.0 freeze the unprefixed name is the universally-available
1293 /// engine-native variant.
1294 pub fn install_syntax_spans(&mut self, spans: Vec<Vec<(usize, usize, crate::types::Style)>>) {
1295 let line_byte_lens: Vec<usize> = (0..buf_row_count(&self.buffer))
1296 .map(|r| buf_line(&self.buffer, r).map(|s| s.len()).unwrap_or(0))
1297 .collect();
1298 let mut by_row: Vec<Vec<hjkl_buffer::Span>> = Vec::with_capacity(spans.len());
1299 let mut engine_spans: Vec<Vec<(usize, usize, crate::types::Style)>> =
1300 Vec::with_capacity(spans.len());
1301 for (row, row_spans) in spans.iter().enumerate() {
1302 let line_len = line_byte_lens.get(row).copied().unwrap_or(0);
1303 let mut translated = Vec::with_capacity(row_spans.len());
1304 let mut translated_e = Vec::with_capacity(row_spans.len());
1305 for (start, end, style) in row_spans {
1306 let end_clamped = (*end).min(line_len);
1307 if end_clamped <= *start {
1308 continue;
1309 }
1310 let id = self.intern_style(*style);
1311 translated.push(hjkl_buffer::Span::new(*start, end_clamped, id));
1312 translated_e.push((*start, end_clamped, *style));
1313 }
1314 by_row.push(translated);
1315 engine_spans.push(translated_e);
1316 }
1317 self.buffer_spans = by_row;
1318 self.styled_spans = engine_spans;
1319 }
1320
1321 /// Read-only view of the style table in engine-native form —
1322 /// id `i` → `style_table[i]`. Always available, no cfg gate.
1323 ///
1324 /// Ratatui hosts that need a `ratatui::style::Style` slice should
1325 /// use `hjkl_engine_tui::EditorRatatuiExt::ratatui_style_table` or
1326 /// convert individual entries via `hjkl_engine_tui::style_to_ratatui`.
1327 pub fn style_table(&self) -> &[crate::types::Style] {
1328 &self.style_table
1329 }
1330
1331 /// Per-row syntax span overlay, one `Vec<Span>` per buffer row.
1332 /// Hosts feed this slice into [`hjkl_buffer::BufferView::spans`]
1333 /// per draw frame.
1334 ///
1335 /// 0.0.37: replaces `editor.buffer().spans()` per step 3 of
1336 /// `DESIGN_33_METHOD_CLASSIFICATION.md`. The buffer no longer
1337 /// caches spans; they live on the engine and route through the
1338 /// `Host::syntax_highlights` pipeline.
1339 pub fn buffer_spans(&self) -> &[Vec<hjkl_buffer::Span>] {
1340 &self.buffer_spans
1341 }
1342
1343 /// Intern a SPEC [`crate::types::Style`] and return its opaque id.
1344 /// Engine-native — the unified `style_table` is always engine-native.
1345 /// Linear-scan dedup — the table grows only as new tree-sitter token
1346 /// kinds appear, so it stays tiny. Ratatui callers use
1347 /// `hjkl_engine_tui::EditorRatatuiExt::intern_ratatui_style` which
1348 /// converts at the boundary and delegates here.
1349 ///
1350 /// Renamed from `intern_engine_style` in 0.0.32 — at 0.1.0 freeze
1351 /// the unprefixed name is the universally-available engine-native
1352 /// variant.
1353 pub fn intern_style(&mut self, style: crate::types::Style) -> u32 {
1354 if let Some(idx) = self.style_table.iter().position(|s| *s == style) {
1355 return idx as u32;
1356 }
1357 self.style_table.push(style);
1358 (self.style_table.len() - 1) as u32
1359 }
1360
1361 /// Look up an interned style by id and return it as a SPEC
1362 /// [`crate::types::Style`]. Returns `None` for ids past the end
1363 /// of the table.
1364 pub fn engine_style_at(&self, id: u32) -> Option<crate::types::Style> {
1365 self.style_table.get(id as usize).copied()
1366 }
1367
1368 /// Historical reverse-sync hook from when the textarea mirrored
1369 /// the buffer. Now that Buffer is the cursor authority this is a
1370 /// no-op; call sites can remain in place during the migration.
1371 pub fn push_buffer_cursor_to_textarea(&mut self) {}
1372
1373 /// Force the host viewport's top row without touching the
1374 /// cursor. Used by tests that simulate a scroll without the
1375 /// SCROLLOFF cursor adjustment that `scroll_down` / `scroll_up`
1376 /// apply.
1377 ///
1378 /// 0.0.34 (Patch C-δ.1): writes through `Host::viewport_mut`
1379 /// instead of the (now-deleted) `Buffer::viewport_mut`.
1380 pub fn set_viewport_top(&mut self, row: usize) {
1381 let last = buf_row_count(&self.buffer).saturating_sub(1);
1382 let target = row.min(last);
1383 self.host.viewport_mut().top_row = target;
1384 }
1385
1386 /// Set the cursor to `(row, col)`, clamped to the buffer's
1387 /// content. Hosts use this for goto-line, jump-to-mark, and
1388 /// programmatic cursor placement.
1389 ///
1390 /// Resets `sticky_col` (curswant) to `col` — every explicit jump
1391 /// (goto-line, jump-to-mark, search hit, click, `]d`) follows vim
1392 /// semantics. Only `j`/`k`/`+`/`-` READ `sticky_col`; everything
1393 /// else resets it to the column where the cursor actually landed.
1394 pub fn jump_cursor(&mut self, row: usize, col: usize) {
1395 buf_set_cursor_rc(&mut self.buffer, row, col);
1396 self.sticky_col = Some(col);
1397 }
1398
1399 /// Set the cursor to `(row, col)` without modifying `sticky_col`.
1400 ///
1401 /// Use this for host-side state restores (viewport sync, snapshot
1402 /// replay) where the cursor was already at this position semantically
1403 /// and the host's sticky tracking should remain authoritative.
1404 ///
1405 /// For user-facing jumps (goto-line, search hit, picker `<CR>`, `]d`,
1406 /// click), use [`Editor::jump_cursor`] which DOES reset `sticky_col`
1407 /// per vim curswant semantics.
1408 pub fn set_cursor_quiet(&mut self, row: usize, col: usize) {
1409 buf_set_cursor_rc(&mut self.buffer, row, col);
1410 }
1411
1412 /// `(row, col)` cursor read sourced from the migration buffer.
1413 /// Equivalent to `self.textarea.cursor()` when the two are in
1414 /// sync — which is the steady state during Phase 7f because
1415 /// every step opens with `sync_buffer_content_from_textarea` and
1416 /// every ported motion pushes the result back. Prefer this over
1417 /// `self.textarea.cursor()` so call sites keep working unchanged
1418 /// once the textarea field is ripped.
1419 pub fn cursor(&self) -> (usize, usize) {
1420 buf_cursor_rc(&self.buffer)
1421 }
1422
1423 /// Drain any pending LSP intent raised by the last key. Returns
1424 /// `None` when no intent is armed.
1425 pub fn take_lsp_intent(&mut self) -> Option<LspIntent> {
1426 self.pending_lsp.take()
1427 }
1428
1429 /// Drain every [`crate::types::FoldOp`] raised since the last
1430 /// call. Hosts that mirror the engine's fold storage (or that
1431 /// project folds onto a separate fold tree, LSP folding ranges,
1432 /// …) drain this each step and dispatch as their own
1433 /// [`crate::types::Host::Intent`] requires.
1434 ///
1435 /// The engine has already applied every op locally against the
1436 /// in-tree [`hjkl_buffer::Buffer`] fold storage via
1437 /// [`crate::buffer_impl::BufferFoldProviderMut`], so hosts that
1438 /// don't track folds independently can ignore the queue
1439 /// (or simply never call this drain).
1440 ///
1441 /// Introduced in 0.0.38 (Patch C-δ.4).
1442 pub fn take_fold_ops(&mut self) -> Vec<crate::types::FoldOp> {
1443 std::mem::take(&mut self.pending_fold_ops)
1444 }
1445
1446 /// Dispatch a [`crate::types::FoldOp`] through the canonical fold
1447 /// surface: queue it for host observation (drained by
1448 /// [`Editor::take_fold_ops`]) and apply it locally against the
1449 /// in-tree buffer fold storage via
1450 /// [`crate::buffer_impl::BufferFoldProviderMut`]. Engine call sites
1451 /// (vim FSM `z…` chords, `:fold*` Ex commands, edit-pipeline
1452 /// invalidation) route every fold mutation through this method.
1453 ///
1454 /// Introduced in 0.0.38 (Patch C-δ.4).
1455 pub fn apply_fold_op(&mut self, op: crate::types::FoldOp) {
1456 use crate::types::FoldProvider;
1457 self.pending_fold_ops.push(op);
1458 let mut provider = crate::buffer_impl::BufferFoldProviderMut::new(&mut self.buffer);
1459 provider.apply(op);
1460 }
1461
1462 /// Refresh the host viewport's height from the cached
1463 /// `viewport_height_value()`. Called from the per-step
1464 /// boilerplate; was the textarea → buffer mirror before Phase 7f
1465 /// put Buffer in charge. 0.0.28 hoisted sticky_col out of
1466 /// `Buffer`. 0.0.34 (Patch C-δ.1) routes the height write through
1467 /// `Host::viewport_mut`.
1468 pub fn sync_buffer_from_textarea(&mut self) {
1469 let height = self.viewport_height_value();
1470 self.host.viewport_mut().height = height;
1471 }
1472
1473 /// Was the full textarea → buffer content sync. Buffer is the
1474 /// content authority now; this remains as a no-op so the per-step
1475 /// call sites don't have to be ripped in the same patch.
1476 pub(crate) fn sync_buffer_content_from_textarea(&mut self) {
1477 self.sync_buffer_from_textarea();
1478 }
1479
1480 /// Push a `(row, col)` onto the back-jumplist so `Ctrl-o` returns
1481 /// to it later. Used by host-driven jumps (e.g. `gd`) that move
1482 /// the cursor without going through the vim engine's motion
1483 /// machinery, where push_jump fires automatically.
1484 pub fn record_jump(&mut self, pos: (usize, usize)) {
1485 const JUMPLIST_MAX: usize = 100;
1486 self.vim.jump_back.push(pos);
1487 if self.vim.jump_back.len() > JUMPLIST_MAX {
1488 self.vim.jump_back.remove(0);
1489 }
1490 self.vim.jump_fwd.clear();
1491 }
1492
1493 /// Host apps call this each draw with the current text area height so
1494 /// scroll helpers can clamp the cursor without recomputing layout.
1495 pub fn set_viewport_height(&self, height: u16) {
1496 self.viewport_height.store(height, Ordering::Relaxed);
1497 }
1498
1499 /// Last height published by `set_viewport_height` (in rows).
1500 pub fn viewport_height_value(&self) -> u16 {
1501 self.viewport_height.load(Ordering::Relaxed)
1502 }
1503
1504 /// Apply `edit` against the buffer and return the inverse so the
1505 /// host can push it onto an undo stack. Side effects: dirty
1506 /// flag, change-list ring, mark / jump-list shifts, change_log
1507 /// append, fold invalidation around the touched rows.
1508 ///
1509 /// The primary edit funnel — both FSM operators and ex commands
1510 /// route mutations through here so the side effects fire
1511 /// uniformly.
1512 pub fn mutate_edit(&mut self, edit: hjkl_buffer::Edit) -> hjkl_buffer::Edit {
1513 // `:set readonly` short-circuits every mutation funnel: no
1514 // buffer change, no dirty flag, no undo entry, no change-log
1515 // emission. We swallow the requested `edit` and hand back a
1516 // self-inverse no-op (`InsertStr` of an empty string at the
1517 // current cursor) so callers that push the return value onto
1518 // an undo stack still get a structurally valid round trip.
1519 if self.settings.readonly {
1520 let _ = edit;
1521 return hjkl_buffer::Edit::InsertStr {
1522 at: buf_cursor_pos(&self.buffer),
1523 text: String::new(),
1524 };
1525 }
1526 let pre_row = buf_cursor_row(&self.buffer);
1527 let pre_rows = buf_row_count(&self.buffer);
1528 // Capture the pre-edit cursor for the dot mark (`'.` / `` `. ``).
1529 // Vim's `:h '.` says "the position where the last change was made",
1530 // meaning the change-start, not the post-insert cursor. We snap it
1531 // here before `apply_buffer_edit` moves the cursor.
1532 let (pre_edit_row, pre_edit_col) = buf_cursor_rc(&self.buffer);
1533 // Map the underlying buffer edit to a SPEC EditOp for
1534 // change-log emission before consuming it. Coarse — see
1535 // change_log field doc on the struct.
1536 self.change_log.extend(edit_to_editops(&edit));
1537 // Compute ContentEdit fan-out from the pre-edit buffer state.
1538 // Done before `apply_buffer_edit` consumes `edit` so we can
1539 // inspect the operation's fields and the buffer's pre-edit row
1540 // bytes (needed for byte_of_row / col_byte conversion). Edits
1541 // are pushed onto `pending_content_edits` for host drain.
1542 let content_edits = content_edits_from_buffer_edit(&self.buffer, &edit);
1543 self.pending_content_edits.extend(content_edits);
1544 // 0.0.42 (Patch C-δ.7): the `apply_edit` reach is centralized
1545 // in [`crate::buf_helpers::apply_buffer_edit`] (option (c) of
1546 // the 0.0.42 plan — see that fn's doc comment). The free fn
1547 // takes `&mut hjkl_buffer::Buffer` so the editor body itself
1548 // no longer carries a `self.buffer.<inherent>` hop.
1549 let inverse = apply_buffer_edit(&mut self.buffer, edit);
1550 let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1551 // Drop any folds the edit's range overlapped — vim opens the
1552 // surrounding fold automatically when you edit inside it. The
1553 // approximation here invalidates folds covering either the
1554 // pre-edit cursor row or the post-edit cursor row, which
1555 // catches the common single-line / multi-line edit shapes.
1556 let lo = pre_row.min(pos_row);
1557 let hi = pre_row.max(pos_row);
1558 self.apply_fold_op(crate::types::FoldOp::Invalidate {
1559 start_row: lo,
1560 end_row: hi,
1561 });
1562 // Dot mark records the PRE-edit position (change start), matching
1563 // vim's `:h '.` semantics. Previously this stored the post-edit
1564 // cursor, which diverged from nvim on `iX<Esc>j`.
1565 self.vim.last_edit_pos = Some((pre_edit_row, pre_edit_col));
1566 // Append to the change-list ring (skip when the cursor sits on
1567 // the same cell as the last entry — back-to-back keystrokes on
1568 // one column shouldn't pollute the ring). A new edit while
1569 // walking the ring trims the forward half, vim style.
1570 let entry = (pos_row, pos_col);
1571 if self.vim.change_list.last() != Some(&entry) {
1572 if let Some(idx) = self.vim.change_list_cursor.take() {
1573 self.vim.change_list.truncate(idx + 1);
1574 }
1575 self.vim.change_list.push(entry);
1576 let len = self.vim.change_list.len();
1577 if len > crate::vim::CHANGE_LIST_MAX {
1578 self.vim
1579 .change_list
1580 .drain(0..len - crate::vim::CHANGE_LIST_MAX);
1581 }
1582 }
1583 self.vim.change_list_cursor = None;
1584 // Shift / drop marks + jump-list entries to track the row
1585 // delta the edit produced. Without this, every line-changing
1586 // edit silently invalidates `'a`-style positions.
1587 let post_rows = buf_row_count(&self.buffer);
1588 let delta = post_rows as isize - pre_rows as isize;
1589 if delta != 0 {
1590 self.shift_marks_after_edit(pre_row, delta);
1591 }
1592 self.push_buffer_content_to_textarea();
1593 self.mark_content_dirty();
1594 inverse
1595 }
1596
1597 /// Migrate user marks + jumplist entries when an edit at row
1598 /// `edit_start` changes the buffer's row count by `delta` (positive
1599 /// for inserts, negative for deletes). Marks tied to a deleted row
1600 /// are dropped; marks past the affected band shift by `delta`.
1601 fn shift_marks_after_edit(&mut self, edit_start: usize, delta: isize) {
1602 if delta == 0 {
1603 return;
1604 }
1605 // Deleted-row band (only meaningful for delta < 0). Inclusive
1606 // start, exclusive end.
1607 let drop_end = if delta < 0 {
1608 edit_start.saturating_add((-delta) as usize)
1609 } else {
1610 edit_start
1611 };
1612 let shift_threshold = drop_end.max(edit_start.saturating_add(1));
1613
1614 // 0.0.36: lowercase + uppercase marks share the unified
1615 // `marks` map; one pass migrates both.
1616 let mut to_drop: Vec<char> = Vec::new();
1617 for (c, (row, _col)) in self.marks.iter_mut() {
1618 if (edit_start..drop_end).contains(row) {
1619 to_drop.push(*c);
1620 } else if *row >= shift_threshold {
1621 *row = ((*row as isize) + delta).max(0) as usize;
1622 }
1623 }
1624 for c in to_drop {
1625 self.marks.remove(&c);
1626 }
1627
1628 // Shift global marks that belong to the current buffer.
1629 let cur_bid = self.current_buffer_id;
1630 let mut global_to_drop: Vec<char> = Vec::new();
1631 for (c, (bid, row, _col)) in self.global_marks.iter_mut() {
1632 if *bid != cur_bid {
1633 continue;
1634 }
1635 if (edit_start..drop_end).contains(row) {
1636 global_to_drop.push(*c);
1637 } else if *row >= shift_threshold {
1638 *row = ((*row as isize) + delta).max(0) as usize;
1639 }
1640 }
1641 for c in global_to_drop {
1642 self.global_marks.remove(&c);
1643 }
1644
1645 let shift_jumps = |entries: &mut Vec<(usize, usize)>| {
1646 entries.retain(|(row, _)| !(edit_start..drop_end).contains(row));
1647 for (row, _) in entries.iter_mut() {
1648 if *row >= shift_threshold {
1649 *row = ((*row as isize) + delta).max(0) as usize;
1650 }
1651 }
1652 };
1653 shift_jumps(&mut self.vim.jump_back);
1654 shift_jumps(&mut self.vim.jump_fwd);
1655 }
1656
1657 /// Reverse-sync helper paired with [`Editor::mutate_edit`]: rebuild
1658 /// the textarea from the buffer's lines + cursor, preserving yank
1659 /// text. Heavy (allocates a fresh `TextArea`) but correct; the
1660 /// textarea field disappears at the end of Phase 7f anyway.
1661 /// No-op since Buffer is the content authority. Retained as a
1662 /// shim so call sites in `mutate_edit` and friends don't have to
1663 /// be ripped in lockstep with the field removal.
1664 pub(crate) fn push_buffer_content_to_textarea(&mut self) {}
1665
1666 /// Single choke-point for "the buffer just changed". Sets the
1667 /// dirty flag and drops the cached `content_arc` snapshot so
1668 /// subsequent reads rebuild from the live textarea. Callers
1669 /// mutating `textarea` directly (e.g. the TUI's bracketed-paste
1670 /// path) must invoke this to keep the cache honest.
1671 pub fn mark_content_dirty(&mut self) {
1672 self.content_dirty = true;
1673 self.cached_content = None;
1674 }
1675
1676 /// Returns true if content changed since the last call, then clears the flag.
1677 pub fn take_dirty(&mut self) -> bool {
1678 let dirty = self.content_dirty;
1679 self.content_dirty = false;
1680 dirty
1681 }
1682
1683 /// Drain the queue of [`crate::types::ContentEdit`]s emitted since
1684 /// the last call. Each entry corresponds to a single buffer
1685 /// mutation funnelled through [`Editor::mutate_edit`]; block edits
1686 /// fan out to one entry per row touched.
1687 ///
1688 /// Hosts call this each frame (after [`Editor::take_content_reset`])
1689 /// to fan edits into a tree-sitter parser via `Tree::edit`.
1690 pub fn take_content_edits(&mut self) -> Vec<crate::types::ContentEdit> {
1691 std::mem::take(&mut self.pending_content_edits)
1692 }
1693
1694 /// Returns `true` if a bulk buffer replacement happened since the
1695 /// last call (e.g. `set_content` / `restore` / undo restore), then
1696 /// clears the flag. When this returns `true`, hosts should drop
1697 /// any retained syntax tree before consuming
1698 /// [`Editor::take_content_edits`].
1699 pub fn take_content_reset(&mut self) -> bool {
1700 let r = self.pending_content_reset;
1701 self.pending_content_reset = false;
1702 r
1703 }
1704
1705 /// Pull-model coarse change observation. If content changed since
1706 /// the last call, returns `Some(Arc<String>)` with the new content
1707 /// and clears the dirty flag; otherwise returns `None`.
1708 ///
1709 /// Hosts that need fine-grained edit deltas (e.g., DOM patching at
1710 /// the character level) should diff against their own previous
1711 /// snapshot. The SPEC `take_changes() -> Vec<EditOp>` API lands
1712 /// once every edit path inside the engine is instrumented; this
1713 /// coarse form covers the pull-model use case in the meantime.
1714 pub fn take_content_change(&mut self) -> Option<std::sync::Arc<String>> {
1715 if !self.content_dirty {
1716 return None;
1717 }
1718 let arc = self.content_arc();
1719 self.content_dirty = false;
1720 Some(arc)
1721 }
1722
1723 /// Width in cells of the line-number gutter for the current buffer
1724 /// and settings. Matches what [`Editor::cursor_screen_pos`] reserves
1725 /// in front of the text column. Returns `0` when both `number` and
1726 /// `relativenumber` are off.
1727 pub fn lnum_width(&self) -> u16 {
1728 if self.settings.number || self.settings.relativenumber {
1729 let needed = buf_row_count(&self.buffer).to_string().len() + 1;
1730 needed.max(self.settings.numberwidth) as u16
1731 } else {
1732 0
1733 }
1734 }
1735
1736 /// Returns the cursor's row within the visible textarea (0-based), updating
1737 /// the stored viewport top so subsequent calls remain accurate.
1738 pub fn cursor_screen_row(&mut self, height: u16) -> u16 {
1739 let cursor = buf_cursor_row(&self.buffer);
1740 let top = self.host.viewport().top_row;
1741 cursor.saturating_sub(top).min(height as usize - 1) as u16
1742 }
1743
1744 /// Returns the cursor's screen position `(x, y)` for the textarea
1745 /// described by `(area_x, area_y, area_width, area_height)`.
1746 /// Accounts for line-number gutter, viewport scroll, and any extra
1747 /// gutter width to the left of the number column (sign column, fold
1748 /// column). Returns `None` if the cursor is outside the visible
1749 /// viewport. Always available (engine-native; no ratatui dependency).
1750 ///
1751 /// `extra_gutter_width` is added to the number-column width before
1752 /// computing the cursor x position. Callers (e.g. `apps/hjkl/src/render.rs`)
1753 /// pass `sign_w + fold_w` here so the cursor lands on the correct cell
1754 /// when a dedicated sign or fold column is present.
1755 ///
1756 /// Renamed from `cursor_screen_pos_xywh` in 0.0.32.
1757 pub fn cursor_screen_pos(
1758 &self,
1759 area_x: u16,
1760 area_y: u16,
1761 area_width: u16,
1762 area_height: u16,
1763 extra_gutter_width: u16,
1764 ) -> Option<(u16, u16)> {
1765 let (pos_row, pos_col) = buf_cursor_rc(&self.buffer);
1766 let v = self.host.viewport();
1767 if pos_row < v.top_row || pos_col < v.top_col {
1768 return None;
1769 }
1770 let lnum_width = self.lnum_width();
1771 // Full offset from the left edge of the window to the first text cell.
1772 let gutter_total = lnum_width + extra_gutter_width;
1773 let dy = (pos_row - v.top_row) as u16;
1774 // Convert char column to visual column so cursor lands on the
1775 // correct cell when the line contains tabs (which the renderer
1776 // expands to TAB_WIDTH stops). Tab width must match the renderer.
1777 let line = self.buffer.line(pos_row).unwrap_or_default();
1778 let tab_width = if v.tab_width == 0 {
1779 4
1780 } else {
1781 v.tab_width as usize
1782 };
1783 let visual_pos = visual_col_for_char(&line, pos_col, tab_width);
1784 let visual_top = visual_col_for_char(&line, v.top_col, tab_width);
1785 let dx = (visual_pos - visual_top) as u16;
1786 if dy >= area_height || dx + gutter_total >= area_width {
1787 return None;
1788 }
1789 Some((area_x + gutter_total + dx, area_y + dy))
1790 }
1791
1792 /// Returns the current vim mode. Phase 6.3: reads from the stable
1793 /// `current_mode` field (kept in sync by both the FSM step loop and
1794 /// the Phase 6.3 primitive bridges) rather than deriving from the
1795 /// FSM-internal `mode` field via `public_mode()`.
1796 pub fn vim_mode(&self) -> VimMode {
1797 self.vim.current_mode
1798 }
1799
1800 /// Bounds of the active visual-block rectangle as
1801 /// `(top_row, bot_row, left_col, right_col)` — all inclusive.
1802 /// `None` when we're not in VisualBlock mode.
1803 /// Read-only view of the live `/` or `?` prompt. `None` outside
1804 /// search-prompt mode.
1805 pub fn search_prompt(&self) -> Option<&crate::vim::SearchPrompt> {
1806 self.vim.search_prompt.as_ref()
1807 }
1808
1809 /// Most recent committed search pattern (persists across `n` / `N`
1810 /// and across prompt exits). `None` before the first search.
1811 pub fn last_search(&self) -> Option<&str> {
1812 self.vim.last_search.as_deref()
1813 }
1814
1815 /// Whether the last committed search was a forward `/` (`true`) or
1816 /// a backward `?` (`false`). `n` and `N` consult this to honour the
1817 /// direction the user committed.
1818 pub fn last_search_forward(&self) -> bool {
1819 self.vim.last_search_forward
1820 }
1821
1822 /// Set the most recent committed search text + direction. Used by
1823 /// host-driven prompts (e.g. apps/hjkl's `/` `?` prompt that lives
1824 /// outside the engine's vim FSM) so `n` / `N` repeat the host's
1825 /// most recent commit with the right direction. Pass `None` /
1826 /// `true` to clear.
1827 pub fn set_last_search(&mut self, text: Option<String>, forward: bool) {
1828 self.vim.last_search = text;
1829 self.vim.last_search_forward = forward;
1830 }
1831
1832 /// The most recent successful `:s` command. `None` before the first substitute.
1833 /// Used by `:&` / `:&&` to repeat it.
1834 pub fn last_substitute(&self) -> Option<&crate::substitute::SubstituteCmd> {
1835 self.vim.last_substitute.as_ref()
1836 }
1837
1838 /// Store the last successful substitute so `:&` / `:&&` can repeat it.
1839 pub fn set_last_substitute(&mut self, cmd: crate::substitute::SubstituteCmd) {
1840 self.vim.last_substitute = Some(cmd);
1841 }
1842
1843 /// Start/end `(row, col)` of the active char-wise Visual selection
1844 /// (inclusive on both ends, positionally ordered). `None` when not
1845 /// in Visual mode.
1846 pub fn char_highlight(&self) -> Option<((usize, usize), (usize, usize))> {
1847 if self.vim_mode() != VimMode::Visual {
1848 return None;
1849 }
1850 let anchor = self.vim.visual_anchor;
1851 let cursor = self.cursor();
1852 let (start, end) = if anchor <= cursor {
1853 (anchor, cursor)
1854 } else {
1855 (cursor, anchor)
1856 };
1857 Some((start, end))
1858 }
1859
1860 /// Top/bottom rows of the active VisualLine selection (inclusive).
1861 /// `None` when we're not in VisualLine mode.
1862 pub fn line_highlight(&self) -> Option<(usize, usize)> {
1863 if self.vim_mode() != VimMode::VisualLine {
1864 return None;
1865 }
1866 let anchor = self.vim.visual_line_anchor;
1867 let cursor = buf_cursor_row(&self.buffer);
1868 Some((anchor.min(cursor), anchor.max(cursor)))
1869 }
1870
1871 pub fn block_highlight(&self) -> Option<(usize, usize, usize, usize)> {
1872 if self.vim_mode() != VimMode::VisualBlock {
1873 return None;
1874 }
1875 let (ar, ac) = self.vim.block_anchor;
1876 let cr = buf_cursor_row(&self.buffer);
1877 let cc = self.vim.block_vcol;
1878 let top = ar.min(cr);
1879 let bot = ar.max(cr);
1880 let left = ac.min(cc);
1881 let right = ac.max(cc);
1882 Some((top, bot, left, right))
1883 }
1884
1885 /// Active selection in `hjkl_buffer::Selection` shape. `None` when
1886 /// not in a Visual mode. Phase 7d-i wiring — the host hands this
1887 /// straight to `BufferView` once render flips off textarea
1888 /// (Phase 7d-ii drops the `paint_*_overlay` calls on the same
1889 /// switch).
1890 pub fn buffer_selection(&self) -> Option<hjkl_buffer::Selection> {
1891 use hjkl_buffer::{Position, Selection};
1892 match self.vim_mode() {
1893 VimMode::Visual => {
1894 let (ar, ac) = self.vim.visual_anchor;
1895 let head = buf_cursor_pos(&self.buffer);
1896 Some(Selection::Char {
1897 anchor: Position::new(ar, ac),
1898 head,
1899 })
1900 }
1901 VimMode::VisualLine => {
1902 let anchor_row = self.vim.visual_line_anchor;
1903 let head_row = buf_cursor_row(&self.buffer);
1904 Some(Selection::Line {
1905 anchor_row,
1906 head_row,
1907 })
1908 }
1909 VimMode::VisualBlock => {
1910 let (ar, ac) = self.vim.block_anchor;
1911 let cr = buf_cursor_row(&self.buffer);
1912 let cc = self.vim.block_vcol;
1913 Some(Selection::Block {
1914 anchor: Position::new(ar, ac),
1915 head: Position::new(cr, cc),
1916 })
1917 }
1918 _ => None,
1919 }
1920 }
1921
1922 /// Force back to normal mode (used when dismissing completions etc.)
1923 pub fn force_normal(&mut self) {
1924 self.vim.force_normal();
1925 }
1926
1927 pub fn content(&self) -> String {
1928 let n = buf_row_count(&self.buffer);
1929 let mut s = String::new();
1930 for r in 0..n {
1931 if r > 0 {
1932 s.push('\n');
1933 }
1934 s.push_str(&crate::types::Query::line(&self.buffer, r as u32));
1935 }
1936 s.push('\n');
1937 s
1938 }
1939
1940 /// Same logical output as [`content`], but returns a cached
1941 /// `Arc<String>` so back-to-back reads within an un-mutated window
1942 /// are ref-count bumps instead of multi-MB joins. The cache is
1943 /// invalidated by every [`mark_content_dirty`] call.
1944 pub fn content_arc(&mut self) -> std::sync::Arc<String> {
1945 if let Some(arc) = &self.cached_content {
1946 return std::sync::Arc::clone(arc);
1947 }
1948 let arc = std::sync::Arc::new(self.content());
1949 self.cached_content = Some(std::sync::Arc::clone(&arc));
1950 arc
1951 }
1952
1953 pub fn set_content(&mut self, text: &str) {
1954 let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1955 while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1956 lines.pop();
1957 }
1958 if lines.is_empty() {
1959 lines.push(String::new());
1960 }
1961 let _ = lines;
1962 crate::types::BufferEdit::replace_all(&mut self.buffer, text);
1963 self.undo_stack.clear();
1964 self.redo_stack.clear();
1965 // Whole-buffer replace supersedes any queued ContentEdits.
1966 self.pending_content_edits.clear();
1967 self.pending_content_reset = true;
1968 self.mark_content_dirty();
1969 }
1970
1971 /// Whole-buffer replace that **preserves the undo history**.
1972 ///
1973 /// Equivalent to [`Editor::set_content`] but pushes the current buffer
1974 /// state onto the undo stack first, so a subsequent `u` walks back to
1975 /// the pre-replacement content. Use this for any operation the user
1976 /// expects to undo as a single step — e.g. external formatter output
1977 /// (`hjkl-mangler`) installed via the async [`crate::app::FormatWorker`].
1978 ///
1979 /// Like `push_undo`, this clears the redo stack (vim semantics: any
1980 /// new edit invalidates redo).
1981 pub fn set_content_undoable(&mut self, text: &str) {
1982 self.push_undo();
1983 let mut lines: Vec<String> = text.lines().map(|l| l.to_string()).collect();
1984 while lines.last().map(|l| l.is_empty()).unwrap_or(false) {
1985 lines.pop();
1986 }
1987 if lines.is_empty() {
1988 lines.push(String::new());
1989 }
1990 let _ = lines;
1991 crate::types::BufferEdit::replace_all(&mut self.buffer, text);
1992 // Whole-buffer replace supersedes any queued ContentEdits.
1993 self.pending_content_edits.clear();
1994 self.pending_content_reset = true;
1995 self.mark_content_dirty();
1996 }
1997
1998 /// Drain the pending change log produced by buffer mutations.
1999 ///
2000 /// Returns a `Vec<EditOp>` covering edits applied since the last
2001 /// call. Empty when no edits ran. Pull-model, complementary to
2002 /// [`Editor::take_content_change`] which gives back the new full
2003 /// content.
2004 ///
2005 /// Mapping coverage:
2006 /// - InsertChar / InsertStr → exact `EditOp` with empty range +
2007 /// replacement.
2008 /// - DeleteRange (`Char` kind) → exact range + empty replacement.
2009 /// - Replace → exact range + new replacement.
2010 /// - DeleteRange (`Line`/`Block`), JoinLines, SplitLines,
2011 /// InsertBlock, DeleteBlockChunks → best-effort placeholder
2012 /// covering the touched range. Hosts wanting per-cell deltas
2013 /// should diff their own `lines()` snapshot.
2014 pub fn take_changes(&mut self) -> Vec<crate::types::Edit> {
2015 std::mem::take(&mut self.change_log)
2016 }
2017
2018 /// Read the engine's current settings as a SPEC
2019 /// [`crate::types::Options`].
2020 ///
2021 /// Bridges between the legacy [`Settings`] (which carries fewer
2022 /// fields than SPEC) and the planned 0.1.0 trait surface. Fields
2023 /// not present in `Settings` fall back to vim defaults (e.g.,
2024 /// `expandtab=false`, `wrapscan=true`, `timeout_len=1000ms`).
2025 /// Once trait extraction lands, this becomes the canonical config
2026 /// reader and `Settings` retires.
2027 pub fn current_options(&self) -> crate::types::Options {
2028 crate::types::Options {
2029 shiftwidth: self.settings.shiftwidth as u32,
2030 tabstop: self.settings.tabstop as u32,
2031 softtabstop: self.settings.softtabstop as u32,
2032 textwidth: self.settings.textwidth as u32,
2033 expandtab: self.settings.expandtab,
2034 ignorecase: self.settings.ignore_case,
2035 smartcase: self.settings.smartcase,
2036 wrapscan: self.settings.wrapscan,
2037 wrap: match self.settings.wrap {
2038 hjkl_buffer::Wrap::None => crate::types::WrapMode::None,
2039 hjkl_buffer::Wrap::Char => crate::types::WrapMode::Char,
2040 hjkl_buffer::Wrap::Word => crate::types::WrapMode::Word,
2041 },
2042 readonly: self.settings.readonly,
2043 autoindent: self.settings.autoindent,
2044 smartindent: self.settings.smartindent,
2045 undo_levels: self.settings.undo_levels,
2046 undo_break_on_motion: self.settings.undo_break_on_motion,
2047 iskeyword: self.settings.iskeyword.clone(),
2048 timeout_len: self.settings.timeout_len,
2049 ..crate::types::Options::default()
2050 }
2051 }
2052
2053 /// Apply a SPEC [`crate::types::Options`] to the engine's settings.
2054 /// Only the fields backed by today's [`Settings`] take effect;
2055 /// remaining options become live once trait extraction wires them
2056 /// through.
2057 pub fn apply_options(&mut self, opts: &crate::types::Options) {
2058 self.settings.shiftwidth = opts.shiftwidth as usize;
2059 self.settings.tabstop = opts.tabstop as usize;
2060 self.settings.softtabstop = opts.softtabstop as usize;
2061 self.settings.textwidth = opts.textwidth as usize;
2062 self.settings.expandtab = opts.expandtab;
2063 self.settings.ignore_case = opts.ignorecase;
2064 self.settings.smartcase = opts.smartcase;
2065 self.settings.wrapscan = opts.wrapscan;
2066 self.settings.wrap = match opts.wrap {
2067 crate::types::WrapMode::None => hjkl_buffer::Wrap::None,
2068 crate::types::WrapMode::Char => hjkl_buffer::Wrap::Char,
2069 crate::types::WrapMode::Word => hjkl_buffer::Wrap::Word,
2070 };
2071 self.settings.readonly = opts.readonly;
2072 self.settings.autoindent = opts.autoindent;
2073 self.settings.smartindent = opts.smartindent;
2074 self.settings.undo_levels = opts.undo_levels;
2075 self.settings.undo_break_on_motion = opts.undo_break_on_motion;
2076 self.set_iskeyword(opts.iskeyword.clone());
2077 self.settings.timeout_len = opts.timeout_len;
2078 self.settings.number = opts.number;
2079 self.settings.relativenumber = opts.relativenumber;
2080 self.settings.numberwidth = opts.numberwidth;
2081 self.settings.cursorline = opts.cursorline;
2082 self.settings.cursorcolumn = opts.cursorcolumn;
2083 self.settings.signcolumn = opts.signcolumn;
2084 self.settings.foldcolumn = opts.foldcolumn;
2085 self.settings.colorcolumn = opts.colorcolumn.clone();
2086 }
2087
2088 /// Active visual selection as a SPEC [`crate::types::Highlight`]
2089 /// with [`crate::types::HighlightKind::Selection`].
2090 ///
2091 /// Returns `None` when the editor isn't in a Visual mode.
2092 /// Visual-line and visual-block selections collapse to the
2093 /// bounding char range of the selection — the SPEC `Selection`
2094 /// kind doesn't carry sub-line info today; hosts that need full
2095 /// line / block geometry continue to read [`buffer_selection`]
2096 /// (the legacy [`hjkl_buffer::Selection`] shape).
2097 pub fn selection_highlight(&self) -> Option<crate::types::Highlight> {
2098 use crate::types::{Highlight, HighlightKind, Pos};
2099 let sel = self.buffer_selection()?;
2100 let (start, end) = match sel {
2101 hjkl_buffer::Selection::Char { anchor, head } => {
2102 let a = (anchor.row, anchor.col);
2103 let h = (head.row, head.col);
2104 if a <= h { (a, h) } else { (h, a) }
2105 }
2106 hjkl_buffer::Selection::Line {
2107 anchor_row,
2108 head_row,
2109 } => {
2110 let (top, bot) = if anchor_row <= head_row {
2111 (anchor_row, head_row)
2112 } else {
2113 (head_row, anchor_row)
2114 };
2115 let last_col = buf_line(&self.buffer, bot).map(|l| l.len()).unwrap_or(0);
2116 ((top, 0), (bot, last_col))
2117 }
2118 hjkl_buffer::Selection::Block { anchor, head } => {
2119 let (top, bot) = if anchor.row <= head.row {
2120 (anchor.row, head.row)
2121 } else {
2122 (head.row, anchor.row)
2123 };
2124 let (left, right) = if anchor.col <= head.col {
2125 (anchor.col, head.col)
2126 } else {
2127 (head.col, anchor.col)
2128 };
2129 ((top, left), (bot, right))
2130 }
2131 };
2132 Some(Highlight {
2133 range: Pos {
2134 line: start.0 as u32,
2135 col: start.1 as u32,
2136 }..Pos {
2137 line: end.0 as u32,
2138 col: end.1 as u32,
2139 },
2140 kind: HighlightKind::Selection,
2141 })
2142 }
2143
2144 /// SPEC-typed highlights for `line`.
2145 ///
2146 /// Two emission modes:
2147 ///
2148 /// - **IncSearch**: the user is typing a `/` or `?` prompt and
2149 /// `Editor::search_prompt` is `Some`. Live-preview matches of
2150 /// the in-flight pattern surface as
2151 /// [`crate::types::HighlightKind::IncSearch`].
2152 /// - **SearchMatch**: the prompt has been committed (or absent)
2153 /// and the buffer's armed pattern is non-empty. Matches surface
2154 /// as [`crate::types::HighlightKind::SearchMatch`].
2155 ///
2156 /// Selection / MatchParen / Syntax(id) variants land once the
2157 /// trait extraction routes the FSM's selection set + the host's
2158 /// syntax pipeline through the [`crate::types::Host`] trait.
2159 ///
2160 /// Returns an empty vec when there is nothing to highlight or
2161 /// `line` is out of bounds.
2162 pub fn highlights_for_line(&mut self, line: u32) -> Vec<crate::types::Highlight> {
2163 use crate::types::{Highlight, HighlightKind, Pos};
2164 let row = line as usize;
2165 if row >= buf_row_count(&self.buffer) {
2166 return Vec::new();
2167 }
2168
2169 // Live preview while the prompt is open beats the committed
2170 // pattern.
2171 if let Some(prompt) = self.search_prompt() {
2172 if prompt.text.is_empty() {
2173 return Vec::new();
2174 }
2175 let translated = crate::search::vim_to_rust_regex(&prompt.text);
2176 let Ok(re) = regex::Regex::new(&translated) else {
2177 return Vec::new();
2178 };
2179 let Some(haystack) = buf_line(&self.buffer, row) else {
2180 return Vec::new();
2181 };
2182 return re
2183 .find_iter(&haystack)
2184 .map(|m| Highlight {
2185 range: Pos {
2186 line,
2187 col: m.start() as u32,
2188 }..Pos {
2189 line,
2190 col: m.end() as u32,
2191 },
2192 kind: HighlightKind::IncSearch,
2193 })
2194 .collect();
2195 }
2196
2197 if self.search_state.pattern.is_none() {
2198 return Vec::new();
2199 }
2200 let dgen = crate::types::Query::dirty_gen(&self.buffer);
2201 crate::search::search_matches(&self.buffer, &mut self.search_state, dgen, row)
2202 .into_iter()
2203 .map(|(start, end)| Highlight {
2204 range: Pos {
2205 line,
2206 col: start as u32,
2207 }..Pos {
2208 line,
2209 col: end as u32,
2210 },
2211 kind: HighlightKind::SearchMatch,
2212 })
2213 .collect()
2214 }
2215
2216 /// Build the engine's [`crate::types::RenderFrame`] for the
2217 /// current state. Hosts call this once per redraw and diff
2218 /// across frames.
2219 ///
2220 /// Coarse today — covers mode + cursor + cursor shape + viewport
2221 /// top + line count. SPEC-target fields (selections, highlights,
2222 /// command line, search prompt, status line) land once trait
2223 /// extraction routes them through `SelectionSet` and the
2224 /// `Highlight` pipeline.
2225 pub fn render_frame(&self) -> crate::types::RenderFrame {
2226 use crate::types::{CursorShape, RenderFrame, SnapshotMode};
2227 let (cursor_row, cursor_col) = self.cursor();
2228 let (mode, shape) = match self.vim_mode() {
2229 crate::VimMode::Normal => (SnapshotMode::Normal, CursorShape::Block),
2230 crate::VimMode::Insert => (SnapshotMode::Insert, CursorShape::Bar),
2231 crate::VimMode::Visual => (SnapshotMode::Visual, CursorShape::Block),
2232 crate::VimMode::VisualLine => (SnapshotMode::VisualLine, CursorShape::Block),
2233 crate::VimMode::VisualBlock => (SnapshotMode::VisualBlock, CursorShape::Block),
2234 };
2235 RenderFrame {
2236 mode,
2237 cursor_row: cursor_row as u32,
2238 cursor_col: cursor_col as u32,
2239 cursor_shape: shape,
2240 viewport_top: self.host.viewport().top_row as u32,
2241 line_count: crate::types::Query::line_count(&self.buffer),
2242 }
2243 }
2244
2245 /// Capture the editor's coarse state into a serde-friendly
2246 /// [`crate::types::EditorSnapshot`].
2247 ///
2248 /// Today's snapshot covers mode, cursor, lines, viewport top.
2249 /// Registers, marks, jump list, undo tree, and full options arrive
2250 /// once phase 5 trait extraction lands the generic
2251 /// `Editor<B: Buffer, H: Host>` constructor — this method's surface
2252 /// stays stable; only the snapshot's internal fields grow.
2253 ///
2254 /// Distinct from the internal `snapshot` used by undo (which
2255 /// returns `(Vec<String>, (usize, usize))`); host-facing
2256 /// persistence goes through this one.
2257 pub fn take_snapshot(&self) -> crate::types::EditorSnapshot {
2258 use crate::types::{EditorSnapshot, SnapshotMode};
2259 let mode = match self.vim_mode() {
2260 crate::VimMode::Normal => SnapshotMode::Normal,
2261 crate::VimMode::Insert => SnapshotMode::Insert,
2262 crate::VimMode::Visual => SnapshotMode::Visual,
2263 crate::VimMode::VisualLine => SnapshotMode::VisualLine,
2264 crate::VimMode::VisualBlock => SnapshotMode::VisualBlock,
2265 };
2266 let cursor = self.cursor();
2267 let cursor = (cursor.0 as u32, cursor.1 as u32);
2268 let lines: Vec<String> = buf_lines_to_vec(&self.buffer);
2269 let viewport_top = self.host.viewport().top_row as u32;
2270 let marks = self
2271 .marks
2272 .iter()
2273 .map(|(c, (r, col))| (*c, (*r as u32, *col as u32)))
2274 .collect();
2275 let global_marks = self
2276 .global_marks
2277 .iter()
2278 .map(|(c, &(bid, r, col))| (*c, (bid, r as u32, col as u32)))
2279 .collect();
2280 EditorSnapshot {
2281 version: EditorSnapshot::VERSION,
2282 mode,
2283 cursor,
2284 lines,
2285 viewport_top,
2286 registers: self.registers.clone(),
2287 marks,
2288 global_marks,
2289 }
2290 }
2291
2292 /// Restore editor state from an [`EditorSnapshot`]. Returns
2293 /// [`crate::EngineError::SnapshotVersion`] if the snapshot's
2294 /// `version` doesn't match [`EditorSnapshot::VERSION`].
2295 ///
2296 /// Mode is best-effort: `SnapshotMode` only round-trips the
2297 /// status-line summary, not the full FSM state. Visual / Insert
2298 /// mode entry happens through synthetic key dispatch when needed.
2299 pub fn restore_snapshot(
2300 &mut self,
2301 snap: crate::types::EditorSnapshot,
2302 ) -> Result<(), crate::EngineError> {
2303 use crate::types::EditorSnapshot;
2304 if snap.version != EditorSnapshot::VERSION {
2305 return Err(crate::EngineError::SnapshotVersion(
2306 snap.version,
2307 EditorSnapshot::VERSION,
2308 ));
2309 }
2310 let text = snap.lines.join("\n");
2311 self.set_content(&text);
2312 self.jump_cursor(snap.cursor.0 as usize, snap.cursor.1 as usize);
2313 self.host.viewport_mut().top_row = snap.viewport_top as usize;
2314 self.registers = snap.registers;
2315 self.marks = snap
2316 .marks
2317 .into_iter()
2318 .map(|(c, (r, col))| (c, (r as usize, col as usize)))
2319 .collect();
2320 self.global_marks = snap
2321 .global_marks
2322 .into_iter()
2323 .map(|(c, (bid, r, col))| (c, (bid, r as usize, col as usize)))
2324 .collect();
2325 Ok(())
2326 }
2327
2328 /// Install `text` as the pending yank buffer so the next `p`/`P` pastes
2329 /// it. Linewise is inferred from a trailing newline, matching how `yy`/`dd`
2330 /// shape their payload.
2331 pub fn seed_yank(&mut self, text: String) {
2332 let linewise = text.ends_with('\n');
2333 self.vim.yank_linewise = linewise;
2334 self.registers.unnamed = crate::registers::Slot { text, linewise };
2335 }
2336
2337 /// Scroll the viewport down by `rows`. The cursor stays on its
2338 /// absolute line (vim convention) unless the scroll would take it
2339 /// off-screen — in that case it's clamped to the first row still
2340 /// visible.
2341 pub fn scroll_down(&mut self, rows: i16) {
2342 self.scroll_viewport(rows);
2343 }
2344
2345 /// Scroll the viewport up by `rows`. Cursor stays unless it would
2346 /// fall off the bottom of the new viewport, then clamp to the
2347 /// bottom-most visible row.
2348 pub fn scroll_up(&mut self, rows: i16) {
2349 self.scroll_viewport(-rows);
2350 }
2351
2352 /// Scroll the viewport right by `cols` columns. Only the horizontal
2353 /// offset (`top_col`) moves — the cursor is NOT adjusted (matches
2354 /// vim's `zl` behaviour for horizontal scroll without wrap).
2355 pub fn scroll_right(&mut self, cols: i16) {
2356 let vp = self.host.viewport_mut();
2357 let cols_i = cols as isize;
2358 let new_top = (vp.top_col as isize + cols_i).max(0) as usize;
2359 vp.top_col = new_top;
2360 }
2361
2362 /// Scroll the viewport left by `cols` columns. Delegates to
2363 /// `scroll_right` with a negated argument so the floor-at-zero
2364 /// clamp is shared.
2365 pub fn scroll_left(&mut self, cols: i16) {
2366 self.scroll_right(-cols);
2367 }
2368
2369 /// Vim's `scrolloff` default — keep the cursor at least this many
2370 /// rows away from the top / bottom edge of the viewport while
2371 /// scrolling. Collapses to `height / 2` for tiny viewports.
2372 const SCROLLOFF: usize = 5;
2373
2374 /// Scroll the viewport so the cursor stays at least `SCROLLOFF`
2375 /// rows from each edge. Replaces the bare
2376 /// `Buffer::ensure_cursor_visible` call at end-of-step so motions
2377 /// don't park the cursor on the very last visible row.
2378 pub fn ensure_cursor_in_scrolloff(&mut self) {
2379 let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2380 if height == 0 {
2381 // 0.0.42 (Patch C-δ.7): viewport math lifted onto engine
2382 // free fns over `B: Query [+ Cursor]` + `&dyn FoldProvider`.
2383 // Disjoint-field borrow split: `self.buffer` (immutable via
2384 // `folds` snapshot + cursor) and `self.host` (mutable
2385 // viewport ref) live on distinct struct fields, so one
2386 // statement satisfies the borrow checker.
2387 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2388 crate::viewport_math::ensure_cursor_visible(
2389 &self.buffer,
2390 &folds,
2391 self.host.viewport_mut(),
2392 );
2393 return;
2394 }
2395 // Cap margin at (height - 1) / 2 so the upper + lower bands
2396 // can't overlap on tiny windows (margin=5 + height=10 would
2397 // otherwise produce contradictory clamp ranges).
2398 let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2399 // Soft-wrap path: scrolloff math runs in *screen rows*, not
2400 // doc rows, since a wrapped doc row spans many visual lines.
2401 if !matches!(self.host.viewport().wrap, hjkl_buffer::Wrap::None) {
2402 self.ensure_scrolloff_wrap(height, margin);
2403 return;
2404 }
2405 let cursor_row = buf_cursor_row(&self.buffer);
2406 let last_row = buf_row_count(&self.buffer).saturating_sub(1);
2407 let v = self.host.viewport_mut();
2408 // Top edge: cursor_row should sit at >= top_row + margin.
2409 if cursor_row < v.top_row + margin {
2410 v.top_row = cursor_row.saturating_sub(margin);
2411 }
2412 // Bottom edge: cursor_row should sit at <= top_row + height - 1 - margin.
2413 let max_bottom = height.saturating_sub(1).saturating_sub(margin);
2414 if cursor_row > v.top_row + max_bottom {
2415 v.top_row = cursor_row.saturating_sub(max_bottom);
2416 }
2417 // Clamp top_row so we never scroll past the buffer's bottom.
2418 let max_top = last_row.saturating_sub(height.saturating_sub(1));
2419 if v.top_row > max_top {
2420 v.top_row = max_top;
2421 }
2422 // Defer to Buffer for column-side scroll (no scrolloff for
2423 // horizontal scrolling — vim default `sidescrolloff = 0`).
2424 let cursor = buf_cursor_pos(&self.buffer);
2425 self.host.viewport_mut().ensure_visible(cursor);
2426 }
2427
2428 /// Soft-wrap-aware scrolloff. Walks `top_row` one visible doc row
2429 /// at a time so the cursor's *screen* row stays inside
2430 /// `[margin, height - 1 - margin]`, then clamps `top_row` so the
2431 /// buffer's bottom never leaves blank rows below it.
2432 fn ensure_scrolloff_wrap(&mut self, height: usize, margin: usize) {
2433 let cursor_row = buf_cursor_row(&self.buffer);
2434 // Step 1 — cursor above viewport: snap top to cursor row,
2435 // then we'll fix up the margin below.
2436 if cursor_row < self.host.viewport().top_row {
2437 let v = self.host.viewport_mut();
2438 v.top_row = cursor_row;
2439 v.top_col = 0;
2440 }
2441 // Step 2 — push top forward until cursor's screen row is
2442 // within the bottom margin (`csr <= height - 1 - margin`).
2443 // 0.0.33 (Patch C-γ): fold-iteration goes through the
2444 // [`crate::types::FoldProvider`] surface via
2445 // [`crate::buffer_impl::BufferFoldProvider`]. 0.0.34 (Patch
2446 // C-δ.1): `cursor_screen_row` / `max_top_for_height` now take
2447 // a `&Viewport` parameter; the host owns the viewport, so the
2448 // disjoint `(self.host, self.buffer)` borrows split cleanly.
2449 let max_csr = height.saturating_sub(1).saturating_sub(margin);
2450 loop {
2451 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2452 let csr =
2453 crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2454 .unwrap_or(0);
2455 if csr <= max_csr {
2456 break;
2457 }
2458 let top = self.host.viewport().top_row;
2459 let row_count = buf_row_count(&self.buffer);
2460 let next = {
2461 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2462 <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::next_visible_row(&folds, top, row_count)
2463 };
2464 let Some(next) = next else {
2465 break;
2466 };
2467 // Don't walk past the cursor's row.
2468 if next > cursor_row {
2469 self.host.viewport_mut().top_row = cursor_row;
2470 break;
2471 }
2472 self.host.viewport_mut().top_row = next;
2473 }
2474 // Step 3 — pull top backward until cursor's screen row is
2475 // past the top margin (`csr >= margin`).
2476 loop {
2477 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2478 let csr =
2479 crate::viewport_math::cursor_screen_row(&self.buffer, &folds, self.host.viewport())
2480 .unwrap_or(0);
2481 if csr >= margin {
2482 break;
2483 }
2484 let top = self.host.viewport().top_row;
2485 let prev = {
2486 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2487 <crate::buffer_impl::BufferFoldProvider<'_> as crate::types::FoldProvider>::prev_visible_row(&folds, top)
2488 };
2489 let Some(prev) = prev else {
2490 break;
2491 };
2492 self.host.viewport_mut().top_row = prev;
2493 }
2494 // Step 4 — clamp top so the buffer's bottom doesn't leave
2495 // blank rows below it. `max_top_for_height` walks segments
2496 // backward from the last row until it accumulates `height`
2497 // screen rows.
2498 let max_top = {
2499 let folds = crate::buffer_impl::BufferFoldProvider::new(&self.buffer);
2500 crate::viewport_math::max_top_for_height(
2501 &self.buffer,
2502 &folds,
2503 self.host.viewport(),
2504 height,
2505 )
2506 };
2507 if self.host.viewport().top_row > max_top {
2508 self.host.viewport_mut().top_row = max_top;
2509 }
2510 self.host.viewport_mut().top_col = 0;
2511 }
2512
2513 fn scroll_viewport(&mut self, delta: i16) {
2514 if delta == 0 {
2515 return;
2516 }
2517 // Bump the host viewport's top within bounds.
2518 let total_rows = buf_row_count(&self.buffer) as isize;
2519 let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2520 let cur_top = self.host.viewport().top_row as isize;
2521 let new_top = (cur_top + delta as isize)
2522 .max(0)
2523 .min((total_rows - 1).max(0)) as usize;
2524 self.host.viewport_mut().top_row = new_top;
2525 // Mirror to textarea so its viewport reads (still consumed by
2526 // a couple of helpers) stay accurate.
2527 let _ = cur_top;
2528 if height == 0 {
2529 return;
2530 }
2531 // Apply scrolloff: keep the cursor at least SCROLLOFF rows
2532 // from the visible viewport edges.
2533 let (cursor_row, cursor_col) = buf_cursor_rc(&self.buffer);
2534 let margin = Self::SCROLLOFF.min(height / 2);
2535 let min_row = new_top + margin;
2536 let max_row = new_top + height.saturating_sub(1).saturating_sub(margin);
2537 let target_row = cursor_row.clamp(min_row, max_row.max(min_row));
2538 if target_row != cursor_row {
2539 let line_len = buf_line(&self.buffer, target_row)
2540 .map(|l| l.chars().count())
2541 .unwrap_or(0);
2542 let target_col = cursor_col.min(line_len.saturating_sub(1));
2543 buf_set_cursor_rc(&mut self.buffer, target_row, target_col);
2544 }
2545 }
2546
2547 pub fn goto_line(&mut self, line: usize) {
2548 let row = line.saturating_sub(1);
2549 let max = buf_row_count(&self.buffer).saturating_sub(1);
2550 let target = row.min(max);
2551 buf_set_cursor_rc(&mut self.buffer, target, 0);
2552 // Vim: `:N` / `+N` jump scrolls the viewport too — without this
2553 // the cursor lands off-screen and the user has to scroll
2554 // manually to see it.
2555 self.ensure_cursor_in_scrolloff();
2556 }
2557
2558 /// Scroll so the cursor row lands at the given viewport position:
2559 /// `Center` → middle row, `Top` → first row, `Bottom` → last row.
2560 /// Cursor stays on its absolute line; only the viewport moves.
2561 pub(super) fn scroll_cursor_to(&mut self, pos: CursorScrollTarget) {
2562 let height = self.viewport_height.load(Ordering::Relaxed) as usize;
2563 if height == 0 {
2564 return;
2565 }
2566 let cur_row = buf_cursor_row(&self.buffer);
2567 let cur_top = self.host.viewport().top_row;
2568 // Scrolloff awareness: `zt` lands the cursor at the top edge
2569 // of the viable area (top + margin), `zb` at the bottom edge
2570 // (top + height - 1 - margin). Match the cap used by
2571 // `ensure_cursor_in_scrolloff` so contradictory bounds are
2572 // impossible on tiny viewports.
2573 let margin = Self::SCROLLOFF.min(height.saturating_sub(1) / 2);
2574 let new_top = match pos {
2575 CursorScrollTarget::Center => cur_row.saturating_sub(height / 2),
2576 CursorScrollTarget::Top => cur_row.saturating_sub(margin),
2577 CursorScrollTarget::Bottom => {
2578 cur_row.saturating_sub(height.saturating_sub(1).saturating_sub(margin))
2579 }
2580 };
2581 if new_top == cur_top {
2582 return;
2583 }
2584 self.host.viewport_mut().top_row = new_top;
2585 }
2586
2587 /// Jump the cursor to the given 1-based line/column, clamped to the document.
2588 pub fn jump_to(&mut self, line: usize, col: usize) {
2589 let r = line.saturating_sub(1);
2590 let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2591 let r = r.min(max_row);
2592 let line_len = buf_line(&self.buffer, r)
2593 .map(|l| l.chars().count())
2594 .unwrap_or(0);
2595 let c = col.saturating_sub(1).min(line_len);
2596 buf_set_cursor_rc(&mut self.buffer, r, c);
2597 }
2598
2599 // ── Host-agnostic doc-coord mouse primitives (Phase 1 of issue #114) ─────
2600 //
2601 // These primitives operate on document (row, col) coordinates that the HOST
2602 // computes from its own layout knowledge (cell geometry for the TUI host,
2603 // pixel geometry for the future GUI host). The engine has no u16 terminal
2604 // assumption here — it just moves the cursor in doc-space.
2605
2606 /// Set the cursor to the given doc-space `(row, col)`, clamped to the
2607 /// document bounds. Hosts use this for programmatic cursor placement and
2608 /// as the building block for the mouse-click path.
2609 ///
2610 /// `col` may equal `line.chars().count()` (Insert-mode "one past end"
2611 /// position); values beyond that are clamped to `char_count`.
2612 pub fn set_cursor_doc(&mut self, row: usize, col: usize) {
2613 let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2614 let r = row.min(max_row);
2615 let line_len = buf_line(&self.buffer, r)
2616 .map(|l| l.chars().count())
2617 .unwrap_or(0);
2618 let c = col.min(line_len);
2619 buf_set_cursor_rc(&mut self.buffer, r, c);
2620 }
2621
2622 /// Handle a left-button click at doc-space `(row, col)`.
2623 ///
2624 /// Exits Visual mode if active, breaks the insert-mode undo group (Vim
2625 /// parity for `undo_break_on_motion`), then moves the cursor. The host
2626 /// performs cell→doc or pixel→doc translation before calling this.
2627 ///
2628 /// Mode-aware EOL clamp (neovim parity): in Normal / Visual modes the
2629 /// cursor lives on chars and never on the implicit `\n` — `col` is
2630 /// capped at `line.chars().count().saturating_sub(1)`. Insert mode
2631 /// allows the one-past-EOL insert position (`col == chars().count()`).
2632 ///
2633 /// Resets `sticky_col` to the clicked column so the next `j`/`k`
2634 /// motion uses the clicked column as the intended visual column
2635 /// (otherwise the cursor would snap back to the keyboard-tracked
2636 /// column on the first vertical motion after a click).
2637 pub fn mouse_click_doc(&mut self, row: usize, col: usize) {
2638 if self.vim.is_visual() {
2639 self.vim.force_normal();
2640 }
2641 // Mouse-position click counts as a motion — break the active
2642 // insert-mode undo group when the toggle is on (vim parity).
2643 crate::vim::break_undo_group_in_insert(self);
2644
2645 let max_row = buf_row_count(&self.buffer).saturating_sub(1);
2646 let r = row.min(max_row);
2647 let line_len = buf_line(&self.buffer, r)
2648 .map(|l| l.chars().count())
2649 .unwrap_or(0);
2650 let cap = if self.vim.current_mode == crate::VimMode::Insert {
2651 line_len
2652 } else {
2653 line_len.saturating_sub(1)
2654 };
2655 let c = col.min(cap);
2656 buf_set_cursor_rc(&mut self.buffer, r, c);
2657 self.sticky_col = Some(c);
2658 }
2659
2660 /// Begin a mouse-drag selection: anchor at the current cursor and enter
2661 /// Visual-char mode. Idempotent if already in Visual-char mode.
2662 pub fn mouse_begin_drag(&mut self) {
2663 if !self.vim.is_visual_char() {
2664 vim::enter_visual_char_bridge(self);
2665 }
2666 }
2667
2668 /// Extend an in-progress mouse drag to doc-space `(row, col)`.
2669 ///
2670 /// Moves the live cursor; the Visual anchor stays where
2671 /// [`Editor::mouse_begin_drag`] set it. Call after the host has
2672 /// translated the drag position to doc coordinates.
2673 pub fn mouse_extend_drag_doc(&mut self, row: usize, col: usize) {
2674 self.set_cursor_doc(row, col);
2675 }
2676
2677 pub fn insert_str(&mut self, text: &str) {
2678 let pos = crate::types::Cursor::cursor(&self.buffer);
2679 crate::types::BufferEdit::insert_at(&mut self.buffer, pos, text);
2680 self.push_buffer_content_to_textarea();
2681 self.mark_content_dirty();
2682 }
2683
2684 pub fn accept_completion(&mut self, completion: &str) {
2685 use crate::types::{BufferEdit, Cursor as CursorTrait, Pos};
2686 let cursor_pos = CursorTrait::cursor(&self.buffer);
2687 let cursor_row = cursor_pos.line as usize;
2688 let cursor_col = cursor_pos.col as usize;
2689 let line = buf_line(&self.buffer, cursor_row).unwrap_or_default();
2690 let chars: Vec<char> = line.chars().collect();
2691 let prefix_len = chars[..cursor_col.min(chars.len())]
2692 .iter()
2693 .rev()
2694 .take_while(|c| c.is_alphanumeric() || **c == '_')
2695 .count();
2696 if prefix_len > 0 {
2697 let start = Pos {
2698 line: cursor_row as u32,
2699 col: (cursor_col - prefix_len) as u32,
2700 };
2701 BufferEdit::delete_range(&mut self.buffer, start..cursor_pos);
2702 }
2703 let cursor = CursorTrait::cursor(&self.buffer);
2704 BufferEdit::insert_at(&mut self.buffer, cursor, completion);
2705 self.push_buffer_content_to_textarea();
2706 self.mark_content_dirty();
2707 }
2708
2709 pub(super) fn snapshot(&self) -> (Vec<String>, (usize, usize)) {
2710 let rc = buf_cursor_rc(&self.buffer);
2711 (buf_lines_to_vec(&self.buffer), rc)
2712 }
2713
2714 /// Walk one step back through the undo history. Equivalent to the
2715 /// user pressing `u` in normal mode. Drains the most recent undo
2716 /// entry and pushes it onto the redo stack.
2717 pub fn undo(&mut self) {
2718 crate::vim::do_undo(self);
2719 }
2720
2721 /// Walk one step forward through the redo history. Equivalent to
2722 /// `<C-r>` in normal mode.
2723 pub fn redo(&mut self) {
2724 crate::vim::do_redo(self);
2725 }
2726
2727 /// Snapshot current buffer state onto the undo stack and clear
2728 /// the redo stack. Bounded by `settings.undo_levels` — older
2729 /// entries pruned. Call before any group of buffer mutations the
2730 /// user might want to undo as a single step.
2731 pub fn push_undo(&mut self) {
2732 let snap = self.snapshot();
2733 self.undo_stack.push(snap);
2734 self.cap_undo();
2735 self.redo_stack.clear();
2736 }
2737
2738 /// Trim the undo stack down to `settings.undo_levels`, dropping
2739 /// the oldest entries. `undo_levels == 0` is treated as
2740 /// "unlimited" (vim's 0-means-no-undo semantics intentionally
2741 /// skipped — guarding with `> 0` is one line shorter than gating
2742 /// the cap path with an explicit zero-check above the call site).
2743 pub(crate) fn cap_undo(&mut self) {
2744 let cap = self.settings.undo_levels as usize;
2745 if cap > 0 && self.undo_stack.len() > cap {
2746 let diff = self.undo_stack.len() - cap;
2747 self.undo_stack.drain(..diff);
2748 }
2749 }
2750
2751 /// Test-only accessor for the undo stack length.
2752 #[doc(hidden)]
2753 pub fn undo_stack_len(&self) -> usize {
2754 self.undo_stack.len()
2755 }
2756
2757 /// Replace the buffer with `lines` joined by `\n` and set the
2758 /// cursor to `cursor`. Used by undo / `:e!` / snapshot restore
2759 /// paths. Marks the editor dirty.
2760 pub fn restore(&mut self, lines: Vec<String>, cursor: (usize, usize)) {
2761 let text = lines.join("\n");
2762 crate::types::BufferEdit::replace_all(&mut self.buffer, &text);
2763 buf_set_cursor_rc(&mut self.buffer, cursor.0, cursor.1);
2764 // Bulk replace — supersedes any queued ContentEdits.
2765 self.pending_content_edits.clear();
2766 self.pending_content_reset = true;
2767 self.mark_content_dirty();
2768 }
2769
2770 /// Returns true if the key was consumed by the editor.
2771 /// Replace the char under the cursor with `ch`, `count` times. Matches
2772 /// vim `r<x>` semantics: cursor ends on the last replaced char, undo
2773 /// snapshot taken once at start. Promoted to public surface in 0.5.5
2774 /// so hjkl-vim's pending-state reducer can dispatch `Replace` without
2775 /// re-entering the FSM.
2776 pub fn replace_char_at(&mut self, ch: char, count: usize) {
2777 vim::replace_char(self, ch, count);
2778 }
2779
2780 /// Apply vim's `f<x>` / `F<x>` / `t<x>` / `T<x>` motion. Moves the cursor
2781 /// to the `count`-th occurrence of `ch` on the current line, respecting
2782 /// `forward` (direction) and `till` (stop one char before target).
2783 /// Records `last_find` so `;` / `,` repeat work.
2784 ///
2785 /// No-op if the target char isn't on the current line within range.
2786 /// Cursor / scroll / sticky-col semantics match `f<x>` via `execute_motion`.
2787 pub fn find_char(&mut self, ch: char, forward: bool, till: bool, count: usize) {
2788 vim::apply_find_char(self, ch, forward, till, count.max(1));
2789 }
2790
2791 /// Apply the g-chord effect for `g<ch>` with a pre-captured `count`.
2792 /// Mirrors the full `handle_after_g` dispatch table — `gg`, `gj`, `gk`,
2793 /// `gv`, `gU` / `gu` / `g~` (→ operator-pending), `gi`, `g*`, `g#`, etc.
2794 ///
2795 /// Promoted to public surface in 0.5.10 so hjkl-vim's
2796 /// `PendingState::AfterG` reducer can dispatch `AfterGChord` without
2797 /// re-entering the engine FSM.
2798 pub fn after_g(&mut self, ch: char, count: usize) {
2799 vim::apply_after_g(self, ch, count);
2800 }
2801
2802 /// Apply the z-chord effect for `z<ch>` with a pre-captured `count`.
2803 /// Mirrors the full `handle_after_z` dispatch table — `zz` / `zt` / `zb`
2804 /// (scroll-cursor), `zo` / `zc` / `za` / `zR` / `zM` / `zE` / `zd`
2805 /// (fold ops), and `zf` (fold-add over visual selection or → op-pending).
2806 ///
2807 /// Promoted to public surface in 0.5.11 so hjkl-vim's
2808 /// `PendingState::AfterZ` reducer can dispatch `AfterZChord` without
2809 /// re-entering the engine FSM.
2810 pub fn after_z(&mut self, ch: char, count: usize) {
2811 vim::apply_after_z(self, ch, count);
2812 }
2813
2814 /// Apply an operator over a single-key motion. `op` is the engine `Operator`
2815 /// and `motion_key` is the raw character (e.g. `'w'`, `'$'`, `'G'`). The
2816 /// engine resolves the char to a [`vim::Motion`] via `parse_motion`, applies
2817 /// the vim quirks (`cw` → `ce`, `cW` → `cE`, `FindRepeat` → stored find),
2818 /// then calls `apply_op_with_motion`. `total_count` is already the product of
2819 /// the prefix count and any inner count accumulated by the reducer.
2820 ///
2821 /// No-op when `motion_key` does not map to a known motion (engine silently
2822 /// cancels the operator, matching vim's behaviour on unknown motions).
2823 ///
2824 /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2825 /// `PendingState::AfterOp` reducer can dispatch `ApplyOpMotion` without
2826 /// re-entering the engine FSM.
2827 pub fn apply_op_motion(
2828 &mut self,
2829 op: crate::vim::Operator,
2830 motion_key: char,
2831 total_count: usize,
2832 ) {
2833 vim::apply_op_motion_key(self, op, motion_key, total_count);
2834 }
2835
2836 /// Apply a doubled-letter line op (`dd` / `yy` / `cc` / `>>` / `<<`).
2837 /// `total_count` is the product of prefix count and inner count.
2838 ///
2839 /// Promoted to the public surface in 0.5.12 so the hjkl-vim
2840 /// `PendingState::AfterOp` reducer can dispatch `ApplyOpDouble` without
2841 /// re-entering the engine FSM.
2842 pub fn apply_op_double(&mut self, op: crate::vim::Operator, total_count: usize) {
2843 vim::apply_op_double(self, op, total_count);
2844 }
2845
2846 /// Apply an operator over a find motion (`df<x>` / `dF<x>` / `dt<x>` /
2847 /// `dT<x>`). Builds `Motion::Find { ch, forward, till }`, applies it via
2848 /// `apply_op_with_motion`, records `last_find` for `;` / `,` repeat, and
2849 /// updates `last_change` when `op` is Change (for dot-repeat).
2850 ///
2851 /// `total_count` is the product of prefix count and any inner count
2852 /// accumulated by the reducer — already folded at transition time.
2853 ///
2854 /// Promoted to the public surface in 0.5.14 so the hjkl-vim
2855 /// `PendingState::OpFind` reducer can dispatch `ApplyOpFind` without
2856 /// re-entering the engine FSM. `handle_op_find_target` (used by the
2857 /// chord-init op path) delegates here to avoid logic duplication.
2858 pub fn apply_op_find(
2859 &mut self,
2860 op: crate::vim::Operator,
2861 ch: char,
2862 forward: bool,
2863 till: bool,
2864 total_count: usize,
2865 ) {
2866 vim::apply_op_find_motion(self, op, ch, forward, till, total_count);
2867 }
2868
2869 /// Apply an operator over a text-object range (`diw` / `daw` / `di"` etc.).
2870 /// Maps `ch` to a `TextObject` per the standard vim table, calls
2871 /// `apply_op_with_text_object`, and records `last_change` when `op` is
2872 /// Change (dot-repeat). Unknown `ch` values are silently ignored (no-op),
2873 /// matching the engine FSM's behaviour on unrecognised text-object chars.
2874 ///
2875 /// `total_count` is accepted for API symmetry with `apply_op_motion` /
2876 /// `apply_op_find` but is currently unused — text objects don't repeat in
2877 /// vim's current grammar. Kept for future-proofing.
2878 ///
2879 /// Promoted to the public surface in 0.5.15 so the hjkl-vim
2880 /// `PendingState::OpTextObj` reducer can dispatch `ApplyOpTextObj` without
2881 /// re-entering the engine FSM. `handle_text_object` (chord-init op path)
2882 /// delegates to the shared `apply_op_text_obj_inner` helper to avoid logic
2883 /// duplication.
2884 pub fn apply_op_text_obj(
2885 &mut self,
2886 op: crate::vim::Operator,
2887 ch: char,
2888 inner: bool,
2889 total_count: usize,
2890 ) {
2891 vim::apply_op_text_obj_inner(self, op, ch, inner, total_count);
2892 }
2893
2894 /// Apply an operator over a g-chord motion or case-op linewise form
2895 /// (`dgg` / `dge` / `dgE` / `dgj` / `dgk` / `gUgU` etc.).
2896 ///
2897 /// - If `op` is Uppercase/Lowercase/ToggleCase and `ch` matches the op's
2898 /// letter (`U`/`u`/`~`), executes the line op (linewise form).
2899 /// - Otherwise maps `ch` to a motion:
2900 /// - `'g'` → `Motion::FileTop` (gg)
2901 /// - `'e'` → `Motion::WordEndBack` (ge)
2902 /// - `'E'` → `Motion::BigWordEndBack` (gE)
2903 /// - `'j'` → `Motion::ScreenDown` (gj)
2904 /// - `'k'` → `Motion::ScreenUp` (gk)
2905 /// - unknown → no-op (silently ignored, matching engine FSM behaviour)
2906 /// - Updates `last_change` for dot-repeat when `op` is a change operator.
2907 ///
2908 /// `total_count` is the already-folded product of prefix and inner counts.
2909 ///
2910 /// Promoted to the public surface in 0.5.16 so the hjkl-vim
2911 /// `PendingState::OpG` reducer can dispatch `ApplyOpG` without
2912 /// re-entering the engine FSM. `handle_op_after_g` (chord-init op path)
2913 /// delegates to the shared `apply_op_g_inner` helper to avoid logic
2914 /// duplication.
2915 pub fn apply_op_g(&mut self, op: crate::vim::Operator, ch: char, total_count: usize) {
2916 vim::apply_op_g_inner(self, op, ch, total_count);
2917 }
2918
2919 // ─── Range-query helpers for partial-format dispatch (#119) ─────────────
2920
2921 /// Dry-run `motion_key` and return `(min_row, max_row)` between the cursor
2922 /// row and the motion's target row. Used by the app layer to compute the
2923 /// [`hjkl_mangler::RangeSpec`] for `=<motion>` before submitting the async
2924 /// format job.
2925 ///
2926 /// Returns `None` when `motion_key` does not map to a known motion (same
2927 /// condition that makes `apply_op_motion` a no-op).
2928 ///
2929 /// The cursor is restored to its original position after the probe —
2930 /// the buffer content is not touched.
2931 pub fn range_for_op_motion(
2932 &mut self,
2933 motion_key: char,
2934 total_count: usize,
2935 ) -> Option<(usize, usize)> {
2936 let start = self.cursor();
2937 // Reuse the same logic as apply_op_motion_key but only read the
2938 // target row — we parse the motion, apply it to move the cursor,
2939 // then immediately restore.
2940 let input = crate::input::Input {
2941 key: crate::input::Key::Char(motion_key),
2942 ctrl: false,
2943 alt: false,
2944 shift: false,
2945 };
2946 let motion = vim::parse_motion(&input)?;
2947 // Resolve FindRepeat and cw/cW quirks just like apply_op_motion_key.
2948 let motion = match motion {
2949 vim::Motion::FindRepeat { reverse } => match self.vim.last_find {
2950 Some((ch, forward, till)) => vim::Motion::Find {
2951 ch,
2952 forward: if reverse { !forward } else { forward },
2953 till,
2954 },
2955 None => return None,
2956 },
2957 m => m,
2958 };
2959 vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
2960 let end = self.cursor();
2961 // Restore cursor.
2962 buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
2963 let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2964 Some((r0, r1))
2965 }
2966
2967 /// Dry-run a `g`-prefixed motion and return `(min_row, max_row)`. Used for
2968 /// `=gg` / `=gj` etc. Returns `None` for unknown `ch` values or case-op
2969 /// linewise forms that don't map to a row range.
2970 ///
2971 /// The cursor is restored after the probe.
2972 pub fn range_for_op_g(&mut self, ch: char, total_count: usize) -> Option<(usize, usize)> {
2973 let start = self.cursor();
2974 let motion = match ch {
2975 'g' => vim::Motion::FileTop,
2976 'e' => vim::Motion::WordEndBack,
2977 'E' => vim::Motion::BigWordEndBack,
2978 'j' => vim::Motion::ScreenDown,
2979 'k' => vim::Motion::ScreenUp,
2980 _ => return None,
2981 };
2982 vim::apply_motion_cursor_ctx(self, &motion, total_count, true);
2983 let end = self.cursor();
2984 buf_set_cursor_rc(&mut self.buffer, start.0, start.1);
2985 let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
2986 Some((r0, r1))
2987 }
2988
2989 /// Dry-run a text-object lookup and return `(min_row, max_row)` for the
2990 /// matched region. Returns `None` when `ch` is not a known text-object
2991 /// kind or the text object could not be resolved (e.g. no enclosing bracket).
2992 ///
2993 /// The buffer is not mutated.
2994 pub fn range_for_op_text_obj(
2995 &self,
2996 ch: char,
2997 inner: bool,
2998 _total_count: usize,
2999 ) -> Option<(usize, usize)> {
3000 let obj = match ch {
3001 'w' => vim::TextObject::Word { big: false },
3002 'W' => vim::TextObject::Word { big: true },
3003 '"' | '\'' | '`' => vim::TextObject::Quote(ch),
3004 '(' | ')' | 'b' => vim::TextObject::Bracket('('),
3005 '[' | ']' => vim::TextObject::Bracket('['),
3006 '{' | '}' | 'B' => vim::TextObject::Bracket('{'),
3007 '<' | '>' => vim::TextObject::Bracket('<'),
3008 'p' => vim::TextObject::Paragraph,
3009 't' => vim::TextObject::XmlTag,
3010 's' => vim::TextObject::Sentence,
3011 _ => return None,
3012 };
3013 let (start, end, _kind) = vim::text_object_range(self, obj, inner)?;
3014 let (r0, r1) = (start.0.min(end.0), start.0.max(end.0));
3015 Some((r0, r1))
3016 }
3017
3018 // ─── Phase 4a: pub range-mutation primitives (hjkl#70) ──────────────────
3019 //
3020 // These do not consume input — the caller (hjkl-vim's visual-mode operator
3021 // path, chunk 4e) has already resolved the range from the visual selection
3022 // before calling in. Normal-mode op dispatch continues to use
3023 // `apply_op_motion` / `apply_op_double` / `apply_op_find` / `apply_op_text_obj`.
3024
3025 /// Delete the region `[start, end)` and stash the removed text in
3026 /// `register`. `'"'` selects the unnamed register (vim default); `'a'`–`'z'`
3027 /// select named registers.
3028 ///
3029 /// Pure range-mutation primitive — does not consume input. Called by
3030 /// hjkl-vim's visual-mode operator path which has already resolved the range
3031 /// from the visual selection.
3032 ///
3033 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3034 /// grammar migration (kryptic-sh/hjkl#70).
3035 pub fn delete_range(
3036 &mut self,
3037 start: (usize, usize),
3038 end: (usize, usize),
3039 kind: crate::vim::RangeKind,
3040 register: char,
3041 ) {
3042 vim::delete_range_bridge(self, start, end, kind, register);
3043 }
3044
3045 /// Yank (copy) the region `[start, end)` into `register` without mutating
3046 /// the buffer. `'"'` selects the unnamed register; `'0'` the yank-only
3047 /// register; `'a'`–`'z'` select named registers.
3048 ///
3049 /// Pure range-mutation primitive — does not consume input. Called by
3050 /// hjkl-vim's visual-mode operator path which has already resolved the range
3051 /// from the visual selection.
3052 ///
3053 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3054 /// grammar migration (kryptic-sh/hjkl#70).
3055 pub fn yank_range(
3056 &mut self,
3057 start: (usize, usize),
3058 end: (usize, usize),
3059 kind: crate::vim::RangeKind,
3060 register: char,
3061 ) {
3062 vim::yank_range_bridge(self, start, end, kind, register);
3063 }
3064
3065 /// Delete the region `[start, end)` and transition to Insert mode (vim `c`
3066 /// operator). The deleted text is stashed in `register`. On return the
3067 /// editor is in Insert mode; the caller must not issue further normal-mode
3068 /// ops until the insert session ends.
3069 ///
3070 /// Pure range-mutation primitive — does not consume input. Called by
3071 /// hjkl-vim's visual-mode operator path which has already resolved the range
3072 /// from the visual selection.
3073 ///
3074 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3075 /// grammar migration (kryptic-sh/hjkl#70).
3076 pub fn change_range(
3077 &mut self,
3078 start: (usize, usize),
3079 end: (usize, usize),
3080 kind: crate::vim::RangeKind,
3081 register: char,
3082 ) {
3083 vim::change_range_bridge(self, start, end, kind, register);
3084 }
3085
3086 /// Indent (`count > 0`) or outdent (`count < 0`) the row span
3087 /// `[start.0, end.0]`. Column components are ignored — indent is always
3088 /// linewise. `shiftwidth` overrides the editor's configured shiftwidth for
3089 /// this call; pass `0` to use the current editor setting. `count == 0` is a
3090 /// no-op.
3091 ///
3092 /// Pure range-mutation primitive — does not consume input. Called by
3093 /// hjkl-vim's visual-mode operator path which has already resolved the range
3094 /// from the visual selection.
3095 ///
3096 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3097 /// grammar migration (kryptic-sh/hjkl#70).
3098 pub fn indent_range(
3099 &mut self,
3100 start: (usize, usize),
3101 end: (usize, usize),
3102 count: i32,
3103 shiftwidth: u32,
3104 ) {
3105 vim::indent_range_bridge(self, start, end, count, shiftwidth);
3106 }
3107
3108 /// Apply a case transformation (`Operator::Uppercase` /
3109 /// `Operator::Lowercase` / `Operator::ToggleCase`) to the region
3110 /// `[start, end)`. Other `Operator` variants are silently ignored (no-op).
3111 /// Yanks registers are left untouched — vim's case operators do not write
3112 /// to registers.
3113 ///
3114 /// Pure range-mutation primitive — does not consume input. Called by
3115 /// hjkl-vim's visual-mode operator path which has already resolved the range
3116 /// from the visual selection.
3117 ///
3118 /// Promoted to the public surface in 0.6.7 for Phase 4 visual-mode op
3119 /// grammar migration (kryptic-sh/hjkl#70).
3120 pub fn case_range(
3121 &mut self,
3122 start: (usize, usize),
3123 end: (usize, usize),
3124 kind: crate::vim::RangeKind,
3125 op: crate::vim::Operator,
3126 ) {
3127 vim::case_range_bridge(self, start, end, kind, op);
3128 }
3129
3130 // ─── Phase 4e: pub block-shape range-mutation primitives (hjkl#70) ──────
3131 //
3132 // Rectangular VisualBlock operations. `top_row`/`bot_row` are inclusive
3133 // line indices; `left_col`/`right_col` are inclusive char-column bounds.
3134 // Ragged-edge handling (short lines not reaching `right_col`) matches the
3135 // engine FSM's `apply_block_operator` path — short lines lose only the
3136 // chars that exist.
3137 //
3138 // `register` is the target register; `'"'` selects the unnamed register.
3139
3140 /// Delete a rectangular VisualBlock selection. `top_row` / `bot_row` are
3141 /// inclusive line bounds; `left_col` / `right_col` are inclusive column
3142 /// bounds at the visual (display) column level. Ragged-edge handling
3143 /// matches engine FSM's VisualBlock op behavior — short lines that don't
3144 /// reach `right_col` lose only the chars that exist.
3145 ///
3146 /// `register` honors the user's pending register selection.
3147 ///
3148 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3149 pub fn delete_block(
3150 &mut self,
3151 top_row: usize,
3152 bot_row: usize,
3153 left_col: usize,
3154 right_col: usize,
3155 register: char,
3156 ) {
3157 vim::delete_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3158 }
3159
3160 /// Yank a rectangular VisualBlock selection into `register` without
3161 /// mutating the buffer. `'"'` selects the unnamed register.
3162 ///
3163 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3164 pub fn yank_block(
3165 &mut self,
3166 top_row: usize,
3167 bot_row: usize,
3168 left_col: usize,
3169 right_col: usize,
3170 register: char,
3171 ) {
3172 vim::yank_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3173 }
3174
3175 /// Delete a rectangular VisualBlock selection and enter Insert mode (`c`
3176 /// operator). The deleted text is stashed in `register`. Mode is Insert
3177 /// on return; the caller must not issue further normal-mode ops until the
3178 /// insert session ends.
3179 ///
3180 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3181 pub fn change_block(
3182 &mut self,
3183 top_row: usize,
3184 bot_row: usize,
3185 left_col: usize,
3186 right_col: usize,
3187 register: char,
3188 ) {
3189 vim::change_block_bridge(self, top_row, bot_row, left_col, right_col, register);
3190 }
3191
3192 /// Indent (`count > 0`) or outdent (`count < 0`) rows `top_row..=bot_row`.
3193 /// Column bounds are ignored — vim's block indent is always linewise.
3194 /// `count == 0` is a no-op.
3195 ///
3196 /// Promoted in 0.6.X for Phase 4e block-op grammar migration.
3197 pub fn indent_block(
3198 &mut self,
3199 top_row: usize,
3200 bot_row: usize,
3201 _left_col: usize,
3202 _right_col: usize,
3203 count: i32,
3204 ) {
3205 vim::indent_block_bridge(self, top_row, bot_row, count);
3206 }
3207
3208 /// Auto-indent (v1 dumb shiftwidth) the row span `[start.0, end.0]`.
3209 /// Column components are ignored — auto-indent is always linewise.
3210 ///
3211 /// The algorithm is a naive bracket-depth counter: it scans the buffer from
3212 /// row 0 to compute the correct depth at `start.0`, then for each line in
3213 /// the target range strips existing leading whitespace and prepends
3214 /// `depth × indent_unit` where `indent_unit` is `"\t"` when `expandtab`
3215 /// is `false`, or `" " × shiftwidth` when `expandtab` is `true`. Lines
3216 /// whose first non-whitespace character is a close bracket (`}`, `)`, `]`)
3217 /// get one fewer indent level. Empty / whitespace-only lines are cleared.
3218 ///
3219 /// After the operation the cursor lands on the first non-whitespace
3220 /// character of `start_row` (vim parity for `==`).
3221 ///
3222 /// **v1 limitation**: the bracket scan does not detect brackets inside
3223 /// string literals or comments. Code such as `let s = "{";` will increment
3224 /// the depth counter even though the brace is not a structural opener.
3225 /// Tree-sitter / LSP indentation is deferred to a follow-up.
3226 pub fn auto_indent_range(&mut self, start: (usize, usize), end: (usize, usize)) {
3227 vim::auto_indent_range_bridge(self, start, end);
3228 }
3229
3230 /// Drain the row range set by the most recent auto-indent operation.
3231 ///
3232 /// Returns `Some((top_row, bot_row))` (inclusive) on the first call after
3233 /// an `=` / `==` / `=G` / Visual-`=` operator, then clears the stored
3234 /// value so a subsequent call returns `None`. The host (e.g. `apps/hjkl`)
3235 /// uses this to arm a brief visual flash over the reindented rows.
3236 pub fn take_last_indent_range(&mut self) -> Option<(usize, usize)> {
3237 self.last_indent_range.take()
3238 }
3239
3240 /// Filter rows `top_row..=bot_row` through an external shell command.
3241 ///
3242 /// Spawns `sh -c "<command>"` (or `cmd /C "<command>"` on Windows), pipes
3243 /// the selected lines (joined by `\n`) to stdin, and waits up to
3244 /// `timeout_secs` seconds (default 10) for the process to finish.
3245 ///
3246 /// On success: the rows are replaced with stdout. No trailing-newline trim.
3247 /// On non-zero exit, spawn failure, or timeout: returns `Err(stderr_or_msg)`
3248 /// without mutating the buffer.
3249 ///
3250 /// `top_row` and `bot_row` are clamped to the buffer's valid row range.
3251 pub fn filter_range(
3252 &mut self,
3253 top_row: usize,
3254 bot_row: usize,
3255 command: &str,
3256 timeout_secs: Option<u64>,
3257 ) -> Result<(), String> {
3258 use std::io::Write;
3259 use std::process::{Command, Stdio};
3260 use std::thread;
3261 use std::time::Instant;
3262
3263 let timeout = std::time::Duration::from_secs(timeout_secs.unwrap_or(10));
3264 let lines = self.buffer().lines();
3265 let line_count = lines.len();
3266 let top = top_row.min(line_count.saturating_sub(1));
3267 let bot = bot_row.min(line_count.saturating_sub(1));
3268 let (top, bot) = (top.min(bot), top.max(bot));
3269 let input_text = lines[top..=bot].join("\n");
3270
3271 tracing::debug!(
3272 top_row = top,
3273 bot_row = bot,
3274 command = command,
3275 "filter_range: spawning shell command"
3276 );
3277
3278 #[cfg(not(windows))]
3279 let mut child = Command::new("sh")
3280 .args(["-c", command])
3281 .stdin(Stdio::piped())
3282 .stdout(Stdio::piped())
3283 .stderr(Stdio::piped())
3284 .spawn()
3285 .map_err(|e| format!("spawn failed: {e}"))?;
3286
3287 #[cfg(windows)]
3288 let mut child = Command::new("cmd")
3289 .args(["/C", command])
3290 .stdin(Stdio::piped())
3291 .stdout(Stdio::piped())
3292 .stderr(Stdio::piped())
3293 .spawn()
3294 .map_err(|e| format!("spawn failed: {e}"))?;
3295
3296 // Write stdin on a thread to avoid deadlock when output > pipe buffer.
3297 let mut stdin = child.stdin.take().ok_or("no stdin handle")?;
3298 let input_bytes = input_text.into_bytes();
3299 thread::spawn(move || {
3300 let _ = stdin.write_all(&input_bytes);
3301 // stdin drops here, signalling EOF to the child.
3302 });
3303
3304 // Drain stdout/stderr on separate threads so the child's pipes don't
3305 // fill and deadlock the child. Keep `child` here so we can kill it on
3306 // timeout.
3307 let mut stdout_pipe = child.stdout.take().ok_or("no stdout handle")?;
3308 let mut stderr_pipe = child.stderr.take().ok_or("no stderr handle")?;
3309 let stdout_thread = thread::spawn(move || {
3310 let mut buf = Vec::new();
3311 let _ = std::io::Read::read_to_end(&mut stdout_pipe, &mut buf);
3312 buf
3313 });
3314 let stderr_thread = thread::spawn(move || {
3315 let mut buf = Vec::new();
3316 let _ = std::io::Read::read_to_end(&mut stderr_pipe, &mut buf);
3317 buf
3318 });
3319
3320 // Poll try_wait until exit or timeout. On timeout: SIGKILL the child
3321 // (std Child::kill sends SIGKILL on Unix / TerminateProcess on Windows).
3322 // A proper TERM→KILL escalation would need nix/libc; skip for v1.
3323 let start = Instant::now();
3324 let status = loop {
3325 match child.try_wait() {
3326 Ok(Some(status)) => break status,
3327 Ok(None) => {
3328 if start.elapsed() >= timeout {
3329 tracing::debug!(command, "filter_range: timeout — killing child");
3330 let _ = child.kill();
3331 let _ = child.wait(); // reap so the OS can free resources
3332 return Err(format!("command timed out after {}s", timeout.as_secs()));
3333 }
3334 thread::sleep(std::time::Duration::from_millis(20));
3335 }
3336 Err(e) => return Err(format!("wait failed: {e}")),
3337 }
3338 };
3339
3340 let stdout_bytes = stdout_thread.join().unwrap_or_default();
3341 let stderr_bytes = stderr_thread.join().unwrap_or_default();
3342
3343 if !status.success() {
3344 let stderr = String::from_utf8_lossy(&stderr_bytes).into_owned();
3345 tracing::debug!(
3346 command,
3347 exit_code = ?status.code(),
3348 "filter_range: command exited with non-zero status"
3349 );
3350 return Err(if stderr.is_empty() {
3351 format!("command exited with status {}", status.code().unwrap_or(-1))
3352 } else {
3353 stderr
3354 });
3355 }
3356
3357 let stdout = String::from_utf8_lossy(&stdout_bytes).into_owned();
3358 tracing::debug!(
3359 command,
3360 stdout_bytes = stdout_bytes.len(),
3361 "filter_range: command succeeded, replacing rows"
3362 );
3363
3364 // Replace the row range with the stdout lines.
3365 let mut all_lines = lines;
3366 let new_lines: Vec<String> = stdout.lines().map(|l| l.to_owned()).collect();
3367 // If stdout ended with a newline, stdout.lines() drops the trailing empty
3368 // entry — this preserves vim's "no trailing-newline trim" spec because
3369 // a trailing '\n' from the command means the last replacement line is the
3370 // line BEFORE the newline, not an empty line after it.
3371 let after = all_lines.split_off(bot + 1);
3372 all_lines.truncate(top);
3373 all_lines.extend(new_lines);
3374 all_lines.extend(after);
3375
3376 self.push_undo();
3377 self.restore(all_lines, (top, 0));
3378 // Leave mode as Normal after a successful filter operation (vim parity).
3379 self.force_normal();
3380
3381 Ok(())
3382 }
3383
3384 // ─── Phase 4b: pub text-object resolution (hjkl#70) ─────────────────────
3385 //
3386 // Pure functions — no cursor mutation, no mode change, no register write.
3387 // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3388 // the existing `word_text_object` private resolver in vim.rs.
3389 //
3390 // Called by hjkl-vim's `OpTextObj` reducer (chunk 4e) to resolve the range
3391 // before invoking a range-mutation primitive (`delete_range`, etc.).
3392 //
3393 // Return value: `Some((start, end))` where both positions are `(row, col)`
3394 // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3395 // on), matching the convention used by `delete_range` / `yank_range` / etc.
3396 // Returns `None` when the cursor is on an empty line or the resolver cannot
3397 // find a word boundary.
3398
3399 /// Resolve the range of `iw` (inner word) at the current cursor position.
3400 ///
3401 /// An inner word is the contiguous run of keyword characters (or punctuation
3402 /// characters if the cursor is on punctuation) under the cursor, without any
3403 /// surrounding whitespace. Whitespace-only positions return `None`.
3404 ///
3405 /// Pure function — does not move the cursor or change any editor state.
3406 /// Called by hjkl-vim's `OpTextObj` reducer to resolve the range before
3407 /// invoking a range-mutation primitive (`delete_range`, etc.).
3408 ///
3409 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3410 /// migration (kryptic-sh/hjkl#70).
3411 pub fn text_object_inner_word(&self) -> Option<((usize, usize), (usize, usize))> {
3412 vim::text_object_inner_word_bridge(self)
3413 }
3414
3415 /// Resolve the range of `aw` (around word) at the current cursor position.
3416 ///
3417 /// Like `iw` but extends the range to include trailing whitespace after the
3418 /// word. If no trailing whitespace exists, leading whitespace before the word
3419 /// is absorbed instead (vim `:help text-objects` behaviour).
3420 ///
3421 /// Pure function — does not move the cursor or change any editor state.
3422 ///
3423 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3424 /// migration (kryptic-sh/hjkl#70).
3425 pub fn text_object_around_word(&self) -> Option<((usize, usize), (usize, usize))> {
3426 vim::text_object_around_word_bridge(self)
3427 }
3428
3429 /// Resolve the range of `iW` (inner WORD) at the current cursor position.
3430 ///
3431 /// A WORD is any contiguous run of non-whitespace characters — punctuation
3432 /// is not treated as a word boundary. Returns the span of the WORD under the
3433 /// cursor, without surrounding whitespace.
3434 ///
3435 /// Pure function — does not move the cursor or change any editor state.
3436 ///
3437 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3438 /// migration (kryptic-sh/hjkl#70).
3439 pub fn text_object_inner_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3440 vim::text_object_inner_big_word_bridge(self)
3441 }
3442
3443 /// Resolve the range of `aW` (around WORD) at the current cursor position.
3444 ///
3445 /// Like `iW` but extends the range to include trailing whitespace after the
3446 /// WORD. If no trailing whitespace exists, leading whitespace before the WORD
3447 /// is absorbed instead.
3448 ///
3449 /// Pure function — does not move the cursor or change any editor state.
3450 ///
3451 /// Promoted to the public surface in 0.6.X for Phase 4b text-object grammar
3452 /// migration (kryptic-sh/hjkl#70).
3453 pub fn text_object_around_big_word(&self) -> Option<((usize, usize), (usize, usize))> {
3454 vim::text_object_around_big_word_bridge(self)
3455 }
3456
3457 // ─── Phase 4c: pub text-object resolution — quote + bracket (hjkl#70) ───
3458 //
3459 // Pure functions — no cursor mutation, no mode change, no register write.
3460 // Each method delegates to `vim::text_object_*_bridge`, which in turn calls
3461 // the existing private resolvers (`quote_text_object`, `bracket_text_object`)
3462 // in vim.rs.
3463 //
3464 // Quote methods take the quote char itself (`'"'`, `'\''`, `` '`' ``).
3465 // Bracket methods take the OPEN bracket char (`'('`, `'{'`, `'['`, `'<'`);
3466 // close-bracket variants (`)`, `}`, `]`, `>`) are NOT accepted here — the
3467 // hjkl-vim grammar layer normalises close→open before calling these methods.
3468 //
3469 // Return value: `Some((start, end))` where both positions are `(row, col)`
3470 // byte-column pairs and `end` is *exclusive* (one past the last byte to act
3471 // on), matching the convention used by `delete_range` / `yank_range` / etc.
3472 // `bracket_text_object` internally distinguishes Linewise vs Exclusive
3473 // ranges for multi-line pairs; that tag is stripped here — callers receive
3474 // the same flat shape as all other text-object resolvers.
3475
3476 /// Resolve the range of `i<quote>` (inner quote) at the cursor position.
3477 ///
3478 /// `quote` is one of `'"'`, `'\''`, or `` '`' ``. Returns `None` when the
3479 /// cursor's line contains fewer than two occurrences of `quote`, or when no
3480 /// matching pair can be found around or ahead of the cursor.
3481 ///
3482 /// Inner range excludes the quote characters themselves.
3483 ///
3484 /// Pure function — no cursor mutation.
3485 ///
3486 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3487 /// migration (kryptic-sh/hjkl#70).
3488 pub fn text_object_inner_quote(&self, quote: char) -> Option<((usize, usize), (usize, usize))> {
3489 vim::text_object_inner_quote_bridge(self, quote)
3490 }
3491
3492 /// Resolve the range of `a<quote>` (around quote) at the cursor position.
3493 ///
3494 /// Like `i<quote>` but includes the quote characters themselves plus
3495 /// surrounding whitespace on one side: trailing whitespace after the closing
3496 /// quote if any exists; otherwise leading whitespace before the opening
3497 /// quote. This matches vim `:help text-objects` behaviour.
3498 ///
3499 /// Pure function — no cursor mutation.
3500 ///
3501 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3502 /// migration (kryptic-sh/hjkl#70).
3503 pub fn text_object_around_quote(
3504 &self,
3505 quote: char,
3506 ) -> Option<((usize, usize), (usize, usize))> {
3507 vim::text_object_around_quote_bridge(self, quote)
3508 }
3509
3510 /// Resolve the range of `i<bracket>` (inner bracket pair) at the cursor.
3511 ///
3512 /// `open` must be one of `'('`, `'{'`, `'['`, `'<'` — the corresponding
3513 /// close bracket is derived automatically. Close-bracket chars (`)`, `}`,
3514 /// `]`, `>`) are **not** accepted; hjkl-vim normalises close→open before
3515 /// calling this method. Returns `None` when no enclosing pair is found.
3516 ///
3517 /// The cursor may be anywhere inside the pair or on a bracket character
3518 /// itself. When not inside any pair the resolver falls back to a forward
3519 /// scan (targets.vim-style: `ci(` works when the cursor is before `(`).
3520 ///
3521 /// Inner range excludes the bracket characters. Multi-line pairs are
3522 /// supported; the returned range spans the full content between the
3523 /// brackets.
3524 ///
3525 /// Pure function — no cursor mutation.
3526 ///
3527 /// `ib` / `iB` aliases live in the hjkl-vim grammar layer and are not
3528 /// handled here.
3529 ///
3530 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3531 /// migration (kryptic-sh/hjkl#70).
3532 pub fn text_object_inner_bracket(
3533 &self,
3534 open: char,
3535 ) -> Option<((usize, usize), (usize, usize))> {
3536 vim::text_object_inner_bracket_bridge(self, open)
3537 }
3538
3539 /// Resolve the range of `a<bracket>` (around bracket pair) at the cursor.
3540 ///
3541 /// Like `i<bracket>` but includes the bracket characters themselves.
3542 /// `open` must be one of `'('`, `'{'`, `'['`, `'<'`.
3543 ///
3544 /// Pure function — no cursor mutation.
3545 ///
3546 /// `aB` alias lives in the hjkl-vim grammar layer and is not handled here.
3547 ///
3548 /// Promoted to the public surface in 0.6.X for Phase 4c text-object grammar
3549 /// migration (kryptic-sh/hjkl#70).
3550 pub fn text_object_around_bracket(
3551 &self,
3552 open: char,
3553 ) -> Option<((usize, usize), (usize, usize))> {
3554 vim::text_object_around_bracket_bridge(self, open)
3555 }
3556
3557 // ── Sentence text objects (is / as) ───────────────────────────────────
3558
3559 /// Resolve `is` (inner sentence) at the cursor position.
3560 ///
3561 /// Returns the range of the current sentence, excluding trailing
3562 /// whitespace. Sentence boundaries follow vim's `is` semantics (period /
3563 /// `?` / `!` followed by whitespace or end-of-paragraph).
3564 ///
3565 /// Pure function — no cursor mutation.
3566 ///
3567 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3568 /// grammar migration (kryptic-sh/hjkl#70).
3569 pub fn text_object_inner_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3570 vim::text_object_inner_sentence_bridge(self)
3571 }
3572
3573 /// Resolve `as` (around sentence) at the cursor position.
3574 ///
3575 /// Like `is` but includes trailing whitespace after the sentence
3576 /// terminator.
3577 ///
3578 /// Pure function — no cursor mutation.
3579 ///
3580 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3581 /// grammar migration (kryptic-sh/hjkl#70).
3582 pub fn text_object_around_sentence(&self) -> Option<((usize, usize), (usize, usize))> {
3583 vim::text_object_around_sentence_bridge(self)
3584 }
3585
3586 // ── Paragraph text objects (ip / ap) ──────────────────────────────────
3587
3588 /// Resolve `ip` (inner paragraph) at the cursor position.
3589 ///
3590 /// A paragraph is a block of non-blank lines bounded by blank lines or
3591 /// buffer edges. Returns `None` when the cursor is on a blank line.
3592 ///
3593 /// Pure function — no cursor mutation.
3594 ///
3595 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3596 /// grammar migration (kryptic-sh/hjkl#70).
3597 pub fn text_object_inner_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3598 vim::text_object_inner_paragraph_bridge(self)
3599 }
3600
3601 /// Resolve `ap` (around paragraph) at the cursor position.
3602 ///
3603 /// Like `ip` but includes one trailing blank line when present.
3604 ///
3605 /// Pure function — no cursor mutation.
3606 ///
3607 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3608 /// grammar migration (kryptic-sh/hjkl#70).
3609 pub fn text_object_around_paragraph(&self) -> Option<((usize, usize), (usize, usize))> {
3610 vim::text_object_around_paragraph_bridge(self)
3611 }
3612
3613 // ── Tag text objects (it / at) ────────────────────────────────────────
3614
3615 /// Resolve `it` (inner tag) at the cursor position.
3616 ///
3617 /// Matches XML/HTML-style `<tag>...</tag>` pairs. Returns the range of
3618 /// inner content between the open and close tags (excluding the tags
3619 /// themselves).
3620 ///
3621 /// Pure function — no cursor mutation.
3622 ///
3623 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3624 /// grammar migration (kryptic-sh/hjkl#70).
3625 pub fn text_object_inner_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3626 vim::text_object_inner_tag_bridge(self)
3627 }
3628
3629 /// Resolve `at` (around tag) at the cursor position.
3630 ///
3631 /// Like `it` but includes the open and close tag delimiters themselves.
3632 ///
3633 /// Pure function — no cursor mutation.
3634 ///
3635 /// Promoted to the public surface in 0.6.X for Phase 4d text-object
3636 /// grammar migration (kryptic-sh/hjkl#70).
3637 pub fn text_object_around_tag(&self) -> Option<((usize, usize), (usize, usize))> {
3638 vim::text_object_around_tag_bridge(self)
3639 }
3640
3641 /// Execute a named cursor motion `kind` repeated `count` times.
3642 ///
3643 /// Maps the keymap-layer `crate::MotionKind` to the engine's internal
3644 /// motion primitives, bypassing the engine FSM. Identical cursor semantics
3645 /// to the FSM path — sticky column, scroll sync, and big-jump tracking are
3646 /// all applied via `vim::execute_motion` (for Down/Up) or the same helpers
3647 /// used by the FSM arms.
3648 ///
3649 /// Introduced in 0.6.1 as the host entry point for Phase 3a of
3650 /// kryptic-sh/hjkl#69: the app keymap dispatches `AppAction::Motion` and
3651 /// calls this method rather than re-entering the engine FSM.
3652 ///
3653 /// Engine FSM arms for `h`/`j`/`k`/`l`/`<BS>`/`<Space>`/`+`/`-` remain
3654 /// intact for macro-replay coverage (macros re-feed raw keys through the
3655 /// FSM). This method is the keymap / controller path only.
3656 pub fn apply_motion(&mut self, kind: crate::MotionKind, count: usize) {
3657 vim::apply_motion_kind(self, kind, count);
3658 }
3659
3660 /// Set `vim.pending_register` to `Some(reg)` if `reg` is a valid register
3661 /// selector (`a`–`z`, `A`–`Z`, `0`–`9`, `"`, `+`, `*`, `_`). Invalid
3662 /// chars are silently ignored (no-op), matching the engine FSM's
3663 /// `handle_select_register` behaviour.
3664 ///
3665 /// Promoted to the public surface in 0.5.17 so the hjkl-vim
3666 /// `PendingState::SelectRegister` reducer can dispatch `SetPendingRegister`
3667 /// without re-entering the engine FSM. `handle_select_register` (engine FSM
3668 /// path for macro-replay / defensive coverage) delegates here to avoid
3669 /// logic duplication.
3670 pub fn set_pending_register(&mut self, reg: char) {
3671 if reg.is_ascii_alphanumeric() || matches!(reg, '"' | '+' | '*' | '_') {
3672 self.vim.pending_register = Some(reg);
3673 }
3674 // Invalid chars silently no-op (matches engine FSM behavior).
3675 }
3676
3677 /// Record a mark named `ch` at the current cursor position.
3678 ///
3679 /// Validates `ch` (must be `a`–`z` or `A`–`Z` to match vim's mark-name
3680 /// rules). Invalid chars are silently ignored (no-op), matching the engine
3681 /// FSM's `handle_set_mark` behaviour.
3682 ///
3683 /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3684 /// `PendingState::SetMark` reducer can dispatch `EngineCmd::SetMark`
3685 /// without re-entering the engine FSM. `handle_set_mark` delegates here.
3686 pub fn set_mark_at_cursor(&mut self, ch: char) {
3687 vim::set_mark_at_cursor(self, ch);
3688 }
3689
3690 /// `.` dot-repeat: replay the last buffered change at the current cursor.
3691 /// `count` scales repeats (e.g. `3.` runs the last change 3 times). When
3692 /// `count` is 0, defaults to 1. No-op when no change has been buffered yet.
3693 ///
3694 /// Storage of `LastChange` stays inside engine for now; Phase 5c of
3695 /// kryptic-sh/hjkl#71 just lifts the `.` chord binding into the app
3696 /// keymap so the engine FSM `.` arm is no longer the entry point. Engine
3697 /// FSM `.` arm stays for macro-replay defensive coverage.
3698 pub fn replay_last_change(&mut self, count: usize) {
3699 vim::replay_last_change(self, count);
3700 }
3701
3702 /// Jump to the mark named `ch`, linewise (row only; col snaps to first
3703 /// non-blank). Pushes the pre-jump position onto the jumplist if the
3704 /// cursor actually moved.
3705 ///
3706 /// Accepts the same mark chars as vim's `'<ch>` command: `a`–`z`,
3707 /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3708 /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3709 /// are silently ignored (no-op), matching the engine FSM's
3710 /// `handle_goto_mark` behaviour.
3711 ///
3712 /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3713 /// `PendingState::GotoMarkLine` reducer can dispatch
3714 /// `EngineCmd::GotoMarkLine` without re-entering the engine FSM.
3715 pub fn goto_mark_line(&mut self, ch: char) {
3716 vim::goto_mark(self, ch, true);
3717 }
3718
3719 /// Jump to the mark named `ch`, charwise (exact row + col). Pushes the
3720 /// pre-jump position onto the jumplist if the cursor actually moved.
3721 ///
3722 /// Accepts the same mark chars as vim's `` `<ch> `` command: `a`–`z`,
3723 /// `A`–`Z`, `'`/`` ` `` (jump-back peek), `.` (last edit), and the
3724 /// special auto-marks `[`, `]`, `<`, `>`. Unset marks and invalid chars
3725 /// are silently ignored (no-op), matching the engine FSM's
3726 /// `handle_goto_mark` behaviour.
3727 ///
3728 /// Promoted to the public surface in 0.6.7 so the hjkl-vim
3729 /// `PendingState::GotoMarkChar` reducer can dispatch
3730 /// `EngineCmd::GotoMarkChar` without re-entering the engine FSM.
3731 pub fn goto_mark_char(&mut self, ch: char) {
3732 vim::goto_mark(self, ch, false);
3733 }
3734
3735 /// Jump to the mark named `ch`, linewise. For uppercase marks (`'A'`–`'Z'`)
3736 /// that live in a different buffer, returns `MarkJump::CrossBuffer` so the
3737 /// app can switch slots before positioning the cursor. Returns
3738 /// `MarkJump::SameBuffer` for same-buffer / lowercase / special marks, and
3739 /// `MarkJump::Unset` when the mark is not set.
3740 pub fn try_goto_mark_line(&mut self, ch: char) -> MarkJump {
3741 vim::try_goto_mark(self, ch, true)
3742 }
3743
3744 /// Jump to the mark named `ch`, charwise. For uppercase marks (`'A'`–`'Z'`)
3745 /// that live in a different buffer, returns `MarkJump::CrossBuffer` so the
3746 /// app can switch slots before positioning the cursor. Returns
3747 /// `MarkJump::SameBuffer` for same-buffer / lowercase / special marks, and
3748 /// `MarkJump::Unset` when the mark is not set.
3749 pub fn try_goto_mark_char(&mut self, ch: char) -> MarkJump {
3750 vim::try_goto_mark(self, ch, false)
3751 }
3752
3753 // ── Macro controller API (Phase 5b) ──────────────────────────────────────
3754
3755 /// Begin recording keystrokes into register `reg`. The caller (app) is
3756 /// responsible for stopping the recording via `stop_macro_record` when the
3757 /// user presses bare `q`.
3758 ///
3759 /// - Uppercase `reg` (e.g. `'A'`) appends to the existing lowercase
3760 /// recording by pre-seeding `recording_keys` with the decoded text of the
3761 /// matching lowercase register, matching vim's capital-register append
3762 /// semantics.
3763 /// - Lowercase `reg` clears `recording_keys` (fresh recording).
3764 /// - Invalid chars (non-alphabetic, non-digit) are silently ignored.
3765 ///
3766 /// Promoted to the public surface in Phase 5b so the app's
3767 /// `route_chord_key` can start a recording without re-entering the engine
3768 /// FSM. `handle_record_macro_target` (engine FSM path for macro-replay
3769 /// defensive coverage) continues to use the same logic via delegation.
3770 pub fn start_macro_record(&mut self, reg: char) {
3771 if !(reg.is_ascii_alphabetic() || reg.is_ascii_digit()) {
3772 return;
3773 }
3774 self.vim.recording_macro = Some(reg);
3775 if reg.is_ascii_uppercase() {
3776 // Seed recording_keys with the existing lowercase register's text
3777 // decoded back to inputs so capital-register append continues from
3778 // where the previous recording left off.
3779 let lower = reg.to_ascii_lowercase();
3780 let text = self
3781 .registers
3782 .read(lower)
3783 .map(|s| s.text.clone())
3784 .unwrap_or_default();
3785 self.vim.recording_keys = crate::input::decode_macro(&text);
3786 } else {
3787 self.vim.recording_keys.clear();
3788 }
3789 }
3790
3791 /// Finalize the active recording: encode `recording_keys` as text and write
3792 /// to the matching (lowercase) named register. Clears both `recording_macro`
3793 /// and `recording_keys`. No-ops if no recording is active.
3794 ///
3795 /// Promoted to the public surface in Phase 5b so the app's `QChord` action
3796 /// can stop a recording when the user presses bare `q` without re-entering
3797 /// the engine FSM.
3798 pub fn stop_macro_record(&mut self) {
3799 let Some(reg) = self.vim.recording_macro.take() else {
3800 return;
3801 };
3802 let keys = std::mem::take(&mut self.vim.recording_keys);
3803 let text = crate::input::encode_macro(&keys);
3804 self.set_named_register_text(reg.to_ascii_lowercase(), text);
3805 }
3806
3807 /// Returns `true` while a `q{reg}` recording is in progress.
3808 /// Hosts use this to show a "recording @r" status indicator and to decide
3809 /// whether bare `q` should stop the recording or open the `RecordMacroTarget`
3810 /// chord.
3811 pub fn is_recording_macro(&self) -> bool {
3812 self.vim.recording_macro.is_some()
3813 }
3814
3815 /// Returns `true` while a macro is being replayed. The app sets this flag
3816 /// (via `play_macro`) and clears it (via `end_macro_replay`) around the
3817 /// re-feed loop so the recorder hook can skip double-capture.
3818 pub fn is_replaying_macro(&self) -> bool {
3819 self.vim.replaying_macro
3820 }
3821
3822 /// Decode the named register `reg` into a `Vec<crate::input::Input>` and
3823 /// prepare for replay, returning the inputs the app should re-feed through
3824 /// `route_chord_key`.
3825 ///
3826 /// Resolves `reg`:
3827 /// - `'@'` → use `vim.last_macro`; returns empty vec if none.
3828 /// - Any other char → lowercase it, read the register, decode.
3829 ///
3830 /// Side-effects:
3831 /// - Sets `vim.last_macro` to the resolved register.
3832 /// - Sets `vim.replaying_macro = true` so the recorder hook skips during
3833 /// replay. The app calls `end_macro_replay` after the loop finishes.
3834 ///
3835 /// Returns an empty vec (and no side-effects for `'@'`) if the register is
3836 /// unset or empty.
3837 pub fn play_macro(&mut self, reg: char, count: usize) -> Vec<crate::input::Input> {
3838 let resolved = if reg == '@' {
3839 match self.vim.last_macro {
3840 Some(r) => r,
3841 None => return vec![],
3842 }
3843 } else {
3844 reg.to_ascii_lowercase()
3845 };
3846 let text = match self.registers.read(resolved) {
3847 Some(slot) if !slot.text.is_empty() => slot.text.clone(),
3848 _ => return vec![],
3849 };
3850 let keys = crate::input::decode_macro(&text);
3851 self.vim.last_macro = Some(resolved);
3852 self.vim.replaying_macro = true;
3853 // Multiply by count (minimum 1).
3854 keys.repeat(count.max(1))
3855 }
3856
3857 /// Clear the `replaying_macro` flag. Called by the app after the
3858 /// re-feed loop in the `PlayMacro` commit arm completes (or aborts).
3859 pub fn end_macro_replay(&mut self) {
3860 self.vim.replaying_macro = false;
3861 }
3862
3863 /// Append `input` to the active recording (`recording_keys`) if and only
3864 /// if a recording is in progress AND we are not currently replaying.
3865 /// Called by the app's `route_chord_key` recorder hook so that user
3866 /// keystrokes captured through the app-level chord path are recorded
3867 /// (rather than relying solely on the engine FSM's in-step hook).
3868 pub fn record_input(&mut self, input: crate::input::Input) {
3869 if self.vim.recording_macro.is_some() && !self.vim.replaying_macro {
3870 self.vim.recording_keys.push(input);
3871 }
3872 }
3873
3874 // ─── Phase 6.1: public insert-mode primitives (kryptic-sh/hjkl#87) ────────
3875 //
3876 // Each method is the publicly callable form of one insert-mode action.
3877 // All logic lives in the corresponding `vim::*_bridge` free function;
3878 // these methods are thin delegators so the public surface stays on `Editor`.
3879 //
3880 // Invariants (enforced by the bridge fns):
3881 // - Buffer mutations go through `mutate_edit` (dirty/undo/change-list).
3882 // - Navigation keys call `break_undo_group_in_insert` when the FSM did.
3883 // - `push_buffer_cursor_to_textarea` is called after every mutation
3884 // (currently a no-op, kept for migration hygiene).
3885
3886 /// Insert `ch` at the cursor. In Replace mode, overstrike the cell under
3887 /// the cursor instead of inserting; at end-of-line, always appends. With
3888 /// `smartindent` on, closing brackets (`}`/`)`/`]`) trigger one-unit
3889 /// dedent on an otherwise-whitespace line.
3890 ///
3891 /// Callers must ensure the editor is in Insert or Replace mode before
3892 /// calling this method.
3893 pub fn insert_char(&mut self, ch: char) {
3894 let mutated = vim::insert_char_bridge(self, ch);
3895 if mutated {
3896 self.mark_content_dirty();
3897 let (row, _) = self.cursor();
3898 self.vim.widen_insert_row(row);
3899 }
3900 }
3901
3902 /// Insert a newline at the cursor, applying autoindent / smartindent to
3903 /// prefix the new line with the appropriate leading whitespace.
3904 ///
3905 /// Callers must ensure the editor is in Insert mode before calling.
3906 pub fn insert_newline(&mut self) {
3907 let mutated = vim::insert_newline_bridge(self);
3908 if mutated {
3909 self.mark_content_dirty();
3910 let (row, _) = self.cursor();
3911 self.vim.widen_insert_row(row);
3912 }
3913 }
3914
3915 /// Insert a tab character (or spaces up to the next `softtabstop` boundary
3916 /// when `expandtab` is set).
3917 ///
3918 /// Callers must ensure the editor is in Insert mode before calling.
3919 pub fn insert_tab(&mut self) {
3920 let mutated = vim::insert_tab_bridge(self);
3921 if mutated {
3922 self.mark_content_dirty();
3923 let (row, _) = self.cursor();
3924 self.vim.widen_insert_row(row);
3925 }
3926 }
3927
3928 /// Delete the character before the cursor (Backspace). With `softtabstop`
3929 /// active, deletes the entire soft-tab run at an aligned boundary. Joins
3930 /// with the previous line when at column 0.
3931 ///
3932 /// Callers must ensure the editor is in Insert mode before calling.
3933 pub fn insert_backspace(&mut self) {
3934 let mutated = vim::insert_backspace_bridge(self);
3935 if mutated {
3936 self.mark_content_dirty();
3937 let (row, _) = self.cursor();
3938 self.vim.widen_insert_row(row);
3939 }
3940 }
3941
3942 /// Delete the character under the cursor (Delete key). Joins with the
3943 /// next line when at end-of-line.
3944 ///
3945 /// Callers must ensure the editor is in Insert mode before calling.
3946 pub fn insert_delete(&mut self) {
3947 let mutated = vim::insert_delete_bridge(self);
3948 if mutated {
3949 self.mark_content_dirty();
3950 let (row, _) = self.cursor();
3951 self.vim.widen_insert_row(row);
3952 }
3953 }
3954
3955 /// Move the cursor one step in `dir` (arrow key), breaking the undo group
3956 /// per `undo_break_on_motion`.
3957 ///
3958 /// Callers must ensure the editor is in Insert mode before calling.
3959 pub fn insert_arrow(&mut self, dir: vim::InsertDir) {
3960 vim::insert_arrow_bridge(self, dir);
3961 let (row, _) = self.cursor();
3962 self.vim.widen_insert_row(row);
3963 }
3964
3965 /// Move the cursor to the start of the current line (Home key), breaking
3966 /// the undo group.
3967 ///
3968 /// Callers must ensure the editor is in Insert mode before calling.
3969 pub fn insert_home(&mut self) {
3970 vim::insert_home_bridge(self);
3971 let (row, _) = self.cursor();
3972 self.vim.widen_insert_row(row);
3973 }
3974
3975 /// Move the cursor to the end of the current line (End key), breaking the
3976 /// undo group.
3977 ///
3978 /// Callers must ensure the editor is in Insert mode before calling.
3979 pub fn insert_end(&mut self) {
3980 vim::insert_end_bridge(self);
3981 let (row, _) = self.cursor();
3982 self.vim.widen_insert_row(row);
3983 }
3984
3985 /// Scroll up one full viewport height (PageUp), moving the cursor with it.
3986 /// `viewport_h` is the current viewport height in rows; pass
3987 /// `self.viewport_height_value()` if the stored value is current.
3988 ///
3989 /// Callers must ensure the editor is in Insert mode before calling.
3990 pub fn insert_pageup(&mut self, viewport_h: u16) {
3991 vim::insert_pageup_bridge(self, viewport_h);
3992 let (row, _) = self.cursor();
3993 self.vim.widen_insert_row(row);
3994 }
3995
3996 /// Scroll down one full viewport height (PageDown), moving the cursor with
3997 /// it. `viewport_h` is the current viewport height in rows.
3998 ///
3999 /// Callers must ensure the editor is in Insert mode before calling.
4000 pub fn insert_pagedown(&mut self, viewport_h: u16) {
4001 vim::insert_pagedown_bridge(self, viewport_h);
4002 let (row, _) = self.cursor();
4003 self.vim.widen_insert_row(row);
4004 }
4005
4006 /// Delete from the cursor back to the start of the previous word (`Ctrl-W`).
4007 /// At column 0, joins with the previous line (vim `b`-motion semantics).
4008 ///
4009 /// Callers must ensure the editor is in Insert mode before calling.
4010 pub fn insert_ctrl_w(&mut self) {
4011 let mutated = vim::insert_ctrl_w_bridge(self);
4012 if mutated {
4013 self.mark_content_dirty();
4014 let (row, _) = self.cursor();
4015 self.vim.widen_insert_row(row);
4016 }
4017 }
4018
4019 /// Delete from the cursor back to the start of the current line (`Ctrl-U`).
4020 /// No-op when already at column 0.
4021 ///
4022 /// Callers must ensure the editor is in Insert mode before calling.
4023 pub fn insert_ctrl_u(&mut self) {
4024 let mutated = vim::insert_ctrl_u_bridge(self);
4025 if mutated {
4026 self.mark_content_dirty();
4027 let (row, _) = self.cursor();
4028 self.vim.widen_insert_row(row);
4029 }
4030 }
4031
4032 /// Delete one character backwards (`Ctrl-H`) — alias for Backspace in
4033 /// insert mode. Joins with the previous line when at col 0.
4034 ///
4035 /// Callers must ensure the editor is in Insert mode before calling.
4036 pub fn insert_ctrl_h(&mut self) {
4037 let mutated = vim::insert_ctrl_h_bridge(self);
4038 if mutated {
4039 self.mark_content_dirty();
4040 let (row, _) = self.cursor();
4041 self.vim.widen_insert_row(row);
4042 }
4043 }
4044
4045 /// Enter "one-shot normal" mode (`Ctrl-O`): suspend insert for the next
4046 /// complete normal-mode command, then return to insert automatically.
4047 ///
4048 /// Callers must ensure the editor is in Insert mode before calling.
4049 pub fn insert_ctrl_o_arm(&mut self) {
4050 vim::insert_ctrl_o_bridge(self);
4051 }
4052
4053 /// Arm the register-paste selector (`Ctrl-R`). The next call to
4054 /// `insert_paste_register(reg)` will insert the register contents.
4055 /// Alternatively, feeding a `Key::Char(c)` through the FSM will consume
4056 /// the armed state and paste register `c`.
4057 ///
4058 /// Callers must ensure the editor is in Insert mode before calling.
4059 pub fn insert_ctrl_r_arm(&mut self) {
4060 vim::insert_ctrl_r_bridge(self);
4061 }
4062
4063 /// Indent the current line by one `shiftwidth` and shift the cursor right
4064 /// by the same amount (`Ctrl-T`).
4065 ///
4066 /// Callers must ensure the editor is in Insert mode before calling.
4067 pub fn insert_ctrl_t(&mut self) {
4068 let mutated = vim::insert_ctrl_t_bridge(self);
4069 if mutated {
4070 self.mark_content_dirty();
4071 let (row, _) = self.cursor();
4072 self.vim.widen_insert_row(row);
4073 }
4074 }
4075
4076 /// Outdent the current line by up to one `shiftwidth` and shift the cursor
4077 /// left by the amount stripped (`Ctrl-D`).
4078 ///
4079 /// Callers must ensure the editor is in Insert mode before calling.
4080 pub fn insert_ctrl_d(&mut self) {
4081 let mutated = vim::insert_ctrl_d_bridge(self);
4082 if mutated {
4083 self.mark_content_dirty();
4084 let (row, _) = self.cursor();
4085 self.vim.widen_insert_row(row);
4086 }
4087 }
4088
4089 /// Paste the contents of register `reg` at the cursor (the commit arm of
4090 /// `Ctrl-R {reg}`). Unknown or empty registers are a no-op.
4091 ///
4092 /// Callers must ensure the editor is in Insert mode before calling.
4093 pub fn insert_paste_register(&mut self, reg: char) {
4094 vim::insert_paste_register_bridge(self, reg);
4095 let (row, _) = self.cursor();
4096 self.vim.widen_insert_row(row);
4097 }
4098
4099 /// Exit insert mode to Normal: finish the insert session, step the cursor
4100 /// one cell left (vim convention on Esc), record the `gi` target position,
4101 /// and update the sticky column.
4102 ///
4103 /// Callers must ensure the editor is in Insert mode before calling.
4104 pub fn leave_insert_to_normal(&mut self) {
4105 vim::leave_insert_to_normal_bridge(self);
4106 }
4107
4108 // ── Phase 6.2: normal-mode primitive controller methods ───────────────────
4109 //
4110 // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4111 // `vim.rs` following the same pattern as Phase 6.1. The FSM's
4112 // `handle_normal_only` now calls the same bridges so both paths are
4113 // identical. See kryptic-sh/hjkl#88 for the full promotion plan.
4114
4115 /// `i` — transition to Insert mode at the current cursor position.
4116 /// `count` is stored in the insert session and replayed by dot-repeat
4117 /// as a repeat count on the inserted text.
4118 pub fn enter_insert_i(&mut self, count: usize) {
4119 vim::enter_insert_i_bridge(self, count);
4120 }
4121
4122 /// `I` — move to the first non-blank character on the line, then
4123 /// transition to Insert mode. `count` is stored for dot-repeat.
4124 pub fn enter_insert_shift_i(&mut self, count: usize) {
4125 vim::enter_insert_shift_i_bridge(self, count);
4126 }
4127
4128 /// `a` — advance the cursor one cell past the current position, then
4129 /// transition to Insert mode (append). `count` is stored for dot-repeat.
4130 pub fn enter_insert_a(&mut self, count: usize) {
4131 vim::enter_insert_a_bridge(self, count);
4132 }
4133
4134 /// `A` — move the cursor to the end of the line, then transition to
4135 /// Insert mode (append at end). `count` is stored for dot-repeat.
4136 pub fn enter_insert_shift_a(&mut self, count: usize) {
4137 vim::enter_insert_shift_a_bridge(self, count);
4138 }
4139
4140 /// `o` — open a new line below the current line with smart-indent, then
4141 /// transition to Insert mode. `count` is stored for dot-repeat replay.
4142 pub fn open_line_below(&mut self, count: usize) {
4143 vim::open_line_below_bridge(self, count);
4144 }
4145
4146 /// `O` — open a new line above the current line with smart-indent, then
4147 /// transition to Insert mode. `count` is stored for dot-repeat replay.
4148 pub fn open_line_above(&mut self, count: usize) {
4149 vim::open_line_above_bridge(self, count);
4150 }
4151
4152 /// `R` — enter Replace mode: subsequent typed characters overstrike the
4153 /// cell under the cursor rather than inserting. `count` is for replay.
4154 pub fn enter_replace_mode(&mut self, count: usize) {
4155 vim::enter_replace_mode_bridge(self, count);
4156 }
4157
4158 /// `x` — delete `count` characters forward from the cursor and write them
4159 /// to the unnamed register. No-op on an empty line. Records for `.`.
4160 pub fn delete_char_forward(&mut self, count: usize) {
4161 vim::delete_char_forward_bridge(self, count);
4162 }
4163
4164 /// `X` — delete `count` characters backward from the cursor and write
4165 /// them to the unnamed register. No-op at column 0. Records for `.`.
4166 pub fn delete_char_backward(&mut self, count: usize) {
4167 vim::delete_char_backward_bridge(self, count);
4168 }
4169
4170 /// `s` — substitute `count` characters: delete them (writing to the
4171 /// unnamed register) then enter Insert mode. Equivalent to `cl`.
4172 /// Records as `OpMotion { Change, Right }` for dot-repeat.
4173 pub fn substitute_char(&mut self, count: usize) {
4174 vim::substitute_char_bridge(self, count);
4175 }
4176
4177 /// `S` — substitute the current line: wipe its contents (writing to the
4178 /// unnamed register) then enter Insert mode. Equivalent to `cc`.
4179 /// Records as `LineOp { Change }` for dot-repeat.
4180 pub fn substitute_line(&mut self, count: usize) {
4181 vim::substitute_line_bridge(self, count);
4182 }
4183
4184 /// `D` — delete from the cursor to end-of-line, writing to the unnamed
4185 /// register. The cursor parks on the new last character. Records for `.`.
4186 pub fn delete_to_eol(&mut self) {
4187 vim::delete_to_eol_bridge(self);
4188 }
4189
4190 /// `C` — change from the cursor to end-of-line: delete to EOL then enter
4191 /// Insert mode. Equivalent to `c$`. Does not record its own `last_change`
4192 /// (the insert session records `DeleteToEol` on exit, like `c` motions).
4193 pub fn change_to_eol(&mut self) {
4194 vim::change_to_eol_bridge(self);
4195 }
4196
4197 /// `Y` — yank from the cursor to end-of-line into the unnamed register.
4198 /// Vim 8 default: equivalent to `y$`. `count` multiplies the motion.
4199 pub fn yank_to_eol(&mut self, count: usize) {
4200 vim::yank_to_eol_bridge(self, count);
4201 }
4202
4203 /// `J` — join `count` lines (default 2) onto the current line, inserting
4204 /// a single space between each non-empty pair. Records for dot-repeat.
4205 pub fn join_line(&mut self, count: usize) {
4206 vim::join_line_bridge(self, count);
4207 }
4208
4209 /// `~` — toggle the case of `count` characters from the cursor, advancing
4210 /// right after each toggle. Records `ToggleCase` for dot-repeat.
4211 pub fn toggle_case_at_cursor(&mut self, count: usize) {
4212 vim::toggle_case_at_cursor_bridge(self, count);
4213 }
4214
4215 /// `p` — paste the unnamed register (or the register selected via `"r`)
4216 /// after the cursor. Linewise content opens a new line below; charwise
4217 /// content is inserted inline. Records `Paste { before: false }` for `.`.
4218 pub fn paste_after(&mut self, count: usize) {
4219 vim::paste_after_bridge(self, count);
4220 }
4221
4222 /// `P` — paste the unnamed register (or the `"r` register) before the
4223 /// cursor. Linewise content opens a new line above; charwise is inline.
4224 /// Records `Paste { before: true }` for dot-repeat.
4225 pub fn paste_before(&mut self, count: usize) {
4226 vim::paste_before_bridge(self, count);
4227 }
4228
4229 /// `<C-o>` — jump back `count` entries in the jumplist, saving the
4230 /// current position on the forward stack so `<C-i>` can return.
4231 pub fn jump_back(&mut self, count: usize) {
4232 vim::jump_back_bridge(self, count);
4233 }
4234
4235 /// `<C-i>` / `Tab` — redo `count` entries on the forward jumplist stack,
4236 /// saving the current position on the backward stack.
4237 pub fn jump_forward(&mut self, count: usize) {
4238 vim::jump_forward_bridge(self, count);
4239 }
4240
4241 /// `<C-f>` / `<C-b>` — scroll the cursor by one full viewport height
4242 /// (height − 2 rows, preserving two-line overlap). `count` multiplies.
4243 /// `dir = Down` for `<C-f>`, `Up` for `<C-b>`.
4244 pub fn scroll_full_page(&mut self, dir: vim::ScrollDir, count: usize) {
4245 vim::scroll_full_page_bridge(self, dir, count);
4246 }
4247
4248 /// `<C-d>` / `<C-u>` — scroll the cursor by half the viewport height.
4249 /// `count` multiplies the step. `dir = Down` for `<C-d>`, `Up` for `<C-u>`.
4250 pub fn scroll_half_page(&mut self, dir: vim::ScrollDir, count: usize) {
4251 vim::scroll_half_page_bridge(self, dir, count);
4252 }
4253
4254 /// `<C-e>` / `<C-y>` — scroll the viewport `count` lines without moving
4255 /// the cursor (cursor is clamped to the new visible region if necessary).
4256 /// `dir = Down` for `<C-e>` (scroll text up), `Up` for `<C-y>`.
4257 pub fn scroll_line(&mut self, dir: vim::ScrollDir, count: usize) {
4258 vim::scroll_line_bridge(self, dir, count);
4259 }
4260
4261 /// `n` — repeat the last `/` or `?` search `count` times in its original
4262 /// direction. `forward = true` keeps the direction; `false` inverts (`N`).
4263 pub fn search_repeat(&mut self, forward: bool, count: usize) {
4264 vim::search_repeat_bridge(self, forward, count);
4265 }
4266
4267 /// `*` / `#` / `g*` / `g#` — search for the word under the cursor.
4268 /// `forward` chooses direction; `whole_word` wraps the pattern in `\b`
4269 /// anchors (true for `*` / `#`, false for `g*` / `g#`). `count` repeats.
4270 pub fn word_search(&mut self, forward: bool, whole_word: bool, count: usize) {
4271 vim::word_search_bridge(self, forward, whole_word, count);
4272 }
4273
4274 // ── Phase 6.3: visual-mode primitive controller methods ──────────────────
4275 //
4276 // Each method is a thin wrapper around a `pub(crate) fn *_bridge` in
4277 // `vim.rs` following the same pattern as Phase 6.1 / 6.2. Both the FSM
4278 // and these wrappers write `current_mode` so `vim_mode()` returns correct
4279 // values regardless of which path performed the transition.
4280 // See kryptic-sh/hjkl#89 for the full promotion plan.
4281
4282 /// `v` from Normal — enter charwise Visual mode, anchoring the selection
4283 /// at the current cursor position.
4284 pub fn enter_visual_char(&mut self) {
4285 vim::enter_visual_char_bridge(self);
4286 }
4287
4288 /// `V` from Normal — enter linewise Visual mode, anchoring on the current
4289 /// line. Motions extend the selection by whole lines.
4290 pub fn enter_visual_line(&mut self) {
4291 vim::enter_visual_line_bridge(self);
4292 }
4293
4294 /// `<C-v>` from Normal — enter Visual-block mode. The selection is a
4295 /// rectangle whose corners are the anchor and the live cursor.
4296 pub fn enter_visual_block(&mut self) {
4297 vim::enter_visual_block_bridge(self);
4298 }
4299
4300 /// Esc from any visual mode — set `<` / `>` marks, stash the selection
4301 /// for `gv` re-entry, then return to Normal mode.
4302 pub fn exit_visual_to_normal(&mut self) {
4303 vim::exit_visual_to_normal_bridge(self);
4304 }
4305
4306 /// `o` in Visual / VisualLine / VisualBlock — swap the cursor and anchor
4307 /// so the user can extend the other end of the selection. Does NOT
4308 /// mutate the selection range; only the active endpoint changes.
4309 pub fn visual_o_toggle(&mut self) {
4310 vim::visual_o_toggle_bridge(self);
4311 }
4312
4313 /// `gv` — restore the last visual selection (mode + anchor + cursor
4314 /// position). No-op when no visual selection has been exited yet.
4315 pub fn reenter_last_visual(&mut self) {
4316 vim::reenter_last_visual_bridge(self);
4317 }
4318
4319 /// Direct mode-transition entry point. Sets both the internal FSM mode
4320 /// and the stable `current_mode` field read by [`Editor::vim_mode`].
4321 ///
4322 /// Prefer the semantic primitives (`enter_visual_char`, `enter_insert_i`,
4323 /// …) which also set up required bookkeeping (anchors, sessions, …).
4324 /// Use `set_mode` only when you need a raw mode flip without side-effects.
4325 pub fn set_mode(&mut self, mode: VimMode) {
4326 vim::set_mode_bridge(self, mode);
4327 }
4328}
4329
4330// ── Phase 6.6b: FSM state accessors (for hjkl-vim ownership) ─────────────────
4331//
4332// The FSM (now in hjkl-vim) reads/writes `VimState` fields through public
4333// `Editor` accessors and mutators defined in this block. Each method gets a
4334// one-line `///` rustdoc. Fields mutated as a unit get a combined action method
4335// rather than individual getters + setters (e.g. `accumulate_count_digit`).
4336
4337/// State carried between [`Editor::begin_step`] and [`Editor::end_step`].
4338///
4339/// Treat as opaque — construct by calling `begin_step` and pass the
4340/// returned value directly into `end_step` without modification.
4341/// The fields capture per-step pre-dispatch state that the epilogue
4342/// needs to run its invariants correctly.
4343pub struct StepBookkeeping {
4344 /// True when the pending chord before this step was a macro-chord
4345 /// (`q{reg}` or `@{reg}`). The recorder hook skips these bookkeeping
4346 /// keys so that only the *payload* keys enter `recording_keys`.
4347 pub pending_was_macro_chord: bool,
4348 /// True when the mode was Insert *before* the FSM body ran. Used by
4349 /// the Ctrl-o one-shot-normal epilogue to decide whether to bounce
4350 /// back into Insert.
4351 pub was_insert: bool,
4352 /// Pre-dispatch visual snapshot. When the FSM body transitions out of
4353 /// a visual mode the epilogue uses this to set the `<`/`>` marks and
4354 /// store `last_visual` for `gv`.
4355 pub pre_visual_snapshot: Option<vim::LastVisual>,
4356}
4357
4358impl<H: crate::types::Host> Editor<hjkl_buffer::Buffer, H> {
4359 // ── Pending chord ─────────────────────────────────────────────────────────
4360
4361 /// Return a clone of the current pending chord state.
4362 pub fn pending(&self) -> vim::Pending {
4363 self.vim.pending.clone()
4364 }
4365
4366 /// Overwrite the pending chord state.
4367 pub fn set_pending(&mut self, p: vim::Pending) {
4368 self.vim.pending = p;
4369 }
4370
4371 /// Atomically take the pending chord, replacing it with `Pending::None`.
4372 pub fn take_pending(&mut self) -> vim::Pending {
4373 std::mem::take(&mut self.vim.pending)
4374 }
4375
4376 // ── Count prefix ──────────────────────────────────────────────────────────
4377
4378 /// Return the raw digit-prefix count (`0` = no prefix typed yet).
4379 pub fn count(&self) -> usize {
4380 self.vim.count
4381 }
4382
4383 /// Overwrite the digit-prefix count directly.
4384 pub fn set_count(&mut self, c: usize) {
4385 self.vim.count = c;
4386 }
4387
4388 /// Accumulate one more digit into the count prefix (mirrors `count * 10 + digit`).
4389 pub fn accumulate_count_digit(&mut self, digit: usize) {
4390 self.vim.count = self.vim.count.saturating_mul(10) + digit;
4391 }
4392
4393 /// Reset the count prefix to zero (no pending count).
4394 pub fn reset_count(&mut self) {
4395 self.vim.count = 0;
4396 }
4397
4398 /// Consume the count and return it; resets to zero. Returns `1` when no
4399 /// prefix was typed (mirrors `take_count` in vim.rs).
4400 pub fn take_count(&mut self) -> usize {
4401 if self.vim.count > 0 {
4402 let n = self.vim.count;
4403 self.vim.count = 0;
4404 n
4405 } else {
4406 1
4407 }
4408 }
4409
4410 // ── Internal FSM mode ─────────────────────────────────────────────────────
4411
4412 /// Return the FSM-internal mode (Normal / Insert / Visual / …).
4413 pub fn fsm_mode(&self) -> vim::Mode {
4414 self.vim.mode
4415 }
4416
4417 /// Overwrite the FSM-internal mode without side-effects. Prefer the
4418 /// semantic primitives (`enter_insert_i`, `enter_visual_char`, …).
4419 pub fn set_fsm_mode(&mut self, m: vim::Mode) {
4420 self.vim.mode = m;
4421 self.vim.current_mode = self.vim.public_mode();
4422 }
4423
4424 // ── Replaying flag ────────────────────────────────────────────────────────
4425
4426 /// `true` while the `.` dot-repeat replay is running.
4427 pub fn is_replaying(&self) -> bool {
4428 self.vim.replaying
4429 }
4430
4431 /// Set or clear the dot-replay flag.
4432 pub fn set_replaying(&mut self, v: bool) {
4433 self.vim.replaying = v;
4434 }
4435
4436 // ── One-shot normal (Ctrl-o) ──────────────────────────────────────────────
4437
4438 /// `true` when we entered Normal from Insert via `Ctrl-o` and will return
4439 /// to Insert after the next complete command.
4440 pub fn is_one_shot_normal(&self) -> bool {
4441 self.vim.one_shot_normal
4442 }
4443
4444 /// Set or clear the Ctrl-o one-shot-normal flag.
4445 pub fn set_one_shot_normal(&mut self, v: bool) {
4446 self.vim.one_shot_normal = v;
4447 }
4448
4449 // ── Last find (f/F/t/T target) ────────────────────────────────────────────
4450
4451 /// Return the last `f`/`F`/`t`/`T` target as `(char, forward, till)`, or
4452 /// `None` before any find command was executed.
4453 pub fn last_find(&self) -> Option<(char, bool, bool)> {
4454 self.vim.last_find
4455 }
4456
4457 /// Overwrite the stored last-find target.
4458 pub fn set_last_find(&mut self, target: Option<(char, bool, bool)>) {
4459 self.vim.last_find = target;
4460 }
4461
4462 // ── Last change (dot-repeat payload) ─────────────────────────────────────
4463
4464 /// Return a clone of the last recorded mutating change, or `None` before
4465 /// any change has been made.
4466 pub fn last_change(&self) -> Option<vim::LastChange> {
4467 self.vim.last_change.clone()
4468 }
4469
4470 /// Overwrite the stored last-change record.
4471 pub fn set_last_change(&mut self, lc: Option<vim::LastChange>) {
4472 self.vim.last_change = lc;
4473 }
4474
4475 /// Borrow the last-change record mutably (e.g. to fill in an `inserted`
4476 /// field after the insert session completes).
4477 pub fn last_change_mut(&mut self) -> Option<&mut vim::LastChange> {
4478 self.vim.last_change.as_mut()
4479 }
4480
4481 // ── Insert session ────────────────────────────────────────────────────────
4482
4483 /// Borrow the active insert session, or `None` when not in Insert mode.
4484 pub fn insert_session(&self) -> Option<&vim::InsertSession> {
4485 self.vim.insert_session.as_ref()
4486 }
4487
4488 /// Borrow the active insert session mutably.
4489 pub fn insert_session_mut(&mut self) -> Option<&mut vim::InsertSession> {
4490 self.vim.insert_session.as_mut()
4491 }
4492
4493 /// Atomically take the insert session out, leaving `None`.
4494 pub fn take_insert_session(&mut self) -> Option<vim::InsertSession> {
4495 self.vim.insert_session.take()
4496 }
4497
4498 /// Install a new insert session, replacing any existing one.
4499 pub fn set_insert_session(&mut self, s: Option<vim::InsertSession>) {
4500 self.vim.insert_session = s;
4501 }
4502
4503 // ── Visual anchors ────────────────────────────────────────────────────────
4504
4505 /// Return the charwise Visual-mode anchor `(row, col)`.
4506 pub fn visual_anchor(&self) -> (usize, usize) {
4507 self.vim.visual_anchor
4508 }
4509
4510 /// Overwrite the charwise Visual-mode anchor.
4511 pub fn set_visual_anchor(&mut self, anchor: (usize, usize)) {
4512 self.vim.visual_anchor = anchor;
4513 }
4514
4515 /// Return the VisualLine anchor row.
4516 pub fn visual_line_anchor(&self) -> usize {
4517 self.vim.visual_line_anchor
4518 }
4519
4520 /// Overwrite the VisualLine anchor row.
4521 pub fn set_visual_line_anchor(&mut self, row: usize) {
4522 self.vim.visual_line_anchor = row;
4523 }
4524
4525 /// Return the VisualBlock anchor `(row, col)`.
4526 pub fn block_anchor(&self) -> (usize, usize) {
4527 self.vim.block_anchor
4528 }
4529
4530 /// Overwrite the VisualBlock anchor.
4531 pub fn set_block_anchor(&mut self, anchor: (usize, usize)) {
4532 self.vim.block_anchor = anchor;
4533 }
4534
4535 /// Return the VisualBlock virtual column used to survive j/k row clamping.
4536 pub fn block_vcol(&self) -> usize {
4537 self.vim.block_vcol
4538 }
4539
4540 /// Overwrite the VisualBlock virtual column.
4541 pub fn set_block_vcol(&mut self, vcol: usize) {
4542 self.vim.block_vcol = vcol;
4543 }
4544
4545 // ── Yank linewise flag ────────────────────────────────────────────────────
4546
4547 /// `true` when the last yank/cut was linewise (affects `p`/`P` layout).
4548 pub fn yank_linewise(&self) -> bool {
4549 self.vim.yank_linewise
4550 }
4551
4552 /// Set or clear the linewise-yank flag.
4553 pub fn set_yank_linewise(&mut self, v: bool) {
4554 self.vim.yank_linewise = v;
4555 }
4556
4557 // ── Pending register selector ─────────────────────────────────────────────
4558 // Note: `pending_register()` getter already exists at line ~1254 (Phase 4e).
4559 // Only the mutators are new here.
4560
4561 /// Overwrite the pending register selector (Phase 6.6b mutator companion to
4562 /// the existing `pending_register()` getter).
4563 pub fn set_pending_register_raw(&mut self, reg: Option<char>) {
4564 self.vim.pending_register = reg;
4565 }
4566
4567 /// Atomically take the pending register, returning `None` afterward.
4568 pub fn take_pending_register_raw(&mut self) -> Option<char> {
4569 self.vim.pending_register.take()
4570 }
4571
4572 // ── Macro recording ───────────────────────────────────────────────────────
4573
4574 /// Return the register currently being recorded into, or `None`.
4575 pub fn recording_macro(&self) -> Option<char> {
4576 self.vim.recording_macro
4577 }
4578
4579 /// Overwrite the recording-macro target register.
4580 pub fn set_recording_macro(&mut self, reg: Option<char>) {
4581 self.vim.recording_macro = reg;
4582 }
4583
4584 /// Append one input to the in-progress macro recording buffer.
4585 pub fn push_recording_key(&mut self, input: crate::input::Input) {
4586 self.vim.recording_keys.push(input);
4587 }
4588
4589 /// Atomically take the recorded key sequence, leaving an empty vec.
4590 pub fn take_recording_keys(&mut self) -> Vec<crate::input::Input> {
4591 std::mem::take(&mut self.vim.recording_keys)
4592 }
4593
4594 /// Overwrite the recording-keys buffer (e.g. to seed from a register).
4595 pub fn set_recording_keys(&mut self, keys: Vec<crate::input::Input>) {
4596 self.vim.recording_keys = keys;
4597 }
4598
4599 /// Return the number of keys currently in the recording buffer.
4600 /// Useful for integration tests that verify macro-recording bookkeeping
4601 /// without draining the buffer via [`take_recording_keys`].
4602 pub fn recording_keys_len(&self) -> usize {
4603 self.vim.recording_keys.len()
4604 }
4605
4606 // ── Macro replay flag ─────────────────────────────────────────────────────
4607
4608 /// `true` while `@reg` macro replay is running (suppresses re-recording).
4609 pub fn is_replaying_macro_raw(&self) -> bool {
4610 self.vim.replaying_macro
4611 }
4612
4613 /// Set or clear the macro-replay-in-progress flag.
4614 pub fn set_replaying_macro_raw(&mut self, v: bool) {
4615 self.vim.replaying_macro = v;
4616 }
4617
4618 // ── Last macro register ───────────────────────────────────────────────────
4619
4620 /// Return the register of the most recently played macro (`@@` source).
4621 pub fn last_macro(&self) -> Option<char> {
4622 self.vim.last_macro
4623 }
4624
4625 /// Overwrite the last-played-macro register.
4626 pub fn set_last_macro(&mut self, reg: Option<char>) {
4627 self.vim.last_macro = reg;
4628 }
4629
4630 // ── Last insert position ──────────────────────────────────────────────────
4631
4632 /// Return the cursor position when Insert mode was last exited (for `gi`).
4633 pub fn last_insert_pos(&self) -> Option<(usize, usize)> {
4634 self.vim.last_insert_pos
4635 }
4636
4637 /// Overwrite the stored last-insert position.
4638 pub fn set_last_insert_pos(&mut self, pos: Option<(usize, usize)>) {
4639 self.vim.last_insert_pos = pos;
4640 }
4641
4642 // ── Last visual selection ─────────────────────────────────────────────────
4643
4644 /// Return the saved visual selection snapshot for `gv`, or `None`.
4645 pub fn last_visual(&self) -> Option<vim::LastVisual> {
4646 self.vim.last_visual
4647 }
4648
4649 /// Overwrite the saved visual selection snapshot.
4650 pub fn set_last_visual(&mut self, snap: Option<vim::LastVisual>) {
4651 self.vim.last_visual = snap;
4652 }
4653
4654 // ── Viewport-pinned flag ──────────────────────────────────────────────────
4655
4656 /// `true` when `zz`/`zt`/`zb` pinned the viewport this step (suppresses
4657 /// the end-of-step scrolloff pass).
4658 pub fn viewport_pinned(&self) -> bool {
4659 self.vim.viewport_pinned
4660 }
4661
4662 /// Set or clear the viewport-pinned flag.
4663 pub fn set_viewport_pinned(&mut self, v: bool) {
4664 self.vim.viewport_pinned = v;
4665 }
4666
4667 // ── Insert pending register (Ctrl-R wait) ─────────────────────────────────
4668
4669 /// `true` while waiting for the register-name key after `Ctrl-R` in
4670 /// Insert mode.
4671 pub fn insert_pending_register(&self) -> bool {
4672 self.vim.insert_pending_register
4673 }
4674
4675 /// Set or clear the `Ctrl-R` register-wait flag.
4676 pub fn set_insert_pending_register(&mut self, v: bool) {
4677 self.vim.insert_pending_register = v;
4678 }
4679
4680 // ── Change-mark start ─────────────────────────────────────────────────────
4681
4682 /// Return the stashed `[` mark start for a Change operation, or `None`.
4683 pub fn change_mark_start(&self) -> Option<(usize, usize)> {
4684 self.vim.change_mark_start
4685 }
4686
4687 /// Atomically take the change-mark start, leaving `None`.
4688 pub fn take_change_mark_start(&mut self) -> Option<(usize, usize)> {
4689 self.vim.change_mark_start.take()
4690 }
4691
4692 /// Overwrite the change-mark start.
4693 pub fn set_change_mark_start(&mut self, pos: Option<(usize, usize)>) {
4694 self.vim.change_mark_start = pos;
4695 }
4696
4697 // ── Timeout tracking ──────────────────────────────────────────────────────
4698
4699 /// Return the wall-clock `Instant` of the last keystroke.
4700 pub fn last_input_at(&self) -> Option<std::time::Instant> {
4701 self.vim.last_input_at
4702 }
4703
4704 /// Overwrite the wall-clock last-input timestamp.
4705 pub fn set_last_input_at(&mut self, t: Option<std::time::Instant>) {
4706 self.vim.last_input_at = t;
4707 }
4708
4709 /// Return the `Host::now()` duration at the last keystroke.
4710 pub fn last_input_host_at(&self) -> Option<core::time::Duration> {
4711 self.vim.last_input_host_at
4712 }
4713
4714 /// Overwrite the host-clock last-input timestamp.
4715 pub fn set_last_input_host_at(&mut self, d: Option<core::time::Duration>) {
4716 self.vim.last_input_host_at = d;
4717 }
4718
4719 // ── Search prompt ──────────────────────────────────────────────────────────
4720
4721 /// Borrow the live search prompt, or `None` when not in search-prompt mode.
4722 pub fn search_prompt_state(&self) -> Option<&vim::SearchPrompt> {
4723 self.vim.search_prompt.as_ref()
4724 }
4725
4726 /// Borrow the live search prompt mutably.
4727 pub fn search_prompt_state_mut(&mut self) -> Option<&mut vim::SearchPrompt> {
4728 self.vim.search_prompt.as_mut()
4729 }
4730
4731 /// Atomically take the search prompt, leaving `None`.
4732 pub fn take_search_prompt_state(&mut self) -> Option<vim::SearchPrompt> {
4733 self.vim.search_prompt.take()
4734 }
4735
4736 /// Install a new search prompt (entering search-prompt mode).
4737 pub fn set_search_prompt_state(&mut self, prompt: Option<vim::SearchPrompt>) {
4738 self.vim.search_prompt = prompt;
4739 }
4740
4741 // ── Last search pattern / direction ───────────────────────────────────────
4742 // Note: `last_search_forward()` getter already exists at line ~1909.
4743 // `set_last_search()` combined mutator exists at line ~1918.
4744 // Only new / complementary accessors are added here.
4745
4746 /// Return the most recently committed search pattern, or `None`.
4747 pub fn last_search_pattern(&self) -> Option<&str> {
4748 self.vim.last_search.as_deref()
4749 }
4750
4751 /// Overwrite the stored last-search pattern without changing direction
4752 /// (use the existing `set_last_search` for the combined update).
4753 pub fn set_last_search_pattern_only(&mut self, pattern: Option<String>) {
4754 self.vim.last_search = pattern;
4755 }
4756
4757 /// Overwrite only the last-search direction flag.
4758 pub fn set_last_search_forward_only(&mut self, forward: bool) {
4759 self.vim.last_search_forward = forward;
4760 }
4761
4762 // ── Search history ────────────────────────────────────────────────────────
4763
4764 /// Borrow the committed search-pattern history (oldest first).
4765 pub fn search_history(&self) -> &[String] {
4766 &self.vim.search_history
4767 }
4768
4769 /// Borrow the search history mutably (e.g. to push a new entry).
4770 pub fn search_history_mut(&mut self) -> &mut Vec<String> {
4771 &mut self.vim.search_history
4772 }
4773
4774 /// Return the current search-history navigation cursor index.
4775 pub fn search_history_cursor(&self) -> Option<usize> {
4776 self.vim.search_history_cursor
4777 }
4778
4779 /// Overwrite the search-history navigation cursor.
4780 pub fn set_search_history_cursor(&mut self, idx: Option<usize>) {
4781 self.vim.search_history_cursor = idx;
4782 }
4783
4784 // ── Jump lists ────────────────────────────────────────────────────────────
4785
4786 /// Borrow the back half of the jump list (entries Ctrl-o pops from).
4787 pub fn jump_back_list(&self) -> &[(usize, usize)] {
4788 &self.vim.jump_back
4789 }
4790
4791 /// Borrow the back jump list mutably (push / pop).
4792 pub fn jump_back_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4793 &mut self.vim.jump_back
4794 }
4795
4796 /// Borrow the forward half of the jump list (entries Ctrl-i pops from).
4797 pub fn jump_fwd_list(&self) -> &[(usize, usize)] {
4798 &self.vim.jump_fwd
4799 }
4800
4801 /// Borrow the forward jump list mutably (push / pop / clear).
4802 pub fn jump_fwd_list_mut(&mut self) -> &mut Vec<(usize, usize)> {
4803 &mut self.vim.jump_fwd
4804 }
4805
4806 // ── Phase 6.6c: search + jump helpers (public Editor API) ───────────────
4807 //
4808 // `push_search_pattern`, `push_jump`, `record_search_history`, and
4809 // `walk_search_history` are public `Editor` methods so that `hjkl-vim`'s
4810 // search-prompt and normal-mode FSM can call them via the public API.
4811
4812 /// Compile `pattern` into a regex and install it as the active search
4813 /// pattern. Respects `:set ignorecase` / `:set smartcase`. An empty or
4814 /// invalid pattern clears the highlight without raising an error.
4815 pub fn push_search_pattern(&mut self, pattern: &str) {
4816 let compiled = if pattern.is_empty() {
4817 None
4818 } else {
4819 let case_insensitive = self.settings().ignore_case
4820 && !(self.settings().smartcase && pattern.chars().any(|c| c.is_uppercase()));
4821 let translated = crate::search::vim_to_rust_regex(pattern);
4822 let effective: std::borrow::Cow<'_, str> = if case_insensitive {
4823 std::borrow::Cow::Owned(format!("(?i){translated}"))
4824 } else {
4825 std::borrow::Cow::Owned(translated)
4826 };
4827 regex::Regex::new(&effective).ok()
4828 };
4829 let wrap = self.settings().wrapscan;
4830 self.set_search_pattern(compiled);
4831 self.search_state_mut().wrap_around = wrap;
4832 }
4833
4834 /// Record a pre-jump cursor position onto the back jumplist. Called
4835 /// before any "big jump" motion (`gg`/`G`, `%`, `*`/`#`, `n`/`N`,
4836 /// committed `/` or `?`, …). Branching off the history clears the
4837 /// forward half, matching vim's "redo-is-lost" semantics.
4838 pub fn push_jump(&mut self, from: (usize, usize)) {
4839 self.vim.jump_back.push(from);
4840 if self.vim.jump_back.len() > vim::JUMPLIST_MAX {
4841 self.vim.jump_back.remove(0);
4842 }
4843 self.vim.jump_fwd.clear();
4844 }
4845
4846 /// Push `pattern` onto the committed search history. Skips if the
4847 /// most recent entry already matches (consecutive dedupe) and trims
4848 /// the oldest entries beyond the history cap.
4849 pub fn record_search_history(&mut self, pattern: &str) {
4850 if pattern.is_empty() {
4851 return;
4852 }
4853 if self.vim.search_history.last().map(String::as_str) == Some(pattern) {
4854 return;
4855 }
4856 self.vim.search_history.push(pattern.to_string());
4857 let len = self.vim.search_history.len();
4858 if len > vim::SEARCH_HISTORY_MAX {
4859 self.vim
4860 .search_history
4861 .drain(0..len - vim::SEARCH_HISTORY_MAX);
4862 }
4863 }
4864
4865 /// Walk the search-prompt history by `dir` steps. `dir = -1` moves
4866 /// toward older entries (Ctrl-P / Up); `dir = 1` toward newer ones
4867 /// (Ctrl-N / Down). Stops at the ends; does nothing if there is no
4868 /// active search prompt.
4869 pub fn walk_search_history(&mut self, dir: isize) {
4870 if self.vim.search_history.is_empty() || self.vim.search_prompt.is_none() {
4871 return;
4872 }
4873 let len = self.vim.search_history.len();
4874 let next_idx = match (self.vim.search_history_cursor, dir) {
4875 (None, -1) => Some(len - 1),
4876 (None, 1) => return,
4877 (Some(i), -1) => i.checked_sub(1),
4878 (Some(i), 1) if i + 1 < len => Some(i + 1),
4879 _ => None,
4880 };
4881 let Some(idx) = next_idx else {
4882 return;
4883 };
4884 self.vim.search_history_cursor = Some(idx);
4885 let text = self.vim.search_history[idx].clone();
4886 if let Some(prompt) = self.vim.search_prompt.as_mut() {
4887 prompt.cursor = text.chars().count();
4888 prompt.text = text.clone();
4889 }
4890 self.push_search_pattern(&text);
4891 }
4892
4893 // ── Phase 6.6d: pre/post FSM bookkeeping ────────────────────────────────
4894 //
4895 // `begin_step` and `end_step` are the bookkeeping prelude/epilogue that
4896 // `hjkl_vim::dispatch_input` wraps around its per-mode FSM dispatch.
4897
4898 /// Pre-dispatch bookkeeping that must run before every per-mode FSM step.
4899 ///
4900 /// Call this at the start of every step; pass the returned
4901 /// [`StepBookkeeping`] to [`end_step`] after the FSM body finishes.
4902 ///
4903 /// Returns `Ok(bk)` when the caller should proceed with FSM dispatch.
4904 /// Returns `Err(consumed)` when the prelude itself handled the input
4905 /// (macro-stop chord); in that case skip the FSM body and do NOT call
4906 /// `end_step` — the macro-stop path is a true short-circuit with no
4907 /// epilogue needed.
4908 ///
4909 /// This method does NOT handle the search-prompt intercept — callers
4910 /// must check `search_prompt_state().is_some()` before calling `begin_step`
4911 /// and dispatch to the search-prompt FSM body directly.
4912 pub fn begin_step(&mut self, input: Input) -> Result<StepBookkeeping, bool> {
4913 use crate::input::Key;
4914 use vim::{Mode, Pending};
4915 // ── Timestamps ───────────────────────────────────────────────────────
4916 // Phase 7f: sync buffer before motion handlers see it.
4917 self.sync_buffer_content_from_textarea();
4918 // `:set timeoutlen` chord-timeout handling.
4919 let now = std::time::Instant::now();
4920 let host_now = self.host.now();
4921 let timed_out = match self.vim.last_input_host_at {
4922 Some(prev) => host_now.saturating_sub(prev) > self.settings.timeout_len,
4923 None => false,
4924 };
4925 if timed_out {
4926 let chord_in_flight = !matches!(self.vim.pending, Pending::None)
4927 || self.vim.count != 0
4928 || self.vim.pending_register.is_some()
4929 || self.vim.insert_pending_register;
4930 if chord_in_flight {
4931 self.vim.clear_pending_prefix();
4932 }
4933 }
4934 self.vim.last_input_at = Some(now);
4935 self.vim.last_input_host_at = Some(host_now);
4936 // ── Macro-stop: bare `q` outside Insert ends the recording ───────────
4937 if self.vim.recording_macro.is_some()
4938 && !self.vim.replaying_macro
4939 && matches!(self.vim.pending, Pending::None)
4940 && self.vim.mode != Mode::Insert
4941 && input.key == Key::Char('q')
4942 && !input.ctrl
4943 && !input.alt
4944 {
4945 let reg = self.vim.recording_macro.take().unwrap();
4946 let keys = std::mem::take(&mut self.vim.recording_keys);
4947 let text = crate::input::encode_macro(&keys);
4948 self.set_named_register_text(reg.to_ascii_lowercase(), text);
4949 return Err(true);
4950 }
4951 // ── Snapshots for epilogue ────────────────────────────────────────────
4952 let pending_was_macro_chord = matches!(
4953 self.vim.pending,
4954 Pending::RecordMacroTarget | Pending::PlayMacroTarget { .. }
4955 );
4956 let was_insert = self.vim.mode == Mode::Insert;
4957 let pre_visual_snapshot = match self.vim.mode {
4958 Mode::Visual => Some(vim::LastVisual {
4959 mode: Mode::Visual,
4960 anchor: self.vim.visual_anchor,
4961 cursor: self.cursor(),
4962 block_vcol: 0,
4963 }),
4964 Mode::VisualLine => Some(vim::LastVisual {
4965 mode: Mode::VisualLine,
4966 anchor: (self.vim.visual_line_anchor, 0),
4967 cursor: self.cursor(),
4968 block_vcol: 0,
4969 }),
4970 Mode::VisualBlock => Some(vim::LastVisual {
4971 mode: Mode::VisualBlock,
4972 anchor: self.vim.block_anchor,
4973 cursor: self.cursor(),
4974 block_vcol: self.vim.block_vcol,
4975 }),
4976 _ => None,
4977 };
4978 Ok(StepBookkeeping {
4979 pending_was_macro_chord,
4980 was_insert,
4981 pre_visual_snapshot,
4982 })
4983 }
4984
4985 /// Post-dispatch bookkeeping that must run after every per-mode FSM step.
4986 ///
4987 /// `input` is the same input that was passed to `begin_step`.
4988 /// `bk` is the [`StepBookkeeping`] returned by `begin_step`.
4989 /// `consumed` is the return value of the FSM body; this method returns
4990 /// it after running all epilogue invariants.
4991 ///
4992 /// Must NOT be called when `begin_step` returned `Err(...)`.
4993 pub fn end_step(&mut self, input: Input, bk: StepBookkeeping, consumed: bool) -> bool {
4994 use crate::input::Key;
4995 use vim::{Mode, Pending};
4996 let StepBookkeeping {
4997 pending_was_macro_chord,
4998 was_insert,
4999 pre_visual_snapshot,
5000 } = bk;
5001 // ── Visual-exit: set `<`/`>` marks and stash `last_visual` ───────────
5002 if let Some(snap) = pre_visual_snapshot
5003 && !matches!(
5004 self.vim.mode,
5005 Mode::Visual | Mode::VisualLine | Mode::VisualBlock
5006 )
5007 {
5008 let (lo, hi) = match snap.mode {
5009 Mode::Visual => {
5010 if snap.anchor <= snap.cursor {
5011 (snap.anchor, snap.cursor)
5012 } else {
5013 (snap.cursor, snap.anchor)
5014 }
5015 }
5016 Mode::VisualLine => {
5017 let r_lo = snap.anchor.0.min(snap.cursor.0);
5018 let r_hi = snap.anchor.0.max(snap.cursor.0);
5019 let last_col = self
5020 .buffer()
5021 .lines()
5022 .get(r_hi)
5023 .map(|l| l.chars().count().saturating_sub(1))
5024 .unwrap_or(0);
5025 ((r_lo, 0), (r_hi, last_col))
5026 }
5027 Mode::VisualBlock => {
5028 let (r1, c1) = snap.anchor;
5029 let (r2, c2) = snap.cursor;
5030 ((r1.min(r2), c1.min(c2)), (r1.max(r2), c1.max(c2)))
5031 }
5032 _ => {
5033 if snap.anchor <= snap.cursor {
5034 (snap.anchor, snap.cursor)
5035 } else {
5036 (snap.cursor, snap.anchor)
5037 }
5038 }
5039 };
5040 self.set_mark('<', lo);
5041 self.set_mark('>', hi);
5042 self.vim.last_visual = Some(snap);
5043 }
5044 // ── Ctrl-o one-shot-normal return to Insert ───────────────────────────
5045 if !was_insert
5046 && self.vim.one_shot_normal
5047 && self.vim.mode == Mode::Normal
5048 && matches!(self.vim.pending, Pending::None)
5049 {
5050 self.vim.one_shot_normal = false;
5051 self.vim.mode = Mode::Insert;
5052 }
5053 // ── Content + viewport sync ───────────────────────────────────────────
5054 self.sync_buffer_content_from_textarea();
5055 if !self.vim.viewport_pinned {
5056 self.ensure_cursor_in_scrolloff();
5057 }
5058 self.vim.viewport_pinned = false;
5059 // ── Recorder hook ─────────────────────────────────────────────────────
5060 if self.vim.recording_macro.is_some()
5061 && !self.vim.replaying_macro
5062 && input.key != Key::Char('q')
5063 && !pending_was_macro_chord
5064 {
5065 self.vim.recording_keys.push(input);
5066 }
5067 // ── Phase 6.3: current_mode sync ─────────────────────────────────────
5068 self.vim.current_mode = self.vim.public_mode();
5069 consumed
5070 }
5071
5072 // ── Phase 6.6e: additional public primitives for hjkl-vim::normal ─────────
5073
5074 /// `true` when the editor is in any visual mode (Visual / VisualLine /
5075 /// VisualBlock). Convenience wrapper around `vim_mode()` for hjkl-vim.
5076 pub fn is_visual(&self) -> bool {
5077 matches!(
5078 self.vim.mode,
5079 vim::Mode::Visual | vim::Mode::VisualLine | vim::Mode::VisualBlock
5080 )
5081 }
5082
5083 /// Compute the VisualBlock rectangle corners: `(top_row, bot_row,
5084 /// left_col, right_col)`. Uses `block_anchor` and `block_vcol` (the
5085 /// virtual column, which survives j/k clamping to shorter rows).
5086 ///
5087 /// Promoted in Phase 6.6e so `hjkl-vim::normal` can compute the block
5088 /// extents needed for VisualBlock `I` / `A` / `r` without accessing
5089 /// engine-private helpers.
5090 pub fn visual_block_bounds(&self) -> (usize, usize, usize, usize) {
5091 let (ar, ac) = self.vim.block_anchor;
5092 let (cr, _) = self.cursor();
5093 let cc = self.vim.block_vcol;
5094 let top = ar.min(cr);
5095 let bot = ar.max(cr);
5096 let left = ac.min(cc);
5097 let right = ac.max(cc);
5098 (top, bot, left, right)
5099 }
5100
5101 /// Return the character count (code-point count) of line `row`, or `0`
5102 /// when `row` is out of range. Used by hjkl-vim::normal for VisualBlock
5103 /// I / A column computations.
5104 pub fn line_char_count(&self, row: usize) -> usize {
5105 buf_line_chars(&self.buffer, row)
5106 }
5107
5108 /// Apply operator over `motion` with `count` repetitions. The full
5109 /// vim-quirks path (operator context for `l`, clamping, etc.) is applied.
5110 ///
5111 /// Promoted to the public surface in Phase 6.6e so `hjkl-vim::normal`'s
5112 /// relocated `handle_after_op` can call it directly with a parsed `Motion`
5113 /// without re-entering the engine FSM.
5114 pub fn apply_op_with_motion_direct(
5115 &mut self,
5116 op: crate::vim::Operator,
5117 motion: &crate::vim::Motion,
5118 count: usize,
5119 ) {
5120 vim::apply_op_with_motion(self, op, motion, count);
5121 }
5122
5123 /// `Ctrl-a` / `Ctrl-x` — adjust the number under or after the cursor.
5124 /// `delta = 1` increments; `delta = -1` decrements; larger deltas
5125 /// multiply as in vim's `5<C-a>`. Promoted in Phase 6.6e so
5126 /// `hjkl-vim::normal` can dispatch `Ctrl-a` / `Ctrl-x`.
5127 pub fn adjust_number(&mut self, delta: i64) {
5128 vim::adjust_number(self, delta);
5129 }
5130
5131 /// Open the `/` or `?` search prompt. `forward = true` for `/`,
5132 /// `false` for `?`. Promoted in Phase 6.6e so `hjkl-vim::normal` can
5133 /// dispatch `/` and `?` without re-entering the engine FSM.
5134 pub fn enter_search(&mut self, forward: bool) {
5135 vim::enter_search(self, forward);
5136 }
5137
5138 /// Enter Insert mode at the left edge of a VisualBlock selection for
5139 /// `I`. Moves the cursor to `(top, col)`, resets to Normal internally,
5140 /// then begins an insert session with `InsertReason::BlockEdge`.
5141 ///
5142 /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5143 /// VisualBlock `I` command without accessing engine-private helpers.
5144 pub fn visual_block_insert_at_left(&mut self, top: usize, bot: usize, col: usize) {
5145 self.jump_cursor(top, col);
5146 self.vim.mode = vim::Mode::Normal;
5147 vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5148 }
5149
5150 /// Enter Insert mode at the right edge of a VisualBlock selection for
5151 /// `A`. Moves the cursor to `(top, col)`, resets to Normal internally,
5152 /// then begins an insert session with `InsertReason::BlockEdge`.
5153 ///
5154 /// Promoted in Phase 6.6e so `hjkl-vim::normal` can dispatch the
5155 /// VisualBlock `A` command without accessing engine-private helpers.
5156 pub fn visual_block_append_at_right(&mut self, top: usize, bot: usize, col: usize) {
5157 self.jump_cursor(top, col);
5158 self.vim.mode = vim::Mode::Normal;
5159 vim::begin_insert(self, 1, vim::InsertReason::BlockEdge { top, bot, col });
5160 }
5161
5162 /// Execute a motion (cursor movement), push to the jumplist for big jumps,
5163 /// and update the sticky column. Mirrors the engine FSM's `execute_motion`
5164 /// free function. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5165 pub fn execute_motion(&mut self, motion: crate::vim::Motion, count: usize) {
5166 vim::execute_motion(self, motion, count);
5167 }
5168
5169 /// Update the VisualBlock virtual column after a motion in VisualBlock mode.
5170 /// Horizontal motions sync `block_vcol` to the cursor column; vertical /
5171 /// non-h/l motions leave it alone so the intended column survives clamping
5172 /// to shorter rows. Promoted in Phase 6.6e for `hjkl-vim::normal`.
5173 pub fn update_block_vcol(&mut self, motion: &crate::vim::Motion) {
5174 vim::update_block_vcol(self, motion);
5175 }
5176
5177 /// Apply `op` over the current visual selection (char-wise, linewise, or
5178 /// block). Mirrors the engine's internal `apply_visual_operator` free fn.
5179 /// Promoted in Phase 6.6e for `hjkl-vim::normal`.
5180 pub fn apply_visual_operator(&mut self, op: crate::vim::Operator) {
5181 vim::apply_visual_operator(self, op);
5182 }
5183
5184 /// Replace each character cell in the current VisualBlock selection with
5185 /// `ch`. Mirrors the engine's `block_replace` free fn. Promoted in Phase
5186 /// 6.6e for the VisualBlock `r<ch>` command in `hjkl-vim::normal`.
5187 pub fn replace_block_char(&mut self, ch: char) {
5188 vim::block_replace(self, ch);
5189 }
5190
5191 /// Extend the current visual selection to cover the text object identified
5192 /// by `ch` and `inner`. Maps `ch` to a `TextObject`, resolves its range
5193 /// via `text_object_range`, then updates the visual anchor and cursor.
5194 ///
5195 /// Promoted in Phase 6.6e for the visual-mode `i<ch>` / `a<ch>` commands
5196 /// in `hjkl-vim::normal::handle_visual_text_obj`.
5197 pub fn visual_text_obj_extend(&mut self, ch: char, inner: bool) {
5198 use crate::vim::{Mode, TextObject};
5199 let obj = match ch {
5200 'w' => TextObject::Word { big: false },
5201 'W' => TextObject::Word { big: true },
5202 '"' | '\'' | '`' => TextObject::Quote(ch),
5203 '(' | ')' | 'b' => TextObject::Bracket('('),
5204 '[' | ']' => TextObject::Bracket('['),
5205 '{' | '}' | 'B' => TextObject::Bracket('{'),
5206 '<' | '>' => TextObject::Bracket('<'),
5207 'p' => TextObject::Paragraph,
5208 't' => TextObject::XmlTag,
5209 's' => TextObject::Sentence,
5210 _ => return,
5211 };
5212 let Some((start, end, kind)) = vim::text_object_range(self, obj, inner) else {
5213 return;
5214 };
5215 match kind {
5216 crate::vim::RangeKind::Linewise => {
5217 self.vim.visual_line_anchor = start.0;
5218 self.vim.mode = Mode::VisualLine;
5219 self.vim.current_mode = VimMode::VisualLine;
5220 self.jump_cursor(end.0, 0);
5221 }
5222 _ => {
5223 self.vim.mode = Mode::Visual;
5224 self.vim.current_mode = VimMode::Visual;
5225 self.vim.visual_anchor = (start.0, start.1);
5226 let (er, ec) = vim::retreat_one(self, end);
5227 self.jump_cursor(er, ec);
5228 }
5229 }
5230 }
5231}
5232
5233/// Visual column of the character at `char_col` in `line`, treating `\t`
5234/// as expansion to the next `tab_width` stop and every other char as
5235/// 1 cell wide. Wide-char support (CJK, emoji) is a separate concern —
5236/// the cursor math elsewhere also assumes single-cell chars.
5237fn visual_col_for_char(line: &str, char_col: usize, tab_width: usize) -> usize {
5238 let mut visual = 0usize;
5239 for (i, ch) in line.chars().enumerate() {
5240 if i >= char_col {
5241 break;
5242 }
5243 if ch == '\t' {
5244 visual += tab_width - (visual % tab_width);
5245 } else {
5246 visual += 1;
5247 }
5248 }
5249 visual
5250}