summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig83
1 files changed, 70 insertions, 13 deletions
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;