summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--abi/wire.zig5
-rw-r--r--core/Core.zig15
-rw-r--r--core/Messages.zig43
-rw-r--r--core/TagState.zig363
-rw-r--r--core/bindings.zig2
-rw-r--r--core/config.zig4
-rw-r--r--core/draw.zig38
-rw-r--r--core/edit.zig5
-rw-r--r--core/keys.zig155
-rw-r--r--core/root.zig2
-rw-r--r--core/tagline.zig14
-rw-r--r--core/walk.zig6
-rw-r--r--test/daemon.py2
13 files changed, 593 insertions, 61 deletions
diff --git a/abi/wire.zig b/abi/wire.zig
index f558cb9a..e0a932ac 100644
--- a/abi/wire.zig
+++ b/abi/wire.zig
@@ -10,6 +10,9 @@ const pardes = @import("core");
const config = @import("pardes_options");
const build_info = @import("build_info");
+/// 36: a view's tag out of rest (walk UI: `View.tag_state`, its root's
+/// delay, its level unrolled and that level's cursor, the leaf each group
+/// ran last) in the dump and the digest;
/// 35: a view's top is a line and a row of that line's wrapped rows: a
/// file's `scroll_row` (and its share's in each view) in the dump and the
/// digest; a dump without it loads at its lines' first rows;
@@ -76,7 +79,7 @@ const build_info = @import("build_info");
/// part B); 14: the hello names a session and
/// carries the semantics hash, and a ping is answered (phase 5's
/// groundwork); 13: the frames went (phase 3).
-pub const version: u16 = 35;
+pub const version: u16 = 36;
/// The terminal's build and the window's step alike (`semantics`).
const family = switch (config.platform) {
diff --git a/core/Core.zig b/core/Core.zig
index 835eb86c..d0c9976d 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -184,6 +184,9 @@ pub const View = struct {
window_pick: ?usize = null,
leader_keys: [4]u8 = undefined,
leader_n: u8 = 0,
+ /// Its tag out of rest: the which-key level, the typing states
+ /// (TagState.zig).
+ tag_state: pardes.TagState = .{},
/// The keyboard is on a header: a column's tag, or the workspace's.
header: ?Header = null,
/// A mouse sweep is extending the focused header's selection.
@@ -2010,11 +2013,12 @@ pub fn sync(p: *Pardes) void {
pane.tag_lead = lead;
}
// A row per shown row: the tag's lines wrapped at its width.
- var lines = std.mem.count(u8, tagline.curTail(pane), "\n") + 1;
+ const tail = tagline.shownTail(p, wa, pane) catch tagline.curTail(pane);
+ var lines = std.mem.count(u8, tail, "\n") + 1;
var wanted = lines;
var caret_row: usize = @intCast(@max(0, pane.tag.cur_row));
if (prefix) |made| wrap: {
- const whole = std.mem.concat(wa, u8, &.{ made, tagline.curTail(pane) }) catch break :wrap;
+ const whole = std.mem.concat(wa, u8, &.{ made, tail }) catch break :wrap;
const shown = tagline.wrapTag(p.settings.tab, wa, whole, tagline.tagWidth(p, id)) catch break :wrap;
lines = shown.rows();
// Collapsed, one row, as acme's Tagup sets taglines to 1
@@ -2174,6 +2178,11 @@ pub fn nextWake(p: *const Pardes) ?u64 {
const wait = @max(1, (pane.question_due_ns - p.clock.stepped_ns + frame_ns - 1) / frame_ns);
frames = if (frames) |soonest| @min(soonest, wait) else wait;
};
+ // A which-key level shows at the end of its root's delay (TagState).
+ if (pardes.TagState.wake(p)) |due| {
+ const wait = @max(1, (due - p.clock.stepped_ns + frame_ns - 1) / frame_ns);
+ frames = if (frames) |soonest| @min(soonest, wait) else wait;
+ }
// A paste's Enter waiting on its REPL goes at the end of its wait,
// a time on the clock: the frames from `stepped_ns` that reach it.
for (p.panes) |slot| if (slot) |pane| if (pane.repl_enter > 0) {
@@ -2215,6 +2224,8 @@ fn advanceInner(p: *Pardes, now_ns: u64) void {
for (p.panes) |slot| if (slot) |pane| if (pane.question_due_ns == Pane.question_asked) {
pane.question_due_ns = now_ns + config.del_question_delay_ns;
};
+ // A which-key root struck since: its level's delay from now.
+ pardes.TagState.dated(p, now_ns);
const frame = frame_ns;
var moving: u32 = 0;
// The session's ticks are the recording session's: a picture still
diff --git a/core/Messages.zig b/core/Messages.zig
index 040b8d2c..6b406e91 100644
--- a/core/Messages.zig
+++ b/core/Messages.zig
@@ -56,18 +56,14 @@ pub const LoggedMessage = struct {
}
};
-/// `SPC` plus the chord keys typed so far, spaced out the way the help
-/// index spells a leader path.
-fn leaderText(p: *const Pardes, buf: *[16]u8) []const u8 {
- buf.* = @splat(' ');
- @memcpy(buf[0..3], "SPC");
- var end: usize = 3;
- for (p.view().leader_keys[0..p.view().leader_n]) |ch| {
- if (end + 2 > buf.len) break;
- buf[end + 1] = ch;
- end += 2;
- }
- return buf[0..end];
+/// The room the leader band's line takes (TagState.lastUsed): a leaf's
+/// key, word and summary (builtins.summary_max), and "(last used here)".
+pub const leader_room = 192;
+
+/// The leaf the current which-key level ran last, and what it does: the
+/// line under a rolled level (walk UI SPEC §3.7).
+fn leaderText(p: *const Pardes, buf: *[leader_room]u8) []const u8 {
+ return pardes.TagState.lastUsed(p, p.view(), buf) orelse "";
}
pub const Message = struct {
@@ -316,7 +312,7 @@ pub fn messageFrames(ms: u16) u16 {
/// What notice band `i` says. The grid pass and the band layers both
/// read it here, so a terminal and a pixel shell show the same line.
-pub fn noticeText(p: *const Pardes, pane: *const Pane, i: usize, leader_buf: *[16]u8) []const u8 {
+pub fn noticeText(p: *const Pardes, pane: *const Pane, i: usize, leader_buf: *[leader_room]u8) []const u8 {
return switch (pane.notices.kinds[i]) {
.message => pane.msg[0..pane.msg_len],
.older => pane.msg_older[pane.notices.index[i]].slice(),
@@ -497,7 +493,7 @@ pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void {
// draws, and a chip narrower than its text there would clip it.
const tx = r.x + config.GUTTER;
const tw = r.w -| config.GUTTER;
- var leader_buf: [16]u8 = undefined;
+ var leader_buf: [leader_room]u8 = undefined;
for (0..pane.notices.len) |i| {
const chip = panes.File.displayWidth(p.settings.tab, noticeText(p, pane, i, &leader_buf)) + 2;
pane.notices.left[i] = if (tw == 0) std.math.maxInt(u16) else tx + tw - @as(u16, @intCast(@min(chip, tw)));
@@ -514,7 +510,8 @@ pub fn bands(p: *const Pardes, v: *const Pardes.View, pane: *const Pane, tag_row
// new one lands under them; when rows run out the oldest go first.
for (pane.msg_older[0..pane.msg_older_len], 0..) |line, i| n.pushAt(.older, @intCast(i), line.slot);
if (pane.msg_len > 0) n.pushAt(.message, 0, pane.msg_slot);
- if (id == v.active and v.prefix == .leader) n.push(.leader);
+ var used_buf: [leader_room]u8 = undefined;
+ if (id == v.active and pardes.TagState.lastUsed(p, v, &used_buf) != null) n.push(.leader);
// Del's question shows once its delay is up: answered before that, it
// was never there.
const del_waiting = pane.prompt == .del_side and pane.question_due_ns > p.clock.now_ns;
@@ -834,7 +831,7 @@ test "a builtin announces itself on the message row, and Verbose silences it" {
}
test "notices stack over the body's top rows without moving them" {
- const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 24 });
defer p.deinit();
const id = p.view().active;
const pane = p.panes[id].?;
@@ -845,11 +842,15 @@ test "notices stack over the body's top rows without moving them" {
const r = p.view().rects[id];
const top = p.bodyTop(pane, r);
- // A message and a leader prefix at once: two chips, in the order
- // collectNotices chose, each one row tall and each one row further down.
- try std.testing.expect(p.executeBuiltinLine(id, "Msg first"));
+ // A message and the leader's line (a shown level's last leaf) at once:
+ // two chips, in the order collectNotices chose, each one row tall and
+ // each one row further down.
+ try std.testing.expect(p.executeBuiltinLine(id, "Msg first message here"));
p.view().prefix = .leader;
p.view().leader_n = 0;
+ p.view().tag_state.used[0] = 'q';
+ p.view().tag_state.due_ns = 1;
+ p.clock.now_ns = 1;
_ = frame.reset(.retain_capacity);
_ = try p.render(frame.allocator());
try std.testing.expectEqual(@as(u8, 2), pane.notices.len);
@@ -869,8 +870,8 @@ test "notices stack over the body's top rows without moving them" {
seen += 1;
}
try std.testing.expectEqual(@as(usize, 2), seen);
- // "first" is longer than "SPC", so its chip is wider.
- try std.testing.expect(pane.notices.left[0] < pane.notices.left[1]);
+ // The leader's line is longer than the message, so its chip is wider.
+ try std.testing.expect(pane.notices.left[0] > pane.notices.left[1]);
// Gone with the notices: no stale chip left behind.
p.view().prefix = .none;
diff --git a/core/TagState.zig b/core/TagState.zig
new file mode 100644
index 00000000..3c400b3a
--- /dev/null
+++ b/core/TagState.zig
@@ -0,0 +1,363 @@
+//! A tag out of rest (walk UI, pick-ui SPEC.md §3): which-key over a
+//! root's level, rolled into the head or unrolled into the band, and the
+//! typing states. Each view's own (the keyboard's transient state is the
+//! view's, as `View.prefix` is: the coordinator's R4), in its digest and
+//! dumps. A root and its keys are what keys.zig's leader, a text's normal
+//! prefix or window mode already hold; this keeps what they do not: when
+//! the level shows, whether it is unrolled, its cursor, the key it missed,
+//! and the leaf each group ran last.
+//!
+//! The head (keys, level words, hints) is computed tag text drawn after
+//! the prefix in place of the tag's own text (R1), never written into it:
+//! `head`, which tagline.zig's `shownTail` reads.
+const TagState = @This();
+
+const std = @import("std");
+const pardes = @import("root.zig");
+const bindings = @import("bindings.zig");
+const tagline = @import("tagline.zig");
+const panes = @import("panes.zig");
+const Pardes = pardes.Pardes;
+const View = Pardes.View;
+const Pane = pardes.Pane;
+const Root = bindings.Root;
+
+pub const Kind = enum(u8) { rest, help, describe };
+
+kind: Kind = .rest,
+/// 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
+/// until the next advance (Pardes.advance), which has the time; 0 for none.
+due_ns: u64 = 0,
+unrolled: bool = false,
+/// The unrolled level's cursor, a child's key: 0 is the leaf the group ran
+/// last, else its first child.
+hi: u8 = 0,
+/// A key the level has no child for, shown struck until the next.
+miss: u8 = 0,
+/// The leaf each group ran last, by its key (bindings.groups): the cursor
+/// starts there and the rolled level underlines it (SPEC §4.3).
+used: [bindings.ngroups]u8 = @splat(0),
+
+/// 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);
+
+/// The root current in view `v` and the keys typed under it: SPC's
+/// leader keys, window mode, or the helix prefix the text with the
+/// keyboard waits on. A view not live has its leader and window mode
+/// alone (its texts' prefixes are in their panes' shares).
+pub const Current = struct { root: Root, path: []const u8 };
+
+pub fn current(p: *const Pardes, v: *const View) ?Current {
+ if (v.prefix == .leader) return .{ .root = .leader, .path = v.leader_keys[0..v.leader_n] };
+ if (v.window_pick != null) return .{ .root = .window, .path = "" };
+ if (v != p.view() or v.header != null) return null;
+ const pane = p.panes[v.active] orelse return null;
+ const t = if (pane.focus == .tag) &pane.tag else &pane.body;
+ const prefix = t.normal.prefix orelse return null;
+ const root: Root = switch (prefix) {
+ .goto_prefix => .goto,
+ .view_prefix => if (t.normal.sticky) .view_sticky else .view,
+ .match_prefix => if (t.normal.match_sub == null) .match else return null,
+ .next_prefix => .next,
+ .prev_prefix => .prev,
+ .register_prefix => .register,
+ else => return null,
+ };
+ return .{ .root = root, .path = "" };
+}
+
+/// Whether view `v`'s current root has shown its level: its delay is up.
+pub fn shown(p: *const Pardes, v: *const View) bool {
+ const s = &v.tag_state;
+ if (s.unrolled) return true; // Down shows it at once
+ return s.due_ns != asked and s.due_ns != 0 and p.clock.now_ns >= s.due_ns;
+}
+
+/// The pane whose tag is out of rest in the live view, if one is: the one
+/// with the keyboard while a root's key cell or level shows. Cheap at rest,
+/// as every tag's text asks it.
+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;
+ 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;
+}
+
+fn textPrefix(p: *const Pardes, v: *const View) bool {
+ const pane = p.panes[v.active] orelse return false;
+ return pane.body.normal.prefix != null or pane.tag.normal.prefix != null;
+}
+
+/// 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 {
+ 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;
+ s.root = now;
+ s.unrolled = false;
+ s.hi = 0;
+ s.miss = 0;
+ s.due_ns = if (now == null) 0 else asked;
+}
+
+/// The root a key strikes when its time is had (Pardes.advance).
+pub fn dated(p: *Pardes, now_ns: u64) void {
+ for (&p.views) |*slot| if (slot.*) |*v| {
+ const s = &v.tag_state;
+ if (s.due_ns != asked) continue;
+ s.due_ns = if (s.root) |r| now_ns + bindings.info.get(r).delay_ns else 0;
+ };
+}
+
+/// When a level next shows, for the loop's wake (Pardes.nextWake).
+pub fn wake(p: *const Pardes) ?u64 {
+ var soonest: ?u64 = null;
+ for (p.views) |slot| if (slot) |v| {
+ const due = v.tag_state.due_ns;
+ if (due == asked or due == 0 or due <= p.clock.stepped_ns) continue;
+ soonest = if (soonest) |n| @min(n, due) else due;
+ };
+ return soonest;
+}
+
+/// The last leaf run in the current level, as the line under it says it
+/// (Messages' leader band): `k Hover: its summary (last used here)`, while
+/// the level is shown rolled.
+pub fn lastUsed(p: *const Pardes, v: *const View, buf: []u8) ?[]const u8 {
+ const s = &v.tag_state;
+ if (s.kind != .rest or s.unrolled or !shown(p, v)) return null;
+ const cur = current(p, v) orelse return null;
+ const node = bindings.nodeAt(cur.root, cur.path) orelse return null;
+ const key = s.used[node.group];
+ if (key == 0) return null;
+ var children: [bindings.max_children]bindings.Child = undefined;
+ for (bindings.level(p, cur.root, cur.path, &children)) |c| if (c.key == key) {
+ var doc_buf: [160]u8 = undefined;
+ return std.fmt.bufPrint(buf, "{s} {s}: {s} (last used here)", .{ bindings.keyName(c.key), c.label, bindings.docOf(cur.root, c, &doc_buf) }) catch null;
+ };
+ return null;
+}
+
+/// Remember `key` as the leaf run last at `path` under `root`.
+pub fn ran(v: *View, root: Root, path: []const u8, key: u8) void {
+ const node = bindings.nodeAt(root, path) orelse return;
+ v.tag_state.used[node.group] = key;
+}
+
+/// 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 };
+/// The head: its text, the spans drawn apart, and the caret in it when the
+/// head is typed in.
+pub const Head = struct {
+ text: []const u8,
+ spans: []const Span = &.{},
+ caret: ?u32 = null,
+};
+
+const Builder = struct {
+ arena: std.mem.Allocator,
+ text: std.ArrayList(u8) = .empty,
+ spans: std.ArrayList(Span) = .empty,
+
+ fn put(b: *Builder, s: []const u8) !void {
+ try b.text.appendSlice(b.arena, s);
+ }
+ fn styled(b: *Builder, s: []const u8, style: Style) !void {
+ const lo: u32 = @intCast(b.text.items.len);
+ try b.text.appendSlice(b.arena, s);
+ try b.spans.append(b.arena, .{ .lo = lo, .hi = @intCast(b.text.items.len), .style = style });
+ }
+ /// The keys typed as 2-cell key cells: the root's, then each key.
+ fn keyCells(b: *Builder, root: Root, path: []const u8) !void {
+ try b.styled(bindings.info.get(root).cell, .key);
+ try b.put(" ");
+ for (path) |k| {
+ try b.styled(if (k == ' ') "␣" else bindings.keyName(k), .key);
+ try b.put(" ");
+ }
+ }
+ fn done(b: *const Builder) Head {
+ return .{ .text = b.text.items, .spans = b.spans.items };
+ }
+};
+
+/// The head of pane `pane`'s tag while it is out of rest in the live view,
+/// null at rest.
+pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head {
+ const id = hostOf(p) orelse return null;
+ 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 };
+ try b.put(" ");
+ try b.keyCells(cur.root, cur.path);
+ if (!shown(p, v)) {
+ try b.put("…");
+ return b.done();
+ }
+ const node = bindings.nodeAt(cur.root, cur.path) orelse return b.done();
+ if (s.unrolled) {
+ try b.put(if (cur.path.len == 0) bindings.info.get(cur.root).label else node.label);
+ try b.put(" ↑ roll up · ? field · esc");
+ return b.done();
+ }
+ try b.styled("▸", .key);
+ // The rolled level wraps to three rows at most (SPEC §3.6): what does
+ // not fit is `…`, and `↓` unrolls it whole.
+ const hint = " ↓ unroll · ? help";
+ const prefix = try tagline.prefixIn(p, arena, pane);
+ const tab = p.settings.tab;
+ const room = 3 * tagline.tagWidth(p, id) -| panes.File.displayWidth(tab, prefix) -| panes.File.displayWidth(tab, hint) -| 2;
+ var children: [bindings.max_children]bindings.Child = undefined;
+ const used = s.used[node.group];
+ for (bindings.level(p, cur.root, cur.path, &children)) |c| {
+ const width = panes.File.displayWidth(tab, b.text.items) + 2 + bindings.keyName(c.key).len + 1 + panes.File.displayWidth(tab, c.label);
+ if (width > room) {
+ try b.put(" …");
+ break;
+ }
+ try b.put(if (b.text.items.len > 0 and std.mem.endsWith(u8, b.text.items, "▸")) " " else " ");
+ const lo: u32 = @intCast(b.text.items.len);
+ try b.styled(bindings.keyName(c.key), .key);
+ try b.put(" ");
+ try b.put(c.label);
+ if (c.group) try b.put("…");
+ const hi: u32 = @intCast(b.text.items.len);
+ if (bindings.dimmed(pane, c)) try b.spans.append(arena, .{ .lo = lo, .hi = hi, .style = .dim });
+ if (c.key == used) try b.spans.append(arena, .{ .lo = lo, .hi = hi, .style = .under });
+ }
+ if (s.miss != 0) {
+ try b.put(" ");
+ try b.styled(bindings.keyName(s.miss), .strike);
+ }
+ try b.put(hint);
+ return b.done();
+}
+
+fn testTag(p: *Pardes, arena: std.mem.Allocator) ![]const u8 {
+ return tagline.tagText(p, arena, p.panes[p.view().active].?);
+}
+
+test "SPC writes its key cell at once and its level 250 ms on; g changes nothing until 500 ms, and gg never" {
+ const Key = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\ntwo\nthree\n");
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const a = frame.allocator();
+ p.clock.now_ns = 5 * std.time.ns_per_s;
+ p.clock.stepped_ns = p.clock.now_ns;
+ p.sync();
+ const rest = try a.dupe(u8, try testTag(p, a));
+ // SPC: its cell at once, the own words gone under the head.
+ p.update(.{ .key = .{ .cp = ' ' } });
+ try std.testing.expect(std.mem.endsWith(u8, try testTag(p, a), " ␣ …"));
+ // Its time is the next advance's; the level waits 250 ms from it, and
+ // the loop wakes for it then.
+ try std.testing.expectEqual(asked, p.view().tag_state.due_ns);
+ p.advance(p.clock.now_ns);
+ const due = p.view().tag_state.due_ns;
+ try std.testing.expectEqual(p.clock.now_ns + 250 * std.time.ns_per_ms, due);
+ const woken = p.nextWake().?;
+ try std.testing.expect(woken >= due and woken < due + pardes.frame_ns);
+ p.clock.now_ns = due - 1;
+ try std.testing.expect(std.mem.endsWith(u8, try testTag(p, a), " ␣ …"));
+ p.clock.now_ns = due;
+ const level = try testTag(p, a);
+ try std.testing.expect(std.mem.indexOf(u8, level, " ␣ ▸ b buffer…") != null);
+ try std.testing.expect(std.mem.indexOf(u8, level, "l lsp…") != null);
+ try std.testing.expect(std.mem.endsWith(u8, level, "↓ unroll · ? help"));
+ // Down a level: its keys, its children.
+ p.update(.{ .key = .{ .cp = 'l' } });
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " ␣ l ▸ a CodeAction b Callstack") != null);
+ // Esc: rest, the tag as it was.
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+ try std.testing.expectEqual(@as(u64, 0), p.view().tag_state.due_ns);
+
+ // g: nothing until 500 ms pass with no next key, then its cell and
+ // its level together.
+ p.update(.{ .key = .{ .cp = 'g' } });
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+ p.advance(p.clock.now_ns);
+ const g_due = p.view().tag_state.due_ns;
+ try std.testing.expectEqual(p.clock.now_ns + 500 * std.time.ns_per_ms, g_due);
+ p.clock.now_ns = g_due - 1;
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+ p.clock.now_ns = g_due;
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), " g ▸ ") != null);
+ try std.testing.expect(std.mem.indexOf(u8, try testTag(p, a), "d Def") != null);
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+
+ // gg at speed: the tag never changes, the cursor goes to the top.
+ const pane = p.panes[p.view().active].?;
+ pane.body.cur_row = 2;
+ p.update(.{ .key = .{ .cp = 'g' } });
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+ p.update(.{ .key = .{ .cp = 'g' } });
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+ try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
+ p.advance(p.clock.now_ns + std.time.ns_per_s);
+ try std.testing.expectEqualStrings(rest, try testTag(p, a));
+ try std.testing.expect(p.view().tag_state.root == null);
+}
+
+test "a leaf run from a level is remembered: underlined there, and said on the line under it" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\n");
+ p.clock.now_ns = 5 * std.time.ns_per_s;
+ p.clock.stepped_ns = p.clock.now_ns;
+ // SPC t w: Wrap, run from SPC t.
+ for (" tw") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expectEqual(@as(u8, 'w'), p.view().tag_state.used[bindings.nodeAt(.leader, "t").?.group]);
+ var buf: [192]u8 = undefined;
+ // Back in SPC t, rolled: the line under it says Wrap ran last here.
+ for (" t") |c| p.update(.{ .key = .{ .cp = c } });
+ p.advance(p.clock.now_ns);
+ try std.testing.expect(lastUsed(p, p.view(), &buf) == null); // not shown yet
+ p.clock.now_ns = p.view().tag_state.due_ns;
+ const said = lastUsed(p, p.view(), &buf).?;
+ try std.testing.expect(std.mem.startsWith(u8, said, "w Wrap: "));
+ try std.testing.expect(std.mem.endsWith(u8, said, " (last used here)"));
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const h = (try head(p, frame.allocator(), p.panes[p.view().active].?)).?;
+ const under = for (h.spans) |span| {
+ if (span.style == .under) break h.text[span.lo..span.hi];
+ } else "";
+ try std.testing.expectEqualStrings("w Wrap", under);
+ // Enter, rolled, runs it again.
+ const wrapped = p.settings.wrap;
+ p.update(.{ .key = .{ .cp = pardes.Key.enter } });
+ try std.testing.expect(p.settings.wrap != wrapped);
+ try std.testing.expect(p.view().prefix == .none);
+}
+
+test "a key the level has no child for is struck and the level stays; Backspace goes up a level" {
+ const Key = pardes.Key;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\n");
+ for (" l") |c| p.update(.{ .key = .{ .cp = c } });
+ p.update(.{ .key = .{ .cp = 'z' } });
+ try std.testing.expect(p.view().prefix == .leader);
+ try std.testing.expectEqual(@as(u8, 'z'), p.view().tag_state.miss);
+ try std.testing.expectEqualStrings("l", p.view().leader_keys[0..p.view().leader_n]);
+ p.update(.{ .key = .{ .cp = Key.backspace } });
+ try std.testing.expectEqual(@as(u8, 0), p.view().leader_n);
+ try std.testing.expect(p.view().prefix == .leader);
+ p.update(.{ .key = .{ .cp = Key.backspace } });
+ try std.testing.expect(p.view().prefix == .none);
+}
diff --git a/core/bindings.zig b/core/bindings.zig
index 345c2a4a..b2d84a7c 100644
--- a/core/bindings.zig
+++ b/core/bindings.zig
@@ -292,7 +292,7 @@ pub fn level(p: *const Pardes, root: Root, path: []const u8, out: *[max_children
}
};
const shown = out[0..n];
- for (1..shown.len) |i| {
+ for (1..@max(1, shown.len)) |i| {
var j = i;
while (j > 0 and rank(shown[j].key, shown[j].group) < rank(shown[j - 1].key, shown[j - 1].group)) : (j -= 1)
std.mem.swap(Child, &shown[j], &shown[j - 1]);
diff --git a/core/config.zig b/core/config.zig
index e9225c53..324102b5 100644
--- a/core/config.zig
+++ b/core/config.zig
@@ -1656,7 +1656,11 @@ test "Space-k is unbound while Exit, Kill and other k chords remain available" {
while (p.nextEffect()) |_| {}
p.update(.{ .key = .{ .cp = ' ' } });
try std.testing.expect(p.view().prefix == .leader);
+ // A key SPC has no child for is struck, the level kept (walk UI); Esc
+ // leaves it.
p.update(.{ .key = .{ .cp = 'k' } });
+ try std.testing.expect(p.view().prefix == .leader and p.view().tag_state.miss == 'k' and !p.quit);
+ p.update(.{ .key = .{ .cp = Key.escape } });
try std.testing.expect(p.view().prefix != .leader and !p.quit);
try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row);
while (p.nextEffect()) |effect| try std.testing.expect(effect != .quit);
diff --git a/core/draw.zig b/core/draw.zig
index ff76aa75..de08ef4f 100644
--- a/core/draw.zig
+++ b/core/draw.zig
@@ -16,6 +16,7 @@ const edit = @import("edit.zig");
const tagline = @import("tagline.zig");
const body_layer = @import("body_layer.zig");
const Messages = @import("Messages.zig");
+const TagState = @import("TagState.zig");
const builtins = @import("builtins.zig");
const Color = @import("surface.zig").Color;
const FontRole = @import("surface.zig").FontRole;
@@ -227,7 +228,7 @@ fn joinNotice(p: *Pardes, s: *Surface, arena: std.mem.Allocator, region: Region)
const kind = pane.notices.kinds[i];
const r = v.rects[id];
const chip = region.rect.w;
- var leader_buf: [16]u8 = undefined;
+ var leader_buf: [Messages.leader_room]u8 = undefined;
const text = Messages.noticeText(p, pane, i, &leader_buf);
const bg = chrome.tag_bg;
// Where an exec was sent to a REPL reads in the tag's name tint, set
@@ -812,7 +813,9 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane,
.font_role = .tagline,
});
const whole = try tagline.tagText(p, arena, pane);
- const tail = tagline.curTail(pane);
+ // Out of rest, the head in place of the tag's own text (TagState).
+ const head = try TagState.head(p, arena, pane);
+ const tail = if (head) |h| h.text else tagline.curTail(pane);
const prefix_len = whole.len - tail.len;
// A row per line shown, from `tag_top`: the tag's first line carries
// the prefix, the rest are the tag's own text alone.
@@ -835,7 +838,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane,
const at = modal.positionAt(shown.text, shown.fromReal(@intCast(@max(0, pane.input.cur_col))));
if (at.row < top or at.row >= top + rows) break :caret null;
break :caret .{ .row = at.row - top, .at = @min(lines[at.row - top].len, at.col) };
- } else if (pane.focus == .tag and pane.prompt == .none) caret: {
+ } else if (pane.focus == .tag and pane.prompt == .none and head == null) caret: {
const real = modal.offsetAt(whole, .{ .row = @intCast(@max(0, pane.tag.cur_row)), .col = @intCast(@max(0, pane.tag.cur_col)) });
const at = modal.positionAt(shown.text, shown.fromReal(real));
if (at.row < top or at.row >= top + rows) break :caret null;
@@ -868,9 +871,36 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane,
s.at(tx + @as(u16, @intCast(col)), y).style.fg = .{ .rgb = name_fg };
}
};
+ // The head's keys in the key ink, its dimmed, underlined and struck
+ // words (TagState.Span), over whichever rows they wrapped onto.
+ if (head) |h| for (h.spans) |span| {
+ const a = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.lo));
+ const b = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.hi));
+ var row = @max(a.row, top);
+ while (row <= b.row and row < top + rows) : (row += 1) {
+ const line = lines[row - top];
+ const from = if (row == a.row) a.col else 0;
+ const to = if (row == b.row) b.col else line.len;
+ var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len));
+ const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len));
+ const y = tag_y + @as(u16, @intCast(row - top));
+ while (col < end and col < tw) : (col += 1) {
+ const style = &s.at(tx + @as(u16, @intCast(col)), y).style;
+ switch (span.style) {
+ .key => {
+ style.fg = .{ .rgb = chrome.box };
+ style.bold = true;
+ },
+ .dim => style.dim = true,
+ .under => style.ul = .single,
+ .strike => style.strikethrough = true,
+ }
+ }
+ }
+ };
// The tag's own selections, while it has the keyboard, a line at a
// time. A range of one grapheme is the block cursor, not a selection.
- if (pane.focus == .tag) {
+ if (pane.focus == .tag and head == null) {
var ranges: [Text.max_selections]modal.Selection = undefined;
const got = pane.tag.ranges(whole, 0, &ranges);
for (ranges[0..got.n]) |range| {
diff --git a/core/edit.zig b/core/edit.zig
index 3e57927a..8ed7752e 100644
--- a/core/edit.zig
+++ b/core/edit.zig
@@ -680,6 +680,11 @@ fn setEditTextSpan(e: Editor, t: *Text, new: []u8, known_end: ?modal.Cursor, spa
// caller reads the new text back, as a body's is kept.
defer retireEditText(e, new);
const pane = t.pane() orelse return;
+ // Out of rest the tag shows its head, which is no one's to edit.
+ if (try pardes.TagState.head(e.p, e.scratch, pane) != null) {
+ t.refused = true;
+ return;
+ }
const prefix = try tagline.pathPrefix(e.p, e.scratch, pane);
if (!std.mem.startsWith(u8, new, prefix)) {
t.refused = true;
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;
+ },
}
}
diff --git a/core/root.zig b/core/root.zig
index 576454de..e8938fd9 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -90,6 +90,7 @@ pub const Kind = @import("Kind.zig");
pub const Term = @import("Term.zig");
pub const keys = @import("keys.zig");
pub const bindings = @import("bindings.zig");
+pub const TagState = @import("TagState.zig");
pub const Addon = @import("Addon.zig");
pub const dump = @import("dump.zig");
pub const lsp = @import("lsp/lsp.zig");
@@ -406,6 +407,7 @@ test {
_ = @import("Jump.zig");
_ = @import("keys.zig");
_ = @import("bindings.zig");
+ _ = @import("TagState.zig");
_ = @import("Effect.zig");
_ = @import("tape.zig");
_ = @import("replica.zig");
diff --git a/core/tagline.zig b/core/tagline.zig
index 02d1b277..a6a85217 100644
--- a/core/tagline.zig
+++ b/core/tagline.zig
@@ -11,6 +11,7 @@ const exec = @import("exec.zig");
const config = @import("config.zig");
const Layer = @import("Layer.zig");
const layout = @import("layout.zig");
+const TagState = @import("TagState.zig");
const Pane = panes.Pane;
const Text = panes.Text;
const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET;
@@ -210,10 +211,19 @@ pub fn defaultTail(pane: *const Pane) []const u8 {
return pane_tail;
}
+/// What the tag shows after its prefix: its own text, or while the pane
+/// is out of rest in this view the head (TagState.head: the keys, the
+/// level, the field), computed and read-only, the own text untouched under
+/// it and back at rest (walk UI R1).
+pub fn shownTail(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 {
+ if (try TagState.head(p, arena, pane)) |h| return h.text;
+ return curTail(pane);
+}
+
/// The tag as it is shown, read and pointed at: its computed prefix and
-/// its own text after it.
+/// what follows it (shownTail).
pub fn tagText(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]u8 {
- return std.mem.concat(arena, u8, &.{ try tagPrefix(p, pane), curTail(pane) });
+ return std.mem.concat(arena, u8, &.{ try tagPrefix(p, pane), try shownTail(p, arena, pane) });
}
/// Give the keyboard to the tag in normal mode (`:`), its cursor where
diff --git a/core/walk.zig b/core/walk.zig
index 7a77fdb6..6734c0b7 100644
--- a/core/walk.zig
+++ b/core/walk.zig
@@ -2742,6 +2742,12 @@ pub const Get = struct {
l.made.items[at].dead = true;
return err;
};
+ // A damaged count past the elements the list holds: the rest would
+ // be undefined (a restored image's 256 rows of 0xAA).
+ if (l.filled < n) {
+ l.made.items[at].dead = true;
+ return error.BadDump;
+ }
return many;
}
diff --git a/test/daemon.py b/test/daemon.py
index 97b0de76..f9cbc9e1 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -340,7 +340,7 @@ class Replica:
"""A replica's connection (abi/wire.zig): length-prefixed
messages, a tag byte and a little-endian u32 length. It is a near
client: its first word after the hello says so."""
- WIRE = 35
+ WIRE = 36
def __init__(self, path, timeout=10):
self.path, self.timeout = path, timeout