diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 03:51:15 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 79a1e816ceb13ef81b712007716c8add2de75434 (patch) | |
| tree | 1714b1bf19da3a58be2623ffe29e3f8be1b21fe3 /src/look.zig | |
| parent | a22b579ad2df8d1811f0e194fa1bbc06e2282cf8 (diff) | |
| parent | f1af015f01e4e8d42f6224c4d4063f3aea218f5a (diff) | |
| download | pardes-79a1e816ceb13ef81b712007716c8add2de75434.tar.gz pardes-79a1e816ceb13ef81b712007716c8add2de75434.zip | |
integration: the terminal a module of its own, with the plumber whole and the walker's cheaper compile
A merge of term-phase1a and pardes-next, no conflicts. core-test (the core with no terminal) 411 passed, 535 skipped of 946; shell-test (the same tests, the terminal compiled in) 989/989, on tui.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/look.zig')
| -rw-r--r-- | src/look.zig | 287 |
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(¬es_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); } |
