diff options
Diffstat (limited to 'core/Prompt.zig')
| -rw-r--r-- | core/Prompt.zig | 47 |
1 files changed, 34 insertions, 13 deletions
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,8 +806,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 +817,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 +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); }; |
