summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Output.zig7
-rw-r--r--src/pardes.zig43
-rw-r--r--src/pdf_view.zig50
3 files changed, 91 insertions, 9 deletions
diff --git a/src/Output.zig b/src/Output.zig
index 01e3e0f1..a17b0459 100644
--- a/src/Output.zig
+++ b/src/Output.zig
@@ -701,6 +701,11 @@ pub fn openJumps(p: *Pardes, id: usize) !void {
// A closed file's entry: marked, and a look at it opens it again.
if (j.serial == 0) {
const e = p.recentByHash(j.path_hash) orelse continue;
+ // A PDF's `col` is how far down its page, no Look's column.
+ if (j.pdf) {
+ try out.writer.print("{s}:{d} (closed)\n", .{ e.path, @max(1, j.line) });
+ continue;
+ }
try out.writer.print("{s}:{d}:{d} (closed)\n", .{ e.path, @max(1, j.line), @max(1, j.col) });
continue;
}
@@ -724,6 +729,8 @@ pub fn openJumps(p: *Pardes, id: usize) !void {
while (cut > 0 and cut < what.len and what[cut] & 0xc0 == 0x80) cut -= 1;
if (j.line == 0)
try out.writer.print("{s} {s}\n", .{ loc, what[0..cut] })
+ else if (j.pdf)
+ try out.writer.print("{s}:{d} {s}\n", .{ loc, j.line, what[0..cut] })
else
try out.writer.print("{s}:{d}:{d} {s}\n", .{ loc, j.line, j.col, what[0..cut] });
}
diff --git a/src/pardes.zig b/src/pardes.zig
index 26b167d5..a23039ef 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -462,6 +462,9 @@ pub const Loc = struct {
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,
};
test {
@@ -4924,6 +4927,12 @@ pub const Pardes = struct {
return null;
}
+ /// The file a jump in `pane` is in: a text's, a PDF's.
+ fn jumpPath(pane: *const Pane) ?[]const u8 {
+ if (recentDot(pane)) |d| return d.path;
+ return pane.pdfPath();
+ }
+
fn recentDot(pane: *const Pane) ?struct { path: []const u8, line: u32, col: u32, aline: u32 = 0, acol: u32 = 0 } {
const f = pane.file orelse return null;
if (f.output != null) return null;
@@ -7355,6 +7364,14 @@ pub const Pardes = struct {
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;
@@ -7374,8 +7391,8 @@ pub const Pardes = struct {
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 (recentDot(pane)) |d| {
- if (std.hash.Wyhash.hash(0, d.path) != j.path_hash) continue;
+ 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;
@@ -7399,12 +7416,14 @@ pub const Pardes = struct {
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 (pane.pdfPage()) |page| @intCast(page + 1) else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_row) + 1) else 0,
- .col = if (pane.pdfPage() != null) 0 else if (pane.body.cur_pinned) @intCast(@max(0, pane.body.cur_col) + 1) else 0,
- .path_hash = if (recentDot(pane)) |d| std.hash.Wyhash.hash(0, d.path) else 0,
+ .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 p.samePlace(p.jumps[p.jcur], now)) {
p.jumps[p.jcur] = now; // same visit; the entry IS where you are
@@ -7447,7 +7466,7 @@ pub const Pardes = struct {
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 (recentPlace(here)) |place| if (place.path.len > 0) {
- const hash = if (recentDot(here)) |d| std.hash.Wyhash.hash(0, d.path) else 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;
@@ -7492,10 +7511,11 @@ pub const Pardes = struct {
return p.jumpBy(delta);
}
const from = p.active;
- look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = @max(1, j.col) });
+ 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 f = opened.file orelse return;
- if (!std.mem.eql(u8, f.path, e.path)) return; // it did not open
+ 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) {
@@ -7510,6 +7530,11 @@ pub const Pardes = struct {
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);
}
diff --git a/src/pdf_view.zig b/src/pdf_view.zig
index bcca11b4..f6d26eac 100644
--- a/src/pdf_view.zig
+++ b/src/pdf_view.zig
@@ -3070,6 +3070,56 @@ pub fn revealOutlineDestination(
_ = ensurePaneLayout(core, pane);
}
+/// A PDF's place, as the jumplist keeps it: the page at the strip's top and
+/// how far down that page it stands, a fraction in 2^-32 of the page. A
+/// fraction, not pixels: a zoom, a fit change or a resize scales every page,
+/// and a fraction of one lands on the same line of print after it.
+pub const Place = struct { page: usize, at: u32 };
+const place_scale: f64 = std.math.maxInt(u32);
+
+pub fn place(pane: *const pardes.Pane) ?Place {
+ if (comptime !enabled) return null;
+ const state = if (pane.pdf) |*pv| pv else return null;
+ if (!state.layout_valid or state.document_height == 0) return .{ .page = state.page, .at = 0 };
+ const page = pageAtOffset(state, state.document_scroll_y);
+ const start: f64 = @floatFromInt(state.page_starts[page]);
+ const height: f64 = @floatFromInt(@max(1, state.page_heights[page]));
+ const fraction = std.math.clamp((state.document_scroll_y - start) / height, 0, 1);
+ return .{ .page = page, .at = @intFromFloat(@round(fraction * place_scale)) };
+}
+
+/// Where `at` stands on the strip as laid out now, in document pixels.
+pub fn placeY(pane: *const pardes.Pane, at: Place) ?f64 {
+ if (comptime !enabled) return null;
+ const state = if (pane.pdf) |*pv| pv else return null;
+ if (!state.layout_valid) return null;
+ const page = @min(at.page, state.page_count -| 1);
+ return @as(f64, @floatFromInt(state.page_starts[page])) +
+ @as(f64, @floatFromInt(at.at)) / place_scale * @as(f64, @floatFromInt(state.page_heights[page]));
+}
+
+/// A jump to `at` (Back, Forward): the core is there at once and the strip
+/// follows through the motion system, a far one as one body's slide, the
+/// page rendered where it comes to rest. Not laid out yet (a pane just
+/// reopened), it is anchored there for the first layout.
+pub fn landAt(core: *pardes.Pardes, pane: *pardes.Pane, at: Place) void {
+ if (comptime !enabled) return;
+ const state = &(pane.pdf orelse return);
+ const page = @min(at.page, state.page_count -| 1);
+ activatePage(core, pane, page, false);
+ state.scroll_to_page_pending = false;
+ if (placeY(pane, .{ .page = page, .at = at.at })) |y| {
+ state.layout_anchor_pending = false;
+ // Where it stands already (half a pixel is the place's rounding):
+ // not moved, not kicked.
+ if (@abs(y - state.document_scroll_y) >= 0.5) state.document_scroll_y = y;
+ } else {
+ state.layout_anchor_pending = true;
+ state.layout_anchor_page = page;
+ state.layout_anchor_fraction = @as(f64, @floatFromInt(at.at)) / place_scale;
+ }
+}
+
pub fn setPage(core: *pardes.Pardes, pane: *pardes.Pane, page: usize) void {
activatePage(core, pane, page, true);
}