diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-24 19:37:15 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-24 19:38:18 -0300 |
| commit | 480f36b85299fa7407c20a2d6482dd093f842bf7 (patch) | |
| tree | 5d07c37c3e4eae82ec35ed100799d5810a8912aa /core | |
| parent | f44d1b7d040acf674814be3cde336527833450e9 (diff) | |
| parent | 9f63733156853c925c31a948b50d4a3a7b77aa70 (diff) | |
| download | pardes-480f36b85299fa7407c20a2d6482dd093f842bf7.tar.gz pardes-480f36b85299fa7407c20a2d6482dd093f842bf7.zip | |
walk UI wave-2 base: line A items 1-4 (kquzozvs) merged with line B items 1-5 (mqmtqvtt)
Diffstat (limited to 'core')
| -rw-r--r-- | core/File.zig | 2 | ||||
| -rw-r--r-- | core/Jump.zig | 4 | ||||
| -rw-r--r-- | core/Trail.zig | 662 | ||||
| -rw-r--r-- | core/builtins.zig | 11 | ||||
| -rw-r--r-- | core/colors.zig | 5 | ||||
| -rw-r--r-- | core/config.zig | 12 | ||||
| -rw-r--r-- | core/draw.zig | 62 | ||||
| -rw-r--r-- | core/layout.zig | 43 | ||||
| -rw-r--r-- | core/look.zig | 134 | ||||
| -rw-r--r-- | core/mouse.zig | 16 | ||||
| -rw-r--r-- | core/panes.zig | 7 | ||||
| -rw-r--r-- | core/root.zig | 2 | ||||
| -rw-r--r-- | core/tagline.zig | 75 | ||||
| -rw-r--r-- | core/themes/acme.zig | 2 |
14 files changed, 970 insertions, 67 deletions
diff --git a/core/File.zig b/core/File.zig index 07dfa52d..2affde32 100644 --- a/core/File.zig +++ b/core/File.zig @@ -1312,6 +1312,8 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who } f.revision +%= 1; f.splice = .{ .revision = f.revision, .head = span.head, .tail = span.tail, .old_len = old_len }; + // Edited, a reading pane is held (pick-ui SPEC §6.5). + if (how == .edit) pardes.Trail.edited(p, f); if (!told) { // The file's text is as it was: as clean as it was, its // declarations still its own. diff --git a/core/Jump.zig b/core/Jump.zig index a83b6ab4..a4ecca04 100644 --- a/core/Jump.zig +++ b/core/Jump.zig @@ -274,6 +274,10 @@ pub fn trackJump(p: *Pardes) void { /// always have, at the newest entry. pub fn jumpBy(p: *Pardes, delta: i32) void { const v = p.view(); + // A reading pane's walk first (pick-ui SPEC §6.4). Past its newest + // step, Ctrl-i is to step the Walk queue once the pickers make one; + // until then, the jump list's. + if (pardes.Trail.walkBy(p, v.active, delta)) return; const older = delta < 0; // At an end of the list: said, not a step that silently does nothing. if (v.jumps.n == 0 or (older and v.jumps.cur == 0) or (!older and v.jumps.cur + 1 >= v.jumps.n)) diff --git a/core/Trail.zig b/core/Trail.zig new file mode 100644 index 00000000..7166d810 --- /dev/null +++ b/core/Trail.zig @@ -0,0 +1,662 @@ +//! A pane's walk (pick-ui SPEC §6): the places a reading pane stepped +//! through, written as words in its tag before its path, and whether it +//! is held. A held pane (edited, kept, or not a file) never walks: a jump +//! from it steps its one reader, a reading pane below it whose trail +//! starts with it (`‹@p3`). A reading pane steps in place. +//! +//! The steps are one array, oldest first: `cur` is where the pane is (its +//! place as it was at its last step: the caret is the truth between +//! steps), the steps before it the back steps, the ones after it the +//! forward steps a step back left, drawn dimmed after the path. +const std = @import("std"); +const pardes = @import("root.zig"); +const Kind = @import("Kind.zig"); +const config = @import("config.zig"); +const layout = @import("layout.zig"); +const panes = @import("panes.zig"); +const look = @import("look.zig"); +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const Spot = look.Spot; + +const Trail = @This(); + +/// The steps a trail keeps; past it the oldest go, the owner kept first. +pub const max_steps = 16; +/// The bytes of a step's path or word. A jump to a longer path is no step: +/// it opens a pane as `Jumps new-pane` does (look.zig open). +pub const max_text = 256; +/// The newest back steps written whole; older ones fold to `·` (§6.3). +pub const shown_steps = 3; + +/// `cur` held to the steps there are (walk.zig `.place`). +pub const replica = .{ .len = .{.{ "steps", "n" }}, .place = .{.{ "cur", "lastCur" }} }; + +steps: [max_steps]Step = undefined, +n: u8 = 0, +cur: u8 = 0, +/// Kept, or edited since it was shown: it no longer walks (§6.5). +held: bool = false, + +pub fn lastCur(t: *const Trail) usize { + return t.n -| 1; +} + +/// One step of a walk, as its word says it (§2 "trail word"). +pub const Step = union(enum) { + /// A place the pane was at: `rank.zig:14`. + place: Place, + /// The held pane this is the reader of, by its serial: `‹@p3`, always + /// the first step. + owner: u32, + /// A list the pane came through (`Find pick 3/6 •2`, §5.6): its word + /// and the pickers' index for it. Made by the pickers (not yet). + list: Word, + /// A search made in the pane (`/dirty/ 3/7`, §7.3). Made by the + /// prompts (not yet). + search: Word, + /// A chain of selection prompts as an Edit line (§7.5). Made by the + /// prompts (not yet). + recipe: Word, +}; +pub const Place = struct { path: Kind.Buf(max_text) = .{}, line: u32 = 0, col: u32 = 0 }; +pub const Word = struct { text: Kind.Buf(max_text) = .{}, index: u32 = 0 }; + +/// A file the walk can show: a text on its path, not an output, a +/// listing, a pager, a query or a kind (§2: what is not a file is held). +pub fn isFile(pane: *const Pane) bool { + const f = pane.file orelse return false; + return f.output == null and f.listing == null and !f.pager and f.mini == null and pane.query == null and pane.addon == null; +} + +/// A pane that walks: a file not held, in a session whose jumps walk. +pub fn reading(p: *const Pardes, pane: *const Pane) bool { + return p.settings.jumps == .walk and isFile(pane) and !pane.trail.held; +} + +/// The reading pane that is held pane `owner`'s reader, if it has one. +pub fn readerOf(p: *const Pardes, owner: *const Pane) ?usize { + for (p.panes, 0..) |slot, id| { + const r = slot orelse continue; + if (r == owner or !isFile(r) or r.trail.held or r.trail.n == 0) continue; + if (r.trail.steps[0] == .owner and r.trail.steps[0].owner == owner.serial) return id; + } + return null; +} + +/// A new reader's trail: its owner, then where it opened. +pub fn reader(owner: u32, path: []const u8, at: Spot) Trail { + var t: Trail = .{}; + push(&t, .{ .owner = owner }); + if (placeOf(path, at.line, at.col)) |s| push(&t, s); + t.cur = t.n - 1; + return t; +} + +/// A place as a step, null when its path does not fit one. +fn placeOf(path: []const u8, line: usize, col: usize) ?Step { + if (path.len > max_text) return null; + var at: Place = .{ .line = std.math.lossyCast(u32, line), .col = std.math.lossyCast(u32, col) }; + at.path.set(path); + return .{ .place = at }; +} + +/// Where `pane` is now, as a step. +fn here(pane: *const Pane) ?Step { + const f = pane.file orelse return null; + return placeOf(f.path, @as(usize, @intCast(@max(0, pane.body.cur_row))) + 1, @as(usize, @intCast(@max(0, pane.body.cur_col))) + 1); +} + +fn samePlace(s: Step, path: []const u8, line: usize) bool { + return s == .place and s.place.line == line and std.mem.eql(u8, s.place.path.slice(), path); +} + +/// `s` as the newest step, the oldest that is not the owner dropped when +/// the trail is full. +fn push(t: *Trail, s: Step) void { + if (t.n == max_steps) { + const first: usize = @intFromBool(t.steps[0] == .owner); + std.mem.copyForwards(Step, t.steps[first .. max_steps - 1], t.steps[first + 1 ..]); + t.n -= 1; + t.cur -|= 1; + } + t.steps[t.n] = s; + t.n += 1; +} + +/// Step reading pane `id` to `path` at `at` (§6.2): a place already on its +/// trail is gone back or forward to, never held twice; anything else is a +/// new step, which drops the forward steps. The pane's slot after it: a +/// step to another file is a new pane in its place (`replace`). +pub fn step(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { + const pane = p.panes[id].?; + const t = &pane.trail; + // Where it is already: no step. + if (here(pane)) |was| if (samePlace(was, path, at.line)) return show(p, id, path, at); + for (t.steps[0..t.cur], 0..) |s, i| if (samePlace(s, path, at.line)) return go(p, id, @intCast(i)); + if (t.n > t.cur + 1) for (t.steps[t.cur + 1 .. t.n], t.cur + 1..) |s, j| if (samePlace(s, path, at.line)) return go(p, id, @intCast(j)); + const to = placeOf(path, at.line, at.col) orelse return error.NameTooLong; + // Where it was: the current step's place, or a first one. + if (here(pane)) |was| { + if (t.n > t.cur) { + t.n = t.cur + 1; + t.steps[t.cur] = was; + } else push(t, was); + } else t.n = t.cur; + push(t, to); + t.cur = t.n - 1; + return show(p, id, path, at); +} + +/// Go to step `i` of reading pane `id`'s trail, back or forward (§6.4): +/// where it is now becomes the step it leaves, the ones between stay. +pub fn go(p: *Pardes, id: usize, i: u8) !usize { + const pane = p.panes[id].?; + const t = &pane.trail; + if (i >= t.n or t.steps[i] != .place) return id; + const s = t.steps[i]; + if (t.n > t.cur) if (here(pane)) |was| { + t.steps[t.cur] = was; + }; + t.cur = i; + return show(p, id, s.place.path.slice(), .{ .line = s.place.line, .col = s.place.col }); +} + +/// Pane `id` showing `path` at `at`, the keyboard on it: its caret moved +/// in the file it shows, or a new pane on `path` in its place, its walk +/// with it. A line past the new file's end is a miss that changes nothing. +fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { + const pane = p.panes[id].?; + if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) { + look.clearNavigationSelection(p, pane); + look.focusPaneLine(p, id, at, .center); + return id; + }; + const free = p.freeSlot() orelse return error.NoPaneSlots; + const nt = try panes.File.open(p, free, path, at.line); + // Refused: closed at the step's end, never placed (Pardes.unplaced). + errdefer { + p.unplaced.set(free); + nt.vweight = 0; + } + if (at.line > panes.File.nlines(p.gpa, &nt.file.?)) return error.LineMissing; + nt.trail = pane.trail; + // Before the layout gives its place away: its file is told closed + // while it is still in its slot. + try panes.deinitPane(p, pane); + layout.replacePane(p, id, free); + p.panes[id] = null; + layout.compute(p); + look.focusPaneLine(p, free, at, .keep); + // Its line half the body's rows down, as a look's new pane has it. + const rows = @max(1, p.view().rects[free].h -| pardes.BOX_H); + panes.File.setTop(nt, panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, rows / 2)).to); + return free; +} + +/// Pane `id` held (§6.5): Keep, or an edit. It no longer walks, so the +/// walk leaves its trail (places, lists, its owner, forward steps); words +/// about its own text (a search, a recipe) stay. The next jump from its +/// old owner makes a new reader. +pub fn keep(pane: *Pane) void { + const t = &pane.trail; + if (t.held) return; + t.held = true; + var w: u8 = 0; + for (t.steps[0..t.cur]) |s| if (s == .search or s == .recipe) { + t.steps[w] = s; + w += 1; + }; + t.n = w; + t.cur = w; +} + +/// An edit of file `f`: the panes showing it are held from now on (§6.5). +pub fn edited(p: *Pardes, f: *const panes.File.State) void { + for (p.panes) |slot| { + const pane = slot orelse continue; + if (pane.trail.held) continue; + const shown = if (pane.file) |*pf| pf else continue; + if (shown == f or (f.twin != 0 and shown.twin == f.twin)) keep(pane); + } +} + + +/// Back (`delta` < 0) or forward one place in reading pane `id`'s walk, +/// as Ctrl-o and Ctrl-i go first (pick-ui SPEC §6.4): true when it had +/// one that way, false to leave the step to the jump list (Jump.jumpBy). +pub fn walkBy(p: *Pardes, id: usize, delta: i32) bool { + const pane = p.panes[id] orelse return false; + if (!reading(p, pane)) return false; + const t = &pane.trail; + var i: usize = t.cur; + while (if (delta < 0) i > 0 else i + 1 < t.n) { + i = if (delta < 0) i - 1 else i + 1; + if (t.steps[i] != .place) continue; + _ = go(p, id, @intCast(i)) catch |err| p.reportError(.at(id), "walk", err); + return true; + } + return false; +} + +// ---- the words ---- + +pub const Part = enum { back, forward }; + +/// Step `i`'s word: `‹@p3`, `rank.zig:14`, a list's, search's or recipe's +/// own text; `·` when it is folded. +fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 { + const s = t.steps[i]; + if (s != .owner and i < t.cur and t.cur - i > shown_steps) return "·"; + return switch (s) { + .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits + .place => |at| std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(at.path.slice()), at.line }) catch unreachable, // a basename and a u32 fit + .list, .search, .recipe => |w| w.text.slice(), + }; +} + +const sep = " › "; + +/// The steps a part writes. +fn range(t: *const Trail, part: Part) [2]usize { + return switch (part) { + .back => .{ 0, t.cur }, + .forward => .{ @min(t.n, t.cur + 1), t.n }, + }; +} + +/// The back steps' words, each with the separator after it (before the +/// path), or the forward steps', each with one before it (after the path). +pub fn write(t: *const Trail, part: Part, w: *std.Io.Writer) std.Io.Writer.Error!void { + var buf: [max_text + 16]u8 = undefined; + const r = range(t, part); + for (r[0]..r[1]) |i| switch (part) { + .back => try w.print("{s}" ++ sep, .{wordOf(t, i, &buf)}), + .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, &buf)}), + }; +} + +/// The bytes `write` writes for `part`. +pub fn bytes(t: *const Trail, part: Part) usize { + var buf: [max_text + 16]u8 = undefined; + var n: usize = 0; + const r = range(t, part); + for (r[0]..r[1]) |i| n += wordOf(t, i, &buf).len + sep.len; + return n; +} + +/// The step whose word in `part` is at byte `off` of what `write` wrote +/// for it, null on a separator or past them. +pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { + var buf: [max_text + 16]u8 = undefined; + var x: usize = 0; + const r = range(t, part); + for (r[0]..r[1]) |i| { + if (part == .forward) x += sep.len; + const n = wordOf(t, i, &buf).len; + if (off >= x and off < x + n) return @intCast(i); + x += n; + if (part == .back) x += sep.len; + } + return null; +} + +/// Look on step `i` of pane `id`'s trail (§6.3): a place is gone back or +/// forward to, the owner focused; a list, a search or a recipe is the +/// pickers' and the prompts' to take (not yet: said). +pub fn lookStep(p: *Pardes, id: usize, i: u8) void { + const pane = p.panes[id] orelse return; + switch (pane.trail.steps[i]) { + .place => _ = go(p, id, i) catch |err| p.reportError(.at(id), "walk", err), + .owner => |serial| if (p.paneBySerial(serial)) |owner| { + p.view().active = owner; + } else p.reportFailure(id, "walk: its owner is closed"), + .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not walked yet"), + } +} + +/// Exec on step `i` (§6.3): a place and the owner as Look; a list runs its +/// source again, a search searches again, a recipe replays (not yet). +pub fn execStep(p: *Pardes, id: usize, i: u8) void { + const pane = p.panes[id] orelse return; + switch (pane.trail.steps[i]) { + .place, .owner => lookStep(p, id, i), + .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not run again yet"), + } +} + +fn typeKeys(p: *Pardes, keys: []const u8) void { + for (keys) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); +} + +fn tagOf(p: *Pardes, pane: *Pane) ![]const u8 { + return pardes.tagline.tagText(p, p.scratch.allocator(), pane); +} + +/// The ground of the cell under the first cell of pane `id`'s tag text. +fn tagGroundOf(p: *Pardes, arena: *std.heap.ArenaAllocator, id: usize) !pardes.Color { + p.sync(); + _ = arena.reset(.retain_capacity); + const s = try p.render(arena.allocator()); + const r = p.view().rects[id]; + return s.at(r.x + pardes.TAG_TEXT_INSET, p.tagTop(p.panes[id].?, r)).style.bg; +} + +test "a file's pane is reading, its tag in the reading tint with Keep after its path; typed into, it is held, and Jumps new-pane reads none" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + p.settings.jumps = .walk; + const pane = try p.setTestFile("alpha\nbeta\n"); + try std.testing.expect(reading(p, pane)); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, pane), "/test.txt Keep ")); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().tag_reading_bg }, try tagGroundOf(p, &arena, 0)); + // Under Jumps new-pane nothing reads: the tag as it always was. + p.settings.jumps = .@"new-pane"; + try std.testing.expect(!reading(p, pane)); + try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del", try tagOf(p, pane)); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().tag_active_bg }, try tagGroundOf(p, &arena, 0)); + p.settings.jumps = .walk; + // Typed into: held from now on, though Undo takes the text back. + typeKeys(p, "ix"); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + typeKeys(p, "u"); + try std.testing.expectEqualStrings("alpha\nbeta\n", pane.file.?.content); + try std.testing.expect(!reading(p, pane)); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, pane), "Keep") == null); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().tag_active_bg }, try tagGroundOf(p, &arena, 0)); +} + +test "Keep holds a reading pane; a scratch and a listing are never reading" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + p.settings.jumps = .walk; + const pane = try p.setTestFile("alpha\n"); + pardes.exec.runBuiltin(p, .Keep, 0, "Keep", null); + try std.testing.expect(!reading(p, pane)); + try std.testing.expect(pane.trail.held); + pardes.exec.runBuiltin(p, .New, 0, "New", null); + const scratch = p.panes[p.view().active].?; + try std.testing.expect(scratch != pane and scratch.file != null and !reading(p, scratch)); + var dir = std.testing.tmpDir(.{}); + defer dir.cleanup(); + var buf: [4096]u8 = undefined; + const len = try dir.dir.realPath(std.testing.io, &buf); + pardes.look.lookAt(p, 0, buf[0..len]); + const listing = p.panes[p.view().active].?; + try std.testing.expect(listing.file.?.listing != null and !reading(p, listing)); +} + +/// A session over three files of 100 lines in a directory of its own, +/// search.zig, rank.zig and place.zig; search.zig open and edited (held) +/// in pane `held`, under the session's first pane. +const Fixture = struct { + p: *Pardes, + tmp: std.testing.TmpDir, + dir_buf: [4096]u8 = undefined, + dir: []const u8 = "", + held: usize = 0, + + fn init(f: *Fixture, rows: u16) !void { + f.tmp = std.testing.tmpDir(.{}); + errdefer f.tmp.cleanup(); + var text: [100 * 9]u8 = undefined; + var w: std.Io.Writer = .fixed(&text); + for (1..101) |n| try w.print("line {d}\n", .{n}); + for ([_][]const u8{ "search.zig", "rank.zig", "place.zig" }) |name| + try f.tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() }); + f.dir = f.dir_buf[0..try f.tmp.dir.realPath(std.testing.io, &f.dir_buf)]; + f.p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = rows }); + errdefer f.p.deinit(); + _ = try f.p.setTestFile("scratch\n"); + f.p.settings.jumps = .@"new-pane"; + f.look(0, "search.zig", 88); + f.p.settings.jumps = .walk; + f.held = f.p.view().active; + typeKeys(f.p, "ix"); + f.p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + f.p.sync(); + } + + fn deinit(f: *Fixture) void { + f.p.deinit(); + f.tmp.cleanup(); + } + + /// A Look from pane `id` at `name:line` in the fixture's directory. + fn look(f: *Fixture, id: usize, name: []const u8, line: usize) void { + var buf: [4096 + 64]u8 = undefined; + pardes.look.lookAt(f.p, id, std.fmt.bufPrint(&buf, "{s}/{s}:{d}", .{ f.dir, name, line }) catch unreachable); + f.p.sync(); + } + + fn active(f: *Fixture) *Pane { + return f.p.panes[f.p.view().active].?; + } + + /// The active pane's file's name and line, as `rank.zig:12`. + fn at(f: *Fixture, buf: []u8) []const u8 { + const pane = f.active(); + return std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(pane.file.?.path), pane.body.cur_row + 1 }) catch unreachable; + } +}; + +test "a jump from a held pane steps its reader, made under it with half its rows; the reader walks in place, and a place on its trail is gone back to, never held twice" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const held = f.p.panes[f.held].?; + try std.testing.expect(!reading(f.p, held)); + const before = f.p.view().rects[f.held]; + var buf: [64]u8 = undefined; + // g d on rank: its reader, under it, half its rows. + f.look(f.held, "rank.zig", 12); + const r1 = f.p.view().active; + try std.testing.expect(r1 != f.held); + try std.testing.expectEqualStrings("rank.zig:12", f.at(&buf)); + try std.testing.expectEqual(held.serial, f.active().trail.steps[0].owner); + const col = layout.findPane(f.p, f.held).?; + try std.testing.expectEqual(col.col, layout.findPane(f.p, r1).?.col); + try std.testing.expectEqual(col.idx + 1, layout.findPane(f.p, r1).?.idx); + try std.testing.expect(f.p.view().rects[f.held].h <= before.h / 2 + 1); + // Look at place.zig:8 in the reader, from line 14: the reader walks. + typeKeys(f.p, "2j"); + f.look(r1, "place.zig", 8); + const r2 = f.p.view().active; + try std.testing.expect(f.p.panes[r1] == null or r2 == r1); + try std.testing.expectEqualStrings("place.zig:8", f.at(&buf)); + try std.testing.expectEqual(layout.findPane(f.p, f.held).?.idx + 1, layout.findPane(f.p, r2).?.idx); + // g d on Span, from line 9: one more step in the same file. + typeKeys(f.p, "j"); + f.look(r2, "place.zig", 40); + try std.testing.expectEqual(r2, f.p.view().active); + try std.testing.expectEqualStrings("place.zig:40", f.at(&buf)); + const t = &f.active().trail; + try std.testing.expectEqual(@as(u8, 4), t.n); + try std.testing.expectEqual(@as(u8, 3), t.cur); + try std.testing.expectEqual(@as(u32, 14), t.steps[1].place.line); + try std.testing.expectEqual(@as(u32, 9), t.steps[2].place.line); + // The next jump from the held pane lands in the same reader. + f.look(f.held, "rank.zig", 30); + const r3 = f.p.view().active; + try std.testing.expectEqualStrings("rank.zig:30", f.at(&buf)); + try std.testing.expectEqual(held.serial, f.active().trail.steps[0].owner); + var readers: usize = 0; + for (f.p.panes) |slot| if (slot) |pane| { + readers += @intFromBool(pane.trail.n > 0 and pane.trail.steps[0] == .owner); + }; + try std.testing.expectEqual(@as(usize, 1), readers); + // A Look at a place on its trail goes back to it: nothing pushed. + f.look(r3, "rank.zig", 14); + try std.testing.expectEqualStrings("rank.zig:14", f.at(&buf)); + try std.testing.expectEqual(@as(u8, 1), f.active().trail.cur); + try std.testing.expectEqual(@as(u8, 5), f.active().trail.n); +} + +test "a Look from inside the reader at a file its held pane shows focuses the held pane and replaces nothing" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + f.look(f.held, "rank.zig", 12); + const reader_id = f.p.view().active; + const reader_pane = f.active(); + f.look(reader_id, "search.zig", 89); + try std.testing.expectEqual(f.held, f.p.view().active); + try std.testing.expectEqual(@as(i32, 88), f.active().body.cur_row); + try std.testing.expectEqual(reader_pane, f.p.panes[reader_id].?); + try std.testing.expectEqualStrings("rank.zig", std.Io.Dir.path.basename(reader_pane.file.?.path)); +} + +test "the reader of a held pane too short to halve goes to the bottom of the next column, of the previous one from the last" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(24); + defer f.deinit(); + const col = layout.findPane(f.p, f.held).?.col; + try std.testing.expect(f.p.view().rects[f.held].h / 2 < f.p.panes[f.held].?.tag_rows + layout.reader_keeps); + try std.testing.expect(layout.insertColumn(f.p, col, false) != null); + f.look(f.held, "rank.zig", 12); + const reader_id = f.p.view().active; + try std.testing.expectEqual(col + 1, layout.findPane(f.p, reader_id).?.col); +} + +/// Where `word` starts in pane `id`'s tag, on the screen. +fn tagCell(p: *Pardes, id: usize, word: []const u8) ![2]u16 { + const pane = p.panes[id].?; + const tag = try tagOf(p, pane); + const i = std.mem.indexOf(u8, tag, word) orelse return error.NotInTag; + const r = p.view().rects[id]; + return .{ r.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(p.settings.tab, tag[0..i]))), p.tagTop(pane, r) }; +} + +/// A click with `button` on `word` in pane `id`'s tag: a press and a +/// release on its first cell. +fn clickTag(p: *Pardes, id: usize, word: []const u8, button: pardes.Mouse.Button) !void { + const at = try tagCell(p, id, word); + for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| p.update(.{ .mouse = .{ .button = button, .kind = kind, .col = at[0], .row = at[1] } }); + p.sync(); +} + +test "the session of pick-ui §6.7: four jumps across three files, back by a click on a trail word, the later steps kept dimmed after the path, forward again, Keep, and a new reader on the next jump" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + const owner = p.panes[f.held].?.serial; + var buf: [4096 + 128]u8 = undefined; + var at: [64]u8 = undefined; + // 1 · g d on rank from the held pane: its reader, made below it. + f.look(f.held, "rank.zig", 12); + // 2 · Look at Place.of from line 14 of rank.zig: the reader walks. + typeKeys(p, "2j"); + f.look(p.view().active, "place.zig", 8); + // 3 · g d on Span from line 9: one more step in the same file. + typeKeys(p, "j"); + f.look(p.view().active, "place.zig", 40); + var r = p.view().active; + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:14 › place.zig:9 › {s}/place.zig Keep ", .{ owner, f.dir }))); + // 4 · a click on rank.zig:14: back, the later steps after the path, + // dimmed. + try clickTag(p, r, "rank.zig:14", .left); + r = p.view().active; + try std.testing.expectEqualStrings("rank.zig:14", f.at(&at)); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › {s}/rank.zig › place.zig:9 › place.zig:40 Keep ", .{ owner, f.dir }))); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const dim = try tagCell(p, r, "place.zig:40"); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().lineno }, s.at(dim[0], dim[1]).style.fg); + // A click on a forward step goes forward to it. + try clickTag(p, r, "place.zig:9", .left); + r = p.view().active; + try std.testing.expectEqualStrings("place.zig:9", f.at(&at)); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:14 › {s}/place.zig › place.zig:40 Keep ", .{ owner, f.dir }))); + // Exec (B2) on the owner's word: the owner has the keyboard. + try clickTag(p, r, "‹@p", .middle); + try std.testing.expectEqual(f.held, p.view().active); + // A new step from the reader drops the forward steps. + f.look(r, "rank.zig", 50); + r = p.view().active; + try std.testing.expectEqual(p.panes[r].?.trail.cur + 1, p.panes[r].?.trail.n); + // 5 · Keep: the walk leaves its tag; the next jump from the owner + // makes a new reader. + pardes.exec.runBuiltin(p, .Keep, r, "Keep", null); + p.sync(); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "{s}/rank.zig Save", .{f.dir}))); + f.look(f.held, "place.zig", 20); + const fresh = p.view().active; + try std.testing.expect(fresh != r and fresh != f.held and p.panes[r] != null); + try std.testing.expectEqual(owner, p.panes[fresh].?.trail.steps[0].owner); +} + +test "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + const owner = p.panes[f.held].?.serial; + for ([_]usize{ 10, 20, 30, 40, 50 }) |line| f.look(f.held, "rank.zig", line); + const r = p.view().active; + var buf: [4096 + 128]u8 = undefined; + var at: [64]u8 = undefined; + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › · › rank.zig:20 › rank.zig:30 › rank.zig:40 › {s}/rank.zig Keep ", .{ owner, f.dir }))); + try clickTag(p, r, "·", .right); + try std.testing.expectEqualStrings("rank.zig:10", f.at(&at)); +} + +test "Ctrl-o goes back one place in a reader's walk and Ctrl-i forward again; past its first place, or from a held pane, the jump list's" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + var at: [64]u8 = undefined; + f.look(f.held, "rank.zig", 12); + f.look(p.view().active, "place.zig", 8); + f.look(p.view().active, "place.zig", 40); + const back: pardes.Key = .{ .cp = 'o', .ctrl = true }; + const ahead: pardes.Key = .{ .cp = 'i', .ctrl = true }; + p.update(.{ .key = back }); + p.sync(); + try std.testing.expectEqualStrings("place.zig:8", f.at(&at)); + p.update(.{ .key = back }); + p.sync(); + try std.testing.expectEqualStrings("rank.zig:12", f.at(&at)); + p.update(.{ .key = ahead }); + p.sync(); + try std.testing.expectEqualStrings("place.zig:8", f.at(&at)); + p.update(.{ .key = back }); + p.sync(); + try std.testing.expectEqualStrings("rank.zig:12", f.at(&at)); + // The owner's word is no place: past the first, the jump list's. + try std.testing.expect(!walkBy(p, p.view().active, -1)); + try std.testing.expect(!walkBy(p, f.held, -1)); +} + +test "closing a held pane closes its reader; a kept reader stays; typed into, a reader is held and its walk leaves its tag" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + const owner = p.panes[f.held].?.serial; + var buf: [4096 + 128]u8 = undefined; + f.look(f.held, "rank.zig", 12); + f.look(p.view().active, "place.zig", 8); + const r = p.view().active; + // Typed into: held, its walk gone from its tag, kept when its owner goes. + typeKeys(p, "ix"); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.sync(); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "{s}/place.zig Save", .{f.dir}))); + try std.testing.expectEqual(@as(u8, 0), p.panes[r].?.trail.n); + // The next jump from its old owner makes a new reader. + f.look(f.held, "rank.zig", 30); + const fresh = p.view().active; + try std.testing.expect(fresh != r); + try std.testing.expectEqual(owner, p.panes[fresh].?.trail.steps[0].owner); + // Del of the owner: its reader goes with it; the edited one stays. + const fresh_serial = p.panes[fresh].?.serial; + try p.removePane(f.held, null); + p.sync(); + try std.testing.expect(p.paneBySerial(fresh_serial) == null); + try std.testing.expect(p.panes[r] != null); +} diff --git a/core/builtins.zig b/core/builtins.zig index a236b0d6..12ab9da5 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1998,6 +1998,17 @@ pub const Forward = struct { } }; +/// Keep this pane: a reading pane, which a jump steps, is held from now on. +/// +/// Its walk leaves its tag (its places, its owner): the next jump from its +/// owner makes a new reader. Editing a reading pane keeps it too. +pub const Keep = struct { + pub fn run(c: Ctx) void { + if (!pardes.Trail.reading(c.p, c.pane)) return c.p.setMessage(.at(c.id), "Keep: this pane is held already"); + pardes.Trail.keep(c.pane); + } +}; + /// Select the next thing a Look would open: a place in a list, a word that /// names a file. /// diff --git a/core/colors.zig b/core/colors.zig index 5579ed05..84595a30 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -287,6 +287,9 @@ pub const Theme = struct { lineno_active: ?[3]u8 = null, /// A tag's selection, where it differs from the body's (acme's #9eeeee). tag_sel_bg: ?[3]u8 = null, + /// The tag of a reading pane, one a jump steps (Trail.zig): unset, the + /// page tinted toward the search's ground, so it reads as the body's. + tag_reading_bg: ?[3]u8 = null, /// Each pointer button's sweep, select, exec and look (Mouse.Button /// order), and the ink over it. Unset, tints of `sel_bg` in `sel_fg`. sweep_bg: ?[3][3]u8 = null, @@ -543,6 +546,7 @@ pub const ChromeTheme = struct { empty_col: [3]u8, search_bg: [3]u8, search_fg: [3]u8, + tag_reading_bg: [3]u8, diagnostic_error: [3]u8, diagnostic_warning: [3]u8, diagnostic_info: [3]u8, @@ -579,6 +583,7 @@ pub const ChromeTheme = struct { .empty_col = th.empty_col orelse separatorOf(th), .search_bg = th.search_bg orelse th.sel_bg, .search_fg = th.search_fg orelse th.sel_fg, + .tag_reading_bg = th.tag_reading_bg orelse mixOklab(th.bg orelse th.tag_bg, th.search_bg orelse th.sel_bg, 0.4), .diagnostic_error = th.diagnostic_error orelse (th.fg orelse th.tag_fg), .diagnostic_warning = th.diagnostic_warning orelse (th.fg orelse th.tag_fg), .diagnostic_info = th.diagnostic_info orelse (th.fg orelse th.tag_fg), diff --git a/core/config.zig b/core/config.zig index 324102b5..6adebe1b 100644 --- a/core/config.zig +++ b/core/config.zig @@ -144,6 +144,7 @@ pub const leader_path = paths: { .Unfollow = null, .Back = "jo", .Forward = "ji", + .Keep = "jk", .Last = "jj", .Jumplist = "jl", .History = "fh", @@ -157,6 +158,7 @@ pub const leader_path = paths: { table.set(.TaglineSize, null); } table.set(.MessageLinger, null); + table.set(.Jumps, null); table.set(.DumpDir, null); if (builtins.capabilities.window_opacity) table.set(.WindowOpacity, null); if (builtins.capabilities.pixels) table.set(.CursorBlink, null); @@ -668,9 +670,16 @@ pub const Settings = struct { tab: u16 = tab_width, callstack_depth: u8 = @import("lsp/lsp.zig").callstack_default_depth, + /// A jump (a Look at a place, a definition, an error's place) steps + /// the pane it is made from, or its reader, as a walk (Trail.zig); or + /// opens a pane for each file, as before the walk, until the walk's + /// pickers and prompts are in (pick-ui SPEC §14.3). + jumps: Jumps = .@"new-pane", + /// A setting that is one of a few words, each an enum field of Settings. - pub const Choice = enum { pet }; + pub const Choice = enum { pet, jumps }; pub const Pet = enum { off, cat, frog }; + pub const Jumps = enum { walk, @"new-pane" }; pub const tagline_percent_min: u8 = 1; pub const tagline_percent_max: u8 = 100; @@ -798,6 +807,7 @@ pub const Settings = struct { .{ .word = "GripWidth", .action = .grip_width, .doc = "The grip and scrollbar width, as a percent of the theme's rail." }, .{ .word = "InactiveDim", .action = .inactive_dim, .doc = "Fade the unfocused panes' text by a percentage, 0 through 100, 0 off." }, .{ .word = "Pet", .action = .{ .choice = .pet }, .doc = "The window's companion in the workspace tag's blank space: off, cat or frog." }, + .{ .word = "Jumps", .action = .{ .choice = .jumps }, .doc = "A jump steps its pane or that pane's reader (walk), or opens a pane for each file (new-pane)." }, .{ .word = "MessageLinger", .action = .message_linger, .doc = "How long, in milliseconds, a message stays after what dismisses it." }, .{ .word = "DumpDir", .action = .dump_dir, .doc = "The directory of the state file, where Dump writes and Recent is kept. A relative Restore name is looked for there too; bare, the default." }, .{ .word = "Theme", .action = .theme, .doc = "Switch to the theme named: `Theme atelier`. Themes lists them, NextColor steps through them." }, diff --git a/core/draw.zig b/core/draw.zig index 772cbece..bc606f02 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -467,7 +467,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: // its ring. A pixel shell draws the ring (gui.zig gripDecor); a grid, // which cannot, marks dirty in the grip's second cell with a bold `*` // on the dirty colour, a cue that survives any palette. - const band = if (active) chrome.tag_active_bg else chrome.tag_bg; + const band = tagGround(p, pane, active); const dirty = tagline.dirty(pane); const box_bg = gripBg(p, active); const box_style: CellStyle = .{ @@ -642,7 +642,7 @@ fn tagTintRange(pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { } return null; } - var start: usize = 0; + var start: usize = tagline.nameAt(pane); const name = if (pane.file) |file| if (pane.prompt == .name) pane.input.own orelse file.path else file.path else if (pane.addon) |k| blk: { @@ -800,7 +800,7 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r layer.cols = columns; layer.rows = rows; layer.context_rows = 0; - layer.bg = if (v.window_pick == id) p.theme().sel_bg else if (active) chrome.tag_active_bg else chrome.tag_bg; + layer.bg = if (v.window_pick == id) p.theme().sel_bg else tagGround(p, pane, active); layer.cursor = if (v.header != null) null else if (temporary.cursor) |cursor| .{ .x = cursor.x - TAG_TEXT_INSET, .y = cursor.y, .bar = cursor.bar } else @@ -830,7 +830,7 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R const tab = p.settings.tab; const rows = pane.band_rows; const columns = @max(band.columns, 1); - const bg = if (active) chrome.tag_active_bg else chrome.tag_bg; + const bg = tagGround(p, pane, active); const fg = if (active) chrome.tag_active_fg else chrome.tag_fg; const storage = try arena.alloc(Cell, @as(usize, columns) * rows); @memset(storage, .{}); @@ -886,6 +886,33 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R for (0..rows) |y| joinCells(s, r.x + TAG_TEXT_INSET, y0 + @as(u16, @intCast(y)), storage[y * columns ..][0..columns], 0, r.w - TAG_TEXT_INSET, .{ .rgb = fg }); } +/// Bytes `lo` to `hi` of a tag's text in `ink`, over whichever of its +/// shown rows (`lines`, from shown row `top`, drawn at `x`, `y`, `w` +/// wide) they wrapped onto. +fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8) void { + const a = modal.positionAt(shown.text, shown.fromReal(lo)); + const b = modal.positionAt(shown.text, shown.fromReal(hi)); + var row = @max(a.row, top); + while (row <= b.row and row < top + lines.len) : (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 row_y = y + @as(u16, @intCast(row - top)); + while (col < end and col < w) : (col += 1) + s.at(x + @as(u16, @intCast(col)), row_y).style.fg = .{ .rgb = ink }; + } +} + +/// A pane's tag's ground: a reading pane's tint (its focus is its grip's), +/// else the focused tag's or the tag's. +fn tagGround(p: *Pardes, pane: *const Pane, active: bool) [3]u8 { + const chrome = p.chromeTheme(); + if (pardes.Trail.reading(p, pane)) return chrome.tag_reading_bg; + return if (active) chrome.tag_active_bg else chrome.tag_bg; +} + fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { const th = p.theme(); const chrome = p.chromeTheme(); @@ -895,7 +922,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, const rows = pane.tag_rows - pane.band_rows; const page_rgb = th.bg orelse th.tag_bg; // Focus is visible along the tag without changing its live text grid. - const tag_bg = if (active) chrome.tag_active_bg else chrome.tag_bg; + const tag_bg = tagGround(p, pane, active); const tag_fg = if (active) chrome.tag_active_fg else chrome.tag_fg; const tag_style: CellStyle = .{ .fg = .{ .rgb = tag_fg }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }; // Padding belongs to the same font row but keeps its default ink: a @@ -927,7 +954,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, else if (pane.prompt == .name) caret: { // A drafted name is the prefix: its caret is on row 0 of the tag, // the rows it wrapped onto included. - const at = modal.positionAt(shown.text, shown.fromReal(@intCast(@max(0, pane.input.cur_col)))); + const at = modal.positionAt(shown.text, shown.fromReal(tagline.nameAt(pane) + @as(usize, @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 and head == null) caret: { @@ -947,22 +974,11 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, // The name's tint, in the tag's first line, over whichever rows it // wrapped onto. const first_line = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len]; - if (tagTintRange(pane, first_line, prefix_len)) |name| if (name.lo < name.hi) { - const name_fg = if (active) chrome.tag_active_name_fg else chrome.tag_name_fg; - const a = modal.positionAt(shown.text, shown.fromReal(name.lo)); - const b = modal.positionAt(shown.text, shown.fromReal(@min(name.hi, first_line.len))); - 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) - s.at(tx + @as(u16, @intCast(col)), y).style.fg = .{ .rgb = name_fg }; - } - }; + if (tagTintRange(pane, first_line, prefix_len)) |name| if (name.lo < name.hi) + inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), if (active) chrome.tag_active_name_fg else chrome.tag_name_fg); + // A walk's forward steps, after the name, dimmed (Trail.zig). + const ahead = tagline.forwardSpan(pane); + if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), chrome.lineno); // 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| { @@ -1177,7 +1193,7 @@ test "a tag of three rows has its grip on the first, and band under it that stil const top = p.tagTop(pane, r); try std.testing.expect(pane.tag_rows >= 3); const chrome = p.chromeTheme(); - const band = chrome.tag_active_bg; + const band = tagGround(p, pane, true); for (0..config.GUTTER) |dx| { const x = r.x + @as(u16, @intCast(dx)); try std.testing.expectEqual(Color{ .rgb = chrome.box }, s.at(x, top).style.bg); diff --git a/core/layout.zig b/core/layout.zig index f9b9ef81..c2f0f96c 100644 --- a/core/layout.zig +++ b/core/layout.zig @@ -860,8 +860,46 @@ fn blankRows(p: *const Pardes, id: usize) u16 { } /// What a new pane is, and who asked: each rule places them differently. -/// `scratch` is New's, `fs` 9P's pane/new, `errors` a +Errors pane. -pub const NewPane = enum { scratch, fs, shell, doc, command, errors }; +/// `scratch` is New's, `fs` 9P's pane/new, `errors` a +Errors pane, +/// `reader` a held pane's reader (Trail.zig). +pub const NewPane = enum { scratch, fs, shell, doc, command, errors, reader }; + +/// The body rows a held pane keeps above its reader: with fewer, the +/// reader goes to the next column (pick-ui SPEC §6.1). +pub const reader_keeps = 8; + +/// A reader's place (pick-ui SPEC §6.1): under its held pane `from`, +/// taking half its rows, unless that leaves it fewer than `reader_keeps` +/// body rows; then at the bottom of the next column, the previous one +/// when `from`'s is the last, as New takes the bottom of a column. +fn readerPlace(p: *Pardes, from: usize, id: usize) void { + const v = p.view(); + const c = columnOf(p, from); + const r = v.rects[from]; + if (findPane(p, from) != null and r.h / 2 >= p.panes[from].?.tag_rows + reader_keeps) return splitAt(p, c, id, r.y + r.h / 2); + acmePlace(p, if (c + 1 < v.ncol) c + 1 else c -| 1, null, id); +} + +/// Pane `new` in pane `old`'s place in every view, with its rows: a +/// reader's step to another file (Trail.zig). `old` leaves no layout. +pub fn replacePane(p: *Pardes, old: usize, new: usize) void { + const was = p.panes[old].?; + const now = p.panes[new].?; + now.vweight = was.vweight; + now.collapsed = was.collapsed; + for (0..pardes.views.max) |k| if (now.pv_known & was.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { + now.pv[k].vweight = was.pv[k].vweight; + now.pv[k].collapsed = was.pv[k].collapsed; + }; + for (&p.views) |*slot| { + const v = &(slot.* orelse continue); + for (v.cols[0..v.ncol]) |*col| for (col.panes[0..col.n]) |*k| if (k.* == old) { + k.* = new; + }; + v.rects[new] = v.rects[old]; + if (v.active == old) v.active = new; + } +} /// Put new pane `id` in the layout: every new pane comes through here, as /// acme's makenewwindow places it, and every one but a command pane takes @@ -888,6 +926,7 @@ pub fn placeNew(p: *Pardes, from: usize, origin: ?usize, id: usize, kind: NewPan // makenewwindow(nil) for 9P's new (acme.c:877), else (t). .fs => acmePlace(p, acmeColumn(p, from), null, id), .shell, .doc => acmePlace(p, acmeColumn(p, from), asked, id), + .reader => readerPlace(p, from, id), } // No pane is made shorter than its tag and two rows: where the place // chosen has not the room, the column's tallest pane gives it, and diff --git a/core/look.zig b/core/look.zig index ee6debf9..cc3e750f 100644 --- a/core/look.zig +++ b/core/look.zig @@ -20,6 +20,7 @@ const Color = @import("surface.zig").Color; const Pane = panes.Pane; const MAX_PANES = pardes.MAX_PANES; const Layer = @import("Layer.zig"); +const Trail = @import("Trail.zig"); const TagHit = pardes.TagHit; const BOX_H = pardes.BOX_H; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; @@ -1164,14 +1165,14 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void { // own clean start would collapse its dot (and the looker's) before the // address ever read it, so `f:/re/` again would find the same match. var realbuf: [4096]u8 = undefined; - const open: ?usize = if (path.len == 0) null else switch (resolveLookTarget(p, id, path, &realbuf)) { + const there: ?usize = if (path.len == 0) null else switch (resolveLookTarget(p, id, path, &realbuf)) { .pane => |t| if (t.id < MAX_PANES and p.panes[t.id] != null) t.id else null, else => null, }; // `@p<serial>:<address>`: that pane, or, not open, a miss said as one. const by_serial = std.mem.startsWith(u8, path, config.pane_addr); - if (by_serial and open == null) return missed(p, id, "no pane {s}", .{path[0..@min(path.len, 32)]}); - if (open) |o| target = o else if (path.len > 0) { + if (by_serial and there == null) return missed(p, id, "no pane {s}", .{path[0..@min(path.len, 32)]}); + if (there) |o| target = o else if (path.len > 0) { // A path that names nothing to open: the whole of what was written // is looked for, as `zzq:2` is, and a miss quotes it as written. if (!by_serial and resolveLookTarget(p, id, path, &realbuf) == .none) @@ -1189,7 +1190,7 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void { } const pane = p.panes[target] orelse return; const f = pane.file orelse { - if (pane.addon != null) return lookKind(p, id, target, expr, open == null and target != id); + if (pane.addon != null) return lookKind(p, id, target, expr, there == null and target != id); if ((by_serial or path.len == 0) and pane.isTerminal()) return lookTerminalAddress(p, id, target, expr); // Named as the pane is, the bare `:addr` (path empty) included. var serial_name: [32]u8 = undefined; @@ -1206,7 +1207,7 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void { const name = missText(f.path, &cut); // A look whose address fails opens nothing: a pane it opened to read // the address in goes again, the miss said from the pane it came from. - const opened_here = open == null and target != id; + const opened_here = there == null and target != id; const r = a.address(ninep_pane.fileDot(pane)) orelse { // A line past the end says so as `file:N` does, whichever way it // was written: `<file>:N has no line N`. @@ -1497,6 +1498,90 @@ fn settleLine(p: *Pardes, id: usize, path: []const u8, at: Spot, before: usize, } } +/// Where a jump lands: in the pane it walks (`replace`), or always in a +/// new pane under the asking one (`below`, as `Jumps new-pane` opens one). +pub const How = enum { replace, below }; + +/// What a jump did: nothing (refused, said), showed it in a pane there +/// was, or opened a pane for it, which a miss of its line closes again. +pub const Landed = union(enum) { none, shown, opened: usize }; + +/// A jump from pane `from` to `path` at `at`, the keyboard with it: a +/// Look at a place, a definition, an error's place, a list's row (pick-ui +/// SPEC §6.1). With `Jumps walk`: a held pane showing the file is focused +/// and nothing replaced; the asking pane showing it moves in it (a step +/// when it is reading); a reading pane steps in place, a held one steps +/// its reader (Trail.zig), made under it on its first jump. With +/// `Jumps new-pane` as before the walk: a pane showing the file focused, +/// else a new one. +pub fn open(p: *Pardes, from: usize, path: []const u8, at: Spot, how: How) Landed { + if (how == .replace and p.settings.jumps == .walk and path.len <= Trail.max_text and p.panes[from] != null) return walk(p, from, path, at); + if (how == .replace and focusPaneByPath(p, path, at)) return .shown; + return if (openNew(p, from, path, at, .doc)) |free| .{ .opened = free } else .none; +} + +fn walk(p: *Pardes, from: usize, path: []const u8, at: Spot) Landed { + const src = p.panes[from].?; + for (p.panes, 0..) |slot, k| { + const held = slot orelse continue; + if (k == from or !Trail.isFile(held) or Trail.reading(p, held) or !std.mem.eql(u8, held.file.?.path, path)) continue; + clearNavigationSelection(p, held); + focusPaneLine(p, k, at, .center); + return .shown; + } + const reads = Trail.reading(p, src); + if (!reads and if (src.file) |f| std.mem.eql(u8, f.path, path) else false) { + clearNavigationSelection(p, src); + focusPaneLine(p, from, at, .center); + return .shown; + } + const into = if (reads) from else Trail.readerOf(p, src) orelse { + const free = openNew(p, from, path, at, .reader) orelse return .none; + p.panes[free].?.trail = Trail.reader(src.serial, path, at); + return .{ .opened = free }; + }; + _ = Trail.step(p, into, path, at) catch |err| { + switch (err) { + error.LineMissing => missedLine(p, from, path, at.line), + error.NoPaneSlots => p.reportError(.at(from), "look", err), + else => lookOpenFailed(p, from, path, err), + } + return .none; + }; + return .shown; +} + +/// `path` opened at `at` in a new pane placed as `kind` places one from +/// `from`, its line half its body's rows down; null when it is not +/// (refused, said). +fn openNew(p: *Pardes, from: usize, path: []const u8, at: Spot, kind: layout.NewPane) ?usize { + const v = p.view(); + const free = p.freeSlot() orelse { + p.reportError(.at(from), "look", error.NoPaneSlots); + return null; + }; + const nt = panes.File.open(p, free, path, at.line) catch |err| { + lookOpenFailed(p, from, path, err); + return null; + }; + nt.body.cur_col = columnOn(nt, nt.body.cur_row, at.col); + const failures = p.fs.failures; + layout.placeNew(p, from, from, free, kind); + // Refused a place: that is the look's one failure, not a miss of its + // line too. + if (p.fs.failures != failures) return null; + layout.compute(p); + // Its line half the body's rows down, a wrapped line's rows above it + // counted. + const centered = panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, @max(1, v.rects[free].h -| BOX_H) / 2)).to; + panes.File.setTop(nt, centered); + if (at.end_line != 0) { + selectSpan(p, nt, at); + panes.File.setTop(nt, centered); + } + return free; +} + fn focusPaneByPath(p: *Pardes, path: []const u8, at: Spot) bool { const target = openPaneTarget(p, path, at) orelse return false; clearNavigationSelection(p, p.panes[target.pane.id].?); @@ -1654,6 +1739,16 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // A directory's pane, looked at again: read again. if (target.file) |f| if (f.listing != null) return panes.Dir.show(p, id, t.id); const before = v.active; + // A file's pane found by its path, not named `@pN`: a jump to + // the file, which walks (open). + if (p.settings.jumps == .walk and Trail.isFile(target) and !std.mem.startsWith(u8, pl.path, config.pane_addr)) { + switch (open(p, id, target.file.?.path, t.at, .replace)) { + .none => {}, + .shown => settleLine(p, id, trimmed, t.at, before, null), + .opened => |free| settleLine(p, id, trimmed, t.at, before, free), + } + return; + } clearNavigationSelection(p, target); focusPaneLine(p, t.id, t.at, .center); settleLine(p, id, trimmed, t.at, before, null); @@ -1673,34 +1768,15 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { .file => |target| { // An open pane on this path too short for the line: a miss that // touches nothing. - if (openPaneTarget(p, target.path, target.at)) |open| if (p.panes[open.pane.id].?.file) |*f| + if (openPaneTarget(p, target.path, target.at)) |there| if (p.panes[there.pane.id].?.file) |*f| if (target.at.line > panes.File.nlines(p.gpa, f)) return missedLine(p, id, target.path, target.at.line); - // focus an existing pane on this path (rescrolled), else open const before = v.active; - var opened: ?usize = null; - if (!focusPaneByPath(p, target.path, target.at)) { - const free = p.freeSlot() orelse return p.reportError(.at(id), "look", error.NoPaneSlots); - const nt = panes.File.open(p, free, target.path, target.at.line) catch |err| - return lookOpenFailed(p, id, target.path, err); - nt.body.cur_col = columnOn(nt, nt.body.cur_row, target.at.col); - const failures = p.fs.failures; - layout.placeNew(p, id, id, free, .doc); - // Refused a place: that is the look's one failure, not a - // miss of its line too. - if (p.fs.failures != failures) return; - layout.compute(p); - // Its line half the body's rows down, a wrapped line's - // rows above it counted. - const centered = panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, @max(1, v.rects[free].h -| BOX_H) / 2)).to; - panes.File.setTop(nt, centered); - if (target.at.end_line != 0) { - selectSpan(p, nt, target.at); - panes.File.setTop(nt, centered); - } - opened = free; + switch (open(p, id, target.path, target.at, .replace)) { + .none => {}, + .shown => settleLine(p, id, target.path, target.at, before, null), + .opened => |free| settleLine(p, id, target.path, target.at, before, free), } - settleLine(p, id, target.path, target.at, before, opened); }, .kind => |target| { var place: [41]u8 = undefined; diff --git a/core/mouse.zig b/core/mouse.zig index 7a918883..f97bc39b 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -1249,8 +1249,12 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { // click on the path, if it can be renamed, drafts a name. const row: usize = @intCast(@max(0, sl.r1)); const col: usize = @intCast(@max(0, sl.c1)); - if (!dragged and tagline.onName(p.settings.tab, pane, row, col)) { - tagline.draftName(p, pane, col); + // A plain click on a word of its walk is its Look + // (pick-ui SPEC §6.3): the one place B1 does more than + // select. + if (!dragged and tagline.trailClick(p, s.id, row, col, false)) return; + if (if (dragged) null else tagline.onName(p.settings.tab, pane, row, col)) |at| { + tagline.draftName(p, pane, at); } else if (tagline.tagOffsetAt(p, pane, row, col)) |cursor| { pane.tag.cur_row = @intCast(cursor.row); pane.tag.cur_col = @intCast(cursor.col); @@ -1316,6 +1320,14 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { return; } }; + // On a word of the tag's walk: that step's Look or Exec + // (Trail.zig), not the word's. + if (clk.r0 >= 0 and clk.r0 < pane.tag_rows and clk.r0 == clk.r1 and clk.c0 == clk.c1 and clk.c0 >= 0 and + tagline.trailClick(p, s.id, @intCast(clk.r0), @intCast(clk.c0), s.button == config.exec_button)) + { + pane.sel[b].state = .none; + return; + } var operand = exec.operandAt(p, pane, clk, true, s.button == config.look_button); if (operand.expanded) |expanded| pane.sel[b] = expanded diff --git a/core/panes.zig b/core/panes.zig index 20feb0b3..115323c4 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -15,6 +15,7 @@ const dump = @import("dump.zig"); const memory = @import("memory.zig"); const ctlfs = @import("ninep/tree.zig"); const Pipe = @import("Pipe.zig"); +const Trail = @import("Trail.zig"); const Key = pardes.Key; const Cell = pardes.Cell; const BOX_H = pardes.BOX_H; @@ -269,6 +270,9 @@ pub const Pane = struct { /// the current one selected (Places.zig). places: Places = .{}, look_at: ?LookSpot = null, + /// Its walk: the places it stepped through, its owner when it is a + /// reader, whether it is held (Trail.zig, pick-ui SPEC §6). + trail: Trail = .{}, /// `gw`'s words, labelled in order while it waits for a label to be /// typed (gpa-owned); `jump_first` is the label's first letter once typed. jump: std.ArrayList(modal.Selection) = .empty, @@ -1508,6 +1512,9 @@ pub fn removePane(p: *Pardes, id: usize, to: ?usize) error{OutOfMemory}!void { try p.removePane(k, id); layout.compute(p); }; + // A held pane's reader is its tether: it closes with it (pick-ui + // SPEC §11), its rows going where a closed pane's go. + if (Trail.readerOf(p, pane)) |r| try p.removePane(r, null); // A docked pane's rows go back to its owner and nobody else. const owner = layout.ownerOf(p, id); const place = layout.findPane(p, id); diff --git a/core/root.zig b/core/root.zig index b6835115..36d940b2 100644 --- a/core/root.zig +++ b/core/root.zig @@ -117,6 +117,7 @@ pub const font_picker = platform == .gui or platform == .macos; pub var turn: Turn = .{}; pub const Turn = @import("Turn.zig"); pub const Jump = @import("Jump.zig"); +pub const Trail = @import("Trail.zig"); const Loc = Jump.Loc; pub const can_attach = platform == .tui or platform == .gui; @@ -408,6 +409,7 @@ test { _ = @import("Plumb.zig"); _ = @import("lsp/Ask.zig"); _ = @import("Jump.zig"); + _ = @import("Trail.zig"); _ = @import("keys.zig"); _ = @import("bindings.zig"); _ = @import("TagState.zig"); diff --git a/core/tagline.zig b/core/tagline.zig index a6a85217..25eb8387 100644 --- a/core/tagline.zig +++ b/core/tagline.zig @@ -154,8 +154,23 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co // 3/27` (Query.zig): its owner is right above it. if (pane.query) |q| if (pane.file) |f| if (pane.prompt != .name) return panes.Query.tagPrefix(q, arena, f.path); // Unsaved text shows on the grip, as acme's modbutton does, not as a - // mark in the text: the prefix is the name alone. - if (pane.file) |f| return if (pane.prompt == .name) pane.input.own orelse f.path else f.path; + // mark in the text: the prefix is the name alone, and Keep after it + // while a jump would step it (Trail.zig). + if (pane.file) |f| { + const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; + const keep = pardes.Trail.reading(p, pane); + if (pane.trail.n == 0 and !keep) return name; + // Its walk's back steps before the name, its forward steps after + // it (Trail.zig), in the room `bytes` measures them. + const keep_word = if (keep) " Keep" else ""; + const room = try arena.alloc(u8, pane.trail.bytes(.back) + name.len + pane.trail.bytes(.forward) + keep_word.len); + var w: std.Io.Writer = .fixed(room); + pane.trail.write(.back, &w) catch return error.OutOfMemory; + w.writeAll(name) catch return error.OutOfMemory; + pane.trail.write(.forward, &w) catch return error.OutOfMemory; + w.writeAll(keep_word) catch return error.OutOfMemory; + return w.buffered(); + } // A terminal showing kitty graphics says how it draws them, as an image // pane does, and so does one drawing them as glyph art by choice. if (pane.terminal != null) { @@ -167,6 +182,37 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co return (try shownDir(arena, pane)); } +/// Where a file's name starts in its tag: after its walk's back steps. +pub fn nameAt(pane: *const Pane) usize { + return if (pane.file != null) pane.trail.bytes(.back) else 0; +} + +/// The bytes of a file's tag its walk's forward steps take, after its name. +pub fn forwardSpan(pane: *const Pane) [2]usize { + const f = pane.file orelse return .{ 0, 0 }; + const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; + const lo = pane.trail.bytes(.back) + name.len; + return .{ lo, lo + pane.trail.bytes(.forward) }; +} + +/// A click at shown row `row`, display column `col` of pane `id`'s tag on +/// a word of its walk: that step's Look, or with `run` its Exec +/// (Trail.zig, pick-ui SPEC §6.3). True when it was on one. +pub fn trailClick(p: *Pardes, id: usize, row: usize, col: usize, run: bool) bool { + const pane = p.panes[id] orelse return false; + if (pane.trail.n == 0 or pane.file == null) return false; + const at = tagOffsetAt(p, pane, row, col) orelse return false; + if (at.row != 0) return false; + const lead = nameAt(pane); + const ahead = forwardSpan(pane); + const step = if (at.col < lead) + pane.trail.stepAt(.back, at.col) + else if (at.col >= ahead[0] and at.col < ahead[1]) pane.trail.stepAt(.forward, at.col - ahead[0]) else null; + const i = step orelse return false; + if (run) pardes.Trail.execStep(p, id, i) else pardes.Trail.lookStep(p, id, i); + return true; +} + pub fn tagPrefix(p: *Pardes, pane: *Pane) ![]u8 { return prefixIn(p, p.scratch.allocator(), pane); } @@ -352,14 +398,20 @@ pub fn tagOffsetAt(p: *Pardes, pane: *Pane, row: usize, col: usize) ?modal.Curso return modal.positionAt(whole, @min(shown.toReal(off), whole.len)); } -/// Whether display column `col` of the tag's shown row `row` is on a name -/// a click drafts: a saving file's path. -pub fn onName(tab: u16, pane: *Pane, row: usize, col: usize) bool { - if (row + pane.tag_top != 0) return false; - const f = pane.file orelse return false; - if (!panes.Output.fileTraits(f.output).saves) return false; +/// The display column in the name a click drafts at display column `col` +/// of the tag's shown row `row`: on a saving file's path, after its walk's +/// words; null off it. +pub fn onName(tab: u16, pane: *Pane, row: usize, col: usize) ?usize { + if (row + pane.tag_top != 0) return null; + const f = pane.file orelse return null; + if (!panes.Output.fileTraits(f.output).saves) return null; const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; - return col < panes.File.displayWidth(tab, name); + var room: [pardes.Trail.max_steps * (pardes.Trail.max_text + 32)]u8 = undefined; + var w: std.Io.Writer = .fixed(&room); + pane.trail.write(.back, &w) catch return null; + const lead = panes.File.displayWidth(tab, w.buffered()); + if (col < lead or col >= lead + panes.File.displayWidth(tab, name)) return null; + return col - lead; } /// A key typed into a saving file's path, with the tag's cursor there, @@ -369,7 +421,10 @@ pub fn onName(tab: u16, pane: *Pane, row: usize, col: usize) bool { pub fn typedIntoName(p: *Pardes, pane: *Pane, key: pardes.Key) bool { const f = pane.file orelse return false; if (!panes.Output.fileTraits(f.output).saves or pane.tag.cur_row != 0) return false; - const at: usize = @intCast(@max(0, pane.tag.cur_col)); + // The name starts after its walk's words. + const col: usize = @intCast(@max(0, pane.tag.cur_col)); + if (col < nameAt(pane)) return false; + const at = col - nameAt(pane); const back = key.cp == pardes.Key.backspace or pardes.hit(key, config.insert_backspace_alias); const forward = key.cp == pardes.Key.delete or pardes.hit(key, config.insert_delete_alias); const typed = key.text.len > 0 and !key.ctrl and !key.alt; diff --git a/core/themes/acme.zig b/core/themes/acme.zig index 4e4e9a43..34d99805 100644 --- a/core/themes/acme.zig +++ b/core/themes/acme.zig @@ -49,6 +49,8 @@ pub const theme = .{ .box_dirty = .{ 0x00, 0x00, 0x99 }, .rail_px = 12, .tag_sel_bg = .{ 0x9e, 0xee, 0xee }, + // A reading pane's tag: the page's yellow, a shade down (pick-ui's). + .tag_reading_bg = .{ 0xf3, 0xf0, 0xcc }, .sweep_bg = .{ .{ 0xee, 0xee, 0x9e }, .{ 0xaa, 0x00, 0x00 }, .{ 0x00, 0x66, 0x00 } }, .sweep_fg = .{ .{ 0x00, 0x00, 0x00 }, .{ 0xff, 0xff, 0xff }, .{ 0xff, 0xff, 0xff } }, .column_box = .{ 0x88, 0x88, 0xcc }, |
