diff options
Diffstat (limited to 'src/look.zig')
| -rw-r--r-- | src/look.zig | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/src/look.zig b/src/look.zig index ca9aa80a..236f0687 100644 --- a/src/look.zig +++ b/src/look.zig @@ -667,7 +667,10 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, kind: Search, start nrows += 1; } } - if (!open_empty and nrows == 0) return 0; + // A Find or Grep that finds nothing empties its directory's +Search, if + // there is one, rather than leave the last search's rows looking like + // this one's. + if (!open_empty and nrows == 0 and (kind == .text or !panes.Output.hasFileSearch(p, dir))) 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 @@ -1381,8 +1384,52 @@ fn spotAt(text: []const u8, q: u32) struct { line: usize, col: usize } { return .{ .line = line, .col = upto.len - start + 1 }; } +/// A remembered place in `path` (Recent, the jumplist): the file opened or +/// focused, then `at` clamped to it as it is now, a line past the end its +/// last line, never a miss; a selection's two ends restored where they +/// still fit. +pub fn reopenAt(p: *Pardes, from: usize, path: []const u8, at: Spot) void { + lookAt(p, from, path); + const id = p.active; + const pane = p.panes[id] orelse return; + const f = if (pane.file) |*file| file else return; + if (!std.mem.eql(u8, f.path, path)) return; + const n = @max(1, panes.File.nlines(p.gpa, f)); + const clamp = struct { + fn spot(text: []const u8, lines: usize, line: usize, col: usize) [2]usize { + const l = std.math.clamp(line, 1, lines); + const width = modal.lineSlice(text, l - 1).len + 1; + return .{ l, std.math.clamp(col, 1, width) }; + } + }; + const head = clamp.spot(f.content, n, at.line, at.col); + var to: Spot = .{ .line = head[0], .col = head[1] }; + if (at.end_line != 0) { + const end = clamp.spot(f.content, n, at.end_line, at.end_col); + to.end_line = end[0]; + to.end_col = end[1]; + } + clearNavigationSelection(p, pane); + focusPaneLine(p, id, to, .center); +} + +/// Whether `pane` is the +Recent list, whose rows are remembered places. +fn isRecentList(pane: *const Pane) bool { + const f = pane.file orelse return false; + const o = f.output orelse return false; + return std.meta.eql(o.from, panes.Output.Origin{ .cmd = .Recent }); +} + pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void { const pane = p.panes[id] orelse return; + // A +Recent row is a remembered place: reopened and clamped to the file + // as it is now, never a miss for a line the file no longer has. + if (isRecentList(pane)) { + const row = std.mem.trim(u8, operand, " \t\r\n"); + const loc = row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len]; + const pl = parsePathLine(loc); + if (pl.path.len > 0 and pl.path[0] == '/' and pl.at.line != 0) return reopenAt(p, id, pl.path, pl.at); + } p.noteHaptic(.look); noteLookSource(p, id); const command = std.mem.trim(u8, operand, " \t\r\n"); @@ -1423,12 +1470,16 @@ pub fn lookAt(p: *Pardes, id: usize, operand: []const u8) void { } switch (found) { .none => { - // `.` or `..` from a pane whose directory is gone: said, ENOENT, - // not searched for as a word. + // A `./` or `../` name opened nothing: said, ENOENT, not searched + // for as a word. What is missing is the pane's directory when + // that is gone, else the file named. if (filesystem.dotRelative(trimmed)) { const dir = Pardes.paneDir(pane); var said: [4200]u8 = undefined; - return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such directory", .{dir[0..@min(dir.len, 4096)]}) catch "look: no such directory"); + const gone = if (comptime pardes.hosted) if (filesystem.localPath(dir)) |local| !exec.isDirectory(local) else false else true; + if (gone) return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such directory", .{dir[0..@min(dir.len, 4096)]}) catch "look: no such directory"); + var cut: [miss_path_cap + "…".len]u8 = undefined; + return p.reportFailure(id, std.fmt.bufPrint(&said, "look: {s}: no such file", .{missText(trimmed, &cut)}) catch "look: no such file"); } // A raw terminal's clicks are its program's; a look written over // 9P is no keyboard input, and searches whatever the key mode. |
