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 --- src/CHANGELOG.md | 5 +++ src/Core.zig | 5 +++ src/builtins.zig | 39 ++++++++++++++++ src/config.zig | 1 + src/look.zig | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+) (limited to 'src') 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); +} -- cgit v1.3