diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-24 19:31:05 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-24 19:31:05 -0300 |
| commit | f44d1b7d040acf674814be3cde336527833450e9 (patch) | |
| tree | 9c22c45541f16e4281a0a504084e3cf6e389d1c8 | |
| parent | 326a2bac9558da0ff78987e697546a363b2b8e59 (diff) | |
| download | pardes-f44d1b7d040acf674814be3cde336527833450e9.tar.gz pardes-f44d1b7d040acf674814be3cde336527833450e9.zip | |
TagState, Field, Band: the typing state, and Help over the binding tree (walk UI, pick-ui SPEC.md §3.2's typing row and §4.5, item 4 of line A): a tag's typing state is the view's (TagState.kind help, its host pane by serial, the hostOf every tag's text asks), its box `^` whatever the body's mode, its caret a bar at the field's (the body's caret and its layer's put by while the head is typed in), and it takes every key before anything but a question (keys.zig tagStateKey: Esc back to rest, Enter runs the row typed as its keys, Backspace on an empty field back to the level `?` was struck from, unrolled); SPC ? is Help over every leaf of every root (SPC's, g m z Z [ ]'s, window mode's, the addons' words) and `?` down a level or under a helix prefix Help for that level's (SPC l ?, g ?); its head ` Help ␣ l <field> n/N`; its rows `chord · word · group` scored by the query pane's scorer (Names.match, now pub) on each key into the view's fixed hits (max_hits: the tree's 160 leaves and the registry's 64 words; best first, the tree's order while the field is empty), the band shows eleven around the cursor, the query's bytes in the match tint, the cursor's leaf's doc beside them; `Help <name>` stays the word's +Help; FOR THE PICKERS AND PROMPTS, as they are: Field (core/Field.zig: a one-line field in a fixed 256-byte buffer with its caret, each view's in TagState.field; Field.key takes a line editor's keys: characters, Backspace/C-h, Delete/C-d, Left/C-b, Right/C-f, Home/C-a, End/C-e, C-u, C-k, C-w/M-Backspace, M-b, M-f; false for a key it has no use for, Enter, Esc, the arrows, which its owner takes; set, insert, clear) and Band.Cursor (a list's cursor, TagState.rows: Cursor.key steps it on Down/C-j/C-n, Up/C-k/C-p, PageDown/PageUp; Cursor.window(n) is the eleven rows [lo, hi) a band shows around it); a state's head is a TagState.Head {text, spans, caret}, its band Band.of's switch on TagState.kind; TagState.follow takes no argument now (a root a state puts back is current already: Help's Backspace keeps its level unrolled); TESTS: Field.zig's (a line editor's keys, and the keys it leaves), TagState.zig's two (SPC ? over every root, filtered, the box and the caret, Enter runs Wrap; SPC l ? and g ? scoped, Backspace back to the level unrolled, Esc to rest); core-test 1003 pass, 166 skip, 0 fail
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| -rw-r--r-- | core/Band.zig | 87 | ||||
| -rw-r--r-- | core/Field.zig | 142 | ||||
| -rw-r--r-- | core/Names.zig | 2 | ||||
| -rw-r--r-- | core/TagState.zig | 218 | ||||
| -rw-r--r-- | core/draw.zig | 20 | ||||
| -rw-r--r-- | core/keys.zig | 81 | ||||
| -rw-r--r-- | core/root.zig | 1 |
7 files changed, 533 insertions, 18 deletions
diff --git a/core/Band.zig b/core/Band.zig index 0e67bcf4..079a1bcc 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -26,6 +26,7 @@ const modal = @import("modal.zig"); const panes = @import("panes.zig"); const builtins = @import("builtins.zig"); const TagState = @import("TagState.zig"); +const Names = @import("Names.zig"); const Pardes = pardes.Pardes; const Pane = pardes.Pane; const File = panes.File; @@ -83,9 +84,13 @@ columns: u16 = 0, pub fn takes(p: *Pardes, id: usize) u16 { const v = p.view(); const s = &v.tag_state; - if (id != v.active) return 0; - if (s.kind != .rest) return 0; - if (!s.unrolled or TagState.hostOf(p) != id) return 0; + if (TagState.hostOf(p) != id) return 0; + switch (s.kind) { + .rest => {}, + .help => return @max(doc_rows, @max(1, @min(Cursor.shown, s.nhits))), + .describe => return 0, + } + if (!s.unrolled) return 0; const cur = TagState.current(p, v) orelse return 0; var children: [bindings.max_children]bindings.Child = undefined; const n: u16 = @intCast(bindings.level(p, cur.root, cur.path, &children).len); @@ -103,8 +108,17 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { if (rows == 0) return null; const v = p.view(); const s = &v.tag_state; - const cur = TagState.current(p, v).?; var band: Band = .{ .height = rows, .columns = layout.tagCapacity(p, p.view().rects[id].w -| pardes.TAG_TEXT_INSET) }; + switch (s.kind) { + .rest => {}, + .help => { + try band.helpSteps(p, arena, s); + try band.place(p, arena, pane, id); + return band; + }, + .describe => return null, + } + const cur = TagState.current(p, v).?; // The level, its cursor on the child it names, else the leaf its group // ran last, else the first; and that child's doc beside it. const node = bindings.nodeAt(cur.root, cur.path); @@ -121,6 +135,71 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { return band; } +/// Help's rows (`chord · word · group`, the query's bytes in the match +/// tint) around the cursor, and the cursor's leaf's doc beside them. +fn helpSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, s: *const TagState) !void { + if (s.nhits == 0) { + const none = try arena.alloc(Row, 1); + none[0] = .{ .text = if (s.field.len == 0) "no keys here" else "no key matches", .dim = true }; + band.add(.{ .rows = none }); + return; + } + const w = s.rows.window(s.nhits); + const rows = try arena.alloc(Row, w.hi - w.lo); + const q = s.field.text(); + for (rows, w.lo..) |*row, i| { + const buf = try arena.create([TagState.row_max]u8); + const leaf = TagState.helpLeaf(p, s.hits[i], buf); + var marks: [Names.max_query * 4]u16 = undefined; + var nmarks: usize = 0; + if (std.mem.trim(u8, q, " ").len > 0) { + const low = try std.ascii.allocLowerString(arena, leaf.row); + _ = Names.match(q, leaf.row, low, 0, &marks, &nmarks); + } + row.* = .{ .text = leaf.row, .on = i == s.rows.at, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .dim = bindings.dimmed(p.panes[p.view().active].?, leaf.child) }; + } + band.add(.{ .rows = rows }); + var buf: [TagState.row_max]u8 = undefined; + const at = TagState.helpLeaf(p, s.hits[s.rows.at], &buf); + band.add(try docStep(p, arena, at.root, at.path[0 .. at.path.len - 1], at.child)); +} + +/// A list's cursor and the rows of it a band shows: eleven at most, around +/// the cursor (SPEC §3.6, the prototype's `H.window`). Each view's (a +/// tag state's field): Help's rows, a picker's, a prompt's history. +pub const Cursor = struct { + at: u16 = 0, + + pub const shown = 11; + + /// Rows [lo, hi) of `n` the band shows. + pub fn window(c: Cursor, n: u16) struct { lo: u16, hi: u16 } { + const lo = @min(c.at -| shown / 2, n -| shown); + return .{ .lo = lo, .hi = @min(n, lo + shown) }; + } + + /// A key that steps a list of `n`: Down, C-j, C-n a row on, Up, C-k, + /// C-p a row back, PageDown and PageUp ten; true when taken. + pub fn key(c: *Cursor, k: pardes.Key, n: u16) bool { + const Key = pardes.Key; + const plain = !k.ctrl and !k.alt; + const ctrl = k.ctrl and !k.alt; + const step: i32 = if (plain and k.cp == Key.down or ctrl and (k.cp == 'j' or k.cp == 'n')) + 1 + else if (plain and k.cp == Key.up or ctrl and (k.cp == 'k' or k.cp == 'p')) + -1 + else if (plain and k.cp == Key.page_down) + 10 + else if (plain and k.cp == Key.page_up) + -10 + else + return false; + if (n == 0) return true; + c.at = @intCast(std.math.clamp(@as(i32, c.at) + step, 0, @as(i32, n) - 1)); + return true; + } +}; + fn add(band: *Band, step: Step) void { if (band.nsteps == max_steps) return; band.steps[band.nsteps] = step; diff --git a/core/Field.zig b/core/Field.zig new file mode 100644 index 00000000..7b8ea399 --- /dev/null +++ b/core/Field.zig @@ -0,0 +1,142 @@ +//! A field typed in a tag's head (walk UI, pick-ui SPEC.md §3.2, the typing +//! row): one line of text and its caret, in a fixed buffer, each view's +//! own (TagState.field). Help's query, a picker's, a prompt's: the keys +//! that edit it are a line editor's (readline's, helix's prompt's), and +//! the state that owns it takes the rest (Enter, Esc, the arrows a list +//! steps with). +const Field = @This(); + +const std = @import("std"); +const pardes = @import("root.zig"); +const modal = @import("modal.zig"); +const Key = pardes.Key; + +/// The longest field, in bytes: a query, a command line, a pattern. +pub const max = 256; + +buf: [max]u8 = undefined, +len: u16 = 0, +/// A byte offset in the text, at a character's start. +caret: u16 = 0, + +pub const replica = .{ .len = .{.{ "buf", "len" }} }; + +pub fn text(f: *const Field) []const u8 { + return f.buf[0..f.len]; +} + +pub fn clear(f: *Field) void { + f.len = 0; + f.caret = 0; +} + +/// `s` in place of the text, the caret at its end (a history's line). +pub fn set(f: *Field, s: []const u8) void { + var n = @min(s.len, max); + while (n > 0 and n < s.len and s[n] & 0xC0 == 0x80) n -= 1; + @memcpy(f.buf[0..n], s[0..n]); + f.len = @intCast(n); + f.caret = f.len; +} + +/// An editing key taken: true, the text or the caret moved (or a key that +/// would have, at an edge). False for a key the field has no use for, +/// which its owner takes. +pub fn key(f: *Field, k: Key) bool { + const t = f.text(); + const ctrl = k.ctrl and !k.alt; + const alt = k.alt and !k.ctrl; + const plain = !k.ctrl and !k.alt; + if (plain and k.cp == Key.backspace or ctrl and k.cp == 'h') { + if (f.caret > 0) f.cut(prev(t, f.caret), f.caret); + } else if (plain and k.cp == Key.delete or ctrl and k.cp == 'd') { + if (f.caret < f.len) f.cut(f.caret, @intCast(modal.nextGrapheme(t, f.caret))); + } else if (plain and k.cp == Key.left or ctrl and k.cp == 'b') { + f.caret = prev(t, f.caret); + } else if (plain and k.cp == Key.right or ctrl and k.cp == 'f') { + if (f.caret < f.len) f.caret = @intCast(modal.nextGrapheme(t, f.caret)); + } else if (plain and k.cp == Key.home or ctrl and k.cp == 'a') { + f.caret = 0; + } else if (plain and k.cp == Key.end or ctrl and k.cp == 'e') { + f.caret = f.len; + } else if (ctrl and k.cp == 'u') { + f.cut(0, f.caret); + } else if (ctrl and k.cp == 'k') { + f.cut(f.caret, f.len); + } else if (ctrl and k.cp == 'w' or alt and k.cp == Key.backspace) { + f.cut(wordBack(t, f.caret), f.caret); + } else if (alt and k.cp == 'b') { + f.caret = wordBack(t, f.caret); + } else if (alt and k.cp == 'f') { + f.caret = wordOn(t, f.caret); + } else if (plain and k.cp >= 0x20 and k.cp != Key.backspace and k.cp < 0xF0000) { + var utf: [4]u8 = undefined; + const n = std.unicode.utf8Encode(k.cp, &utf) catch return true; + f.insert(if (k.text.len > 0) k.text else utf[0..n]); + } else return false; + return true; +} + +/// `s` typed at the caret; what does not fit is dropped whole. +pub fn insert(f: *Field, s: []const u8) void { + if (f.len + s.len > max) return; + std.mem.copyBackwards(u8, f.buf[f.caret + s.len .. f.len + s.len], f.buf[f.caret..f.len]); + @memcpy(f.buf[f.caret..][0..s.len], s); + f.len += @intCast(s.len); + f.caret += @intCast(s.len); +} + +fn cut(f: *Field, lo: u16, hi: u16) void { + std.mem.copyForwards(u8, f.buf[lo .. f.len - (hi - lo)], f.buf[hi..f.len]); + f.len -= hi - lo; + f.caret = lo; +} + +fn prev(t: []const u8, at: u16) u16 { + var i = at; + while (i > 0) { + i -= 1; + if (t[i] & 0xC0 != 0x80) break; + } + return i; +} + +fn wordBack(t: []const u8, at: u16) u16 { + var i = at; + while (i > 0 and t[i - 1] == ' ') i -= 1; + while (i > 0 and t[i - 1] != ' ') i -= 1; + return i; +} + +fn wordOn(t: []const u8, at: u16) u16 { + var i = at; + while (i < t.len and t[i] == ' ') i += 1; + while (i < t.len and t[i] != ' ') i += 1; + return i; +} + +test "a field types, moves and deletes as a line editor does" { + var f: Field = .{}; + for ("find me") |c| try std.testing.expect(f.key(.{ .cp = c })); + try std.testing.expectEqualStrings("find me", f.text()); + try std.testing.expect(f.key(.{ .cp = 'w', .ctrl = true })); + try std.testing.expectEqualStrings("find ", f.text()); + try std.testing.expect(f.key(.{ .cp = Key.home })); + try std.testing.expect(f.key(.{ .cp = '>' })); + try std.testing.expectEqualStrings(">find ", f.text()); + try std.testing.expectEqual(@as(u16, 1), f.caret); + try std.testing.expect(f.key(.{ .cp = Key.backspace })); + try std.testing.expectEqualStrings("find ", f.text()); + try std.testing.expect(f.key(.{ .cp = 0x65e5 })); // 日 + try std.testing.expect(f.key(.{ .cp = Key.left })); + try std.testing.expectEqual(@as(u16, 0), f.caret); + try std.testing.expect(f.key(.{ .cp = Key.delete })); + try std.testing.expectEqualStrings("find ", f.text()); + try std.testing.expect(f.key(.{ .cp = 'k', .ctrl = true })); + try std.testing.expectEqualStrings("", f.text()); + // Enter, Esc and the arrows a list steps with are its owner's. + try std.testing.expect(!f.key(.{ .cp = Key.enter })); + try std.testing.expect(!f.key(.{ .cp = Key.escape })); + try std.testing.expect(!f.key(.{ .cp = Key.down })); + try std.testing.expect(!f.key(.{ .cp = 'n', .ctrl = true })); +} diff --git a/core/Names.zig b/core/Names.zig index 2fbbec37..47165f52 100644 --- a/core/Names.zig +++ b/core/Names.zig @@ -233,7 +233,7 @@ fn bonusAt(text: []const u8, j: usize) i32 { /// `text`. Terms split on spaces are ANDed; smart case per term. With /// `marks`, the bytes matched are written there, ascending (the table they /// are backtracked through is `dp_trace`). -fn match(query: []const u8, text_in: []const u8, lower_in: []const u8, base: usize, marks: ?[]u16, marks_len: ?*usize) ?i32 { +pub fn match(query: []const u8, text_in: []const u8, lower_in: []const u8, base: usize, marks: ?[]u16, marks_len: ?*usize) ?i32 { const text = text_in[0..@min(text_in.len, max_text)]; const lower = lower_in[0..text.len]; var total: i32 = 0; diff --git a/core/TagState.zig b/core/TagState.zig index daf05b98..56c715af 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -21,10 +21,37 @@ const Pardes = pardes.Pardes; const View = Pardes.View; const Pane = pardes.Pane; const Root = bindings.Root; +const Field = @import("Field.zig"); +const Band = @import("Band.zig"); +const Names = @import("Names.zig"); +/// The tag's states besides a root's level (which `kind` rest with a root +/// current is): `help`, a field over every leaf (SPEC §4.5); `describe`, +/// a sequence replayed in the tree (item 5). pub const Kind = enum(u8) { rest, help, describe }; +pub const replica = .{ .len = .{ .{ "scope", "scope_n" }, .{ "hits", "nhits" } } }; + kind: Kind = .rest, +/// The pane whose tag a typing state shows in, by serial: the one the +/// keyboard was on when it began (a picker's is the pane its Enter lands +/// in, SPEC §3.5). +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_n: u8 = 0, +/// The field typed in the head (Field.zig): Help's query; a picker's, a +/// prompt's. +field: Field = .{}, +/// Help's rows, best first: an index of bindings.flat, or past it an +/// addon's word (`flat.len` + its slot in the registry); `ntotal` in scope. +hits: [max_hits]u16 = undefined, +nhits: u16 = 0, +ntotal: u16 = 0, +/// The rows' cursor (Band.Cursor). +rows: Band.Cursor = .{}, /// The root the last key left current, whose delay `due_ns` counts. root: ?Root = null, /// When its level shows, on the session's clock: `asked` from its key @@ -44,6 +71,9 @@ 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, +/// Help's rows at most: every leaf of every root, and the registry's words. +pub const max_hits = bindings.flat.len + 64; + /// A root struck, its time not had yet (as Pane.question_asked: the step /// that strikes it reads no clock). pub const asked: u64 = std.math.maxInt(u64); @@ -86,7 +116,9 @@ pub fn shown(p: *const Pardes, v: *const View) bool { pub fn hostOf(p: *const Pardes) ?usize { const v = p.view(); if (v.tag_state.kind == .rest and v.prefix == .none and v.window_pick == null and !textPrefix(p, v)) return null; - if (v.tag_state.kind != .rest) return null; + if (v.tag_state.kind != .rest) return for (p.panes, 0..) |slot, i| { + if (slot) |pane| if (pane.serial == v.tag_state.host) break i; + } else null; const cur = current(p, v) orelse return null; if (!bindings.info.get(cur.root).at_once and !shown(p, v)) return null; return if (p.panes[v.active] != null) v.active else null; @@ -98,12 +130,13 @@ fn textPrefix(p: *const Pardes, v: *const View) bool { } /// After a key (keys.handleKey): a root newly current starts its delay; one -/// gone leaves the level as it was never there. -pub fn follow(p: *Pardes, before: ?Root) void { +/// gone leaves the level as it was never there. A root a state put back +/// (Help's Backspace) is current already, its level as that state left it. +pub fn follow(p: *Pardes) void { const v = p.view(); const s = &v.tag_state; const now = if (current(p, v)) |cur| cur.root else null; - if (now == before and now == s.root) return; + if (now == s.root) return; s.root = now; s.unrolled = false; s.hi = 0; @@ -123,6 +156,7 @@ pub fn toRest(p: *Pardes) void { } const s = &v.tag_state; s.kind = .rest; + s.previewing = .empty; s.root = null; s.due_ns = 0; s.unrolled = false; @@ -184,6 +218,99 @@ pub fn ran(v: *View, root: Root, path: []const u8, key: u8) void { v.tag_state.used[node.group] = key; } +/// Help over the tree (SPEC §4.5), in the tag of the pane with the +/// keyboard: its field empty, its rows every leaf `root` and `path` lead +/// to, or with no root every leaf of every root. The level it was struck +/// from goes. +pub fn openHelp(p: *Pardes, root: ?Root, path: []const u8) void { + const v = p.view(); + const pane = p.panes[v.active] orelse return; + toRest(p); + const s = &v.tag_state; + s.kind = .help; + s.host = pane.serial; + s.scope_root = root; + s.scope_n = @intCast(@min(path.len, s.scope.len)); + @memcpy(s.scope[0..s.scope_n], path[0..s.scope_n]); + s.field.clear(); + s.rows = .{}; + filter(p); +} + +/// Help's rows again for its field: the leaves in its scope this view +/// takes, scored by the query pane's scorer (Names.match) and best first, +/// or in the tree's order while the field is empty. Each key's, over the +/// tree's leaves and the registry's words: a fixed count (max_hits). +pub fn filter(p: *Pardes) void { + const s = &p.view().tag_state; + const q = s.field.text(); + const empty = std.mem.trim(u8, q, " ").len == 0; + var found: [max_hits]struct { at: u16, score: i32 } = undefined; + var n: usize = 0; + var total: u16 = 0; + for (bindings.flat, 0..) |f, i| { + if (!inScope(s, f.root, f.path)) continue; + if (f.node.leaf.? == .builtin and !pardes.builtins.registry.here(p, f.node.leaf.?.builtin)) continue; + total += 1; + const score = if (empty) 0 else Names.match(q, f.row, f.lower, 0, null, null) orelse continue; + found[n] = .{ .at = @intCast(i), .score = score }; + n += 1; + } + for (p.kinds.words[0..p.kinds.nwords], 0..) |*w, k| { + if (w.leader.len == 0 or !inScope(s, .leader, w.leader.slice())) continue; + total += 1; + var buf: [row_max]u8 = undefined; + var low: [row_max]u8 = undefined; + const row = wordRow(w, &buf); + for (row, 0..) |c, i| low[i] = std.ascii.toLower(c); + const score = if (empty) 0 else Names.match(q, row, low[0..row.len], 0, null, null) orelse continue; + found[n] = .{ .at = @intCast(bindings.flat.len + k), .score = score }; + n += 1; + } + const Found = @TypeOf(found[0]); + if (!empty) std.sort.pdq(Found, found[0..n], {}, struct { + fn lt(_: void, a: Found, b: Found) bool { + return a.score > b.score or (a.score == b.score and a.at < b.at); + } + }.lt); + for (found[0..n], 0..) |f, i| s.hits[i] = f.at; + s.nhits = @intCast(n); + s.ntotal = total; + s.rows.at = 0; +} + +fn inScope(s: *const TagState, root: Root, path: []const u8) bool { + const r = s.scope_root orelse return true; + return r == root and std.mem.startsWith(u8, path, s.scope[0..s.scope_n]); +} + +/// The longest row Help makes of an addon's word: SPC, eight keys, a +/// 32-byte name. +pub const row_max = 80; + +/// An addon's word as Help's row: `SPC x y · Name · addon`. +fn wordRow(w: *const pardes.Kind.Word, buf: *[row_max]u8) []const u8 { + var out = std.Io.Writer.fixed(buf); + out.writeAll("SPC") catch {}; + for (w.leader.slice()) |k| out.print(" {s}", .{bindings.keyName(k)}) catch {}; + out.print(" · {s} · addon", .{w.name.slice()}) catch {}; + return out.buffered(); +} + +/// Help's hit `at` as a leaf: its row, its root and keys, and the child +/// its level shows it as (its doc's). +pub const Leaf = struct { row: []const u8, root: Root, path: []const u8, child: bindings.Child }; + +pub fn helpLeaf(p: *const Pardes, at: u16, buf: *[row_max]u8) Leaf { + if (at < bindings.flat.len) { + const f = bindings.flat[at]; + return .{ .row = f.row, .root = f.root, .path = f.path, .child = .{ .key = f.node.key, .label = f.node.label, .node = f.node, .lsp = f.node.lsp } }; + } + const w = &p.kinds.words[at - bindings.flat.len]; + const keys = w.leader.slice(); + return .{ .row = wordRow(w, buf), .root = .leader, .path = keys, .child = .{ .key = keys[keys.len - 1], .label = w.name.slice(), .word = w } }; +} + /// How a span of the head is drawn (draw.zig paintPaneTag). pub const Style = enum { key, dim, under, strike }; pub const Span = struct { lo: u32, hi: u32, style: Style }; @@ -234,8 +361,25 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { if (p.panes[id] != pane) return null; const v = p.view(); const s = &v.tag_state; - const cur = current(p, v) orelse return null; var b: Builder = .{ .arena = arena }; + switch (s.kind) { + .rest => {}, + // `Help`, its scope's keys, the field, its count: + // ` Help ␣ l hov 2/17`. + .help => { + try b.put(" Help "); + if (s.scope_root) |r| try b.keyCells(r, s.scope[0..s.scope_n]); + var h = b.done(); + h.caret = @intCast(b.text.items.len + s.field.caret); + try b.put(s.field.text()); + try b.text.print(arena, " {d}/{d}", .{ s.nhits, s.ntotal }); + h.text = b.text.items; + h.spans = b.spans.items; + return h; + }, + .describe => return null, + } + const cur = current(p, v) orelse return null; try b.put(" "); try b.keyCells(cur.root, cur.path); if (!shown(p, v)) { @@ -399,3 +543,67 @@ test "a key the level has no child for is struck and the level stays; Backspace p.update(.{ .key = .{ .cp = Key.backspace } }); try std.testing.expect(p.view().prefix == .none); } + +test "SPC ? is Help over every root's leaves: typed in its head, filtered by the scorer, its rows and the cursor's doc in the band; Enter runs the row" { + 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\n"); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + p.update(.{ .key = .{ .cp = ' ' } }); + p.update(.{ .key = .{ .cp = '?' } }); + const s = &p.view().tag_state; + try std.testing.expectEqual(Kind.help, s.kind); + try std.testing.expect(p.view().prefix == .none); + // Every leaf of every root: SPC's, the helix prefixes', window mode's. + try std.testing.expect(s.nhits == s.ntotal and s.ntotal > 100); + for ("wrap") |c| p.update(.{ .key = .{ .cp = c } }); + try std.testing.expectEqualStrings("wrap", s.field.text()); + try std.testing.expect(s.nhits > 0 and s.nhits < s.ntotal); + var buf: [row_max]u8 = undefined; + try std.testing.expectEqualStrings("SPC t w · Wrap · view", helpLeaf(p, s.hits[0], &buf).row); + const tag = try testTag(p, a); + try std.testing.expect(std.mem.indexOf(u8, tag, " Help wrap ") != null); + // Its box says it is typed in; the band holds its rows and a doc. + _ = try p.render(a); + const r = p.view().rects[p.view().active]; + try std.testing.expectEqualStrings("^", p.surface.at(r.x, r.y).grapheme()); + try std.testing.expect(pane.band_rows >= 4); + try std.testing.expect(p.surface.cursor != null and p.surface.cursor.?.bar); + // Enter runs it, typed as its keys, and the tag is back at rest. + const wrapped = p.settings.wrap; + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(p.settings.wrap != wrapped); + try std.testing.expectEqual(Kind.rest, s.kind); + try std.testing.expectEqual(@as(u16, 0), pane.band_rows); +} + +test "? down a level is Help for that level; Backspace on its empty field goes back to the level, Esc to rest" { + 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"); + for (" l?") |c| p.update(.{ .key = .{ .cp = c } }); + const s = &p.view().tag_state; + try std.testing.expectEqual(Kind.help, s.kind); + var buf: [row_max]u8 = undefined; + for (s.hits[0..s.nhits]) |at| try std.testing.expect(std.mem.startsWith(u8, helpLeaf(p, at, &buf).row, "SPC l ")); + try std.testing.expect(s.nhits >= 15); + // g ?: the goto level's. + p.update(.{ .key = .{ .cp = Key.backspace } }); + try std.testing.expectEqual(Kind.rest, s.kind); + try std.testing.expect(p.view().prefix == .leader and s.unrolled); + try std.testing.expectEqualStrings("l", p.view().leader_keys[0..p.view().leader_n]); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(p.view().prefix == .none); + for ("g?") |c| p.update(.{ .key = .{ .cp = c } }); + try std.testing.expectEqual(Kind.help, s.kind); + for (s.hits[0..s.nhits]) |at| try std.testing.expect(std.mem.startsWith(u8, helpLeaf(p, at, &buf).row, "g ")); + // Down moves the cursor; Enter runs `g g`. + const pane = p.panes[p.view().active].?; + pane.body.cur_row = 0; + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(Kind.rest, s.kind); +} diff --git a/core/draw.zig b/core/draw.zig index 37fdee1e..772cbece 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -488,13 +488,18 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: }); // In window mode the box holds the pane's letter instead of its mode. const label: ?u8 = if (p.view().window_pick != null) layout.windowLabel(p, id) else null; - s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else switch (pane.body.mode) { + // A tag in a typing state is typed in: its box says so (walk UI SPEC + // §3.2), whatever the body's mode. + const typing = p.view().tag_state.kind != .rest and TagState.hostOf(p) == id; + s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else if (typing) config.tag_insert else switch (pane.body.mode) { .normal => config.tag_normal, .insert => config.tag_insert, .tty => config.tag_tty, }, box_style); try joinPaneTag(p, s, arena, pane, r, id, active); + // The keyboard is in the head's field: its caret, not the body's. + const head_caret = if (typing) s.cursor else null; if (pane.band_rows > 0) try joinBand(p, s, arena, pane, r, id, active); // Folded panes retain their body state, but neither body pixels nor @@ -530,6 +535,10 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: } try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*); + if (typing) { + s.body_layers[id].cursor = null; + s.cursor = head_caret; + } if (pane.layers.heat > 0) @import("jj.zig").railHeat(p, s, pane, id, rail.rect); // A terminal panned to its cursor in a view shorter than it: an arrow // in the gutter's first body row says rows are above (views.zig). @@ -1003,6 +1012,15 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, } } } + // A head typed in: its caret a bar at the field's. + if (head) |h| if (h.caret) |c| if (active) { + const at = modal.positionAt(shown.text, shown.fromReal(prefix_len + c)); + if (at.row >= top and at.row < top + rows) { + const line = lines[at.row - top]; + const col = panes.File.rawDisplayCol(p.settings.tab, line, @min(at.col, line.len)); + if (col < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(col)), .y = tag_y + @as(u16, @intCast(at.row - top)), .bar = true }; + } + }; // the caret: bar while typing, block in normal mode (same rule as a body) if (caret) |at| { const col = panes.File.rawDisplayCol(p.settings.tab, lines[at.row], at.at) -| pane.tag_scroll; 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 => { diff --git a/core/root.zig b/core/root.zig index bdec2d49..b6835115 100644 --- a/core/root.zig +++ b/core/root.zig @@ -412,6 +412,7 @@ test { _ = @import("bindings.zig"); _ = @import("TagState.zig"); _ = @import("Band.zig"); + _ = @import("Field.zig"); _ = @import("Effect.zig"); _ = @import("tape.zig"); _ = @import("replica.zig"); |
