From 639a2a7931394742af2a2e6f02006fdfd4dd6052 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:19:12 -0300 Subject: text: a terminal's edit overlay and its undo are the Text's: EditBuffer, Snapshot and history_max move from terminal.zig into Text.zig, as they are, so Text names no terminal type (terminal.zig and panes.zig's ovl name them there, as test/panes.zig does); a move, nothing else --- src/Text.zig | 36 +++++++++++++++++++++++++++--------- src/panes.zig | 2 +- src/terminal.zig | 21 +++------------------ 3 files changed, 31 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/Text.zig b/src/Text.zig index 43a515e1..271269ab 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -14,7 +14,6 @@ const config = @import("config.zig"); const memory = @import("memory.zig"); const Pane = panes.Pane; const File = panes.File; -const terminal = panes.terminal; const Text = @This(); pub const Mode = enum { normal, insert, tty }; @@ -30,6 +29,25 @@ pub const CharSel = struct { explicit: bool = false, }; +/// The owned text standing in for `rows` live terminal rows, beginning at +/// absolute surface row `row`. The emulator grid remains untouched underneath. +pub const EditBuffer = struct { + row: i32 = 0, + rows: i32 = 1, + text: []u8 = &.{}, +}; + +/// One whole-state terminal edit boundary. Null means the pane has not yet +/// materialized an edit buffer; non-null snapshots own their text. +pub const Snapshot = struct { + ovl: ?EditBuffer, + cur_row: i32, + cur_col: i32, + vsel: CharSel, +}; + +pub const history_max = 64; + pub const max_selections = memory.limits.selections; pub const SelRange = struct { @@ -80,9 +98,9 @@ normal: modal.Normal.State = .{}, /// last f/F/t/T motion, for Alt-. repeat find_op: ?modal.Normal.Find = null, find_ch: u21 = 0, -ed_undo: [terminal.history_max]terminal.Snapshot = undefined, +ed_undo: [history_max]Snapshot = undefined, ed_undo_len: usize = 0, -ed_redo: [terminal.history_max]terminal.Snapshot = undefined, +ed_redo: [history_max]Snapshot = undefined, ed_redo_len: usize = 0, /// Set when an edit would have changed characters this text does not own /// (a tag's computed prefix) and was refused; the key that tried it puts @@ -604,10 +622,10 @@ fn unchanged(gpa: std.mem.Allocator, entries: []const Entry, text: []const u8) e return std.mem.eql(u8, window.items, text[lo..hi]); } -fn pushHistory(gpa: std.mem.Allocator, slots: []terminal.Snapshot, len: *usize, value: terminal.Snapshot) void { +fn pushHistory(gpa: std.mem.Allocator, slots: []Snapshot, len: *usize, value: Snapshot) void { if (len.* == slots.len) { if (slots[0].ovl) |overlay| gpa.free(overlay.text); - std.mem.copyForwards(terminal.Snapshot, slots[0 .. slots.len - 1], slots[1..]); + std.mem.copyForwards(Snapshot, slots[0 .. slots.len - 1], slots[1..]); len.* -= 1; } slots[len.*] = value; @@ -617,14 +635,14 @@ fn pushHistory(gpa: std.mem.Allocator, slots: []terminal.Snapshot, len: *usize, /// Record an edit buffer (a terminal's overlay, a tag's own text) as it /// stands before an edit, once per change of it, and forget what could have /// been redone. Null is a buffer not made yet. -pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer) error{OutOfMemory}!void { +pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?EditBuffer) error{OutOfMemory}!void { if (t.ed_undo_len > 0) { const top = t.ed_undo[t.ed_undo_len - 1]; const same = if (top.ovl) |overlay| if (current) |now| overlay.row == now.row and overlay.rows == now.rows and std.mem.eql(u8, overlay.text, now.text) else false else current == null; if (same) return; } - const copy: ?terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = try gpa.dupe(u8, now.text) } else null; + const copy: ?EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = try gpa.dupe(u8, now.text) } else null; pushHistory(gpa, &t.ed_undo, &t.ed_undo_len, .{ .ovl = copy, .cur_row = t.cur_row, .cur_col = t.cur_col, .vsel = t.vsel }); for (t.ed_redo[0..t.ed_redo_len]) |item| if (item.ovl) |overlay| gpa.free(overlay.text); t.ed_redo_len = 0; @@ -634,13 +652,13 @@ pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer) /// other history and the state to return to comes back, its cursor and /// selection already restored here. The caller installs its buffer, which /// it then owns. Null when there is nothing to step to. -pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer, back: bool) error{OutOfMemory}!?terminal.Snapshot { +pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?EditBuffer, back: bool) error{OutOfMemory}!?Snapshot { const from, const from_len, const to, const to_len = if (back) .{ &t.ed_undo, &t.ed_undo_len, &t.ed_redo, &t.ed_redo_len } else .{ &t.ed_redo, &t.ed_redo_len, &t.ed_undo, &t.ed_undo_len }; if (from_len.* == 0) return null; - const copy: ?terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = try gpa.dupe(u8, now.text) } else null; + const copy: ?EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = try gpa.dupe(u8, now.text) } else null; pushHistory(gpa, to, to_len, .{ .ovl = copy, .cur_row = t.cur_row, .cur_col = t.cur_col, .vsel = t.vsel }); from_len.* -= 1; const back_to = from[from_len.*]; diff --git a/src/panes.zig b/src/panes.zig index 99652b04..29a9f051 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -292,7 +292,7 @@ pub const Pane = struct { next_pointer_selection: u64 = 0, /// terminals only: the typed-text buffer standing in for shell rows - ovl: ?terminal.EditBuffer = null, + ovl: ?Text.EditBuffer = null, tty_filter: bool = false, msg: [256]u8 = undefined, msg_len: u16 = 0, diff --git a/src/terminal.zig b/src/terminal.zig index 46790de1..4ee2e9fa 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -16,7 +16,9 @@ const File = panes.File; const ghostty_vt = @import("ghostty-vt"); -pub const history_max = 64; +const EditBuffer = panes.Text.EditBuffer; +const Snapshot = panes.Text.Snapshot; +const history_max = panes.Text.history_max; pub const VtSlot = ghostty_vt.Terminal; /// A cell of an emulator's pages, as `getCell` reads it (src/echo.zig). @@ -416,23 +418,6 @@ fn asGhostRgb(rgb: [3]u8) GColor.RGB { return .{ .r = rgb[0], .g = rgb[1], .b = rgb[2] }; } -/// The owned text standing in for `rows` live terminal rows, beginning at -/// absolute surface row `row`. The emulator grid remains untouched underneath. -pub const EditBuffer = struct { - row: i32 = 0, - rows: i32 = 1, - text: []u8 = &.{}, -}; - -/// One whole-state terminal edit boundary. Null means the pane has not yet -/// materialized an edit buffer; non-null snapshots own their text. -pub const Snapshot = struct { - ovl: ?EditBuffer, - cur_row: i32, - cur_col: i32, - vsel: panes.Text.CharSel, -}; - pub const PendingCommand = struct { bytes: []u8 = &.{}, wait: enum { none, spawn, input } = .none, -- cgit v1.3