summaryrefslogtreecommitdiff
path: root/core/Prompt.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Prompt.zig')
-rw-r--r--core/Prompt.zig142
1 files changed, 86 insertions, 56 deletions
diff --git a/core/Prompt.zig b/core/Prompt.zig
index c54bb228..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);
@@ -445,8 +432,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 +677,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,8 +793,9 @@ 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
@@ -806,15 +804,25 @@ 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;
const pat = patternOf(w);
p.registers.put(p.gpa, '/', pat, 0, 1, true) catch |err| return p.reportError(.at(id), "search", err);
p.registers.last_search = '/';
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 +849,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);
};
@@ -1240,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.
@@ -1254,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));
}
@@ -1271,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| {
@@ -1291,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).?;