From 3ace8b385e02a54830decb6aa48d1fa28d076a18 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 00:04:32 -0300 Subject: Trail, Pick, Prompt, Jump: Ctrl-o pops the walk one word at a time (UX DESIGN L'1, binding; the user: "I did spc f f then did /something but I didn't know how to get back to the previous one", "the pickers are kinda shown as a stack right? I felt like I could pop the current one"): the trail's words, left to right, are the stack, and Ctrl-o steps one word left of where the pane is, Ctrl-i one right; onto a place as before (Trail.go); onto a list's word, that kept picker unrolls again in this pane's tag as it was left (query, cursor, marks), previewing here (Pick.reopen with `walking`, its step): Esc gives rest exactly as before the Ctrl-o, Enter lands as that word's (a step from it, the steps after it dropped, its word saying where it is now), Ctrl-o, Ctrl-i and Backspace on its empty field (after its refinements) roll it up unlanded and walk on (Pick.walkOn; past the last word the jump list's step alone, Jump.listBy); Ctrl-i in it is Tab, a mark, where the terminal cannot tell them apart; onto a search's word, the caret back where the search started and its places lit (Prompt.light, Look's without the select): `Step.search` is a `Search` that holds its start, path, line and column, recorded as `/` commits (Prompt.word from Places.origin) and by `*`; the pane is then at that word (`cur` on it: Trail.backs writes it before the path, n and N walk it, keep keeps it, a step from it keeps it, go and replaceStep never write a place over it); a held pane walks its own words, a search's and a recipe's; a list no longer kept is said (`walk: that list is gone: the last 8 are kept`) and passed by; the owner is no word to walk; Jumps new-pane walks nothing, as before; D8's fold takes places only: each run of places older than the newest `whole` back steps (or past the first two forward) is one `+N` where it starts, a list's word folds only where the tag keeps its newest step alone and never the newest list's, a search's or a recipe's never (Trail.folds; draw.zig dims every run's `+N`); after a landing (Pick.listed) and a search's Enter (Prompt.land) the notice row says, as an echo never logged, what the next Ctrl-o reaches, `C-o back to Find words 1/1` (Trail.sayBack); THE CAUSE of the user's trouble (UX L1): Trail.walkBy skipped every word but a place (`if (t.steps[i] != .place) continue;`), so Ctrl-o after the search went to main.zig:1, past both the search and the list, and nothing said which key would pop; RUN (TEST FIRST): Jump.zig "Ctrl-o pops the walk one word at a time (UX L'1) ..." (W2's test extended) types the user's keys in an 84x20 session of main.zig (SPC f f words, Enter, /in_word, Enter, then Ctrl-o x3 and Ctrl-i x3) and asks after each key the file, the trail's four steps and where the pane is among them, the caret's line, Find words unrolled or not and the search lit or not, then Esc from a reopened list (as before the Ctrl-o), its Enter (the search's word and the place after it dropped, `Find words 1/1` kept) and Backspace on its empty field (walks on to main.zig): fails on the base at Jump.zig:565 on the first Ctrl-o (expected words.zig, found main.zig), passes now; Trail.zig "the fold takes places alone (UX L'1) ..." (runs of places each a `+N`, a click on each its newest, two lists with the newest step alone, a search's word never) and "a held pane walks its own words (UX L'1) ..." (Ctrl-o puts the caret at 20:3 where `/line 9` began, its places lit; past it the jump list's); the notice-row test of walk UI G 2e now reads the echo, then the caret's place once it is retired; a user hit it on every Ctrl-o after a picker's Enter or a search; goldens re-recorded: find and fsearch (the notice row after a picker's Enter and a search's Enter reads the echo); ttyhelp is the merge's placeholder (M1 re-records it); the perf cell walk-pop (test/perf.zig, run only when named: Ctrl-o onto a list word reopening it in the key's own frame, then Esc), 326 us a pair on small in ReleaseFast; core-test 1124/1298 (173 skipped; the G 2e notice-row test, failing on the echo, updated and passing alone), shell-test 1346/1349 (3 skipped), behaviour-test 280/280, pane-test 183/184, process-test 3 of 147 failed before the two goldens moved (find, fsearch: the echo; ttyhelp the placeholder); load 2.2 to 7.2 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- core/Prompt.zig | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'core/Prompt.zig') diff --git a/core/Prompt.zig b/core/Prompt.zig index c54bb228..afe9f78e 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -445,8 +445,10 @@ fn land(p: *Pardes, id: usize, pane: *Pane, op: Op, pat: []const u8, bad: bool) p.registers.put(p.gpa, '/', pat, 0, 1, true) catch |err| return p.reportError(.at(id), "search", err); p.registers.last_search = '/'; set.provenance = .search; + const from = set.origin(pane); set.forget(p.gpa); - word(pane, if (op == .search_back) '?' else '/', pat); + word(p, pane, if (op == .search_back) '?' else '/', pat, from); + Trail.sayBack(p, id); } /// Alt-Enter in `/ ?` or `s S K A-K` (SPEC §7.2): the result kept as a @@ -688,36 +690,44 @@ pub fn execRecipe(p: *Pardes, id: usize) void { /// Pane `pane`'s search word, `/re/ 3/7`: one a pane, its `index` the /// length of its `/re/` with bit 16 set after n or N went round an end. -fn wordOf(pane: *Pane) ?*Trail.Word { +fn wordOf(pane: *Pane) ?*Trail.Search { for (pane.trail.steps[0..pane.trail.n]) |*step| if (step.* == .search) return &step.search; return null; } -fn patternOf(w: *const Trail.Word) []const u8 { +fn patternOf(w: *const Trail.Search) []const u8 { const end = patternEnd(w); return if (end < 2) "" else w.text.slice()[1 .. end - 1]; } /// Where a search word's `/re/` ends: held to its text (a damaged image's /// says anything). -fn patternEnd(w: *const Trail.Word) u16 { +fn patternEnd(w: *const Trail.Search) u16 { return std.math.clamp(@as(u16, @truncate(w.index)), @min(2, w.text.len), w.text.len); } /// Whether the places the pane's body shows are its search word's. -fn lights(pane: *const Pane, w: *const Trail.Word) bool { +fn lights(pane: *const Pane, w: *const Trail.Search) bool { const set = &pane.places; return set.shows(pane) and set.provenance == .search and !set.literal and std.mem.eql(u8, set.pattern, patternOf(w)); } /// `pat`, delimited by `delim`, as pane `pane`'s one search word, in place -/// of the one it had, written for where its places stand. -fn word(pane: *Pane, delim: u8, pat: []const u8) void { +/// of the one it had, written for where its places stand, its start the +/// byte `from` of its text (UX L'1: a walk back onto it puts the caret +/// there). +fn word(p: *Pardes, pane: *Pane, delim: u8, pat: []const u8, from: usize) void { // Its pattern and its place in the trail's room, else none. if (pat.len + 2 + 32 > Trail.max_text) return; - var w: Trail.Word = .{}; + const f = &pane.file.?; + var w: Trail.Search = .{}; _ = w.text.print("{c}{s}{c}", .{ delim, pat, delim }); w.index = w.text.len; + if (f.path.len <= Trail.max_text) if (File.lineIndex(p.gpa, f)) |index| { + const line = Places.lineOf(index, @min(from, f.content.len)); + w.from = .{ .line = @intCast(line + 1), .col = @intCast(@min(from, f.content.len) - File.lineStart(p.gpa, f, line) + 1) }; + w.from.path.set(f.path); + } else |_| {}; pane.trail.setWord(.{ .search = w }); refresh(pane, false); } @@ -796,13 +806,27 @@ pub fn stepped(p: *Pardes, id: usize, forward: bool, before: ?usize) void { /// `*`: pattern `pat`, the selection's (in `/` already), lights its places /// in `pane`'s body and is its search word, no prompt asked (SPEC §7.3). pub fn star(p: *Pardes, pane: *Pane, pat: []const u8) void { + const sel = pane.body.primaryRange(pane.file.?.content, 0); pane.places.search(p, pane, .search, 0, pat, false, null, false); - word(pane, '/', pat); + word(p, pane, '/', pat, @min(sel.anchor, sel.head)); } /// Look on pane `id`'s search word (SPEC §6.3): its places lit again and /// the one it was on selected. pub fn lookWord(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const w = wordOf(pane) orelse return; + const set = &pane.places; + light(p, id); + const rest = w.text.slice()[patternEnd(w)..]; + const n = std.fmt.parseInt(usize, std.mem.trim(u8, rest[0 .. std.mem.indexOfScalar(u8, rest, '/') orelse rest.len], " "), 10) catch return; + if (n > 0 and n <= set.hits.items.len) set.goTo(p.settings.tab, pane, n - 1, false); +} + +/// Pane `id`'s search word's places lit again, `/` its pattern, the caret +/// where it is: a Look on the word, a walk onto it (Trail.walkFrom, the +/// caret where the search started, UX L'1). +pub fn light(p: *Pardes, id: usize) void { const pane = p.panes[id] orelse return; const w = wordOf(pane) orelse return; const set = &pane.places; @@ -812,9 +836,6 @@ pub fn lookWord(p: *Pardes, id: usize) void { set.search(p, pane, .search, 0, pat, false, null, false); set.fill(p, pane, std.math.maxInt(usize), regexp.Regex.budget); p.view().active = id; - const rest = w.text.slice()[patternEnd(w)..]; - const n = std.fmt.parseInt(usize, std.mem.trim(u8, rest[0 .. std.mem.indexOfScalar(u8, rest, '/') orelse rest.len], " "), 10) catch return; - if (n > 0 and n <= set.hits.items.len) set.goTo(p.settings.tab, pane, n - 1, false); } /// Exec on it: searched again from the caret, as its prompt and Enter @@ -841,7 +862,7 @@ pub fn unlit(p: *const Pardes, pane: *Pane) ?[2]usize { if (Trail.outOfRest(p, pane)) return null; const t = &pane.trail; const whole = pardes.tagline.folded(p, pane).whole; - for (t.steps[0..t.cur], 0..) |*step, i| if (step.* == .search) { + for (t.steps[0..t.backs()], 0..) |*step, i| if (step.* == .search) { if (lights(pane, &step.search) or t.isFolded(i, whole)) return null; return t.spanOf(i, whole); }; -- cgit v1.3 From 1ce3bcb6178b720058518a12e45f25769be7d39c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 01:14:33 -0300 Subject: Pick, Prompt, keys: Ctrl-r in a picker unrolls its source's earlier queries (UX DESIGN L2's last line, "Small, any of them: `C-r` in a picker recalls that source's earlier queries, as prompts do", in the L options the user liked; the coordinator's item 4): a history per source (Find, Grep, Recent, Panes, Symbols, All, ...; Lists keeps none), kept as the prompts' are, beside them (`Prompt.History.pick`, a `Ring` per source: the last 16 lines of a field's room, an older copy of a line gone, the same dump and digest walk, held to its count), a query kept as its picker rolls up with it typed, which a landing does too (Pick.rollUp); Ctrl-r in a picker is a band step like any other (the spine's `history`, beside the rows): its lines newest first, those holding the field, a cursor on one; Down and Up (C-n, C-p) move that cursor, Enter puts its line in the field and the picker filters again, Ctrl-r rolls it up and so does Esc, back to the picker (Pick.closeHistory, asked by keys.tagStateKey before its Esc rolls the tag to rest); anything else types in the field, which filters both; out of it Up and Down are the rows' cursor, no recall; Prompt's history step, its cursor and its rows take the ring, the field and the cursor as arguments now (historyStep, historyAt, historyRows, historyKey: Down and Up's walk, once in Prompt.key), one for the prompts and the pickers; the reference's "Back to a picker" says it; RUN (TEST FIRST): Pick.zig "Ctrl-r in a picker unrolls its source's earlier queries ..." types SPC f f `words` Enter, a Grep left with `in_w`, then SPC f f Ctrl-r and asks the band's history rows (`words` alone: Grep's is not Find's), Esc (the band rolled up, the picker live), Ctrl-r Enter (`words` in the field, the rows one), Grep's Ctrl-r (`in_w`), a Lists left typed (nothing kept), then seventeen Find queries (16 kept, q1 the oldest, q16 the newest) and Ctrl-r over the field `q1` (q16 .. q10, q1, no `words`): on L'3's tree the first Ctrl-r shows no history (Pick.zig:3196, expected `words`), passes now; a user hit it on every picker whose earlier query they wanted again; docs/typ/builtins-gui.json regenerated here for L'3's Lists and Resume's doc (the window build's list); with L'1 to L'3 under it: core-test 1128/1301 (173 skipped), shell-test 1349/1352 (3 skipped), pane-test 183/184, behaviour-test 280/280 and --replay 280/280, process-test 1 of 147 failed (ttyhelp, the merge's placeholder), term-test 17/17, kitty-test 45/45, the gui unit-test 1952/1956 (4 skipped; its perf check's BaselineHarnessMismatch, every base's); the dump fuzz (Prompt.History.pick) at 40,000 join seeds twice and 1,779 image seeds 13/13; the soak replicas --clients 5 --rtt 30,80,207: diverged 0, unequal 0, quiet checks 492/0 457/0 491/0, stops not on may_wait 0, the corpus 22 of 22 scenes the same, no leak; no frame or perf gate (the user's rule of 2026-11-26: at the programme's end); load 3.4 to 14.0 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- core/Pick.zig | 141 +++++++++++++++++++++++++++++++++++++++++++-- core/Prompt.zig | 95 ++++++++++++++++-------------- core/keys.zig | 1 + docs/typ/builtins-gui.json | 3 +- docs/typ/reference.typ | 6 +- 5 files changed, 197 insertions(+), 49 deletions(-) (limited to 'core/Prompt.zig') diff --git a/core/Pick.zig b/core/Pick.zig index 0c6b1c68..1d332f25 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -140,9 +140,12 @@ marks: std.ArrayList(u32) = .empty, refined: [max_refine]Refined = undefined, nrefined: u8 = 0, /// The band's second step: the sources to switch to (M-h), the acts on -/// the picked rows (M-l, M-Enter); `edit`: the field is the act Edit's -/// command, the query kept in `query`. -spine: enum(u8) { rows, sources, acts, edit } = .rows, +/// the picked rows (M-l, M-Enter), its source's earlier queries (C-r); +/// `edit`: the field is the act Edit's command, the query kept in `query`. +spine: enum(u8) { rows, sources, acts, edit, history } = .rows, +/// The line of its source's history the cursor of it unrolled is on +/// (Prompt.historyAt: 0 the newest holding the field). +recall: u8 = 0, /// The act step's cursor, among the acts offered. act_at: u8 = 0, /// The places Walk queued (C-i steps them in the landing pane): each a @@ -805,6 +808,7 @@ pub fn bandRows(p: *const Pardes) u16 { for (acts) |act| n += @intFromBool(offered(pk, act)); break :acts @max(rows, n); }, + .history => @max(rows, pardes.Prompt.historyRows(p.prompt_history.pick.getPtrConst(pk.source), p.view().tag_state.field.text())), .rows, .edit => rows, }); } @@ -814,7 +818,8 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const pk = live(p) orelse return &.{}; const s = &p.view().tag_state; const n = count(p, pk); - const out = try arena.alloc(Band.Step, if (pk.spine == .sources or pk.spine == .acts or pk.source == .lists) 2 else 1); + const out = try arena.alloc(Band.Step, if (pk.spine == .sources or pk.spine == .acts or pk.spine == .history or pk.source == .lists) 2 else 1); + if (pk.spine == .history) _ = try pardes.Prompt.historyStep(arena, p.prompt_history.pick.getPtrConst(pk.source), s.field.text(), pk.recall, &out[1]); if (pk.spine == .acts) out[1] = try actStep(p, pk, arena); if (pk.source == .lists) out[1] = try listedStep(p, pk, arena); if (pk.spine == .sources) { @@ -1050,7 +1055,10 @@ pub fn rollUp(p: *Pardes) void { const pk = live(p) orelse return; const s = &p.view().tag_state; if (pk.spine != .edit) pk.query.set(s.field.text()); + // Its query its source's history's (UX L2's C-r): Lists keeps none. + if (pk.source != .lists) p.prompt_history.pick.getPtr(pk.source).add(pk.query.slice()); pk.spine = .rows; + pk.recall = 0; pk.at = s.rows.at; pk.unrolled = false; pk.walking = null; @@ -1216,6 +1224,29 @@ pub fn key(p: *Pardes, k: Key) void { return; } if (pk.spine == .acts) return actKey(p, pk, k); + // Its source's earlier queries unrolled (C-r, UX L2): Down and Up + // move their cursor, Enter puts the one under it in the field, C-r + // and Esc (closeHistory) roll them up; anything else is the field's, + // which filters both. + if (pk.spine == .history) { + const r = p.prompt_history.pick.getPtr(pk.source); + if (pardes.Prompt.historyKey(r, s.field.text(), &pk.recall, k)) return; + if (plain and k.cp == Key.enter) { + const at = pardes.Prompt.historyAt(r, s.field.text(), pk.recall); + pk.spine = .rows; + pk.recall = 0; + if (at == 0) return; + s.field.set(r.get(at).?); + return filter(p, pk); + } + if (ctrl and k.cp == 'r') { + pk.spine = .rows; + return; + } + pk.recall = 0; + if (s.field.key(k)) filter(p, pk); + return; + } // Themes: Enter wears the one under the cursor; actions: Enter, or an // action's digit with the field empty, applies it; the rows and the // field. @@ -1273,6 +1304,11 @@ pub fn key(p: *Pardes, k: Key) void { } if (ctrl and (k.cp == ' ' or k.cp == '@')) return refine(p, pk, false); if (ctrl and k.cp == 'q') return keep(p, pk); + if (ctrl and k.cp == 'r') { + pk.spine = .history; + pk.recall = 0; + return; + } if (alt and k.cp == 'p') { pk.previews = !pk.previews; pk.shown = no_row; @@ -1288,6 +1324,16 @@ pub fn key(p: *Pardes, k: Key) void { if (s.field.key(k)) filter(p, pk); } +/// Esc with the live picker's history unrolled (keys.tagStateKey): that +/// rolled up alone, the picker as it was; false with none. +pub fn closeHistory(p: *Pardes) bool { + const pk = live(p) orelse return false; + if (pk.spine != .history) return false; + pk.spine = .rows; + pk.recall = 0; + return true; +} + /// The source spine (M-h): a key a source, the query kept. const spine_rows = [_]struct { key: u8, word: []const u8, source: ?Source }{ .{ .key = 'f', .word = "Find", .source = .find }, @@ -3150,3 +3196,90 @@ test "Lists (UX L'3): a picker of the kept pickers, newest first, each its list try std.testing.expectEqualStrings("J 1/2", (try rowAt(p, live(p).?, a, 0)).text); p.update(.{ .key = .{ .cp = Key.escape } }); } + +test "Ctrl-r in a picker unrolls its source's earlier queries (UX L2's last line): recorded as a picker lands and as it is left with its field typed; the band's rows filtered by the field, Enter puts one in the field, Esc closes the band; each source its own; Lists none; as many as a prompt's history keeps" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + p.stream.engine = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const w_path = try testFile(p, &tmp, "words.zig", "in_word\n"); + defer p.gpa.free(w_path); + const base = try p.setTestFile("text\n"); + p.gpa.free(base.file.?.path); + base.file.?.path = try testFile(p, &tmp, "main.zig", "text\n"); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const History = struct { + /// The history band's rows, joined by `|`; "" with none shown. + fn rows(pp: *Pardes, a: std.mem.Allocator) ![]const u8 { + const band = try steps(pp, a); + if (band.len < 2) return ""; + var out: std.ArrayList(u8) = .empty; + for (band[1].rows, 0..) |row, i| try out.print(a, "{s}{s}", .{ if (i > 0) "|" else "", row.text }); + return out.items; + } + }; + const ctrl_r: Key = .{ .cp = 'r', .ctrl = true }; + const Type = struct { + fn keys(pp: *Pardes, text: []const u8) void { + for (text) |c| pp.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + } + }; + // SPC f f words, Enter: `words` is Find's. + Type.keys(p, " ffwords"); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter } }); + p.sync(); + try std.testing.expect(live(p) == null); + // A Grep left with its field typed: `in_w` is Grep's. + Type.keys(p, " fgin_w"); + p.update(.{ .key = .{ .cp = Key.escape } }); + // SPC f f, Ctrl-r: Find's alone. + Type.keys(p, " ff"); + p.update(.{ .key = ctrl_r }); + try std.testing.expectEqualStrings("words", try History.rows(p, frame.allocator())); + // Esc closes the band, back to the picker; Ctrl-r again, Enter: the + // line in the field, the rows filtered by it. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(live(p) != null); + try std.testing.expectEqualStrings("", try History.rows(p, frame.allocator())); + p.update(.{ .key = ctrl_r }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(live(p) != null); + try std.testing.expectEqualStrings("words", p.view().tag_state.field.text()); + try std.testing.expectEqualStrings("", try History.rows(p, frame.allocator())); + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, live(p).?)); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Grep's: its own line. + Type.keys(p, " fg"); + p.update(.{ .key = ctrl_r }); + try std.testing.expectEqualStrings("in_w", try History.rows(p, frame.allocator())); + p.update(.{ .key = .{ .cp = Key.escape } }); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Lists keeps none: left with its field typed, nothing recorded. + Type.keys(p, " flzz"); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(u8, 0), p.prompt_history.pick.get(.lists).n); + // The bound: a prompt's history's, the oldest gone, newest first; the + // rows filtered by the field. + for (0..pardes.Prompt.Ring.max + 1) |i| { + Type.keys(p, " ff"); + var q: [8]u8 = undefined; + Type.keys(p, try std.fmt.bufPrint(&q, "q{d}", .{i})); + p.update(.{ .key = .{ .cp = Key.escape } }); + } + const find = p.prompt_history.pick.get(.find); + try std.testing.expectEqual(@as(u8, pardes.Prompt.Ring.max), find.n); + try std.testing.expectEqualStrings("q16", find.get(1).?); + try std.testing.expectEqualStrings("q1", find.get(pardes.Prompt.Ring.max).?); + Type.keys(p, " ffq1"); + p.update(.{ .key = ctrl_r }); + const shown = try History.rows(p, frame.allocator()); + try std.testing.expect(std.mem.startsWith(u8, shown, "q16|q15|q14|q13|q12|q11|q10|q1")); + try std.testing.expect(std.mem.indexOf(u8, shown, "words") == null); + p.update(.{ .key = .{ .cp = Key.escape } }); + p.update(.{ .key = .{ .cp = Key.escape } }); +} diff --git a/core/Prompt.zig b/core/Prompt.zig index afe9f78e..b76b2d9a 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -78,11 +78,14 @@ refused: bool = false, /// `:`'s (Command.zig): what its rows are of, and where Tab writes. command: Command = .{}, -/// The session's prompt histories: the lines Enter took, newest last. +/// The session's prompt histories: the lines Enter took, newest last; and +/// each picker source's queries, as its picker landed or was left typed +/// (Pick.zig, UX L2). pub const History = struct { search: Ring = .{}, pipe: Ring = .{}, command: Ring = .{}, + pick: std.EnumArray(pardes.Pick.Source, Ring) = .initFill(.{}), }; pub const Ring = struct { @@ -262,25 +265,9 @@ pub fn key(p: *Pardes, k: Key) void { // Up and C-p move its cursor to the next line it shows, older or newer, // the field kept as typed; Enter takes the line under it. if (s.prompt.history) { - const down = (plain and k.cp == Key.down) or (ctrl and k.cp == 'n'); - const up = (plain and k.cp == Key.up) or (ctrl and k.cp == 'p'); const r = ring(p, s.prompt.op); - const at = historyAt(p); - if (down or up) { - var newer: u8 = 0; - var older: u8 = 0; - var rows: usize = 0; - var back: u8 = 1; - while (back <= r.n and rows < Band.Cursor.shown) : (back += 1) { - if (std.mem.indexOf(u8, r.get(back).?, s.field.text()) == null) continue; - rows += 1; - if (back < at) newer = back; - if (back > at and older == 0) older = back; - } - const to = if (down) older else newer; - if (to != 0) s.prompt.recall = to; - return; - } + if (historyKey(r, s.field.text(), &s.prompt.recall, k)) return; + const at = historyAt(r, s.field.text(), s.prompt.recall); if (plain and k.cp == Key.enter and at != 0) { s.field.set(r.get(at).?); live(p, pane); @@ -1261,11 +1248,12 @@ fn plural(n: usize) []const u8 { /// lines the field is in, eleven at most. pub fn takes(p: *Pardes) u16 { const s = &p.view().tag_state; - if (s.prompt.op == .command) return @max(Command.takes(p), if (s.prompt.history) historyRows(p) else 0); + const rows = historyRows(ring(p, s.prompt.op), s.field.text()); + if (s.prompt.op == .command) return @max(Command.takes(p), if (s.prompt.history) rows else 0); if (s.prompt.op == .pipe_to) if (p.panes[TagState.hostOf(p) orelse return 0]) |pane| if (answered(p, pane, s.field.text())) |l| - return @max(runRows(l), if (s.prompt.history) historyRows(p) else 0); + return @max(runRows(l), if (s.prompt.history) rows else 0); if (!s.prompt.history) return 0; - return historyRows(p); + return rows; } /// `A-|`'s runs as rows: each `#1 exit 0`, then its output's lines. @@ -1275,12 +1263,12 @@ fn runRows(l: *const Pipe.PendingPipe) u16 { return @intCast(@min(Band.Cursor.shown, n)); } -fn historyRows(p: *Pardes) u16 { - const s = &p.view().tag_state; +/// The rows history `r` unrolled takes: its lines holding `q`, one at +/// least, eleven at most. +pub fn historyRows(r: *const Ring, q: []const u8) u16 { var n: u16 = 0; - const r = ring(p, s.prompt.op); for (r.lines[0..r.n]) |l| { - if (std.mem.indexOf(u8, l.slice(), s.field.text()) != null) n += 1; + if (std.mem.indexOf(u8, l.slice(), q) != null) n += 1; } return @max(1, @min(Band.Cursor.shown, n)); } @@ -1292,8 +1280,9 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St var made: usize = 0; // `:`'s history before its rows and their doc, which takes the width // left after the others. + const r = ring(p, s.prompt.op); if (s.prompt.op == .command) { - if (s.prompt.history) made = try historyStep(p, arena, &out[0]); + if (s.prompt.history) made = try historyStep(arena, r, s.field.text(), s.prompt.recall, &out[0]); return out[0 .. made + try Command.steps(p, arena, out[made..])]; } if (s.prompt.op == .pipe_to) if (p.panes[TagState.hostOf(p) orelse return out[0..0]]) |pane| if (answered(p, pane, s.field.text())) |l| { @@ -1312,35 +1301,55 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St made += 1; }; if (!s.prompt.history) return out[0..made]; - return out[0 .. made + try historyStep(p, arena, &out[made])]; + return out[0 .. made + try historyStep(arena, r, s.field.text(), s.prompt.recall, &out[made])]; } -/// The line of its history the cursor of the history unrolled is on: the -/// one the keys moved to (`recall`), else the newest holding the field; -/// 0 with none. -fn historyAt(p: *Pardes) u8 { - const s = &p.view().tag_state; - if (s.prompt.recall != 0) return s.prompt.recall; - const r = ring(p, s.prompt.op); +/// The line of history `r` the cursor of it unrolled is on: the one the +/// keys moved to (`recall`), else the newest holding the field `q`; 0 +/// with none. +pub fn historyAt(r: *const Ring, q: []const u8, recall: u8) u8 { + if (recall != 0) return recall; var back: u8 = 1; while (back <= r.n) : (back += 1) { - if (std.mem.indexOf(u8, r.get(back).?, s.field.text()) != null) return back; + if (std.mem.indexOf(u8, r.get(back).?, q) != null) return back; } return 0; } +/// Down and Ctrl-n, Up and Ctrl-p in history `r` unrolled (UX D5: a list +/// like any other): its cursor `recall` to the next line it shows, older +/// or newer, the field `q` kept as typed; true when the key was one. +pub fn historyKey(r: *const Ring, q: []const u8, recall: *u8, k: Key) bool { + const plain = !k.ctrl and !k.alt; + const ctrl = k.ctrl and !k.alt; + const down = (plain and k.cp == Key.down) or (ctrl and k.cp == 'n'); + const up = (plain and k.cp == Key.up) or (ctrl and k.cp == 'p'); + if (!down and !up) return false; + const at = historyAt(r, q, recall.*); + var newer: u8 = 0; + var older: u8 = 0; + var rows: usize = 0; + var back: u8 = 1; + while (back <= r.n and rows < Band.Cursor.shown) : (back += 1) { + if (std.mem.indexOf(u8, r.get(back).?, q) == null) continue; + rows += 1; + if (back < at) newer = back; + if (back > at and older == 0) older = back; + } + const to = if (down) older else newer; + if (to != 0) recall.* = to; + return true; +} + /// A history step's row's index: its line's place in the ring with this /// bit, which a click tells from the other steps' rows (Command.point). pub const history_row: u16 = 0x8000; -/// The history step (Ctrl-r): its lines newest first, those holding the -/// field, its cursor on one. -fn historyStep(p: *Pardes, arena: std.mem.Allocator, out: *Band.Step) !usize { - const s = &p.view().tag_state; - const r = ring(p, s.prompt.op); +/// History `r`'s step (Ctrl-r): its lines newest first, those holding the +/// field `q`, its cursor on one. +pub fn historyStep(arena: std.mem.Allocator, r: *const Ring, q: []const u8, recall: u8, out: *Band.Step) !usize { var rows: std.ArrayList(Band.Row) = .empty; - const q = s.field.text(); - const at = historyAt(p); + const at = historyAt(r, q, recall); var back: u8 = 1; while (back <= r.n and rows.items.len < Band.Cursor.shown) : (back += 1) { const l = r.get(back).?; diff --git a/core/keys.zig b/core/keys.zig index ea25c2f7..803b2b3c 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -586,6 +586,7 @@ fn tagStateKey(p: *Pardes, key: Key) void { const v = p.view(); const s = &v.tag_state; if (s.kind == .prompt) return Prompt.key(p, key); + if (s.kind == .pick and hit(key, config.escape) and @import("Pick.zig").closeHistory(p)) return; if (TagState.hostOf(p) == null or hit(key, config.escape)) return TagState.toRest(p); switch (s.kind) { .rest, .prompt => unreachable, diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index 2243756d..7897295d 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -63,6 +63,7 @@ {"name": "Kill", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Stop the commands pardes started that are still running.", "text": "Stop the commands pardes started that are still running.\nThey are the lines pardes typed into a terminal (from an exec, a middle click, a pty/run) and its command panes. Bare, every one; `Kill make ls`, those whose line starts with one of the words. Each gets SIGTERM: a command pane's whole command, a terminal's foreground job, never its shell, which stays."}, {"name": "Last", "leader": "SPC j j", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Go back to the last place jumped from in another pane.", "text": "Go back to the last place jumped from in another pane.\nWith none, to the pane beside this one."}, {"name": "Left", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane, or column tag, to the left.", "text": "Move the keyboard to the pane, or column tag, to the left."}, + {"name": "Lists", "leader": "SPC f l", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Pick from the last pickers kept, newest first: Enter reopens one as it was left.", "text": "Pick from the last pickers kept, newest first: Enter reopens one as it was left.\nA row each: its source, query, cursor and marks, where it last landed at its right. The body previews its current row; beside the rows, its first rows. `SPC f l`, or `'` in a picker's sources (M-h)."}, {"name": "LocationsConfig", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Set how a list's location rows are drawn: `LocationsConfig layout:inline context:2`.", "text": "Set how a list's location rows are drawn: `LocationsConfig layout:inline context:2`.\nIts fields: `context:` around each, `tscontext:on|off`, `tslocations:on|off`, `layout:inline|stacked`; those not given stay. Bare, it shows them in a +LocationsConfig."}, {"name": "Look", "leader": null, "also": ["enter", "right-click"], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Look at its argument as B3 does: open the file or address it names, else find the word.", "text": "Look at its argument as B3 does: open the file or address it names, else find the word.\n`Look notes.txt:12` opens the file at line 12, or goes to the pane already showing it; an address may be a line, `:12:5`, `/re/`, a directory (a pane listing it) or a URL (the browser, by a plumbing rule). A relative path is found in the directory of the pane looked from, then of each pane on the jump list. Bare (Enter): the selection, else what is under the cursor."}, {"name": "LookNext", "leader": "SPC j n", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Select the next thing a Look would open: a place in a list, a word that names a file.", "text": "Select the next thing a Look would open: a place in a list, a word that names a file.\nThe walk goes on through the panes in order, the keyboard with it; Enter opens what it selected."}, @@ -92,7 +93,7 @@ {"name": "Rename", "leader": "SPC l r", "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Rename the symbol under the cursor to its argument, wherever the language server finds it.", "text": "Rename the symbol under the cursor to its argument, wherever the language server finds it.\nBare, it asks for the new name. Within this file it is done at once, saying how many places changed; one that reaches other files changes nothing and lists its edits in a +Search to look over."}, {"name": "Repl", "leader": null, "also": [], "arg": true, "scope": "pane", "setting": false, "choices": null, "doc": "Bind this terminal as a language's REPL: an exec on that language's files is typed into it.", "text": "Bind this terminal as a language's REPL: an exec on that language's files is typed into it.\n`Repl python` binds it (the language named as a code fence names it: `py`, `Python`, `sh`); `Repl -` unbinds it; bare, it says what it is bound as. An exec made on the body of a file in that language (a middle click, the execute key, a single word like `ls` as much as a selection), and one made on this terminal's own body, is typed into it (bracketed paste if its program asked for that, else line by line, then Enter) instead of run. With several bound for a language, the exec asks which.\nStill commands, whatever is bound: a word in a tag, `Exec `, a ctl line, and a command word @`cmd` in the text, looked at or clicked."}, {"name": "Restore", "leader": "SPC s r", "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Replace every pane with those of a dump.", "text": "Replace every pane with those of a dump.\n`Restore