summaryrefslogtreecommitdiff
path: root/core/panes.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/panes.zig')
-rw-r--r--core/panes.zig116
1 files changed, 84 insertions, 32 deletions
diff --git a/core/panes.zig b/core/panes.zig
index d9759d18..032cb4db 100644
--- a/core/panes.zig
+++ b/core/panes.zig
@@ -375,6 +375,13 @@ pub const Pane = struct {
/// it): the rows its top is passed over by, so its cursor stays in
/// sight (Pane.panFor); 0 everywhere else.
pan: u16 = 0,
+ /// Where this view stands in its terminal's history: the row its top
+ /// shows, counted from the history's top; null, on the output, following
+ /// it. Each view's own (views.zig), so the copy a rollback puts back
+ /// holds it, and the digest and the dump: the emulator keeps no view.
+ /// Carried with every other view's through each write of the emulator
+ /// that moves rows (`Scrolled`); one the active area holds is null.
+ scrolled_to: ?usize = null,
/// The view whose step a `.name` prompt is (tagline.draftName): only
/// that view's focus leaving the pane drops it.
prompt_view: u8 = 0,
@@ -597,7 +604,15 @@ 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(@as(i32, @intCast(Term.grid(pane.terminal).offset)) + pane.pan);
+ return pane.surfRow(@as(i32, @intCast(pane.termTop())) + pane.pan);
+ }
+
+ /// The row of its terminal's history (from the top) this view's top
+ /// shows: where it is scrolled to, else the output's first.
+ pub fn termTop(pane: *const Pane) usize {
+ const g = Term.grid(pane.terminal);
+ const output = g.total - g.len;
+ return @min(pane.scrolled_to orelse output, output);
}
/// The line and byte a body row shows from; a ghost row (jj.zig) is no
@@ -637,10 +652,14 @@ pub const Pane = struct {
f.syntax_dirty = true;
}
} else if (pane.addon == null) {
- // the vt scrolls in SHELL rows; convert through the edit buffer
+ // the vt scrolls in SHELL rows; convert through the edit buffer.
+ // The alternate screen has no history (ghostty's scrolls none):
+ // the place on the primary's stays.
const state = pane.terminal orelse return;
- const off: i32 = @intCast(Term.grid(state).offset);
- Term.scroll(state, pane.gridRow(pane.surfRow(off) + delta) - off);
+ if (Term.modes(state).alternate) return;
+ const g = Term.grid(state);
+ const to: i64 = pane.gridRow(pane.surfRow(@intCast(pane.termTop())) + delta);
+ pane.scrolled_to = if (to >= g.total - g.len) null else @intCast(@max(0, to));
}
}
@@ -722,15 +741,16 @@ pub const Pane = struct {
pane.body.cur_col = 0;
} else {
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_row = pane.surfRow(@as(i32, grid.cursor_y) + @as(i32, @intCast(pane.termTop())));
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.
+ /// Record and parse one live pty read and invalidate its motion surface.
+ /// Sticky bottom, as every other terminal does it: a view on the output
+ /// follows it, one scrolled back into the history stays on its row
+ /// (`Scrolled`, every view's).
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;
@@ -738,25 +758,51 @@ pub const Pane = struct {
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;
- // 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);
+ var scrolled: Scrolled = .of(pane, p.live);
+ defer scrolled.put(pane, p.live);
if (!has_positions) {
- _ = Term.feed(state, bytes, &.{}, &.{}, &.{});
- } else if (pane.followRows(state, bytes)) if (paneIdOf(p, pane)) |id| p.setMessage(.at(id), positions_lost);
+ _ = Term.feed(state, bytes, &.{}, &.{}, &.{}, scrolled.places());
+ } else if (pane.followRows(state, bytes, scrolled.places())) if (paneIdOf(p, pane)) |id| p.setMessage(.at(id), positions_lost);
p.shell_rows.markStale(pane);
- if (pane.body.mode == .tty and at_bottom) pane.followOutput();
}
+ /// Every open view's place in its terminal's history (`scrolled_to`:
+ /// the live view's in the pane, each other's in its share, `pv`),
+ /// carried through a write of the emulator that moves rows
+ /// (`Term.feed`, `Term.resize`) and put back after it.
+ const Scrolled = struct {
+ at: [views.max]?usize = @splat(null),
+ /// One stands off the output: else the write carries none, at no
+ /// cost (a terminal's output, read after read).
+ any: bool = false,
+
+ fn of(pane: *const Pane, live: u8) Scrolled {
+ var pl: Scrolled = .{};
+ for (&pl.at, 0..) |*at, k| {
+ if (k == live) at.* = pane.scrolled_to else if (pane.pv_known >> @intCast(k) & 1 != 0) at.* = pane.pv[k].scrolled_to;
+ pl.any = pl.any or at.* != null;
+ }
+ return pl;
+ }
+
+ fn places(pl: *Scrolled) []?usize {
+ return pl.at[0..if (pl.any) pl.at.len else 0];
+ }
+
+ fn put(pl: *const Scrolled, pane: *Pane, live: u8) void {
+ if (!pl.any) return;
+ for (pl.at, 0..) |at, k| {
+ if (k == live) pane.scrolled_to = at else if (pane.pv_known >> @intCast(k) & 1 != 0) pane.pv[k].scrolled_to = at;
+ }
+ }
+ };
+
/// 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 {
+ fn followRows(pane: *Pane, state: *Term.State, bytes: []const u8, places: []?usize) bool {
const Group = struct { overlay: ?*Text.EditBuffer, cursor: *i32, selection: *Text.CharSel };
var groups: [Text.history_max * 2 + 1]Group = undefined;
groups[0] = .{
@@ -852,11 +898,11 @@ pub const Pane = struct {
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);
+ const track = if (n <= track_buf.len) track_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes, places);
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);
+ const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes, places);
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);
+ const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return feedLost(state, bytes, places);
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| {
@@ -867,7 +913,7 @@ pub const Pane = struct {
track[k] = at.grid;
k += 1;
};
- const fed = Term.feed(state, bytes, track, rows_after, erased_after);
+ const fed = Term.feed(state, bytes, track, rows_after, erased_after, places);
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;
@@ -948,8 +994,8 @@ pub const Pane = struct {
return t;
}
- fn feedLost(state: *Term.State, bytes: []const u8) bool {
- _ = Term.feed(state, bytes, &.{}, &.{}, &.{});
+ fn feedLost(state: *Term.State, bytes: []const u8, places: []?usize) bool {
+ _ = Term.feed(state, bytes, &.{}, &.{}, &.{}, places);
return true;
}
@@ -961,14 +1007,17 @@ pub const Pane = struct {
return count;
}
- /// Its terminal's view snapped back onto live output.
+ /// This view of its terminal snapped back onto live output. On the
+ /// alternate screen (no history) the place on the primary's stays, as
+ /// ghostty's own did.
pub fn followOutput(pane: *Pane) void {
const state = pane.terminal orelse return;
- Term.follow(state);
+ if (!Term.modes(state).alternate) pane.scrolled_to = null;
}
- /// Its terminal's grid reflowed to `cols` by `rows`.
- pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void {
+ /// Its terminal's grid reflowed to `cols` by `rows`, every open view's
+ /// place carried through it (`Scrolled`; `live` the live view's index).
+ pub fn resizeGrid(pane: *Pane, live: u8, cols: u16, rows: u16) void {
// A client's speculation does what its replay does (its
// selections) but the grid: it waits for no round trip. Under bytes
// it owes the grid (`touchTerminal`): its input is replayed when
@@ -979,7 +1028,9 @@ pub const Pane = struct {
pane.sel[button].state = .none;
};
if (tape.speculating()) return tape.touchTerminal();
- Term.resize(state, cols, rows);
+ var scrolled: Scrolled = .of(pane, live);
+ defer scrolled.put(pane, live);
+ Term.resize(state, cols, rows, scrolled.places());
}
/// The rows its terminal's top is passed over by in a view whose rect
@@ -993,10 +1044,11 @@ pub const Pane = struct {
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 top = pane.termTop();
+ // The cursor's row in the view; 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);
+ const at: usize = if (row < top) 0 else @min(row - top, pane.rows - 1);
return @intCast(@min(pane.rows - shown, (at + 1) -| shown));
}