Skip to main content

justerm_core/
lib.rs

1// The crate-level docs and the compiled usage example both live in README.md,
2// pulled in here so the published crates.io front page and this doctest are one
3// source: the `cargo test` doc pass compiles the README's `rust` block, so the
4// published usage snippet cannot drift from the real API (#483, and the #473
5// rule that a shipped usage snippet must compile against the real types).
6#![doc = include_str!("../README.md")]
7
8mod cell;
9mod color;
10mod cursor;
11mod damage;
12mod event;
13mod grapheme;
14mod grid;
15mod input;
16mod logical;
17mod search;
18mod selection;
19mod serialize;
20mod term;
21
22pub use cell::{Cell, CellFlags};
23pub use color::Color;
24pub use cursor::{Cursor, CursorShape, Pen};
25pub use damage::{LineDamage, ScrollOp, TermDamage};
26pub use event::TermEvent;
27pub use grid::{Grid, Row};
28pub use input::{
29    Key, KeyAction, KeyEvent, KeypadKey, Modifiers, MouseAction, MouseButton, MouseEvent,
30    MouseEvents,
31};
32pub use logical::LogicalLine;
33pub use search::{Match, SearchOptions, is_valid_regex};
34pub use selection::{SelectionSpan, SelectionType, Side};
35pub use serialize::{
36    CELL_RECORD_LEN, DecodeError, Frame, FrameKind, MarkerId, MarkerKind, MarkerPosition, Overlay,
37    Span, WIRE_VERSION, decode, encode, encode_cell_record, encode_color,
38};
39
40pub use term::{
41    CommandLine, DEFAULT_WORD_SEPARATORS, Hyperlink, MAX_COLUMNS, MAX_COMMAND_TEXT, MAX_MARKERS,
42    MAX_ROWS, MIN_COLUMNS, MarkerEntry, MarkerIndex, Term, TrackedId,
43};
44
45use vte::Parser;
46
47/// The terminal engine: pairs the `vte` parser with our state model.
48///
49/// `Parser` and `Term` are kept as separate fields because `Parser::advance`
50/// borrows both the parser and the performer mutably at once — a single struct
51/// owning both could not satisfy the borrow checker.
52pub struct Engine {
53    parser: Parser,
54    term: Term,
55}
56
57impl Engine {
58    /// A blank engine with a `cols` × `rows` screen and a default scrollback cap.
59    ///
60    /// `cols` is widened to [`MIN_COLUMNS`] — a narrower screen cannot represent a
61    /// width-2 glyph, so the engine clamps rather than accepting a size it would
62    /// have three different answers for (#547).
63    pub fn new(cols: usize, rows: usize) -> Self {
64        Engine {
65            parser: Parser::new(),
66            term: Term::new(cols, rows),
67        }
68    }
69
70    /// Like [`Engine::new`] but with an explicit scrollback line limit. `cols` is
71    /// clamped to [`MIN_COLUMNS`] the same way.
72    pub fn with_scrollback(cols: usize, rows: usize, scrollback_limit: usize) -> Self {
73        Engine {
74            parser: Parser::new(),
75            term: Term::with_scrollback(cols, rows, scrollback_limit),
76        }
77    }
78
79    /// Push a slice of VT bytes. The caller owns the PTY/SSH/socket I/O — the
80    /// engine only consumes the bytes it is handed.
81    pub fn feed(&mut self, bytes: &[u8]) {
82        self.parser.advance(&mut self.term, bytes);
83    }
84
85    /// Resize the screen to `cols` x `rows`. Rows that scroll off the top enter
86    /// scrollback; the whole screen is damaged.
87    ///
88    /// **The primary screen reflows; the alternate screen does not (#567).** On the
89    /// primary, soft-wrapped logical lines are re-split at the new width — scrollback
90    /// included, since it is one buffer with the screen — so a long line keeps its tail
91    /// instead of being truncated. Reflow is *not* gated on DECAWM: the wrap flag records
92    /// that a row continues into the next one, which stays true after a re-split, and
93    /// re-reading a momentary mode at resize time would decide the fate of history written
94    /// under the opposite setting. The alt screen is re-fit only — rows are dropped or added
95    /// to reach the new size and nothing re-wraps, because a full-screen application places
96    /// its own lines and re-wrapping them would change what it drew.
97    ///
98    /// **What a consumer must redo afterwards.** Query-derived state is *invalidated* and
99    /// user-authored state is *re-anchored*: search highlights are dropped (re-run the
100    /// search at the new width — a reflow moves match coordinates and can change the match
101    /// set), while the selection is carried to its new coordinates for you.
102    ///
103    /// `cols` is widened to [`MIN_COLUMNS`] **silently**: a `resize(1, rows)` during
104    /// a pane drag yields a two-column screen with no error. Read the resulting
105    /// width back from [`Engine::grid`] or the frame header rather than assuming the
106    /// value passed here, and size the PTY from that same width (#547).
107    pub fn resize(&mut self, cols: usize, rows: usize) {
108        self.term.resize(cols, rows);
109    }
110
111    /// The current screen grid.
112    pub fn grid(&self) -> &Grid {
113        self.term.grid()
114    }
115
116    /// The current cursor (position, pending-wrap, pen).
117    pub fn cursor(&self) -> &Cursor {
118        self.term.cursor()
119    }
120
121    /// Whether bracketed-paste mode (DEC ?2004) is enabled. A consumer's input
122    /// encoder reads this to decide whether to wrap pasted text in markers.
123    pub fn bracketed_paste(&self) -> bool {
124        self.term.bracketed_paste()
125    }
126
127    /// Encode a key event to the bytes an application expects, honouring the
128    /// engine's cursor-key mode (DECCKM). The inverse of [`Engine::feed`] — the
129    /// consumer hands a decoded key event and writes the bytes to its PTY.
130    /// Returns `None` for a key with no defined encoding.
131    pub fn encode_key(&self, ev: KeyEvent) -> Option<Vec<u8>> {
132        self.term.encode_key(ev)
133    }
134
135    /// Encode a mouse event using the engine's active tracking mode + encoding.
136    /// Returns `None` when mouse reporting is off, or when the event is filtered
137    /// out by the mode (e.g. a bare move while only ?1000 is set).
138    pub fn encode_mouse(&self, ev: MouseEvent) -> Option<Vec<u8>> {
139        self.term.encode_mouse(ev)
140    }
141
142    /// Encode pasted text — wrapped in bracketed-paste markers when ?2004 is on,
143    /// raw otherwise.
144    pub fn encode_paste(&self, text: &str) -> Vec<u8> {
145        self.term.encode_paste(text)
146    }
147
148    /// Encode a focus change (`CSI I` on focus-in, `CSI O` on focus-out), or
149    /// `None` when focus reporting (?1004) is off.
150    pub fn encode_focus(&self, focused: bool) -> Option<Vec<u8>> {
151        self.term.encode_focus(focused)
152    }
153
154    /// Take the consumer events accumulated since the last drain (title / bell /
155    /// cwd — see [`TermEvent`]), emptying the queue. The pull counterpart to a
156    /// callback: poll this alongside [`Engine::frame`].
157    pub fn drain_events(&mut self) -> Vec<TermEvent> {
158        self.term.drain_events()
159    }
160
161    /// Take the reply bytes the engine produced for app queries (DA / DSR /
162    /// DECRQM) since the last drain — the consumer writes them straight back to
163    /// the PTY. The inbound-query counterpart to [`Engine::drain_events`].
164    pub fn drain_replies(&mut self) -> Vec<u8> {
165        self.term.drain_replies()
166    }
167
168    /// The OSC 8 hyperlink **URI** at **screen** `(row, col)` — the live grid, same
169    /// coordinates as [`Engine::grid`]'s `cell(row, col)` — or `None` if that cell
170    /// carries no declared link.
171    ///
172    /// **One call, not two, since #628.** This returned a `NonZeroU32` index that a
173    /// second method resolved against a buffer-wide pool; the pool is gone (it was never
174    /// reclaimed, and nothing interned across opens that a shared `Arc` does not), so
175    /// there is no index left to hand out.
176    ///
177    /// **Owned, not borrowed** — a `&str` into the row's map would be tied to `&Engine`,
178    /// so a hover handler could not keep it across the next [`Engine::feed`]. Measured:
179    /// the borrow reads at 0.75 ns but cannot be held at all, and the caller's workaround
180    /// (copying the string) costs 62.6 ns against this handle's 17.9 ns. See
181    /// [`Hyperlink`].
182    ///
183    /// Do **not** confuse this with a decoded `Span`'s `links`, which is a *frame-local*
184    /// index into that frame's `link_table` and belongs to the wire, not to the engine.
185    /// The old two-call form invited exactly that mix-up and its doc-comment recommended
186    /// it: the two index spaces coincide only when a frame carries a single link.
187    pub fn link_at(&self, row: usize, col: usize) -> Option<Hyperlink> {
188        self.term.screen_link_at(row, col)
189    }
190
191    /// The underline colour (SGR 58, #520) at **screen** `(row, col)` — same
192    /// coordinates as [`Engine::grid`]'s `cell(row, col)`. A theme-agnostic
193    /// [`Color`] reference; [`Color::Default`] means the underline follows the
194    /// glyph's foreground (the common case, and what a cell with no SGR 58 returns).
195    /// Like the hyperlink, the colour rides a per-row side table, not the 12-byte
196    /// [`Cell`] (#520).
197    pub fn underline_color_at(&self, row: usize, col: usize) -> Color {
198        self.term.screen_underline_color_at(row, col)
199    }
200
201    /// The OSC 8 hyperlink **URI** at **viewport** `(row, col)` — the visible window
202    /// including scrollback at the current scroll, same coordinates as
203    /// [`Engine::viewport_line`] — or `None`. Mirror of [`Engine::link_at`], including
204    /// its #628 note about the vanished index.
205    pub fn viewport_link_at(&self, row: usize, col: usize) -> Option<Hyperlink> {
206        self.term.viewport_link_at(row, col)
207    }
208
209    /// Number of lines currently held in scrollback history.
210    pub fn scrollback_len(&self) -> usize {
211        self.term.scrollback_len()
212    }
213
214    /// Whether the app has an open **synchronized-output** block (DEC `?2026`):
215    /// it has asked that the next frame of output be painted atomically. The
216    /// engine only *reports* this — **the consumer owns the paint-hold and the
217    /// spec-mandated timeout** (a buggy app that never closes the block must not
218    /// freeze the screen forever, and the engine has no clock). Poll this after
219    /// `feed`; while it is `true`, defer applying frames, and apply once it
220    /// clears (or your own timeout fires). (#73)
221    pub fn synchronized_output(&self) -> bool {
222        self.term.synchronized_output()
223    }
224
225    /// Whether the app enabled color-scheme-update notifications (DEC `?2031`).
226    /// The engine is theme-agnostic — it never knows the scheme. The consumer
227    /// answers a [`TermEvent::ColorSchemeQuery`] (from `?996`) and, when its
228    /// scheme changes *and* this is `true`, sends an unsolicited notification, in
229    /// both cases by calling [`Engine::report_color_scheme`] (#85).
230    pub fn color_scheme_updates(&self) -> bool {
231        self.term.color_scheme_updates()
232    }
233
234    /// Report the current light/dark color scheme to the app as `CSI ? 997 ; 1 n`
235    /// (dark) / `; 2 n` (light), drained via [`Engine::drain_replies`]. Call this
236    /// to answer a [`TermEvent::ColorSchemeQuery`], or — guarded by
237    /// [`Engine::color_scheme_updates`] — when the scheme changes. The engine only
238    /// formats the bit you pass; it stores no scheme (#85).
239    pub fn report_color_scheme(&mut self, dark: bool) {
240        self.term.report_color_scheme(dark);
241    }
242
243    /// Answer an OSC 11 `QueryBackground` event (#122): the consumer hands back
244    /// the current background spec (it owns the palette) and the engine queues
245    /// the OSC 11 reply for `drain_replies`. Theme-agnostic — the engine never
246    /// knows the colour, only formats the envelope.
247    pub fn report_background(&mut self, spec: &str) {
248        self.term.report_background(spec);
249    }
250
251    /// Answer an OSC 10 `QueryForeground` event (#122): queue the OSC 10 reply
252    /// from the consumer-supplied spec. Theme-agnostic envelope-only.
253    pub fn report_foreground(&mut self, spec: &str) {
254        self.term.report_foreground(spec);
255    }
256
257    /// Answer an OSC 4 `QueryPaletteColor` event (#122): queue the OSC 4 reply for
258    /// `index` from the consumer-supplied spec. Theme-agnostic envelope-only.
259    pub fn report_palette_color(&mut self, index: u8, spec: &str) {
260        self.term.report_palette_color(index, spec);
261    }
262
263    /// Whether the app enabled **win32-input-mode** (DEC `?9001`): it asked for
264    /// keys as raw Windows key-records. The engine only tracks the flag — encoding
265    /// the records (`CSI Vk;Sc;Uc;Kd;Cs;Rc _`) is a non-goal (raw passthrough, no
266    /// semantic conversion), so [`Engine::encode_key`] is unchanged. A ConPTY
267    /// consumer reads this to decide whether to emit the records itself (#86).
268    pub fn win32_input_mode(&self) -> bool {
269        self.term.win32_input_mode()
270    }
271
272    /// What changed since the last [`Engine::reset_damage`] — line ranges each
273    /// with a changed column span (see ADR-0003).
274    pub fn damage(&self) -> TermDamage {
275        self.term.damage()
276    }
277
278    /// Build a serializable [`Frame`] of the current diff — the damaged spans
279    /// (or every row, when `Full`), the recorded scroll op, and a frame-local
280    /// grapheme side-table. Pass it to [`encode`] for the wire (see #6). Reading
281    /// a frame does not clear damage; call [`Engine::reset_damage`] on ack.
282    pub fn frame(&self) -> Frame {
283        self.term.frame()
284    }
285
286    /// Clear accumulated damage after a frame is applied (the consumer's ack).
287    pub fn reset_damage(&mut self) {
288        self.term.reset_damage();
289    }
290
291    /// Force the next [`Engine::frame`] to be a `Full` frame (every row), even if
292    /// little changed. The use case is **reattach / late subscribe**: a renderer
293    /// that connects after output has already been parsed needs the whole current
294    /// viewport once, then incremental diffs. Marks the screen fully damaged; the
295    /// next `frame()` reports `FrameKind::Full`.
296    pub fn mark_fully_damaged(&mut self) {
297        self.term.mark_fully_damaged();
298    }
299
300    /// The first-class scroll recorded since the last [`Engine::reset_damage`],
301    /// if any — lets the renderer shift rows instead of redrawing them.
302    ///
303    /// **`count` is capped at the scroll region's own height (#661).** Repeated
304    /// scrolls of one region accumulate into a single op between acks, and a flood
305    /// accumulates far past the region: 32 KB of newlines in one [`Engine::feed`] is
306    /// enough. Shifting a region by more than its height already moves every source
307    /// row outside it, so the surplus names nothing a consumer can act on — while it
308    /// did overflow the `i16` this value rides on the wire and arrive as a scroll in
309    /// the *opposite* direction. Suppressed entirely while the viewport is scrolled
310    /// up, since a content scroll must not shift a frozen view.
311    pub fn scroll_delta(&self) -> Option<ScrollOp> {
312        self.term.scroll_delta()
313    }
314
315    /// The cells of visible row `i` (0..rows) at the current scroll position.
316    pub fn viewport_line(&self, i: usize) -> &[Cell] {
317        self.term.viewport_line(i)
318    }
319
320    /// Scroll the viewport up by `n` lines into scrollback history.
321    pub fn scroll_up(&mut self, n: usize) {
322        self.term.scroll_up(n);
323    }
324
325    /// Scroll the viewport down by `n` lines toward the live screen.
326    pub fn scroll_down(&mut self, n: usize) {
327        self.term.scroll_down(n);
328    }
329
330    /// Jump the viewport back to the live screen (follow the bottom).
331    pub fn scroll_to_bottom(&mut self) {
332        self.term.scroll_to_bottom();
333    }
334
335    /// Begin a selection of `ty` at viewport cell `(row, col)`, on `side` of the
336    /// cell. Coordinates are viewport-relative (what a mouse event carries).
337    pub fn selection_begin(&mut self, row: usize, col: usize, side: Side, ty: SelectionType) {
338        self.term.selection_begin(row, col, side, ty);
339    }
340
341    /// Extend the live selection to viewport cell `(row, col)`, on `side`.
342    pub fn selection_extend(&mut self, row: usize, col: usize, side: Side) {
343        self.term.selection_extend(row, col, side);
344    }
345
346    /// Replace the characters that end a word for [`SelectionType::Word`] — consumer
347    /// policy injected into a core mechanism (ADR-0017). Defaults to
348    /// [`DEFAULT_WORD_SEPARATORS`]. `' '` is forced in; see [`Term::set_word_separators`]
349    /// for why that floor is load-bearing rather than defensive.
350    pub fn set_word_separators(&mut self, separators: &str) {
351        self.term.set_word_separators(separators);
352    }
353
354    /// The word-boundary set currently in force (including the forced `' '`).
355    pub fn word_separators(&self) -> &str {
356        self.term.word_separators()
357    }
358
359    /// Clear the selection.
360    pub fn selection_clear(&mut self) {
361        self.term.selection_clear();
362    }
363
364    /// The selection projected onto the viewport: one inclusive-column span per
365    /// visible row, for the renderer to highlight. Empty when nothing is
366    /// selected or the selection is fully scrolled off-screen.
367    ///
368    /// **A span never ends inside a wide glyph** (#454). An endpoint landing on
369    /// half of a width-2 pair takes the whole pair, so a highlight cannot split
370    /// a CJK glyph down the middle — which also means a span may be one column
371    /// wider than the columns the caller's gesture named. On a `Block`
372    /// selection that widening is per row, so the rectangle's rows can differ
373    /// in width. [`selection_text`](Self::selection_text) widens identically;
374    /// the two never disagree.
375    pub fn selection_range(&self) -> Vec<SelectionSpan> {
376        self.term.selection_range()
377    }
378
379    /// The selected text for copy (respects scrollback), or `None` if no
380    /// selection.
381    ///
382    /// Widened onto whole wide-glyph pairs exactly as
383    /// [`selection_range`](Self::selection_range) is (#454) — a spacer extracts
384    /// as nothing, so a range ending inside a pair would copy text the
385    /// highlight does not show.
386    pub fn selection_text(&self) -> Option<String> {
387        self.term.selection_text()
388    }
389
390    /// Literal search over the grid + scrollback, returning every match in
391    /// absolute buffer coordinates (top-to-bottom). Smart-case: a query with no
392    /// uppercase matches case-insensitively. The consumer drives next/prev by
393    /// walking the returned `Vec` and calling [`Engine::scroll_to_match`].
394    pub fn search(&self, query: &str) -> Vec<Match> {
395        self.term.search(query)
396    }
397
398    /// Search with explicit [`SearchOptions`] — regex, whole-word, and a case-sensitivity override
399    /// beyond the literal + smart-case [`search`](Self::search) (#314).
400    pub fn search_with(&self, query: &str, opts: SearchOptions) -> Vec<Match> {
401        self.term.search_with(query, opts)
402    }
403
404    /// The viewport's logical lines (#113/ADR-0017): each soft-wrap-joined line's
405    /// text plus a per-char map to its viewport `(row, col)`. The buffer-wide
406    /// mechanism for consumer-side URL detection — the consumer runs its own
407    /// regex / `new URL()` over the text and maps matches back through `cells`.
408    /// Also serves the a11y mirror (#119).
409    pub fn viewport_logical_lines(&self) -> Vec<LogicalLine> {
410        self.term.viewport_logical_lines()
411    }
412
413    /// The whole buffer (scrollback + screen) as one text document for a
414    /// screen-reader accessible view (#150) — soft-wrap-joined, wide-spacers
415    /// skipped, trailing blanks trimmed at the logical end, `\n` between logical
416    /// lines. A query seam the consumer summons (frame mode: over IPC, like
417    /// [`selection_text`](Self::selection_text)); no wire-format change. On the
418    /// alt screen only the alt buffer is shown.
419    ///
420    /// **This is the document [`CommandLine::line`] indexes, which makes that last
421    /// sentence a pairing obligation rather than a detail (#743):** ask both in the
422    /// same breath and keep them together, because a document line is meaningless
423    /// against a document sampled at another instant — and while the alt screen is up
424    /// the two queries are about different buffers entirely. See
425    /// [`Engine::command_lines`].
426    pub fn accessible_text(&self) -> String {
427        self.term.accessible_text()
428    }
429
430    /// Scroll the viewport so `m` is visible (next/prev navigation: the consumer
431    /// picks the match, the engine scrolls to it).
432    pub fn scroll_to_match(&mut self, m: &Match) {
433        self.term.search_scroll_to(m);
434    }
435
436    /// The match projected onto the viewport as inclusive-column spans per
437    /// visible row, for the renderer to highlight.
438    ///
439    /// **A span never ends inside a wide glyph** (#454), the same widening
440    /// [`selection_range`](Self::selection_range) applies. It matters more here,
441    /// because a `Match` may be one the *caller* assembled: an out-of-range
442    /// column is bounded onto the row's last cell, which is a trailing spacer
443    /// whenever the row ends in a wide glyph — so without the widening a
444    /// highlight could be that glyph's right half alone.
445    pub fn match_spans(&self, m: &Match) -> Vec<SelectionSpan> {
446        self.term.match_spans(m)
447    }
448
449    /// Set the search highlights the frame should carry (#108). The
450    /// consumer owns match navigation, so it hands the set to highlight back
451    /// here; [`Engine::frame`] then projects them onto the viewport overlay
452    /// alongside the selection. An empty vec clears the highlights.
453    pub fn set_search_highlights(&mut self, matches: Vec<Match>) {
454        self.term.set_search_highlights(matches);
455    }
456
457    /// Designate which member of the held highlight set is the *active* match
458    /// (#428) — the one next/prev navigation currently points at (that choice is
459    /// the consumer's policy). [`Engine::frame`] projects it into the overlay's
460    /// `active_match` group; it also stays in `matches`, and the renderer's
461    /// highlight ranking resolves the overlap (#424). `None` or an out-of-range
462    /// index projects nothing. Passing a new set to
463    /// [`set_search_highlights`](Self::set_search_highlights) resets the
464    /// designation, so re-designate after every hand-over.
465    pub fn set_active_search_highlight(&mut self, index: Option<usize>) {
466        self.term.set_active_search_highlight(index);
467    }
468
469    /// Designate the *active* match by its absolute span (#436), independent of
470    /// the held highlight set — the past-cap path. A backend that caps its
471    /// hand-over (the documented 1000, xterm's `highlightLimit`) can still give
472    /// the current match its active emphasis: xterm builds its active
473    /// decoration from the found result *outside* the capped list, and this is
474    /// that model. The span projects through the same wrap-aware viewport math
475    /// as any match; past the cap it paints the ACTIVE colour only (no plain
476    /// highlight underneath — honest about the cap). `None` clears. Same
477    /// lifecycle as the index form: reset on every
478    /// [`set_search_highlights`](Self::set_search_highlights) hand-over and on
479    /// any coordinate-shifting invalidation (eviction, region scroll, reflow,
480    /// alt-screen swaps), so re-designate after each hand-over.
481    pub fn set_active_search_match(&mut self, m: Option<Match>) {
482        self.term.set_active_search_match(m);
483    }
484
485    /// Register a decoration marker at viewport `row`, returning its stable id
486    /// (#118). The marker anchors the content currently on that row and tracks
487    /// it through scroll/eviction/reflow; [`Engine::frame`] reports its viewport
488    /// position while visible. Use the id to remove it or to match the
489    /// `TermEvent::MarkerDisposed` fired when its line leaves the buffer.
490    ///
491    /// A buffer holds at most [`MAX_MARKERS`] live markers (#721) — the population is
492    /// also grown by the *stream*, through OSC 133 command marks, so it is bounded.
493    /// Past the cap the **oldest** marker is retired and announced through the same
494    /// `MarkerDisposed` event, so a consumer that already handles disposal needs no new
495    /// handling; a consumer that ignores it can leave a decoration bound to a dead id.
496    pub fn add_marker(&mut self, row: usize) -> MarkerId {
497        self.term.add_marker(row)
498    }
499
500    /// Remove a marker by id (#118), firing `TermEvent::MarkerDisposed`. A no-op
501    /// for an unknown or already-disposed id.
502    pub fn remove_marker(&mut self, id: MarkerId) {
503        self.term.remove_marker(id);
504    }
505
506    /// Track absolute buffer `(line, col)`, returning a stable id (#691): the
507    /// engine keeps the position on the content that is there now, through
508    /// scrollback eviction, region scrolls and reflow.
509    ///
510    /// This is what an absolute coordinate held *outside* the engine needs to stay
511    /// meaningful — a search anchor carrying an emphasis across a re-search is the
512    /// case it exists for. The engine renumbers this space (evicting the oldest
513    /// history line shifts every index down by one), and it renumbers it in the
514    /// consumer's absence, so a remembered `Match` silently comes to name
515    /// different text.
516    ///
517    /// Mechanism only: which position is worth remembering, and what to do once it
518    /// is gone, stay with the consumer (ADR-0017). Release it with
519    /// [`Engine::untrack_point`] — the engine cannot know when you are done.
520    ///
521    /// **The line is maintained; the column is carried, not tracked.** In-row edits
522    /// (ICH / DCH) shift cells past a tracked column without moving it, so a point
523    /// on text that was pushed sideways names the wrong cell in that row. No
524    /// reference maintains a column here either — xterm's markers carry none at
525    /// all, and ghostty's pins are untouched by its `insertChars`/`deleteChars` —
526    /// so this is the convergent behaviour rather than an omission.
527    pub fn track_point(&mut self, line: usize, col: usize) -> TrackedId {
528        self.term.track_point(line, col)
529    }
530
531    /// Where the point registered as `id` sits now, in the **active** screen's
532    /// coordinates — or `None` (#691).
533    ///
534    /// `None` covers three cases, and a caller does not need to tell them apart:
535    /// the content has left the buffer, the id is unknown or released, or the point
536    /// belongs to *the other screen*. The last one is not a limitation but the only
537    /// honest answer: the primary grid and the alt grid occupy the **same** absolute
538    /// indices, so a number alone cannot say which screen it means. All three say
539    /// *do not move anything on account of this point*.
540    ///
541    /// An out-of-range coordinate is clamped rather than rejected, at both ends
542    /// (ADR-0026 D2/D3): the line into the buffer's range, the column to the grid
543    /// width. That bound is applied here, at the read; a coordinate that was never
544    /// in range to begin with is also **resolved by a reflow** (it maps to the top
545    /// of the buffer), so "bounded once" holds for the site, not for the value.
546    pub fn tracked_point(&self, id: TrackedId) -> Option<(usize, usize)> {
547        self.term.tracked_point(id)
548    }
549
550    /// Release a tracked point (#691). A no-op for an unknown or already-released
551    /// id.
552    pub fn untrack_point(&mut self, id: TrackedId) {
553        self.term.untrack_point(id);
554    }
555
556    /// The OSC 133 shell-integration command marks in buffer order — `(id,
557    /// absolute line, kind)` (#158). Excludes plain `add_marker` decorations.
558    /// The consumer pairs prompt/command/finished marks to drive prompt-to-prompt
559    /// navigation and command/exit announcements (#160); the engine only parses
560    /// the `133;A/B/C/D` sequences and anchors the marks.
561    ///
562    /// **The answer is instantaneous — it describes the buffer it was asked of, and
563    /// nothing on it dates it (#742). Re-ask; never keep it and never rebase it.**
564    /// The lines move on *both* of the axes [`MarkerIndex`] carries a scalar for:
565    /// scrollback eviction shifts every mark by the same amount, and a top-anchored
566    /// `DECSTBM` region shifts the marks below its margin once per output line — the
567    /// second inside a single [`Engine::feed`], with no resize anywhere.
568    ///
569    /// **Why this is not shaped like its sibling.** [`Engine::marker_index`] carries a
570    /// basis and an epoch because a consumer *must* hold its answer: it feeds an
571    /// overview ruler that has to be current in every frame, and re-pulling per frame
572    /// is the `O(M)`-per-frame payload ADR-0020 R3 exists to forbid. This query is
573    /// consumed when a user acts, so re-asking **is** the natural act — and here a
574    /// re-ask always answers, because this population's frame of reference never
575    /// changes. That is the property the sibling lacks, and the reason it needed the
576    /// epoch rather than a reason this one does: an alt switch is one of the four
577    /// moves that epoch announces.
578    ///
579    /// **The lines are `[scrollback ++ primary]`, always — including while the alt
580    /// screen is up.** They do not name the *active* buffer. The two buffers occupy the
581    /// same absolute indices, so one integer from here and the same integer from
582    /// [`Engine::marker_index`] name different content, and neither tuple nor struct
583    /// says which. [`Engine::tracked_point`] meets that ambiguity and answers `None`
584    /// rather than a number (ADR-0026 D2/D3); it can, because it is asked about *one*
585    /// point of unknown origin. This query enumerates a population whose screen is
586    /// fixed by definition, so it answers — and states the screen here instead.
587    ///
588    /// Consequently an empty answer means every mark was disposed and can mean nothing
589    /// else, where `marker_index`'s silence is ambiguous between that and *"you are on
590    /// the other screen"*.
591    ///
592    /// **A mark also dies when a whole row is blanked where it stands (#750).** Until
593    /// then the only deaths were the buffer *moving* — eviction, a region rotate, a
594    /// reflow — and a `clear` left every mark on the screen alive over blank rows. `ED`
595    /// now retires the marks on each whole row it blanks, through the same
596    /// `TermEvent::MarkerDisposed` a consumer already handles, so this query going empty
597    /// after a `clear` is the ordinary meaning above and not a new one. **`EL` and `ECH`
598    /// deliberately do not**, whatever they blank: a line editor redraws its input line
599    /// with `\r ESC[K` on every keystroke, and the `CommandStart` of the command being
600    /// typed is on that row.
601    pub fn command_marks(&self) -> Vec<(MarkerId, usize, MarkerKind)> {
602        self.term.command_marks()
603    }
604
605    /// The executed shell commands recovered from OSC-133 marks, in buffer order
606    /// (#166) — the query behind screen-reader command navigation. Each
607    /// [`CommandLine`] carries the typed command text (prompt/output excluded via
608    /// the captured columns), its jump line (CommandStart), and the exit code.
609    /// This is a full-buffer query, wired to the frame-mode consumer over IPC like
610    /// [`Engine::accessible_text`]; the web side has no scrollback cells to derive
611    /// it (ADR-0017 — buffer-wide text is core's).
612    ///
613    /// **The text and the exit are frozen when the stream reveals them; only the line
614    /// is derived (#750).** [`CommandLine::command`] is captured at the `133;C` that
615    /// closes the command — the instant it is complete and on screen — and
616    /// [`CommandLine::exit`] is written down when `133;D` is parsed. Neither is
617    /// recoverable afterwards: re-reading the text through the recorded columns names
618    /// whatever *now* occupies those cells, which a plain overwrite, `ICH`, `DCH` and an
619    /// erase all arrange, and an exit code is in no cell at any time. A capture is
620    /// bounded at [`MAX_COMMAND_TEXT`] `char`s, truncated at a `char` boundary, for the
621    /// reason [`MAX_MARKERS`] exists: the stream chooses the distance between `B` and
622    /// `C`. [`CommandLine::line`] stays derived, because it is the half the anchor
623    /// fixups already maintain.
624    ///
625    /// **The answer is instantaneous — it describes the buffer it was asked of, and
626    /// nothing on it dates it (#743). Re-ask; never keep it past the document it
627    /// indexes, and never rebase it.** Same discharge as [`Engine::command_marks`] and
628    /// for the same two reasons (ADR-0029 D3): the clock is a user action, so the ask
629    /// *is* the act; and this population's frame of reference never flips, so a re-ask
630    /// always answers. Absence means the command is gone **or** that its output has not
631    /// started yet — both of which the next ask resolves. What absence never means is
632    /// *"you are on the other screen"*, which is the meaning no re-ask could undo.
633    ///
634    /// **Do not rebase by [`MarkerIndex::evicted_total`].** That dates the *absolute*
635    /// space. [`CommandLine::line`] is a **document** line, and the two spaces move
636    /// apart in both directions: an eviction that pops a soft-wrap continuation row
637    /// moves the absolute lines and not this one, and flipping a row's wrap bit — which
638    /// ordinary output does — moves this one while the absolute lines and both of
639    /// `MarkerIndex`'s scalars stay put. They agree most of the time, which is what
640    /// makes rebasing look correct right up until it silently is not.
641    ///
642    /// **Ask [`Engine::accessible_text`] in the same breath, and only on the primary
643    /// screen.** The lines index that document; while the alt screen is up it returns
644    /// the *alt* document instead, and these lines are indices into the primary one. If
645    /// the alt screen is taller than the held index — a full-screen TUI, which is the
646    /// normal case — the index still **resolves**, onto unrelated content, so a bounds
647    /// check does not save a caller here. The query keeps answering on the alt screen
648    /// deliberately: emptying it would give absence the one meaning a re-ask cannot
649    /// recover from, which is what the discharge above rests on. Pairing the two is the
650    /// caller's, and this is where it is said.
651    pub fn command_lines(&self) -> Vec<CommandLine> {
652        self.term.command_lines()
653    }
654
655    /// Every live marker of the active buffer with its **absolute** buffer line, plus
656    /// the basis that says how long the answer stays usable (#490).
657    ///
658    /// The pull half of the marker surface. It shares [`Engine::command_lines`]'s
659    /// *shape* — the consumer asks once and keeps the answer, rather than being handed
660    /// every live marker inside every frame, which is `O(M)` payload per frame for a
661    /// quantity unrelated to what changed (ADR-0020 R3). It does **not** share its
662    /// coordinate: only the lines *here* are buffer-absolute and rebasable by the
663    /// `evicted_total` delta. [`CommandLine::line`] is a **document** line over
664    /// [`Engine::accessible_text`], where soft-wrapped rows collapse — eviction moves it
665    /// by an amount no scalar on this surface expresses, so it is an answer to keep only
666    /// as long as the buffer it was asked of.
667    ///
668    /// Ask again when [`MarkerIndex::epoch`] differs from the one you hold. Drop an
669    /// entry when its `TermEvent::MarkerDisposed` arrives, and append one when
670    /// `TermEvent::MarkerCreated` does — neither deliberately moves the epoch, so
671    /// neither costs a re-pull. **Append it on the instant the event carries, not on the
672    /// newest frame's**: a `feed` can create a marker and then evict, and those are two
673    /// different origins (#737).
674    ///
675    /// **Adopt a birth only into the generation it names (#741).** The event carries this
676    /// pull's whole triple — line, basis, [`MarkerIndex::epoch`] — because the basis dates
677    /// only a *uniform* move. A reflow or a region rotate moves markers individually, so a
678    /// line dated to the generation before one is not stale by a delta; it is an answer
679    /// about a different buffer, and the re-pull the epoch already forces is what supplies
680    /// the marker instead. Compare generations for **equality**: the counter wraps.
681    ///
682    /// **Draining before you read the frame is then a cost preference, not a correctness
683    /// one.** Reading the frame first leaves `marker_count` one ahead of an index that has
684    /// not been told yet, so a consumer comparing the two spends an `O(M)` re-pull
685    /// reconciling a fact this event delivered at `O(1)`. Placement does not depend on the
686    /// order, on either axis.
687    pub fn marker_index(&self) -> MarkerIndex {
688        self.term.marker_index()
689    }
690}