summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Band.zig24
-rw-r--r--core/TagState.zig158
-rw-r--r--core/builtins.zig13
-rw-r--r--core/config.zig1
-rw-r--r--core/keys.zig24
-rw-r--r--docs/typ/builtins-gui.json1
-rw-r--r--docs/typ/builtins-tui.json1
7 files changed, 217 insertions, 5 deletions
diff --git a/core/Band.zig b/core/Band.zig
index 079a1bcc..8a433243 100644
--- a/core/Band.zig
+++ b/core/Band.zig
@@ -88,7 +88,11 @@ pub fn takes(p: *Pardes, id: usize) u16 {
switch (s.kind) {
.rest => {},
.help => return @max(doc_rows, @max(1, @min(Cursor.shown, s.nhits))),
- .describe => return 0,
+ .describe => {
+ if (!s.found) return 0;
+ var children: [bindings.max_children]bindings.Child = undefined;
+ return @max(doc_rows, levelRows(@intCast(bindings.level(p, s.scope_root.?, s.scope[0 .. s.scope_n - 1], &children).len)));
+ },
}
if (!s.unrolled) return 0;
const cur = TagState.current(p, v) orelse return 0;
@@ -116,7 +120,23 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band {
try band.place(p, arena, pane, id);
return band;
},
- .describe => return null,
+ // describe-key's leaf: its level replayed, the leaf on the cursor,
+ // and its doc.
+ .describe => {
+ const root = s.scope_root.?;
+ const path = s.scope[0 .. s.scope_n - 1];
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const level = bindings.level(p, root, path, &children);
+ var at: usize = 0;
+ for (level, 0..) |c, i| if (c.key == s.scope[s.scope_n - 1]) {
+ at = i;
+ };
+ const node = bindings.nodeAt(root, path);
+ band.add(try levelStep(arena, pane, level, at, if (node) |n| s.used[n.group] else 0));
+ band.add(try docStep(p, arena, root, path, level[at]));
+ try band.place(p, arena, pane, id);
+ return band;
+ },
}
const cur = TagState.current(p, v).?;
// The level, its cursor on the child it names, else the leaf its group
diff --git a/core/TagState.zig b/core/TagState.zig
index 56c715af..0a4d99a8 100644
--- a/core/TagState.zig
+++ b/core/TagState.zig
@@ -40,8 +40,12 @@ host: u32 = 0,
/// Help's scope: the root and keys its `?` was struck under (`SPC l ?`),
/// or every root's leaves (`SPC ?`: null).
scope_root: ?Root = null,
-scope: [4]u8 = undefined,
+scope: [8]u8 = undefined,
scope_n: u8 = 0,
+/// describe-key's sequence (its root in scope_root, its keys in scope):
+/// it reached a leaf, or nothing is bound to it.
+found: bool = false,
+missed: bool = false,
/// The field typed in the head (Field.zig): Help's query; a picker's, a
/// prompt's.
field: Field = .{},
@@ -71,6 +75,61 @@ used: [bindings.ngroups]u8 = @splat(0),
/// while the others are (SPEC §3.6). Set by the state that previews.
previewing: std.StaticBitSet(pardes.MAX_PANES) = .empty,
+/// describe-key (SPEC §4.5), in the tag of pane `id`: the next keys are
+/// replayed in the tree.
+pub fn openDescribe(p: *Pardes, id: usize) void {
+ const pane = p.panes[id] orelse return;
+ toRest(p);
+ const s = &p.view().tag_state;
+ s.kind = .describe;
+ s.host = pane.serial;
+ s.scope_root = null;
+ s.scope_n = 0;
+ s.found = false;
+ s.missed = false;
+}
+
+/// A key of describe-key's sequence: the first a root's, then down its
+/// tree to a group (more keys) or a leaf (found), or to nothing (missed). A
+/// key after a sequence ended starts the next.
+pub fn describeKey(p: *Pardes, key: pardes.Key) void {
+ const s = &p.view().tag_state;
+ if (s.found or s.missed) {
+ s.scope_root = null;
+ s.scope_n = 0;
+ s.found = false;
+ s.missed = false;
+ }
+ const root = s.scope_root orelse {
+ s.scope_root = bindings.rootOf(key);
+ if (s.scope_root == null or s.scope_root == .register) {
+ // Not a root: the key alone, struck.
+ s.scope_root = null;
+ s.scope[0] = if (key.cp >= 0x20 and key.cp < 0x7f and !key.ctrl and !key.alt) @intCast(key.cp) else '?';
+ s.scope_n = 1;
+ s.missed = true;
+ }
+ return;
+ };
+ const printable = key.cp >= 0x20 and key.cp < 0x7f and !key.ctrl and !key.alt;
+ if (!printable or s.scope_n == s.scope.len) {
+ s.missed = true;
+ return;
+ }
+ const k: u8 = @intCast(key.cp);
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const child = for (bindings.level(p, root, s.scope[0..s.scope_n], &children)) |c| {
+ if (c.key == k) break c;
+ } else null;
+ s.scope[s.scope_n] = k;
+ s.scope_n += 1;
+ const c = child orelse {
+ s.missed = true;
+ return;
+ };
+ if (!c.group) s.found = true;
+}
+
/// Help's rows at most: every leaf of every root, and the registry's words.
pub const max_hits = bindings.flat.len + 64;
@@ -377,7 +436,34 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head {
h.spans = b.spans.items;
return h;
},
- .describe => return null,
+ // `Keys`, then the keys replayed: `Keys g ▸ d Def`; struck,
+ // `runs nothing`.
+ .describe => {
+ try b.put(" Keys ");
+ if (s.missed) {
+ var buf: [64]u8 = undefined;
+ const keys = if (s.scope_root) |r| bindings.chord(r, s.scope[0..s.scope_n], &buf) else bindings.keyName(s.scope[0]);
+ try b.styled(keys, .strike);
+ // A key no root is: normal mode's own, no sequence.
+ try b.put(if (s.scope_root == null) " starts no sequence" else " runs nothing");
+ } else if (s.scope_root) |r| {
+ const n = s.scope_n - @intFromBool(s.found);
+ try b.keyCells(r, s.scope[0..n]);
+ if (s.found) {
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const k = s.scope[n];
+ try b.styled("▸", .key);
+ try b.put(" ");
+ try b.styled(bindings.keyName(k), .key);
+ for (bindings.level(p, r, s.scope[0..n], &children)) |c| if (c.key == k) {
+ try b.put(" ");
+ try b.put(c.label);
+ };
+ try b.put(" ⏎ run · esc");
+ } else try b.put("…");
+ } else try b.put("… press a key or a sequence · esc");
+ return b.done();
+ },
}
const cur = current(p, v) orelse return null;
try b.put(" ");
@@ -607,3 +693,71 @@ test "? down a level is Help for that level; Backspace on its empty field goes b
p.update(.{ .key = .{ .cp = Key.escape } });
try std.testing.expectEqual(Kind.rest, s.kind);
}
+
+test "SPC h k is describe-key: g d replays `g ▸ d Def` with its level and doc under the tag; every root answers; Enter runs it; an unbound one runs nothing" {
+ const Key = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 });
+ defer p.deinit();
+ const pane = try p.setTestFile("one\ntwo\nthree\n");
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const a = frame.allocator();
+ for (" hk") |c| p.update(.{ .key = .{ .cp = c } });
+ const s = &p.view().tag_state;
+ try std.testing.expectEqual(Kind.describe, s.kind);
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys …") != null);
+ for ("gd") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expect(s.found);
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys g ▸ d Def") != null);
+ // The level unrolled under the tag, the leaf on the cursor, its doc.
+ try std.testing.expect(pane.band_rows >= 4);
+ _ = try p.render(a);
+ const layer = &p.surface.tag_layers[pardes.BAND_LAYER_BASE + p.view().active];
+ var text: std.ArrayList(u8) = .empty;
+ for (0..layer.rows) |row| for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| try text.appendSlice(a, cell.grapheme());
+ try std.testing.expect(std.mem.indexOf(u8, text.items, "▶ d Def") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text.items, "keys g d") != null);
+ // Another sequence from the next key: ] f, then SPC l r.
+ for ("]f") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys ] ▸ f function") != null);
+ for (" lr") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " Keys ␣ l ▸ r Rename") != null);
+ // Unbound: struck, runs nothing.
+ for ("gq") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expect(s.missed);
+ const struck = (try head(p, a, pane)).?;
+ try std.testing.expect(std.mem.endsWith(u8, struck.text, "g q runs nothing"));
+ try std.testing.expectEqual(Style.strike, struck.spans[0].style);
+ // Enter runs a found one, typed as its keys: g e, the last line.
+ for ("ge") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expect(s.found);
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(Kind.rest, s.kind);
+ try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row);
+ try std.testing.expectEqual(@as(u16, 0), pane.band_rows);
+}
+
+test "C-w's level is window mode's, shown at once and kept while its keys move the outline, until Enter" {
+ const Key = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\n");
+ const top = p.view().active;
+ p.newScratchBelow(top);
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const a = frame.allocator();
+ p.update(.{ .key = .{ .cp = 'w', .ctrl = true } });
+ try std.testing.expect(p.view().window_pick != null);
+ try std.testing.expect(std.mem.endsWith(u8, try testTag(p, a), " C-w …"));
+ p.advance(p.clock.now_ns);
+ p.clock.now_ns = p.view().tag_state.due_ns;
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " C-w ▸ c Collapse d Del h Left j Down k Up l Right") != null);
+ // Sticky: a move keeps the level.
+ p.update(.{ .key = .{ .cp = 'k' } });
+ try std.testing.expect(p.view().window_pick != null);
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " C-w ▸ ") != null);
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expect(p.view().window_pick == null);
+ try std.testing.expect(p.view().tag_state.root == null);
+}
diff --git a/core/builtins.zig b/core/builtins.zig
index a236b0d6..718a0f1f 100644
--- a/core/builtins.zig
+++ b/core/builtins.zig
@@ -1622,6 +1622,19 @@ pub const Help = struct {
}
};
+/// Show what a key sequence runs: the next keys typed are replayed in the tag.
+///
+/// The tag shows `Keys` and the keys as they are typed; a sequence that
+/// reaches a leaf unrolls its level under the tag with the leaf on the
+/// cursor and its documentation beside it, and Enter runs it. Every root
+/// answers: SPC, g, m, z, Z, [, ], window mode's C-w. A sequence nothing
+/// is bound to is shown struck, `runs nothing`. Esc leaves it.
+pub const Keys = struct {
+ pub fn run(c: Ctx) void {
+ pardes.TagState.openDescribe(c.p, c.id);
+ }
+};
+
/// Open the startup file, `init` in the configuration directory, in a pane.
///
/// Its pane when it is open, else the file; not there yet, a pane of that
diff --git a/core/config.zig b/core/config.zig
index 324102b5..b7ffad81 100644
--- a/core/config.zig
+++ b/core/config.zig
@@ -115,6 +115,7 @@ pub const leader_path = paths: {
.Changelog = "hc",
.Build = "hb",
.Messages = "hm",
+ .Keys = "hk",
.Newcol = "cn",
.Delcol = "cd",
.Joincol = "cj",
diff --git a/core/keys.zig b/core/keys.zig
index 6f43e166..58777338 100644
--- a/core/keys.zig
+++ b/core/keys.zig
@@ -552,7 +552,29 @@ fn tagStateKey(p: *Pardes, key: Key) void {
if (TagState.hostOf(p) == null or hit(key, config.escape)) return TagState.toRest(p);
switch (s.kind) {
.rest => unreachable,
- .describe => return TagState.toRest(p),
+ .describe => {
+ const plain = !key.ctrl and !key.alt;
+ if (plain and key.cp == Key.enter and s.found) {
+ var out: [16]Key = undefined;
+ var path: [8]u8 = undefined;
+ const n = s.scope_n;
+ @memcpy(path[0..n], s.scope[0..n]);
+ const typed = bindings.keysOf(s.scope_root.?, path[0..n], &out);
+ TagState.toRest(p);
+ for (typed) |k| handleKey(p, k);
+ return;
+ }
+ if (plain and key.cp == Key.backspace) {
+ if (s.scope_n > 0 and s.scope_root != null) {
+ s.scope_n -= 1;
+ } else s.scope_root = null;
+ if (s.scope_root == null) s.scope_n = 0;
+ s.found = false;
+ s.missed = false;
+ return;
+ }
+ TagState.describeKey(p, key);
+ },
.help => {
const plain = !key.ctrl and !key.alt;
if (plain and key.cp == Key.enter) {
diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json
index 8ec78384..ea406ca1 100644
--- a/docs/typ/builtins-gui.json
+++ b/docs/typ/builtins-gui.json
@@ -57,6 +57,7 @@
{"name": "Joincol", "leader": "SPC c j", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Fold the keyboard's column into the one on its right, keeping its panes.", "text": "Fold the keyboard's column into the one on its right, keeping its panes.\nRefused for the last column, and where the joined column could not give each pane its tag and 2 rows."},
{"name": "JumpTrail", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "A jump of the cursor leaves a short trail that fades, on or off.", "text": "A jump of the cursor leaves a short trail that fades, on or off."},
{"name": "Jumplist", "leader": "SPC j l", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "List the places jumped from in a +Jumps, each a place to go.", "text": "List the places jumped from in a +Jumps, each a place to go."},
+ {"name": "Keys", "leader": "SPC h k", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Show what a key sequence runs: the next keys typed are replayed in the tag.", "text": "Show what a key sequence runs: the next keys typed are replayed in the tag.\nThe tag shows `Keys` and the keys as they are typed; a sequence that reaches a leaf unrolls its level under the tag with the leaf on the cursor and its documentation beside it, and Enter runs it. Every root answers: SPC, g, m, z, Z, [, ], window mode's C-w. A sequence nothing is bound to is shown struck, `runs nothing`. Esc leaves it."},
{"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."},
diff --git a/docs/typ/builtins-tui.json b/docs/typ/builtins-tui.json
index a503c70d..f5b14c2b 100644
--- a/docs/typ/builtins-tui.json
+++ b/docs/typ/builtins-tui.json
@@ -53,6 +53,7 @@
{"name": "Joincol", "leader": "SPC c j", "also": ["topbar"], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Fold the keyboard's column into the one on its right, keeping its panes.", "text": "Fold the keyboard's column into the one on its right, keeping its panes.\nRefused for the last column, and where the joined column could not give each pane its tag and 2 rows."},
{"name": "JumpTrail", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "A jump of the cursor leaves a short trail that fades, on or off.", "text": "A jump of the cursor leaves a short trail that fades, on or off."},
{"name": "Jumplist", "leader": "SPC j l", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "List the places jumped from in a +Jumps, each a place to go.", "text": "List the places jumped from in a +Jumps, each a place to go."},
+ {"name": "Keys", "leader": "SPC h k", "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Show what a key sequence runs: the next keys typed are replayed in the tag.", "text": "Show what a key sequence runs: the next keys typed are replayed in the tag.\nThe tag shows `Keys` and the keys as they are typed; a sequence that reaches a leaf unrolls its level under the tag with the leaf on the cursor and its documentation beside it, and Enter runs it. Every root answers: SPC, g, m, z, Z, [, ], window mode's C-w. A sequence nothing is bound to is shown struck, `runs nothing`. Esc leaves it."},
{"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."},