summaryrefslogtreecommitdiff
path: root/core/keys.zig
diff options
context:
space:
mode:
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;
+ },
}
}