diff options
Diffstat (limited to 'core/keys.zig')
| -rw-r--r-- | core/keys.zig | 249 |
1 files changed, 218 insertions, 31 deletions
diff --git a/core/keys.zig b/core/keys.zig index 68e3585d..c30ef233 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,9 @@ 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). + defer TagState.follow(p); // 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); @@ -223,6 +228,8 @@ pub fn handleKey(p: *Pardes, key: Key) void { return; } p.view().answering = 0; + // A tag in a typing state takes every key (TagState: Help, describe-key). + if (p.view().tag_state.kind != .rest) return tagStateKey(p, key); if (p.view().window_pick != null) return layout.windowKey(p, key); const pane = p.panes[p.view().active] orelse return; layout.noteTyped(p); @@ -385,6 +392,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). @@ -492,45 +502,222 @@ 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); - if (c == config.leader_help) { - panes.Output.openHelp(p, v.active, v.leader_keys[0..p.view().leader_n]) catch |err| - p.reportError(.at(v.active), "help", err); - return; + 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)); + }, } - 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)) { +} + +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 + // `SPC ?`: Help over every root's leaves; `?` down a level, Help for + // that level's (SPEC §4.5). + if (c == config.leader_help) return TagState.openHelp(p, if (v.leader_n == 0) null else .leader, v.leader_keys[0..v.leader_n]); + 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)) { + 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 + } +} + +/// A key in a tag's typing state: Esc back to rest; Enter runs the row, +/// typed as its keys; the rows' cursor; the field; Backspace on an empty +/// field back to the level `?` was struck from. +fn tagStateKey(p: *Pardes, key: Key) void { + const v = p.view(); + const s = &v.tag_state; + if (TagState.hostOf(p) == null or hit(key, config.escape)) return TagState.toRest(p); + switch (s.kind) { + .rest => unreachable, + .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); + }, + .pick => return @import("Pick.zig").key(p, key), + .help => { + const plain = !key.ctrl and !key.alt; + if (plain and key.cp == Key.enter) { + if (s.nhits == 0) return; + var buf: [TagState.row_max]u8 = undefined; + const leaf = TagState.helpLeaf(p, s.hits[s.rows.at], &buf); + var out: [16]Key = undefined; + const typed = bindings.keysOf(leaf.root, leaf.path, &out); + TagState.toRest(p); + for (typed) |k| handleKey(p, k); + return; + } + if (plain and key.cp == Key.backspace and s.field.len == 0) return helpBack(p); + if (s.rows.key(key, s.nhits)) return; + if (s.field.key(key)) TagState.filter(p); + }, + } +} + +/// Help left by Backspace on its empty field: the level it was struck +/// from, unrolled, as `?` found it; rest from `SPC ?`. +fn helpBack(p: *Pardes) void { + const v = p.view(); + const s = &v.tag_state; + const root = s.scope_root orelse .leader; + var path: [4]u8 = undefined; + const n = s.scope_n; + @memcpy(path[0..n], s.scope[0..n]); + TagState.toRest(p); + const pane = p.panes[v.active] orelse return; + switch (root) { + .leader => { v.prefix = .leader; - return; - } + @memcpy(v.leader_keys[0..n], path[0..n]); + v.leader_n = n; + }, + .goto, .match, .view, .view_sticky, .next, .prev => { + const t = if (pane.focus == .tag) &pane.tag else &pane.body; + t.normal.prefix = switch (root) { + .goto => .goto_prefix, + .match => .match_prefix, + .view, .view_sticky => .view_prefix, + .next => .next_prefix, + .prev => .prev_prefix, + else => unreachable, + }; + t.normal.sticky = root == .view_sticky; + }, + .window, .register => return, } - // 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 => {}, + s.root = root; + s.unrolled = true; +} + +/// 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 { + if (root != .register and key.cp == config.leader_help and !key.ctrl and !key.alt) { + TagState.openHelp(p, root, ""); + return true; + } + 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; + }, } } |
