summaryrefslogtreecommitdiff
path: root/core/keys.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/keys.zig')
-rw-r--r--core/keys.zig81
1 files changed, 74 insertions, 7 deletions
diff --git a/core/keys.zig b/core/keys.zig
index 1a172045..6f43e166 100644
--- a/core/keys.zig
+++ b/core/keys.zig
@@ -195,8 +195,7 @@ 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);
+ 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);
@@ -212,6 +211,8 @@ pub fn handleKey(p: *Pardes, key: Key) void {
}
return;
}
+ // 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);
@@ -512,11 +513,9 @@ 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..v.leader_n]) catch |err|
- p.reportError(.at(v.active), "help", err);
- return;
- }
+ // `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| {
@@ -544,6 +543,70 @@ fn leaderTake(p: *Pardes, c: u8) void {
}
}
+/// 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 => return TagState.toRest(p),
+ .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;
+ @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,
+ }
+ 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
@@ -594,6 +657,10 @@ fn levelKey(p: *Pardes, root: bindings.Root, path: []const u8, key: Key) LevelSt
/// 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 => {