summaryrefslogtreecommitdiff
path: root/src/Jump.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Jump.zig')
-rw-r--r--src/Jump.zig129
1 files changed, 71 insertions, 58 deletions
diff --git a/src/Jump.zig b/src/Jump.zig
index 237be82a..131d7e9d 100644
--- a/src/Jump.zig
+++ b/src/Jump.zig
@@ -15,6 +15,19 @@ const Pane = panes.Pane;
const MAX_PANES = pardes.MAX_PANES;
const MAX_JUMPS = pardes.MAX_JUMPS;
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;
@@ -68,7 +81,7 @@ const JumpsTest = struct {
for (order, 0..) |letter, i| {
const which = std.mem.indexOfScalar(u8, letters, letter).?;
const pane = p.panes[t.ids[which]].?;
- p.jumps[i] = .{
+ p.jumps.list[i] = .{
.pane = @intCast(t.ids[which]),
.serial = pane.serial,
.line = 10 + 40 * seen[which],
@@ -77,9 +90,9 @@ const JumpsTest = struct {
};
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.jumps.n = order.len;
+ p.jumps.cur = order.len - 1;
+ look.focusPaneLine(p, p.jumps.list[p.jumps.cur].pane, .{ .line = p.jumps.list[p.jumps.cur].line, .col = 1 }, .center);
p.sync();
return t;
}
@@ -101,9 +114,9 @@ const JumpsTest = struct {
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;
+ const before = t.p.jumps.cur;
t.step(way, delta);
- if (t.p.jcur == before) break;
+ if (t.p.jumps.cur == 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 });
@@ -139,8 +152,8 @@ fn samePlace(p: *const Pardes, a: Loc, b: Loc) bool {
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
+ for (p.jumps.list[0..p.jumps.n], 0..) |j, i| {
+ if (i == p.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
@@ -161,22 +174,22 @@ pub fn trackJump(p: *Pardes) void {
}
// 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;
+ if (w > 0 and std.meta.eql(p.jumps.list[w - 1], kept)) {
+ if (i == p.jumps.cur) cur = w - 1;
continue;
}
- p.jumps[w] = kept;
+ p.jumps.list[w] = kept;
w += 1;
}
- p.njumps = w;
- p.jcur = @min(cur, w -| 1);
+ p.jumps.n = w;
+ p.jumps.cur = @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 force = p.jumps.force;
+ p.jumps.force = false;
const pdf_place = panes.Pdf.place(pane);
const now: Loc = .{
.pane = @intCast(p.active),
@@ -186,26 +199,26 @@ pub fn trackJump(p: *Pardes) void {
.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
+ if (!force and p.jumps.n > 0 and samePlace(p, p.jumps.list[p.jumps.cur], now)) {
+ p.jumps.list[p.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 (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;
+ if (p.jumps.n > 0) p.jumps.n = p.jumps.cur + 1;
+ if (!force and p.jumps.n >= 2 and samePlace(p, p.jumps.list[p.jumps.n - 2], now)) {
+ p.jumps.list[p.jumps.n - 2] = p.jumps.list[p.jumps.n - 1];
+ p.jumps.list[p.jumps.n - 1] = now;
+ p.jumps.cur = p.jumps.n - 1;
return;
}
- if (p.njumps == MAX_JUMPS) {
- std.mem.copyForwards(Loc, p.jumps[0 .. MAX_JUMPS - 1], p.jumps[1..]);
- p.njumps -= 1;
+ if (p.jumps.n == MAX_JUMPS) {
+ std.mem.copyForwards(Loc, p.jumps.list[0 .. MAX_JUMPS - 1], p.jumps.list[1..]);
+ p.jumps.n -= 1;
}
- p.jumps[p.njumps] = now;
- p.jcur = p.njumps;
- p.njumps += 1;
+ p.jumps.list[p.jumps.n] = now;
+ p.jumps.cur = p.jumps.n;
+ p.jumps.n += 1;
}
/// One step through the jumplist, older for a negative `delta`, newer
@@ -222,19 +235,19 @@ pub fn trackJump(p: *Pardes) void {
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))
+ if (p.jumps.n == 0 or (older and p.jumps.cur == 0) or (!older and p.jumps.cur + 1 >= p.jumps.n))
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;
+ var next = if (older) p.jumps.cur - 1 else p.jumps.cur + 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) {
+ var i = p.jumps.cur;
+ while (if (older) i > 0 else i + 1 < p.jumps.n) {
i = if (older) i - 1 else i + 1;
- const k = p.jumps[i];
+ const k = p.jumps.list[i];
if (k.pane == p.active and k.serial == here.serial) {
pick = i;
break;
@@ -243,7 +256,7 @@ pub fn jumpBy(p: *Pardes, delta: i32) void {
}
next = pick orelse next;
};
- const j = p.jumps[next];
+ const j = p.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.
@@ -257,17 +270,17 @@ pub fn jumpBy(p: *Pardes, delta: i32) void {
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| {
+ var cur = p.jumps.cur;
+ for (p.jumps.list[0..p.jumps.n], 0..) |k, i| {
if (k.serial == 0 and k.path_hash == hash) {
- if (i < p.jcur) cur -= 1;
+ if (i < p.jumps.cur) cur -= 1;
continue;
}
- p.jumps[w] = k;
+ p.jumps.list[w] = k;
w += 1;
}
- p.njumps = w;
- p.jcur = @min(cur, w -| 1);
+ p.jumps.n = w;
+ p.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);
@@ -281,18 +294,18 @@ pub fn jumpBy(p: *Pardes, delta: i32) void {
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) {
+ for (p.jumps.list[0..p.jumps.n]) |*k| if (k.serial == 0 and k.path_hash == j.path_hash) {
k.pane = @intCast(p.active);
k.serial = opened.serial;
};
- p.jcur = next;
+ p.jumps.cur = 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;
+ p.jumps.cur = 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 });
@@ -307,36 +320,36 @@ test "the jump list never holds two entries in a row the same, when the one betw
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;
+ p.jumps.list[0] = at;
+ p.jumps.list[1] = .{ .pane = 5, .serial = 999, .line = 1, .col = 1 };
+ p.jumps.list[2] = at;
+ p.jumps.list[3] = .{ .pane = 0, .serial = pane.serial, .line = 150, .col = 1 };
+ p.jumps.n = 4;
+ p.jumps.cur = 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);
+ for (1..p.jumps.n) |i| try std.testing.expect(!std.meta.eql(p.jumps.list[i - 1], p.jumps.list[i]));
+ try std.testing.expectEqual(@as(usize, 3), p.jumps.n);
+ try std.testing.expectEqual(@as(u32, 1), p.jumps.list[0].line);
+ try std.testing.expectEqual(@as(u32, 150), p.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.njumps = 2;
- p.jcur = 1;
- p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 };
+ p.jumps.n = 2;
+ p.jumps.cur = 1;
+ p.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.jumps[0] = missing;
+ p.jumps.list[0] = missing;
jumpBy(p, -1);
- try std.testing.expectEqual(@as(usize, 1), p.jcur);
+ try std.testing.expectEqual(@as(usize, 1), p.jumps.cur);
try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0));
}
}