summaryrefslogtreecommitdiff
path: root/src/panes.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/panes.zig')
-rw-r--r--src/panes.zig319
1 files changed, 310 insertions, 9 deletions
diff --git a/src/panes.zig b/src/panes.zig
index e1eb7e0e..ed2f3767 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -331,7 +331,7 @@ pub const Pane = struct {
sized_by: u8 = 0,
/// A terminal shown in a view shorter than it (one that does not size
/// it): the rows its top is passed over by, so its cursor stays in
- /// sight (terminal.panFor); 0 everywhere else.
+ /// sight (Pane.panFor); 0 everywhere else.
pan: u16 = 0,
/// The view whose step a `.name` prompt is (tagline.draftName): only
/// that view's focus leaving the pane drops it.
@@ -555,7 +555,7 @@ pub const Pane = struct {
/// current scroll offset: file top line, or the scrollback offset
pub fn scroll(pane: *Pane) i32 {
if (pane.file) |f| return @intCast(f.scroll);
- return pane.surfRow(terminal.gridOffset(pane) + pane.pan);
+ return pane.surfRow(@as(i32, @intCast(Term.grid(pane.terminal).offset)) + pane.pan);
}
pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } {
@@ -592,8 +592,10 @@ pub const Pane = struct {
}
} else if (pane.addon == null) {
// the vt scrolls in SHELL rows; convert through the edit buffer
- const off = terminal.gridOffset(pane);
- terminal.scrollGrid(pane, pane.gridRow(pane.surfRow(off) + delta) - off);
+ if (tape.viewOnly("scroll a terminal")) return;
+ const state = pane.terminal orelse return;
+ const off: i32 = @intCast(Term.grid(state).offset);
+ Term.scroll(state, pane.gridRow(pane.surfRow(off) + delta) - off);
}
}
@@ -674,13 +676,301 @@ pub const Pane = struct {
pane.body.cur_row = pane.scroll();
pane.body.cur_col = 0;
} else {
- const cur = terminal.gridCursor(pane);
- pane.body.cur_row = pane.surfRow(@as(i32, cur.y) + terminal.gridOffset(pane));
- pane.body.cur_col = @intCast(cur.x);
+ const grid = Term.grid(pane.terminal);
+ pane.body.cur_row = pane.surfRow(@as(i32, grid.cursor_y) + @as(i32, @intCast(grid.offset)));
+ pane.body.cur_col = grid.cursor_x;
}
pane.body.cur_pinned = true;
}
+ /// Record and parse one live pty read, invalidate its motion surface,
+ /// and follow it only when the body (possibly parked under a tag edit)
+ /// is raw.
+ pub fn feedOutput(pane: *Pane, p: *Pardes, bytes: []const u8) void {
+ // There are no pty reads at all without an emulator to parse them into.
+ const state = pane.terminal orelse return;
+ if (tape.authorityOnly("a terminal's output")) return;
+ const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or
+ pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or
+ pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pane.pointerRowCount() > 0;
+ // A replica's emulator is what the authority's deltas make it; what
+ // pardes keeps over it moves as the authority's feed told it.
+ if (tape.screenReplica()) {
+ if (has_positions and pane.followRows(state, bytes)) if (paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost);
+ return;
+ }
+ // Sticky bottom, as every other terminal does it: output drags the
+ // viewport down only when the viewport was already on the last row,
+ // so a reader scrolled back into the scrollback stays where they
+ // scrolled.
+ const grid = Term.grid(state);
+ const at_bottom = grid.offset + grid.len >= grid.total;
+ Term.cellPixels(state, p.view().cell_pixels.w, p.view().cell_pixels.h);
+ if (!has_positions) {
+ _ = Term.feed(state, bytes, &.{}, &.{}, &.{});
+ } else if (pane.followRows(state, bytes)) if (paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost);
+ p.shell_rows.markStale(pane);
+ if (pane.body.mode == .tty and at_bottom) pane.followOutput();
+ }
+
+ /// Its terminal fed `bytes`, with every position kept over its rows
+ /// (the edit copy, the cursors and selections, their undo, the
+ /// pointer's rows) moved as the rows move (`Term.Told`). True when one
+ /// could not be kept (no memory for its pin).
+ fn followRows(pane: *Pane, state: *Term.State, bytes: []const u8) bool {
+ const on_replica = tape.screenReplica();
+ const Group = struct { overlay: ?*Text.EditBuffer, cursor: *i32, selection: *Text.CharSel };
+ var groups: [Text.history_max * 2 + 1]Group = undefined;
+ groups[0] = .{
+ .overlay = if (pane.ovl) |*overlay| overlay else null,
+ .cursor = &pane.body.cur_row,
+ .selection = &pane.body.vsel,
+ };
+ var ngroups: usize = 1;
+ for ([_][]Text.Snapshot{ pane.body.ed_undo[0..pane.body.ed_undo_len], pane.body.ed_redo[0..pane.body.ed_redo_len] }) |history| {
+ for (history) |*snapshot| {
+ groups[ngroups] = .{
+ .overlay = if (snapshot.ovl) |*overlay| overlay else null,
+ .cursor = &snapshot.cur_row,
+ .selection = &snapshot.vsel,
+ };
+ ngroups += 1;
+ }
+ }
+ // A position on a row of an edit copy's text stays on it (its line
+ // of the copy, `edit_row`); one below the copy is tracked on the
+ // shell row it stands for.
+ const Position = struct { target: *i32, overlay: ?*Text.EditBuffer, edit_row: ?i32 = null, grid: i32 = -1 };
+ const position = struct {
+ fn of(target: *i32, overlay: ?*Text.EditBuffer) Position {
+ var grid = target.*;
+ if (overlay) |edit| {
+ const lines: i32 = @intCast(modal.lineCount(edit.text));
+ if (grid >= edit.row and grid < edit.row +| lines)
+ return .{ .target = target, .overlay = edit, .edit_row = grid - edit.row };
+ if (grid > edit.row) grid = grid -| lines +| edit.rows;
+ }
+ return .{ .target = target, .overlay = overlay, .grid = grid };
+ }
+ }.of;
+ var spans: [groups.len]*Text.EditBuffer = undefined;
+ var nspans: usize = 0;
+ var positions: [groups.len * 2 + Text.max_selections * 2 + 4]Position = undefined;
+ var npositions: usize = 0;
+ for (groups[0..ngroups]) |group| {
+ if (group.overlay) |overlay| {
+ spans[nspans] = overlay;
+ nspans += 1;
+ }
+ positions[npositions] = position(group.cursor, group.overlay);
+ npositions += 1;
+ if (group.selection.active) {
+ positions[npositions] = position(&group.selection.row, group.overlay);
+ npositions += 1;
+ }
+ }
+ const overlay = groups[0].overlay;
+ // Where the live pane's own selections sit in `positions`: one whose
+ // row the program erased goes (below), as the edited copy does.
+ const vsel_at: ?usize = if (pane.body.vsel.active) 1 else null;
+ const sels_at = npositions;
+ var extra: [Text.max_selections * 2 + 2]*i32 = undefined;
+ var nextra: usize = 0;
+ for (pane.body.sels[0..pane.body.nsel]) |*selection| {
+ extra[nextra] = &selection.row;
+ extra[nextra + 1] = &selection.arow;
+ nextra += 2;
+ }
+ if (pane.body.last_edit) |*at| {
+ extra[nextra] = &at.row;
+ nextra += 1;
+ }
+ if (pane.look_at) |*at| {
+ extra[nextra] = &at.row;
+ nextra += 1;
+ }
+ for (extra[0..nextra]) |target| {
+ positions[npositions] = position(target, overlay);
+ npositions += 1;
+ }
+ var failed = false;
+ const pointer_positions = pane.gpa.alloc(Position, pane.pointerRowCount()) catch null;
+ defer if (pointer_positions) |held| pane.gpa.free(held);
+ if (pointer_positions) |held| {
+ var index: usize = 0;
+ for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| {
+ for (rows) |*row| {
+ held[index] = position(&row.row, if (row.raw_terminal) null else overlay);
+ index += 1;
+ }
+ };
+ } else failed = true;
+ const pointers: []Position = pointer_positions orelse &.{};
+ // The rows to track, in the order told: each copy's first and last
+ // row, then the positions, then the pointer's rows. On the stack,
+ // as the local path never allocated here but for the pointer's rows.
+ const n = 2 * nspans + npositions + pointers.len;
+ var track_buf: [512]i32 = undefined;
+ var rows_buf: [512]i32 = undefined;
+ var erased_buf: [512]bool = undefined;
+ // Out of memory for them, the read is parsed and nothing moves.
+ const track = if (n <= track_buf.len) track_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes);
+ defer if (n > track_buf.len) pane.gpa.free(track);
+ const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes);
+ defer if (n > rows_buf.len) pane.gpa.free(rows_after);
+ const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return feedLost(state, bytes);
+ defer if (n > erased_buf.len) pane.gpa.free(erased_after);
+ var k: usize = 0;
+ for (spans[0..nspans]) |span| for ([_]i32{ span.row, span.row +| (span.rows - 1) }) |row| {
+ track[k] = row;
+ k += 1;
+ };
+ for ([_][]Position{ positions[0..npositions], pointers }) |batch| for (batch) |at| {
+ track[k] = at.grid;
+ k += 1;
+ };
+ // A replica's emulator is fed by its authority's deltas: it is told
+ // what the authority's feed told.
+ const fed: Term.Told = if (on_replica) told: {
+ @memset(rows_after, 0);
+ @memset(erased_after, false);
+ break :told .{ .rows = rows_after, .erased = erased_after, .cleared = false, .alive = false, .failed = false };
+ } else Term.feed(state, bytes, track, rows_after, erased_after);
+ const after = tape.value(.positions, keepTold, .{Term.Told{ .rows = fed.rows, .erased = fed.erased, .cleared = fed.cleared, .alive = fed.alive, .failed = failed or fed.failed }});
+ if (after.rows.len != n or after.erased.len != n) return false; // a replica its authority's states apart (diverged)
+ failed = after.failed;
+ const at_position = 2 * nspans;
+ const at_pointer = at_position + npositions;
+ var moved = false;
+ var pointer_rows_lost = !after.alive or pointer_positions == null;
+ for (0..pointers.len) |i| if (after.erased[at_pointer + i]) {
+ pointer_rows_lost = true;
+ };
+ // The program cleared the screen, or the rows the live copy stood on
+ // are gone (pruned, a reset): the copy goes with them, as what the
+ // program drew next. Kept, the emulator would move its rows to the
+ // top of what is left, over the program's new text.
+ var copy_lost = after.cleared or !after.alive;
+ for (spans[0..nspans], 0..) |span, i| {
+ if (span == overlay and after.erased[2 * i]) copy_lost = true;
+ const start = after.rows[2 * i];
+ const rows = @max(1, after.rows[2 * i + 1] -| start +| 1);
+ moved = moved or start != span.row or rows != span.rows;
+ span.row = start;
+ span.rows = rows;
+ }
+ // The rule above for the copy holds for the selections: one whose
+ // row is gone (ED 3 erased its history row) goes too, rather than
+ // being clamped to the top.
+ const erased = after.erased[at_position..];
+ const vsel_lost = if (vsel_at) |i|
+ erased[i] or (copy_lost and positions[i].edit_row != null)
+ else
+ false;
+ var sels_keep: [Text.max_selections]bool = undefined;
+ for (0..pane.body.nsel) |i| sels_keep[i] = !(erased[sels_at + 2 * i] or erased[sels_at + 2 * i + 1]);
+ for ([_][]Position{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |at, i| {
+ var row = after.rows[i];
+ if (at.overlay) |edit| {
+ if (at.edit_row) |relative| {
+ row = edit.row +| relative;
+ } else if (row > edit.row) {
+ row = if (row < edit.row +| edit.rows)
+ edit.row
+ else
+ row -| edit.rows +| @as(i32, @intCast(modal.lineCount(edit.text)));
+ }
+ }
+ moved = moved or row != at.target.*;
+ at.target.* = row;
+ };
+ if (pointer_rows_lost) for (0..pane.sel.len) |button| {
+ pane.clearPointerSelection(button);
+ pane.sel[button].state = .none;
+ };
+ if (vsel_lost) {
+ pane.body.vsel.active = false;
+ moved = true;
+ }
+ if (pane.body.nsel > 0) {
+ var kept: u16 = 0;
+ for (0..pane.body.nsel) |i| if (sels_keep[i]) {
+ pane.body.sels[kept] = pane.body.sels[i];
+ kept += 1;
+ };
+ if (kept != pane.body.nsel) {
+ moved = true;
+ if (kept == 0) pane.body.dropSels() else pane.body.nsel = kept;
+ }
+ }
+ if (copy_lost) if (pane.ovl) |copy| {
+ pane.gpa.free(copy.text);
+ pane.ovl = null;
+ moved = true;
+ };
+ if (moved) pane.nsel_snap = 0; // Saved regex-preview offsets name the old flat text.
+ return failed;
+ }
+
+ fn keepTold(t: Term.Told) Term.Told {
+ return t;
+ }
+
+ fn feedLost(state: *Term.State, bytes: []const u8) bool {
+ if (!tape.screenReplica()) _ = Term.feed(state, bytes, &.{}, &.{}, &.{});
+ return true;
+ }
+
+ fn pointerRowCount(pane: *const Pane) usize {
+ var count: usize = 0;
+ for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| {
+ count += rows.len;
+ };
+ return count;
+ }
+
+ /// Its terminal's view snapped back onto live output.
+ pub fn followOutput(pane: *Pane) void {
+ if (tape.viewOnly("follow a terminal")) return;
+ const state = pane.terminal orelse return;
+ Term.follow(state);
+ }
+
+ /// Its terminal's grid reflowed to `cols` by `rows`: on a screen-state
+ /// replica by its authority (the next delta says), on such an authority
+ /// once the step's unit is out (`resizeDeferred`).
+ pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void {
+ if (tape.authorityOnly("resize a terminal")) return;
+ const state = pane.terminal orelse return;
+ if (cols != pane.cols) for (0..pane.sel.len) |button| {
+ pane.clearPointerSelection(button);
+ pane.sel[button].state = .none;
+ };
+ if (tape.screenReplica()) return;
+ // A client typing at another size than the last typist's read
+ // history rows its replay held as holes, and the rows came
+ // renumbered (2026-10-30): the authority's waits for the unit.
+ Term.resize(state, cols, rows, tape.screenAuthority());
+ }
+
+ /// The rows its terminal's top is passed over by in a view whose rect
+ /// `r` is shorter than it, one that does not size it (Pardes.sizes): as
+ /// many as keep its cursor's row in sight, as tmux pans a bigger window
+ /// in a smaller client. 0 in the view that sizes it, and in one tall
+ /// enough.
+ pub fn panFor(pane: *const Pane, p: *const Pardes, r: pardes.Rect) u16 {
+ if (pane.sized_by == p.live or pane.collapsed or !pane.isTerminal()) return 0;
+ const shown = r.h -| pane.tag_rows;
+ if (shown == 0 or shown >= pane.rows) return 0;
+ const state = pane.terminal orelse return 0;
+ const grid = Term.grid(state);
+ // The cursor's row in the viewport; past its end (scrolled back),
+ // its last row.
+ const row = grid.total - grid.len + grid.cursor_y;
+ const at: usize = if (row < grid.offset) 0 else @min(row - grid.offset, pane.rows - 1);
+ return @intCast(@min(pane.rows - shown, (at + 1) -| shown));
+ }
+
/// `run <serial> <word>` or `exit <serial> <N|?>` in the log.
/// The whole line, one record as `msg` carries its text: its control
/// characters, a newline too, become spaces (events.pushLog). A new pane is
@@ -708,8 +998,8 @@ pub const Pane = struct {
var buf: [16]u8 = undefined;
const code = if (status) |n| std.fmt.bufPrint(&buf, "{d}", .{n}) catch unreachable else "?"; // an exit status fits 16
var said: pardes.Kind.Buf(32) = .{};
- const lead = if (panes.terminal.gridCursor(pane).x != 0) "\r\n" else "";
- panes.terminal.feedOutput(p, pane, said.print("{s}exit {s}\r\n", .{ lead, code }));
+ const lead = if (Term.grid(pane.terminal).cursor_x != 0) "\r\n" else "";
+ pane.feedOutput(p, said.print("{s}exit {s}\r\n", .{ lead, code }));
pane.noteRun(p, "exit", code);
p.needs_frame = true;
}
@@ -734,6 +1024,17 @@ pub const terminal = @import("term/terminal.zig");
pub const Term = @import("Term.zig");
+/// What a pane says when a position kept over its terminal's rows could
+/// not follow the output (no memory for its pin), so it was reset.
+pub const positions_lost = "terminal edit position reset: out of memory";
+
+/// The sizes steps gave terminals (`Pane.resizeGrid`), made: between
+/// units, on an authority with screen-state replicas (replica.Peers.unit).
+pub fn resizeDeferred(p: *Pardes) void {
+ for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (Term.resizeDeferred(state))
+ p.shell_rows.markStale(pane);
+}
+
/// A pane with nothing in it yet, `cols` by `rows`: what a file, a
/// directory, a kind's pane or a terminal is made from.
pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {