diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Band.zig | 24 | ||||
| -rw-r--r-- | core/TagState.zig | 158 | ||||
| -rw-r--r-- | core/builtins.zig | 13 | ||||
| -rw-r--r-- | core/config.zig | 1 | ||||
| -rw-r--r-- | core/keys.zig | 24 |
5 files changed, 215 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) { |
