diff options
| -rw-r--r-- | core/Band.zig | 66 | ||||
| -rw-r--r-- | core/TagState.zig | 11 | ||||
| -rw-r--r-- | core/bindings.zig | 172 | ||||
| -rw-r--r-- | core/keys.zig | 28 |
4 files changed, 266 insertions, 11 deletions
diff --git a/core/Band.zig b/core/Band.zig index 3c800bf0..13c4b9c0 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -105,6 +105,8 @@ pub fn takes(p: *Pardes, id: usize) u16 { 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); + // The registers, one column of eleven at most (registerSteps). + if (cur.root == .register and cur.path.len == 0) return @max(doc_rows, @min(Cursor.shown, n)); return @max(doc_rows, levelRows(n)); } @@ -151,6 +153,11 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { }, } const cur = TagState.current(p, v).?; + if (cur.root == .register) { + try band.registerSteps(p, arena, pane, cur.path); + try band.place(p, arena, pane, id); + return band; + } // 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); @@ -196,6 +203,63 @@ fn helpSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, s: *const TagSta band.add(try docStep(p, arena, at.root, at.path[0 .. at.path.len - 1], at.child)); } +/// `"` unrolled (SPEC §9): the registers, a row each (its key, what it +/// is, its first line) around the cursor, or under `"a` its verbs; beside +/// them the value of the one on the cursor (or named) in full, as many of +/// its lines as the band has rows, only those read. +fn registerSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *const Pane, path: []const u8) !void { + const s = &p.view().tag_state; + var children: [bindings.max_children]bindings.Child = undefined; + const level = bindings.level(p, .register, path, &children); + const node = bindings.nodeAt(.register, path); + const used = if (node) |n| s.used[n.group] else 0; + var at: usize = 0; + for (level, 0..) |c, i| if (c.key == (if (s.hi != 0) s.hi else used)) { + at = i; + }; + if (path.len > 0) band.add(try levelStep(arena, pane, level, at, used)) else { + const w = (Cursor{ .at = @intCast(at) }).window(@intCast(level.len)); + const rows = try arena.alloc(Row, w.hi - w.lo); + for (rows, w.lo..) |*row, i| { + const c = level[i]; + const name = for (bindings.register_names) |r| { + if (r.key == c.key) break r.name; + } else "named"; + row.* = .{ + .text = try std.fmt.allocPrint(arena, "{s} {s: <17} {s}{s}", .{ bindings.keyName(c.key), name, c.label, if (c.more) "…" else "" }), + .on = i == at, + .key = 1, + .under = c.key == used, + .index = @intCast(i), + }; + } + band.add(.{ .rows = rows }); + } + const key = if (path.len > 0) path[0] else level[at].key; + const name = for (bindings.register_names) |r| { + if (r.key == key) break r.name; + } else "named"; + var rows: std.ArrayList(Row) = .empty; + try rows.append(arena, .{ .text = try std.fmt.allocPrint(arena, "\"{s} · {s}", .{ bindings.keyName(key), name }), .key = 2 }); + const value = bindings.registerValue(p, key); + if (value.len == 0) try rows.append(arena, .{ .text = "(empty)", .dim = true }); + // A line is cut at what the band could show of it: a long one is + // never scanned past that. + const most = @as(usize, band.columns) * band.height; + var rest = value; + while (rest.len > 0 and rows.items.len + 1 < band.height) { + const look = rest[0..@min(rest.len, most)]; + const end = std.mem.indexOfScalar(u8, look, '\n') orelse { + try rows.append(arena, .{ .text = rest[0..if (look.len < rest.len) modal.graphemeStart(rest, look.len) else look.len] }); + break; + }; + try rows.append(arena, .{ .text = look[0..end] }); + rest = rest[end + 1 ..]; + } + if (path.len == 0) try rows.append(arena, .{ .text = "next p after P before R replace y into", .dim = true }); + band.add(.{ .rows = rows.items, .doc = true }); +} + /// 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. @@ -245,7 +309,7 @@ pub fn levelStep(arena: std.mem.Allocator, pane: *const Pane, level: []const bin for (level, rows, 0..) |c, *row, i| { const key = bindings.keyName(c.key); row.* = .{ - .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ key, c.label, if (c.group) "…" else "" }), + .text = try std.fmt.allocPrint(arena, "{s} {s}{s}", .{ key, c.label, if (c.group or c.more) "…" else "" }), .on = i == at, .key = @intCast(key.len), .dim = bindings.dimmed(pane, c), diff --git a/core/TagState.zig b/core/TagState.zig index 5a30f182..95df8937 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -152,7 +152,11 @@ pub fn current(p: *const Pardes, v: *const View) ?Current { 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 prefix = t.normal.prefix orelse { + // `"a`: the register named, its verbs next (SPEC §9). + const r = t.normal.register; + return if (r > ' ' and r <= '~') .{ .root = .register, .path = bindings.keyName(@intCast(r)) } else null; + }; const root: Root = switch (prefix) { .goto_prefix => .goto, .view_prefix => if (t.normal.sticky) .view_sticky else .view, @@ -188,7 +192,8 @@ pub fn hostOf(p: *const Pardes) ?usize { 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; + return pane.body.normal.prefix != null or pane.tag.normal.prefix != null or + pane.body.normal.register != 0 or pane.tag.normal.register != 0; } /// After a key (keys.handleKey): a root newly current starts its delay; one @@ -515,7 +520,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { try b.styled(bindings.keyName(c.key), .key); try b.put(" "); try b.put(c.label); - if (c.group) try b.put("…"); + if (c.group or c.more) 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 }); diff --git a/core/bindings.zig b/core/bindings.zig index b2d84a7c..f7be1a43 100644 --- a/core/bindings.zig +++ b/core/bindings.zig @@ -15,6 +15,8 @@ const Pardes = pardes.Pardes; const Pane = pardes.Pane; const Key = pardes.Key; const Role = modal.Normal.Role; +const edit = @import("edit.zig"); +const File = @import("File.zig"); /// The keys a sequence starts with. `register` (`"`) is drawn the same way /// but its rows are the registers' contents, not a tree. @@ -87,8 +89,9 @@ pub const groups = groups: { }; break :groups paths; }; -/// SPC's groups, then one for each other root. -pub const ngroups = groups.len + std.enums.values(Root).len - 1; +/// SPC's groups, then one for each other root, then the verbs a register +/// takes (`"a ▸`). +pub const ngroups = groups.len + std.enums.values(Root).len; fn rootGroup(comptime r: Root) u8 { return @intCast(groups.len + @intFromEnum(r) - 1); @@ -233,6 +236,15 @@ const view_sticky_tree: Node = .{ .key = config.view_prefix_sticky, .label = "vi const prev_tree: Node = .{ .key = config.prev_prefix, .label = "previous", .children = roleLevel(.goto_paragraph, .goto_xml_element, &.{}), .group = rootGroup(.prev) }; const next_tree: Node = .{ .key = config.next_prefix, .label = "next", .children = roleLevel(.goto_paragraph, .goto_xml_element, &.{}), .group = rootGroup(.next) }; const register_tree: Node = .{ .key = config.register_prefix, .label = "registers", .group = rootGroup(.register) }; +/// What the register `"` named does next (SPEC §9: `"a ▸ p after P before +/// y into R replace`): normal mode's paste, yank and replace, which read +/// the register the parser holds. +const register_verbs: Node = .{ .key = 0, .label = "register", .group = ngroups - 1, .children = sorted(&.{ + .{ .key = roleKey(.paste_after).?, .label = "after", .leaf = .{ .role = .paste_after } }, + .{ .key = roleKey(.paste_before).?, .label = "before", .leaf = .{ .role = .paste_before } }, + .{ .key = roleKey(.yank).?, .label = "into", .leaf = .{ .role = .yank } }, + .{ .key = roleKey(.replace_with_yank).?, .label = "replace", .leaf = .{ .role = .replace_with_yank } }, +}) }; /// pardes's window mode (layout.windowKey): its moves, Collapse and Del; /// a letter picks the pane its box shows. const window_tree: Node = .{ .key = 'w', .label = "window", .group = rootGroup(.window), .children = window: { @@ -248,7 +260,13 @@ const window_tree: Node = .{ .key = 'w', .label = "window", .group = rootGroup(. /// The node `path` reaches under `root`, or null. pub fn nodeAt(root: Root, path: []const u8) ?*const Node { var n = info.get(root).node; - for (path) |k| n = for (n.children) |*c| { + var keys = path; + // A register's name is any key: under each, its verbs. + if (root == .register and path.len > 0) { + n = ®ister_verbs; + keys = path[1..]; + } + for (keys) |k| n = for (n.children) |*c| { if (c.key == k) break c; } else return null; return n; @@ -263,6 +281,8 @@ pub const Child = struct { word: ?*const pardes.Kind.Word = null, group: bool = false, lsp: bool = false, + /// Its label is cut (a register's first line): `…` after it. + more: bool = false, }; /// The most children a level shows: SPC's top has 17, and an addon's /// words (Kind.Registry's 64) may each start a key of their own. @@ -273,6 +293,7 @@ pub const max_children = 96; /// addons' leader words joined in (SPC alone). Bounded by the level and the /// registry's 64 words. pub fn level(p: *const Pardes, root: Root, path: []const u8, out: *[max_children]Child) []Child { + if (root == .register and path.len == 0) return registerLevel(p, out); var n: usize = 0; if (nodeAt(root, path)) |node| for (node.children) |*c| { if (c.leaf) |leaf| if (leaf == .builtin and !builtins.registry.here(p, leaf.builtin)) continue; @@ -300,6 +321,87 @@ pub fn level(p: *const Pardes, root: Root, path: []const u8, out: *[max_children return shown; } +/// The registers `"` shows (SPEC §9), in its order, and what each is: the +/// default first, the ones a name was given (`"ay`) after it, by name, +/// then these, the last four computed from the pane with the keyboard. +pub const register_names = [_]struct { key: u8, name: []const u8 }{ + .{ .key = '"', .name = "yank" }, + .{ .key = '/', .name = "last search" }, + .{ .key = ':', .name = "last command" }, + .{ .key = '|', .name = "last pipe" }, + .{ .key = '+', .name = "system clipboard" }, + .{ .key = '@', .name = "last macro" }, + .{ .key = '%', .name = "this file's name" }, + .{ .key = '.', .name = "the selections" }, + .{ .key = '#', .name = "selection numbers" }, + .{ .key = '_', .name = "black hole" }, +}; + +/// The display columns of a register's first line which-key shows; the +/// rest is `…`. +pub const register_cut = 24; + +/// `"`'s level: a child a register, its label the first line of its value +/// (`—` when empty), cut. Read when the level is (a key, a frame), never +/// kept: the first `register_cut` columns of each, the named ones bounded +/// by the level's room. +fn registerLevel(p: *const Pardes, out: *[max_children]Child) []Child { + var n: usize = 1; + for (p.registers.list.items) |*reg| { + if (reg.name <= ' ' or reg.name > '~' or n == out.len - (register_names.len - 1)) continue; + const k: u8 = @intCast(reg.name); + for (register_names) |r| { + if (r.key == k) break; + } else { + var j = n; + while (j > 1 and out[j - 1].key > k) : (j -= 1) out[j] = out[j - 1]; + out[j] = .{ .key = k, .label = "" }; + n += 1; + } + } + out[0] = .{ .key = '"', .label = "" }; + for (register_names[1..]) |r| { + out[n] = .{ .key = r.key, .label = "" }; + n += 1; + } + for (out[0..n]) |*c| { + const value = registerValue(p, c.key); + const bound = if (value.len > 4 * register_cut) modal.graphemeStart(value, 4 * register_cut) else value.len; + const line = value[0 .. std.mem.indexOfScalar(u8, value[0..bound], '\n') orelse bound]; + const cut = File.byteAtDisplay(p.settings.tab, line, register_cut); + c.label = if (value.len == 0) "—" else line[0..cut]; + c.more = cut < value.len; + } + return out[0..n]; +} + +/// Register `key`'s value as a paste of the pane with the keyboard reads +/// its first range's (edit.registerValue): a stored one's first value; `%` +/// the file's name, `.` the primary selection's text, `#` its number, `_` +/// nothing. Borrowed from the register or the pane, for the frame. +pub fn registerValue(p: *const Pardes, key: u8) []const u8 { + const pane = p.panes[p.view().active] orelse return ""; + switch (key) { + '_' => return "", + '#' => return "1", + '%' => return if (pane.file) |*f| f.path else "", + '.' => { + const f = if (pane.file) |*f| f else return ""; + const t = &pane.body; + const b = edit.cellBounds(.{ .row = t.cur_row, .col = t.cur_col, .arow = if (t.vsel.active) t.vsel.row else t.cur_row, .acol = if (t.vsel.active) t.vsel.col else t.cur_col }); + const first = File.sourceLine(pane, b.lo_row); + const last = File.sourceLine(pane, b.hi_row); + if (f.content.len == 0) return ""; + const lo = @min(f.content.len, first.ptr - f.content.ptr + @as(usize, @intCast(@max(0, b.lo_col)))); + const hi = @min(f.content.len, last.ptr - f.content.ptr + @as(usize, @intCast(@max(0, b.hi_col)))); + return f.content[lo..modal.nextGrapheme(f.content, @max(lo, hi))]; + }, + else => for (p.registers.list.items) |*reg| { + if (reg.name == key) return if (reg.values.len == 0) "" else reg.values[0]; + } else return "", + } +} + /// A key as a cell shows it: `SPC` for the space. pub fn keyName(key: u8) []const u8 { return if (key == ' ') "SPC" else @as(*const [1]u8, &printable[key - 0x20]); @@ -450,3 +552,67 @@ test "Help's rows: every leaf of every root, spelled as its chord, its word and try std.testing.expectEqual(@as(usize, 4), found); for (flat) |f| try std.testing.expectEqual(f.row.len, f.lower.len); } + +test "\" shows the registers rolled, each its first line; Down unrolls them beside the cursor's value; a register's key goes down to its verbs, Backspace back up, p pastes it" { + const tagline = @import("tagline.zig"); + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("one\ntwo\n"); + try p.registers.put(p.gpa, 'a', "const best = rank(q, name) orelse continue;\nreturn best;", 0, 1, true); + try p.registers.put(p.gpa, '"', "self.dirty = true;", 0, 1, true); + 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(); + // Its key cell at once, the level 300 ms on. + p.update(.{ .key = .{ .cp = '"' } }); + try std.testing.expect(std.mem.endsWith(u8, try tagline.tagText(p, a, pane), " \" …")); + p.advance(p.clock.now_ns); + try std.testing.expectEqual(p.clock.now_ns + 300 * std.time.ns_per_ms, p.view().tag_state.due_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + const rolled = try tagline.tagText(p, a, pane); + try std.testing.expect(std.mem.indexOf(u8, rolled, " \" ▸ \" self.dirty = true; a const best = rank(q, nam… / —") != null); + try std.testing.expect(std.mem.indexOf(u8, rolled, "% /test.txt . o # 1 _ —") != null); + // Down unrolls: a row each, the cursor on the default, its value + // beside them. + p.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expect(p.view().tag_state.unrolled); + 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 band: std.ArrayList(u8) = .empty; + for (0..layer.rows) |row| for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| try band.appendSlice(a, cell.grapheme()); + try std.testing.expect(std.mem.indexOf(u8, band.items, "▶ \" yank self.dirty = true;") != null); + try std.testing.expect(std.mem.indexOf(u8, band.items, "\"\" · yank") != null); + // The next row's value in full, every line of it. + p.update(.{ .key = .{ .cp = Key.down } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(a); + band = .empty; + for (0..layer.rows) |row| for (layer.cells[row * layer.cols ..][0..layer.cols]) |cell| try band.appendSlice(a, cell.grapheme()); + try std.testing.expect(std.mem.indexOf(u8, band.items, "const best = rank(q, name) orelse continue;") != null); + try std.testing.expect(std.mem.indexOf(u8, band.items, "return best;") != null); + // Enter chooses it: its verbs, rolled. + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), " \" a ▸ p after P before R replace y into") != null); + try std.testing.expect(!p.view().tag_state.unrolled); + // Backspace: the registers again; a register's own key goes down too. + p.update(.{ .key = .{ .cp = Key.backspace } }); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), " \" ▸ \" self.dirty") != null); + p.update(.{ .key = .{ .cp = 'a' } }); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagText(p, a, pane), " \" a ▸ p after") != null); + // p pastes it after the selection, and the tag is at rest. + p.update(.{ .key = .{ .cp = 'p' } }); + try std.testing.expect(std.mem.indexOf(u8, pane.file.?.content, "const best = rank(q, name) orelse continue;") != null); + try std.testing.expect(p.view().tag_state.root == null); + try std.testing.expect(@import("TagState.zig").hostOf(p) == null); + // Esc from the registers: rest, the keyboard where it was. + const id = p.view().active; + p.update(.{ .key = .{ .cp = '"' } }); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(p.view().tag_state.root == null); + try std.testing.expectEqual(id, p.view().active); + try std.testing.expectEqual(@as(u21, 0), pane.body.normal.register); +} diff --git a/core/keys.zig b/core/keys.zig index 04a3cc35..78f45820 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -376,7 +376,7 @@ pub fn handleKey(p: *Pardes, key: Key) void { 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 (prefixKey(p, t, cur, 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 @@ -679,15 +679,27 @@ 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 { +fn prefixKey(p: *Pardes, t: *Text, cur: TagState.Current, key: Key) bool { + const root = cur.root; 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)) { + // `"` and a register's verbs: Esc goes to rest (normal mode's would + // name the register, or leave the pane). + if (root == .register and hit(key, config.escape)) { + t.normal.clear(); + return true; + } + switch (levelKey(p, root, cur.path, key)) { .taken => return true, .up => { t.normal = .{}; + // A register's verbs go back up to the registers (SPEC §9). + if (root == .register and cur.path.len > 0) { + t.normal.prefix = .register_prefix; + p.view().tag_state.hi = 0; + } return true; }, .take => |c| { @@ -697,7 +709,15 @@ fn prefixKey(p: *Pardes, t: *Text, root: bindings.Root, key: Key) bool { .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); + var buf: [2]u8 = .{ 0, k }; + if (cur.path.len > 0) buf[0] = cur.path[0]; + const here = if (cur.path.len > 0) buf[0..2] else buf[1..2]; + if (bindings.nodeAt(root, here) != null) TagState.ran(p.view(), root, cur.path, k); + // A register named: its verbs, rolled, the cursor afresh. + if (root == .register) { + p.view().tag_state.hi = 0; + p.view().tag_state.unrolled = false; + } } return false; }, |
