1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
//! 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 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 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.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 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.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 pdf_place = panes.Pdf.place(pane);
const now: Loc = .{
.pane = @intCast(v.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 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: [recent_path_cap + 32]u8 = undefined;
if (found) |e| p.setMessage(v.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped");
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.pdf) 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
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 (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.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.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);
}
|