summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Dir.zig8
-rw-r--r--src/File.zig104
-rw-r--r--src/Messages.zig69
-rw-r--r--src/Names.zig6
-rw-r--r--src/Output.zig3
-rw-r--r--src/Places.zig8
-rw-r--r--src/Query.zig6
-rw-r--r--src/Stream.zig82
-rw-r--r--src/Tables.zig41
-rw-r--r--src/Text.zig4
-rw-r--r--src/body_layer.zig25
-rw-r--r--src/builtins.zig12
-rw-r--r--src/config.zig5
-rw-r--r--src/draw.zig21
-rw-r--r--src/dump.zig13
-rw-r--r--src/exec.zig13
-rw-r--r--src/fs.zig143
-rw-r--r--src/image.zig4
-rw-r--r--src/look.zig2
-rw-r--r--src/main.zig2
-rw-r--r--src/mouse.zig5
-rw-r--r--src/ninep/pane.zig3
-rw-r--r--src/ninep/tree.zig2
-rw-r--r--src/normal.zig1
-rw-r--r--src/panes.zig29
-rw-r--r--src/pardes.zig159
-rw-r--r--src/pdf.zig3
-rw-r--r--src/pdf_view.zig4
-rw-r--r--src/selection_pipe.zig1
-rw-r--r--src/tape.zig741
-rw-r--r--src/terminal.zig24
31 files changed, 1327 insertions, 216 deletions
diff --git a/src/Dir.zig b/src/Dir.zig
index b994bc06..ebe6b69c 100644
--- a/src/Dir.zig
+++ b/src/Dir.zig
@@ -53,6 +53,10 @@ pub fn read(gpa: std.mem.Allocator, dir: []const u8) ![]u8 {
var own: [4096]u8 = undefined;
if (local.len > own.len) return error.NameTooLong;
@memcpy(own[0..local.len], local);
+ return pardes.tape.site(.dir_listing, gpa, readHost, .{ gpa, own[0..local.len] });
+}
+
+fn readHost(gpa: std.mem.Allocator, local: []const u8) ![]u8 {
var entries: std.ArrayList([]u8) = .empty;
defer {
for (entries.items) |entry| gpa.free(entry);
@@ -60,10 +64,8 @@ pub fn read(gpa: std.mem.Allocator, dir: []const u8) ![]u8 {
}
var total: usize = 0;
{
- pardes.turn.yield();
- defer pardes.turn.back();
const io = std.Io.Threaded.global_single_threaded.io();
- var d = try std.Io.Dir.cwd().openDir(io, own[0..local.len], .{ .iterate = true });
+ var d = try std.Io.Dir.cwd().openDir(io, local, .{ .iterate = true });
defer d.close(io);
var it = d.iterate();
while (try it.next(io)) |entry| {
diff --git a/src/File.zig b/src/File.zig
index e4bca000..d795eaa1 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -104,6 +104,7 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void {
}
pub const History = struct {
+ pub const replica = .{ .len = .{ .{ "undo", "undo_len" }, .{ "redo", "redo_len" } } };
undo: [limits.undo_max]Snapshot = undefined,
undo_len: usize = 0,
redo: [limits.undo_max]Snapshot = undefined,
@@ -137,6 +138,10 @@ pub const History = struct {
/// A file pane's backing: owned content, its derived caches, and undo history.
pub const State = struct {
+ /// What the replay digest leaves out (src/walk.zig): what is made again
+ /// from the text whenever it is wanted.
+ pub const replica = .{ .derived = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" } };
+
path: []u8,
content: []u8,
revision: u32 = 0,
@@ -1252,7 +1257,8 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) {
f.saved_revision = f.revision;
};
- f.mtime = pardes.ctlfs.events.now();
+ // The clock the session that recorded read (src/tape.zig).
+ f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{});
if (f.line_starts.len > 0) p.gpa.free(f.line_starts);
f.line_starts = starts;
if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights);
@@ -1647,6 +1653,8 @@ pub fn redo(p: *Pardes, pane: *Pane) void {
/// `<file> deleted on disk (Save writes it again)`, logged `changed
/// <serial> deleted`.
pub fn deleted(p: *Pardes, id: u8) void {
+ const taped = pardes.tape.enter(p, .{ .file_deleted = id });
+ defer pardes.tape.leave(p, taped);
const pane = p.panes[id] orelse return;
const f = if (pane.file) |*file| file else return;
if (f.disk_gone) return;
@@ -1703,6 +1711,17 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void {
/// re-highlight the visible window of any file whose syntax went stale
/// (edit, scroll, load) — visible-range-first so big files stay snappy
+/// The declarations a TreeContext pane's context rows are drawn from, for
+/// its text as it is now: found in the step (Pardes.sync), never only by a
+/// render, since the view's geometry is made of them (`geometry`).
+pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void {
+ if (!f.tree_context or !supportsContext(pane) or f.context_revision == f.revision) return;
+ const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch return;
+ if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations);
+ f.context_declarations = declarations;
+ f.context_revision = f.revision;
+}
+
pub fn refreshHighlights(p: *Pardes) void {
const tz = tracy.zone(@src(), "refreshHighlights");
defer tz.end();
@@ -1710,12 +1729,6 @@ pub fn refreshHighlights(p: *Pardes) void {
const pane = slot orelse continue;
if (pane.file == null) continue;
const f = &pane.file.?;
- if (f.tree_context and supportsContext(pane) and f.context_revision != f.revision) {
- const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch continue;
- if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations);
- f.context_declarations = declarations;
- f.context_revision = f.revision;
- }
if (f.mini != null) {
f.syntax_dirty = false;
continue;
@@ -1907,18 +1920,79 @@ pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow {
}
}
-pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 {
- const width = wrapWidth(pane, wrap);
+/// The body's text as it is shown: its geometry's rows, each a line's
+/// number and its stretch of the line.
+pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const u8 {
+ geometry(pane);
+ const out = try arena.alloc(u8, pane.geometry_len);
+ const filled = fillBody(out, pane, f, wrapWidth(pane, pane.view.wrap), false);
+ std.debug.assert(filled == out.len);
+ return out;
+}
+
+/// The pane's view geometry for its state as it is now: the wrap map (the
+/// line, column and pad each body row starts at), the context rows, and how
+/// many rows the body layer has. Made again when anything it is made of
+/// changes (`geometryKey`), by whoever asks first, the render or a step that
+/// reads it: so a core that never draws has the geometry of one that does
+/// (docs/next-daemon.md). What it reads of the session is the pane's `view`,
+/// which each step sets.
+pub fn geometry(pane: *Pane) void {
+ const f = if (pane.file) |*file| file else return;
+ const key = geometryKey(pane, f);
+ if (pane.geometry_key == key) return;
+ pane.geometry_key = key;
+ pane.body_rows = 0;
+ pane.body_visible_rows = 0;
+ pane.context_row_limit = null;
+ pane.geometry_len = mapRows(pane, f);
+ // A body with context rows is a layer taller than the grid's rows, its
+ // context rows at the tagline's height (body_layer.joinBody).
+ const v = pane.view;
+ if (!v.layered or pane.collapsed or v.rect_h <= pane.tag_rows) return;
+ const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len);
+ if (pane.context_rows == 0 or logical_rows <= pane.context_rows) return;
+ pane.body_rows = @intCast(logical_rows);
+ const bh: usize = @max(1, v.body_h);
+ const th: usize = std.math.clamp(v.tagline_h, 1, bh);
+ const body_pixels = @as(usize, v.rect_h - pane.tag_rows) * bh;
+ const limit: u16 = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th));
+ pane.context_row_limit = limit;
+ pane.body_visible_rows = @intCast(@min(logical_rows, limit + @max(1, (body_pixels -| @as(usize, limit) * th) / bh)));
+ pane.geometry_len = mapRows(pane, f);
+}
+
+/// One walk of the rows the body shows, recording the wrap map: the bytes
+/// its text takes.
+fn mapRows(pane: *Pane, f: *State) usize {
+ const width = wrapWidth(pane, pane.view.wrap);
pane.body_wrapped = width > 0;
f.tables.ensure(pane.gpa, f);
prepareContextRows(pane, f);
- pane.wrap_n = 0;
+ return fillBody(null, pane, f, width, true);
+}
- const len = fillBody(null, pane, f, width, false);
- const out = try arena.alloc(u8, len);
- const filled = fillBody(out, pane, f, width, true);
- std.debug.assert(filled == out.len);
- return out;
+/// Everything the geometry is made of, hashed: the text (its revision and
+/// length), where it is scrolled, the pane's size, its tables as far as
+/// they have been read, its context declarations, the session's view.
+fn geometryKey(pane: *const Pane, f: *const State) u64 {
+ const t = &f.tables;
+ const facts = .{
+ f.revision, f.content.len, f.scroll, pane.hscroll,
+ pane.rows, pane.cols, pane.tag_rows, pane.collapsed,
+ pane.query != null, f.pager, f.mini != null, f.output != null,
+ if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0,
+ f.context_declarations.len,
+ f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize),
+ pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h,
+ pane.view.rect_h,
+ };
+ // One hash of them laid out as words: every reader of the map asks for
+ // this key, a hundred times a frame.
+ var words: [facts.len]u64 = undefined;
+ inline for (facts, &words) |fact, *w| w.* = if (@TypeOf(fact) == bool) @intFromBool(fact) else if (@typeInfo(@TypeOf(fact)).int.signedness == .signed) @bitCast(@as(i64, fact)) else fact;
+ // Never the "made for nothing" key a reset leaves (`geometry_key = 0`).
+ return std.hash.Wyhash.hash(0x9e0, std.mem.sliceAsBytes(&words)) | 1;
}
/// Run the file-body row walk. With no destination it is the exact sizing
diff --git a/src/Messages.zig b/src/Messages.zig
index cb52907a..4cf81ac5 100644
--- a/src/Messages.zig
+++ b/src/Messages.zig
@@ -31,6 +31,7 @@ len: usize = 0,
pub const LoggedMessage = struct {
pub const cap = 256;
+ pub const replica = .{ .len = .{.{ "text", "len" }} };
text: [cap]u8 = undefined,
len: u16 = 0,
/// The serial of the pane that said it, or 0 for none in particular. A
@@ -117,6 +118,24 @@ pub const Message = struct {
};
extern "c" fn localtime_r(timep: *const libc.time_t, result: *Tm) ?*Tm;
+ fn timeOfDay() [8]u8 {
+ var clock: [8]u8 = "--:--:--".*;
+ const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false;
+ if (notime) return clock;
+ var ts: libc.timespec = undefined;
+ _ = libc.clock_gettime(.REALTIME, &ts);
+ const secs: libc.time_t = ts.sec;
+ var tm: Tm = undefined;
+ if (localtime_r(&secs, &tm) != null) {
+ _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{
+ @as(u32, @intCast(tm.hour)),
+ @as(u32, @intCast(tm.min)),
+ @as(u32, @intCast(tm.sec)),
+ }) catch {};
+ }
+ return clock;
+ }
+
pub fn body(text: []const u8) []const u8 {
if (text.len < 10) return text;
if (text[2] != ':' or text[5] != ':' or text[8] != ' ' or text[9] != ' ') return text;
@@ -127,21 +146,9 @@ pub const Message = struct {
}
pub fn stamp(buf: []u8, verb: []const u8, subject: []const u8) []const u8 {
- var clock: [8]u8 = "--:--:--".*;
- const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false;
- if (!notime) {
- var ts: libc.timespec = undefined;
- _ = libc.clock_gettime(.REALTIME, &ts);
- const secs: libc.time_t = ts.sec;
- var tm: Tm = undefined;
- if (localtime_r(&secs, &tm) != null) {
- _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{
- @as(u32, @intCast(tm.hour)),
- @as(u32, @intCast(tm.min)),
- @as(u32, @intCast(tm.sec)),
- }) catch {};
- }
- }
+ // The time of day as the session that said it read it, in its
+ // zone: a replica's clock and TZ are its own (src/tape.zig).
+ const clock = pardes.tape.value(.clock, timeOfDay, .{});
const head = std.fmt.bufPrint(buf, "{s} {s} ", .{ &clock, verb }) catch return buf[0..0];
const room = buf.len - head.len;
const tail = if (subject.len <= room) subject else subject[subject.len - room ..];
@@ -153,10 +160,14 @@ pub const Message = struct {
/// A status updates the message line in place (a progress count does not
/// pile up); a message is an event, and stacks.
pub fn setStatus(p: *Pardes, id: usize, text: []const u8) void {
+ const taped = pardes.tape.enter(p, .{ .set_status = .{ .id = id, .text = text } });
+ defer pardes.tape.leave(p, taped);
showMessage(p, id, text, false);
}
pub fn setMessage(p: *Pardes, id: usize, text: []const u8) void {
+ const taped = pardes.tape.enter(p, .{ .set_message = .{ .id = id, .text = text } });
+ defer pardes.tape.leave(p, taped);
logMessage(p, id, text);
showMessage(p, id, text, true);
}
@@ -186,7 +197,7 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void {
const arrives = pane.msg_len == 0 or pane.msg_life.phase == .leaving;
for (text[0..n], pane.msg[0..n]) |c, *cell| cell.* = if (c < 0x20 or c == 0x7f) ' ' else c;
pane.msg_len = @intCast(n);
- pane.msg_said_ms = nowMs();
+ pane.msg_said_ms = @intCast(p.now_ns / std.time.ns_per_ms);
p.needs_frame = true;
if (arrives) {
pane.msg_life = .{ .phase = if (p.settings.message_animation and n > 0) .entering else .shown };
@@ -200,13 +211,6 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void {
}
}
-fn nowMs() i64 {
- if (comptime !pardes.hosted) return 0;
- var ts: std.c.timespec = undefined;
- if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0;
- return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000);
-}
-
/// With no frontend attached no input dismisses a message, and a detached
/// session's /screen and messages kept what was said an hour ago: each
/// pane's lines go once its newest has been up `message_linger_ms`, on the
@@ -217,7 +221,9 @@ fn nowMs() i64 {
/// clock; the render pipeline's clock will give every line its own.
pub fn expireUnattended(p: *Pardes) void {
if (!p.unattended) return;
- const now = nowMs();
+ // The session's clock (`advance`), as every tick's: no replica reads
+ // its own.
+ const now: i64 = @intCast(p.now_ns / std.time.ns_per_ms);
for (p.panes) |slot| {
const pane = slot orelse continue;
if (pane.msg_len == 0 and pane.msg_older_len == 0) continue;
@@ -453,6 +459,8 @@ pub fn dialReason(err: anyerror) ?[]const u8 {
}
pub fn reportError(p: *Pardes, id: usize, operation: []const u8, err: anyerror) void {
+ const taped = pardes.tape.enter(p, .{ .report_error = .{ .id = id, .operation = operation, .err = err } });
+ defer pardes.tape.leave(p, taped);
// Room for an operation naming the longest path and the reason after
// it: a shorter one cut the reason off (`Save /long…`), and the reason
// is what a failure is for. Where it must be shorter, the path gives.
@@ -575,6 +583,19 @@ test "an error reads as words and a failure is cut between them" {
/// so it does not paint under them, and the paint pass so it can stack
/// them instead of letting the last one win the row.
pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void {
+ collectBands(p, pane, r, id);
+ // Each band's chip at the right edge, as wide as its own text: a click
+ // finds it whether or not a frame has drawn it.
+ const tx = r.x + config.GUTTER;
+ const tw = r.w -| config.GUTTER;
+ var leader_buf: [16]u8 = undefined;
+ for (0..pane.notices.len) |i| {
+ const chip = noticeCols(p, noticeText(p, pane, i, &leader_buf), tw);
+ pane.notices.left[i] = if (chip == 0) std.math.maxInt(u16) else tx + tw - chip;
+ }
+}
+
+fn collectBands(p: *Pardes, pane: *Pane, r: Rect, id: usize) void {
pane.notices.len = 0;
if (r.w <= config.GUTTER or r.h <= pane.tag_rows) return;
// Oldest message line on top, so lines already up do not move when a
diff --git a/src/Names.zig b/src/Names.zig
index 0c8ba075..66f71bb9 100644
--- a/src/Names.zig
+++ b/src/Names.zig
@@ -146,8 +146,7 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void {
pub fn rescore(n: *Names, gpa: std.mem.Allocator) void {
const q = n.queryText();
const empty = std.mem.trim(u8, q, " ").len == 0;
- const io = std.Io.Threaded.global_single_threaded.io();
- const started = std.Io.Clock.awake.now(io);
+ const started = pardes.FrameStats.nowNs();
const had = n.hits.items.len;
const was_scored = n.scored;
var count: usize = 0;
@@ -164,8 +163,7 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void {
const s = if (empty) 0 else match(q, key, n.lower.items[c.start + c.key ..][0..c.key_len], c.base, null, null, null);
if (s) |score| n.hits.append(gpa, .{ .cand = i, .score = score }) catch break;
// The clock every 256 candidates: reading it is not free either.
- if (!empty and count % 256 == 255 and
- started.durationTo(std.Io.Clock.awake.now(io)).toNanoseconds() > score_budget_ns) break;
+ if (!empty and count % 256 == 255 and pardes.tape.over(started, score_budget_ns)) break;
}
// An empty query keeps the source's order (the walk's, recency): no
// row moves as the walk goes on, it only grows at the foot.
diff --git a/src/Output.zig b/src/Output.zig
index a17b0459..83914d52 100644
--- a/src/Output.zig
+++ b/src/Output.zig
@@ -43,6 +43,7 @@ pub const Origin = union(enum) {
pub const max_arg = dump.max_origin_arg;
pub const State = struct {
+ pub const replica = .{ .len = .{.{ "arg_buf", "arg_len" }} };
from: Origin,
arg_buf: [max_arg]u8 = undefined,
arg_len: u16 = 0,
@@ -1115,7 +1116,7 @@ pub fn resetBody(p: *Pardes, pane: *Pane) void {
pane.body.cur_col = 0;
pane.body.cur_pinned = true;
pane.hscroll = 0;
- pane.wrap_n = 0;
+ pane.geometry_key = 0;
pane.body.msel = .{};
pane.body.vsel = .{};
pane.body.nsel = 0;
diff --git a/src/Places.zig b/src/Places.zig
index cff7c22e..3dca3adf 100644
--- a/src/Places.zig
+++ b/src/Places.zig
@@ -67,6 +67,10 @@ pub fn nowNs() u64 {
return @as(u64, @intCast(ts.sec)) *| std.time.ns_per_s +| @as(u64, @intCast(ts.nsec));
}
+/// What the replay digest leaves out (src/walk.zig): the rail's ticks,
+/// made again from the places when they are drawn.
+pub const replica = .{ .derived = .{ "ticks", "ticks_gen", "re" } };
+
started: bool = false,
provenance: Provenance = .search,
/// The query pane that fed them, by serial; 0 for `*` and the register.
@@ -477,7 +481,9 @@ pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) voi
if (short) break;
// A frame's time spent (a whole search's for n and N, whose steps
// are mvzr's whole budget).
- if (started_ns != 0 and bytes != std.math.maxInt(usize) and nowNs() - started_ns > frame_ns) break;
+ // The clock's verdict is the session's (src/tape.zig): a replica
+ // finds as many places a frame as this did.
+ if (started_ns != 0 and bytes != std.math.maxInt(usize) and pardes.tape.over(started_ns, frame_ns)) break;
}
if (set.hits.items.len != found_before) {
set.gen +%= 1;
diff --git a/src/Query.zig b/src/Query.zig
index 09b46f3b..7440dd0e 100644
--- a/src/Query.zig
+++ b/src/Query.zig
@@ -1743,11 +1743,7 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void
const f = &pane.file.?;
if (!std.mem.eql(u8, f.path, path)) {
if (exec.kindOf(path)) |kind| if (kind != .file) return;
- const bytes = blk: {
- pardes.turn.yield();
- defer pardes.turn.back();
- break :blk filesystem.read(p, path) catch return;
- };
+ const bytes = pardes.tape.site(.preview, p.gpa, filesystem.read, .{ p, path }) catch return;
if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) {
p.gpa.free(bytes);
return;
diff --git a/src/Stream.zig b/src/Stream.zig
index da7d12eb..8d9663dd 100644
--- a/src/Stream.zig
+++ b/src/Stream.zig
@@ -104,7 +104,13 @@ pub const ParseJob = struct {
}
};
-const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null };
+const ParseTask = struct {
+ // The worker the host started for it, live: a replica has none.
+ pub const replica = .{ .authority = .{ "future", "io" } };
+ job: *ParseJob,
+ future: ?std.Io.Future(void) = null,
+ io: ?std.Io = null,
+};
/// The colours of the text a scroll is heading into, worked out on a
/// worker before it gets there, as a game streams in the level ahead of
@@ -134,12 +140,21 @@ pub const PaintJob = struct {
}
};
-const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null };
+const PaintTask = struct {
+ // The worker the host started for it, live: a replica has none.
+ pub const replica = .{ .authority = .{ "future", "io" } };
+ job: *PaintJob,
+ future: ?std.Io.Future(void) = null,
+ io: ?std.Io = null,
+};
/// A paint's generation has the top bit, so it can never be a search's or
/// a parse's (those count from 0 in `gen`, and it would take two billion
/// searches to reach it).
const paint_bit: u32 = 1 << 31;
+/// A query rows' paint's has the next bit too: counted apart from the
+/// paints a view's frames ask for (`rows_count`), which are its own.
+const rows_bit: u32 = 1 << 30;
const max_paint_bytes = 512 << 10;
/// Where a worker hands a batch: false when it was turned away (the inbox
@@ -172,6 +187,19 @@ pub const Job = struct {
const Task = struct { job: *Job, future: std.Io.Future(void), io: std.Io };
+/// What the replay digest leaves out (src/walk.zig): the workers' timings,
+/// and the colours painted ahead of a view for its frames, which is the
+/// render's own prefetch (a replica that draws nothing paints none).
+pub const replica = .{
+ // The worker a search runs on, which the host starts.
+ .authority = .{"task"},
+ .local = .{
+ "started_ns", "first_ns", "finished_ns", "worker_ns", "parse_ns",
+ "paint", "paint_retired", "paint_count", "ahead", "ahead_for",
+ "painted_ahead", "painted_on_loop", "ahead_seen", "ahead_ask",
+ },
+};
+
gen: u32 = 0,
state: State = .idle,
kind: Kind = .grep,
@@ -210,6 +238,10 @@ paint: ?PaintTask = null,
/// loop for half a second). No new paint starts while it is out.
paint_retired: ?PaintTask = null,
paint_count: u32 = 0,
+rows_count: u32 = 0,
+/// The paint a frame asked for, its worker started once the frame is
+/// presented (`askAhead`): starting one is a thread, not a frame's to wait.
+ahead_ask: ?u32 = null,
ahead: []u8 = &.{},
ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{},
/// Stretches a view took painted ahead, and painted on the loop itself
@@ -321,7 +353,17 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont
const gen = paint_bit | (s.paint_count & ~paint_bit);
job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source };
s.paint = .{ .job = job };
- p.emit(.{ .search = .{ .gen = gen } });
+ // Asked of the host once the frame is presented, not queued as an
+ // effect of the session's: a replica that draws nothing asks for none.
+ s.ahead_ask = gen;
+}
+
+/// The worker for the paint the last frame asked for: the host starts it
+/// after the present (Pardes.pump), off the keystroke's way to the screen.
+pub fn askAhead(p: *Pardes) void {
+ const gen = p.stream.ahead_ask orelse return;
+ p.stream.ahead_ask = null;
+ if (p.host.vtable.search) |f| f(p.host.ctx, gen);
}
/// A file's colours for the query rows that show its lines, the whole
@@ -363,8 +405,8 @@ pub fn paintRows(p: *Pardes, path: []const u8) void {
};
s.rows_asked[s.rows_asked_at] = key;
s.rows_asked_at +%= 1;
- s.paint_count +%= 1;
- const gen = paint_bit | (s.paint_count & ~paint_bit);
+ s.rows_count +%= 1;
+ const gen = paint_bit | rows_bit | (s.rows_count & ~(paint_bit | rows_bit));
job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = 0, .revision = revision, .start = 0, .path = path_copy, .source = source };
s.rows_paint = .{ .job = job };
p.emit(.{ .search = .{ .gen = gen } });
@@ -1145,22 +1187,30 @@ const testing = std.testing;
/// worker, hand what it posts to the core.
const TestShell = struct {
q: host_io.Inbox,
+ p: ?*Pardes = null,
fn init() TestShell {
return .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } };
}
+ /// A shell's search: a worker started for the generation, as the tty's.
const vtable: pardes.Host.VTable = .{ .search = struct {
- fn f(_: ?*anyopaque, _: u32) void {}
+ fn f(ctx: ?*anyopaque, gen: u32) void {
+ const t: *TestShell = @ptrCast(@alignCast(ctx.?));
+ start(testing.io, testing.allocator, t.p.?, gen, &t.q, host_io.Inbox.pushSearch);
+ }
}.f };
- fn attach(p: *Pardes) void {
- p.host = .{ .vtable = &vtable };
+ fn attach(t: *TestShell, p: *Pardes) void {
+ t.p = p;
+ p.host = .{ .ctx = t, .vtable = &vtable };
}
fn pump(t: *TestShell, p: *Pardes) void {
+ // What the last frame asked for, as Pardes.pump asks after a present.
+ askAhead(p);
while (p.nextEffect()) |e| switch (e) {
- .search => |s| start(testing.io, testing.allocator, p, s.gen, &t.q, host_io.Inbox.pushSearch),
+ .search => p.perform(e),
else => {},
};
var batch: host_io.Inbox.Batch = .{};
@@ -1226,7 +1276,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw
pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" });
p.advance(1);
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
shell.q.deinit();
@@ -1328,7 +1378,7 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop,
pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig");
p.advance(1);
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
p.stream.cancelParse();
@@ -1367,7 +1417,7 @@ test "Stream: a scroll's colours are painted ahead on a worker, so PageDown afte
pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig");
p.advance(1);
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
p.stream.cancelParse();
@@ -1410,7 +1460,7 @@ test "zz stream timings on the stress file with a worker (QUERY_TIMES=<path>)" {
pane.file.?.path = try p.gpa.dupe(u8, path);
p.advance(1);
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
p.stream.cancelParse();
@@ -1464,7 +1514,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's
pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" });
p.advance(1);
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
shell.q.deinit();
@@ -1508,7 +1558,7 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work
pane.file.?.path = try p.gpa.dupe(u8, path);
p.advance(1);
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
p.stream.cancelParse();
@@ -1604,7 +1654,7 @@ test "Stream: a Grep's rows are coloured as their file's language colours them,
p.gpa.free(pane.file.?.path);
pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" });
var shell: TestShell = .init();
- TestShell.attach(p);
+ shell.attach(p);
defer {
p.stream.cancelTask();
p.stream.cancelPaint();
diff --git a/src/Tables.zig b/src/Tables.zig
index 2e65a716..44918559 100644
--- a/src/Tables.zig
+++ b/src/Tables.zig
@@ -95,8 +95,13 @@ pending: ?Resume = null,
/// and whether the line before ended a block.
const Resume = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool };
-/// What one frame spends reading a big text from the top.
+/// What one frame spends reading a big text from the top: on the session's
+/// tick, by the clock (src/tape.zig keeps a replica's reading in step).
pub const frame_ns: u64 = 4 * std.time.ns_per_ms;
+/// What a new text's first look reads at once, wherever it is asked for
+/// (the render, or a step reading the view's geometry): a count, never the
+/// clock, so whoever asks first finds the same tables.
+pub const first_lines: u32 = 4096;
pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void {
t.list.deinit(gpa);
@@ -130,10 +135,7 @@ pub fn find(t: *const Tables, line: usize) ?*const Table {
/// touch shift and only the block around it is read again (`reflow`): a
/// keystroke costs its table, not the file.
pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void {
- if (t.current(f)) {
- if (t.pending != null) t.go(gpa, f.content, frame_ns);
- return;
- }
+ if (t.current(f)) return;
defer {
t.revision = f.revision;
t.content = @intFromPtr(f.content.ptr);
@@ -157,13 +159,20 @@ pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void {
// read: it goes on.
if (lineHolding(starts, f.splice.head) >= r.n) {
t.lines = starts.len;
- t.go(gpa, f.content, frame_ns);
+ t.go(gpa, f.content, 0, first_lines);
return;
}
} else if (t.reflow(gpa, f)) |_| return else |_| {}
}
t.begin(starts.len);
- t.go(gpa, f.content, frame_ns);
+ t.go(gpa, f.content, 0, first_lines);
+}
+
+/// A frame's more of a reading under way: on the session's tick, so a core
+/// that draws reads exactly as far as one that does not.
+pub fn step(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void {
+ t.ensure(gpa, f);
+ if (t.pending != null) t.go(gpa, f.content, frame_ns, std.math.maxInt(u32));
}
/// Whether a reading of the text is still under way (Pardes.nextWake).
@@ -183,11 +192,11 @@ fn begin(t: *Tables, lines: usize) void {
t.pending = .{ .at = 0, .n = 0, .fence = null, .prev_blank = true };
}
-/// Read on for about `budget_ns` (0: to the end). On no memory there are
-/// no tables (the text is shown as it is).
-fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64) void {
+/// Read on for about `budget_ns` (0: no clock) or `max_lines`, whichever
+/// ends first. On no memory there are no tables (the text is shown as it is).
+fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64, max_lines: u32) void {
var at = t.pending orelse return;
- const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns) catch {
+ const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns, max_lines) catch {
t.list.clearRetainingCapacity();
t.pending = null;
return;
@@ -290,7 +299,7 @@ fn reflow(t: *Tables, gpa: std.mem.Allocator, f: *File.State) !void {
const prev_blank = r0 == 0 or blank(prev) or closesAny(prev) or std.mem.startsWith(u8, prev[@min(indentOf(prev), prev.len)..], "#");
const head = lineOf(content, starts, r0);
var at: Resume = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank };
- _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0);
+ _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0, std.math.maxInt(u32));
const fresh = try gpa.dupe(Table, t.list.items[before..]);
defer gpa.free(fresh);
t.list.shrinkRetainingCapacity(before);
@@ -500,7 +509,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void {
t.aligns.clearRetainingCapacity();
t.begin(std.mem.count(u8, content, "\n") + 1);
var at = t.pending.?;
- _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0);
+ _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0, std.math.maxInt(u32));
t.pending = null;
}
@@ -508,7 +517,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void {
/// appending them; with `fences`, keep the fences. With a budget, stop
/// when it is spent, `at` where to go on from. Whether it reached the end
/// (or `stop`).
-fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64) !bool {
+fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64, max_lines: u32) !bool {
var fence = at.fence;
var prev_blank = at.prev_blank;
var line = lineAt(content, at.at, at.n);
@@ -516,7 +525,7 @@ fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume
var lines: usize = 0;
while (line) |l| : (lines += 1) {
if (l.n > stop) break;
- if (budget_ns > 0 and lines % 256 == 255 and panes.Places.nowNs() -| t0 > budget_ns) {
+ if (lines % 256 == 255 and (lines >= max_lines or (budget_ns > 0 and pardes.tape.over(t0, budget_ns)))) {
at.* = .{ .at = l.start, .n = l.n, .fence = fence, .prev_blank = prev_blank };
return false;
}
@@ -1049,6 +1058,7 @@ test "tables: a big text's tables are read a frame's budget at a time from the t
File.setContent(p, f, new);
edits += 1;
}
+ p.update(.tick);
_ = frame.reset(.retain_capacity);
_ = try p.render(frame.allocator());
try testing.expect(frames < 10_000);
@@ -1077,6 +1087,7 @@ test "tables: a table wider than a wrapping pane is shown as its source, laid ou
// Source: a pipe is a pipe, at its source column.
try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7));
try testing.expect(p.executeBuiltinLine(0, "Wrap off"));
+ p.sync();
_ = frame.reset(.retain_capacity);
_ = try p.render(frame.allocator());
try testing.expect(!pane.body_wrapped);
diff --git a/src/Text.zig b/src/Text.zig
index b57c953a..3f107542 100644
--- a/src/Text.zig
+++ b/src/Text.zig
@@ -49,6 +49,10 @@ pub const SelRange = struct {
sticky: i32 = -1,
};
+/// The replay digest's walk (src/walk.zig) of the histories: as far as
+/// they go.
+pub const replica = .{ .len = .{ .{ "ed_undo", "ed_undo_len" }, .{ "ed_redo", "ed_redo_len" } } };
+
what: What = .body,
/// The characters of a text that holds its own (gpa-owned): everything but
/// a body, which reads its pane's. A tag nobody has changed has none yet and
diff --git a/src/body_layer.zig b/src/body_layer.zig
index bfca268a..5bffb691 100644
--- a/src/body_layer.zig
+++ b/src/body_layer.zig
@@ -169,22 +169,17 @@ pub fn buttonInks(th: *const colors.Theme) [3][3]u8 {
/// rows the grid cannot show, and is painted straight onto the grid. The
/// rail is the grid's alone.
pub fn joinBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rail: pardes.Region) !void {
- const file = if (pane.file) |*f| f else return renderBody(p, s, arena, pane, r, id, active, rail, null);
- if (!p.settings.tree_context_tag_style or pane.collapsed or r.h <= pane.tag_rows)
+ if (pane.file == null) return renderBody(p, s, arena, pane, r, id, active, rail, null);
+ // Made afresh for every frame from the pane as it is, as a step reading
+ // it would make it (File.geometry): never only by drawing.
+ pane.geometry_key = 0;
+ panes.File.geometry(pane);
+ // Which rows are context rows is decided by the text the body shows: a
+ // layer taller than the grid's rows when there are any.
+ if (!p.settings.tree_context_tag_style or pane.collapsed or r.h <= pane.tag_rows or pane.body_rows == 0)
return renderBody(p, s, arena, pane, r, id, active, rail, null);
- // Which rows are context rows is decided by the text the body shows.
- const text = try panes.File.bodyText(arena, pane, file, p.settings.wrap);
const viewport_rows = r.h - pane.tag_rows;
- const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len);
- if (pane.context_rows == 0 or logical_rows <= pane.context_rows)
- return renderBody(p, s, arena, pane, r, id, active, rail, text);
- pane.body_rows = @intCast(logical_rows);
- const bh: usize = @max(1, p.row_metrics.body_h);
- const th: usize = std.math.clamp(p.row_metrics.tagline_h, 1, bh);
- const body_pixels = @as(usize, viewport_rows) * bh;
- pane.context_row_limit = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th));
- const context_pixels = @as(usize, pane.context_row_limit.?) * th;
- pane.body_visible_rows = @intCast(@min(logical_rows, pane.context_row_limit.? + @max(1, (body_pixels -| context_pixels) / bh)));
+ const logical_rows: usize = pane.body_rows;
const width = r.w - config.GUTTER;
const layer = &s.body_layers[id];
const count = logical_rows * width;
@@ -512,6 +507,6 @@ pub fn bodyText(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 {
);
unreachable;
}
- if (pane.file) |*f| return panes.File.bodyText(arena, pane, f, p.settings.wrap);
+ if (pane.file) |*f| return panes.File.bodyText(arena, pane, f);
return panes.terminal.bodyText(arena, pane);
}
diff --git a/src/builtins.zig b/src/builtins.zig
index 3a66a316..a89b1180 100644
--- a/src/builtins.zig
+++ b/src/builtins.zig
@@ -793,11 +793,7 @@ pub const Mount = struct {
};
// A mount that cannot be reached is said now, and taken back, not
// found out by the first look through it.
- const reached = reached: {
- pardes.turn.yield();
- defer pardes.turn.back();
- break :reached @import("9p_io.zig").Client.probe(c.p.gpa, dial);
- };
+ const reached = pardes.tape.site(.probe, null, @import("9p_io.zig").Client.probe, .{ c.p.gpa, dial });
reached catch |err| {
if (c.p.fs.mounts.pop()) |gone| {
c.p.gpa.free(gone.name);
@@ -927,11 +923,7 @@ pub const TreeContext = struct {
};
file.tree_context = on;
file.syntax_dirty = true;
- c.pane.wrap_n = 0;
- c.pane.context_rows = 0;
- c.pane.body_rows = 0;
- c.pane.body_visible_rows = 0;
- c.pane.context_row_limit = null;
+ c.pane.geometry_key = 0;
c.p.surface.body_layers[c.id].rows = 0;
}
};
diff --git a/src/config.zig b/src/config.zig
index f0023193..13404461 100644
--- a/src/config.zig
+++ b/src/config.zig
@@ -455,6 +455,11 @@ test "wheel drift guard" {
/// The shell when no `Shell` is set: the user's login shell ($SHELL), as a
/// terminal would start, else /bin/sh. An explicit `Shell` overrides it.
pub fn defaultShell() []const u8 {
+ // As the session that recorded found it (src/tape.zig).
+ return pardes.tape.value(.env, defaultShellHost, .{});
+}
+
+fn defaultShellHost() []const u8 {
if (comptime pardes.hosted) if (std.c.getenv("SHELL")) |s| {
const shell = std.mem.span(s);
// One that is not there, or not executable, is no shell: /bin/sh.
diff --git a/src/draw.zig b/src/draw.zig
index e141ce31..4f9c6c87 100644
--- a/src/draw.zig
+++ b/src/draw.zig
@@ -96,12 +96,10 @@ pub fn place(p: *Pardes, s: *Surface) void {
if (r.h <= pane.tag_rows) continue;
const tx = r.x + config.GUTTER;
const tw = r.w - config.GUTTER;
- var leader_buf: [16]u8 = undefined;
for (0..pane.notices.len) |i| {
- const text = Messages.noticeText(p, pane, i, &leader_buf);
- const chip = Messages.noticeCols(p, text, tw);
- if (chip == 0) continue;
- pane.notices.left[i] = tx + tw - chip;
+ const left = pane.notices.left[i];
+ if (left == std.math.maxInt(u16)) continue;
+ const chip = tx + tw - left;
s.addRegion(.{ .kind = .notice, .owner = owner, .serial = pane.serial, .active = active, .notice = @intCast(i), .rect = .{ .x = tx + tw - chip, .y = body_y + @as(u16, pane.notices.row[i]), .w = chip, .h = BOX_H } });
}
}
@@ -521,6 +519,8 @@ fn resolved(color: Color, default: ?[3]u8, palette: ?[16][3]u8) ?[3]u8 {
}
pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface {
+ const taped = pardes.tape.enter(p, .render);
+ defer pardes.tape.leave(p, taped);
panes.File.refreshHighlights(p);
const s = &p.surface;
const ncells = @as(usize, p.screen_w) * p.screen_h;
@@ -606,9 +606,6 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface {
for (&p.panes, 0..) |*slot, id| {
const pane = slot.* orelse continue;
- pane.body_rows = 0;
- pane.body_visible_rows = 0;
- pane.context_row_limit = null;
s.body_layers[id].rows = 0;
try renderPane(p, s, arena, pane, p.rects[id], id, id == p.active);
}
@@ -976,7 +973,6 @@ pub fn paintHeaders(p: *Pardes, s: *Surface, arena: std.mem.Allocator) !void {
const chrome = p.chromeTheme();
// global tagbar: full width, top rows — unless a native shell has
// taken its commands over, in which case the rows are not ours to draw
- tagline.revealHeader(p);
if (s.region(.workspace_tag, Region.none)) |region| {
const top = if (p.header_focus and p.header_column == null) p.header_top else 0;
const layer = &s.tag_layers[MAX_PANES + MAX_COLS];
@@ -2286,12 +2282,9 @@ test "a body with context rows is painted once, and the grid's rows are what a p
const cells = try arena.allocator().alloc(Cell, s.cells.len);
@memset(cells, .{});
var plain: Surface = .{ .cols = s.cols, .rows = s.rows, .cells = cells };
- const saved = .{ pane.body_rows, pane.body_visible_rows, pane.context_row_limit };
- pane.body_rows = 0;
- pane.body_visible_rows = 0;
- pane.context_row_limit = null;
+ pane.view.layered = false;
try body_layer.renderBody(p, &plain, arena.allocator(), pane, p.rects[0], 0, true, null, null);
- pane.body_rows, pane.body_visible_rows, pane.context_row_limit = saved;
+ pane.view.layered = true;
for (0..body.rect.h) |row| {
const y = body.rect.y + row;
for (0..body.rect.w) |col| {
diff --git a/src/dump.zig b/src/dump.zig
index 0da2513f..f9f01950 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -880,12 +880,16 @@ fn applySettings(p: *Pardes, lines: []const []const u8) void {
/// The replacement core for a Restore of the dump at `from`, whose log says
/// so after its panes' `new`s, for a client that reconnects to read.
pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes {
+ const taped = pardes.tape.enter(p, .{ .restore = .{ .bytes = zon_bytes, .from = from } });
+ defer pardes.tape.leave(p, taped);
var opts = p.opts;
opts.cols = p.screen_w;
opts.rows = p.screen_h;
var old: OldSerials = .{};
const replacement = try initDump(p.gpa, opts, zon_bytes, p, &old);
replacement.pointRestoreAt(from);
+ // The tape follows the replacement from here (it is the session).
+ pardes.tape.adopt(replacement);
pardes.ctlfs.events.notePath(replacement, "restore", from);
// Which serial each pane and column had, for a script that held them.
for (replacement.panes, old.panes) |slot, was| if (slot) |pane| if (was != 0) {
@@ -900,7 +904,14 @@ pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes {
}
pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8) !*Pardes {
- return initDump(gpa, opts, zon_bytes, null, null);
+ pardes.tape.startFromEnv();
+ const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = zon_bytes } });
+ const p = initDump(gpa, opts, zon_bytes, null, null) catch |err| {
+ pardes.tape.leave(null, taped);
+ return err;
+ };
+ pardes.tape.leave(p, taped);
+ return p;
}
/// The serials a dump's panes and columns had, read as it is restored.
diff --git a/src/exec.zig b/src/exec.zig
index c14a648c..8f84d2c6 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -485,8 +485,10 @@ pub fn isFile(path: []const u8) bool {
pub fn kindOf(path: []const u8) ?std.Io.File.Kind {
var buf: [4096]u8 = undefined;
const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null;
- pardes.turn.yield();
- defer pardes.turn.back();
+ return pardes.tape.site(.kind, null, kindHost, .{z});
+}
+
+fn kindHost(z: [:0]const u8) ?std.Io.File.Kind {
const stat = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), z, .{}) catch return null;
return stat.kind;
}
@@ -1116,7 +1118,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[]
}
if (comptime pardes.hosted) {
var why: [320]u8 = undefined;
- if (@import("host_io.zig").Shell.refusal(want[0..@min(want.len, 200)], &why)) |refused| {
+ if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| {
var text: [340]u8 = undefined;
return p.reportFailure(p.active, std.fmt.bufPrint(&text, "Shell: {s}", .{refused}) catch "Shell: no such shell");
}
@@ -1161,10 +1163,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[]
for (p.panes, 0..) |slot, id| {
p.surface.body_layers[id].rows = 0;
const pane = slot orelse continue;
- pane.body_rows = 0;
- pane.body_visible_rows = 0;
- pane.context_row_limit = null;
- pane.wrap_n = 0;
+ pane.geometry_key = 0;
}
p.abandonPanelAnimations();
}
diff --git a/src/fs.zig b/src/fs.zig
index 9104c93f..e4373bf0 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -171,11 +171,7 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT);
if (req.op == .release) return .{ .tag = req.tag };
const io = std.Io.Threaded.global_single_threaded.io();
- const stat = stat: {
- pardes.turn.yield();
- defer pardes.turn.back();
- break :stat statPath(io, path, .{}) catch return Reply.fail(req.tag, E.NOENT);
- };
+ const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT);
const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 };
switch (req.op) {
.getattr => return .{ .tag = req.tag, .attr = attr },
@@ -199,32 +195,10 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
// Listed into a buffer of this request's own first; the shared
// reply buffer is another request's to use while the turn is out.
var listing: [16 * 1024]u8 = undefined;
- var fixed: std.heap.FixedBufferAllocator = .init(&listing);
- var listed: std.ArrayList(u8) = .empty;
- {
- pardes.turn.yield();
- defer pardes.turn.back();
- var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return Reply.fail(req.tag, E.NOTDIR);
- defer dir.close(io);
- var it = dir.iterate();
- var skip = req.off;
- while (it.next(io) catch return Reply.fail(req.tag, E.IO)) |entry| {
- var buf: [4096]u8 = undefined;
- const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue;
- var normalized_buf: [4096]u8 = undefined;
- const normalized = resolveOs(joined, &normalized_buf) orelse continue;
- const child = dir.statFile(io, entry.name, .{}) catch continue;
- if (skip > 0) {
- skip -= 1;
- continue;
- }
- const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1));
- tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name);
- if (listed.items.len >= @max(req.size, 512)) break;
- }
- }
+ const listed = pardes.tape.site(.os_listing, null, osListing, .{ io, path, req.off, req.size, &listing }) catch |err|
+ return Reply.fail(req.tag, if (err == error.NotDir) E.NOTDIR else E.IO);
const out = p.fs.stage(p.gpa);
- out.appendSlice(p.gpa, listed.items) catch return Reply.fail(req.tag, E.NOMEM);
+ out.appendSlice(p.gpa, listed) catch return Reply.fail(req.tag, E.NOMEM);
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } };
},
.read, .write, .setattr => {
@@ -237,37 +211,70 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
var z: [4096]u8 = undefined;
const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return Reply.fail(req.tag, E.NOENT);
var buf: [ninep_io.msize]u8 = undefined; // a read never asks for more than a frame
- const got = io: {
- pardes.turn.yield();
- defer pardes.turn.back();
- const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true });
- if (fd < 0) return Reply.fail(req.tag, E.PERM);
- defer _ = libc.close(fd);
- if (req.op == .setattr) {
- if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero");
- var truncated = attr;
- truncated.size = 0;
- return .{ .tag = req.tag, .attr = truncated };
- }
- if (req.off > std.math.maxInt(i64)) return tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold");
- if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return Reply.fail(req.tag, E.IO);
- if (req.op == .write) {
- const written = libc.write(fd, req.data.ptr, req.data.len);
- if (written < 0) return Reply.fail(req.tag, E.IO);
- return .{ .tag = req.tag, .written = @intCast(written) };
- }
- const got = libc.read(fd, &buf, @min(req.size, buf.len));
- if (got < 0) return Reply.fail(req.tag, E.IO);
- break :io @as(usize, @intCast(got));
+ const got = switch (pardes.tape.site(.os_rw, null, osReadWrite, .{ path_z, req, attr, &buf })) {
+ .reply => |reply| return reply,
+ .read => |bytes| bytes,
};
const out = p.fs.stage(p.gpa);
- out.appendSlice(p.gpa, buf[0..got]) catch return Reply.fail(req.tag, E.NOMEM);
- return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got) } };
+ out.appendSlice(p.gpa, got) catch return Reply.fail(req.tag, E.NOMEM);
+ return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got.len) } };
},
else => return Reply.fail(req.tag, E.PERM),
}
}
+/// A directory's listing under /os, the entries from `off` until `size`
+/// bytes of them are staged, into `listing`: the syscalls of a readdir.
+fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 {
+ var fixed: std.heap.FixedBufferAllocator = .init(listing);
+ var listed: std.ArrayList(u8) = .empty;
+ var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return error.NotDir;
+ defer dir.close(io);
+ var it = dir.iterate();
+ var skip = off;
+ while (it.next(io) catch return error.Io) |entry| {
+ var buf: [4096]u8 = undefined;
+ const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue;
+ var normalized_buf: [4096]u8 = undefined;
+ const normalized = resolveOs(joined, &normalized_buf) orelse continue;
+ const child = dir.statFile(io, entry.name, .{}) catch continue;
+ if (skip > 0) {
+ skip -= 1;
+ continue;
+ }
+ const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1));
+ tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name);
+ if (listed.items.len >= @max(size, 512)) break;
+ }
+ return listed.items;
+}
+
+const OsReadWrite = union(enum) { reply: Reply, read: []const u8 };
+
+/// The syscalls of an /os read, write or truncate: the reply, or the bytes
+/// read into `buf`.
+fn osReadWrite(path_z: [:0]const u8, req: Req, attr: Reply.Attr, buf: []u8) OsReadWrite {
+ const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true });
+ if (fd < 0) return .{ .reply = Reply.fail(req.tag, E.PERM) };
+ defer _ = libc.close(fd);
+ if (req.op == .setattr) {
+ if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero") };
+ var truncated = attr;
+ truncated.size = 0;
+ return .{ .reply = .{ .tag = req.tag, .attr = truncated } };
+ }
+ if (req.off > std.math.maxInt(i64)) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold") };
+ if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return .{ .reply = Reply.fail(req.tag, E.IO) };
+ if (req.op == .write) {
+ const written = libc.write(fd, req.data.ptr, req.data.len);
+ if (written < 0) return .{ .reply = Reply.fail(req.tag, E.IO) };
+ return .{ .reply = .{ .tag = req.tag, .written = @intCast(written) } };
+ }
+ const got = libc.read(fd, buf.ptr, @min(req.size, buf.len));
+ if (got < 0) return .{ .reply = Reply.fail(req.tag, E.IO) };
+ return .{ .read = buf[0..@intCast(got)] };
+}
+
pub fn validMountName(name: []const u8) bool {
if (name.len == 0 or name.len > name_capacity or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false;
if (std.mem.eql(u8, name, "os") or std.mem.eql(u8, name, "self")) return false;
@@ -687,8 +694,10 @@ test "a path with a part over 255 bytes is NameTooLong, never a panic" {
pub fn makeDirs(dir: []const u8) void {
if (comptime !platform_has_fs) return;
if (dir.len == 0) return;
- pardes.turn.yield();
- defer pardes.turn.back();
+ pardes.tape.site(.make_dirs, null, makeDirsHost, .{dir});
+}
+
+fn makeDirsHost(dir: []const u8) void {
_ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {};
}
@@ -798,6 +807,12 @@ pub fn dotRelative(word: []const u8) bool {
/// `~` to expand, or no home for it.
pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 {
if (comptime !pardes.hosted) return path;
+ if (path.len == 0 or path[0] != '~') return path;
+ // Home as the session that recorded found it (src/tape.zig).
+ return pardes.tape.value(.env, expandHomeHost, .{ path, out });
+}
+
+fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 {
return expandHomeFrom(path, out, std.c.getenv("HOME"));
}
@@ -846,8 +861,10 @@ pub fn resolveOs(path: []const u8, out: *[4096]u8) ?Resolved {
if (comptime !platform_has_fs) return null;
var z: [4096]u8 = undefined;
const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return null;
- pardes.turn.yield();
- defer pardes.turn.back();
+ return pardes.tape.site(.resolve, null, resolveHost, .{ path_z, out });
+}
+
+fn resolveHost(path_z: [:0]const u8, out: *[4096]u8) ?Resolved {
const resolved = realpath(path_z, out) orelse return null;
return .{ .path = std.mem.span(resolved), .dir = isDir(resolved) };
}
@@ -904,9 +921,7 @@ pub fn readLimit(p: *pardes.Pardes, path: []const u8, max_bytes: usize) ![]u8 {
if (!std.mem.eql(u8, name, mount.name)) continue;
// A peer answers on its own time, and the peer may be this very
// editor: the turn goes out with the request.
- pardes.turn.yield();
- defer pardes.turn.back();
- return ninep_io.Client.readLimit(p.gpa, mount.dial, remote_path, path, limit);
+ return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readLimit, .{ p.gpa, mount.dial, remote_path, path, limit });
}
return error.FileNotFound;
}
@@ -1233,8 +1248,10 @@ fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 {
const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{native}, 0) catch return error.PathTooLong;
// The path may be a mount this editor serves: the turn goes out with the
// syscalls, and the bytes come back into memory of the caller's own.
- pardes.turn.yield();
- defer pardes.turn.back();
+ return pardes.tape.site(.read_file, gpa, readHost, .{ gpa, path_z, limit });
+}
+
+fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u8 {
const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .NONBLOCK = true });
if (fd < 0) return switch (libc.errno(fd)) {
.ACCES, .PERM => error.PermissionDenied,
diff --git a/src/image.zig b/src/image.zig
index e334965a..20d6d6f0 100644
--- a/src/image.zig
+++ b/src/image.zig
@@ -931,6 +931,10 @@ const GridKey = struct {
};
pub const State = struct {
+ /// What the replay digest leaves out (src/walk.zig): the picture as
+ /// decoded from its bytes, off the loop as the render first wants it,
+ /// and the glyph art drawn from that.
+ pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "grid", "grid_key", "decoder", "decoded", "answer" } };
path: []u8,
glyph_art: bool = false,
pmode: PaletteMode = .commodore,
diff --git a/src/look.zig b/src/look.zig
index 1a574212..226c96e3 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -954,7 +954,7 @@ fn noteLookHover(p: *Pardes, col: u16, row: u16, body_hit: ?Mouse.BodyHit, tag_h
pub fn refreshLookHoverFromRaw(p: *Pardes) void {
if (!p.pointer_inside or !p.raw_hover_intent) return;
- const mapped = p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row) orelse
+ const mapped = p.pointerCell(p.pointer_raw_col, p.pointer_raw_row) orelse
return cancelLookHover(p);
p.hover_col = mapped.col;
p.hover_row = mapped.row;
diff --git a/src/main.zig b/src/main.zig
index 3d26d1fd..378dbea8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -333,6 +333,8 @@ fn webMain() !void {
fn nativeMain(std_init: std.process.Init) !void {
var init = std_init;
if (builtin.mode != .debug) init.gpa = pardes.memory.host(std.heap.c_allocator);
+ // A recorded session's tape replayed, no session of its own (src/tape.zig).
+ if (init.environ_map.get("PARDES_REPLAY")) |tape| std.process.exit(pardes.tape.replayMain(init.io, tape));
var stderr_probe: [64]u8 = undefined;
_ = std.debug.lockStderr(&stderr_probe);
std.debug.unlockStderr();
diff --git a/src/mouse.zig b/src/mouse.zig
index 2ff4fef3..67ebf7cf 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -380,7 +380,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool {
else => return false,
};
const bit: u3 = if (button) |b| @as(u3, 1) << @intCast(@backingInt(b)) else 0;
- const at = p.presentation.pointer(p.screen_w, p.screen_h, @min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1));
+ const at = p.pointerCell(@min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1));
var mods: panes.terminal.MouseMods = .{ .shift = m.shift, .alt = m.alt, .ctrl = m.ctrl };
var buf: [64]u8 = undefined;
if (p.mouse_grab) |*grab| {
@@ -535,7 +535,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void {
return;
}
p.raw_hover_intent = false;
- const mapped = p.presentation.pointer(p.screen_w, p.screen_h, raw_col, raw_row) orelse {
+ const mapped = p.pointerCell(raw_col, raw_row) orelse {
look.cancelLookHover(p);
if (m.kind == .release) {
if (p.header_sweep) |sweep| if (sweep.button == m.button) {
@@ -931,6 +931,7 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo
const r = p.rects[s.id];
const b = @backingInt(s.button);
const tag_rows: i32 = pane.tag_rows;
+ panes.File.geometry(pane);
if (pane.body_rows > 0 and pane.sel[b].r0 >= tag_rows) if (body_hit) |supplied| {
const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return;
pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER;
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index f9112ae2..33f816fe 100644
--- a/src/ninep/pane.zig
+++ b/src/ninep/pane.zig
@@ -1002,8 +1002,7 @@ pub fn nameBuffer(p: *Pardes, id: usize, full: []const u8, preserve_tag: bool) !
f.highlight_start = 0;
f.syntax_dirty = true;
f.context_revision = null;
- pane.context_rows = 0;
- pane.wrap_n = 0;
+ pane.geometry_key = 0;
look.invalidateLookHover(p, id);
p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } });
events.noteLog(p, .rename, pane);
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index 7b107ea7..bda2c1d1 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -1192,6 +1192,8 @@ fn linesOpen(target: Target, req: Req) bool {
/// The editor's step: the last lines of closed opens run, each as a write
/// of its own; a refusal is the log's err, as no open is left to answer.
pub fn runClosedLines(p: *Pardes) void {
+ const taped = pardes.tape.enter(p, .run_closed_lines);
+ defer pardes.tape.leave(p, taped);
for (p.fs.closed_lines.items) |*line| {
defer line.bytes.deinit(p.gpa);
const t = Node.target(line.node) orelse continue;
diff --git a/src/normal.zig b/src/normal.zig
index 9cac18b0..f214121a 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -713,6 +713,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void
.middle => {
// helix align_view_middle: the cursor's column to the middle
// of an unwrapped view
+ panes.File.geometry(pane);
if (pane.body_wrapped) return;
const line = text[modal.lineStartOffset(text, modal.lineAtOffset(text, cur))..modal.lineEndOffset(text, modal.lineAtOffset(text, cur))];
const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane))));
diff --git a/src/panes.zig b/src/panes.zig
index 51710d08..a782e3e0 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -24,6 +24,14 @@ const Key = pardes.Key;
pub const EditText = struct { text: []u8, row0: i32 };
pub const Pane = struct {
+ /// What the replay digest leaves out (src/walk.zig): the view's geometry,
+ /// made again from the rest (File.geometry); the 9P server's record of
+ /// the pane's opens and readers.
+ pub const replica = .{
+ .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices" },
+ .authority = .{"fs"},
+ .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } },
+ };
/// The words that throw unsaved text away (builtins.zig warnModified).
pub const Discarding = enum { Exit, Restore, get, Del, Delcol };
@@ -128,7 +136,12 @@ pub const Pane = struct {
/// The last command line pardes typed into this terminal (an exec, a
/// middle click, a pty/run): its first word, which Kill matches, and
/// the shell's command count its start (C mark) reaches.
- sent_command: ?struct { word: [64]u8 = undefined, len: u8 = 0, seq: u32 = 0 } = null,
+ sent_command: ?struct {
+ pub const replica = .{ .len = .{.{ "word", "len" }} };
+ word: [64]u8 = undefined,
+ len: u8 = 0,
+ seq: u32 = 0,
+ } = null,
/// A command pane (exec.zig runCommand): the line its child runs as
/// `$SHELL -c`, owned, where an interactive terminal has null.
command: ?[]u8 = null,
@@ -245,6 +258,12 @@ pub const Pane = struct {
body_rows: u16 = 0,
body_visible_rows: u16 = 0,
context_row_limit: ?u16 = null,
+ /// What the geometry above was made for (File.geometry), and the bytes
+ /// its text takes; 0 has it made again by whoever reads it next.
+ geometry_key: u64 = 0,
+ geometry_len: usize = 0,
+ /// The session's part of the geometry, as of the last step (`sync`).
+ view: struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 } = .{},
sel: [3]Sel = @splat(.{}),
pointer_selections: [3]?PointerSelection = @splat(null),
next_pointer_selection: u64 = 0,
@@ -292,6 +311,7 @@ pub const Pane = struct {
/// Older message lines kept on screen under a new one.
pub const message_stack = 3;
pub const OlderMessage = struct {
+ pub const replica = .{ .len = .{.{ "text", "len" }} };
text: [256]u8 = undefined,
len: u16 = 0,
life: MessageLife = .{},
@@ -515,6 +535,7 @@ pub const Pane = struct {
}
pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } {
+ File.geometry(pane);
if (pane.wrap_n > 0 and vr >= 0 and vr < @as(i32, pane.wrap_n))
return .{ .line = pane.wrap_line[@intCast(vr)], .at = pane.wrap_col[@intCast(vr)], .pad = pane.wrap_pad[@intCast(vr)] };
// unwrapped: rows ARE lines, and the byte column a row starts at is the
@@ -523,6 +544,7 @@ pub const Pane = struct {
}
pub fn wrapRow(pane: *Pane, line: i32, col: i32) struct { row: i32, at: i32, pad: i32 = 0 } {
+ File.geometry(pane);
if (pane.wrap_n == 0) return .{ .row = line - pane.scroll(), .at = pane.hscroll };
var i: u16 = 0;
while (i < pane.wrap_n) : (i += 1) {
@@ -572,10 +594,11 @@ pub const Pane = struct {
}
pub fn ensureCursorVisible(pane: *Pane) void {
+ File.geometry(pane);
const off = pane.scroll();
var in_context = false;
- // Maps survive until the next render. A jump may have changed scroll
- // already, so only treat an ancestor as pinned in the matching view.
+ // The map is the view's as it is now (File.geometry): an ancestor is
+ // pinned only in the view it was found for.
if (pane.context_rows < pane.wrap_n and pane.wrap_line[pane.context_rows] == off) {
for (pane.wrap_line[0..pane.context_rows]) |row| {
if (row == pane.body.cur_row) {
diff --git a/src/pardes.zig b/src/pardes.zig
index 2bee1f98..b5e729bb 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -61,6 +61,8 @@ pub const config = @import("config.zig");
pub const pdf_enabled = panes.Pdf.enabled;
pub const pdf = panes.Pdf.pdf;
pub const memory = @import("memory.zig");
+pub const tape = @import("tape.zig");
+pub const walk = @import("walk.zig");
pub const image = @import("image.zig");
pub const dump = @import("dump.zig");
pub const lsp = @import("lsp/lsp.zig");
@@ -165,6 +167,8 @@ pub const Turn = struct {
/// a step. Nests: a directory listing that resolves each entry yields
/// inside a yield, and only the outermost one lets go.
pub fn yield(t: *Turn) void {
+ // A replica makes no syscall: each is a site of the tape's.
+ if (tape.replaying()) @panic("tape: a syscall that is no tape site, made replaying");
const io = t.io orelse return;
yield_depth += 1;
if (yield_depth != 1) return;
@@ -504,6 +508,7 @@ test {
_ = @import("Finder.zig");
_ = @import("Tables.zig");
_ = @import("Handover.zig");
+ _ = @import("walk.zig");
}
pub const Builtin = builtins.registry.Builtin();
@@ -759,6 +764,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest
pane.file.?.tree_context = true;
pane.file.?.scroll = 8;
pane.msg_len = 0;
+ p.sync();
_ = try p.render(p.scratch.allocator());
try std.testing.expect(p.settings.tree_context_tag_style);
const full_height = p.surface.body_layers[0].viewport.h;
@@ -766,6 +772,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest
try std.testing.expectEqual(FontRole.tagline, p.surface.body_layers[0].cells[0].style.font_role);
pane.msg[0] = '!';
pane.msg_len = 1;
+ p.sync();
_ = try p.render(p.scratch.allocator());
// An overlay takes nothing: the message is a chip OVER the body's first
// row, so the viewport keeps every row it had.
@@ -783,10 +790,12 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest
try std.testing.expectEqualStrings("!", band.cells[p.tagCapacity(band.viewport.w) - 2].grapheme());
pane.msg_len = 0;
p.leader_on = true;
+ p.sync();
_ = try p.render(p.scratch.allocator());
try std.testing.expectEqual(full_height, p.surface.body_layers[0].viewport.h);
p.leader_on = false;
try std.testing.expect(p.executeBuiltinLine(0, "TreeContextTagStyle"));
+ p.sync();
_ = try p.render(p.scratch.allocator());
try std.testing.expectEqual(@as(u16, 0), p.surface.body_layers[0].rows);
try std.testing.expectEqual(@as(u16, 0), pane.body_rows);
@@ -3763,6 +3772,7 @@ pub const Effect = union(enum) {
fn Buf(comptime n: usize) type {
return struct {
+ pub const replica = .{ .len = .{.{ "data", "len" }} };
data: [n]u8 = undefined,
len: u16 = 0,
pub const cap = n;
@@ -4701,6 +4711,26 @@ pub const Pardes = struct {
/// The message row's entry points, called from everywhere as
/// `p.setMessage(...)`; they live with the rest of it in Messages.zig.
+ /// What the replay digest leaves out (src/walk.zig): what drawing
+ /// keeps, what is recomputed, and what only the session that performs
+ /// effects has.
+ pub const replica = .{
+ .local = .{
+ // A frame, and the animations and timings that make it.
+ "surface", "presentation", "frames", "chrome_animation", "lifts", "lift_serials",
+ "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "frame_arena", "scratch",
+ "needs_frame", "present_skipped", "shell_continuous", "native_image_patches",
+ // Drained by every pump before its digest; a replica's queues
+ // are its own.
+ "effects", "in_q",
+ },
+ .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" },
+ // The host's handles, the 9P server's fids and log, the store file
+ // every window of the user shares (and Recent read from it).
+ .authority = .{ "host", "fs", "store", "recent" },
+ .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } },
+ };
+
pub const setStatus = Messages.setStatus;
pub const setMessage = Messages.setMessage;
pub const reportError = Messages.reportError;
@@ -4739,12 +4769,26 @@ pub const Pardes = struct {
return p;
}
+ /// A new session, its tape written from here when PARDES_RECORD asks.
pub fn init(gpa: std.mem.Allocator, opts: Options) !*Pardes {
+ tape.startFromEnv();
+ const taped = tape.enter(null, .{ .init = opts });
+ const p = initSession(gpa, opts) catch |err| {
+ tape.leave(null, taped);
+ return err;
+ };
+ tape.leave(p, taped);
+ return p;
+ }
+
+ fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes {
// A new session draws a tab at the default until `Tab` says otherwise.
config.tab_now = config.tab_width;
const p = try create(gpa, opts);
errdefer p.deinit();
p.boot = Boot.of(opts);
+ // Asked once, at the start, whoever asks first after.
+ _ = p.sessionDir();
switch (p.boot) {
.document => {
const path = opts.file.?;
@@ -4877,7 +4921,9 @@ pub const Pardes = struct {
/// The store read once at start: a new session's, Recent from it.
pub fn loadRecent(p: *Pardes) void {
- p.store = Store.init(p.opts.store_path);
+ // A replica's store is its own, in memory: the recording session's
+ // file is every window's of its user (src/tape.zig).
+ p.store = Store.init(if (tape.replaying()) null else p.opts.store_path);
p.store.load(p);
}
@@ -4941,6 +4987,9 @@ pub const Pardes = struct {
}
pub fn deinit(p: *Pardes) void {
+ const taped = tape.enter(p, .{ .deinit = tape.current(p) });
+ tape.forget(p);
+ defer tape.leave(null, taped);
if (comptime hosted) if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path));
// Every file open now is closed with its dot, and the list kept.
for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane);
@@ -5001,6 +5050,8 @@ pub const Pardes = struct {
/// shell feedback after write_dump: remember where the dump landed (the
/// topbar grows a `Restore <path>` word pointing at it)
pub fn setLastDump(p: *Pardes, path: []const u8) void {
+ const taped = tape.enter(p, .{ .set_last_dump = path });
+ defer tape.leave(p, taped);
p.pointRestoreAt(path);
ctlfs.events.notePath(p, "dump", path);
}
@@ -5028,6 +5079,8 @@ pub const Pardes = struct {
/// the shell polls this each frame: a pending Restore's dump path, or null
pub fn takeRestore(p: *Pardes) ?[]const u8 {
+ const taped = tape.enter(p, .take_restore);
+ defer tape.leave(p, taped);
const r = p.restore_req;
p.restore_req = null;
return r;
@@ -5386,6 +5439,8 @@ pub const Pardes = struct {
/// The shell reports the spawned pane's working directory (and later cwd
/// changes it observes, e.g. via /proc/<pid>/cwd before each frame).
pub fn setCwd(p: *Pardes, id: usize, cwd: []const u8) void {
+ const taped = tape.enter(p, .{ .set_cwd = .{ .id = id, .cwd = cwd } });
+ defer tape.leave(p, taped);
const pane = p.panes[id] orelse return;
// A command pane stays the directory's it was run for (newCommand).
if (pane.command != null) return;
@@ -5415,6 +5470,8 @@ pub const Pardes = struct {
}
pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void {
+ const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } });
+ defer tape.leave(p, taped);
if (id < MAX_PANES) if (p.panes[id]) |pane| {
ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran
pane.shell_failed = false;
@@ -5511,10 +5568,14 @@ pub const Pardes = struct {
/// file reloading under it, a theme file rewritten on disk. `pump` draws
/// only when it has a reason to, and this is the reason.
pub fn invalidate(p: *Pardes) void {
+ const taped = tape.enter(p, .invalidate);
+ defer tape.leave(p, taped);
p.needs_frame = true;
}
pub fn reloadWatchedFile(p: *Pardes, id: u8, bytes: []const u8) bool {
+ const taped = tape.enter(p, .{ .reload_watched_file = .{ .id = id, .bytes = bytes } });
+ defer tape.leave(p, taped);
p.invalidate();
look.invalidateLookHover(p, id);
const applied = p.applyWatchedFileChanged(id, bytes);
@@ -5531,6 +5592,8 @@ pub const Pardes = struct {
}
pub fn nextEffect(p: *Pardes) ?Effect {
+ const taped = tape.enter(p, .next_effect);
+ defer tape.leave(p, taped);
p.refillPendingWrites();
if (p.effects_len == 0) {
p.effects_head = 0;
@@ -5667,6 +5730,8 @@ pub const Pardes = struct {
/// on it, or written to it from now on, hears `error shell gone`
/// rather than wait for a prompt that never comes.
pub fn shellFailed(p: *Pardes, id: u8, err: anyerror) void {
+ const taped = tape.enter(p, .{ .shell_failed = .{ .id = id, .err = err } });
+ defer tape.leave(p, taped);
const pane = p.panes[id] orelse return;
pane.shell_failed = true;
ctlfs.pty.shellGone(p, pane, false);
@@ -5688,6 +5753,8 @@ pub const Pardes = struct {
/// A shell started again in a terminal whose shell runs (pty/ctl's
/// exec) did not start: the host kept the running one, so only said.
pub fn restartFailed(p: *Pardes, id: u8, err: anyerror) void {
+ const taped = tape.enter(p, .{ .restart_failed = .{ .id = id, .err = err } });
+ defer tape.leave(p, taped);
if (p.panes[id] == null) return;
p.sayShellFailure(id, err);
}
@@ -5700,9 +5767,7 @@ pub const Pardes = struct {
// The script is there; the program its `#!` names is not.
var said: [320]u8 = undefined;
var interp: [256]u8 = undefined;
- var buf: [std.fs.max_path_bytes]u8 = undefined;
- const Shell = if (comptime hosted) @import("host_io.zig").Shell else void;
- const name: ?[]const u8 = if (comptime hosted) (if (Shell.find(pane.shell orelse p.shellBin(), &buf)) |f| Shell.missingInterpreter(f, &interp) else null) else null;
+ const name: ?[]const u8 = if (comptime hosted) tape.value(.interpreter, missingInterpreter, .{ pane.shell orelse p.shellBin(), &interp }) else null;
p.reportFailure(id, if (name) |n| std.fmt.bufPrint(&said, "shell: interpreter {s} not found", .{n}) catch "shell: interpreter not found" else "shell: interpreter not found");
} else {
p.reportError(id, "shell", err);
@@ -5712,6 +5777,13 @@ pub const Pardes = struct {
p.noteLateFailure(id);
}
+ /// The program the shell's `#!` names that is not there, in `out`.
+ fn missingInterpreter(bin: []const u8, out: []u8) ?[]const u8 {
+ const Shell = @import("host_io.zig").Shell;
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ return Shell.missingInterpreter(Shell.find(bin, &buf) orelse return null, out);
+ }
+
/// A Save makes the directories its file goes in, and any above them,
/// first: one rule, for the pane's own name and `Save path` alike. A
/// directory it cannot make leaves the write to fail and say why.
@@ -5729,11 +5801,13 @@ pub const Pardes = struct {
const local = if (comptime hosted) filesystem.localPath(dir) else null;
const kind: ?std.Io.File.Kind = if (comptime hosted) (if (local) |l| exec.kindOf(l) else .directory) else .directory;
const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory;
- const denied = missing and (if (comptime hosted) (if (local) |l| filesystem.deniedAbove(l) else false) else false);
+ const denied = missing and (if (comptime hosted) (if (local) |l| tape.value(.denied, filesystem.deniedAbove, .{l}) else false) else false);
return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied };
}
pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void {
+ const taped = tape.enter(p, .{ .save_failed = .{ .id = id, .path = path, .err = err } });
+ defer tape.leave(p, taped);
if (p.panes[id]) |pane| if (pane.file) |*f| {
// The `-%` spelling fs.zig already uses for "make this dirty".
f.saved_revision = f.revision -% 1;
@@ -5755,6 +5829,8 @@ pub const Pardes = struct {
/// log, and the 9P write that asked for it fails with it, as a failed
/// Save's does.
pub fn dumpFailed(p: *Pardes, path: []const u8, err: anyerror) void {
+ const taped = tape.enter(p, .{ .dump_failed = .{ .path = path, .err = err } });
+ defer tape.leave(p, taped);
var what: [limits.host_path_cap + 8]u8 = undefined;
const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path}) catch "Dump";
// Into a DumpDir that is not there: said so, ENOENT, as a Save is;
@@ -5797,6 +5873,8 @@ pub const Pardes = struct {
}
pub fn perform(p: *Pardes, e: Effect) void {
+ const taped = tape.enter(p, .{ .perform = e });
+ defer tape.leave(p, taped);
// A Save performed on one twin is every twin's (File.syncTwins).
defer panes.File.syncTwins(p);
const v = p.host.vtable;
@@ -5927,16 +6005,23 @@ pub const Pardes = struct {
}
}
- pub fn pump(p: *Pardes, h: Host) !void {
- p.host = h;
+ pub fn pump(p: *Pardes, host: Host) !void {
+ p.host = host;
+ const taped = tape.enter(p, .pump);
+ defer tape.leave(p, taped);
+ // The host as the tape sees it, while it records.
+ const h = p.host;
const v = h.vtable;
if (v.wait_input) |f| {
// Asleep until input or until core animation next needs a
// frame; 0 is no timeout at all.
// A render-time animation draws every loop: the wait only takes
// what input is there.
+ // The clock read whether or not it is wanted: what a replica
+ // waits on is its own (a picture decoding wakes only a frame
+ // that draws it), and the tape has the same calls either way.
+ const now = if (v.now) |clock| clock(h.ctx) else p.now_ns;
const timeout: u32 = if (p.continuous()) 1 else if (p.nextWake()) |due| wake: {
- const now = if (v.now) |clock| clock(h.ctx) else p.now_ns;
const ms = ((due -| now) + std.time.ns_per_ms - 1) / std.time.ns_per_ms;
break :wake @intCast(std.math.clamp(ms, 1, std.math.maxInt(u32)));
} else 0;
@@ -5972,6 +6057,7 @@ pub const Pardes = struct {
const surface = try p.render(p.frame_arena.allocator());
p.frames.mark_render = FrameStats.nowNs();
if (v.present) |f| f(h.ctx, surface);
+ Stream.askAhead(p);
// Settled before post_present: a shell ticks there, and what that
// tick moves owes the next frame.
const skipped = p.present_skipped;
@@ -5990,6 +6076,8 @@ pub const Pardes = struct {
/// that changes a pane costs a frame, which is why a round trip on a
/// local socket costs microseconds and not a vsync.
pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply {
+ const taped = tape.enter(p, .{ .serve_fs = req });
+ defer tape.leave(p, taped);
// The held write of an Edit whose commands run, asked again by a
// retry of its park: still waiting, or its answer, never run twice.
if (p.fs.edit_hold) |*h| if (h.names(req)) {
@@ -6028,6 +6116,8 @@ pub const Pardes = struct {
}
pub fn update(p: *Pardes, ev: Event) void {
+ const taped = tape.enter(p, .{ .update = ev });
+ defer tape.leave(p, taped);
defer panes.File.syncTwins(p);
defer p.reportFocus();
// A tick with nothing animating is the one event that cannot change
@@ -6074,6 +6164,7 @@ pub const Pardes = struct {
.tick => {
Stream.step(p);
Messages.advanceMessages(p);
+ for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| f.tables.step(pane.gpa, f);
for (0..p.panes.len) |id| exec.flushReplEnter(p, id, false);
},
else => {},
@@ -6661,10 +6752,16 @@ pub const Pardes = struct {
pub fn sessionDir(p: *Pardes) []const u8 {
if (p.session_dir_len) |n| return p.session_dir[0..n];
if (comptime !hosted) return "/";
- if (std.c.getcwd(&p.session_dir, p.session_dir.len) == null) return "/";
- const n: u16 = @intCast(std.mem.indexOfScalar(u8, &p.session_dir, 0) orelse return "/");
- p.session_dir_len = n;
- return p.session_dir[0..n];
+ // The directory the session that recorded was started in.
+ const dir = tape.value(.cwd, processDir, .{&p.session_dir}) orelse return "/";
+ std.mem.copyForwards(u8, &p.session_dir, dir);
+ p.session_dir_len = @intCast(dir.len);
+ return p.session_dir[0..dir.len];
+ }
+
+ fn processDir(buf: *[limits.host_path_cap]u8) ?[]const u8 {
+ if (std.c.getcwd(buf, buf.len) == null) return null;
+ return buf[0 .. std.mem.indexOfScalar(u8, buf, 0) orelse return null];
}
/// The directory a word run from pane `id`'s text or tag runs in, or,
@@ -7007,6 +7104,8 @@ pub const Pardes = struct {
/// What was said of a failure just now on pane `id` is also the waiting
/// 9P write's (9p_io, `fs.late_failure`).
pub fn noteLateFailure(p: *Pardes, id: usize) void {
+ const taped = tape.enter(p, .{ .note_late_failure = id });
+ defer tape.leave(p, taped);
// Said by reportFailure just now: its words fitted whole, the
// reason kept; else what the message row has.
const said = if (p.fs.said_at == p.fs.failures and p.fs.said_len > 0)
@@ -7182,9 +7281,11 @@ pub const Pardes = struct {
// ---- dumb undo/redo: whole-state snapshots, one per edit op ----
pub fn acknowledgePanelPresentation(p: *Pardes, tracks: []const animation.Track) void {
+ const taped = tape.enter(p, .{ .acknowledge_panel_presentation = tracks });
+ defer tape.leave(p, taped);
p.presentation.acknowledge(p, tracks);
if (p.pointer_inside and p.drag != .none) {
- if (p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row)) |mapped| {
+ if (p.pointerCell(p.pointer_raw_col, p.pointer_raw_row)) |mapped| {
p.hover_col = mapped.col;
p.hover_row = mapped.row;
mouse.dragUpdate(p, mapped.col, mapped.row, p.pointer_body_hit);
@@ -7193,6 +7294,14 @@ pub const Pardes = struct {
look.refreshLookHoverFromRaw(p);
}
+ /// The cell of the session's grid a pointer at this cell of the frame
+ /// is over, seen through the frame (a pane sliding in): the recording
+ /// session's answer, whose frames these were (src/tape.zig). Null over
+ /// a pane on its way out.
+ pub fn pointerCell(p: *const Pardes, col: u16, row: u16) ?Presentation.CellPosition {
+ return tape.value(.pointer, Presentation.pointer, .{ &p.presentation, p.screen_w, p.screen_h, col, row });
+ }
+
pub fn abandonPanelAnimations(p: *Pardes) void {
p.presentation.cancel();
look.cancelLookHover(p);
@@ -7588,6 +7697,10 @@ pub const Pardes = struct {
state.reply_len = 0;
};
const r = p.rects[id];
+ // The session's part of the pane's view geometry (File.geometry),
+ // and its context declarations.
+ if (pane.file) |*f| panes.File.refreshContext(p, pane, f);
+ pane.view = .{ .wrap = p.settings.wrap, .layered = p.settings.tree_context_tag_style, .body_h = p.row_metrics.body_h, .tagline_h = p.row_metrics.tagline_h, .rect_h = r.h };
// The tag takes a row per line, leaving the body one row if it
// had any; a folded pane shows its first line only.
// Measured on the stack: this runs on every event, and not
@@ -7661,6 +7774,9 @@ pub const Pardes = struct {
// columns do (Dir.fit).
if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) panes.Dir.fit(p, pane, pane.cols);
panes.terminal.releasePendingCommandIfReady(p, id, pane);
+ // Which bands a pane shows is the step's, as its geometry is:
+ // what is drawn under them, and where a click lands, read it.
+ Messages.collectNotices(p, pane, r, id);
// The greeting `ls` of a new terminal in a directory is typed at
// its shell's FIRST prompt, or never: typed later it would land
// in whatever runs then, or in the middle of a line someone
@@ -7675,6 +7791,9 @@ pub const Pardes = struct {
},
};
}
+ // A header's caret kept in its view, as a pane's is: the step's, not
+ // the frame's (paintHeaders draws what this leaves).
+ tagline.revealHeader(p);
}
pub fn theme(p: *const Pardes) *const Theme {
@@ -7780,6 +7899,8 @@ pub const Pardes = struct {
/// the look-hover delay) is jumped to its end in one step and drawn
/// once. Idle, time is not banked: the next animation starts from now.
pub fn advance(p: *Pardes, now_ns: u64) void {
+ const taped = tape.enter(p, .{ .advance = now_ns });
+ defer tape.leave(p, taped);
// The first time a shell's clock is seen, core time starts there.
const started = p.clock_started;
p.clock_started = true;
@@ -7787,7 +7908,10 @@ pub const Pardes = struct {
if (!started) p.stepped_ns = now_ns;
const frame = animation.frame_ns;
var moving: u32 = 0;
- while (p.nextWake()) |due| {
+ // The session's ticks are the recording session's: a picture still
+ // decoding or a fade still moving asks for frames the way a message
+ // waiting to go does, and a replica that draws nothing has none.
+ while (tape.value(.wake, nextWake, .{p})) |due| {
if (due > now_ns) break;
// Only waiting until `due`: its counters move there at once.
const held = (due - p.stepped_ns) / frame;
@@ -8136,8 +8260,12 @@ test "a prompt chip is a band with its caret, still on the grid, and only the ch
// Inside it is chrome: mapped as body it would land on Sel row 0, which is
// the TAG row, so a sweep there would highlight the tagline instead.
+ // The sweep closed the prompt, and its chip with it: open it again.
pane.clearPointerSelection(0);
pane.sel[0] = .{};
+ p.startPrompt(pane, &pane.body, .{ .search = "/" });
+ p.sync();
+ try std.testing.expectEqual(left, pane.notices.left[0]);
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = r.x + r.w - 3, .row = row } });
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = r.x + r.w - 2, .row = row } });
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = r.x + r.w - 2, .row = row } });
@@ -8523,6 +8651,9 @@ test "Esc back into a file leaves its view where it was" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "src/memory.zig" });
defer p.deinit();
+ // Rows are lines: the centring below counts lines, and a wrapped line
+ // above the cursor would push it out of the view it centres.
+ p.settings.wrap = false;
p.update(.{ .resize = .{ .cols = 80, .rows = 24 } });
p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc
const shell = p.active;
diff --git a/src/pdf.zig b/src/pdf.zig
index 18037bb7..d3796d70 100644
--- a/src/pdf.zig
+++ b/src/pdf.zig
@@ -703,6 +703,9 @@ pub const Document = struct {
handle: *c.pardes_pdf_document,
pages: usize,
+ /// The open document is MuPDF's, made from the file's bytes (src/walk.zig).
+ pub const replica = .{ .derived = .{"handle"} };
+
pub fn deinit(document: *Document) void {
c.pardes_pdf_close(document.handle);
document.* = undefined;
diff --git a/src/pdf_view.zig b/src/pdf_view.zig
index f6d26eac..4ddbf299 100644
--- a/src/pdf_view.zig
+++ b/src/pdf_view.zig
@@ -84,6 +84,7 @@ pub const TintKey = if (enabled) struct {
pub const Highlight = if (enabled) pdf.Highlight else void;
pub const Highlights = if (enabled) struct {
const Page = struct { page: usize, items: []const Highlight };
+ pub const replica = .{ .len = .{.{ "pages", "len" }} };
pages: [3]Page = undefined,
len: usize = 0,
/// Repaint whenever the set changes, not only when a page's raster was
@@ -619,6 +620,9 @@ pub const SectionsOutput = if (enabled) struct {
pub const SelectionUpdate = enum { failed, stationary, unchanged, changed };
pub const State = if (enabled) struct {
+ /// What the replay digest leaves out (src/walk.zig): the pages' pixels,
+ /// rendered from the document, and the lanes rendering them.
+ pub const replica = .{ .derived = .{ "rasters", "rasters_len" }, .local = .{"lanes"} };
path: []u8,
document: Document,
page: usize = 0,
diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig
index 73bac520..37cf22d0 100644
--- a/src/selection_pipe.zig
+++ b/src/selection_pipe.zig
@@ -184,6 +184,7 @@ pub const Tasks = struct {
future: std.Io.Future(void),
};
+ pub const replica = .{ .len = .{.{ "items", "len" }} };
items: [capacity]Task = undefined,
len: usize = 0,
diff --git a/src/tape.zig b/src/tape.zig
new file mode 100644
index 00000000..eaf1a9fa
--- /dev/null
+++ b/src/tape.zig
@@ -0,0 +1,741 @@
+//! The recorder and its tape (docs/next-daemon.md, phase 1). Recording,
+//! everything the core consumes is appended in the order it consumed it,
+//! which is the turn's order (a 9P request served while a step is out in a
+//! syscall lands inside that syscall's record):
+//!
+//! - each call the host makes into the core (`Input`), with its arguments;
+//! - each call the core makes out into the host -- a Host function, or a
+//! syscall of its own (`Site`) -- and what it returned;
+//! - each field the host wrote in the core between calls (`watched`);
+//! - after every call the host made at the top, the session's digest.
+//!
+//! Replaying (`PARDES_REPLAY=<tape> pardes`, the same build), a core takes
+//! its inputs off the tape instead: a host call answers what it answered, a
+//! site returns what it returned, nothing is performed and no syscall is
+//! made, and each digest is checked against the recorded one -- by two
+//! replicas at different addresses and a third that never draws.
+//!
+//! The local path pays a null check a call: `on` is null unless
+//! PARDES_RECORD names where to write, `<prefix><pid>.tape`.
+const std = @import("std");
+const pardes = @import("pardes.zig");
+const walk = @import("walk.zig");
+const Pardes = pardes.Pardes;
+const VTable = pardes.Host.VTable;
+const libc = std.c;
+
+pub var on: ?*Tape = null;
+
+/// Every call into the core a host makes (a 9P connection task's too).
+pub const Input = union(enum) {
+ init: pardes.Options,
+ init_from_dump: struct { opts: pardes.Options, bytes: []const u8 },
+ restore: struct { bytes: []const u8, from: []const u8 },
+ /// Whether it was the session's own core, or one a Restore replaced.
+ deinit: bool,
+ update: pardes.Event,
+ serve_fs: pardes.ctlfs.Req,
+ pump,
+ perform: pardes.Effect,
+ next_effect,
+ advance: u64,
+ render,
+ take_restore,
+ set_cwd: struct { id: usize, cwd: []const u8 },
+ acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool },
+ shell_failed: struct { id: u8, err: anyerror },
+ restart_failed: struct { id: u8, err: anyerror },
+ save_failed: struct { id: u8, path: []const u8, err: anyerror },
+ dump_failed: struct { path: []const u8, err: anyerror },
+ set_last_dump: []const u8,
+ set_message: struct { id: usize, text: []const u8 },
+ set_status: struct { id: usize, text: []const u8 },
+ report_error: struct { id: usize, operation: []const u8, err: anyerror },
+ reload_watched_file: struct { id: u8, bytes: []const u8 },
+ note_late_failure: usize,
+ invalidate,
+ acknowledge_panel_presentation: []const pardes.animation.Track,
+ run_closed_lines,
+ file_deleted: u8,
+};
+
+/// The core's own syscalls (each `turn.yield()`s around itself), numbered
+/// after the Host functions on the tape.
+pub const Site = enum(u16) {
+ // Out of the turn (`site`).
+ os_stat,
+ os_listing,
+ os_rw,
+ make_dirs,
+ resolve,
+ read_mount,
+ read_file,
+ dir_listing,
+ kind,
+ probe,
+ preview,
+ // Read in place (`value`).
+ clock,
+ budget,
+ wake,
+ env,
+ interpreter,
+ denied,
+ shell,
+ mtime,
+ cwd,
+ pointer,
+};
+
+/// Fields a host writes straight into the core between its calls.
+const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active" };
+
+const Kind = enum(u8) { input, done, host, ret, set, digest };
+const host_fns = @typeInfo(VTable).@"struct".field_names;
+/// The `set` that says which Host functions the recording host had.
+const set_mask = 0xff;
+
+pub const Tape = struct {
+ gpa: std.mem.Allocator,
+ replay: bool,
+ /// The core whose digest is taken (a Restore replaces it).
+ core: ?*Pardes = null,
+ /// The host has the turn: a call into the core now is an input, and a
+ /// call out of it is the host's to answer.
+ host_side: bool = true,
+ depth: u32 = 0,
+ vtable: VTable = .{},
+ // Recording.
+ fd: c_int = -1,
+ buf: std.ArrayList(u8) = .empty,
+ scratch: std.Io.Writer.Allocating,
+ real: pardes.Host = .{},
+ seen: [watched.len]u64 = @splat(0),
+ // Replaying.
+ in: walk.Reader = .{ .bytes = &.{} },
+ arena: std.heap.ArenaAllocator,
+ last_ret: walk.Reader = .{ .bytes = &.{} },
+ mask: u64 = 0,
+ headless: bool = false,
+ digests: u64 = 0,
+ diverged: ?[]const u8 = null,
+ /// The input being applied, for a divergence's report.
+ applying: []const u8 = "",
+ /// PARDES_RECORD_EVERY: a digest after every input, a nested one's too
+ /// (to find the step a divergence starts at), not only the host's last.
+ every: bool = false,
+
+ fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void {
+ t.scratch.clearRetainingCapacity();
+ walk.encode(T, v, &t.scratch.writer) catch @panic("tape: out of memory");
+ const payload = t.scratch.written();
+ const len: u32 = @intCast(payload.len);
+ t.buf.append(t.gpa, @intFromEnum(kind)) catch @panic("tape: out of memory");
+ t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory");
+ t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory");
+ if (t.buf.items.len > 1 << 20) t.flush();
+ }
+
+ fn flush(t: *Tape) void {
+ var at: usize = 0;
+ while (at < t.buf.items.len) {
+ const n = libc.write(t.fd, t.buf.items[at..].ptr, t.buf.items.len - at);
+ if (n <= 0) break;
+ at += @intCast(n);
+ }
+ t.buf.clearRetainingCapacity();
+ }
+
+ /// What the host wrote between calls, said before the core goes on.
+ fn noteWrites(t: *Tape) void {
+ const p = t.core orelse return;
+ inline for (watched, 0..) |name, i| {
+ const now = walk.digest(@FieldType(Pardes, name), &@field(p, name));
+ if (now != t.seen[i]) {
+ t.seen[i] = now;
+ const set: struct { u8, @FieldType(Pardes, name) } = .{ i, @field(p, name) };
+ t.record(.set, @TypeOf(set), &set);
+ }
+ }
+ }
+
+ fn seeFields(t: *Tape) void {
+ const p = t.core orelse return;
+ inline for (watched, 0..) |name, i| t.seen[i] = walk.digest(@FieldType(Pardes, name), &@field(p, name));
+ }
+
+ /// The host's functions go through the tape while it records, whoever
+ /// set `p.host` last.
+ fn wrap(t: *Tape, p: *Pardes) void {
+ if (p.host.vtable == &t.vtable) return;
+ // The same host given again (each pump is given its host): only
+ // the wrapper put back.
+ if (p.host.vtable == t.real.vtable and p.host.ctx == t.real.ctx) {
+ p.host = .{ .ctx = t, .vtable = &t.vtable };
+ return;
+ }
+ t.real = p.host;
+ var mask: u64 = 0;
+ inline for (host_fns, 0..) |f, i| if (@field(t.real.vtable.*, f) != null) {
+ mask |= @as(u64, 1) << i;
+ };
+ p.host = t.hostOf(mask);
+ const set: struct { u8, u64 } = .{ set_mask, mask };
+ t.record(.set, @TypeOf(set), &set);
+ }
+
+ /// The tape as a host with these functions.
+ fn hostOf(t: *Tape, mask: u64) pardes.Host {
+ t.mask = mask;
+ t.vtable = .{};
+ inline for (host_fns, 0..) |f, i| if (mask & (@as(u64, 1) << i) != 0 or
+ (t.headless and comptime std.mem.eql(u8, f, "watched")))
+ {
+ @field(t.vtable, f) = &Wrap(i).f;
+ };
+ return .{ .ctx = t, .vtable = &t.vtable };
+ }
+
+ fn enterInput(t: *Tape, p: ?*Pardes, input: *const Input) Entered {
+ if (!t.host_side) return .{};
+ t.host_side = false;
+ t.depth += 1;
+ if (t.replay) return .{ .outer = true };
+ if (p) |core| {
+ if (t.core == null) t.core = core;
+ t.noteWrites();
+ t.wrap(core);
+ }
+ t.record(.input, Input, input);
+ return .{ .outer = true };
+ }
+
+ fn leaveInput(t: *Tape, p: ?*Pardes) void {
+ t.depth -= 1;
+ t.host_side = true;
+ if (t.replay) return;
+ if (p) |core| if (t.core == null) {
+ t.core = core;
+ t.wrap(core);
+ };
+ t.record(.done, void, &{});
+ if (t.depth == 0 or t.every) if (t.core) |core| {
+ var parts: Parts = undefined;
+ const d = digestParts(core, &parts);
+ t.record(.digest, []const u64, &d);
+ };
+ if (t.depth == 0) t.flush();
+ t.seeFields();
+ }
+
+ // ---- replaying ----
+
+ const Record = struct { kind: Kind, payload: walk.Reader };
+
+ fn next(t: *Tape) ?Record {
+ const head = t.in.take(5) catch return null;
+ const len = std.mem.bytesToValue(u32, head[1..5]);
+ const payload = t.in.take(len) catch return null;
+ return .{ .kind = @enumFromInt(head[0]), .payload = .{ .bytes = payload } };
+ }
+
+ fn peek(t: *const Tape) ?Kind {
+ if (t.in.bytes.len - t.in.at < 5) return null;
+ return @enumFromInt(t.in.bytes[t.in.at]);
+ }
+
+ fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void {
+ if (t.diverged != null) return;
+ t.diverged = std.fmt.allocPrint(t.gpa, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why;
+ }
+
+ /// A host call the headless replica does not make (it draws no frame,
+ /// so presents none): what the host told the core inside it (what it
+ /// presented, `acknowledge_panel_presentation`) is applied all the
+ /// same, the host's own calls below it stepped over.
+ fn skipHost(t: *Tape) void {
+ while (t.diverged == null) {
+ var rec = t.next() orelse return;
+ switch (rec.kind) {
+ .ret => return,
+ .input => t.apply(&rec.payload),
+ .set => t.applySet(&rec.payload),
+ .host => t.skipHost(),
+ .digest => t.check(&rec.payload),
+ .done => return t.diverge("a host call's records are out of order", .{}),
+ }
+ }
+ }
+
+ /// Over a call the replica does not make (the headless one's present),
+ /// what was applied inside it too.
+ fn skip(t: *Tape) void {
+ var depth: usize = 1;
+ while (depth > 0) {
+ const rec = t.next() orelse return;
+ switch (rec.kind) {
+ .host, .input => depth += 1,
+ .ret, .done => depth -= 1,
+ else => {},
+ }
+ }
+ }
+
+ /// The records of an input up to its end: what was applied inside it,
+ /// as it was; the headless replica steps over the host calls it did
+ /// not make.
+ fn finishInput(t: *Tape) void {
+ while (t.diverged == null) {
+ const kind = t.peek() orelse return t.diverge("the tape ends inside an input", .{});
+ var rec = t.next().?;
+ switch (kind) {
+ .done => return,
+ .set => t.applySet(&rec.payload),
+ .host => if (t.headless) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{std.mem.bytesToValue(u16, rec.payload.bytes[0..2])}),
+ .input => t.apply(&rec.payload),
+ .digest => t.check(&rec.payload),
+ .ret => return t.diverge("the core's input ended before the recorded one", .{}),
+ }
+ }
+ }
+
+ /// The records inside a host call up to its return, whose result is
+ /// left in `last_ret`.
+ fn finishHost(t: *Tape) bool {
+ while (t.diverged == null) {
+ var rec = t.next() orelse {
+ t.diverge("the tape ends inside a host call", .{});
+ return false;
+ };
+ switch (rec.kind) {
+ .ret => {
+ t.last_ret = rec.payload;
+ return true;
+ },
+ .input => t.apply(&rec.payload),
+ .set => t.applySet(&rec.payload),
+ .digest => t.check(&rec.payload),
+ .host, .done => {
+ t.diverge("a host call's records are out of order", .{});
+ return false;
+ },
+ }
+ }
+ return false;
+ }
+
+ fn expectHost(t: *Tape, id: u16, comptime what: []const u8) bool {
+ if (t.diverged != null) return false;
+ const rec = t.next() orelse {
+ t.diverge("the core called " ++ what ++ " past the tape's end", .{});
+ return false;
+ };
+ if (rec.kind != .host or std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) != id) {
+ const other: u16 = if (rec.kind == .host) std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) else 0;
+ t.diverge("the core called " ++ what ++ " where the tape has a {t} ({d})", .{ rec.kind, other });
+ return false;
+ }
+ const outer = !t.host_side;
+ t.host_side = true;
+ defer t.host_side = !outer;
+ return t.finishHost();
+ }
+
+ /// A recorded digest against the replica's own.
+ fn check(t: *Tape, r: *walk.Reader) void {
+ const want = walk.decode([]const u64, r, t.arena.allocator()) catch return;
+ const p = t.core orelse return;
+ var parts: Parts = undefined;
+ const got = digestParts(p, &parts);
+ if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, partsDiffer(got, want, t.arena.allocator()) });
+ t.digests += 1;
+ }
+
+ fn applySet(t: *Tape, r: *walk.Reader) void {
+ const which = (r.take(1) catch return)[0];
+ if (which == set_mask) {
+ const host = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return));
+ if (t.core) |p| p.host = host;
+ return;
+ }
+ const p = t.core orelse return;
+ inline for (watched, 0..) |name, i| if (i == which) {
+ @field(p, name) = walk.decode(@FieldType(Pardes, name), r, t.arena.allocator()) catch return;
+ };
+ }
+
+ /// One input, applied to the replica as the host applied it.
+ fn apply(t: *Tape, r: *walk.Reader) void {
+ // A session's options are kept by its core (`opts`): theirs for good.
+ const Tag = std.meta.Tag(Input);
+ const tag = std.mem.bytesToValue(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{})));
+ r.at -= @sizeOf(Tag);
+ const kept = tag == .init or tag == .init_from_dump or tag == .restore;
+ const input = walk.decode(Input, r, if (kept) t.gpa else t.arena.allocator()) catch return t.diverge("a short input", .{});
+ const outer = t.applying;
+ t.applying = switch (input) {
+ .update => |ev| @tagName(ev),
+ else => @tagName(input),
+ };
+ defer t.applying = outer;
+ t.host_side = true;
+ defer t.host_side = true;
+ switch (input) {
+ .init, .init_from_dump => {
+ var opts = switch (input) {
+ .init => |o| o,
+ .init_from_dump => |d| d.opts,
+ else => unreachable,
+ };
+ opts.image_allocator = t.gpa;
+ opts.pdf_allocator = t.gpa;
+ opts.tree_sitter_allocator = t.gpa;
+ opts.frame_allocator = t.gpa;
+ t.core = switch (input) {
+ .init => Pardes.init(t.gpa, opts),
+ .init_from_dump => |d| pardes.dump.initFromDump(t.gpa, opts, d.bytes),
+ else => unreachable,
+ } catch |err| return t.diverge("init failed: {t}", .{err});
+ t.core.?.host = t.hostOf(t.mask);
+ return t.finishInput();
+ },
+ else => {},
+ }
+ const p = t.core orelse return t.diverge("an input before the core", .{});
+ switch (input) {
+ .init, .init_from_dump => unreachable,
+ .restore => |rs| {
+ const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err});
+ t.core = nc;
+ nc.host = t.hostOf(t.mask);
+ },
+ // The session is over (or the core a Restore replaced is
+ // freed): nothing done now is compared.
+ .deinit => |own| {
+ if (own) t.core = null;
+ return t.skip();
+ },
+ .update => |ev| p.update(ev),
+ .serve_fs => |req| _ = p.serveFs(req),
+ .pump => p.pump(t.hostOf(t.mask)) catch {},
+ .perform => |e| p.perform(e),
+ .next_effect => _ = p.nextEffect(),
+ .advance => |now| p.advance(now),
+ .render => if (t.headless) return t.skip() else {
+ _ = p.frame_arena.reset(.retain_capacity);
+ _ = p.render(p.frame_arena.allocator()) catch {};
+ },
+ .take_restore => _ = p.takeRestore(),
+ .set_cwd => |a| p.setCwd(a.id, a.cwd),
+ .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks),
+ .shell_failed => |a| p.shellFailed(a.id, a.err),
+ .restart_failed => |a| p.restartFailed(a.id, a.err),
+ .save_failed => |a| p.saveFailed(a.id, a.path, a.err),
+ .dump_failed => |a| p.dumpFailed(a.path, a.err),
+ .set_last_dump => |path| p.setLastDump(path),
+ .set_message => |a| p.setMessage(a.id, a.text),
+ .set_status => |a| p.setStatus(a.id, a.text),
+ .report_error => |a| p.reportError(a.id, a.operation, a.err),
+ .reload_watched_file => |a| _ = p.reloadWatchedFile(a.id, a.bytes),
+ .note_late_failure => |id| p.noteLateFailure(id),
+ .invalidate => p.invalidate(),
+ .acknowledge_panel_presentation => |tracks| p.acknowledgePanelPresentation(tracks),
+ .run_closed_lines => pardes.ctlfs.runClosedLines(p),
+ .file_deleted => |id| pardes.panes.File.deleted(p, id),
+ }
+ t.finishInput();
+ }
+};
+
+const pardes_fields = walk.fieldNames(Pardes);
+const stream_fields = walk.fieldNames(pardes.Stream);
+const pane_fields = walk.fieldNames(pardes.Pane);
+const file_fields = walk.fieldNames(pardes.panes.File.State);
+const places_fields = walk.fieldNames(pardes.panes.Places);
+const Parts = [pardes_fields.len + stream_fields.len + pardes.MAX_PANES * (2 + pane_fields.len + file_fields.len + places_fields.len)]u64;
+
+/// The session's digest as one per field of the core's, its stream's, and
+/// each pane's and its file's, so a divergence says where it is.
+fn digestParts(p: *const Pardes, parts: *Parts) []const u64 {
+ var n = walk.fieldDigests(Pardes, p, parts[0..pardes_fields.len]).len;
+ n += walk.fieldDigests(pardes.Stream, &p.stream, parts[n..][0..stream_fields.len]).len;
+ for (p.panes, 0..) |slot, id| {
+ const pane = slot orelse continue;
+ parts[n] = id;
+ parts[n + 1] = @intFromBool(pane.file != null);
+ n += 2;
+ n += walk.fieldDigests(pardes.Pane, pane, parts[n..][0..pane_fields.len]).len;
+ if (pane.file) |*f| n += walk.fieldDigests(pardes.panes.File.State, f, parts[n..][0..file_fields.len]).len;
+ n += walk.fieldDigests(pardes.panes.Places, &pane.places, parts[n..][0..places_fields.len]).len;
+ }
+ return parts[0..n];
+}
+
+/// The fields whose digests differ, named.
+fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []const u8 {
+ var said: std.ArrayList(u8) = .empty;
+ if (got.len != want.len) said.print(a, "the panes there are ({d} parts, {d} recorded); ", .{ got.len, want.len }) catch {};
+ const n = @min(got.len, want.len);
+ var i: usize = 0;
+ var count: usize = 0;
+ while (i < pardes_fields.len + stream_fields.len and i < n) : (i += 1) {
+ if (got[i] == want[i]) continue;
+ const name = if (i < pardes_fields.len) pardes_fields[i] else stream_fields[i - pardes_fields.len];
+ said.print(a, "{s}{s} ", .{ if (i < pardes_fields.len) "" else "stream.", name }) catch {};
+ count += 1;
+ }
+ while (i + 2 <= n and count < 16) {
+ const id = got[i];
+ const has_file = got[i + 1] != 0;
+ i += 2;
+ for (pane_fields) |name| {
+ if (i >= n) break;
+ if (got[i] != want[i]) {
+ said.print(a, "pane {d}.{s} ", .{ id, name }) catch {};
+ count += 1;
+ }
+ i += 1;
+ }
+ if (has_file) for (file_fields) |name| {
+ if (i >= n) break;
+ if (got[i] != want[i]) {
+ said.print(a, "pane {d}.file.{s} ", .{ id, name }) catch {};
+ count += 1;
+ }
+ i += 1;
+ };
+ for (places_fields) |name| {
+ if (i >= n) break;
+ if (got[i] != want[i]) {
+ said.print(a, "pane {d}.places.{s} ", .{ id, name }) catch {};
+ count += 1;
+ }
+ i += 1;
+ }
+ }
+ return said.items;
+}
+
+/// A call into the core: the host's, and recorded, or the core's own.
+pub const Entered = struct { outer: bool = false };
+
+pub inline fn enter(p: ?*Pardes, input: Input) Entered {
+ const t = on orelse return .{};
+ return t.enterInput(p, &input);
+}
+
+pub inline fn leave(p: ?*Pardes, e: Entered) void {
+ if (e.outer) on.?.leaveInput(p);
+}
+
+/// The core a Restore made is the session's from here.
+pub fn adopt(p: *Pardes) void {
+ const t = on orelse return;
+ if (t.replay) return;
+ t.core = p;
+ t.wrap(p);
+}
+
+/// Whether `p` is the core being recorded (a Restore's old one is not).
+pub fn current(p: *const Pardes) bool {
+ const t = on orelse return true;
+ return t.core == null or t.core == p;
+}
+
+/// The session's core is freed: no digest of it after.
+pub fn forget(p: *Pardes) void {
+ const t = on orelse return;
+ if (t.core == p) t.core = null;
+}
+
+/// The Host function `i`, through the tape.
+fn Wrap(comptime i: usize) type {
+ const Fn = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child;
+ const info = @typeInfo(Fn).@"fn";
+ const R = info.return_type.?;
+ const P = info.param_types;
+ return switch (P.len) {
+ 1 => struct {
+ fn f(ctx: ?*anyopaque) R {
+ return hostCall(i, R, ctx, .{});
+ }
+ },
+ 2 => struct {
+ fn f(ctx: ?*anyopaque, a: P[1].?) R {
+ return hostCall(i, R, ctx, .{a});
+ }
+ },
+ 3 => struct {
+ fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?) R {
+ return hostCall(i, R, ctx, .{ a, b });
+ }
+ },
+ 4 => struct {
+ fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?) R {
+ return hostCall(i, R, ctx, .{ a, b, c });
+ }
+ },
+ 5 => struct {
+ fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?, d: P[4].?) R {
+ return hostCall(i, R, ctx, .{ a, b, c, d });
+ }
+ },
+ else => @compileError("tape: a host function of more arguments"),
+ };
+}
+
+fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R {
+ const t: *Tape = @ptrCast(@alignCast(ctx.?));
+ const name = host_fns[i];
+ if (t.replay) {
+ // A replica that never draws is never watched: it asks no one.
+ if (comptime std.mem.eql(u8, name, "watched")) if (t.headless) return false;
+ if (!t.expectHost(i, name)) return std.mem.zeroes(R);
+ var ret = t.last_ret;
+ if (R == ?[]const u8) {
+ // fg_name's answer is written into the caller's buffer.
+ const text = (walk.decode(R, &ret, t.arena.allocator()) catch return null) orelse return null;
+ const buf = args[args.len - 1];
+ const n = @min(text.len, buf.len);
+ @memcpy(buf[0..n], text[0..n]);
+ return buf[0..n];
+ }
+ return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R);
+ }
+ const real = @field(t.real.vtable.*, name).?;
+ const id: u16 = i;
+ t.record(.host, u16, &id);
+ t.seeFields();
+ const outer = !t.host_side;
+ t.host_side = true;
+ const r = @call(.auto, real, .{t.real.ctx} ++ args);
+ t.host_side = !outer;
+ t.noteWrites();
+ t.record(.ret, R, &r);
+ return r;
+}
+
+/// A syscall of the core's own, `f(args)`, made out of the turn (a 9P
+/// request through a mount of this very session is answered meanwhile):
+/// recorded with what it returned, or answered from the tape and never
+/// made. A result with slices is decoded into `a` on replay, the allocator
+/// `f` would have used.
+pub fn site(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
+ return call(s, a, f, args, true);
+}
+
+/// A read of the host's that is no syscall out of the turn (the clock, the
+/// environment) or yields for itself: as `site`.
+pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
+ return call(s, null, f, args, false);
+}
+
+fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
+ const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?;
+ const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args);
+ const id: u16 = @as(u16, host_fns.len) + @intFromEnum(s);
+ if (t.replay) {
+ if (!t.expectHost(id, @tagName(s))) {
+ std.debug.print("replay: {s}\n", .{t.diverged orelse "?"});
+ @panic("tape: the replay diverged at a host call of the core's own");
+ }
+ var ret = t.last_ret;
+ return walk.decode(R, &ret, a orelse t.arena.allocator()) catch @panic("tape: a short result");
+ }
+ // The host's own (a shell's spawn asking the shell), or one inside a
+ // site's syscall: the outer call is the record.
+ if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args);
+ t.record(.host, u16, &id);
+ t.seeFields();
+ t.host_side = true;
+ const r = if (yields) out(f, args) else @call(.auto, f, args);
+ t.host_side = false;
+ t.noteWrites();
+ t.record(.ret, R, &r);
+ return r;
+}
+
+/// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent:
+/// what the clock said is taped, so a replica stops its work where this did.
+pub fn over(since_ns: u64, budget_ns: u64) bool {
+ return value(.budget, overNow, .{ since_ns, budget_ns });
+}
+
+fn overNow(since_ns: u64, budget_ns: u64) bool {
+ return pardes.FrameStats.nowNs() -| since_ns > budget_ns;
+}
+
+fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
+ pardes.turn.yield();
+ defer pardes.turn.back();
+ return @call(.auto, f, args);
+}
+
+/// Asked by `turn.yield`: a syscall a replica must never make.
+pub fn replaying() bool {
+ return if (on) |t| t.replay else false;
+}
+
+/// PARDES_RECORD set: the tape is written from the session's first call.
+pub fn startFromEnv() void {
+ if (on != null) return;
+ const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return);
+ if (prefix.len == 0) return;
+ const gpa = std.heap.c_allocator;
+ var path_buf: [4096]u8 = undefined;
+ const path = std.fmt.bufPrintSentinel(&path_buf, "{s}{d}.tape", .{ prefix, libc.getpid() }, 0) catch return;
+ const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644));
+ if (fd < 0) return;
+ const t = gpa.create(Tape) catch return;
+ t.* = .{ .gpa = gpa, .replay = false, .fd = fd, .scratch = .init(gpa), .arena = .init(gpa), .every = libc.getenv("PARDES_RECORD_EVERY") != null };
+ on = t;
+}
+
+/// `PARDES_REPLAY=<tape>`: the tape replayed into three replicas, each
+/// digest checked; 0 when none diverged.
+pub fn replayMain(io: std.Io, path: []const u8) u8 {
+ const gpa = std.heap.c_allocator;
+ // What a shell starts before its core (tty.zig's localSession).
+ pardes.image.start(io, gpa);
+ if (comptime pardes.pdf_enabled) pardes.pdf.start(gpa);
+ pardes.syntax.start(gpa);
+ const bytes = std.Io.Dir.cwd().readFileAlloc(std.Io.Threaded.global_single_threaded.io(), path, gpa, .unlimited) catch |err| {
+ std.debug.print("replay: cannot read {s}: {t}\n", .{ path, err });
+ return 2;
+ };
+ const runs = [_]struct { name: []const u8, headless: bool }{
+ .{ .name = "first", .headless = false },
+ // The first replica's memory is still held: this one's lies elsewhere.
+ .{ .name = "second", .headless = false },
+ .{ .name = "headless", .headless = true },
+ };
+ var failed = false;
+ for (runs) |run| {
+ var t: Tape = .{ .gpa = gpa, .replay = true, .in = .{ .bytes = bytes }, .headless = run.headless, .scratch = .init(gpa), .arena = .init(gpa) };
+ on = &t;
+ defer on = null;
+ while (t.diverged == null) {
+ const kind = t.peek() orelse break;
+ var rec = t.next().?;
+ switch (kind) {
+ .input => {
+ t.apply(&rec.payload);
+ _ = t.arena.reset(.retain_capacity);
+ },
+ .set => t.applySet(&rec.payload),
+ .digest => t.check(&rec.payload),
+ .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}),
+ }
+ }
+ // A session killed mid-step leaves its last input cut short: the
+ // end of the tape, not a divergence.
+ if (t.diverged) |why| if (std.mem.startsWith(u8, why, "the tape ends")) {
+ t.diverged = null;
+ };
+ if (t.diverged) |why| {
+ failed = true;
+ std.debug.print("replay {s}: {s} replica diverged after {d} digests: {s}\n", .{ path, run.name, t.digests, why });
+ } else std.debug.print("replay {s}: {s} replica: {d} digests agree\n", .{ path, run.name, t.digests });
+ }
+ return if (failed) 1 else 0;
+}
diff --git a/src/terminal.zig b/src/terminal.zig
index ce41bd95..0afbe43a 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -159,6 +159,30 @@ pub const State = struct {
mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {},
/// The kitty graphics its program placed, as drawn (TerminalImages.zig).
images: TerminalImages = if (images_enabled) .{} else {},
+
+ /// The emulator as its program has made it, for the replay digest
+ /// (src/walk.zig): the active screen's text, how much history is above
+ /// it and where the view is, the cursor, the answers not yet written.
+ /// Not its pages (their addresses are its own), nor the history's text
+ /// (the screen's was all of it once, and a session's digest is taken
+ /// every step), nor the kitty images, whose media is read off the host:
+ /// a replica's terminal is the authority's screen state from phase 5
+ /// on (docs/next-daemon.md).
+ pub fn digest(s: *const State, h: *pardes.walk.Hasher) void {
+ if (comptime !enabled) return;
+ const screen = s.vt.screens.active;
+ const sb = screen.pages.scrollbar();
+ const cur = screen.cursor;
+ const facts = .{ sb.total, sb.offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped };
+ inline for (facts) |fact| h.update(std.mem.asBytes(&fact));
+ h.update(s.reply[0..s.reply_len]);
+ var buf: [4096]u8 = undefined;
+ var text: std.Io.Writer.Hashing(pardes.walk.Hasher) = .initHasher(h.*, &buf);
+ if (screen.pages.getBottomRight(.active)) |br|
+ screen.dumpString(&text.writer, .{ .tl = screen.pages.getTopLeft(.active), .br = br, .unwrap = false }) catch {};
+ text.writer.flush() catch {};
+ h.* = text.hasher;
+ }
};
const GColor = ghostty_vt.color;