summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 02:07:32 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit83b9d45b8665a80bdb70934aa23bbde5cc03d202 (patch)
treec902b557ef624b07cd139659d0a891cd4809e03e /src/look.zig
parentb3d32656413734952bc918c2d4cfd12fede6c214 (diff)
downloadpardes-83b9d45b8665a80bdb70934aa23bbde5cc03d202.tar.gz
pardes-83b9d45b8665a80bdb70934aa23bbde5cc03d202.zip
plumber: a look sends what it looks at (docs/next-plumber.md, phase 2): after the pane's own readings and once a name already open in a pane is focused (never sent), lookAt sends the trimmed name, type text, src pardes (term from a raw terminal, a kind's name from its pane), wdir the pane's directory, with what resolveLookTarget found of it (Plumb.Seen: isfile and isdir take it, so no second stat and Look's own readings of a URL or `//` stay no file); a message the rules give back to `edit` as sent is Look's own (Sent.look) and runs lookAt's switch on that target, one nothing takes or a started word declines is Look's own too, whose `.url` arm is now the word's search; the edit port's open of another message runs with look_text, which now means "unplumbed", so the edit port's lookAt never sends again (the recursion trap); the three diff hooks become the default rules of the record's §5.2 (a bare .diff/.patch to `Diff $file`, a commit hash in the editor to `Diff $0`, after the two edit sets), and `Diff` takes a file or a revision, a rule's Diff declining in silence (Pardes.plumb_declined) where it cannot (no hunk, not readable, no .git above): hunks.lookedAt and lookedAtCommit go, their bodies in hunks.word, the `needs` check of the bare form in the word, PaneKind.diff gone; the old one-line `Plumb` word, Core.plumb, look.Port, plumbRuleError, plumbTarget and resolve's rules block go: bare, `Plumb` opens <config>/plumbing as Config opens init (both through builtins.openConfigFile; `include basic` in a new one), an argument is refused in words, Save applies it (host_io.writeFile beside reapplyInitFile, Plumb.saved), and boot reads it (Plumb.boot in applyStartupConfig); a started program now runs detached as Plan 9's plumber's (the coordinator's decision for the user's question 4): Effect.start, every host's host_io.startDetached (double fork, setsid, /dev/null for 0-2, every other fd closed, argv exec'd in wdir with no shell, the child's errno back through a close-on-exec pipe, said as `plumb start: <prog>: <why>`), so exec_dir goes; resolveLookTarget's and Plumb.Send.search's directory walk is one look.Dirs; test: look.zig's Plumb test rewritten onto the rules (a rule's word, an open pane unsent, the hover's resolve, a URL with no web rule searched for) and a hash inside and outside a git work tree; addons.zig's Plumb test onto plumb/rules (a port no kind reads, its kind gone: Look's own); Plumb.zig: the word, the save and boot; replicas.zig gains a URL (speculated whole, confirmed, no rollback) and a hash, each the authority's and a replica's own; every look golden unchanged; the reference quotes src/plumbing whole and says the order, the changelog says so
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig287
1 files changed, 148 insertions, 139 deletions
diff --git a/src/look.zig b/src/look.zig
index 115e3683..b32c46ac 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -4,7 +4,7 @@ const builtin = @import("builtin");
const libc = std.c;
const pardes = @import("pardes.zig");
const exec = @import("exec.zig");
-const hunks = @import("hunks.zig");
+const Plumb = @import("Plumb.zig");
const config = @import("config.zig");
const diff = @import("diff.zig");
const platform_has_fs = filesystem.platform_has_fs;
@@ -153,56 +153,6 @@ pub fn lineSpan(p: ?*pardes.Pardes, line: []const u8, cwd: []const u8, realbuf:
return null;
}
-/// Where a `Plumb` rule sends what it matched: the targets the built-in
-/// rules below make (url: the browser, through `xdg-open`), or a loaded
-/// kind by its name (`image`, `pdf`).
-pub const Port = enum { url, dir, file };
-
-/// A rule as `Plumb` takes it, plumb(6)'s order on one line: `<test>
-/// [word ...] to <port>`, the test `prefix` (the text starts so), `suffix`
-/// (an existing file's name ends so, any case), `isdir` or `isfile`; the
-/// port `url`, `dir`, `file` or a kind `kinds` has.
-/// Null when it is one; otherwise what is wrong with it, said.
-pub fn plumbRuleError(kinds: *const Kind.Registry, rule: []const u8) ?[]const u8 {
- var words = std.mem.tokenizeAny(u8, rule, " \t");
- const how = words.next() orelse return "a rule: <test> [word ...] to <port>";
- const takes = if (std.mem.eql(u8, how, "prefix") or std.mem.eql(u8, how, "suffix"))
- true
- else if (std.mem.eql(u8, how, "isdir") or std.mem.eql(u8, how, "isfile"))
- false
- else
- return "invalid test: prefix, suffix, isdir or isfile";
- var n: usize = 0;
- while (words.next()) |w| : (n += 1) {
- if (!std.mem.eql(u8, w, "to")) continue;
- if (takes and n == 0) return "invalid rule: prefix and suffix take a word";
- if (!takes and n > 0) return "invalid rule: isdir and isfile take no word";
- const port = words.next() orelse return "invalid rule: to names no port";
- if (std.meta.stringToEnum(Port, port) == null and kinds.byName(port) == null)
- return "invalid port: url, dir, file or a loaded kind";
- if (words.next() != null) return "invalid rule: a word after the port";
- return null;
- }
- return "invalid rule: no `to <port>`";
-}
-
-/// What a rule's port makes of what it matched; null when it cannot (no
-/// such file, a directory to `file`, a kind gone since): the next rule tries.
-fn plumbTarget(p: *pardes.Pardes, port: []const u8, text: []const u8, found: ?filesystem.Resolved, at: Spot) ?Target {
- const to = std.meta.stringToEnum(Port, port);
- if (to == .url) return .{ .url = if (found) |f| f.path else text };
- const f = found orelse return null;
- if (to == null) {
- const k = p.kinds.byName(port) orelse return null;
- return if (f.dir) null else .{ .kind = .{ .path = f.path, .kind = k, .at = at } };
- }
- return switch (to.?) {
- .url => unreachable,
- .dir => if (f.dir) .{ .dir = f.path } else null,
- .file => if (f.dir) null else .{ .file = .{ .path = f.path, .at = at } },
- };
-}
-
pub fn resolve(p: ?*pardes.Pardes, word_raw: []const u8, cwd: []const u8, realbuf: *[4096]u8) Target {
const trimmed = std.mem.trim(u8, word_raw, " \t\r\n");
const pl = parsePathLine(trimmed);
@@ -225,42 +175,6 @@ pub fn resolve(p: ?*pardes.Pardes, word_raw: []const u8, cwd: []const u8, realbu
}
}
- if (p) |pp| if (pp.plumb.items.len > 0) {
- var found: ?filesystem.Resolved = null;
- var looked = false;
- for (pp.plumb.items) |rule| {
- var words = std.mem.tokenizeAny(u8, rule, " \t");
- const how = words.next() orelse continue;
- // The file is asked about once, and only by a rule that needs it.
- const prefix = std.mem.eql(u8, how, "prefix");
- if (!looked and !prefix) {
- found = filesystem.resolve(p, word, cwd, realbuf);
- looked = true;
- }
- const file = if (found) |f| !f.dir else false;
- var hit = std.mem.eql(u8, how, "isfile") and file or
- std.mem.eql(u8, how, "isdir") and found != null and !file;
- var port: []const u8 = "";
- while (words.next()) |w| {
- if (std.mem.eql(u8, w, "to")) {
- port = words.next() orelse "";
- break;
- }
- if (prefix and std.mem.startsWith(u8, trimmed, w)) hit = true;
- // A suffix is a file's: plumb's basic pairs each with `arg isfile`.
- if (std.mem.eql(u8, how, "suffix") and file and std.ascii.endsWithIgnoreCase(found.?.path, w)) hit = true;
- }
- if (!hit) continue;
- const url = std.mem.eql(u8, port, "url");
- if (!looked and !url) {
- found = filesystem.resolve(p, word, cwd, realbuf);
- looked = true;
- }
- // A prefix rule's url is the text, never a file it also names.
- if (plumbTarget(pp, port, trimmed, if (prefix and url) null else found, pl.at)) |t| return t;
- }
- };
-
for (config.url_schemes) |scheme| {
if (std.mem.startsWith(u8, trimmed, scheme)) return .{ .url = trimmed };
}
@@ -873,25 +787,59 @@ pub fn clearNavigationSelection(p: *Pardes, pane: *Pane) void {
if (v.drag == .select and p.panes[v.drag.select.id] == pane) v.drag = .none;
}
+/// The directories a relative name is looked for in, in order, as acme's
+/// look tries its window's and then the other windows' (Incl's come
+/// after, the caller's, as includename's): `first` (the pane's own, a
+/// plumb message's wdir), then the directory of each pane of the jump list
+/// but `from`, newest first, each once.
+pub const Dirs = struct {
+ p: *Pardes,
+ from: ?usize,
+ seen: [MAX_PANES + 1][]const u8 = undefined,
+ nseen: usize = 0,
+ /// The next of the jump list's, counted from its newest.
+ n: usize = 1,
+
+ pub fn init(p: *Pardes, from: ?usize, first: ?[]const u8) Dirs {
+ var d: Dirs = .{ .p = p, .from = from };
+ if (first) |dir| {
+ d.seen[0] = dir;
+ d.nseen = 1;
+ d.n = 0;
+ }
+ return d;
+ }
+
+ pub fn next(d: *Dirs) ?[]const u8 {
+ if (d.n == 0) {
+ d.n = 1;
+ return d.seen[0];
+ }
+ const id = d.from orelse return null;
+ const jumps = &d.p.view().jumps;
+ cand: while (d.n <= jumps.n) {
+ const k = jumps.list[jumps.n - d.n].pane;
+ d.n += 1;
+ if (k == id) continue;
+ const dir = Pardes.paneDir(d.p.panes[k] orelse continue);
+ for (d.seen[0..d.nseen]) |s| if (std.mem.eql(u8, s, dir)) continue :cand;
+ d.seen[d.nseen] = dir;
+ d.nseen += 1;
+ return dir;
+ }
+ return null;
+ }
+};
+
/// Resolve without changing focus or falling back to search. PDF links use
/// the same location rules as an ordinary Look before offering a choice.
fn resolveLookTarget(p: *Pardes, id: usize, txt: []const u8, realbuf: *[4096]u8) Target {
- const v = p.view();
const trimmed = std.mem.trim(u8, txt, " \t\r\n");
const pl = parsePathLine(trimmed);
if (pl.path.len > 0) if (openPaneTarget(p, pl.path, pl.at)) |target| return target;
var found: Target = .none;
- var seen: [MAX_PANES][]const u8 = undefined;
- var nseen: usize = 0;
- var n: usize = 0;
- cand: while (n <= v.jumps.n) : (n += 1) {
- const k: usize = if (n == 0) id else v.jumps.list[v.jumps.n - n].pane;
- if (n > 0 and k == id) continue; // tried first, skip the rerun
- const op = p.panes[k] orelse continue;
- const dir = Pardes.paneDir(op);
- for (seen[0..nseen]) |s| if (std.mem.eql(u8, s, dir)) continue :cand;
- seen[nseen] = dir;
- nseen += 1;
+ var dirs: Dirs = .init(p, id, if (p.panes[id]) |op| Pardes.paneDir(op) else null);
+ while (dirs.next()) |dir| {
if (pl.path.len > 0 and pl.path[0] != '/') {
var joinbuf: [2048]u8 = undefined;
if (std.fmt.bufPrint(&joinbuf, "{s}/{s}", .{ dir, pl.path }) catch null) |j|
@@ -1090,7 +1038,6 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text:
// 9P is no keyboard input, and searches whatever the key mode.
if (pane.addon != null or (pane.body.mode == .tty and !p.fs.serving)) return;
if (text.len == 0) return;
- if (hunks.lookedAtCommit(p, id, pane, trimmed)) return;
if (pane.file != null) return lookWord(p, id, pane, text);
p.word_look = true;
defer p.word_look = false;
@@ -1558,6 +1505,36 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
for (0..pane.sel.len) |slot| pane.clearPointerSelection(slot);
pane.sel = @splat(.{});
}
+ // The plumber, asked before Look's own look as acme asks it (look.c's
+ // look3): what is looked at, sent from this pane. A name already open
+ // in a pane is focused first, never sent; nor is a look that opens
+ // as text (look_text), or the edit port's open of what was. A message
+ // the rules give back to `edit` as sent is Look's own to open, on what
+ // it found; one nothing takes, Look's too.
+ const shown = switch (found) {
+ .pane => true,
+ .file => |f| openPaneTarget(p, f.path, f.at) != null,
+ .kind => |k| openPaneTarget(p, k.path, .{}) != null,
+ .none, .dir, .url => false,
+ };
+ if (!shown and !p.look_text) {
+ const msg: Plumb.Msg = .{
+ .src = if (pane.addon) |k| k.nameOf() else if (pane.body.mode == .tty and !p.fs.serving) "term" else "pardes",
+ .wdir = Pardes.paneDir(pane),
+ .type = "text",
+ .data = trimmed,
+ };
+ const seen: Plumb.Seen = .{ .name = pl.path, .found = switch (found) {
+ .file => |f| .{ .path = f.path },
+ .kind => |k| .{ .path = k.path },
+ .dir => |d| .{ .path = d, .dir = true },
+ .none, .url, .pane => null,
+ } };
+ switch (Plumb.send(p, id, msg, seen)) {
+ .done => return,
+ .look, .refused => {},
+ }
+ }
switch (found) {
.none => lookUnresolved(p, id, pane, trimmed, std.mem.trimEnd(u8, txt, "\r\n")),
.pane => |t| {
@@ -1582,7 +1559,8 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
focusPaneLine(p, t.id, t.at, .center);
settleLine(p, id, trimmed, t.at, before, null);
},
- .url => |u| if (u.len <= 256) p.emit(.{ .open_link = .from(u) }),
+ // acme's own look opens no URL: the plumber's web port does.
+ .url => lookUnresolved(p, id, pane, trimmed, std.mem.trimEnd(u8, txt, "\r\n")),
.dir => |dir| {
// A directory that is not there (the pane's own, gone): said,
// ENOENT, no terminal made in it.
@@ -1603,7 +1581,6 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
const before = v.active;
var opened: ?usize = null;
if (!focusPaneByPath(p, target.path, target.at)) {
- if (hunks.lookedAt(p, id, target.path, target.at)) return;
const free = p.freeSlot() orelse return p.reportError(id, "look", error.NoPaneSlots);
const nt = panes.File.open(p, free, target.path, target.at.line) catch |err|
return lookOpenFailed(p, id, target.path, err);
@@ -2649,51 +2626,83 @@ test "Look on a diff line is the look of the address it names, selected by hand:
try std.testing.expectEqual(@as(i32, 1), seen[0].row);
}
-test "Plumb: the user's rules come before the built-in ones, the first that matches wins, a bad rule is refused, a dump keeps them" {
+test "Plumb: a look sends what it looks at, the rules written first: a rule's word or port takes it, a name open in a pane is focused unsent, one nothing takes is Look's own" {
if (!platform_has_fs) return;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.svg", .data = "<svg/>" });
- try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "shot.PNG", .data = "x" });
var dir_buf: [4096]u8 = undefined;
const root = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var pic_buf: [4200]u8 = undefined;
+ const pic = try std.fmt.bufPrint(&pic_buf, "{s}/pic.svg", .{root});
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 });
defer p.deinit();
- // A kind as a library brings it, its name and extension (no code: a
- // look only names it).
- try std.testing.expect(p.kinds.take(.{ .name = "image", .exts = ".png" }, "") == null);
+ const pane = try p.setTestFile("see https://ziglang.org here\n");
+ // The defaults: a URL to the web port, the host's opener; the hover
+ // asks resolve, as before.
+ lookAt(p, 0, "https://ziglang.org");
+ var linked = false;
+ while (p.nextEffect()) |e| if (e == .open_link) {
+ try std.testing.expectEqualStrings("https://ziglang.org", e.open_link.slice());
+ linked = true;
+ };
+ try std.testing.expect(linked);
var real: [4096]u8 = undefined;
- // None by default: today's ladder.
- try std.testing.expect(resolve(p, "pic.svg", root, &real) == .file);
- try std.testing.expect(resolve(p, "shot.PNG", root, &real) == .kind);
- try std.testing.expect(resolve(p, "https://ziglang.org", root, &real) == .url);
- try std.testing.expect(p.executeBuiltinLine(0, "Plumb suffix .svg .SVGZ to image"));
- try std.testing.expect(p.executeBuiltinLine(0, "Plumb suffix .png to file"));
- try std.testing.expect(p.executeBuiltinLine(0, "Plumb prefix https:// to file"));
- try std.testing.expect(p.executeBuiltinLine(0, "Plumb prefix gopher:// to url"));
- try std.testing.expect(resolve(p, "pic.svg", root, &real) == .kind);
- // Any case, as the kinds' extensions are.
- try std.testing.expect(resolve(p, "shot.PNG", root, &real) == .file);
- // A rule that cannot make its target (no such file) lets the next try.
try std.testing.expect(resolve(p, "https://ziglang.org", root, &real) == .url);
- try std.testing.expectEqualStrings("gopher://x", resolve(p, "gopher://x", root, &real).url);
- // A suffix is a file's: a word naming no file matches none.
- try std.testing.expect(resolve(p, "nothere.svg", root, &real) == .none);
- // Refused, in words, the list as it was: a kind not loaded too.
- for ([_][]const u8{ "Plumb matches x to image", "Plumb suffix to image", "Plumb isdir .x to dir", "Plumb suffix .x to nowhere", "Plumb suffix .x to pdf", "Plumb suffix .x", "Plumb suffix .x to image extra" }) |bad|
- try std.testing.expect(p.executeBuiltinLine(0, bad));
- try std.testing.expectEqual(@as(usize, 4), p.plumb.items.len);
- try std.testing.expectEqualStrings("suffix .svg .SVGZ to image", p.plumb.items[0]);
- try std.testing.expect(p.executeBuiltinLine(0, "Plumb"));
- const pane = p.panes[0].?;
- try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "; suffix .png to file") != null);
- try pardes.dump.dumpState(p);
- const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon");
- defer restored.deinit();
- try std.testing.expectEqual(@as(usize, 4), restored.plumb.items.len);
- // The kind gone, its rule is tried like any that cannot: the ladder's.
- p.kinds.drop(p.kinds.byName("image").?);
- try std.testing.expect(resolve(p, "pic.svg", root, &real) == .file);
- try std.testing.expect(p.executeBuiltinLine(0, "Plumb -"));
- try std.testing.expectEqual(@as(usize, 0), p.plumb.items.len);
+ // A rule set written first takes what it matches: its word started in
+ // place, from the looking pane, and no pane opened.
+ try std.testing.expect(Plumb.apply(p, false, "plumb/rules", "type is text\ndata matches '.*\\.svg'\narg isfile $data\nplumb start Msg svg $file\n\ninclude basic\n") == null);
+ lookAt(p, 0, pic);
+ try std.testing.expectEqual(@as(usize, 0), p.view().active);
+ var said_buf: [4300]u8 = undefined;
+ try std.testing.expectEqualStrings(try std.fmt.bufPrint(&said_buf, "svg {s}", .{pic}), pane.msg[0..pane.msg_len]);
+ // A name open in a pane is focused, never sent.
+ p.look_text = true;
+ lookAt(p, 0, pic);
+ p.look_text = false;
+ const opened = p.view().active;
+ try std.testing.expect(opened != 0);
+ p.view().active = 0;
+ p.setMessage(0, "before");
+ lookAt(p, 0, pic);
+ try std.testing.expectEqual(opened, p.view().active);
+ try std.testing.expectEqualStrings("before", pane.msg[0..pane.msg_len]);
+ // With no web rule, a URL is a word Look searches for.
+ p.view().active = 0;
+ try std.testing.expect(Plumb.apply(p, false, "plumb/rules", "type is text\narg isfile $data\nplumb to edit\n") == null);
+ while (p.nextEffect()) |_| {}
+ lookAt(p, 0, "https://ziglang.org");
+ while (p.nextEffect()) |e| try std.testing.expect(e != .open_link);
+ const dot = @import("ninep/pane.zig").dotOf(pane);
+ try std.testing.expectEqualStrings("https://ziglang.org", pane.file.?.content[dot.q0..dot.q1]);
+}
+
+test "Plumb: a commit hash looked at in a git work tree is git show's, paged; outside one Diff declines, and the word is searched for" {
+ if (!platform_has_fs) return;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDir(std.testing.io, ".git", .default_dir);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "fixed in 3f2a9c1\n" });
+ var dir_buf: [4096]u8 = undefined;
+ const root = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 });
+ defer p.deinit();
+ // /test.txt: its directory, /, is in no work tree.
+ const pane = try p.setTestFile("fixed in 3f2a9c1\n");
+ lookAt(p, 0, "3f2a9c1");
+ const dot = @import("ninep/pane.zig").dotOf(pane);
+ try std.testing.expectEqualStrings("3f2a9c1", pane.file.?.content[dot.q0..dot.q1]);
+ for (p.panes) |slot| if (slot) |pn| try std.testing.expect(pn.command == null);
+ var notes_buf: [4200]u8 = undefined;
+ lookAt(p, 0, try std.fmt.bufPrint(&notes_buf, "{s}/notes.txt", .{root}));
+ const notes = p.view().active;
+ try std.testing.expect(notes != 0);
+ lookAt(p, notes, "3f2a9c1");
+ var paged = false;
+ for (p.panes) |slot| if (slot) |pn| if (pn.command) |cmd| {
+ try std.testing.expect(std.mem.endsWith(u8, cmd, "git show 3f2a9c1"));
+ try std.testing.expect(pn.command_pages);
+ paged = true;
+ };
+ try std.testing.expect(paged);
}