diff options
| author | Gabriel Schneider <[email protected]> | 2026-09-28 17:43:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-01 00:12:15 -0300 |
| commit | 5d29c3151e40c05e764a9112d407082df89a19e1 (patch) | |
| tree | 7dfef2c678e64021908d37c5e293a88dea897605 /src | |
| parent | 63d584a0ba7806759f7139e87f45e411c9a1573d (diff) | |
| download | pardes-5d29c3151e40c05e764a9112d407082df89a19e1.tar.gz pardes-5d29c3151e40c05e764a9112d407082df89a19e1.zip | |
A look takes acme's file:addr and :addr, and one that finds nothing changes nothing
A look at `file:/re/` or `:/re/` searched for the words instead of addressing, and a miss left an empty +Search pane and a cleared selection, while a hit answered the +Search pane on /look. Now an address after a colon (acme look.c:450) is evaluated in the file from its dot; a miss says no match and leaves the panes and the selection alone; a hit reads back as the pane the text is selected in. Both live in lookAt's one flow, so they are one change.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtins.zig | 12 | ||||
| -rw-r--r-- | src/look.zig | 83 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 34 |
3 files changed, 112 insertions, 17 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index a755969b..97f7817f 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1109,8 +1109,10 @@ pub const Find = struct { pub const output: OutputTraits = .{ .name = config.search_buffer, .steps = true }; pub fn run(c: Ctx) void { const pat = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); - if (pat.len > 0) return look.runSearch(c.p, c.id, pat, .find, .top) catch |err| - c.p.reportError(c.id, "find", err); + if (pat.len > 0) { + _ = look.runSearch(c.p, c.id, pat, .find, .top, true) catch |err| c.p.reportError(c.id, "find", err); + return; + } c.p.startPrompt(c.pane, &c.pane.body, .{ .search = config.find_marker }); } }; @@ -1123,8 +1125,10 @@ pub const Grep = struct { pub const output: OutputTraits = .{ .name = config.search_buffer, .steps = true, .locations = true }; pub fn run(c: Ctx) void { const pat = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); - if (pat.len > 0) return look.runSearch(c.p, c.id, pat, .grep, .top) catch |err| - c.p.reportError(c.id, "grep", err); + if (pat.len > 0) { + _ = look.runSearch(c.p, c.id, pat, .grep, .top, true) catch |err| c.p.reportError(c.id, "grep", err); + return; + } c.p.startPrompt(c.pane, &c.pane.body, .{ .search = config.grep_marker }); } }; diff --git a/src/look.zig b/src/look.zig index 2e27a001..020aacb8 100644 --- a/src/look.zig +++ b/src/look.zig @@ -518,7 +518,7 @@ pub fn submitSearch(p: *Pardes, id: usize) void { .grep else .text; - runSearch(p, id, armed[slash + 1 ..], kind, .top) catch |err| return p.reportError(id, "search", err); + _ = runSearch(p, id, armed[slash + 1 ..], kind, .top, true) catch |err| return p.reportError(id, "search", err); if (kind == .text) lookFirstHit(p, id); } @@ -538,10 +538,13 @@ fn lookFirstHit(p: *Pardes, id: usize) void { exec.runBuiltin(p, config.look_cmd, rid, "", txt); } -pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start: SearchStart) !void { - const pane = p.panes[id] orelse return; +/// Fills the results buffer, and answers how many rows a text or PDF +/// search found (0 for Find and Grep, which do not count). With +/// `open_empty` false a text search that found nothing opens no buffer. +pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start: SearchStart, open_empty: bool) !usize { + const pane = p.panes[id] orelse return 0; const pat = std.mem.trim(u8, pat_raw, " \t\r\n"); - if (pat.len == 0) return; + if (pat.len == 0) return 0; const arena = p.scratch.allocator(); // where the pane lives: a file's directory, a shell's cwd — the walk // root, and the directory the results buffer is named in. @@ -617,6 +620,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start nrows += 1; } } + if (!open_empty and kind == .text and nrows == 0) return 0; const content = try p.gpa.dupe(u8, out[0..out_len]); // the buffer records WHICH search filled it, pattern and all: Find and // Grep are builtins (words you can execute), the bare `/` is a key @@ -626,6 +630,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start .grep => .{ .cmd = .Grep }, }; try panes.Output.fillResults(p, id, dir, from, pat, content, anchor); + return nrows; } pub fn searchStep(p: *Pardes, id: usize, delta: i32) bool { @@ -1137,6 +1142,53 @@ pub fn followPdfLink(p: *Pardes, id: usize, probe: *const panes.Pdf.WordProbe) v } } +/// `file:addr` or `:addr`, acme's look.c:450-543: a colon followed by an +/// address character (addr.c:28, isaddrc) that is not a line spot this +/// parser already reads (`file:12`, `file:12:5`). Not a URL. +fn addressed(tok: []const u8) ?struct { path: []const u8, expr: []const u8 } { + if (std.mem.indexOf(u8, tok, "://") != null) return null; + if (std.mem.indexOfAny(u8, tok, " \t") != null) return null; + const colon = std.mem.indexOfScalar(u8, tok, ':') orelse return null; + const expr = tok[colon + 1 ..]; + if (expr.len == 0 or std.mem.indexOfScalar(u8, "0123456789+-/$.#,;", expr[0]) == null) return null; + if (std.ascii.isDigit(expr[0])) { + const pl = parsePathLine(tok); + if (pl.at.line != 0 and pl.end == tok.len) return null; + } + return .{ .path = tok[0..colon], .expr = expr }; +} + +/// Opens (or finds) `path` as Look would, `id`'s own pane when it is empty, +/// and selects what `expr` addresses there from its dot. An address that +/// does not evaluate moves nothing and says why (acme: no jump). +fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void { + const ninep_pane = @import("ninep/pane.zig"); + const Addr = @import("ninep/addr.zig").Addr; + var target = id; + if (path.len > 0) { + const before = p.active; + lookAt(p, id, path); + target = p.active; + // The path opened nothing (a search, or no such file): Look said so. + if (target == before and target != id) return; + } + const pane = p.panes[target] orelse return; + const f = pane.file orelse return; + if (path.len > 0 and (f.output != null or !std.mem.endsWith(u8, f.path, std.fs.path.basename(path)))) return; + var a: Addr = .{ .text = f.content, .lim = null, .expr = expr }; + const r = a.address(ninep_pane.dotOf(pane)) orelse { + var said: [128]u8 = undefined; + return p.setMessage(id, std.fmt.bufPrint(&said, "Look: {s}", .{a.err}) catch "Look: bad address"); + }; + if (a.i < expr.len or r.q0 > r.q1) { + var said: [128]u8 = undefined; + return p.setMessage(id, std.fmt.bufPrint(&said, "Look: {s}", .{if (r.q0 > r.q1) "addresses out of order" else "bad address syntax"}) catch "Look: bad address"); + } + clearNavigationSelection(p, pane); + ninep_pane.setDot(pane, r); + p.active = target; +} + pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void { const pane = p.panes[id] orelse return; p.noteHaptic(.look); @@ -1149,31 +1201,36 @@ pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void { // The operand may borrow selected terminal/PDF text released below. const txt = p.scratch.allocator().dupe(u8, operand) catch return; const trimmed = std.mem.trim(u8, txt, " \t\r\n"); - clearNavigationSelection(p, pane); + // acme's `file:addr` and `:addr` (look.c:450-543): an address after + // the colon, evaluated in that file from its dot (openfile, look.c:675). + if (addressed(trimmed)) |ad| return lookAddress(p, id, ad.path, ad.expr); const pl = parsePathLine(trimmed); + if (comptime pdf_enabled) if (pane.hasPdf()) clearNavigationSelection(p, pane); if (comptime pdf_enabled) if (panes.Pdf.lookSection(p, id, pl.path, pl.at)) return; var realbuf: [4096]u8 = undefined; const found = resolveLookTarget(p, id, txt, &realbuf); + // A miss changes nothing, the selection included; anything else starts + // from a clean one. + if (found != .none) clearNavigationSelection(p, pane); switch (found) { .none => { if (pane.image != null or pane.body.mode == .tty) return; - runSearch(p, id, trimmed, .text, .cursor) catch |err| { + const rows = runSearch(p, id, trimmed, .text, .cursor, false) catch |err| { p.reportError(id, "search", err); return; }; + if (rows == 0) { + var said: [96]u8 = undefined; + return p.setMessage(id, std.fmt.bufPrint(&said, "Look: no match for \"{s}\"", .{@import("Messages.zig").clip(trimmed, 48)}) catch "Look: no match"); + } + clearNavigationSelection(p, pane); const at = pane.search_row; _ = searchStep(p, id, 1); if (at != null and pane.search_row == at) { pane.search_row = null; _ = searchStep(p, id, 1); } - // A path that is not there searches for its text, and a search - // that found nothing says so rather than leave an empty buffer. - const results = if (pane.search_pane) |rid| p.panes[rid] else null; - if (results) |r| if (r.file) |f| if (f.content.len == 0) { - var said: [96]u8 = undefined; - p.setMessage(id, std.fmt.bufPrint(&said, "Look: no match for \"{s}\"", .{@import("Messages.zig").clip(trimmed, 48)}) catch "Look: no match"); - }; + }, .pane => |t| { if (t.id >= MAX_PANES) return; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index e4e983a8..8b73e8f7 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -76,6 +76,9 @@ fn run(p: *Pardes, id: usize, exec: bool, text: []const u8) void { const after = Serials.snapshot(p); var created = false; for (after.list[0..after.len]) |serial| if (std.mem.indexOfScalar(u32, before.list[0..before.len], serial) == null) { + // A look's hits buffer is how it found the text, not where: it + // answers the pane the hit is selected in. + if (!exec) if (p.paneBySerial(serial)) |made| if (p.panes[made].?.file) |f| if (f.output) |o| if (o.from == .search) continue; noteResult(p, serial); created = true; }; @@ -739,6 +742,37 @@ test "a second lock is refused until the holder unlocks or closes, and binds nob for (p.fs.opens) |o| try testing.expect(o.node == 0); } +test "a look takes acme's :addr and file:addr, and a miss changes nothing" { + const gpa = testing.allocator; + const p = try withFile(gpa, "foo x foo y\nbar\n"); + defer p.deinit(); + const serial = serialOf(p); + const look_node = Node.of(serial, .look); + const dot = Node.of(serial, .dot); + // :addr is an address in this pane, evaluated from its dot. + try testing.expectEqual(Status.ok, wr(p, look_node, ":/y/\n").reply.status); + try testing.expectEqualStrings(" 10 11 ", rd(p, dot, 0, 64).bytes); + try testing.expectEqual(Status.ok, wr(p, look_node, ":#4\n").reply.status); + try testing.expectEqualStrings(" 4 4 ", rd(p, dot, 0, 64).bytes); + try testing.expectEqual(Status.ok, wr(p, look_node, ":/foo/\n").reply.status); // from #4: the second foo + try testing.expectEqualStrings(" 6 9 ", rd(p, dot, 0, 64).bytes); + // file:addr names the pane's own file here. + var line: [256]u8 = undefined; + const path = p.panes[0].?.file.?.path; + try testing.expectEqual(Status.ok, wr(p, look_node, try std.fmt.bufPrint(&line, "{s}:/bar/\n", .{path})).reply.status); + try testing.expectEqualStrings(" 12 15 ", rd(p, dot, 0, 64).bytes); + // A miss: no buffer, the selection kept, and it says so. + const panes_before = p.panes.len - std.mem.count(?*pardes.Pane, &p.panes, &.{null}); + try testing.expectEqual(Status.ok, wr(p, look_node, "zzqq\n").reply.status); + try testing.expectEqual(panes_before, p.panes.len - std.mem.count(?*pardes.Pane, &p.panes, &.{null})); + try testing.expectEqualStrings(" 12 15 ", rd(p, dot, 0, 64).bytes); + try testing.expectStringStartsWith(p.panes[0].?.msg[0..p.panes[0].?.msg_len], "Look: no match"); + // A hit answers the pane the text is selected in, not its hits buffer. + try testing.expectEqual(Status.ok, wr(p, look_node, "foo\n").reply.status); + var own: [16]u8 = undefined; + try testing.expectEqualStrings(try std.fmt.bufPrint(&own, "{d}\n", .{serial}), rd(p, look_node, 0, 64).bytes); +} + test "exec runs a builtin at the pane and records the pane it acted on" { const gpa = testing.allocator; const p = try withFile(gpa, "Msg fs-ran\n"); |
