From c37a700474e8b84cc3c7db8bf550b7c5f18b1871 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 13:15:15 -0300 Subject: kinds #23, the plumber: Plumb, skqrvwks's rules ported (one a line in plumb(6)'s order, the session's and kept in a dump, tried before the built-in ladder, prefix, suffix, isdir and isfile), its ports url, dir, file or any loaded kind by its name from the registry, a rule naming a kind not loaded refused in words when written and passed over like any rule that cannot make its target when its kind has gone since, so `Plumb suffix .svg to image` in init sends a look to the image addon Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- docs/typ/builtins-gui.json | 1 + docs/typ/builtins-tui.json | 1 + docs/typ/guide.typ | 2 +- docs/typ/reference.typ | 2 +- src/CHANGELOG.md | 5 ++ src/Core.zig | 5 ++ src/builtins.zig | 39 +++++++++++++ src/config.zig | 1 + src/look.zig | 135 +++++++++++++++++++++++++++++++++++++++++++++ test/behaviour/addons.zig | 23 ++++++++ 10 files changed, 212 insertions(+), 2 deletions(-) diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index 40a99f83..d838b59d 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -73,6 +73,7 @@ {"name": "Panes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pick an open pane by its name in a query pane under this one (SPC b b)."}, {"name": "Pet", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "off, cat, frog", "doc": "The window's companion in the workspace tag's blank space: off, cat or frog."}, {"name": "Petscii", "leader": "SPC t p", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "glyph art over the host's pixels: an image pane's picture, or the kitty graphics a terminal's program placed (yazi's previews)"}, + {"name": "Plumb", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "The plumber's rules, for the session, one a line as plumb(6) orders a rule: `Plumb suffix .svg to image` makes a look at an existing `.svg` file an image pane."}, {"name": "Putall", "leader": null, "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "acme's Putall: every pane holding unsaved edits to a file is saved, each as its own Save; one that is refused (changed on disk since, a write that fails) says so and the rest are saved all the same."}, {"name": "Recent", "leader": "SPC f r", "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "List the files opened lately, closed ones too, each a place to open again."}, {"name": "Redo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Redo the last undone edit."}, diff --git a/docs/typ/builtins-tui.json b/docs/typ/builtins-tui.json index 728bb884..9579a031 100644 --- a/docs/typ/builtins-tui.json +++ b/docs/typ/builtins-tui.json @@ -68,6 +68,7 @@ {"name": "Palette", "leader": "SPC t l", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "the C64 palette or the terminal's own 16"}, {"name": "Panes", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Pick an open pane by its name in a query pane under this one (SPC b b)."}, {"name": "Petscii", "leader": "SPC t p", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "glyph art over the host's pixels: an image pane's picture, or the kitty graphics a terminal's program placed (yazi's previews)"}, + {"name": "Plumb", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "The plumber's rules, for the session, one a line as plumb(6) orders a rule: `Plumb suffix .svg to image` makes a look at an existing `.svg` file an image pane."}, {"name": "Putall", "leader": null, "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "acme's Putall: every pane holding unsaved edits to a file is saved, each as its own Save; one that is refused (changed on disk since, a write that fails) says so and the rest are saved all the same."}, {"name": "Recent", "leader": "SPC f r", "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "List the files opened lately, closed ones too, each a place to open again."}, {"name": "Redo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Redo the last undone edit."}, diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index d69b1471..2128f0cc 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -157,7 +157,7 @@ pardes. addr("notes.txt:12"), [line 12], addr("notes.txt:/TODO/"), [the next match of `TODO`], addr("src/"), [a pane listing it, as acme's directory window: a #word("Look") at an entry opens it, `..` goes up, `Get` reads it again], - addr("https://ziglang.org"), [the browser, through `xdg-open` (`open` on macOS); there is no plumber], + addr("https://ziglang.org"), [the browser, through `xdg-open` (`open` on macOS); #word("Plumb") adds rules of your own], ) #key("Enter") does the same from the keyboard; the other address forms are diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index abf2964f..20c3bf06 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -1263,7 +1263,7 @@ with Client(sys.argv[1]) as c: # a socket path, or (ip, port) [Edit], [every acme command but `B ` command changes nothing, where acme puts in what it wrote (#doc("fs", section: "edit"))], [`win`], [#word("Term") is a VT terminal pane; #file("pty/run") runs a line at its prompt and answers `exit N` and the output; with #word("Repl") bound, #word("Exec") on a source pane types the text into it], [a directory window], [as acme's: a text pane named `dir/` (#file("index") kind `text`, #file("ctl") isdir 1), its entries in columns sorted bytewise, dotfiles shown, a directory's marked `/`; blanks pad the columns where acme's tabs do; a #word("Look") at an entry is from that directory, `Get` or a look at it again reads it again, and an edit is kept until then rather than lost to a resize; never dirty, #word("Save") refuses it], - [the plumber], [none: #word("Look") on an `http://` or `https://` URL runs `xdg-open` (`open` on macOS); files, addresses and directories follow built-in rules], + [the plumber], [no process: #word("Plumb") rules, one a line in plumb(6)'s order (`Plumb suffix .svg to image`), for the session and kept in a dump, are tried before the built-in ones and the first that matches takes the #word("Look"); the tests are `prefix`, `suffix` (an existing file's name, any case), `isdir` and `isfile`, the ports `url` (`xdg-open`, `open` on macOS), `dir`, `file` and any loaded kind by its name (`image`, `pdf`): a rule naming a kind not loaded is refused, and one whose kind has gone since is passed over; no `matches`, attributes, variables or `include`; bare, #word("Plumb") lists the rules, `Plumb -` empties them; the user's own go in `init`, a `Plumb` line each; the built-in rules: an `http://` or `https://` URL to `url`, a directory, a kind's extensions, then any file], [#key("Esc") selects what was typed], [#key("Esc") leaves insert mode, or goes back a pane (#doc("tags", section: "modes"))], ) diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 96f9b735..30ee23cc 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +- `Plumb`: rules of your own for what a `Look` opens, one a line in + plumb(6)'s order, `Plumb suffix .svg to image`, `Plumb prefix gopher:// + to url`; tried before the built-in ones, the first that matches wins. + The tests are `prefix`, `suffix`, `isdir` and `isfile`; the ports + `url`, `dir`, `file` and any loaded kind. In `init` or the root `ctl`. - PDFs are an addon: `lib/pardes/addons/pdf.so` (addons/pdf, over MuPDF, which left the binaries), installed beside them and loaded at start; a pardes without it shows a PDF as text. A PDF's tag starts with its page, diff --git a/src/Core.zig b/src/Core.zig index cd7b4ec5..a595e6b0 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -210,6 +210,9 @@ db: Db = .{}, /// (`stdio.h`, ``) is tried in, in order. The session's, seeded /// with C's (/usr/include, /usr/local/include), kept in a dump. incl: std.ArrayList([]u8) = .empty, +/// Plumb's rules, the user's, tried in order before the built-in ones +/// (look.resolve), the first to match taking the look. Kept in a dump. +plumb: std.ArrayList([]u8) = .empty, /// Zerox's groups of panes on one buffer (File.syncTwins), and the id /// the next takes. twin_groups: std.ArrayList(pardes.panes.File.TwinGroup) = .empty, @@ -761,6 +764,8 @@ pub fn deinit(p: *Pardes) void { p.recent.deinit(p.recentGpa()); for (p.incl.items) |dir| p.gpa.free(dir); p.incl.deinit(p.gpa); + for (p.plumb.items) |rule| p.gpa.free(rule); + p.plumb.deinit(p.gpa); for (p.twin_groups.items) |group| p.gpa.free(group.path); p.twin_groups.deinit(p.gpa); p.fs.batch.bytes.deinit(p.gpa); diff --git a/src/builtins.zig b/src/builtins.zig index e5c7753d..ada02e0c 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1148,6 +1148,45 @@ pub const Incl = struct { } }; +/// The plumber's rules, for the session, one a line as plumb(6) orders a +/// rule: `Plumb suffix .svg to image` makes a look at an existing `.svg` +/// file an image pane. The user's rules are tried in the order given, +/// before the built-in ones, and the first that matches takes the look. +/// Bare, it lists them; `Plumb -` empties the list. +pub const Plumb = struct { + pub const scope: Scope = .session; + pub const takes_arg = true; + pub fn run(c: Ctx) void { + const p = c.p; + const typed = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); + if (std.mem.eql(u8, typed, "-")) { + for (p.plumb.items) |rule| p.gpa.free(rule); + p.plumb.clearRetainingCapacity(); + return p.setMessage(c.id, "Plumb: none"); + } + if (typed.len > 0) { + var said: [320]u8 = undefined; + if (look.plumbRuleError(&p.kinds, typed)) |why| + return p.reportFailure(c.id, std.fmt.bufPrint(&said, "Plumb: {s}", .{why}) catch "Plumb: invalid rule"); + // One blank between words, as the list shows it. + var rule: std.ArrayList(u8) = .empty; + var words = std.mem.tokenizeAny(u8, typed, " \t"); + while (words.next()) |w| { + if (rule.items.len > 0) rule.append(p.gpa, ' ') catch return rule.deinit(p.gpa); + rule.appendSlice(p.gpa, w) catch return rule.deinit(p.gpa); + } + const owned = rule.toOwnedSlice(p.gpa) catch return rule.deinit(p.gpa); + p.plumb.append(p.gpa, owned) catch return p.gpa.free(owned); + return; + } + var list: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + list.writer.writeAll("Plumb:") catch return; + for (p.plumb.items, 0..) |rule, i| list.writer.print("{s} {s}", .{ if (i == 0) "" else ";", rule }) catch return; + if (p.plumb.items.len == 0) list.writer.writeAll(" none") catch return; + p.setMessage(c.id, list.written()); + } +}; + /// acme's Zerox: a second pane on this pane's file, the same text, undo and /// unsaved state, with a scroll and a cursor of its own (File.zerox). pub const Zerox = struct { diff --git a/src/config.zig b/src/config.zig index 17a7305a..2cecd2be 100644 --- a/src/config.zig +++ b/src/config.zig @@ -89,6 +89,7 @@ pub const leader_path = paths: { .Get = null, .Putall = null, .Incl = null, + .Plumb = null, .Tab = null, .CallstackDepth = null, .Zerox = null, diff --git a/src/look.zig b/src/look.zig index 7ac8798b..52674ecf 100644 --- a/src/look.zig +++ b/src/look.zig @@ -304,6 +304,56 @@ test "lookableLineSpan prefers the longest run, so a blank inside a path is one ); } +/// 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: ` +/// [word ...] to `, 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: [word ...] to "; + 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 `"; +} + +/// 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); @@ -326,6 +376,42 @@ 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 }; } @@ -2579,3 +2665,52 @@ test "Look on a diff line is the look of the address it names, selected by hand: /// The rows n/N's walk over look-able text reads before it stops (look.zig). pub const max_look_rows = 8192; + +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" { + 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 = "" }); + 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)]; + 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); + 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); +} diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig index 4a624c22..b64a6974 100644 --- a/test/behaviour/addons.zig +++ b/test/behaviour/addons.zig @@ -174,6 +174,29 @@ test "addons: the image addon opens the pictures a Look names, fitted, with the try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,4,2]")); } +test "addons: a Plumb rule to a loaded kind opens that kind's pane, is refused naming one not loaded, and with its kind gone the look falls through to text" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try load(&s, Session.image_addon); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + // A PNG under a name no kind claims. + const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82"; + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "red.pic", .data = png }); + try std.testing.expect(try s.refused("/ctl", "Plumb suffix .pic to nokind\n") != 0); + try std.testing.expect(has(s.said, "Plumb: invalid port: url, dir, file or a loaded kind")); + try s.write("/ctl", "Plumb suffix .pic to image\n"); + try s.write("/look", s.at("{s}/red.pic\n", .{dir})); + try std.testing.expectEqual(try paneOf(&s, "image"), try s.focused()); + try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n"); + try s.write("/ctl", "Addon unload image\n"); + try s.write("/look", s.at("{s}/red.pic\n", .{dir})); + try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{try s.focused()})), "red.pic")); + try std.testing.expectError(error.NoSuchPane, paneOf(&s, "image")); +} + test "addons: a dump keeps an addon's pane; restored without its addon it waits as a placeholder, and opens in place when the addon comes" { var s = try Session.initFile(std.testing.allocator, "x\n"); defer s.deinit(); -- cgit v1.3