//! 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 = 64; const BOX_H = pardes.BOX_H; const Jump = @This(); /// The places, oldest first, `n` of them, and the one Back and Forward /// are at. list: [MAX_JUMPS]Loc = undefined, n: usize = 0, cur: usize = 0, /// The next place tracked is an entry of its own however near the last /// (a query answered: Ctrl-o goes back to where it was asked). force: bool = false, pub const replica = .{ .len = .{.{ "list", "n" }} }; 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 kind's pane's place, its `where` (a PDF's `12:0.330000`), with /// `line` its first number. place: Kind.Buf(32) = .{}, }; const Kind = @import("Kind.zig"); /// A kind's pane's place now, its `where`. fn kindPlace(pane: *Pane) ?Kind.Buf(32) { const slot = pane.addon orelse return null; if (!slot.live) return null; // As its kind's last call left it: the same on every core. return if (slot.where_text.len == 0) null else slot.where_text; } /// A place's first number: a kind's line (a PDF's page). /// A kind's place `L:F S` as where it is (L + F, in lines) and its /// window's height (S lines); null for one without both. fn spanOf(text: []const u8) ?[2]f64 { const sp = std.mem.lastIndexOfScalar(u8, text, ' ') orelse return null; const colon = std.mem.indexOfScalar(u8, text[0..sp], ':') orelse return null; const line = std.fmt.parseFloat(f64, text[0..colon]) catch return null; const frac = std.fmt.parseFloat(f64, text[colon + 1 .. sp]) catch return null; return .{ line + frac, std.fmt.parseFloat(f64, text[sp + 1 ..]) catch return null }; } pub fn firstNumber(text: []const u8) u32 { var n: u32 = 0; for (text) |ch| { if (ch < '0' or ch > '9') break; n = n *| 10 +| (ch - '0'); } return n; } /// 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.view().jumps.list[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.view().jumps.n = order.len; p.view().jumps.cur = order.len - 1; look.focusPaneLine(p, p.view().jumps.list[p.view().jumps.cur].pane, .{ .line = p.view().jumps.list[p.view().jumps.cur].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.view().jumps.cur; t.step(way, delta); if (t.p.view().jumps.cur == before) break; const pane = t.p.panes[t.p.view().active].?; const which = std.mem.indexOfScalar(usize, &t.ids, t.p.view().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.view().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 kind's (a PDF's). fn jumpPath(pane: *const Pane) ?[]const u8 { if (Pardes.recentDot(pane)) |d| return d.path; if (pane.addon) |k| if (k.path.len > 0) return k.path; return null; } fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool { if (a.pane != b.pane or a.serial != b.serial) return false; // A kind's places within a window of each other (`where`'s `L:F S`: // the window S lines), else by their first numbers (a page or the // next), one visit. if (a.place.len > 0 and b.place.len > 0) { if (spanOf(a.place.slice())) |x| if (spanOf(b.place.slice())) |y| return @abs(x[0] - y[0]) <= @max(x[1], y[1]); return @max(a.line, b.line) - @min(a.line, b.line) <= 1; } if (a.line == 0 or b.line == 0) return true; const body = @max(1, p.view().rects[b.pane].h -| BOX_H); return @max(a.line, b.line) - @min(a.line, b.line) <= body; } pub fn trackJump(p: *Pardes) void { const v = p.view(); var w: usize = 0; var cur: usize = 0; for (v.jumps.list[0..p.view().jumps.n], 0..) |j, i| { if (i == v.jumps.cur) 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(v.jumps.list[w - 1], kept)) { if (i == v.jumps.cur) cur = w - 1; continue; } v.jumps.list[w] = kept; w += 1; } v.jumps.n = w; v.jumps.cur = @min(cur, w -| 1); const pane = p.panes[v.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 = v.jumps.force; v.jumps.force = false; const place = kindPlace(pane); const now: Loc = .{ .pane = @intCast(v.active), .serial = pane.serial, .line = if (place) |at| firstNumber(at.slice()) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0, .col = if (place != null) 0 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, .place = place orelse .{}, }; if (!force and v.jumps.n > 0 and samePlace(p, v.jumps.list[v.jumps.cur], now)) { v.jumps.list[v.jumps.cur] = 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 (v.jumps.n > 0) v.jumps.n = v.jumps.cur + 1; if (!force and v.jumps.n >= 2 and samePlace(p, v.jumps.list[v.jumps.n - 2], now)) { v.jumps.list[v.jumps.n - 2] = v.jumps.list[v.jumps.n - 1]; v.jumps.list[v.jumps.n - 1] = now; v.jumps.cur = v.jumps.n - 1; return; } if (v.jumps.n == MAX_JUMPS) { std.mem.copyForwards(Loc, v.jumps.list[0 .. MAX_JUMPS - 1], v.jumps.list[1..]); v.jumps.n -= 1; } v.jumps.list[v.jumps.n] = now; v.jumps.cur = v.jumps.n; v.jumps.n += 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. /// /// 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; from a terminal or a /// scratch, the nearest of any. 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 v = p.view(); 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)) return p.setMessage(v.active, if (older) "Back: nothing further back" else "Forward: nothing further on"); var next = if (older) v.jumps.cur - 1 else v.jumps.cur + 1; // A terminal or a scratch has no file to keep to: the step is global. if (p.panes[v.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 = v.jumps.cur; while (if (older) i > 0 else i + 1 < v.jumps.n) { i = if (older) i - 1 else i + 1; const k = v.jumps.list[i]; if (k.pane == v.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 = v.jumps.list[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: Kind.Buf(recent_path_cap + 32) = .{}; if (found) |e| p.setMessage(v.active, said.print("{s}: deleted, skipped", .{e.path[0..@min(e.path.len, recent_path_cap)]})); const hash = j.path_hash; var w: usize = 0; var cur = v.jumps.cur; for (v.jumps.list[0..p.view().jumps.n], 0..) |k, i| { if (k.serial == 0 and k.path_hash == hash) { if (i < v.jumps.cur) cur -= 1; continue; } v.jumps.list[w] = k; w += 1; } v.jumps.n = w; v.jumps.cur = @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 = v.active; look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.place.len > 0) 0 else @max(1, j.col) }); const opened = p.panes[v.active] orelse return; const opened_path = jumpPath(opened) orelse return; if (!std.mem.eql(u8, opened_path, e.path)) return; // it did not open var said: Kind.Buf(160) = .{}; if (j.place.len > 0) _ = Kind.address(p, v.active, j.place.slice(), &said); // 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 (v.jumps.list[0..p.view().jumps.n]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) { k.pane = @intCast(v.active); k.serial = opened.serial; }; v.jumps.cur = next; return; } const pane = p.panes[j.pane] orelse return; if (pane.serial != j.serial) return; if (p.panes[v.active]) |source| look.clearNavigationSelection(p, source); if (v.active != j.pane) look.clearNavigationSelection(p, pane); v.jumps.cur = next; if (pane.addon != null) { look.focusPaneLine(p, j.pane, .{ .line = 0, .col = 0 }, .keep); var said: Kind.Buf(160) = .{}; if (j.place.len > 0) _ = Kind.address(p, j.pane, j.place.slice(), &said); 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.view().jumps.list[0] = at; p.view().jumps.list[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 }; p.view().jumps.list[2] = at; p.view().jumps.list[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 }; p.view().jumps.n = 4; p.view().jumps.cur = 3; pane.body.cur_row = 80; // where you are now: far from both trackJump(p); for (1..p.view().jumps.n) |i| try std.testing.expect(!std.meta.eql(p.view().jumps.list[i - 1], p.view().jumps.list[i])); try std.testing.expectEqual(@as(usize, 3), p.view().jumps.n); try std.testing.expectEqual(@as(u32, 1), p.view().jumps.list[0].line); try std.testing.expectEqual(@as(u32, 150), p.view().jumps.list[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.view().jumps.n = 2; p.view().jumps.cur = 1; p.view().jumps.list[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.view().jumps.list[0] = missing; jumpBy(p, -1); try std.testing.expectEqual(@as(usize, 1), p.view().jumps.cur); 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); }