summaryrefslogtreecommitdiff
path: root/src/ninep/ctl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/ctl.zig')
-rw-r--r--src/ninep/ctl.zig93
1 files changed, 48 insertions, 45 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index 78fe15c1..9b87d9aa 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -54,6 +54,7 @@ fn noteResult(p: *Pardes, serial: u32) void {
/// created; failing that, a look records the pane it focused and an exec
/// the pane it acted on (even one it closed).
pub fn run(p: *Pardes, id: usize, exec: bool, text: []const u8, in_tag: bool) void {
+ const v = p.view();
const context = if (p.panes[id]) |pane| pane.serial else 0;
// A pane an event reader holds hears its clicks instead of acting on
// them (editors/acme/look.c:53 and exec.c:173 send them to winevent),
@@ -106,10 +107,10 @@ pub fn run(p: *Pardes, id: usize, exec: bool, text: []const u8, in_tag: bool) vo
// A word in a column's or the workspace's tag ran at no pane: it
// answers one only if it focused one (Newcol answers nothing).
if (p.exec_header) {
- if (p.active != id) if (p.panes[p.active]) |pane| noteResult(p, pane.serial);
+ if (v.active != id) if (p.panes[v.active]) |pane| noteResult(p, pane.serial);
return;
}
- noteResult(p, if (exec) context else (p.panes[p.active] orelse return).serial);
+ noteResult(p, if (exec) context else (p.panes[v.active] orelse return).serial);
}
/// A write's messages, a line each, except that an `Edit` line takes the
@@ -190,9 +191,10 @@ pub fn completeEnd(p: *Pardes, data: []const u8) usize {
/// A null `serial` clicks at whichever pane is active as each line runs,
/// which is what the root's own look and exec mean; a serial stays put.
pub fn command(p: *Pardes, req: Req, serial: ?u32, exec: bool, in_tag: bool) Reply {
+ const v = p.view();
// The root's lines run at the keyboard's pane: a refusal before any runs
// is that pane's too, in its err record.
- p.fs.ran_at = if (serial == null) (if (p.panes[p.active]) |pane| pane.serial else 0) else 0;
+ p.fs.ran_at = if (serial == null) (if (p.panes[v.active]) |pane| pane.serial else 0) else 0;
if (editNotAlone(p, req.data)) return tree.failText(req.tag, E.INVAL, e_after_edit);
for ([2]bool{ false, true }) |apply| {
if (apply) p.fs.results_len = 0;
@@ -204,7 +206,7 @@ pub fn command(p: *Pardes, req: Req, serial: ?u32, exec: bool, in_tag: bool) Rep
for (text) |c| if ((c < ' ' and c != '\t' and c != '\n') or c == 0x7f) return tree.failText(req.tag, E.INVAL, e_control);
if (exec) if (tooLong(req, text)) |refusal| return refusal;
if (!apply) continue;
- var id = if (serial) |s| p.paneBySerial(s) orelse break else p.active;
+ var id = if (serial) |s| p.paneBySerial(s) orelse break else v.active;
// No pane at all: the root's look and exec act in the session's
// directory. An exec runs as the workspace tag's; a look from a
// scratch made as New makes one (and its column), dropped
@@ -220,12 +222,12 @@ pub fn command(p: *Pardes, req: Req, serial: ?u32, exec: bool, in_tag: bool) Rep
const free = p.freeSlot() orelse return Reply.fail(req.tag, E.NOSPC);
panes.newScratchFor(p, id, .scratch);
if (p.panes[free] == null) return Reply.fail(req.tag, E.NOSPC);
- p.active = free;
+ v.active = free;
id = free;
scratch = free;
}
if (p.panes[id] == null) return Reply.fail(req.tag, E.NOENT);
- defer if (scratch) |s| if (p.active != s) if (p.panes[s]) |sp| if (sp.file) |f| {
+ defer if (scratch) |s| if (v.active != s) if (p.panes[s]) |sp| if (sp.file) |f| {
if (f.content.len == 0) p.removePane(s, null) catch {};
};
p.fs.ran_at = p.panes[id].?.serial;
@@ -389,7 +391,7 @@ pub fn statusText(p: *Pardes) []const u8 {
// Whether the pointer is over this window: what a grip let go past
// another window's edge asks of every window, to find the one it was
// let go over (Handover.zig), where no window may know another's place.
- if (p.frontend.windowed) w.writer.print("pointer {s}\n", .{if (p.pointer_inside) "in" else "out"}) catch {};
+ if (p.view().frontend.windowed) w.writer.print("pointer {s}\n", .{if (p.view().pointer_inside) "in" else "out"}) catch {};
return w.written();
}
@@ -431,7 +433,7 @@ pub fn paneText(p: *Pardes, pane: *Pane, buf: []u8) []const u8 {
const font = p.settings.font.effective_name.get();
writeQuoted(&w, if (font.len == 0) "default" else font) catch return w.buffered();
// While a column's or the workspace's tag has the keyboard, no pane does.
- const current = p.panes[p.active] == pane and p.header == null;
+ const current = p.panes[p.view().active] == pane and p.view().header == null;
const f = pane_files.fileOf(pane);
const undo = if (f) |file| file.history.journal.done > 0 else false;
const redo = if (f) |file| file.history.journal.done < file.history.journal.steps.items.len else false;
@@ -684,7 +686,7 @@ pub fn writePager(p: *Pardes, req: Req) Reply {
if (pardes.filesystem.deniedAbove(local))
return tree.failText(req.tag, E.PERM, std.fmt.bufPrint(&p.fs.ename, "pager: {s}: permission denied", .{dir[0..@min(dir.len, 256)]}) catch "pager: permission denied");
};
- const id = pardes.panes.Output.openPager(p, p.active, dir) catch |err|
+ const id = pardes.panes.Output.openPager(p, p.view().active, dir) catch |err|
return tree.failText(req.tag, if (err == error.NoPaneRoom or err == error.NoPaneSlots) E.NOSPC else E.IO, switch (err) {
error.NoPaneRoom, error.NoPaneSlots => "pager: no space for a +Pager pane",
else => "pager: could not make a +Pager pane",
@@ -715,12 +717,12 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM
// +Diff in the +Pager's place, the +Pager closed. The keyboard stays
// where it was, as a page's.
if (f.pager_diff) {
- const keyboard = p.active;
+ const keyboard = p.view().active;
if (panes.Output.openDiff(p, id, f.path, f.content)) |hid| {
// ponytail: the text is gone with it; Diff on the +Diff says no such file.
p.removePane(id, hid) catch {};
} else |_| {}
- if (p.panes[keyboard] != null) p.active = keyboard;
+ if (p.panes[keyboard] != null) p.view().active = keyboard;
}
}
@@ -736,6 +738,7 @@ pub fn fillClosedPagers(p: *Pardes) void {
/// One session builtin a line, at whichever pane has the keyboard as each
/// runs, as the root's look and exec click there; all are checked first.
pub fn writeRoot(p: *Pardes, req: Req) Reply {
+ const v = p.view();
for ([2]bool{ false, true }) |apply| {
if (apply) p.fs.results_len = 0;
var it: Messages = .{ .p = p, .data = req.data };
@@ -749,8 +752,8 @@ pub fn writeRoot(p: *Pardes, req: Req) Reply {
// attached to (--detach, unattended) takes one written.
if (!p.unattended) return refuseQuoting(p, req, "size: a frontend is attached and owns the size", value, "");
if (apply) {
- const was: [2]u16 = .{ p.screen_w, p.screen_h };
- const before = p.rects;
+ const was: [2]u16 = .{ v.screen_w, v.screen_h };
+ const before = v.rects;
p.update(.{ .resize = .{ .cols = size[0], .rows = size[1] } });
p.sync();
// Every pane keeps its tag and two rows, as placement
@@ -785,10 +788,10 @@ pub fn writeRoot(p: *Pardes, req: Req) Reply {
continue;
}
// An empty window (no pane at all) still takes its session words.
- if (p.panes[p.active] == null and panes.anyPane(p) != null) return Reply.fail(req.tag, E.NOENT);
+ if (p.panes[v.active] == null and panes.anyPane(p) != null) return Reply.fail(req.tag, E.NOENT);
p.fs.session_write = true;
defer p.fs.session_write = false;
- if (runBuiltin(p, req, p.active, line)) |refusal| return refusal;
+ if (runBuiltin(p, req, v.active, line)) |refusal| return refusal;
}
}
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
@@ -923,8 +926,8 @@ pub fn commandsText(p: *Pardes) ![]const u8 {
/// rio keeps `current` apart from `unhide`.
pub fn readFocus(p: *Pardes, req: Req) Reply {
const out = p.fs.stage(p.gpa);
- if (p.header != null) return tree.stagedReply(p, req);
- const pane = p.panes[p.active] orelse return tree.stagedReply(p, req);
+ if (p.view().header != null) return tree.stagedReply(p, req);
+ const pane = p.panes[p.view().active] orelse return tree.stagedReply(p, req);
out.print(p.gpa, "{d}\n", .{pane.serial}) catch return Reply.fail(req.tag, E.NOMEM);
return tree.stagedReply(p, req);
}
@@ -937,7 +940,7 @@ pub fn writeFocus(p: *Pardes, req: Req) Reply {
// its column becomes the active one, as the user's click there would
// make it: pane/new and a look place there next.
tagline.exitHeader(p);
- p.active = id;
+ p.view().active = id;
if (pardes.layout.findPane(p, id)) |at| p.setActiveColumn(at.col);
return .{ .tag = req.tag, .written = @intCast(req.data.len) };
}
@@ -1405,11 +1408,11 @@ test "status in a window says whether the pointer is over it" {
// there is a terminal's view.
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- p.frontend.windowed = true;
+ p.view().frontend.windowed = true;
var lines = std.mem.tokenizeScalar(u8, rd(p, root_status, 0, 4096).bytes, '\n');
for (0..3) |_| _ = lines.next();
try testing.expectEqualStrings("pointer out", lines.next().?);
- p.frontend.windowed = false;
+ p.view().frontend.windowed = false;
}
@@ -1591,9 +1594,9 @@ test "a builtin for one kind of pane is refused on another, and still acts on it
// Petscii chose; with them, what it chose.
for ([_]bool{ false, true }) |art| {
doc.addon.?.art.glyph = art;
- p.frontend.native_images = false;
+ p.view().frontend.native_images = false;
try testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagPrefix(p, doc), " petscii:on ") != null);
- p.frontend.native_images = true;
+ p.view().frontend.native_images = true;
try testing.expect(std.mem.indexOf(u8, try pardes.tagline.tagPrefix(p, doc), if (art) " petscii:on " else " petscii:off ") != null);
}
}
@@ -1650,7 +1653,7 @@ test "a builtin's Verbose announcement is logged only when it succeeds, its fail
try testing.expect(!th.logHas(p, said_exit));
// Refused at the keyboard (no write to fail), edited since: the same.
f.revision +%= 1;
- _ = p.executeBuiltinLine(p.active, "Exit");
+ _ = p.executeBuiltinLine(p.view().active, "Exit");
try testing.expect(!th.logHas(p, said_exit));
try testing.expect(!p.quit);
// A Save the host could not do, told a step later: the Save goes too.
@@ -1919,7 +1922,7 @@ test "Term in a directory that is not there fails and makes no pane; a shell tha
// A terminal whose shell the host could not start.
_ = wr(p, Node.of(serial, .name), "/tmp/f.txt\n");
try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .ctl), "Term\n").reply.status);
- const term = p.active;
+ const term = p.view().active;
p.shellFailed(@intCast(term), error.FileNotFound);
const run_node = Node.of(p.panes[term].?.serial, .pty_run);
const h = call(p, .{ .tag = 1, .op = .open, .node = run_node, .omode = 2 }).reply.handle;
@@ -1997,7 +2000,7 @@ test "a terminal told the directory it already goes by logs no rename" {
_ = wr(p, Node.of(serialOf(p), .name), "/tmp/f.txt\n");
try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .ctl), "Term\n").reply.status);
p.update(.tick); // announced
- const term = p.active;
+ const term = p.view().active;
// The host says where its shell started: where it was already named.
p.setCwd(term, "/tmp");
var rec: [32]u8 = undefined;
@@ -2126,7 +2129,7 @@ test "a script whose interpreter is not there: Term refuses it up front, only an
test "a language server question that finds nothing, cannot be answered, or is asked of no file fails, and a write to an image's body is refused" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- const id = p.active;
+ const id = p.view().active;
// Asked of a pane that is no file's: at once.
_ = wr(p, root_exec, "Recent\n");
const listed = p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?;
@@ -2212,7 +2215,7 @@ test "a PDF's body reads its page's text layer, and takes no write" {
var line: [4200]u8 = undefined;
_ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/doc.pdf\n", .{dir}));
p.sync();
- const doc = p.panes[p.active].?;
+ const doc = p.panes[p.view().active].?;
try testing.expect(doc.hasPdf());
const body = Node.of(doc.serial, .body);
try testing.expect(std.mem.indexOf(u8, rd(p, body, 0, 4096).bytes, "target.txt") != null);
@@ -2226,7 +2229,7 @@ test "a PDF's body reads its page's text layer, and takes no write" {
test "workspace symbols, Symbols and Diagnostics share their directory's one +Search" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- const id = p.active;
+ const id = p.view().active;
const Output = pardes.panes.Output;
try Output.fillResults(p, id, "/tmp", .{ .query = .workspace_symbols }, "x", try p.gpa.dupe(u8, "a.txt:1:1 x\n"), null);
try Output.fillResults(p, id, "/tmp", .{ .query = .document_symbols }, "", try p.gpa.dupe(u8, "a.txt:2:1 fn f\n"), null);
@@ -2256,7 +2259,7 @@ test "a failure said while a 9P write waits on it posts no msg, only the write's
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
p.fs.write_waits = true;
- p.saveFailed(@intCast(p.active), "/nowhere/zz.txt", error.FileNotFound);
+ p.saveFailed(@intCast(p.view().active), "/nowhere/zz.txt", error.FileNotFound);
p.fs.write_waits = false;
try testing.expect(!th.logHas(p, "msg "));
try testing.expect(p.fs.late_failure_len > 0);
@@ -2337,14 +2340,14 @@ test "a focus moved over 9P tells the programs that asked for focus reports, as
n += 1;
};
try testing.expectEqual(@as(usize, 2), n);
- p.active = shells[0];
+ p.view().active = shells[0];
p.update(.{ .output = .{ .pane = @intCast(shells[0]), .bytes = "\x1b[?1004h" } });
p.update(.{ .output = .{ .pane = @intCast(shells[1]), .bytes = "\x1b[?1004h" } });
while (p.nextEffect()) |_| {}
var digits: [16]u8 = undefined;
const moved = wr(p, @backingInt(tree.TopFile.focus), try std.fmt.bufPrint(&digits, "{d}\n", .{p.panes[shells[1]].?.serial}));
try testing.expectEqual(Status.ok, moved.reply.status);
- try testing.expectEqual(shells[1], p.active);
+ try testing.expectEqual(shells[1], p.view().active);
try testing.expectEqualStrings("\x1b[O\x1b[I", moved.pty_buf[0..moved.pty_len]);
}
@@ -2402,14 +2405,14 @@ test "a column takes panes until their documented minimums fill it, not until no
const new = @backingInt(tree.TopFile.new);
while (call(p, .{ .tag = 1, .op = .open, .node = new }).reply.status == .ok) p.sync();
p.sync();
- const avail = p.screen_h - (p.topBarHeight() + layout.columnBarHeight(p));
- for (0..p.ncol) |c| {
+ const avail = p.view().screen_h - (p.topBarHeight() + layout.columnBarHeight(p));
+ for (0..p.view().ncol) |c| {
var need: u32 = 0;
- for (p.cols[c].panes[0..p.cols[c].n]) |k| need += layout.minRows(p, k);
+ for (p.view().cols[c].panes[0..p.view().cols[c].n]) |k| need += layout.minRows(p, k);
// Refused only when one more pane's tag and two rows would not fit.
- if (p.cols[c].n > 1) try testing.expect(avail - need < 3);
+ if (p.view().cols[c].n > 1) try testing.expect(avail - need < 3);
// Every pane keeps its minimum.
- for (p.cols[c].panes[0..p.cols[c].n]) |k| try testing.expect(p.rects[k].h >= layout.minRows(p, k));
+ for (p.view().cols[c].panes[0..p.view().cols[c].n]) |k| try testing.expect(p.view().rects[k].h >= layout.minRows(p, k));
}
}
@@ -2461,7 +2464,7 @@ test "size is monotonic: growing is never refused, and a size once taken is take
// until a column has no room (ENOSPC).
try testing.expectEqual(Status.ok, wr(p, root_ctl, "Newcol\n").reply.status);
for (0..3) |_| try testing.expectEqual(Status.ok, call(p, .{ .tag = 1, .op = .open, .node = new }).reply.status);
- try testing.expectEqual(Status.ok, wr(p, Node.of(p.panes[p.active].?.serial, .ctl), "Term\n").reply.status);
+ try testing.expectEqual(Status.ok, wr(p, Node.of(p.panes[p.view().active].?.serial, .ctl), "Term\n").reply.status);
p.sync();
while (call(p, .{ .tag = 1, .op = .open, .node = new }).reply.status == .ok) p.sync();
p.sync();
@@ -2485,7 +2488,7 @@ test "size is monotonic: growing is never refused, and a size once taken is take
}
for ([_]u16{ 60, 61, 64, 70, 100 }) |grown| {
try testing.expectEqual(Status.ok, wr(p, root_ctl, try std.fmt.bufPrint(&line, "size 160 {d}\n", .{grown})).reply.status);
- try testing.expectEqual(grown, p.screen_h);
+ try testing.expectEqual(grown, p.view().screen_h);
}
// Out of range is EINVAL, in words 9ns reads as that.
const bad = wr(p, root_ctl, "size 5 2\n");
@@ -2587,11 +2590,11 @@ test "Newcol at the column cap fails the write and says the cap" {
defer p.deinit();
p.update(.{ .resize = .{ .cols = 4000, .rows = 30 } });
const root_ctl = @backingInt(tree.TopFile.ctl);
- while (p.ncol < pardes.MAX_COLS) {
+ while (p.view().ncol < pardes.MAX_COLS) {
// From the widest column's tag, so no column gets too narrow first.
p.sync();
var widest: usize = 0;
- for (0..p.ncol) |c| if (p.col_w[c] > p.col_w[widest]) {
+ for (0..p.view().ncol) |c| if (p.view().col_w[c] > p.view().col_w[widest]) {
widest = c;
};
pardes.tagline.enterHeader(p, widest);
@@ -2815,7 +2818,7 @@ test "Term+bash on a pane ctl is Term with its argument, as exec and the tag tak
test "a Rename that reaches other files previews them, says so, and is the pane a waiting write answers" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- const id = p.active;
+ const id = p.view().active;
p.fs.lsp_result = null;
p.lspRequest(id, .rename, "sum");
Ask.lspResponse(p, p.lsp_ask.wait.?.id, "lib.rs:2:8 sum\nmain.rs:4:13-15 sum\n");
@@ -2862,7 +2865,7 @@ test "Petscii, Palette and Ascii take the state their tag word names, set rather
test "a Rename the server answered for this file alone, with other open files saying the name, is previewed and says so" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- p.lspRequest(p.active, .rename, "sum");
+ p.lspRequest(p.view().active, .rename, "sum");
Ask.lspResponse(p, p.lsp_ask.wait.?.id, "util.zig:1:8 sum\nmain.zig: also says add, which the server did not rename\n");
try testing.expect(th.logHas(p, "Rename: previewed, not applied: the server renamed in this file only, and 1 other open file(s) say the name"));
}
@@ -2902,7 +2905,7 @@ test "Lspinfo's answer is the +Lsp pane it fills, as a Rename preview's is its +
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
p.fs.lsp_result = null;
- p.lspRequest(p.active, .status, "");
+ p.lspRequest(p.view().active, .status, "");
Ask.lspResponse(p, p.lsp_ask.wait.?.id, "language servers:\n");
const shown = p.paneBySerial(p.fs.lsp_result.?).?;
try testing.expect(std.mem.endsWith(u8, p.panes[shown].?.file.?.path, "+Lsp"));
@@ -2976,10 +2979,10 @@ test "a setting the other frontend has is passed over in the init file, not fail
test "the init file saved in a pane applies its settings again, and runs none of its commands twice" {
const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .startup_config = "Theme dusk\n", .startup_config_path = "/tmp/pardes-init-test/init" });
defer p.deinit();
- const cols = p.ncol;
+ const cols = p.view().ncol;
pardes.exec.reapplyInitFile(p, "/tmp/pardes-init-test/elsewhere", "Theme acme\n");
try testing.expectEqualStrings("dusk", p.theme().name);
pardes.exec.reapplyInitFile(p, "/tmp/pardes-init-test/init", "Theme acme\nNewcol\n");
try testing.expectEqualStrings("acme", p.theme().name);
- try testing.expectEqual(cols, p.ncol);
+ try testing.expectEqual(cols, p.view().ncol);
}