diff options
Diffstat (limited to 'src/views.zig')
| -rw-r--r-- | src/views.zig | 555 |
1 files changed, 220 insertions, 335 deletions
diff --git a/src/views.zig b/src/views.zig index 169bef63..0c96c792 100644 --- a/src/views.zig +++ b/src/views.zig @@ -3,22 +3,24 @@ //! scroll over the panes every view shares: their text, terminals, tags, //! registers and messages. //! -//! The core still sees one view, the live one, in the fields it always had: -//! a view's share of the session (`fields`) and of each pane (`PaneView`) is -//! copied out when another view goes live (`park`) and back when it is live -//! again (`unpark`). No line of the core reads a view through an -//! indirection, and one view never parks: today's single client runs -//! exactly as before. +//! A view's share of the session is `Pardes.View`, one of `Pardes.views`, +//! and the live one is `views[view]` (`Pardes.view()`): going into another +//! is an index. Each view's share of a pane (`PaneView`) is still swapped: +//! the live view's is in the pane's own fields, the others' in `Pane.pv` +//! (copied out when another view goes live, `park`, and back, `unpark`), +//! so the ~3,000 references to a pane's cursor, mode, scroll and geometry +//! read it as they always did. A pane closing leaves every view's layout +//! at once (`gone`). //! -//! A parked view is in no digest (Pardes.replica's `parked`, Pane's `pv`): -//! a slot is written whenever its view goes out, so the live view's slot is -//! stale by design, and a parked view that diverged shows when it is live -//! again. The pump draws the other views by going into each and back -//! (`drawOthers`), which leaves every digested field as it was; a replica -//! that draws nothing skips it. +//! The views are in the digest; the panes' shares of the views not live +//! (Pane's `pv`) are not: a pane's slot is written whenever its view goes +//! out, so the live view's is stale by design, and a share that diverged +//! shows when its view is live again. A view drawn out of turn (`drawOne`, +//! `within`) is put back after exactly as it was. const std = @import("std"); const pardes = @import("pardes.zig"); const Pardes = pardes.Pardes; +const View = Pardes.View; const Pane = pardes.Pane; const Text = pardes.panes.Text; const File = pardes.panes.File; @@ -28,26 +30,6 @@ const MAX_PANES = pardes.MAX_PANES; /// Views at once: a window or terminal each. pub const max = 4; -/// The session's fields that are each view's own: its screen and layout, -/// its focus, its pointer and gestures, its jumps, its key prefixes, and -/// what drawing it keeps (its frame and its timings). -pub const fields = [_][:0]const u8{ - "screen_w", "screen_h", "cell_pixels", "row_metrics", - "ncol", "cols", "col_tags", "rects", - "col_x", "col_w", "active", - "jumps", - "active_column", - "frontend", - "drag", "mouse_grab", "window_focused", "focus_told", - "hover_col", "hover_row", "pointer_raw_col", "pointer_raw_row", - "pointer_body_hit", "pointer_tag_hit", "raw_hover_intent", "pointer_inside", - "last_click", "click_as", "chord_hold", "wheel_guard", - "prefix", "register", "window_pick", - "leader_keys", "leader_n", "header", - "header_drag", "header_sweep", "header_scroll", "header_top", - "chord_arg", "clip_pending", -}; - /// A pane's fields that are each view's own: where it is in the view's /// layout, which of its texts has the keyboard, how far it is scrolled, and /// what the view's step measured of it (its tag's rows, its bands). @@ -56,8 +38,6 @@ const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_ /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; -pub const View = Part(Pardes, &fields); - /// Named field by field, not taken from Pane's type: a pane holds its own. pub const PaneView = struct { focus: Pane.Focus = .body, @@ -78,120 +58,80 @@ pub const PaneView = struct { cols: u16 = 1, rows: u16 = 1, - /// In a dump (`Dumped`): what a pane's step works out again. Its `view` + /// In a dump (`Shares`): what a pane's step works out again. Its `view` /// is dumped: the step after `unpark` reads it before it syncs. pub const replica = .{ .derived = .{ "notices", "pan" } }; }; -/// A view not live: its share of the session, and the serial of each pane -/// slot when it went out, by which the panes closed since leave its layout. -pub const Parked = struct { - view: View, - serials: [MAX_PANES]u32 = @splat(0), -}; - -/// What a session's dump keeps of a view's share: all of it but what -/// drawing keeps (Pardes.replica's local fields: its frame and its -/// timings). -const kept = blk: { - @setEvalBranchQuota(100_000); - var names: []const [:0]const u8 = &.{}; - for (fields) |name| { - const local = for (Pardes.replica.local) |l| { - if (std.mem.eql(u8, l, name)) break true; - } else false; - if (!local) names = names ++ &[_][:0]const u8{name}; - } - const out: [names.len][:0]const u8 = names[0..names.len].*; - break :blk out; -}; - -/// A view not live as a session's dump keeps it (dump.zig): its share of -/// the session, its slots' serials, and its share of each pane. The live -/// view is the core's own fields; its slot is stale, and no part of it. -pub const Dumped = struct { +/// The panes' shares of a view not live, as a session's dump keeps them +/// (dump.zig): the live view's are the panes' own fields. +pub const Shares = struct { k: u8, - view: Part(Pardes, &kept), - serials: [MAX_PANES]u32 = @splat(0), panes: []const Share = &.{}, pub const Share = struct { id: u16, pv: PaneView }; }; +/// A view not live as a dump before `Pardes.views` kept it: its share of +/// the session beside its panes' (read once, by name, into `views[k]`). +pub const OldDumped = struct { + k: u8, + view: View = .{}, + panes: []const Shares.Share = &.{}, +}; + fn bit(k: usize) u8 { return @as(u8, 1) << @intCast(k); } -/// The views not live, as a dump keeps them: the slices `gpa`'s, what -/// they hold the session's. -pub fn dumped(p: *const Pardes, gpa: std.mem.Allocator) ![]Dumped { - var out: std.ArrayList(Dumped) = .empty; +/// The panes' shares of the views not live, as a dump keeps them: the +/// slices `gpa`'s, what they hold the session's. +pub fn dumped(p: *const Pardes, gpa: std.mem.Allocator) ![]Shares { + var out: std.ArrayList(Shares) = .empty; errdefer { for (out.items) |d| gpa.free(d.panes); out.deinit(gpa); } for (0..max) |k| { - if (k == p.view or !isOpen(p, @intCast(k))) continue; - const slot = p.parked[k] orelse continue; - var d: Dumped = .{ .k = @intCast(k), .view = undefined, .serials = slot.serials }; - copy(&kept, &d.view, &slot.view); - var shares: std.ArrayList(Dumped.Share) = .empty; + if (k == p.live or !isOpen(p, @intCast(k))) continue; + var shares: std.ArrayList(Shares.Share) = .empty; errdefer shares.deinit(gpa); // A view not live has a share of every pane: written when it went - // out, made with one made since (`installed`). `pv_known` says - // which shares own their selections, not which there are. + // out, made with one made since (`installed`). for (p.panes, 0..) |s, id| if (s) |pane| { try shares.append(gpa, .{ .id = @intCast(id), .pv = pane.pv[k] }); }; - d.panes = try shares.toOwnedSlice(gpa); - try out.append(gpa, d); + try out.append(gpa, .{ .k = @intCast(k), .panes = try shares.toOwnedSlice(gpa) }); } return out.toOwnedSlice(gpa); } -/// The views a dump kept (`dumped`) parked again in `p`, a session just -/// loaded with its live view: each its slot, each pane its share in it -/// (one a view had none of as the live view has it). What they hold is the -/// session's then, the slices the caller's; a failure moves nothing. A -/// view the core says open that the dump has none of is not. -pub fn undumped(p: *Pardes, others: []const Dumped) !void { +/// The panes' shares a dump kept (`dumped`) parked again in `p`, a +/// session just loaded with its views: each pane its share in each view +/// (one a view had none of as the live view has it). What they hold is +/// the session's then, the slices the caller's. A view the core says open +/// that the dump has no shares of is not. +pub fn undumped(p: *Pardes, others: []const Shares) void { var take: u8 = 0; - for (others) |d| if (d.k < max and d.k != p.view and isOpen(p, d.k) and take & bit(d.k) == 0) { + for (others) |d| if (d.k < max and d.k != p.live and isOpen(p, d.k) and take & bit(d.k) == 0) { take |= bit(d.k); }; - var made: u8 = 0; - errdefer for (0..max) |k| if (made & bit(k) != 0) { - p.gpa.destroy(p.parked[k].?); - p.parked[k] = null; - }; - if (take != 0) for (0..max) |k| if ((take | bit(p.view)) & bit(k) != 0 and p.parked[k] == null) { - const slot = try p.gpa.create(Parked); - slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; - p.parked[k] = slot; - made |= bit(k); - }; - // Nothing fails from here. - p.views_open &= take | bit(p.view); + for (0..max) |k| if (k != p.live and isOpen(p, @intCast(k)) and take & bit(k) == 0) dropView(p, @intCast(k)); + p.views_open &= take | bit(p.live); p.views_away &= p.views_open; // The live view's slot owning nothing, as `installed` makes it. for (p.panes) |s| if (s) |pane| { - if (take != 0) pane.pv[p.view] = fresh(pane); - pane.pv_known |= bit(p.view); + if (take != 0) pane.pv[p.live] = fresh(pane); + pane.pv_known |= bit(p.live); }; var left = take; for (others) |d| { - var view = d.view; if (left & bit(d.k) == 0) { // Refused (another build's, or twice): what it holds goes. - for (&view.col_tags) |*t| t.deinit(p.gpa); - if (view.chord_arg) |arg| p.gpa.free(arg); for (d.panes) |sh| if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); continue; } left &= ~bit(d.k); - const slot = p.parked[d.k].?; - slot.serials = d.serials; - copy(&kept, &slot.view, &view); for (d.panes) |sh| { const pane = (if (sh.id < MAX_PANES) p.panes[sh.id] else null) orelse { if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); @@ -211,15 +151,34 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { } } +/// The views a dump before `Pardes.views` kept (`OldDumped`): each its +/// share of the session into `views[k]`, the rest as `undumped`. What +/// each view holds is the session's then; one refused is freed. +pub fn undumpedOld(p: *Pardes, others: []const OldDumped, gpa: std.mem.Allocator) !void { + var shares: std.ArrayList(Shares) = .empty; + defer shares.deinit(gpa); + var took: u8 = 0; + for (others) |d| { + var view = d.view; + if (d.k >= max or d.k == p.live or !isOpen(p, d.k) or took & bit(d.k) != 0) { + freeView(p, &view); + continue; + } + took |= bit(d.k); + p.views[d.k] = view; + try shares.append(gpa, .{ .k = d.k, .panes = d.panes }); + } + undumped(p, shares.items); +} + /// The live view alone, as view 0: every other closed. A session loaded /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { p.views_away = 0; - for (0..max) |k| if (k != p.view) close(p, @intCast(k)); - emptied(p, p.view); - const k = p.view; + for (0..max) |k| if (k != p.live) close(p, @intCast(k)); + const k = p.live; if (k == 0) return; - std.mem.swap(?*Parked, &p.parked[0], &p.parked[k]); + std.mem.swap(?View, &p.views[0], &p.views[k]); for (p.panes) |s| if (s) |pane| { // The live slot is stale: as `installed` makes it, owning nothing. pane.pv[0] = fresh(pane); @@ -227,7 +186,7 @@ pub fn only(p: *Pardes) void { if (pane.sized_by == k) pane.sized_by = 0; if (pane.prompt_view == k) pane.prompt_view = 0; }; - p.view = 0; + p.live = 0; p.views_open = 1; } @@ -317,7 +276,7 @@ fn fresh(pane: *const Pane) PaneView { /// A new pane is in each other view as it was made, there to be shown. pub fn installed(p: *Pardes, pane: *Pane) void { - pane.sized_by = p.view; + pane.sized_by = p.live; if (!several(p)) return; // The live view's slot too, owning nothing: a pane closed before its // view next parks is torn down with that slot never written, and @@ -330,30 +289,46 @@ pub fn installed(p: *Pardes, pane: *Pane) void { /// A pane going: the other views' selections of it go too. pub fn tornDown(p: *Pardes, pane: *Pane) void { - for (0..max) |k| if (k != p.view and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { + for (0..max) |k| if (k != p.live and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { const sels = pane.pv[k].body.sels; if (sels.len > 0) p.gpa.free(sels); pane.pv[k].body.sels = &.{}; }; } -fn park(p: *Pardes) void { - const slot = p.parked[p.view].?; - copy(&fields, &slot.view, p); - for (p.panes, 0..) |s, id| { - const pane = s orelse { - slot.serials[id] = 0; - continue; - }; - slot.serials[id] = pane.serial; - pane.pv[p.view] = extract(pane); +/// Pane `id` closed: it leaves the layout of every view not live, and the +/// keyboard of one it had moves to that view's first pane left (the live +/// view's is the closing's to move, panes.removePane). +pub fn gone(p: *Pardes, id: usize) void { + if (!several(p)) return; + for (&p.views, 0..) |*slot, k| { + if (k == p.live) continue; + const v = &(slot.* orelse continue); + for (v.cols[0..v.ncol]) |*col| { + var n: usize = 0; + for (col.panes[0..col.n]) |other| if (other != id) { + col.panes[n] = other; + n += 1; + }; + col.n = n; + } + if (v.active != id) continue; + v.active = for (v.cols[0..v.ncol]) |col| { + if (col.n > 0) break col.panes[0]; + } else for (p.panes, 0..) |s, i| { + if (s != null and i != id) break i; + } else 0; } } +fn park(p: *Pardes) void { + for (p.panes) |s| if (s) |pane| { + pane.pv[p.live] = extract(pane); + }; +} + fn unpark(p: *Pardes, k: u8) void { - const slot = p.parked[k].?; - copy(&fields, p, &slot.view); - p.view = k; + p.live = k; for (p.panes) |s| if (s) |pane| { apply(pane, &pane.pv[k]); // The live body owns the selections now; the slot, stale while its @@ -362,28 +337,10 @@ fn unpark(p: *Pardes, k: u8) void { pane.pv[k].body.sels = &.{}; pane.pv[k].body.nsel = 0; }; - // The panes closed while it was out (a slot empty or another pane's - // now) leave its layout, and its keyboard leaves them. - for (0..p.ncol) |c| { - var n: usize = 0; - for (p.cols[c].panes[0..p.cols[c].n]) |id| { - const pane = p.panes[id] orelse continue; - if (pane.serial != slot.serials[id]) continue; - p.cols[c].panes[n] = id; - n += 1; - } - p.cols[c].n = n; - } - if (layout.findPane(p, p.active) == null) { - for (0..p.ncol) |c| if (p.cols[c].n > 0) { - p.active = p.cols[c].panes[0]; - break; - }; - } layout.compute(p); // Its terminals' cursors moved while it was out: each pan follows. for (p.panes, 0..) |s, id| if (s) |pane| { - pane.pan = pardes.panes.terminal.panFor(p, pane, p.rects[id]); + pane.pan = pardes.panes.terminal.panFor(p, pane, p.view().rects[id]); }; } @@ -392,30 +349,18 @@ fn unpark(p: *Pardes, k: u8) void { /// focus), it goes into the view's active column, as kakoune shows a buffer /// in the window that asked for it. pub fn show(p: *Pardes) void { - if (p.panes[p.active] == null or layout.findPane(p, p.active) != null) return; - const c = layout.firstColumn(p) orelse layout.columnBySerial(p, p.active_column) orelse p.ncol - 1; - layout.acmePlace(p, c, null, p.active); + const v = p.view(); + if (p.panes[v.active] == null or layout.findPane(p, v.active) != null) return; + const c = layout.firstColumn(p) orelse layout.columnBySerial(p, v.active_column) orelse v.ncol - 1; + layout.acmePlace(p, c, null, v.active); } -/// Into view `k`: the live one parked, `k`'s put in its place. +/// Into view `k`: the live one's pane shares parked, `k`'s put in their place. pub fn to(p: *Pardes, k: u8) void { std.debug.assert(isOpen(p, k)); - if (k == p.view) return; + if (k == p.live) return; park(p); unpark(p, k); - emptied(p, k); -} - -/// View `k` is live: its slot, stale until it parks again (written whole -/// then), keeps no pointer to what it handed the live fields. A rollback -/// copy walks every slot (src/rollback.zig): one left there, freed by the -/// live view and the memory another's since, would be saved and its old -/// bytes put back over the other's. (A pane's share holds no pointer but -/// its selections, which `unpark` hands over.) -fn emptied(p: *Pardes, k: u8) void { - const slot = p.parked[k] orelse return; - slot.view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h }; - slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); } /// A new view, as the live one is laid out and focused, with its panes @@ -426,26 +371,20 @@ pub fn open(p: *Pardes) ?u8 { const k: u8 = for (0..max) |i| { if (!isOpen(p, @intCast(i))) break @intCast(i); } else return null; - // Each live view parks too: its slot is made with the first other. - for ([_]u8{ p.view, k }) |i| if (p.parked[i] == null) { - const slot = p.gpa.create(Parked) catch return null; - slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; - p.parked[i] = slot; - }; - const slot = p.parked[k].?; - slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; + const live = p.view(); + p.views[k] = .{ .screen_w = live.screen_w, .screen_h = live.screen_h }; + const v = &p.views[k].?; inline for (sized ++ laid_out ++ .{"frontend"}) |name| - @field(slot.view, name) = @field(p, name); + @field(v, name) = @field(live, name); // A query's mark on the jumps is the view's that asked it. - slot.view.jumps.force = false; - slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); - for (p.panes, 0..) |s, id| { + v.jumps.force = false; + v.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); + for (p.panes) |s| { const pane = s orelse continue; - slot.serials[id] = pane.serial; pane.pv[k] = fresh(pane); - pane.pv_known |= @as(u8, 1) << @intCast(k); + pane.pv_known |= bit(k); } - p.views_open |= @as(u8, 1) << @intCast(k); + p.views_open |= bit(k); return k; } @@ -464,51 +403,56 @@ pub fn closeKeeping(p: *Pardes, k: u8) void { to(p, k); // Never into one kept for a rejoin (`away`): that one is its client's. const other = looked(p, k).?; - const slot = p.parked[other].?; + const v = &p.views[other].?; // Its size too: the terminals it sized keep theirs, no shell redrawn for // a size it is given back at the rejoin. - const force = slot.view.jumps.force; - inline for (sized ++ laid_out) |name| @field(slot.view, name) = @field(p, name); - slot.view.jumps.force = force; - for (p.panes, 0..) |s, id| { - const pane = s orelse { - slot.serials[id] = 0; - continue; - }; - if (pane.pv_known & (@as(u8, 1) << @intCast(other)) != 0 and pane.pv[other].body.sels.len > 0) p.gpa.free(pane.pv[other].body.sels); - slot.serials[id] = pane.serial; + const force = v.jumps.force; + inline for (sized ++ laid_out) |name| @field(v, name) = @field(p.views[k].?, name); + v.jumps.force = force; + for (p.panes) |s| if (s) |pane| { + if (pane.pv_known & bit(other) != 0 and pane.pv[other].body.sels.len > 0) p.gpa.free(pane.pv[other].body.sels); pane.pv[other] = fresh(pane); - pane.pv_known |= @as(u8, 1) << @intCast(other); - } + pane.pv_known |= bit(other); + }; close(p, k); } +/// What view `v`'s own share holds, freed. +fn freeView(p: *Pardes, v: *View) void { + for (&v.col_tags) |*t| t.deinit(p.gpa); + if (v.chord_arg) |a| p.gpa.free(a); +} + +/// View `k` closed outright (not live): its share and its panes' shares freed. +fn dropView(p: *Pardes, k: u8) void { + if (p.views[k]) |*v| freeView(p, v); + p.views[k] = null; + for (p.panes) |s| if (s) |pane| { + if (pane.pv_known & bit(k) != 0) { + const sels = pane.pv[k].body.sels; + if (sels.len > 0) p.gpa.free(sels); + } + pane.pv[k].body.sels = &.{}; + pane.pv_known &= ~bit(k); + // A name it was drafting goes with it. + if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); + }; + p.views_open &= ~bit(k); + p.views_away &= ~bit(k); +} + /// View `k` gone, and all it held; the live one moves to another first. /// The last view someone looks at stays: a session without a client keeps /// its own. One kept for a rejoin (`away`) goes whenever it is closed. pub fn close(p: *Pardes, k: u8) void { if (!isOpen(p, k) or @popCount(p.views_open) == 1 or (!isAway(p, k) and count(p) == 1)) return; - if (k == p.view) { + if (k == p.live) { const other: u8 = looked(p, k) orelse for (0..max) |i| { if (i != k and isOpen(p, @intCast(i))) break @intCast(i); } else unreachable; - park(p); - unpark(p, other); - emptied(p, other); + to(p, other); } - const v = &p.parked[k].?.view; - for (&v.col_tags) |*t| t.deinit(p.gpa); - if (v.chord_arg) |a| p.gpa.free(a); - for (p.panes) |s| if (s) |pane| { - const sels = pane.pv[k].body.sels; - if (sels.len > 0) p.gpa.free(sels); - pane.pv[k].body.sels = &.{}; - pane.pv_known &= ~(@as(u8, 1) << @intCast(k)); - // A name it was drafting goes with it. - if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); - }; - p.views_open &= ~(@as(u8, 1) << @intCast(k)); - p.views_away &= ~(@as(u8, 1) << @intCast(k)); + dropView(p, k); } /// View `k`'s client gone a moment, to join again for it (an Upgrade's @@ -519,130 +463,71 @@ pub fn close(p: *Pardes, k: u8) void { /// goes into another someone looks at, or into a copy of this one of its own. pub fn away(p: *Pardes, k: u8) void { if (!isOpen(p, k)) return; - if (k == p.view) if (looked(p, k) orelse open(p)) |other| to(p, other); + if (k == p.live) if (looked(p, k) orelse open(p)) |other| to(p, other); p.views_away |= bit(k); } -/// The session's end: what each view not live holds. +/// The session's end: what each view holds. pub fn deinit(p: *Pardes) void { - for (0..max) |i| { - const k: u8 = @intCast(i); - const slot = p.parked[k] orelse continue; - if (k != p.view and isOpen(p, k)) { - const v = &slot.view; - for (&v.col_tags) |*t| t.deinit(p.gpa); - if (v.chord_arg) |a| p.gpa.free(a); - } - p.gpa.destroy(slot); - } + for (&p.views) |*slot| if (slot.*) |*v| freeView(p, v); } +pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *const pardes.Surface) void }; + /// Each view but the live one drawn and presented, the live one put back -/// after exactly as it was: no digested field moves, so a replica that -/// draws nothing (and skips this) stays the same. A view drawn keeps -/// nothing of its drawing: whatever its unpark brought inside a changed -/// text is not kept, and is done again when it is next live. Before the live one's frame: the surface after the pump is its. +/// after exactly as it was (`drawOne`). Before the live one's frame: the +/// surface after the pump is its. pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { - const live = p.view; - const v = h.vtable; - park(p); - defer { - copy(&fields, p, &p.parked[live].?.view); - emptied(p, live); - p.view = live; - for (p.panes) |s| if (s) |pane| { - const pv = &pane.pv[live]; - copy(&pane_fields, pane, pv); - copy(&body_fields, &pane.body, &pv.body); - if (!pane.isTerminal()) { - pane.cols = pv.cols; - pane.rows = pv.rows; - } - if (pane.file) |*f| f.scroll = pv.scroll; - }; - } for (0..max) |i| { const k: u8 = @intCast(i); - if (k == live or !isOpen(p, k) or (p.undrawn | p.views_away) & bit(k) != 0) continue; + if (k == p.live or !isOpen(p, k) or (p.undrawn | p.views_away) & bit(k) != 0) continue; // No post_present: what a shell acknowledges there moves the // session's pointer and hover, which are the live view's to move; // a view's panels are acknowledged when it is live and drawn. - try drawParked(p, k, .{ .ctx = h.ctx, .f = v.present }); + try drawOne(p, k, .{ .ctx = h.ctx, .f = h.vtable.present }); } } -pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *const pardes.Surface) void }; - -/// Parked view `k` unparked, drawn and presented. `unpark` hands each -/// slot's selections to its body; drawn, the view is out again, and they -/// are the slot's again (else a frame drawn would take them from a view, -/// and a replica that draws nothing would keep them). -fn drawParked(p: *Pardes, k: u8, present: Present) !void { - try unparked(p, k, present, drawLive); -} - fn drawLive(present: Present, p: *Pardes) anyerror!void { _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); const surface = try p.render(p.frame_arena.allocator()); if (present.f) |f| f(present.ctx, surface); } -/// `f(ctx, p)` with parked view `k` unparked, its slot's selections its own -/// again after (`drawParked`). -fn unparked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !void { - var sels: [MAX_PANES]struct { []Text.SelRange, u16 } = undefined; - for (p.panes, 0..) |s, id| if (s) |pane| { - sels[id] = .{ pane.pv[k].body.sels, pane.pv[k].body.nsel }; - }; - unpark(p, k); - defer for (p.panes, 0..) |s, id| if (s) |pane| { - pane.pv[k].body.sels = sels[id][0]; - pane.pv[k].body.nsel = sels[id][1]; - }; - try f(ctx, p); -} - -/// View `k` drawn and presented outside any step, by a replica client -/// (src/replica.zig) whose own it is: as `drawOthers` draws one, the live -/// view put back after exactly as it was, so the replica's digest is the -/// authority's still. No post_present, as there. +/// View `k` drawn and presented outside any step (the pump's other views, +/// a replica client's own): the live view put back after exactly as it +/// was, so the digest is the same. No post_present. pub fn drawOne(p: *Pardes, k: u8, present: Present) !void { - if (k == p.view or !isOpen(p, k)) return drawLive(present, p); - const Draw = struct { - fn draw(pr: Present, q: *Pardes, kk: u8) anyerror!void { - try drawParked(q, kk, pr); - } - }; - try withParked(p, k, present, Draw.draw); + try within(p, k, present, drawLive); } /// `f(ctx, p)` with view `k` live, outside any step, and the live view put /// back after exactly as it was, as `drawOne` draws one: what a replica /// client's frontend reads of its own view (its pointer's cells, its size) -/// while another client's input was the session's last. `f` reads only. +/// while another client's input was the session's last. What going into +/// `k` and drawing it made of it (its layout worked out, a cursor brought +/// inside a changed text) is not kept: it is done again, in every replica +/// alike, when `k` is next live by an input. pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !void { - if (k == p.view or !isOpen(p, k)) return f(ctx, p); - const Read = struct { - fn read(x: @TypeOf(ctx), q: *Pardes, kk: u8) anyerror!void { - try unparked(q, kk, x, f); - } + if (k == p.live or !isOpen(p, k)) return f(ctx, p); + const live = p.live; + const kept = p.views[k]; + var sels: [MAX_PANES]struct { []Text.SelRange, u16 } = undefined; + for (p.panes, 0..) |s, id| if (s) |pane| { + sels[id] = .{ pane.pv[k].body.sels, pane.pv[k].body.nsel }; }; - try withParked(p, k, ctx, Read.read); -} - -/// The live view parked, `f(ctx, p, k)`, and the live view put back after -/// exactly as it was. -fn withParked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes, u8) anyerror!void) !void { - const live = p.view; park(p); + unpark(p, k); defer { - copy(&fields, p, &p.parked[live].?.view); - // Its slot and its panes' shares, stale while it is live, keep no - // pointer to what they handed back, as `unpark` leaves them: a - // replica's rollback copy walks them (`emptied`). - emptied(p, live); - p.view = live; - for (p.panes) |s| if (s) |pane| { + p.views[k] = kept; + p.live = live; + for (p.panes, 0..) |s, id| if (s) |pane| { + // `k`'s slot its selections again (else a frame drawn would + // take them from a view, and a replica that draws nothing + // would keep them); the live view's fields its own again, and + // its slot, stale while it is live, owning nothing. + pane.pv[k].body.sels = sels[id][0]; + pane.pv[k].body.nsel = sels[id][1]; const pv = &pane.pv[live]; copy(&pane_fields, pane, pv); copy(&body_fields, &pane.body, &pv.body); @@ -655,7 +540,7 @@ fn withParked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pa pv.body.nsel = 0; }; } - try f(ctx, p, k); + try f(ctx, p); } // ---- tests ---- @@ -723,30 +608,30 @@ test "each view has its own layout and focus over the same panes" { defer p.deinit(); _ = try p.setTestFile("one\ntwo\n"); const k = p.openView().?; - const ncol = p.ncol; + const ncol = p.view().ncol; p.toView(k); p.update(.{ .command = "Newcol" }); p.update(.{ .command = "New" }); - const made = p.active; + const made = p.view().active; try testing.expect(made != 0); - try testing.expectEqual(ncol + 1, p.ncol); + try testing.expectEqual(ncol + 1, p.view().ncol); p.toView(0); - try testing.expectEqual(ncol, p.ncol); - try testing.expectEqual(@as(usize, 0), p.active); + try testing.expectEqual(ncol, p.view().ncol); + try testing.expectEqual(@as(usize, 0), p.view().active); try testing.expect(layout.findPane(p, made) == null); - try testing.expectEqual(pardes.Rect{}, p.rects[made]); + try testing.expectEqual(pardes.Rect{}, p.view().rects[made]); // The pane is the session's: focused here, this view shows it too. - p.active = made; + p.view().active = made; p.sync(); try testing.expect(layout.findPane(p, made) != null); - try testing.expect(p.rects[made].h > 0); + try testing.expect(p.view().rects[made].h > 0); // Closed here, it leaves the other view's layout, and its keyboard. try p.removePane(made, null); p.sync(); p.toView(k); try testing.expect(layout.findPane(p, made) == null); - try testing.expect(p.panes[p.active] != null); - try testing.expect(layout.findPane(p, p.active) != null); + try testing.expect(p.panes[p.view().active] != null); + try testing.expect(layout.findPane(p, p.view().active) != null); } test "registers are the session's: yanked in one view, put in another" { @@ -766,14 +651,14 @@ test "a terminal shown at two sizes takes the size of the view that typed into i const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); - const term = p.panes[p.active].?; + const term = p.panes[p.view().active].?; try testing.expect(term.isTerminal()); const small = .{ term.cols, term.rows }; const k = p.openView().?; p.toView(k); p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); // Shown bigger here, it keeps the size of the view that typed last. - try testing.expect(p.rects[p.active].w > 80); + try testing.expect(p.view().rects[p.view().active].w > 80); try testing.expectEqual(small, .{ term.cols, term.rows }); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expect(term.cols > small[0] and term.rows > small[1]); @@ -786,7 +671,7 @@ test "a terminal shown at two sizes takes the size of the view that typed into i // That view gone, the one left sizes it. p.toView(k); p.closeView(0); - try testing.expectEqual(k, p.view); + try testing.expectEqual(k, p.live); try testing.expectEqual(big, .{ term.cols, term.rows }); } @@ -808,17 +693,17 @@ test "a view closing leaves the others as they were" { cursorTo(p, pane, 70); p.toView(0); cursorTo(p, pane, 10); - const ncol = p.ncol; + const ncol = p.view().ncol; // The live one closing: the session goes into another. p.toView(b); p.closeView(b); - try testing.expect(p.view != b); + try testing.expect(p.live != b); try testing.expectEqual(@as(usize, 2), count(p)); p.toView(a); try testing.expectEqual(@as(i32, 70), pane.body.cur_row); p.closeView(0); - try testing.expectEqual(a, p.view); - try testing.expectEqual(ncol, p.ncol); + try testing.expectEqual(a, p.live); + try testing.expectEqual(ncol, p.view().ncol); // The last stays. p.closeView(a); try testing.expectEqual(@as(usize, 1), count(p)); @@ -837,23 +722,23 @@ test "the last client's view going leaves the session its layout, which a rejoin p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); p.update(.{ .command = "Newcol" }); p.update(.{ .command = "New" }); - const made = p.active; - const ncol = p.ncol; - p.active = 0; + const made = p.view().active; + const ncol = p.view().ncol; + p.view().active = 0; p.sync(); cursorTo(p, pane, 60); p.leaveView(k); try testing.expectEqual(@as(usize, 1), count(p)); - try testing.expectEqual(ncol, p.ncol); + try testing.expectEqual(ncol, p.view().ncol); try testing.expect(layout.findPane(p, made) != null); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); - try testing.expectEqual(@as(u16, 120), p.screen_w); + try testing.expectEqual(@as(u16, 120), p.view().screen_w); // Its client back (a view copied from the live one): all of it again. const again = p.openView().?; p.toView(again); - try testing.expectEqual(ncol, p.ncol); + try testing.expectEqual(ncol, p.view().ncol); try testing.expect(layout.findPane(p, made) != null); - try testing.expectEqual(@as(usize, 0), p.active); + try testing.expectEqual(@as(usize, 0), p.view().active); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } @@ -869,13 +754,13 @@ test "a client's view kept for its rejoin: through an Upgrade's dump, each clien for (ks, &made) |k, *m| { p.toView(k); p.update(.{ .command = "New" }); - m.* = p.active; + m.* = p.view().active; } // Both let go to upgrade, the live view one of theirs: theirs are kept, // and the session's own is live. for (ks) |k| p.awayView(k); try testing.expectEqual(@as(usize, 1), count(p)); - try testing.expect(!isAway(p, p.view)); + try testing.expect(!isAway(p, p.live)); // The new build loads the dump; each client is back in its own view. const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); @@ -885,7 +770,7 @@ test "a client's view kept for its rejoin: through an Upgrade's dump, each clien try testing.expect(isAway(q, k)); try testing.expect(q.rejoinView(k)); q.toView(k); - try testing.expectEqual(m, q.active); + try testing.expectEqual(m, q.view().active); try testing.expect(layout.findPane(q, m) != null); try testing.expect(layout.findPane(q, made[1 - i]) == null); } @@ -912,7 +797,7 @@ test "drawing the other views leaves the live one as it was, to the digest" { cursorTo(p, pane, 95); const before = pardes.walk.digest(Pardes, p); try drawOthers(p, .{}); - try testing.expectEqual(@as(u8, 0), p.view); + try testing.expectEqual(@as(u8, 0), p.live); try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); p.toView(k); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); @@ -961,7 +846,7 @@ test "a terminal shown in a view shorter than it keeps its cursor in sight" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 20 }); defer p.deinit(); - const id = p.active; + const id = p.view().active; const term = p.panes[id].?; const k = p.openView().?; p.toView(k); @@ -983,7 +868,7 @@ test "a terminal shown in a view shorter than it keeps its cursor in sight" { var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); const s = try p.render(arena.allocator()); - const r = p.rects[id]; + const r = p.view().rects[id]; // The cursor's row, the last, is on screen in the short view, inside // the pane, and so is the line it is on. const cursor = s.cursor.?; @@ -1014,7 +899,7 @@ test "a session's dump keeps its views: loaded, each where it was; revived for o p.toView(0); p.toView(k); cursorTo(p, pane, 30); - const ncol = p.ncol; + const ncol = p.view().ncol; const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); @@ -1026,16 +911,16 @@ test "a session's dump keeps its views: loaded, each where it was; revived for o const lp = loaded.panes[id].?; loaded.toView(0); try testing.expectEqual(@as(i32, 119), lp.body.cur_row); - try testing.expectEqual(ncol - 1, loaded.ncol); + try testing.expectEqual(ncol - 1, loaded.view().ncol); loaded.toView(k); try testing.expectEqual(@as(i32, 30), lp.body.cur_row); - try testing.expectEqual(ncol, loaded.ncol); + try testing.expectEqual(ncol, loaded.view().ncol); // For one client: the view live when dumped, as view 0. const one = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes); defer one.deinit(); try testing.expectEqual(@as(usize, 1), count(one)); - try testing.expectEqual(@as(u8, 0), one.view); - try testing.expectEqual(ncol, one.ncol); + try testing.expectEqual(@as(u8, 0), one.live); + try testing.expectEqual(ncol, one.view().ncol); try testing.expectEqual(@as(i32, 30), one.panes[id].?.body.cur_row); try testing.expectEqual(@as(?u8, 1), one.openView()); } |
