diff options
Diffstat (limited to 'core/Command.zig')
| -rw-r--r-- | core/Command.zig | 128 |
1 files changed, 110 insertions, 18 deletions
diff --git a/core/Command.zig b/core/Command.zig index d0d8d2d3..f41d0639 100644 --- a/core/Command.zig +++ b/core/Command.zig @@ -2,8 +2,9 @@ //! tag after a `:` key cell, a text prompt of its own (Prompt.zig's //! `.command`). Its rows are the answers of a question waiting on the pane //! (SPEC §10.4), then the tag's own words in the tag's order (`Kill` after -//! them for a terminal), marked `in the tag`, each kind ruled off, then the -//! commands a helix hand types, each the pardes word it runs: `write` +//! them for a terminal), marked `in the tag`, then the words of the pane's +//! kind its tag does not show now (UX K'4), marked `for this pane`, each +//! kind ruled off, then the commands a helix hand types, each the pardes word it runs: `write` //! Save, `quit` Del, `theme X` Theme X, `set` and `toggle` a setting's //! word, `reload` Get, `goto N` Look `:N`, `open F` Find F, `sort`, //! `reflow` and `pipe` a pipe of the selections. The field filters them by @@ -57,10 +58,11 @@ rows: Rows = .names, /// values are its. setting: u8 = 0, /// The names' first rows are the waiting question's answers, then the -/// tag's words: how many of each there were when the field was filtered -/// (the commands' come after them). +/// tag's words, then the pane's others: how many of each there were when +/// the field was filtered (the commands' come after them). answers: u8 = 0, words: u8 = 0, +others: u8 = 0, /// Where the query starts in the field: Tab writes the current row from /// there. from: u16 = 0, @@ -86,7 +88,8 @@ const typed = [_]Typed{ .{ .name = "toggle", .arg = "setting", .rows = .settings, .doc = "Flip a setting, or step it to its next value: its word bare." }, }; -/// The tag's words `:` lists at most, and a question's answers. +/// The tag's words and the pane's others `:` lists at most, and a +/// question's answers. const max_words = 32; const max_answers = 8; /// A row's text at most, its name and what it says beside it. @@ -149,6 +152,7 @@ pub fn filter(p: *Pardes, pane: *Pane) void { c.from = 0; c.answers = 0; c.words = 0; + c.others = 0; s.rows = .{}; s.nhits = 0; s.ntotal = 0; @@ -195,7 +199,13 @@ pub fn filter(p: *Pardes, pane: *Pane) void { consider(s, q, w, c.answers + c.words); c.words += 1; } - for (typed, 0..) |t, i| consider(s, q, t.name, c.answers + c.words + i); + var others = otherWords(pane); + while (others.next()) |w| { + if (c.words + c.others == max_words) break; + consider(s, q, w, c.answers + c.words + c.others); + c.others += 1; + } + for (typed, 0..) |t, i| consider(s, q, t.name, c.answers + c.words + c.others + i); }, .themes => for (colors.themes, 0..) |t, i| consider(s, q, t.name, i), .settings => { @@ -287,6 +297,37 @@ fn tagWords(pane: *Pane) Words { return .{ .computed = std.mem.tokenizeAny(u8, tagline.computedWords(pane), " "), .own = std.mem.tokenizeAny(u8, tagline.curTail(pane), " \t\n"), .kill = pane.isTerminal() and pane.command == null }; } +/// The words of the pane's kind its tag does not show now (UX K'4): those +/// its tag had before K' (tagline.kindTail: Save while clean, Term, +/// Collapse, Follow, Def, Refs, TreeContext, Mode, Filter, ...), then a +/// file's Undo and Redo (u, U); each runs from `:` as it ran from the tag. +const Others = struct { + pane: *Pane, + kind: std.mem.TokenIterator(u8, .scalar), + edits: u8, + + fn next(o: *Others) ?[]const u8 { + while (true) { + const word = o.kind.next() orelse edit: { + const all = [_][]const u8{ "Undo", "Redo" }; + if (o.edits == all.len) return null; + o.edits += 1; + break :edit all[o.edits - 1]; + }; + var shown = tagWords(o.pane); + while (shown.next()) |w| { + if (std.mem.eql(u8, w, word)) break; + } else return word; + } + } +}; + +fn otherWords(pane: *Pane) Others { + // A query's text is its rows (Query.zig): none of a file's edits. + const edits = pane.file != null and pane.query == null; + return .{ .pane = pane, .kind = std.mem.tokenizeScalar(u8, tagline.kindTail(pane), ' '), .edits = if (edits) 0 else 2 }; +} + /// What row `at` of the rows stands for, as Tab writes it: an answer's /// word, a tag's word, a command's name, a theme's, a setting's word, a /// value, a file a pane shows, a pipe; null past them (a damaged image's). @@ -299,6 +340,8 @@ fn rowOf(p: *Pardes, pane: *Pane, c: *const Command, at: usize) ?[]const u8 { var words = tagWords(pane); var i: usize = c.answers; while (words.next()) |w| : (i += 1) if (i == at) return w; + var others = otherWords(pane); + while (others.next()) |w| : (i += 1) if (i == at) return w; return null; }, .themes => return if (at < colors.themes.len) colors.themes[at].name else null, @@ -322,8 +365,9 @@ fn answerAt(p: *const Pardes, pane: *const Pane, c: *const Command, at: usize) ? /// The command row `at` of the names is, by its index in `typed`. fn typedAt(c: *const Command, at: usize) ?usize { - if (c.rows != .names or at < @as(usize, c.answers) + c.words) return null; - const t = at - c.answers - c.words; + const before = @as(usize, c.answers) + c.words + c.others; + if (c.rows != .names or at < before) return null; + const t = at - before; return if (t < typed.len) t else null; } @@ -571,9 +615,9 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { _ = Names.match(q, t, std.ascii.lowerString(&low, t), 0, &marks, &nmarks); for (marks[0..nmarks]) |*m| m.* += lead; } - // The answers' last and the tag's last word ruled off from what is - // under them. - const last = c.rows == .names and i + 1 < s.nhits and (at + 1 == c.answers or at + 1 == @as(usize, c.answers) + c.words); + // The answers' last, the tag's last word and the pane's last other + // ruled off from what is under them. + const last = c.rows == .names and i + 1 < s.nhits and (at + 1 == c.answers or at + 1 == @as(usize, c.answers) + c.words or at + 1 == @as(usize, c.answers) + c.words + c.others); row.* = .{ .text = text.items, .on = i == s.rows.at, .key = if (ans != null) 1 else 0, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .under = last }; } out[0] = .{ .rows = rows }; @@ -582,14 +626,14 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { return 2; } -/// What row `at` says on its right: a tag's word `in the tag`, what a -/// command takes, the theme in use, a setting's value now (its line of +/// What row `at` says on its right: a tag's word `in the tag`, the pane's +/// others `for this pane`, what a command takes, the theme in use, a setting's value now (its line of /// the settings' text `now`), the value it has. fn saidOf(p: *Pardes, arena: std.mem.Allocator, c: *const Command, at: usize, name: []const u8, now: []const u8) ![]const u8 { switch (c.rows) { .names => { if (at < c.answers) return "answers the question"; - const t = typed[typedAt(c, at) orelse return "in the tag"]; + const t = typed[typedAt(c, at) orelse return wordSaid(c, at)]; return if (t.arg.len > 0) try std.fmt.allocPrint(arena, "<{s}>", .{t.arg}) else ""; }, .themes => return if (p.custom_theme == null and at == p.settings.theme) "in use" else "", @@ -607,6 +651,11 @@ fn saidOf(p: *Pardes, arena: std.mem.Allocator, c: *const Command, at: usize, na } } +/// What a word row says: in the tag, or one of the pane's others. +fn wordSaid(c: *const Command, at: usize) []const u8 { + return if (at < @as(usize, c.answers) + c.words) "in the tag" else "for this pane"; +} + /// Setting `k`'s value now, as its word is written: `on`, `walk`. fn valueNow(p: *Pardes, k: u8, buf: []u8) []const u8 { const st = config.Settings.settings[@min(k, config.Settings.settings.len - 1)]; @@ -648,7 +697,7 @@ fn docRows(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, c: *const Command, return rows; } const name = rowOf(p, pane, c, at) orelse ""; - rows[0] = .{ .text = try std.fmt.allocPrint(arena, "{s} in the tag", .{name}), .key = @intCast(name.len), .index = none }; + rows[0] = .{ .text = try std.fmt.allocPrint(arena, "{s} {s}", .{ name, wordSaid(c, at) }), .key = @intCast(name.len), .index = none }; rows[1] = .{ .text = if (std.meta.stringToEnum(pardes.Builtin, name)) |b| builtins.registry.doc(b).text else "A word of this pane's tag: ⏎ runs it, as B2 on it does.", .index = none }; return rows; } @@ -704,7 +753,7 @@ test ": opens a field in the tag after its key cell: the tag's own words first, try std.testing.expectEqual(Prompt.Op.command, s.prompt.op); try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); // The head: its key cell, the field, the count, the hints. - try std.testing.expect(has(try tagOf(p), ": 14 commands Tab complete · ⏎ run · C-r history · esc")); + try std.testing.expect(has(try tagOf(p), ": 18 commands Tab complete · ⏎ run · C-r history · esc")); // The rows: the tag's words in its order, `in the tag`, the last ruled // off; then the commands, each what it takes. try std.testing.expect(has(try bandRow(p, a, 0), "▶ Save ")); @@ -715,8 +764,11 @@ test ": opens a field in the tag after its key cell: the tag's own words first, const band = (try Band.of(p, a, pane, p.view().active)).?; const first = band.steps[0].rows; try std.testing.expect(first[2].under and !first[1].under); - try std.testing.expect(has(first[3].text, "write w")); - try std.testing.expect(has(first[5].text, "theme") and has(first[5].text, "<theme>")); + // Then its kind's words the tag does not show (UX K'4), ruled off too. + try std.testing.expect(has(first[3].text, "Term for this pane")); + try std.testing.expect(first[6].under and has(first[6].text, "Redo")); + try std.testing.expect(has(first[7].text, "write w")); + try std.testing.expect(has(first[9].text, "theme") and has(first[9].text, "<theme>")); // Typing filters both, by the scorer, in their order. typeKeys(p, "th"); try std.testing.expect(has(try tagOf(p), ": th = Exec th 1 command ")); @@ -995,3 +1047,43 @@ test "a question waiting on the pane lists its answers first, each after its key try std.testing.expect(has(try bandRow(p, a, 0), "▶ Save ")); p.update(.{ .key = .{ .cp = Key.escape } }); } + +test ": lists the words of the pane's kind its tag does not show now, `for this pane`: Save while the file is clean, Collapse, Undo; Enter runs any builtin as B2 does, Joincol too (UX K'4)" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const pane = try p.setTestFile("one\n"); + const id = p.view().active; + // A file reading, clean: its tag's word `Del` (`Keep` is its walk's, + // after its path); the words its kind had before UX K' and a file's + // edits are the rest, in that order. + typeKeys(p, ":"); + const s = &p.view().tag_state; + try std.testing.expectEqual(@as(u8, 1), s.prompt.command.words); + try std.testing.expectEqual(@as(u8, 6), s.prompt.command.others); + const rows = (try Band.of(p, a, pane, id)).?.steps[0].rows; + try std.testing.expect(has(rows[0].text, "Del in the tag") and rows[0].under); + try std.testing.expect(has(rows[1].text, "Save for this pane")); + try std.testing.expect(has(rows[3].text, "Collapse for this pane")); + try std.testing.expect(has(rows[5].text, "Undo for this pane")); + try std.testing.expect(has(rows[6].text, "Redo for this pane") and rows[6].under); + try std.testing.expect(has(rows[7].text, "write w")); + // Its doc is the builtin's, said for this pane. + typeKeys(p, "Coll"); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ Collapse ")); + try std.testing.expect(has(try bandRow(p, a, 0), "Collapse for this pane")); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Enter runs the line as B2 runs a word: Joincol, in no pane's tag, + // from two columns folds this one into the one on its right. + try std.testing.expect(p.executeBuiltinLine(id, "Newcol")); + tagline.exitHeader(p); + p.view().active = id; + p.sync(); + try std.testing.expectEqual(@as(usize, 2), p.view().ncol); + typeKeys(p, ":Joincol"); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(@as(usize, 1), p.view().ncol); + try std.testing.expectEqualStrings("one\n", pane.file.?.content); +} |
