summaryrefslogtreecommitdiff
path: root/core/keys.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 19:09:49 -0300
committerGabriel Schneider <[email protected]>2026-11-24 19:20:32 -0300
commitb7d275ab179635f1bf69bdb50833303c6942ff47 (patch)
tree3d7c50dd234e2cdff1bd2e567a400ed4546cc786 /core/keys.zig
parent51766ee8e7c4a3e1f19d1dce4dcc2785a5eb0efb (diff)
downloadpardes-b7d275ab179635f1bf69bdb50833303c6942ff47.tar.gz
pardes-b7d275ab179635f1bf69bdb50833303c6942ff47.zip
TagState, keys: which-key, rolled (walk UI, pick-ui SPEC.md §3.2-§3.4, §3.7, item 2 of line A): a view's tag out of rest is its own (View.tag_state: the root the last key left current, when its level shows on the session's clock, whether it is unrolled, its cursor, the key it missed, the leaf each group ran last; in the dump and the digest, wire 36); the root and its keys are what the core already holds (SPC's leader keys, window mode's outline, the helix prefix the text with the keyboard waits on: TagState.current reads View.prefix and Text.normal.prefix both); a key that strikes a root starts its delay at the next advance, whose clock it is (TagState.asked, as Del's question), and the loop wakes for it (nextWake): SPC writes its key cell at once and shows its level 250 ms on, " and C-w the same (300, 250), g m z Z [ ] change nothing until 500 ms pass with no next key, so gg and gd never touch the tag; the head is computed tag text after the prefix in place of the tag's own text, never written into it (the coordinator's R1: tagline.shownTail, which tagText, sync's wrap and paintPaneTag read; an edit of the tag while it shows is refused): ` ␣ l ▸ a CodeAction b Callstack … ↓ unroll · ? help`, wrapped to three rows at most, keys in the key ink, LSP leaves dimmed in a pane no server speaks for, the leaf its group ran last underlined and said on the leader band's line (`k Hover: … (last used here)`, Messages.leaderText, where the SPC chip was); leaderKey on the binding tree (a level's children, not every builtin's row a key): a group goes down, a leaf runs and is remembered, a key with no child is struck and the level stays (was: the leader left), Backspace goes up a level, Enter runs the cursor's leaf (rolled: the last one run here); Down unrolls (the band is item 3), Up on the first row rolls up, Right/M-l and Left/M-h enter and leave a group; the helix prefixes take the same keys before their own (prefixKey), a key the root binds staying its (z Down scrolls); THE FAULT IT MET: walk.Get's list of a damaged image whose count runs past its elements left the rest undefined (fuzz.zig's "a dump and an image cut", restore seed 781 on this layout: a pane loaded with 256 location rows of 0xAA, freed by a key: integer overflow in SafeAllocator) and the load is now refused (core/walk.zig, after listInto); TESTS: TagState.zig's three (SPC at once then 250 ms, g at 500 ms, gg never; a leaf remembered, underlined, said and run again by Enter; a missed key struck, Backspace up), config.zig's Space-k test (a missed key is struck now, Esc leaves), Messages.zig's two chips (the leader's line); core-test 996 pass, 166 skip, 0 fail
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/keys.zig')
-rw-r--r--core/keys.zig155
1 files changed, 126 insertions, 29 deletions
diff --git a/core/keys.zig b/core/keys.zig
index 9afb983b..1a172045 100644
--- a/core/keys.zig
+++ b/core/keys.zig
@@ -18,6 +18,8 @@ const ctlfs = @import("ninep/tree.zig");
const Macro = @import("Macro.zig");
const Pipe = @import("Pipe.zig");
const Registers = @import("Registers.zig");
+const bindings = @import("bindings.zig");
+const TagState = @import("TagState.zig");
const Pardes = pardes.Pardes;
const Pane = panes.Pane;
const Text = panes.Text;
@@ -191,6 +193,10 @@ pub fn answerQuestion(p: *Pardes, id: usize, choice: ?u8) bool {
}
pub fn handleKey(p: *Pardes, key: Key) void {
+ // A root this key strikes starts its which-key delay; one it ends
+ // takes its level away (TagState).
+ const root_was = if (TagState.current(p, p.view())) |cur| cur.root else null;
+ defer TagState.follow(p, root_was);
// A double-click is two clicks with nothing between them.
p.view().last_click = null;
if (p.macro.recording != 0 and p.macro.replaying == 0) Macro.append(&p.macro.recorded, p.gpa, key) catch |err| p.reportError(.at(p.view().active), "macro", err);
@@ -368,6 +374,9 @@ pub fn handleKey(p: *Pardes, key: Key) void {
if (normal.selRegexArmed(pane)) |a| if (panes.promptTarget(p, pane)) |answered| normal.applySelRegex(p, pane, answered, a.pat, a.mode);
return;
}
+ if (header == null) if (TagState.current(p, p.view())) |cur| if (cur.root != .leader and cur.root != .window) {
+ if (prefixKey(p, t, cur.root, key)) return;
+ };
if (t == &pane.body and pane.jump.items.len > 0) return normal.jumpKey(p, pane, t, key);
// A query pane's line 1 answers Enter, Esc and the stepping keys
// itself (Query.zig).
@@ -475,45 +484,133 @@ pub fn leaderTaken(keys: []const u8) bool {
return keys.len > 0 and keys[0] == config.leader_help;
}
+/// A key in SPC's level: which-key's own (levelKey), else the child it
+/// names. A group goes down a level; a leaf runs and remembers it ran
+/// (TagState.used); a key the level has no child for is struck and the
+/// level stays; any other key (Esc) leaves it.
fn leaderKey(p: *Pardes, key: Key) void {
const v = p.view();
- v.prefix = .none; // only "still a prefix" below re-arms it
- if (key.ctrl or key.alt or key.cp < 0x20 or key.cp > 0x7e) return;
- const c: u8 = @intCast(key.cp);
+ switch (levelKey(p, .leader, v.leader_keys[0..v.leader_n], key)) {
+ .taken => return,
+ .up => {
+ if (v.leader_n == 0) v.prefix = .none else v.leader_n -= 1;
+ v.tag_state.hi = 0;
+ v.tag_state.miss = 0;
+ },
+ .take => |c| leaderTake(p, c),
+ .pass => {
+ if (key.ctrl or key.alt or key.cp < 0x20 or key.cp > 0x7e) {
+ v.prefix = .none;
+ return;
+ }
+ leaderTake(p, @intCast(key.cp));
+ },
+ }
+}
+
+fn leaderTake(p: *Pardes, c: u8) void {
+ const v = p.view();
+ const s = &v.tag_state;
+ v.prefix = .none; // only "still a level" below re-arms it
if (c == config.leader_help) {
- panes.Output.openHelp(p, v.active, v.leader_keys[0..p.view().leader_n]) catch |err|
+ panes.Output.openHelp(p, v.active, v.leader_keys[0..v.leader_n]) catch |err|
p.reportError(.at(v.active), "help", err);
return;
}
- if (v.leader_n >= v.leader_keys.len) return;
- v.leader_keys[v.leader_n] = c;
- v.leader_n += 1;
- const pfx = v.leader_keys[0..p.view().leader_n];
- // A path that runs a builtin with its argument (the query pane's
- // sources with no word of their own).
- for (config.leader_args) |la| if (std.mem.eql(u8, la.path, pfx)) return exec.runBuiltin(p, la.cmd, v.active, "", la.arg);
- for (config.leader_args) |la| if (std.mem.startsWith(u8, la.path, pfx)) {
+ const path = v.leader_keys[0..v.leader_n];
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const child = for (bindings.level(p, .leader, path, &children)) |child| {
+ if (child.key == c) break child;
+ } else {
v.prefix = .leader;
+ s.miss = c;
+ return;
};
- // a path-less builtin is a row here too (it is the same index Help
- // reads), and SPC cannot reach one — so both scans skip it; and a
- // window's own word is no path in a terminal's view
- for (builtin_rows) |row| {
- if (!builtins.registry.here(p, row.cmd)) continue;
- if (std.mem.eql(u8, row.path orelse continue, pfx)) return exec.runBuiltin(p, row.cmd, v.active, "", null);
+ s.miss = 0;
+ if (child.group) {
+ v.prefix = .leader;
+ if (v.leader_n == v.leader_keys.len) return;
+ v.leader_keys[v.leader_n] = c;
+ v.leader_n += 1;
+ s.hi = 0;
+ return;
}
- for (builtin_rows) |row| {
- if (!builtins.registry.here(p, row.cmd)) continue;
- if (std.mem.startsWith(u8, row.path orelse continue, pfx)) {
- v.prefix = .leader;
- return;
- }
+ TagState.ran(v, .leader, path, c);
+ if (child.word) |w| return exec.runWord(p, v.active, w, w.name.slice(), "");
+ switch (child.node.?.leaf.?) {
+ .builtin => |b| exec.runBuiltin(p, b, v.active, "", null),
+ .arg => |a| exec.runBuiltin(p, config.leader_args[a].cmd, v.active, "", config.leader_args[a].arg),
+ .role => unreachable, // SPC's leaves are words
}
- // An addon's word, after the core's (a path the core has is no addon's).
- switch (p.kinds.leader(pfx)) {
- .word => |w| exec.runWord(p, v.active, w, w.name.slice(), ""),
- .prefix => v.prefix = .leader,
- .none => {},
+}
+
+/// What a key does to a which-key level before its root reads it: `up` a
+/// level (Backspace, and Left or Alt-h unrolled), `take` a child (Enter:
+/// the cursor's, or rolled the leaf the group ran last; Right or Alt-l
+/// unrolled: the cursor's group), `taken` (Down unrolls, then moves the
+/// cursor; Up moves it, and on the first row rolls up), or `pass`: the
+/// root's own key. A key a root binds is its own (helix's z Down scrolls).
+const LevelStep = union(enum) { pass, taken, up, take: u8 };
+
+fn levelKey(p: *Pardes, root: bindings.Root, path: []const u8, key: Key) LevelStep {
+ const s = &p.view().tag_state;
+ var buf: [bindings.max_children]bindings.Child = undefined;
+ const children = bindings.level(p, root, path, &buf);
+ if (children.len == 0) return .pass;
+ const node = bindings.nodeAt(root, path);
+ const used = if (node) |n| s.used[n.group] else 0;
+ const plain = !key.ctrl and !key.alt;
+ const down = (plain and key.cp == Key.down) or (key.ctrl and !key.alt and key.cp == 'j');
+ const up = (plain and key.cp == Key.up) or (key.ctrl and !key.alt and key.cp == 'k');
+ const right = (plain and key.cp == Key.right) or (key.alt and !key.ctrl and key.cp == 'l');
+ const left = (plain and key.cp == Key.left) or (key.alt and !key.ctrl and key.cp == 'h');
+ if ((root == .view or root == .view_sticky) and plain and (key.cp == Key.down or key.cp == Key.up)) return .pass;
+ if (plain and key.cp == Key.backspace) return .up;
+ // The cursor: its child, else the leaf the group ran last, else the first.
+ var at: usize = 0;
+ for (children, 0..) |c, i| if (c.key == (if (s.hi != 0) s.hi else used)) {
+ at = i;
+ };
+ if (down) {
+ if (!s.unrolled) {
+ s.unrolled = true;
+ } else at = (at + 1) % children.len;
+ s.hi = children[at].key;
+ return .taken;
+ }
+ if (up) {
+ if (s.unrolled and at == 0) s.unrolled = false else if (s.unrolled) s.hi = children[at - 1].key;
+ return .taken;
+ }
+ if (s.unrolled and right) return if (children[at].group) .{ .take = children[at].key } else .taken;
+ if (s.unrolled and left) return .up;
+ if (plain and key.cp == Key.enter) {
+ if (s.unrolled) return .{ .take = children[at].key };
+ return if (used != 0) .{ .take = used } else .taken;
+ }
+ return .pass;
+}
+
+/// A key while a helix prefix waits in text `t` (g m z Z [ ]): which-key's
+/// own, else the prefix's, the leaf it runs remembered. True when taken here.
+fn prefixKey(p: *Pardes, t: *Text, root: bindings.Root, key: Key) bool {
+ switch (levelKey(p, root, "", key)) {
+ .taken => return true,
+ .up => {
+ t.normal = .{};
+ return true;
+ },
+ .take => |c| {
+ handleKey(p, .{ .cp = c });
+ return true;
+ },
+ .pass => {
+ if (!key.ctrl and !key.alt and key.cp >= 0x20 and key.cp < 0x7f) {
+ const k: u8 = @intCast(key.cp);
+ if (bindings.nodeAt(root, &.{k}) != null) TagState.ran(p.view(), root, "", k);
+ }
+ return false;
+ },
}
}