//! A text prompt in a pane's tag (walk UI, pick-ui SPEC.md §7): `/ ?` //! search the body, `s S K A-K` select by a pattern, `| A-| ! A-! $` pipe //! through a command, typed in //! the head's field after the prompt's key cell, the plain word it stands //! for and its count beside it (`s self = Exec Edit x/self/ 4 selections in //! 6 lines`), its effect drawn live in the body on every key; Enter applies //! it and keeps the line in its history, Esc puts the body back. //! //! A sam prompt (`s S K A-K | A-|`, sam's `x y g v | >`) right after //! another of the same pane's, no key between, extends one recipe word in //! the pane's trail, the Edit line it is (`Edit x/self/ |tr a-z A-Z`): Look //! on it puts back the selections it made, Exec runs it as Edit (SPEC §7.5). //! //! A view's own (TagState.prompt, the coordinator's R4): its field the //! head's (TagState.field); its histories the session's, three of them //! (`/ ? s S K A-K` share one, the pipes another, `:` its own). A prompt for //! a tag's text or a header's still asks on the notice band (panes.zig //! startPrompt), as does Save's. const Prompt = @This(); const std = @import("std"); const pardes = @import("root.zig"); const config = @import("config.zig"); const modal = @import("modal.zig"); const panes = @import("panes.zig"); const normal = @import("normal.zig"); const edit = @import("edit.zig"); const regexp = @import("regexp.zig"); const tagline = @import("tagline.zig"); const Pipe = @import("Pipe.zig"); const Field = @import("Field.zig"); const Band = @import("Band.zig"); const TagState = @import("TagState.zig"); const Kind = @import("Kind.zig"); const Places = @import("Places.zig"); const Trail = @import("Trail.zig"); const colors = @import("colors.zig"); const Surface = @import("surface.zig").Surface; const File = panes.File; const Pardes = pardes.Pardes; const Pane = panes.Pane; const Text = panes.Text; const Key = pardes.Key; const hit = pardes.hit; pub const Op = enum(u8) { search, search_back, select, split, keep, remove, pipe, pipe_to, insert, append, keep_pipe, command }; op: Op = .select, /// Up and Down's place in its history: 0 the field as typed, n the n-th /// newest line. recall: u8 = 0, /// Ctrl-r: its history unrolled under the tag, filtered by the field. history: bool = false, /// The field is no regex: the body keeps the last result, dimmed, and the /// count says why. bad: bool = false, /// The ranges the last pattern found (0: none, the selections it started /// from back), and the lines those it started from cover. found: u16 = 0, scope: u32 = 0, /// `?` waits for its place: the last before where it began, once the text /// between them is searched. seeking: bool = false, /// The lines a search's places are on, counted once they are all found, at /// the places' `hits_gen` `counted`. lines: u32 = 0, counted: u32 = std.math.maxInt(u32), /// The pane, by serial, whose sam prompt Enter took last with no key since: /// the next one extends its recipe (0: none). chain: u32 = 0, /// The session's prompt histories: the lines Enter took, newest last. pub const History = struct { search: Ring = .{}, pipe: Ring = .{}, command: Ring = .{}, }; pub const Ring = struct { pub const replica = .{ .len = .{.{ "lines", "n" }} }; /// The lines a history keeps; past it the oldest goes. pub const max = 16; lines: [max]Kind.Buf(Field.max) = undefined, n: u8 = 0, /// `line` the newest, an older copy of it gone. pub fn add(r: *Ring, line: []const u8) void { if (line.len == 0) return; var w: u8 = 0; for (r.lines[0..r.n]) |l| if (!std.mem.eql(u8, l.slice(), line)) { r.lines[w] = l; w += 1; }; r.n = w; if (r.n == max) { std.mem.copyForwards(Kind.Buf(Field.max), r.lines[0 .. max - 1], r.lines[1..]); r.n -= 1; } r.lines[r.n].set(line); r.n += 1; } /// The `back`-th newest line, 1 the newest. pub fn get(r: *const Ring, back: u8) ?[]const u8 { if (back == 0 or back > r.n) return null; return r.lines[r.n - back].slice(); } }; const Info = struct { /// Its key cell. cell: []const u8, hint: []const u8, }; const info = std.EnumArray(Op, Info).init(.{ .search = .{ .cell = "/", .hint = "⏎ land · M-⏎ as a list · C-r history · esc" }, .search_back = .{ .cell = "?", .hint = "⏎ land · M-⏎ as a list · C-r history · esc" }, .select = .{ .cell = "s", .hint = "⏎ select · C-r history · esc" }, .split = .{ .cell = "S", .hint = "⏎ select · C-r history · esc" }, .keep = .{ .cell = "K", .hint = "⏎ keep · C-r history · esc" }, .remove = .{ .cell = "A-K", .hint = "⏎ keep · C-r history · esc" }, .pipe = .{ .cell = "|", .hint = "⏎ replace · C-r history · esc" }, .pipe_to = .{ .cell = "A-|", .hint = "⏎ run · C-r history · esc" }, .insert = .{ .cell = "!", .hint = "⏎ insert · C-r history · esc" }, .append = .{ .cell = "A-!", .hint = "⏎ append · C-r history · esc" }, .keep_pipe = .{ .cell = "$", .hint = "⏎ keep · C-r history · esc" }, .command = .{ .cell = ":", .hint = "⏎ run · C-r history · esc" }, }); /// The history `op` reads and Enter adds to. fn ring(p: *Pardes, op: Op) *Ring { return switch (op) { .search, .search_back, .select, .split, .keep, .remove => &p.prompt_history.search, .pipe, .pipe_to, .insert, .append, .keep_pipe => &p.prompt_history.pipe, .command => &p.prompt_history.command, }; } pub fn ofMode(m: modal.Normal.RegexMode) Op { return switch (m) { .select => .select, .split => .split, .keep => .keep, .remove => .remove, }; } fn mode(op: Op) ?modal.Normal.RegexMode { return switch (op) { .select => .select, .split => .split, .keep => .keep, .remove => .remove, else => null, }; } pub fn ofBehavior(how: modal.Normal.PipeBehavior) Op { return switch (how) { .replace => .pipe, .ignore => .pipe_to, .insert => .insert, .append => .append, .keep => .keep_pipe, }; } fn searches(op: Op) bool { return op == .search or op == .search_back; } fn behavior(op: Op) ?modal.Normal.PipeBehavior { return switch (op) { .pipe => .replace, .pipe_to => .ignore, .insert => .insert, .append => .append, .keep_pipe => .keep, else => null, }; } /// Prompt `op` in the tag of `pane`, the pane with the keyboard, for its /// body: its field empty, the body's selections as they are (`s S K A-K` /// select inside them, `/ ?` search from the primary one; Esc puts them /// back). `/ ?` search a file's body only. pub fn open(p: *Pardes, pane: *Pane, op: Op) void { // Nobody is at the keyboard to answer a command a script wrote to a // ctl file: the write fails instead (core/ninep/ctl.zig). if (p.fs.no_prompt) { p.fs.refused = true; p.fs.failures +%= 1; return; } const t = &pane.body; // A body is piped only when it is a file that saves. if (behavior(op) != null) { const f = pane.file orelse return; if (!panes.Output.fileTraits(f.output).saves) return; } var scope: u32 = 0; if (mode(op) != null) { const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(.at(p.view().active), "prompt", err); const got = t.ranges(text, 0, &pane.sel_snap); pane.nsel_snap = @intCast(got.n); pane.sel_snap_pri = @intCast(got.pri); pane.sel_snap_expl = t.vsel.active and t.vsel.explicit; for (pane.sel_snap[0..got.n]) |r| { const lo = @min(r.anchor, r.head, text.len); const hi = @min(@max(r.anchor, r.head), text.len); scope += @intCast(std.mem.count(u8, text[lo..hi -| @intFromBool(hi > lo and text[hi - 1] == '\n')], "\n") + 1); } } if (searches(op)) pane.places.remember(p, pane); pane.focus = .body; tagline.exitHeader(p); t.mode = .normal; t.normal.clear(); TagState.toRest(p); const s = &p.view().tag_state; s.kind = .prompt; s.host = pane.serial; s.field.clear(); s.rows = .{}; s.prompt = .{ .op = op, .scope = scope, .chain = s.prompt.chain }; } /// A key the tag at rest takes (keys.handleKey): any but one that opens a /// sam prompt ends the recipe Enter left open (a motion, an edit). pub fn keyAtRest(p: *Pardes, k: Key) void { const s = &p.view().tag_state; if (s.prompt.chain == 0) return; if (hit(k, config.select_regex) or hit(k, config.split_regex) or hit(k, config.keep_regex) or hit(k, config.remove_regex) or hit(k, config.pipe_selection) or hit(k, config.pipe_selection_to)) return; s.prompt.chain = 0; } /// A key in the prompt of the live view's tag: the field's editing keys, /// and the prompt's own. pub fn key(p: *Pardes, k: Key) void { const v = p.view(); const s = &v.tag_state; const id = TagState.hostOf(p) orelse return TagState.toRest(p); const pane = p.panes[id].?; const plain = !k.ctrl and !k.alt; const ctrl = k.ctrl and !k.alt; if (hit(k, config.escape)) return cancel(p, pane); if (plain and k.cp == Key.enter) return commit(p, id, pane); if (k.alt and !k.ctrl and k.cp == Key.enter) return asList(p, id, pane); // Backspace on an empty field: the prompt goes, as Esc. if (plain and k.cp == Key.backspace and s.field.len == 0) return cancel(p, pane); // Up and Down bring its history's lines into the field, the newest first. if (plain and (k.cp == Key.up or k.cp == Key.down)) { const r = ring(p, s.prompt.op); const to: u8 = if (k.cp == Key.up) @min(r.n, s.prompt.recall + 1) else s.prompt.recall -| 1; if (to == s.prompt.recall) return; s.prompt.recall = to; s.field.set(r.get(to) orelse ""); return live(p, pane); } if (ctrl and k.cp == 'r') { s.prompt.history = !s.prompt.history; return; } if (!s.field.key(k)) return; s.prompt.recall = 0; live(p, pane); } /// What the field says now, done to the body: the selections a pattern /// finds inside the ones the prompt began with; none, or no pattern, those /// it began with. A pattern that is no regex changes nothing, and the /// prompt says why. fn live(p: *Pardes, pane: *Pane) void { const s = &p.view().tag_state; const pat = s.field.text(); if (mode(s.prompt.op) == null and !searches(s.prompt.op)) return; s.prompt.bad = pat.len > 0 and if (regexp.Regex.compile(pat)) |_| false else |_| true; if (s.prompt.bad) return; if (mode(s.prompt.op)) |m| { s.prompt.found = @intCast(normal.applySelRegex(p, pane, &pane.body, pat, m)); return; } // A search: its places found from where it began, the view's first, // the rest a frame's budget a tick (Places.zig); `/` selects the first // at or after the caret as soon as it is found, `?` the last before it. const set = &pane.places; s.prompt.counted = std.math.maxInt(u32); if (pat.len == 0) { if (set.from == 0) set.clear(p.gpa); set.restore(p.settings.tab, pane); return; } const back = s.prompt.op == .search_back; set.search(p, pane, .typed, 0, pat, false, if (back) null else set.origin(pane), !back); s.prompt.seeking = back; if (back) seekBack(p, pane); } /// `?`'s place: the last before where it began once the text between is /// searched, past the top the last of all once every place is found; /// none, the body as it was. fn seekBack(p: *Pardes, pane: *Pane) void { const s = &p.view().tag_state; const set = &pane.places; if (!set.started) return; const origin = set.origin(pane); const hits = set.hits.items; const i = Places.firstFrom(hits, origin); const whole = set.head_to >= set.lo; const to: ?usize = if (i > 0 and origin <= set.hi and (whole or hits[i - 1].start >= set.lo)) i - 1 else if (set.complete() and hits.len > 0) hits.len - 1 else null; if (to) |at| { s.prompt.seeking = false; return set.goTo(p.settings.tab, pane, at, false); } if (!set.complete()) return; s.prompt.seeking = false; set.restore(p.settings.tab, pane); } /// Enter: what the field says, applied, and the line kept in its history /// and its register (`/` a pattern's, `|` a command's). fn commit(p: *Pardes, id: usize, pane: *Pane) void { const s = &p.view().tag_state; const op = s.prompt.op; const line = s.field.text(); // The field is the view's: what is kept of it is copied before rest. var kept: [Field.max]u8 = undefined; @memcpy(kept[0..line.len], line); const typed = kept[0..line.len]; const bad = s.prompt.bad; s.kind = .rest; TagState.toRest(p); if (typed.len == 0) return undo(p, pane, op); ring(p, op).add(typed); if (searches(op)) return land(p, id, pane, op, typed, bad); if (mode(op) != null) { // No regex: the selections it began with, and why. if (bad) { undo(p, pane, op); return p.setMessage(.at(id), regexMessage(typed)); } // helix keeps the pattern in `/` p.registers.put(p.gpa, '/', typed, 0, 1, true) catch |err| return p.reportError(.at(id), "prompt", err); p.registers.last_search = '/'; // Nothing found: nothing changed, no link. if (s.prompt.found == 0) return; link(p, pane, op, typed); keepSelections(p, pane); return; } if (behavior(op)) |how| { p.registers.put(p.gpa, '|', typed, 0, 1, true) catch |err| return p.reportError(.at(id), "prompt", err); // `! A-! $` apply, and end the recipe: no Edit line says them. if (op == .pipe or op == .pipe_to) link(p, pane, op, typed) else s.prompt.chain = 0; return Pipe.submit(p, id, &pane.body, how, typed); } } /// Enter in `/` or `?`: the place found current, its places tinted from /// now on for n and N, the pattern in `/`, and the pane's search word /// `/re/ 3/7` in its trail (SPEC §7.3). fn land(p: *Pardes, id: usize, pane: *Pane, op: Op, pat: []const u8, bad: bool) void { const set = &pane.places; // A search moves the selection: the recipe ends. p.view().tag_state.prompt.chain = 0; if (bad) { undo(p, pane, op); return p.setMessage(.at(id), regexMessage(pat)); } set.fill(p, pane, std.math.maxInt(usize), regexp.Regex.budget); if (op == .search_back) seekBack(p, pane); if (set.hits.items.len == 0) { undo(p, pane, op); var said: [Field.max + 16]u8 = undefined; return p.setMessage(.at(id), std.fmt.bufPrint(&said, "/{s}/: no match", .{pat}) catch "no match"); } 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; set.forget(p.gpa); word(pane, if (op == .search_back) '?' else '/', pat); } /// Alt-Enter in `/` or `?`: the places kept as a list, a row each, in the /// pane's query pane (Query.zig), the body as it was before the prompt. fn asList(p: *Pardes, id: usize, pane: *Pane) void { const s = &p.view().tag_state; const op = s.prompt.op; if (!searches(op)) return; var kept: [Field.max]u8 = undefined; const typed = kept[0..s.field.len]; @memcpy(typed, s.field.text()); s.kind = .rest; TagState.toRest(p); undo(p, pane, op); panes.Query.open(p, id, .text, typed, true); } /// Esc: the prompt goes and the body is as it was before it. fn cancel(p: *Pardes, pane: *Pane) void { const op = p.view().tag_state.prompt.op; p.view().tag_state.kind = .rest; TagState.toRest(p); undo(p, pane, op); } /// The live effect of prompt `op` on `pane`'s body taken back. fn undo(p: *Pardes, pane: *Pane, op: Op) void { if (mode(op)) |m| _ = normal.applySelRegex(p, pane, &pane.body, "", m); pane.nsel_snap = 0; if (!searches(op)) return; const set = &pane.places; if (set.from == 0 and set.provenance == .typed) set.clear(p.gpa); set.restore(p.settings.tab, pane); set.forget(p.gpa); } /// The prompt left by something else than its keys (TagState.toRest: a /// click, a word run from the band): its effect taken back, as Esc's. pub fn leave(p: *Pardes) void { const s = &p.view().tag_state; const pane = p.panes[TagState.hostOf(p) orelse return] orelse return; s.kind = .rest; undo(p, pane, s.prompt.op); } /// A paste into the field: its first line. pub fn paste(p: *Pardes, bytes: []const u8) void { const s = &p.view().tag_state; const pane = p.panes[TagState.hostOf(p) orelse return] orelse return; const line = bytes[0 .. std.mem.indexOfAny(u8, bytes, "\r\n") orelse bytes.len]; s.field.insert(line[0..modal.graphemeStart(line, @min(line.len, Field.max - s.field.len))]); s.prompt.recall = 0; live(p, pane); } /// Whether `pane`'s body shows a selecting prompt's live result: its /// selections drawn as a preview (body_layer), dimmed while the field is /// no regex. pub fn selecting(p: *const Pardes, pane: *const Pane) ?struct { dim: bool } { const s = &p.view().tag_state; if (s.kind != .prompt or mode(s.prompt.op) == null or s.host != pane.serial) return null; return .{ .dim = s.prompt.bad }; } /// Whether `pane`'s places are a search prompt's last good ones, its field /// no regex now: tinted dimmed (Places.paint). pub fn dims(p: *const Pardes, pane: *const Pane) bool { const s = &p.view().tag_state; return s.kind == .prompt and searches(s.prompt.op) and s.host == pane.serial and s.prompt.bad; } // ---- the recipe word (SPEC §7.5) ---- fn recipeOf(pane: *Pane) ?*Trail.Word { for (pane.trail.steps[0..pane.trail.n]) |*step| if (step.* == .recipe) return &step.recipe; return null; } /// Sam prompt `op`'s `line` as a link of `pane`'s recipe: after the one /// its last sam prompt left open, else a recipe of its own in place of the /// pane's one. One past the trail's room starts none. fn link(p: *Pardes, pane: *Pane, op: Op, line: []const u8) void { const s = &p.view().tag_state; var buf: [Trail.max_text]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); const open_recipe = if (s.prompt.chain == pane.serial) recipeOf(pane) else null; s.prompt.chain = 0; (if (open_recipe) |r| w.writeAll(r.text.slice()) else w.writeAll("Edit")) catch return; w.writeByte(' ') catch return; const said = switch (op) { .select => samWord(&w, 'x', line), .split => samWord(&w, 'y', line), .keep => samWord(&w, 'g', line), .remove => samWord(&w, 'v', line), .pipe => w.print("|{s}", .{line}), .pipe_to => w.print(">{s}", .{line}), else => return, }; said catch return; var word_: Trail.Word = .{}; word_.text.set(w.buffered()); pane.trail.setWord(.{ .recipe = word_ }); s.prompt.chain = pane.serial; } /// The selections a recipe's sam prompts made, kept for Look on its word. fn keepSelections(p: *Pardes, pane: *Pane) void { const text = edit.flatSurface(p.editor(), &pane.body) catch return; var ranges: [Text.max_selections]modal.Selection = undefined; const got = pane.body.ranges(text, 0, &ranges); const kept = p.gpa.dupe(modal.Selection, ranges[0..got.n]) catch return; p.gpa.free(pane.recipe_sels); pane.recipe_sels = kept; pane.recipe_pri = @intCast(got.pri); } /// Look on pane `id`'s recipe word: the selections it made, again. pub fn lookRecipe(p: *Pardes, id: usize) void { const pane = p.panes[id] orelse return; p.view().active = id; if (pane.recipe_sels.len == 0) return; const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(.at(id), "recipe", err); for (pane.recipe_sels) |*r| { r.anchor = @min(r.anchor, text.len); r.head = @min(r.head, text.len); } pane.body.setRanges(p.settings.tab, text, pane.recipe_sels, &.{}, @min(pane.recipe_pri, pane.recipe_sels.len - 1), true); pane.body.show(p.settings.tab); } /// Exec on it: the Edit line it is, run on what is selected now /// (edit_cmd.zig, sam's dot the primary selection). pub fn execRecipe(p: *Pardes, id: usize) void { const pane = p.panes[id] orelse return; const r = recipeOf(pane) orelse return; var line: [Trail.max_text]u8 = undefined; const text = r.text.slice(); @memcpy(line[0..text.len], text); p.view().active = id; pardes.edit_cmd.run(p, id, line["Edit ".len..text.len]); } // ---- the search word (SPEC §7.3) ---- /// 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 { 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 { 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 { 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 { 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 { // 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 = .{}; _ = w.text.print("{c}{s}{c}", .{ delim, pat, delim }); w.index = w.text.len; pane.trail.setWord(.{ .search = w }); refresh(pane, false); } /// The search word's place among its places written after its pattern: /// `3/7`, `–/7` with the selection off them, `⋯` for the count while they /// are found, ` ↻` after n or N went round an end (`wrapped`; null keeps /// what it said). fn refresh(pane: *Pane, wrapped: ?bool) void { const w = wordOf(pane) orelse return; if (!lights(pane, w)) return; const set = &pane.places; const f = &pane.file.?; const end = patternEnd(w); const round = wrapped orelse (w.index >> 16 != 0); const hits = set.hits.items; const sel = pane.body.primaryRange(f.content, 0); const on: ?usize = if (set.current) |c| (if (c < hits.len and hits[c].start == @min(sel.anchor, sel.head) and hits[c].end == @max(sel.anchor, sel.head)) c else null) else null; var buf: [48]u8 = undefined; var out: std.Io.Writer = .fixed(&buf); out.writeByte(' ') catch return; if (on) |c| out.print("{d}", .{c + 1}) catch return else out.writeAll("–") catch return; if (set.complete()) out.print("/{d}", .{hits.len}) catch return else out.writeAll("/⋯") catch return; if (round) out.writeAll(" ↻") catch return; const now = w.text.slice(); if (std.mem.eql(u8, now[end..], out.buffered()) and (w.index >> 16 != 0) == round) return; var text: [Trail.max_text]u8 = undefined; @memcpy(text[0..end], now[0..end]); @memcpy(text[end..][0..out.buffered().len], out.buffered()); w.text.set(text[0 .. end + out.buffered().len]); w.index = end | @as(u32, @intFromBool(round)) << 16; } /// After each step (Core.zig, beside the places'): every search word says /// where its places stand; the live view's search prompt finds `?`'s place /// and counts the lines its places are on once they are all found. pub fn settle(p: *Pardes) void { for (p.panes) |slot| if (slot) |pane| refresh(pane, null); const s = &p.view().tag_state; if (s.kind != .prompt or !searches(s.prompt.op)) return; const pane = p.panes[TagState.hostOf(p) orelse return].?; if (s.prompt.seeking) seekBack(p, pane); const set = &pane.places; if (!set.shows(pane) or !set.complete() or s.prompt.counted == set.hits_gen) return; // A line holds the next place unless a newline lies between them: a // pass over the text to the last place, once. const text = pane.file.?.content; var lines: u32 = 0; var prev: ?usize = null; for (set.hits.items) |h| { if (prev == null or std.mem.indexOfScalarPos(u8, text[0..h.start], prev.?, '\n') != null) lines += 1; prev = h.start; } s.prompt.lines = lines; s.prompt.counted = set.hits_gen; } /// n or N stepped pane `id`'s places from place `before`: its search word /// says where they stand, ` ↻` when the step went round an end, and the /// message row echoes it with the line: `/dirty/ 4/7 · line 112`. pub fn stepped(p: *Pardes, id: usize, forward: bool, before: ?usize) void { const pane = p.panes[id] orelse return; const w = wordOf(pane) orelse return; const set = &pane.places; if (!lights(pane, w)) return; const after = set.current orelse return; if (after >= set.hits.items.len) return; refresh(pane, if (before) |b| (if (forward) after <= b else after >= b) else false); const index = File.lineIndex(p.gpa, &pane.file.?) catch return; var said: [Trail.max_text + 32]u8 = undefined; p.setMessage(.at(id), std.fmt.bufPrint(&said, "{s} · line {d}", .{ w.text.slice(), Places.lineOf(index, set.hits.items[after].start) + 1 }) catch return); } /// `*`: 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 { pane.places.search(p, pane, .search, 0, pat, false, null, false); word(pane, '/', pat); } /// 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; 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 /// would, the next place when they are lit already (n). pub fn execWord(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 = '/'; p.view().active = id; if (lights(pane, w)) { const before = set.current; set.step(p, pane, true, 1, false); return stepped(p, id, true, before); } set.search(p, pane, .search, 0, pat, false, set.origin(pane), true); } /// The span of pane `pane`'s tag its search word takes while its places no /// longer light it (the first Esc cleared them): drawn dimmed (draw.zig). pub fn unlit(pane: *Pane) ?[2]usize { const t = &pane.trail; for (t.steps[0..t.cur], 0..) |*step, i| if (step.* == .search) { if (lights(pane, &step.search) or t.cur - i > Trail.shown_steps) return null; return t.spanOf(i); }; return null; } /// The gutter's marks while a prompt is typed (SPEC §7.1): `▸` on the /// current place's line and `▪` on every other line holding a place (`/ /// ?`); `┃` on each line the selections a sam prompt began with cover (`s /// S K A-K`), its scope. In the blank after a row's line number, on a /// line's first row. pub fn paintGutter(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: u16) void { const ts = &p.view().tag_state; if (ts.kind != .prompt or ts.host != pane.serial) return; const f = &(pane.file orelse return); if (mode(ts.prompt.op) != null) return fence(p, s, pane, r, body_h); if (!searches(ts.prompt.op)) return; const set = &pane.places; if (!set.shows(pane)) return; const hits = set.hits.items; if (hits.len == 0) return; const tab = p.settings.tab; const x = gutterX(pane, r); const y = p.bodyTop(pane, r); const ink = p.chromeTheme().box; const nlines = File.nlines(p.gpa, f); var last: i32 = -1; var vr: u16 = 0; while (vr < body_h) : (vr += 1) { const line = pane.wrapAt(tab, vr).line; if (line == last or line < 0) continue; if (line >= nlines) break; last = line; const ls = File.lineStart(p.gpa, f, @intCast(line)); const next = File.lineStart(p.gpa, f, @intCast(line + 1)); const i = Places.firstFrom(hits, ls); if (i == hits.len or hits[i].start >= @max(next, ls + 1)) continue; const current = if (set.current) |c| c < hits.len and hits[c].start >= ls and hits[c].start < @max(next, ls + 1) else false; var style = s.at(x, y + vr).style; style.fg = .{ .rgb = ink }; s.set(x, y + vr, if (current) "▸" else "▪", style); } } fn regexMessage(pat: []const u8) []const u8 { return if (regexp.Regex.compile(pat)) |_| "" else |err| regexp.Regex.message(err); } /// The sam command a pattern prompt is (`s S K A-K` are sam's `x y g v`), /// its `/` written `\/`. fn samWord(w: *std.Io.Writer, c: u8, pat: []const u8) !void { try w.print("{c}/", .{c}); for (pat) |b| if (b == '/') try w.writeAll("\\/") else try w.writeByte(b); try w.writeByte('/'); } /// The plain word prompt `op` with field `line` stands for: what Look or /// Exec would do typed in a tag. None for `! A-! $`, which sam cannot say. fn plainWord(w: *std.Io.Writer, op: Op, line: []const u8) !?[]const u8 { switch (op) { .search => try w.print("/{s}/", .{line}), .search_back => try w.print("?{s}?", .{line}), .select, .split, .keep, .remove => { try w.writeAll("Edit "); try samWord(w, switch (op) { .select => 'x', .split => 'y', .keep => 'g', else => 'v', }, line); }, .pipe => try w.print("Edit |{s}", .{line}), .pipe_to => try w.print("Edit >{s}", .{line}), .insert, .append, .keep_pipe => return null, .command => try w.writeAll(line), } return if (op == .search or op == .search_back) "Look" else "Exec"; } /// The x of the blank after pane `pane`'s line numbers. fn gutterX(pane: *const Pane, r: pardes.Rect) u16 { return r.x + config.GUTTER + (File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1); } /// `┃` on the lines the selections a sam prompt began with cover, and /// the ones `K` or `A-K` would drop dimmed and struck through. fn fence(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: u16) void { const f = &pane.file.?; const tab = p.settings.tab; const snap = pane.sel_snap[0..pane.nsel_snap]; if (snap.len == 0) return; const x = gutterX(pane, r); const y = p.bodyTop(pane, r); const ink = p.chromeTheme().box; const nlines = File.nlines(p.gpa, f); const op = p.view().tag_state.prompt.op; var kept: [Text.max_selections]modal.Selection = undefined; const now = if (op == .keep or op == .remove) pane.body.ranges(f.content, 0, &kept).n else 0; var last: i32 = -1; var vr: u16 = 0; while (vr < body_h) : (vr += 1) { const line = pane.wrapAt(tab, vr).line; if (line == last or line < 0) continue; if (line >= nlines) break; last = line; const ls = File.lineStart(p.gpa, f, @intCast(line)); const le = File.lineStart(p.gpa, f, @intCast(line + 1)); // The ranges are in the text's order: the first ending past the // line's start, on while they start before its end. var i = endingPast(snap, ls); if (i < snap.len and @min(snap[i].anchor, snap[i].head) < @max(le, ls + 1)) { var style = s.at(x, y + vr).style; style.fg = .{ .rgb = ink }; s.set(x, y + vr, "┃", style); } // K and A-K: the selections that would go, on this line. if (op != .keep and op != .remove) continue; while (i < snap.len and @min(snap[i].anchor, snap[i].head) < le) : (i += 1) { const lo = @min(snap[i].anchor, snap[i].head); const hi = @max(snap[i].anchor, snap[i].head); const k = endingPast(kept[0..now], lo); const stays = k < now and @min(kept[k].anchor, kept[k].head) == lo and @max(kept[k].anchor, kept[k].head) == hi; if (!stays) strike(p, s, pane, r, line, @intCast(@max(lo, ls) - ls), @intCast(@min(hi, le) - ls)); } } } /// The first of `ranges` (in the text's order) ending past byte `at`. fn endingPast(ranges: []const modal.Selection, at: usize) usize { return std.sort.partitionPoint(modal.Selection, ranges, at, struct { fn before(byte: usize, r: modal.Selection) bool { return @max(r.anchor, r.head) <= byte; } }.before); } /// Bytes [lo, hi) of `line`, as many of its rows as show them, dimmed and /// struck through (File.paintWordSelection's walk). fn strike(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, line: i32, lo_: i32, hi_: i32) void { const tab = p.settings.tab; const tw = r.w - config.GUTTER; const prefix: i32 = File.gutterWidth(pane); const body_y = p.bodyTop(pane, r); var vr: i32 = 0; while (vr + @as(i32, pane.tag_rows) < @as(i32, r.h)) : (vr += 1) { const here = pane.wrapAt(tab, vr); if (here.line != line) continue; var hi = hi_; const next = pane.wrapAt(tab, vr + 1); if (next.line == line) hi = @min(hi, next.at); const lo = @max(lo_, here.at); if (hi <= lo) continue; const c0 = prefix + here.pad + File.displayOffset(tab, pane, line, here.at, lo); const c1 = prefix + here.pad + File.displayEndOffset(tab, pane, line, here.at, hi - 1); var col = @max(prefix, c0); while (col <= c1 and col < tw) : (col += 1) { const style = &s.at(r.x + config.GUTTER + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr))).style; style.dim = true; style.strikethrough = true; } } } /// The head of the prompt's tag: ` s self▌ = Exec Edit x/self/ 4 /// selections in 6 lines ⏎ select · C-r history · esc`. pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { const s = &p.view().tag_state; const pr = &s.prompt; var text: std.ArrayList(u8) = .empty; var spans: std.ArrayList(TagState.Span) = .empty; try text.append(arena, ' '); try styled(arena, &text, &spans, info.get(pr.op).cell, .key); try text.append(arena, ' '); const caret: u32 = @intCast(text.items.len + s.field.caret); const line = s.field.text(); try text.appendSlice(arena, line); if (line.len > 0) { var buf: [Field.max + 16]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); if (plainWord(&w, pr.op, line) catch null) |verb| { try styled(arena, &text, &spans, try std.fmt.allocPrint(arena, " = {s}", .{verb}), .dim); try text.print(arena, " {s}", .{w.buffered()}); } if (pr.bad) { try text.appendSlice(arena, " "); try styled(arena, &text, &spans, regexMessage(line), .err); try text.print(arena, " · the {d} from before shown, dimmed", .{@as(usize, pane.body.nsel) + 1}); } else try count(arena, &text, pane, pr); } try text.appendSlice(arena, " "); try styled(arena, &text, &spans, info.get(pr.op).hint, .dim); return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } fn styled(arena: std.mem.Allocator, text: *std.ArrayList(u8), spans: *std.ArrayList(TagState.Span), s: []const u8, style: TagState.Style) !void { const lo: u32 = @intCast(text.items.len); try text.appendSlice(arena, s); try spans.append(arena, .{ .lo = lo, .hi = @intCast(text.items.len), .style = style }); } /// The prompt's count after its field: what it found (`4 selections in 6 /// lines`, `keeps 3 of 9`, `3/7 · 5 lines`, `3/⋯` while its places are /// found, `0`). fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *const Prompt) !void { switch (pr.op) { .search, .search_back => { const set = &pane.places; if (!set.shows(pane)) return; const hits = set.hits.items; if (set.complete() and hits.len == 0) return text.appendSlice(arena, " 0"); try text.appendSlice(arena, " "); const at = if (set.current) |c| (if (c < hits.len) c else null) else null; if (at) |c| try text.print(arena, "{d}", .{c + 1}) else try text.appendSlice(arena, "–"); if (!set.complete()) return text.appendSlice(arena, "/⋯"); try text.print(arena, "/{d} · {d} line{s}", .{ hits.len, pr.lines, plural(pr.lines) }); // Round the end from where it began. const c = at orelse return; const origin = set.origin(pane); if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " · wrapped"); }, .select, .split => if (pr.found == 0) try text.appendSlice(arena, " 0") else try text.print(arena, " {d} selection{s} in {d} line{s}", .{ pr.found, plural(pr.found), pr.scope, plural(pr.scope) }), .keep, .remove => try text.print(arena, " keeps {d} of {d}", .{ pr.found, pane.nsel_snap }), else => {}, } } fn plural(n: usize) []const u8 { return if (n == 1) "" else "s"; } /// The rows the prompt's band takes: its history unrolled (Ctrl-r), the /// lines the field is in, eleven at most. pub fn takes(p: *Pardes) u16 { const s = &p.view().tag_state; if (!s.prompt.history) return 0; 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; } return @max(1, @min(Band.Cursor.shown, n)); } /// The prompt's steps in its band: its history, newest first, the lines /// holding the field, the one Up recalled on. pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.Step) ![]Band.Step { const s = &p.view().tag_state; if (!s.prompt.history) return out[0..0]; const r = ring(p, s.prompt.op); var rows: std.ArrayList(Band.Row) = .empty; const q = s.field.text(); var back: u8 = 1; while (back <= r.n and rows.items.len < Band.Cursor.shown) : (back += 1) { const l = r.get(back).?; if (std.mem.indexOf(u8, l, q) == null) continue; try rows.append(arena, .{ .text = l, .on = back == s.prompt.recall, .index = back }); } if (rows.items.len == 0) try rows.append(arena, .{ .text = if (r.n == 0) "no history yet" else "no line holds it", .dim = true }); out[0] = .{ .rows = rows.items }; return out[0..1]; } fn typeKeys(p: *Pardes, keys: []const u8) void { for (keys) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } fn tagOf(p: *Pardes) ![]const u8 { return tagline.tagText(p, p.scratch.allocator(), p.panes[p.view().active].?); } fn has(hay: []const u8, needle: []const u8) bool { return std.mem.indexOf(u8, hay, needle) != null; } test "s asks in the tag's head: its key cell, the field, the plain word and the count, the selections live in the body; no match is 0, a pattern that is no regex keeps the last result dimmed and says why, Esc puts them back" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); defer p.deinit(); const pane = try p.setTestFile("self.a = 1;\nself.b = 2;\nother = 3;\n"); const rest = try std.testing.allocator.dupe(u8, try tagOf(p)); defer std.testing.allocator.free(rest); typeKeys(p, "%s"); try std.testing.expectEqual(TagState.Kind.prompt, p.view().tag_state.kind); // No chip on the notice band: the prompt is the tag's. try std.testing.expectEqual(Pane.Prompt.none, pane.prompt); try std.testing.expect(std.mem.endsWith(u8, try tagOf(p), " s ⏎ select · C-r history · esc")); typeKeys(p, "self"); try std.testing.expect(has(try tagOf(p), " s self = Exec Edit x/self/ 2 selections in 3 lines ⏎ select")); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expect(selecting(p, pane).?.dim == false); // No match: 0, and the selection it began with. typeKeys(p, "x"); try std.testing.expect(has(try tagOf(p), "= Exec Edit x/selfx/ 0 ")); try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); p.update(.{ .key = .{ .cp = Key.backspace } }); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); // No regex: the two from before, dimmed, and why. typeKeys(p, "("); try std.testing.expect(has(try tagOf(p), " bad regular expression · the 2 from before shown, dimmed ")); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expect(selecting(p, pane).?.dim); // Esc: rest, the tag and the selection as they were. p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expectEqualStrings(rest, try tagOf(p)); try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); const r = pane.body.primaryRange(pane.file.?.content, 0); try std.testing.expectEqual(pane.file.?.content.len, @max(r.anchor, r.head)); try std.testing.expect(selecting(p, pane) == null); } test "Enter keeps what s found, the pattern in / and in the history Up brings back; K keeps, counted; a pipe from the body asks in the tag and runs off the loop" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); defer p.deinit(); while (p.nextEffect()) |_| {} const pane = try p.setTestFile("self.a = 1;\nself.b = 2;\nother = 3;\n"); typeKeys(p, "%sself.."); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expectEqualStrings("self..", p.registers.value('/', 0).?); try std.testing.expectEqualStrings("self..", p.prompt_history.search.get(1).?); // K over the two: Up brings `self..` back, `b` keeps one of them. typeKeys(p, "K"); p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("self..", p.view().tag_state.field.text()); try std.testing.expect(has(try tagOf(p), " K self.. = Exec Edit g/self../ keeps 2 of 2")); p.update(.{ .key = .{ .cp = Key.down } }); typeKeys(p, "b"); try std.testing.expect(has(try tagOf(p), " K b = Exec Edit g/b/ keeps 1 of 2 ⏎ keep")); // Ctrl-r: the history under the tag, a row a line. p.update(.{ .key = .{ .cp = 'r', .ctrl = true } }); try std.testing.expectEqual(@as(u16, 1), Band.takes(p, p.view().active)); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); try std.testing.expectEqualStrings("b", p.prompt_history.search.get(1).?); try std.testing.expectEqualStrings("self..", p.prompt_history.search.get(2).?); // | from the body: in the tag, the plain word Edit's, Enter pipes. typeKeys(p, "|"); try std.testing.expectEqual(Pane.Prompt.none, pane.prompt); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⏎ replace")); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqualStrings("tr a-z A-Z", p.registers.value('|', 0).?); var asked: ?u32 = null; while (p.nextEffect()) |effect| if (effect == .pipe) { asked = effect.pipe.id; }; const request = p.pipe.pipeRequest(asked orelse return error.MissingPipeEffect).?; try std.testing.expectEqualStrings("tr a-z A-Z", request.command); try std.testing.expectEqualSlices(u8, "self.b", request.inputs[0]); // Esc in a pipe's prompt runs nothing. typeKeys(p, "|cat"); p.update(.{ .key = .{ .cp = Key.escape } }); while (p.nextEffect()) |effect| try std.testing.expect(effect != .pipe); } const dirty = "dirty one\nclean\ndirty two dirty\nclean\ndirty three\ndirty four dirty\nclean\ndirty five\n"; /// The glyph in the blank after the line number of body row `row`. fn gutterAt(p: *Pardes, arena: std.mem.Allocator, row: u16) ![]const u8 { p.sync(); const s = try p.render(arena); const pane = p.panes[0].?; const r = p.view().rects[0]; const x = r.x + config.GUTTER + (File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1); return s.at(x, p.bodyTop(pane, r) + row).grapheme(); } test "/ searches the body as it is typed: the current place selected, the gutter marked, counted 3/7 · 5 lines; Enter folds /dirty/ 3/7 into the trail; n says where it went, round the end too; the first Esc clears the tints and dims the word, Look on it lights them again" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); defer p.deinit(); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); const pane = try p.setTestFile(dirty); pane.body.cur_row = 2; pane.body.cur_col = 6; typeKeys(p, "/dirty"); try std.testing.expect(has(try tagOf(p), " / dirty = Look /dirty/ 3/7 · 5 lines ⏎ land · M-⏎ as a list")); try std.testing.expectEqual(@as(?usize, 2), pane.places.current); try std.testing.expectEqualStrings("▪", try gutterAt(p, frame.allocator(), 0)); try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 1)); try std.testing.expectEqualStrings("▸", try gutterAt(p, frame.allocator(), 2)); try std.testing.expectEqualStrings("▪", try gutterAt(p, frame.allocator(), 7)); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 3/7 › /test.txt ")); try std.testing.expectEqualStrings("dirty", p.registers.value('/', 0).?); try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 0)); // n: the word in place, and the line said. typeKeys(p, "n"); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 4/7 › ")); try std.testing.expect(has(pane.msg[0..pane.msg_len], "/dirty/ 4/7 · line 5")); typeKeys(p, "nnnn"); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 1/7 ↻ › ")); // Esc: the tints go, the word stays, dimmed; Look on it lights them // again, the place it was on selected. p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expect(!pane.places.started); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 1/7 ↻ › ")); try std.testing.expect(unlit(pane) != null); typeKeys(p, "j"); Trail.lookStep(p, 0, 0); try std.testing.expect(pane.places.started and unlit(pane) == null); try std.testing.expectEqual(@as(?usize, 0), pane.places.current); const sel = pane.body.primaryRange(pane.file.?.content, 0); try std.testing.expectEqual(@as(usize, 0), @min(sel.anchor, sel.head)); // Off a place: –/7. typeKeys(p, "j"); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ –/7")); // One search word a pane: a new search takes its place. typeKeys(p, "gg/clean"); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/clean/ 1/3 › /test.txt ")); try std.testing.expectEqual(@as(u8, 1), pane.trail.n); } test "? searches back from the caret; no match is 0; a pattern that is no regex keeps the last places dimmed; Esc puts the body back; * makes the search word with no prompt; Alt-Enter keeps the places as the query pane's rows" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); defer p.deinit(); const pane = try p.setTestFile(dirty); pane.body.cur_row = 2; pane.body.cur_col = 6; const was = pane.body.primaryRange(pane.file.?.content, 0); typeKeys(p, "?dirty"); try std.testing.expect(has(try tagOf(p), " ? dirty = Look ?dirty? 2/7 · 5 lines ⏎ land")); typeKeys(p, "x"); try std.testing.expect(has(try tagOf(p), "= Look ?dirtyx? 0 ⏎ land")); try std.testing.expectEqualDeep(was, pane.body.primaryRange(pane.file.?.content, 0)); p.update(.{ .key = .{ .cp = Key.backspace } }); typeKeys(p, "("); try std.testing.expect(has(try tagOf(p), " bad regular expression · ")); try std.testing.expect(dims(p, pane) and pane.places.started); try std.testing.expectEqualStrings("dirty", pane.places.pattern); p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expect(!pane.places.started); try std.testing.expectEqualDeep(was, pane.body.primaryRange(pane.file.?.content, 0)); try std.testing.expectEqual(@as(u8, 0), pane.trail.n); // `*` on `dirty` at the top: its places, its word, no prompt. typeKeys(p, "gge*"); try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/\\bdirty\\b/ 1/7 › ")); // Alt-Enter: the query pane, its line `/dirty`. typeKeys(p, "/dirty"); p.update(.{ .key = .{ .cp = Key.enter, .alt = true } }); try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expect(p.panes[p.view().active].?.query != null); } test "sam prompts one after another make one recipe word, the Edit line it is; a motion ends it and the next starts the pane's one again; ! adds nothing; the scope fenced in the gutter, what K drops struck; Look puts its selections back, Exec runs it as Edit" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); defer p.deinit(); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); while (p.nextEffect()) |_| {} const pane = try p.setTestFile("self.a = 1;\nself.b = 2;\nother = 3;\n"); typeKeys(p, "%sother"); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/other/ › /test.txt ")); // Look on it: the selection it made, again. typeKeys(p, "gg"); Trail.lookStep(p, 0, 0); const back = pane.body.primaryRange(pane.file.?.content, 0); try std.testing.expectEqual(@as(usize, 24), @min(back.anchor, back.head)); try std.testing.expectEqual(@as(usize, 29), @max(back.anchor, back.head)); // A motion ends it: the next s is a recipe of its own, the pane's one. typeKeys(p, "j%sself.."); for ([_]u16{ 0, 1, 2 }) |row| try std.testing.expectEqualStrings("┃", try gutterAt(p, frame.allocator(), row)); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/self../ › /test.txt ")); try std.testing.expectEqual(@as(u8, 1), pane.trail.n); // K right after: `self.b` would go, struck through. typeKeys(p, "Ka"); p.sync(); const s = try p.render(frame.allocator()); const r = p.view().rects[0]; const cell = s.at(r.x + config.GUTTER + File.gutterWidth(pane), p.bodyTop(pane, r) + 1); try std.testing.expect(cell.style.strikethrough and cell.style.dim); try std.testing.expect(!s.at(r.x + config.GUTTER + File.gutterWidth(pane), p.bodyTop(pane, r)).style.strikethrough); p.update(.{ .key = .{ .cp = Key.enter } }); typeKeys(p, "|"); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/self../ g/a/ |tr a-z A-Z › /test.txt ")); // `!` applies and adds nothing: an Edit line cannot say it. typeKeys(p, "!"); p.update(.{ .key = .{ .cp = 'e', .text = "echo" } }); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/self../ g/a/ |tr a-z A-Z › /test.txt ")); while (p.nextEffect()) |_| {} if (p.pipe.wait) |*w| w.deinit(); p.pipe.wait = null; // Exec on it: Edit runs it, its command off the loop. typeKeys(p, "%"); Trail.execStep(p, 0, 0); try std.testing.expect(p.pipe.edit_run != null); }