summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig61
1 files changed, 59 insertions, 2 deletions
diff --git a/src/look.zig b/src/look.zig
index f4fbe52e..02754fff 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -587,6 +587,10 @@ fn lookFirstHit(p: *Pardes, id: usize) void {
/// 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.
+/// A search of one pane's text (or its PDF) writes at most this many bytes
+/// of rows: past it, the rows stop and say where.
+pub const search_text_output_bytes = 16 << 20;
+
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;
// Blanks at either end are the pattern's (a look of an indented line);
@@ -598,7 +602,9 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start
// root, and the directory the results buffer is named in. From a
// column's or the workspace's tag, the session's.
const dir = if (p.exec_header) p.sessionDir() else Pardes.paneDir(pane);
- const out = try arena.alloc(u8, filesystem.search_max_output_bytes);
+ // As much as the search can write and no more: a root's hits for each
+ // root Grep walks, one for Find, `search_text_output_bytes` in a pane.
+ var out: []u8 = &.{};
var out_len: usize = 0;
var nrows: usize = 0;
var anchor: ?usize = null;
@@ -631,6 +637,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start
roots[nroots] = d;
nroots += 1;
}
+ out = try @import("memory.zig").unfilled(arena, @max(1, nroots) * filesystem.search_root_output_bytes);
// A pane's directory that is not there (a scratch named into one
// not made yet, a peer's) has nothing to grep: passed over, not the
// search's failure.
@@ -641,9 +648,11 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start
};
nrows = std.mem.count(u8, out[0..out_len], "\n");
} else if (kind == .find) {
+ out = try @import("memory.zig").unfilled(arena, filesystem.search_root_output_bytes);
out_len = try filesystem.find(arena, dir, pat, out, &notes);
nrows = std.mem.count(u8, out[0..out_len], "\n");
} else if (pane.hasPdf()) {
+ out = try @import("memory.zig").unfilled(arena, search_text_output_bytes);
if (comptime pdf_enabled) {
const found = try panes.Pdf.searchRows(
&pane.pdf.?,
@@ -658,6 +667,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start
anchor = found.anchor;
}
} else {
+ out = try @import("memory.zig").unfilled(arena, search_text_output_bytes);
const lines = try edit.paneCursorLines(p, &pane.body);
// a real file names itself; a terminal or an output buffer has no path
const has_path = if (pane.file) |f| f.output == null else false;
@@ -675,7 +685,10 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start
const row = try std.fmt.allocPrint(arena, "{s}:{d}:{d}{c}{d} {s}\n", .{
loc, i + 1, at + 1, config.range_sep, at + pat.len, std.mem.trimEnd(u8, ln, " \t"),
});
- if (row.len > out.len - out_len) break;
+ if (row.len > out.len - out_len) {
+ notes.rows_cut = nrows;
+ break;
+ }
if (start == .cursor and (i < cl or (i == cl and at <= cc))) anchor = nrows;
@memcpy(out[out_len..][0..row.len], row);
out_len += row.len;
@@ -1451,6 +1464,7 @@ 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.image != null or (pane.body.mode == .tty and !p.fs.serving)) return;
if (text.len == 0) return;
+ if (lookCommit(p, id, pane, trimmed)) return;
if (p.settings.look_word == .search and pane.file != null) return lookWord(p, id, pane, text);
p.word_look = true;
defer p.word_look = false;
@@ -1471,6 +1485,49 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text:
}
}
+/// A commit hash (7 to 40 hex digits, a letter and a digit among them)
+/// looked at inside a git repository: `git show` it, paged into a +Pager,
+/// which stacks its hunks in a +Diff (ctl.fillPager); the command pane
+/// goes when it succeeds (exec.commandDone).
+// ponytail: a hex word that names no commit leaves git's error in a command pane, not a search; check the object first if that bites.
+fn lookCommit(p: *Pardes, id: usize, pane: *Pane, word: []const u8) bool {
+ if (comptime !pardes.hosted) return false;
+ if (!isCommitHash(word)) return false;
+ var dir = filesystem.localPath(Pardes.paneDir(pane)) orelse return false;
+ while (true) {
+ var buf: [4096]u8 = undefined;
+ const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return false;
+ if (exec.kindOf(dot_git) != null) break;
+ dir = std.fs.path.dirname(dir) orelse return false;
+ }
+ var line: [64]u8 = undefined;
+ const dst = exec.execute(p, id, std.fmt.bufPrint(&line, "git show {s}", .{word}) catch return false) orelse return true;
+ if (p.panes[dst]) |c| c.command_pages = true;
+ return true;
+}
+
+fn isCommitHash(word: []const u8) bool {
+ if (word.len < 7 or word.len > 40) return false;
+ var digit = false;
+ var letter = false;
+ for (word) |c| switch (c) {
+ '0'...'9' => digit = true,
+ 'a'...'f' => letter = true,
+ else => return false,
+ };
+ return digit and letter;
+}
+
+test "a commit hash is 7 to 40 lowercase hex digits with a letter and a digit" {
+ try std.testing.expect(isCommitHash("9bffa7041c"));
+ try std.testing.expect(isCommitHash("9bffa7041cd2e8a0b6b1f7c3e4d5a6b7c8d9e0f1"));
+ try std.testing.expect(!isCommitHash("9bffa7"));
+ try std.testing.expect(!isCommitHash("defaced"));
+ try std.testing.expect(!isCommitHash("1234567"));
+ try std.testing.expect(!isCommitHash("0x9bffa7041c"));
+ try std.testing.expect(!isCommitHash("9BFFA7041C"));
+}
+
/// 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).