diff options
Diffstat (limited to 'src/Jump.zig')
| -rw-r--r-- | src/Jump.zig | 369 |
1 files changed, 369 insertions, 0 deletions
diff --git a/src/Jump.zig b/src/Jump.zig new file mode 100644 index 00000000..237be82a --- /dev/null +++ b/src/Jump.zig @@ -0,0 +1,369 @@ +//! The jump list: places jumped from, Back and Forward (Ctrl-o, Ctrl-i), +//! a closed file's place reopened from Recent. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const panes = @import("panes.zig"); +const look = @import("look.zig"); +const exec = @import("exec.zig"); +const mouse = @import("mouse.zig"); +const edit = @import("edit.zig"); +const builtins = @import("builtins.zig"); +const layout = @import("layout.zig"); +const limits = @import("memory.zig").limits; +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const MAX_PANES = pardes.MAX_PANES; +const MAX_JUMPS = pardes.MAX_JUMPS; +const BOX_H = pardes.BOX_H; +const Mouse = pardes.Mouse; + +const recent_path_cap = limits.host_path_cap; + +pub const Loc = struct { + pane: u16, + /// 0 once its pane has closed: a file's entry stays, reopened by a + /// jump to it from `path_hash`'s recent entry. + serial: u32, + line: u32, + col: u32, + /// The file it is in (Wyhash of the path), 0 for a pane with none. + path_hash: u64 = 0, + /// A PDF's place: `line` its page (one-based), `col` how far down it + /// (pdf_view.Place.at, a fraction of the page in 2^-32). + pdf: bool = false, +}; + +/// A session for the side-button jump test: four panes of 100 lines in one +/// column, A (pane 0) and B each on a file of its own, C on A's file, and O +/// an output buffer, no file behind it. `order` is the jumplist, a letter a +/// place, each letter's n-th place its line 10 + 40n: "ABABA" is A10 B10 A50 +/// B50 A90. The keyboard is on the last place, as after jumping there. +const JumpsTest = struct { + p: *Pardes, + ids: [4]usize = .{ 0, 0, 0, 0 }, + + const letters = "ABCO"; + const Way = enum { call, mouse }; + + fn init(order: []const u8) !JumpsTest { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 80 }); + errdefer p.deinit(); + const text = std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*))); + _ = try p.setTestFile(text); + var t: JumpsTest = .{ .p = p }; + for ([_][]const u8{ "/b.txt", "/test.txt", "" }, 1..) |path, n| { + const id = p.freeSlot().?; + const pane = try p.newDocPane(id); + pane.file = .{ + .path = try p.gpa.dupe(u8, path), + .content = try p.gpa.dupe(u8, text), + .history = try panes.File.History.create(p.gpa), + }; + if (path.len == 0) pane.file.?.output = .{ .from = .search }; + layout.insert(p, 0, n, id); + t.ids[n] = id; + } + layout.compute(p); + var seen: [4]u32 = @splat(0); + for (order, 0..) |letter, i| { + const which = std.mem.indexOfScalar(u8, letters, letter).?; + const pane = p.panes[t.ids[which]].?; + p.jumps[i] = .{ + .pane = @intCast(t.ids[which]), + .serial = pane.serial, + .line = 10 + 40 * seen[which], + .col = 1, + .path_hash = if (letter == 'O') 0 else std.hash.Wyhash.hash(0, pane.file.?.path), + }; + seen[which] += 1; + } + p.njumps = order.len; + p.jcur = order.len - 1; + look.focusPaneLine(p, p.jumps[p.jcur].pane, .{ .line = p.jumps[p.jcur].line, .col = 1 }, .center); + p.sync(); + return t; + } + + /// One step `delta` through `way`: the function, or the mouse's side + /// button pressed and let go. + fn step(t: JumpsTest, way: Way, delta: i32) void { + const back = delta < 0; + switch (way) { + .call => jumpBy(t.p, delta), + .mouse => for ([_]Mouse.Kind{ .press, .release }) |kind| + t.p.update(.{ .mouse = .{ .button = if (back) .back else .forward, .kind = kind, .col = 0, .row = 0 } }), + } + t.p.sync(); + } + + /// Steps `delta` until the list ends that way, saying so; the places + /// landed on, as "A50 A10 B10". + fn walk(t: JumpsTest, way: Way, delta: i32, buf: []u8) ![]const u8 { + var out: std.Io.Writer = .fixed(buf); + for (0..2 * MAX_JUMPS) |_| { + const before = t.p.jcur; + t.step(way, delta); + if (t.p.jcur == before) break; + const pane = t.p.panes[t.p.active].?; + const which = std.mem.indexOfScalar(usize, &t.ids, t.p.active).?; + try out.print("{s}{c}{d}", .{ if (out.end > 0) " " else "", letters[which], pane.body.cur_row + 1 }); + } else return error.NoEnd; + const pane = t.p.panes[t.p.active].?; + const said = pane.msg[0..pane.msg_len]; + try std.testing.expect(std.mem.indexOf(u8, said, if (delta < 0) "nothing further back" else "nothing further on") != null); + return out.buffered(); + } +}; + +/// The file a jump in `pane` is in: a text's, a PDF's. +fn jumpPath(pane: *const Pane) ?[]const u8 { + if (Pardes.recentDot(pane)) |d| return d.path; + return pane.pdfPath(); +} + +fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool { + if (a.pane != b.pane or a.serial != b.serial) return false; + // A PDF's places by where they stand on its strip, as a text's by + // lines: within a body of each other, one visit. + if (a.pdf and b.pdf) if (p.panes[b.pane]) |pane| { + const view = panes.Pdf.paneViewport(p, pane) orelse return a.line == b.line; + const ya = panes.Pdf.placeY(pane, .{ .page = a.line - 1, .at = a.col }) orelse return a.line == b.line; + const yb = panes.Pdf.placeY(pane, .{ .page = b.line - 1, .at = b.col }) orelse return a.line == b.line; + return @abs(ya - yb) <= @as(f64, @floatFromInt(view.pixel_h)); + }; + if (a.line == 0 or b.line == 0) return true; + const body = @max(1, p.rects[b.pane].h -| BOX_H); + return @max(a.line, b.line) - @min(a.line, b.line) <= body; +} + +pub fn trackJump(p: *Pardes) void { + var w: usize = 0; + var cur: usize = 0; + for (p.jumps[0..p.njumps], 0..) |j, i| { + if (i == p.jcur) cur = w; // survives -> lands at w; dies -> the next one does + var kept = j; + const live = if (j.serial == 0) false else if (p.panes[j.pane]) |pane| pane.serial == j.serial else false; + // A file's pane closed: its entry stays, closed, and a jump to it + // opens it again from Recent, or drops it when Recent has + // forgotten it (`jumpBy`: Recent is the authority's, read when + // it is wanted, not at every step). + if (!live) { + if (j.path_hash == 0) continue; + kept.serial = 0; + // Open again in some pane (a look, Recent): the entry is that + // pane's, and closed only while no pane has the file. + for (p.panes, 0..) |slot, id| if (slot) |pane| if (jumpPath(pane)) |path| { + if (std.hash.Wyhash.hash(0, path) != j.path_hash) continue; + kept.pane = @intCast(id); + kept.serial = pane.serial; + break; + }; + } + // Two in a row the same (a closed one reopened beside its own, + // or the entry between them gone): one entry, one step. + if (w > 0 and std.meta.eql(p.jumps[w - 1], kept)) { + if (i == p.jcur) cur = w - 1; + continue; + } + p.jumps[w] = kept; + w += 1; + } + p.njumps = w; + p.jcur = @min(cur, w -| 1); + + const pane = p.panes[p.active] orelse return; + // A query being typed moves the owner's selection to show, never a + // jump; its pane is no place to come back to either. + if (pane.query) |q| if (!q.frozen) return; + const force = p.jump_force; + p.jump_force = false; + const pdf_place = panes.Pdf.place(pane); + const now: Loc = .{ + .pane = @intCast(p.active), + .serial = pane.serial, + .line = if (pdf_place) |at| @intCast(at.page + 1) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0, + .col = if (pdf_place) |at| at.at else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_col) + 1) else 0, + .path_hash = if (jumpPath(pane)) |path| std.hash.Wyhash.hash(0, path) else 0, + .pdf = pdf_place != null, + }; + if (!force and p.njumps > 0 and samePlace(p, p.jumps[p.jcur], now)) { + p.jumps[p.jcur] = now; // same visit; the entry IS where you are + return; + } + // a new jump made from the middle of the list drops everything ahead of + // it, the way vim's does: the future you did not take is not history. + if (p.njumps > 0) p.njumps = p.jcur + 1; + if (!force and p.njumps >= 2 and samePlace(p, p.jumps[p.njumps - 2], now)) { + p.jumps[p.njumps - 2] = p.jumps[p.njumps - 1]; + p.jumps[p.njumps - 1] = now; + p.jcur = p.njumps - 1; + return; + } + if (p.njumps == MAX_JUMPS) { + std.mem.copyForwards(Loc, p.jumps[0 .. MAX_JUMPS - 1], p.jumps[1..]); + p.njumps -= 1; + } + p.jumps[p.njumps] = now; + p.jcur = p.njumps; + p.njumps += 1; +} + +/// One step through the jumplist, older for a negative `delta`, newer +/// otherwise: Back and Forward from every key, mouse button, builtin and +/// 9P exec. `jcur` is the entry where you are; the step moves it to the +/// entry it lands on, and the entries it passed over stay where they are. +/// +/// JumpScope file, from a pane with a file: the nearest entry that way in +/// this pane, else in this file in any pane (a closed one's too), else, +/// with none of this file left that way, the nearest of any file, as +/// JumpScope all always takes. Forward mirrors Back, so it retraces a run +/// of Backs inside one file step for step, and Forwards end, as they +/// always have, at the newest entry. +pub fn jumpBy(p: *Pardes, delta: i32) void { + const older = delta < 0; + // At an end of the list: said, not a step that silently does nothing. + if (p.njumps == 0 or (older and p.jcur == 0) or (!older and p.jcur + 1 >= p.njumps)) + return p.setMessage(p.active, if (older) "Back: nothing further back" else "Forward: nothing further on"); + var next = if (older) p.jcur - 1 else p.jcur + 1; + // A terminal or a scratch has no file to keep to: the step is global. + if (p.settings.jump_scope == .file) if (p.panes[p.active]) |here| if (Pardes.recentPlace(here)) |place| if (place.path.len > 0) { + const hash = if (jumpPath(here)) |path| std.hash.Wyhash.hash(0, path) else 0; + // The nearest of this pane's own wins over a nearer one of this + // file in another pane: the step does not move the keyboard. + var pick: ?usize = null; + var i = p.jcur; + while (if (older) i > 0 else i + 1 < p.njumps) { + i = if (older) i - 1 else i + 1; + const k = p.jumps[i]; + if (k.pane == p.active and k.serial == here.serial) { + pick = i; + break; + } + if (pick == null and hash != 0 and k.path_hash == hash) pick = i; + } + next = pick orelse next; + }; + const j = p.jumps[next]; + if (j.pane >= MAX_PANES) return; + // A closed file's entry: opened again at its dot, placed as a look + // places it, and the entry is that pane from here on. + if (j.serial == 0) { + const found = p.recentByHash(j.path_hash); + // Deleted since, or Recent has forgotten it: its entries go (the + // first said), and the step goes on to the next place, never + // stuck on one that cannot open. + if (found == null or !Pardes.recentKeeps(found.?.path)) { + var said: [recent_path_cap + 32]u8 = undefined; + if (found) |e| p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); + const hash = j.path_hash; + var w: usize = 0; + var cur = p.jcur; + for (p.jumps[0..p.njumps], 0..) |k, i| { + if (k.serial == 0 and k.path_hash == hash) { + if (i < p.jcur) cur -= 1; + continue; + } + p.jumps[w] = k; + w += 1; + } + p.njumps = w; + p.jcur = @min(cur, w -| 1); + // The same step again, over the list without them: each + // round takes one file's entries away, so it ends. + return jumpBy(p, delta); + } + const e = found.?; + const from = p.active; + look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.pdf) 0 else @max(1, j.col) }); + const opened = p.panes[p.active] orelse return; + const opened_path = jumpPath(opened) orelse return; + if (!std.mem.eql(u8, opened_path, e.path)) return; // it did not open + if (j.pdf) panes.Pdf.landAt(p, opened, .{ .page = j.line -| 1, .at = j.col }); + // Every entry of that file is the reopened pane from here on, so + // a step onto another of them moves in it rather than reopening. + for (p.jumps[0..p.njumps]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { + k.pane = @intCast(p.active); + k.serial = opened.serial; + }; + p.jcur = next; + return; + } + const pane = p.panes[j.pane] orelse return; + if (pane.serial != j.serial) return; + if (p.panes[p.active]) |source| look.clearNavigationSelection(p, source); + if (p.active != j.pane) look.clearNavigationSelection(p, pane); + p.jcur = next; + if (pane.pdfPath() != null) { + look.focusPaneLine(p, j.pane, .{ .line = 0, .col = 0 }, .keep); + panes.Pdf.landAt(p, pane, .{ .page = j.line -| 1, .at = if (j.pdf) j.col else 0 }); + return; + } + look.focusPaneLine(p, j.pane, .{ .line = j.line, .col = j.col }, .center); +} + +test "the jump list never holds two entries in a row the same, when the one between them goes" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile(std.mem.asBytes(&@as([200][2]u8, @splat("a\n".*)))); + const at: Loc = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; + // A, a pane long closed with no file to keep it, A again, then far on. + p.jumps[0] = at; + p.jumps[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 }; + p.jumps[2] = at; + p.jumps[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 }; + p.njumps = 4; + p.jcur = 3; + pane.body.cur_row = 80; // where you are now: far from both + trackJump(p); + for (1..p.njumps) |i| try std.testing.expect(!std.meta.eql(p.jumps[i - 1], p.jumps[i])); + try std.testing.expectEqual(@as(usize, 3), p.njumps); + try std.testing.expectEqual(@as(u32, 1), p.jumps[0].line); + try std.testing.expectEqual(@as(u32, 150), p.jumps[1].line); +} + +test "jump history skips an entry whose pane slot or serial is gone, leaving the selection" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha beta\ngamma delta\nepsilon zeta\n"); + p.njumps = 2; + p.jcur = 1; + p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; + pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); + const selected = pane.body.primaryRange(pane.file.?.content, 0); + for ([_]Loc{ + .{ .pane = 15, .serial = pane.serial, .line = 1, .col = 1 }, + .{ .pane = 0, .serial = pane.serial + 1, .line = 1, .col = 1 }, + }) |missing| { + p.jumps[0] = missing; + jumpBy(p, -1); + try std.testing.expectEqual(@as(usize, 1), p.jcur); + try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0)); + } +} + +// The keys and builtins are test/behaviour/modes.zig's; a mouse's side +// buttons have no /mousein bits. +test "Back and Forward take the same steps from the mouse's side buttons and jumpBy" { + for ([_]JumpsTest.Way{ .call, .mouse }) |way| { + const t = try JumpsTest.init("BABABA"); + defer t.p.deinit(); + var buf: [128]u8 = undefined; + try std.testing.expectEqualStrings("A50 A10 B10", try t.walk(way, -1, &buf)); + try std.testing.expectEqualStrings("B50 B90 A90", try t.walk(way, 1, &buf)); + } +} + +test "jump history terminal rectangle follows output and clears on reflow" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12, .tty_only = true }); + defer p.deinit(); + const pane = p.panes[0].?; + p.update(.{ .output = .{ .pane = 0, .bytes = "alpha bravo\r\nsecond line\r\n" } }); + pane.sel[0] = .{ .state = .done, .c0 = 0, .c1 = 4, .r0 = BOX_H, .r1 = BOX_H }; + try edit.capturePointerSelection(p, pane, 0); + try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); + for (0..40) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "more output\r\n" } }); + try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); + panes.terminal.resizeGrid(pane, pane.cols - 1, pane.rows); + try std.testing.expect(pane.pointerSelection(pane.sel[0]) == null); + try std.testing.expectEqual(.none, pane.sel[0].state); +} |
