diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Pick.zig | 205 | ||||
| -rw-r--r-- | core/Trail.zig | 42 | ||||
| -rw-r--r-- | core/builtins.zig | 10 | ||||
| -rw-r--r-- | core/config.zig | 1 | ||||
| -rw-r--r-- | core/normal.zig | 2 |
5 files changed, 241 insertions, 19 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 0f9134bb..372ff139 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -168,13 +168,8 @@ pub fn open(p: *Pardes, from: usize, source: Source, word: []const u8, query: [] pk.word.set(if (word.len > 0) word else words.get(source).word); pk.keys.set(words.get(source).keys); pk.dir.set(Pardes.paneDir(asker)); - pk.owner = asker.serial; pk.rows = p.gpa.dupe(u8, rows) catch |err| return p.reportError(.at(from), "pick", err); - const landing = landingFor(p, from) catch |err| return p.reportError(.at(from), "pick", err); - // The keyboard stays where it was: the landing pane is the tag's host. - p.view().active = from; - pk.landing = p.panes[landing].?.serial; - pk.made = landing != from and Trail.readerOf(p, asker) != landing and !Trail.reading(p, asker); + hostIn(p, pk, from) catch |err| return p.reportError(.at(from), "pick", err); unroll(p, pk, query); start(p, pk); filter(p, pk); @@ -206,13 +201,23 @@ fn make(p: *Pardes, source: Source) !*Pick { return pk; } -/// The pane a picker from pane `from` lands in: `from` when it reads; its -/// reader when it is held and has one; else a pane made under it as its -/// reader would be, empty until the preview fills it. +/// Picker `pk` asked from pane `from`: the pane it lands in is `from` when +/// it reads; its reader when it is held and has one; else a pane made under +/// it as its reader would be, empty until the preview fills it. The +/// keyboard stays where it was: the landing pane is the tag's host. +fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void { + const asker = p.panes[from].?; + pk.owner = asker.serial; + pk.made = false; + const reader = if (Trail.reading(p, asker)) from else Trail.readerOf(p, asker); + pk.landing = p.panes[reader orelse try landingFor(p, from)].?.serial; + pk.made = reader == null; + p.view().active = from; +} + +/// A pane made under pane `from` for a picker, where its reader goes. fn landingFor(p: *Pardes, from: usize) !usize { const asker = p.panes[from].?; - if (Trail.reading(p, asker)) return from; - if (Trail.readerOf(p, asker)) |r| return r; const free = p.freeSlot() orelse return error.NoPaneSlots; const content = try p.gpa.dupe(u8, ""); errdefer p.gpa.free(content); @@ -997,9 +1002,13 @@ fn land(p: *Pardes, how: How) void { }, else => {}, }; + // A step from the pane it lands in when that reads (its owner's reader + // or itself), else from its owner, whose reader the place opens as. + const landing = if (pk.made) null else p.paneBySerial(pk.landing); TagState.toRest(p); const v = p.view(); - const from = p.paneBySerial(pk.owner) orelse v.active; + const owner = p.paneBySerial(pk.owner) orelse v.active; + const from = if (how == .walk or how == .queue) landing orelse owner else owner; v.active = from; v.jumps.force = true; for (picked, 0..) |t, k| switch (t) { @@ -1011,7 +1020,10 @@ fn land(p: *Pardes, how: How) void { _ = p.store.reopen(p, serial, from); }, .place => |at| if (k == 0 and (how == .walk or how == .queue)) { - _ = look.open(p, from, at.path, at.at, .replace); + switch (look.open(p, from, at.path, at.at, .replace)) { + .none => {}, + else => listed(p, pk), + } } else if (k == 0 and how == .column) { layout.newColumn(p, from); const c = layout.activeColumn(p) orelse continue; @@ -1029,6 +1041,113 @@ fn land(p: *Pardes, how: How) void { } } +// ---- after Enter: the list word ---- + +/// The list as its word in a trail says it (SPEC §5.6): its source, its +/// query, where its cursor is among its rows, and its marks: +/// `Find pick 3/6 •2`. +fn wordOf(p: *const Pardes, pk: *const Pick, buf: *Kind.Buf(Trail.max_text)) []const u8 { + var w: std.Io.Writer = .fixed(&buf.bytes); + w.writeAll(pk.word.slice()) catch {}; + for (pk.refined[0..pk.nrefined]) |r| w.print(" {s} |", .{r.query.slice()}) catch {}; + if (pk.query.len > 0) w.print(" {s}", .{pk.query.slice()}) catch {}; + const n = count(p, pk); + w.print(" {d}/{d}", .{ if (n == 0) 0 else pk.at + 1, n }) catch {}; + if (pk.marks.items.len > 0) w.print(" •{d}", .{pk.marks.items.len}) catch {}; + buf.len = @intCast(w.buffered().len); + return buf.slice(); +} + +/// The list rolled into the trail of the reading pane it landed in, its +/// word before the place: the list `n` and `N` walk there. +fn listed(p: *Pardes, pk: *const Pick) void { + const pane = p.panes[p.view().active] orelse return; + if (!Trail.reading(p, pane)) return; + var w: Trail.Word = .{ .index = pk.id }; + _ = wordOf(p, pk, &w.text); + Trail.through(&pane.trail, .{ .list = w }); +} + +/// `n` and `N` in a reading pane whose trail's newest list or search word +/// is a list's: its next or previous row in place of the current step +/// (Trail.replaceStep), its word saying where it is now. False when there +/// is no list for them to walk (normal.zig: the search's then). +pub fn walkList(p: *Pardes, id: usize, forward: bool) bool { + const pane = p.panes[id] orelse return false; + if (!Trail.reading(p, pane)) return false; + const t = &pane.trail; + var i: usize = @min(t.cur, t.n); + while (i > 0) { + i -= 1; + switch (t.steps[i]) { + .list => break, + .search => return false, + else => {}, + } + } else return false; + const pk = byId(p, t.steps[i].list.index) orelse return false; + const n = count(p, pk); + if (n == 0) return false; + if (if (forward) pk.at + 1 >= n else pk.at == 0) { + p.setMessage(.at(id), if (forward) "n: the list's last row" else "N: the list's first row"); + return true; + } + pk.at = if (forward) pk.at + 1 else pk.at - 1; + var arena = std.heap.ArenaAllocator.init(p.gpa); + defer arena.deinit(); + const row = rowAt(p, pk, arena.allocator(), pk.at) catch return true; + const at = switch (row.target) { + .place => |x| x, + else => return true, + }; + _ = wordOf(p, pk, &t.steps[i].list.text); + _ = Trail.replaceStep(p, id, at.path, at.at) catch |err| p.reportError(.at(id), "walk", err); + return true; +} + +/// Look on a list word in pane `id`'s trail (SPEC §6.3): the list unrolled +/// again in that pane's tag, its rows, marks, cursor and query as they were. +pub fn reopen(p: *Pardes, id: usize, index: u32) void { + const pk = byId(p, index) orelse return p.setMessage(.at(id), "walk: that list is gone: the last 8 are kept"); + const pane = p.panes[id] orelse return; + TagState.toRest(p); + pk.landing = pane.serial; + pk.made = false; + unroll(p, pk, pk.query.slice()); + unrollRows(p, pk); +} + +/// A list unrolled again: All's places found again, the cursor where it was. +fn unrollRows(p: *Pardes, pk: *Pick) void { + const at = pk.at; + if (!fuzzy(pk) and pk.source == .all) filter(p, pk); + p.view().tag_state.rows.at = @intCast(@min(at, std.math.maxInt(u16))); +} + +/// Exec on a list word (SPEC §6.3): its source run again with its query, +/// from pane `id`. +pub fn rerun(p: *Pardes, id: usize, index: u32) void { + const pk = byId(p, index) orelse return p.setMessage(.at(id), "walk: that list is gone: the last 8 are kept"); + var query: Kind.Buf(256) = .{}; + query.set(pk.query.slice()); + open(p, id, pk.source, pk.word.slice(), query.slice(), pk.rows); +} + +/// Resume (`SPC '`): the last picker reopened as it was, from pane `from`, +/// in the tag of the pane its Enter lands in. +pub fn resumeFrom(p: *Pardes, from: usize) void { + var newest: ?*Pick = null; + for (p.picks) |slot| if (slot) |pk| if (newest == null or pk.id > newest.?.id) { + newest = pk; + }; + const pk = newest orelse return p.setMessage(.at(from), "Resume: no picker yet"); + TagState.toRest(p); + if (p.panes[from] == null) return; + hostIn(p, pk, from) catch |err| return p.reportError(.at(from), "Resume", err); + unroll(p, pk, pk.query.slice()); + unrollRows(p, pk); +} + /// B3 on a picker's row (mouse.zig bandPress): Enter on it. pub fn landHere(p: *Pardes) void { const pk = live(p) orelse return; @@ -1476,6 +1595,66 @@ test "M-l unrolls the act step, which says what it acts on; Copy puts the places try std.testing.expectEqual(@as(usize, 1), pk.queue.items.len); } +test "after Enter the list is a word in the reader's trail: n and N walk its rows in place, a Look on it reopens it with its cursor and marks, Exec runs it again, Resume reopens the last" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "one\ntwo\nthree\nfour\n"); + defer p.gpa.free(a_path); + const b_path = try testFile(p, &tmp, "beta.zig", "b1\nb2\n"); + defer p.gpa.free(b_path); + _ = try p.setTestFile("held\n"); + const held = p.view().active; + Trail.keep(p.panes[held].?); + p.gpa.free(p.panes[held].?.file.?.path); + p.panes[held].?.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ std.Io.Dir.path.dirname(a_path).?, "held.txt" }); + open(p, held, .list, "Refs", "", "alpha.zig:2:1 two\nbeta.zig:2:1 b2\nalpha.zig:4:1 four\n"); + const pk = live(p).?; + settle(p); + p.update(.{ .key = .{ .cp = Key.tab } }); + p.update(.{ .key = .{ .cp = Key.up } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + // The reader: its owner, the list's word, the place. + const reader = Trail.readerOf(p, p.panes[held].?).?; + var t = &p.panes[reader].?.trail; + try std.testing.expectEqual(@as(u8, 3), t.n); + try std.testing.expect(t.steps[1] == .list); + try std.testing.expectEqualStrings("Refs 1/3 •1", t.steps[1].list.text.slice()); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), reader), "‹@p") != null); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), reader), " › Refs 1/3 •1 › ") != null); + // n: the next row, in place of the step (another file: a pane in its place). + p.view().active = reader; + p.update(.{ .key = .{ .cp = 'n', .text = "n" } }); + const r2 = Trail.readerOf(p, p.panes[held].?).?; + try std.testing.expectEqualStrings(b_path, p.panes[r2].?.file.?.path); + t = &p.panes[r2].?.trail; + try std.testing.expectEqual(@as(u8, 3), t.n); + try std.testing.expectEqualStrings("Refs 2/3 •1", t.steps[1].list.text.slice()); + p.update(.{ .key = .{ .cp = 'N', .text = "N", .shift = true } }); + const r3 = Trail.readerOf(p, p.panes[held].?).?; + try std.testing.expectEqualStrings(a_path, p.panes[r3].?.file.?.path); + try std.testing.expectEqual(@as(i32, 1), p.panes[r3].?.body.cur_row); + // Look on the word: the list again, in the reader's tag, as it was. + Trail.lookStep(p, r3, 1); + try std.testing.expect(live(p) == pk); + try std.testing.expectEqual(@as(?usize, r3), TagState.hostOf(p)); + try std.testing.expectEqual(@as(u16, 0), p.view().tag_state.rows.at); + try std.testing.expectEqual(@as(usize, 1), pk.marks.items.len); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Exec on it: a new picker of the same source. + Trail.execStep(p, r3, 1); + try std.testing.expect(live(p) != null and live(p) != pk); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Resume: the last one. + exec.runBuiltin(p, .Resume, held, "", null); + try std.testing.expect(live(p) != null); + p.update(.{ .key = .{ .cp = Key.escape } }); +} + test "a picker from a reading pane unrolls in that pane, its own text set aside while it previews and back on Esc" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); defer p.deinit(); diff --git a/core/Trail.zig b/core/Trail.zig index 7166d810..b88215d9 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -162,6 +162,32 @@ pub fn go(p: *Pardes, id: usize, i: u8) !usize { return show(p, id, s.place.path.slice(), .{ .line = s.place.line, .col = s.place.col }); } +/// A list the pane's newest step came through (§6.2: a picker's Enter, +/// Pick.zig): its word before the step's place. +pub fn through(t: *Trail, s: Step) void { + if (t.cur >= t.n or t.steps[t.cur] != .place) return; + if (t.n == max_steps) { + const first: usize = @intFromBool(t.steps[0] == .owner); + if (t.cur <= first) return; + std.mem.copyForwards(Step, t.steps[first .. max_steps - 1], t.steps[first + 1 ..]); + t.n -= 1; + t.cur -= 1; + } + std.mem.copyBackwards(Step, t.steps[t.cur + 1 .. t.n + 1], t.steps[t.cur..t.n]); + t.steps[t.cur] = s; + t.n += 1; + t.cur += 1; +} + +/// Reading pane `id`'s current step replaced by `path` at `at`, no step +/// pushed (a list's `n`, §5.6); its slot after it, as `step`'s. +pub fn replaceStep(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { + const to = placeOf(path, at.line, at.col) orelse return error.NameTooLong; + const t = &p.panes[id].?.trail; + if (t.cur < t.n) t.steps[t.cur] = to; + return show(p, id, path, at); +} + /// Pane `id` showing `path` at `at`, the keyboard on it: its caret moved /// in the file it shows, or a new pane on `path` in its place, its walk /// with it. A line past the new file's end is a miss that changes nothing. @@ -248,10 +274,11 @@ pub const Part = enum { back, forward }; fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 { const s = t.steps[i]; if (s != .owner and i < t.cur and t.cur - i > shown_steps) return "·"; - return switch (s) { + // The step's own bytes, not a copy's (a copy is gone at the return). + return switch (t.steps[i]) { .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits .place => |at| std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(at.path.slice()), at.line }) catch unreachable, // a basename and a u32 fit - .list, .search, .recipe => |w| w.text.slice(), + .list, .search, .recipe => |*w| w.text.slice(), }; } @@ -302,8 +329,9 @@ pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { } /// Look on step `i` of pane `id`'s trail (§6.3): a place is gone back or -/// forward to, the owner focused; a list, a search or a recipe is the -/// pickers' and the prompts' to take (not yet: said). +/// forward to, the owner focused, a list unrolled again as it was +/// (Pick.reopen); a search or a recipe is the prompts' to take (not yet: +/// said). pub fn lookStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { @@ -311,7 +339,8 @@ pub fn lookStep(p: *Pardes, id: usize, i: u8) void { .owner => |serial| if (p.paneBySerial(serial)) |owner| { p.view().active = owner; } else p.reportFailure(id, "walk: its owner is closed"), - .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not walked yet"), + .list => |w| pardes.Pick.reopen(p, id, w.index), + .search, .recipe => p.setMessage(.at(id), "walk: a search or a recipe is not walked yet"), } } @@ -321,7 +350,8 @@ pub fn execStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { .place, .owner => lookStep(p, id, i), - .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not run again yet"), + .list => |w| pardes.Pick.rerun(p, id, w.index), + .search, .recipe => p.setMessage(.at(id), "walk: a search or a recipe is not run again yet"), } } diff --git a/core/builtins.zig b/core/builtins.zig index 12ab9da5..589915c2 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1998,6 +1998,16 @@ pub const Forward = struct { } }; +/// Reopen the last picker as it was: its rows, marks, cursor and query. +/// +/// In the tag of the pane its Enter lands in from this one: this pane if it +/// reads, else its reader. `SPC '`. +pub const Resume = struct { + pub fn run(c: Ctx) void { + pardes.Pick.resumeFrom(c.p, c.id); + } +}; + /// Keep this pane: a reading pane, which a jump steps, is held from now on. /// /// Its walk leaves its tag (its places, its owner): the next jump from its diff --git a/core/config.zig b/core/config.zig index 6adebe1b..c924796a 100644 --- a/core/config.zig +++ b/core/config.zig @@ -145,6 +145,7 @@ pub const leader_path = paths: { .Back = "jo", .Forward = "ji", .Keep = "jk", + .Resume = "'", .Last = "jj", .Jumplist = "jl", .History = "fh", diff --git a/core/normal.zig b/core/normal.zig index 1f7ce1a7..2ec8a4da 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -1379,6 +1379,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! return look.lookWalk(p, if (a.role == .search_next) 1 else -1, false) else if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) return pane.places.step(p, pane, a.role == .search_next, a.count, t.select) + else if (t.what == .body and panes.Pick.walkList(p, v.active, a.role == .search_next)) + return else return searchStep(p.editor(), t, text, a.role == .search_next, a.count), .goto_word => return startJump(p, pane, t), |
