diff options
Diffstat (limited to 'core/Pick.zig')
| -rw-r--r-- | core/Pick.zig | 205 |
1 files changed, 192 insertions, 13 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(); |
