From 7d359addc420eca66bd1583d8fd8d6a104986a03 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:31:38 -0300 Subject: panes: a pane's PDF state is its own allocation, `pdf: ?*Pdf.State` (made by openPane with pdf_gpa, freed by deinitPane), as a terminal's is, where every Pane held the State inline, a text file's and a terminal's too: a Pane is 49232 bytes where it was 88936 (the State 39704), and a replica's rollback copy of a pane without a PDF is that much smaller; the dump and the wire change with it, the walk writing the slot as a pointer to a State where it wrote the State in place, so a dump written before this, or a peer of an older build, gives its PDF panes no PDF (the walk notes `Pane.pdf: its kind changed; its default taken`, and dump.ensureKind makes each a terminal); the 97 uses change form only (a `|*x|` capture or a `&` dropped, normal.zig's one among them), applyNormal untouched Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/body_layer.zig | 2 +- src/builtins.zig | 4 ++-- src/colors.zig | 2 +- src/draw.zig | 2 +- src/edit.zig | 6 +++--- src/exec.zig | 2 +- src/fs.zig | 2 +- src/look.zig | 22 +++++++++---------- src/macos.zig | 6 +++--- src/mouse.zig | 2 +- src/ninep/ctl.zig | 2 +- src/ninep/pane.zig | 2 +- src/normal.zig | 2 +- src/panes.zig | 17 +++++++++------ src/pardes.zig | 8 +++---- src/pdf_view.zig | 62 +++++++++++++++++++++++++++++------------------------- src/tagline.zig | 2 +- 17 files changed, 76 insertions(+), 69 deletions(-) (limited to 'src') diff --git a/src/body_layer.zig b/src/body_layer.zig index 7a4103c3..7b1be4f7 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -502,7 +502,7 @@ pub fn bodyText(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { } if (pane.hasPdf()) { if (comptime pdf_enabled) return panes.Pdf.visibleText( - &pane.pdf.?, + pane.pdf.?, p.pdf_gpa, arena, pane.rows -| BOX_H, diff --git a/src/builtins.zig b/src/builtins.zig index 95a6c692..1afba918 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -983,7 +983,7 @@ pub const PdfFit = struct { pub const enabled = pardes.pdf_enabled; pub const takes_arg = true; pub fn run(c: Ctx) void { - const state = &(c.pane.pdf orelse return); + const state = c.pane.pdf orelse return; if (c.arg != null) { const want = stateArg(c, panes.Pdf.FitMode, "PdfFit", "width or height") orelse return; if (want == state.fit) return; @@ -999,7 +999,7 @@ pub const PdfTint = struct { pub const enabled = pardes.pdf_enabled; pub const takes_arg = true; pub fn run(c: Ctx) void { - const state = &(c.pane.pdf orelse return); + const state = c.pane.pdf orelse return; if (c.arg != null) { const want = stateArg(c, panes.Pdf.TintMode, "PdfTint", "disabled, filtered or full") orelse return; while (state.tint != want) panes.Pdf.toggleTint(c.pane); diff --git a/src/colors.zig b/src/colors.zig index 141e0b0e..9c666d9b 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -824,7 +824,7 @@ pub fn finishThemeInitialization(p: *Pardes) void { fn invalidateThemeDependentRasters(p: *Pardes) void { if (comptime pdf_enabled) for (p.panes) |slot| { const pane = slot orelse continue; - const pv = &(pane.pdf orelse continue); + const pv = pane.pdf orelse continue; if (pv.tint != .disabled) pv.invalidateAllRasters(); }; } diff --git a/src/draw.zig b/src/draw.zig index 518f0800..1e17df3d 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -1071,7 +1071,7 @@ fn tagTintRange(pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { start = std.mem.lastIndexOf(u8, tag[0..@min(prefix_len, tag.len)], state.path) orelse return null; break :blk state.path; } else if (comptime pdf_enabled) blk: { - break :blk if (pane.pdf) |*state| state.path else return null; + break :blk if (pane.pdf) |state| state.path else return null; } else return null; const basename = if (std.mem.lastIndexOfScalar(u8, name, '/')) |slash| slash + 1 else 0; if (basename == name.len) return null; diff --git a/src/edit.zig b/src/edit.zig index 85ace33f..2ba6d57a 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -182,7 +182,7 @@ fn pasteInto(p: *Pardes, t: *Text, bytes: []const u8) void { } pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void { - if (comptime pdf_enabled) if (pane.pdf) |*pv| { + if (comptime pdf_enabled) if (pane.pdf) |pv| { if (pv.selection_text.len > 0) setClipboard(p, pv.selection_text); return; }; @@ -360,7 +360,7 @@ pub fn spanHas(r: i32, c: i32, ar: i32, ac: i32, br: i32, bc: i32) bool { /// the active modal selection as text (v range or x lines), if any pub fn currentSelText(p: *Pardes, pane: *Pane) ?[]const u8 { - if (comptime pdf_enabled) if (p.native_images) if (pane.pdf) |*pv| + if (comptime pdf_enabled) if (p.native_images) if (pane.pdf) |pv| if (pv.selection != null and pv.selection_text.len > 0) return pv.selection_text; if (pane.body.vsel.active) return vselText(p, &pane.body); if (!pane.body.msel.active) return null; @@ -401,7 +401,7 @@ pub fn paneCursorLines(p: *Pardes, t: *Text) ![]const []const u8 { const pane = t.pane() orelse return &.{}; if (pane.file) |*f| return panes.File.cursorLines(arena, pane, f); if (pane.hasPdf()) { - if (comptime pdf_enabled) return panes.Pdf.textLines(&pane.pdf.?, p.pdf_gpa, arena); + if (comptime pdf_enabled) return panes.Pdf.textLines(pane.pdf.?, p.pdf_gpa, arena); unreachable; } return panes.terminal.cursorLines(p, pane); diff --git a/src/exec.zig b/src/exec.zig index 27effda0..4706d11a 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -277,7 +277,7 @@ fn heldPane(p: *Pardes, first: usize, k: usize) ?*Pane { pub fn heldSelection(p: *Pardes, first: usize) ?[]const u8 { for (0..p.panes.len + 2) |k| { const t = heldPane(p, first, k) orelse continue; - if (comptime pdf_enabled) if (p.native_images) if (t.pdf) |*pv| { + if (comptime pdf_enabled) if (p.native_images) if (t.pdf) |pv| { if (pv.selection != null and pv.selection_text.len > 0) return pv.selection_text; }; diff --git a/src/fs.zig b/src/fs.zig index 3e80fc1b..207f1018 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -1481,7 +1481,7 @@ pub fn unmount(p: *Pardes, name: []const u8) !void { const pane = slot orelse continue; const paths = [_][]const u8{ if (pane.file) |file| file.path else "", - if (comptime pardes.pdf_enabled) (if (pane.pdf) |*pdf| pdf.path else "") else "", + if (comptime pardes.pdf_enabled) (if (pane.pdf) |pdf| pdf.path else "") else "", if (pane.image) |image| image.path else "", Pardes.paneDir(pane), }; diff --git a/src/look.zig b/src/look.zig index a4a64b14..f6095fb9 100644 --- a/src/look.zig +++ b/src/look.zig @@ -596,7 +596,7 @@ pub fn runSearch(p: *Pardes, id: usize, pat_raw: []const u8, start: SearchStart, if (pane.hasPdf()) { if (comptime pdf_enabled) { const found = try panes.Pdf.searchRows( - &pane.pdf.?, + pane.pdf.?, p.pdf_gpa, pat, start == .cursor, @@ -898,7 +898,7 @@ fn landLookSpot(p: *Pardes, id: usize, pane: *Pane, spot: Pane.LookSpot) void { pub fn cancelLookHover(p: *Pardes) void { if (comptime pdf_enabled) if (p.pdf_hover_preview) |*preview| { if (preview.pane < p.panes.len) if (p.panes[preview.pane]) |pane| { - if (pane.serial == preview.serial) if (pane.pdf) |*pv| + if (pane.serial == preview.serial) if (pane.pdf) |pv| pv.invalidateRaster(preview.probe.page); }; preview.deinit(p.pdf_gpa); @@ -1005,7 +1005,7 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at: Spot, landing: enum { center, ke if (pane.hasPdf()) { if (landing == .keep) return; if (comptime pdf_enabled) { - const pv = &pane.pdf.?; + const pv = pane.pdf.?; if (pv.focusLocation(p.pdf_gpa, at.line, at.col)) panes.Pdf.resetPageChrome(pane); } return; @@ -1045,7 +1045,7 @@ fn openPaneTarget(p: *const Pardes, path: []const u8, at: Spot) ?Target { if (tt.image) |iv| if (std.mem.eql(u8, iv.path, path)) { return .{ .pane = .{ .id = i, .at = .{} } }; }; - if (comptime pdf_enabled) if (tt.pdf) |*pv| if (std.mem.eql(u8, pv.path, path)) { + if (comptime pdf_enabled) if (tt.pdf) |pv| if (std.mem.eql(u8, pv.path, path)) { return .{ .pane = .{ .id = i, .at = at } }; }; const ff = if (tt.file) |*f| f else continue; @@ -1073,7 +1073,7 @@ pub fn clearNavigationSelection(p: *Pardes, pane: *Pane) void { pane.body.sticky_col = -1; for (0..pane.sel.len) |slot| pane.clearPointerSelection(slot); pane.sel = @splat(.{}); - if (comptime pdf_enabled) if (pane.pdf) |*state| { + if (comptime pdf_enabled) if (pane.pdf) |state| { state.clearDrag(); state.clearSelection(p.pdf_gpa); }; @@ -1148,7 +1148,7 @@ fn canonicalLookLocation(p: *Pardes, id: usize, text: []const u8) ?[]const u8 { if (target.id >= p.panes.len) return null; const pane = p.panes[target.id] orelse return null; if (pane.file) |file| return locationText(p, file.path, target.at); - if (comptime pdf_enabled) if (pane.pdf) |*doc| return locationText(p, doc.path, target.at); + if (comptime pdf_enabled) if (pane.pdf) |doc| return locationText(p, doc.path, target.at); if (pane.image) |iv| return locationText(p, iv.path, target.at); var path: [32]u8 = undefined; return locationText(p, std.fmt.bufPrint(&path, config.pane_addr ++ "{d}", .{pane.serial}) catch return null, target.at); @@ -1158,7 +1158,7 @@ fn canonicalLookLocation(p: *Pardes, id: usize, text: []const u8) ?[]const u8 { pub fn pdfLinkLocation(p: *Pardes, pane: *const Pane, link: if (pdf_enabled) pdf.Link else void) ?[]const u8 { if (comptime !pdf_enabled) return null; - const state = &(pane.pdf orelse return null); + const state = pane.pdf orelse return null; const id = p.paneIdOf(pane) orelse return null; return switch (link.destination) { .none => null, @@ -1248,7 +1248,7 @@ fn missedHit(p: *Pardes, id: usize, path: []const u8, page: usize, hit: usize) v /// The open PDF pane showing `path`, if one does. fn pdfPaneOf(p: *Pardes, path: []const u8) ?*panes.Pdf.State { if (comptime !pdf_enabled) return null; - for (p.panes) |slot| if (slot) |pane| if (pane.pdf) |*pv| if (std.mem.eql(u8, pv.path, path)) return pv; + for (p.panes) |slot| if (slot) |pane| if (pane.pdf) |pv| if (std.mem.eql(u8, pv.path, path)) return pv; return null; } @@ -1487,7 +1487,7 @@ fn lookAddress(p: *Pardes, id: usize, path: []const u8, expr: []const u8) void { } const pane = p.panes[target] orelse return; const f = pane.file orelse { - if (comptime pdf_enabled) if (pane.pdf) |*pv| return lookPdfPage(p, id, target, pv, expr); + if (comptime pdf_enabled) if (pane.pdf) |pv| return lookPdfPage(p, id, target, pv, expr); if ((by_serial or path.len == 0) and pane.isTerminal()) return lookTerminalAddress(p, id, target, expr); // Named as the pane is, the bare `:addr` (path empty) included. var serial_name: [32]u8 = undefined; @@ -1606,7 +1606,7 @@ pub fn reopenAt(p: *Pardes, from: usize, path: []const u8, at: Spot) void { const id = p.active; const pane = p.panes[id] orelse return; // A PDF's place is its page, the last if it has fewer now. - if (comptime pdf_enabled) if (pane.pdf) |*pv| if (std.mem.eql(u8, pv.path, path)) { + if (comptime pdf_enabled) if (pane.pdf) |pv| if (std.mem.eql(u8, pv.path, path)) { if (at.line > 0 and pv.focusLocation(p.pdf_gpa, @min(at.line, pv.page_count), 0)) panes.Pdf.resetPageChrome(pane); return; }; @@ -1805,7 +1805,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // A PDF's page past its last is a miss, as a line past a file's // end is: never the last page in its place (opening one, the // same: Pdf.openPane's .refuse). - if (comptime pdf_enabled) if (target.pdf) |*pv| { + if (comptime pdf_enabled) if (target.pdf) |pv| { if (t.at.line > pv.page_count) return missedPage(p, id, pv.path, t.at.line); if (!pv.hitExists(p.pdf_gpa, @max(1, t.at.line), t.at.col)) return missedHit(p, id, pv.path, @max(1, t.at.line), t.at.col); }; diff --git a/src/macos.zig b/src/macos.zig index 60f9e00e..af8410e0 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -880,7 +880,7 @@ fn watchInitialGeneration(st: *State, pane: u8, path: []const u8) ?file_watch.Ge const value = st.core.panes[pane] orelse return null; if (value.file) |file| if (std.mem.eql(u8, file.path, path)) return .{ .text = std.hash.Wyhash.hash(0, file.content) }; - if (comptime pardes.pdf_enabled) if (value.pdf) |*pdf| if (std.mem.eql(u8, pdf.path, path)) + if (comptime pardes.pdf_enabled) if (value.pdf) |pdf| if (std.mem.eql(u8, pdf.path, path)) return .{ .pdf = null }; return null; } @@ -998,7 +998,7 @@ fn reloadWatchedFile(st: *State, pane: u8, announce: bool) bool { }, .pdf => |old_identity| pdf: { if (comptime !pardes.pdf_enabled) break :pdf .no_change; - const state_before = if (current.pdf) |*pdf_before| pdf_before else break :pdf .no_change; + const state_before = if (current.pdf) |pdf_before| pdf_before else break :pdf .no_change; if (!std.mem.eql(u8, state_before.path, watched.path)) break :pdf .no_change; const before = file_watch.identify(st.io, watched.path) catch { retryWatchedFile(st, pane, generation); @@ -1012,7 +1012,7 @@ fn reloadWatchedFile(st: *State, pane: u8, announce: bool) bool { const after = st.core.panes[pane] orelse break :pdf .changed_uncommitted; if (after.serial != watched.serial) break :pdf .changed_uncommitted; - const pdf_state = if (after.pdf) |*pdf_after| pdf_after else break :pdf .changed_uncommitted; + const pdf_state = if (after.pdf) |pdf_after| pdf_after else break :pdf .changed_uncommitted; if (!std.mem.eql(u8, pdf_state.path, watched.path)) break :pdf .changed_uncommitted; const after_identity = file_watch.identify(st.io, watched.path) catch { retryWatchedFile(st, pane, generation); diff --git a/src/mouse.zig b/src/mouse.zig index 51a566d9..69d1456e 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -1395,7 +1395,7 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { fn chordCutPaste(p: *Pardes, cut: bool) void { const s = &p.drag.select; const pane = p.panes[s.id] orelse return; - if (comptime pdf_enabled) if (pane.pdf) |*pv| { + if (comptime pdf_enabled) if (pane.pdf) |pv| { if (!s.chorded) { s.chorded = true; pane.sel[sel_slot].state = .none; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index a1504acb..d075e24e 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -441,7 +441,7 @@ pub fn paneText(p: *Pardes, pane: *Pane, buf: []u8) []const u8 { // Then what its tag does not say: Collapse, and a PDF's PdfFit and // PdfTint, in the words they take. if (pane.collapsed) w.writeAll("collapsed ") catch {}; - if (comptime pardes.pdf_enabled) if (pane.pdf) |*pv| w.print("fit:{s} tint:{s} ", .{ @tagName(pv.fit), @tagName(pv.tint) }) catch {}; + if (comptime pardes.pdf_enabled) if (pane.pdf) |pv| w.print("fit:{s} tint:{s} ", .{ @tagName(pv.fit), @tagName(pv.tint) }) catch {}; // Last, the body's mode, which the tag shows as a box: helix's select // is normal mode extending. w.print("mode:{s} ", .{if (pane.body.mode == .normal and pane.body.select) "select" else @tagName(pane.body.mode)}) catch {}; diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 3895414b..df0c7975 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -461,7 +461,7 @@ fn readFlag(p: *Pardes, req: Req, on: bool) Reply { /// with the page), null for any other pane. fn pdfText(p: *Pardes, pane: *Pane) ?[]const u8 { if (comptime !pardes.pdf_enabled) return null; - const pv = &(pane.pdf orelse return null); + const pv = pane.pdf orelse return null; return pv.ensureText(p.pdf_gpa); } diff --git a/src/normal.zig b/src/normal.zig index 5bc24b08..986a8116 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -1006,7 +1006,7 @@ pub fn handlePdfNormal(p: *Pardes, pane: *Pane, key: Key) void { .pending, .ignored, .unbound => {}, .action => |semantic| { const result = panes.Pdf.applyNormal( - &pane.pdf.?, + pane.pdf.?, p.pdf_gpa, semantic, p.native_images, diff --git a/src/panes.zig b/src/panes.zig index f7a3cf8a..95917b42 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -61,7 +61,10 @@ pub const Pane = struct { col1: i32, }; - const PdfSlot = if (Pdf.enabled) ?Pdf.State else void; + /// A PDF's state is its own allocation in the pdf region, as a + /// terminal's is its own: inline, its raster slots would be every + /// pane's, and every rollback copy's. + const PdfSlot = if (Pdf.enabled) ?*Pdf.State else void; /// What an open prompt is asking. The answer is typed into `input`. pub const Prompt = union(enum) { @@ -509,12 +512,12 @@ pub const Pane = struct { } pub fn pdfPath(pane: *const Pane) ?[]const u8 { - if (comptime Pdf.enabled) if (pane.pdf) |*pv| return pv.path; + if (comptime Pdf.enabled) if (pane.pdf) |pv| return pv.path; return null; } pub fn pdfPage(pane: *const Pane) ?usize { - if (comptime Pdf.enabled) if (pane.pdf) |*pv| return pv.page; + if (comptime Pdf.enabled) if (pane.pdf) |pv| return pv.page; return null; } @@ -538,7 +541,7 @@ pub const Pane = struct { /// current scroll offset: file top line, or the scrollback offset pub fn scroll(pane: *Pane) i32 { if (pane.file) |f| return @intCast(f.scroll); - if (comptime Pdf.enabled) if (pane.pdf) |*pv| return @intCast(pv.text_scroll); + if (comptime Pdf.enabled) if (pane.pdf) |pv| return @intCast(pv.text_scroll); return pane.surfRow(terminal.gridOffset(pane) + pane.pan); } @@ -576,7 +579,7 @@ pub const Pane = struct { } } else if (hasPdf(pane)) { if (comptime Pdf.enabled) { - const pv = &pane.pdf.?; + const pv = pane.pdf.?; const max: i64 = @intCast(modal.lineCount(pv.text) -| 1); pv.text_scroll = @intCast(std.math.clamp( @as(i64, @intCast(pv.text_scroll)) + delta, @@ -637,7 +640,7 @@ pub const Pane = struct { to = @min(to, if (off == 0 and pane.textFits()) @max(pane.body.cur_row, lines - 1 - @intFromBool(std.mem.endsWith(u8, f.content, "\n"))) else lines - 1); } if (comptime Pdf.enabled) { - if (pane.pdf) |*pv| + if (pane.pdf) |pv| to = @min(to, @as(i32, @intCast(modal.lineCount(pv.text) -| 1))); } pane.scrollBy(@max(0, to - last)); @@ -684,7 +687,7 @@ pub const Pane = struct { pub fn hasPdfSelection(pane: *const Pane) bool { return if (comptime Pdf.enabled) - if (pane.pdf) |*pv| pv.selection != null and pv.selection_text.len > 0 else false + if (pane.pdf) |pv| pv.selection != null and pv.selection_text.len > 0 else false else false; } diff --git a/src/pardes.zig b/src/pardes.zig index 14c6bd98..bc0266c5 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4600,7 +4600,7 @@ pub const Pardes = struct { if (pane.image) |*iv| { iv.deinit(p.image_gpa); } - if (comptime pdf_enabled) if (pane.pdf) |*pv| panes.Pdf.deinitPane(p, pane, pv); + if (comptime pdf_enabled) if (pane.pdf) |pv| panes.Pdf.deinitPane(p, pane, pv); if (pane.file) |*file| panes.File.deinit(p, pane, file); if (pane.ovl) |o| p.gpa.free(o.text); views.tornDown(p, pane); @@ -5743,7 +5743,7 @@ pub const Pardes = struct { layout.compute(p); for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; - if (pane.pdf) |*pv| { + if (pane.pdf) |pv| { if (!std.meta.eql(before[id], panes.Pdf.paneViewport(p, pane))) { panes.Pdf.captureLayoutAnchor(pv); pv.layout_valid = false; @@ -6331,7 +6331,7 @@ pub const Pardes = struct { .none => {}, } if (pane.file) |f| return panes.Dir.ofName(f.path); - if (comptime pdf_enabled) if (pane.pdf) |*pv| + if (comptime pdf_enabled) if (pane.pdf) |pv| return std.fs.path.dirname(pv.path) orelse "/"; if (pane.image) |iv| return std.fs.path.dirname(iv.path) orelse "/"; return pane.cwdSlice(); @@ -7413,7 +7413,7 @@ pub const Pardes = struct { for (p.panes) |slot| if (slot) |pane| if (pane.places.busy(pane)) return next; for (p.panes) |slot| if (slot) |pane| if (pane.image) |iv| if (iv.decoder != null) return next; // A PDF page being rendered on a lane lands on the frame after it is done. - if (comptime pdf_enabled) for (p.panes) |slot| if (slot) |pane| if (pane.pdf) |*st| if (panes.Pdf.rendering(st)) return next; + if (comptime pdf_enabled) for (p.panes) |slot| if (slot) |pane| if (pane.pdf) |st| if (panes.Pdf.rendering(st)) return next; // A markdown text's tables still being read from the top (Tables.zig). for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.tables.busy()) return next; if (panes.Query.busy(p)) return next; diff --git a/src/pdf_view.zig b/src/pdf_view.zig index a0a48178..654284f4 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -2759,7 +2759,9 @@ pub fn openPane( // pane meanwhile. core.reserved_slots[id] = true; defer core.reserved_slots[id] = false; - var state = if (filesystem.localPath(path) != null) + const state = try core.pdf_gpa.create(State); + errdefer core.pdf_gpa.destroy(state); + state.* = if (filesystem.localPath(path) != null) try State.open(core.pdf_gpa, path, page_one_based) else virtual: { const bytes = try filesystem.read(core, path); @@ -2779,7 +2781,7 @@ pub fn openPane( /// armed over it has nothing to reload. pub fn sameBytes(core: *pardes.Pardes, pane: *const pardes.Pane) bool { if (comptime !enabled) return false; - const state = if (pane.pdf) |*pv| pv else return false; + const state = if (pane.pdf) |pv| pv else return false; if (state.file == 0) return false; const bytes = filesystem.read(core, state.path) catch return false; defer core.gpa.free(bytes); @@ -2792,13 +2794,15 @@ pub fn deinitPane(core: *pardes.Pardes, pane: *pardes.Pane, state: *State) void if (comptime !enabled) return; if (core.paneIdOf(pane)) |id| core.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); state.deinit(core.pdf_gpa); + core.pdf_gpa.destroy(state); + pane.pdf = null; } /// A PDF pane loaded from a dump given its document again (MuPDF's, so /// never in the dump) from its file; one changed since is laid out anew. pub fn reopen(core: *pardes.Pardes, pane: *pardes.Pane) !void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; const local = filesystem.localPath(state.path) orelse return error.NonLocalPath; const bytes = try filesystem.readFile(core.pdf_gpa, local); defer core.pdf_gpa.free(bytes); @@ -2858,7 +2862,7 @@ pub fn reloadWatched( pane: *pardes.Pane, ) !void { if (comptime !enabled) return error.PdfDisabled; - const state = &(pane.pdf orelse return error.MissingPdfState); + const state = pane.pdf orelse return error.MissingPdfState; try state.reload(core.pdf_gpa); resetPageChrome(pane); refreshCleanSections(core, state); @@ -2895,7 +2899,7 @@ fn rearmCleanSections( pub fn openSections(core: *pardes.Pardes, id: usize) void { if (comptime !enabled) return; const pane = core.panes[id] orelse return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (rearmCleanSections(core, id, pane, state)) return; const content = state.renderSections(core.pdf_gpa, core.gpa) catch |err| return core.reportError(id, "PdfSections", err); // No outline: said, not an empty pane to puzzle over. @@ -2945,7 +2949,7 @@ fn sectionsOwner(core: *pardes.Pardes, output_id: usize) ?SectionsOwner { const file = output.file.?; for (core.panes, 0..) |slot, id| { const pane = slot orelse continue; - const state = if (pane.pdf) |*pdf_state| pdf_state else continue; + const state = if (pane.pdf) |pdf_state| pdf_state else continue; const token = state.sections_output orelse continue; if (token.pane == output_id and token.serial == output.serial and token.revision == file.revision) @@ -3017,7 +3021,7 @@ pub fn activatePage( reveal: bool, ) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (state.activatePage(core.pdf_gpa, page, reveal)) resetPageChrome(pane); } @@ -3028,7 +3032,7 @@ pub fn revealOutlineDestination( ) void { if (comptime !enabled) return; activatePage(core, pane, destination.page, false); - const state = &pane.pdf.?; + const state = pane.pdf.?; state.queueOutlineReveal(destination); // Consume immediately when geometry already exists; otherwise the PDF // draw pass consumes the same value after the next layout transaction. @@ -3044,7 +3048,7 @@ 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; + 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]); @@ -3056,7 +3060,7 @@ pub fn place(pane: *const pardes.Pane) ?Place { /// 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; + 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])) + @@ -3069,7 +3073,7 @@ pub fn placeY(pane: *const pardes.Pane, at: Place) ?f64 { /// 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 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; @@ -3087,13 +3091,13 @@ pub fn landAt(core: *pardes.Pardes, pane: *pardes.Pane, at: Place) void { pub fn toggleFit(pane: *pardes.Pane) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; state.toggleFit(); } pub fn toggleTint(pane: *pardes.Pane) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; state.toggleTint(); } @@ -3111,7 +3115,7 @@ pub fn paneViewport(core: *const pardes.Pardes, pane: *const pardes.Pane) ?Viewp pub fn ensurePaneLayout(core: *pardes.Pardes, pane: *pardes.Pane) ?Viewport { if (comptime !enabled) return null; - const state = &(pane.pdf orelse return null); + const state = pane.pdf orelse return null; const view = paneViewport(core, pane) orelse return null; ensureLayout(state, view); return view; @@ -3128,7 +3132,7 @@ pub fn tintColors(core: *const pardes.Pardes) TintColors { pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?image.NativeGeometry { if (comptime !enabled) return null; - const state = if (pane.pdf) |*view| view else return null; + const state = if (pane.pdf) |view| view else return null; const view = paneViewport(core, pane) orelse return null; return activeGeometry(state, view); } @@ -3136,7 +3140,7 @@ pub fn paneGeometry(core: *const pardes.Pardes, pane: *const pardes.Pane) ?image /// The page under grid row `row`, from the middle of its cell. pub fn pageAtGridRow(core: *const pardes.Pardes, pane: *const pardes.Pane, row: u16) ?usize { if (comptime !enabled) return null; - const state = &(pane.pdf orelse return null); + const state = pane.pdf orelse return null; const rect = core.rects[core.paneIdOf(pane) orelse return null]; const body_y = core.bodyTop(pane, rect); if (!state.layout_valid or row < body_y or row >= body_y + (rect.h -| pane.tag_rows)) return null; @@ -3154,7 +3158,7 @@ pub fn pageAtGridRow(core: *const pardes.Pardes, pane: *const pardes.Pane, row: pub fn paneNativeReady(core: *const pardes.Pardes, pane: *const pardes.Pane) bool { if (comptime !enabled) return false; if (!core.native_images) return false; - const state = if (pane.pdf) |*view| view else return false; + const state = if (pane.pdf) |view| view else return false; const view = paneViewport(core, pane) orelse return false; for (state.rasters[0..state.rasters_len]) |*raster| { if (raster.valid and raster.rgba.len > 0 and @@ -3170,7 +3174,7 @@ pub fn nativePageAtGridRow( ) ?usize { if (comptime !enabled) return null; if (!core.native_images) return null; - const state = if (pane.pdf) |*view| view else return null; + const state = if (pane.pdf) |view| view else return null; const page = pageAtGridRow(core, pane, row) orelse return null; const view = paneViewport(core, pane) orelse return null; if (!pageReady(state, view, page)) return null; @@ -3188,7 +3192,7 @@ pub fn panePointAtPage( clamp_to_page: bool, ) ?Point { if (comptime !enabled) return null; - const state = if (pane.pdf) |*view| view else return null; + const state = if (pane.pdf) |view| view else return null; const view = paneViewport(core, pane) orelse return null; const raster = rasterForPageConst(state, page) orelse return null; if (raster.rgba.len == 0) return null; @@ -3210,7 +3214,7 @@ pub fn probeAt( if (comptime !enabled) return null; const page = nativePageAtGridRow(core, pane, row) orelse return null; const point = panePointAtPage(core, pane, page, col, row, false) orelse return null; - const state = &(pane.pdf orelse return null); + const state = pane.pdf orelse return null; if (state.document.linkAt(core.pdf_gpa, page, point) catch null) |found| { var link = found; if (look.pdfLinkLocation(core, pane, link) != null) { @@ -3241,7 +3245,7 @@ pub fn beginDrag( if (comptime !enabled) return false; const hit_page = pageAtGridRow(core, pane, row) orelse return false; if (hit_page != pane.pdf.?.page) activatePage(core, pane, hit_page, false); - const state = &pane.pdf.?; + const state = pane.pdf.?; state.clearDrag(); const point = panePointAtPage(core, pane, state.page, col, row, false) orelse return false; state.drag_anchor = point; @@ -3265,7 +3269,7 @@ pub fn updateSelection( invalidate_raster: bool, ) SelectionUpdate { if (comptime !enabled) return .failed; - const state = &(pane.pdf orelse return .failed); + const state = pane.pdf orelse return .failed; const anchor = state.drag_anchor orelse return .failed; const point = panePointAtPage(core, pane, state.page, col, row, true) orelse return .failed; if (state.drag_head) |head| if (head.x == point.x and head.y == point.y) return .stationary; @@ -3335,7 +3339,7 @@ pub fn pointerUpdate( pub fn pointerCancel(pane: *pardes.Pane, drag: PointerDrag) void { if (comptime !enabled) return; - if (drag.native) if (pane.pdf) |*state| { + if (drag.native) if (pane.pdf) |state| { if (drag.selection_changed) state.invalidateRaster(state.page); state.clearDrag(); }; @@ -3349,7 +3353,7 @@ pub fn pointerRelease( chorded: bool, ) PointerRelease { if (comptime !enabled) return .{}; - const state = &(pane.pdf orelse return .{}); + const state = pane.pdf orelse return .{}; const slot = @backingInt(button); const grid_selection = pane.sel[slot]; pane.sel[slot].state = .none; // native quads, not projected text cells @@ -3427,7 +3431,7 @@ pub fn scrollPane(core: *pardes.Pardes, pane: *pardes.Pane, delta_pixels: f64, g if (comptime !enabled) return false; const tz = tracy.zone(@src(), "pdf.scroll_notch"); defer tz.end(); - const state = &(pane.pdf orelse return false); + const state = pane.pdf orelse return false; const view = ensurePaneLayout(core, pane) orelse return false; const from = state.document_scroll_y; const result = scrollDocument(state, view, delta_pixels) orelse return false; @@ -3461,7 +3465,7 @@ fn stripAt(state: *State, view: Viewport, motion: pardes.animation.Motion, now_n /// A PDF strip is still on its way to where the core stands. pub fn moving(core: *const pardes.Pardes) bool { if (comptime !enabled) return false; - for (core.panes) |slot| if (slot) |pane| if (pane.pdf) |*state| if (state.follow.moving()) return true; + for (core.panes) |slot| if (slot) |pane| if (pane.pdf) |state| if (state.follow.moving()) return true; return false; } @@ -3469,7 +3473,7 @@ pub fn verticalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) vo if (comptime !enabled) return; if (!core.native_images) { // The text layer: a page a notch. - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (stepPage(state, core.pdf_gpa, direction)) resetPageChrome(pane); return; } @@ -3480,7 +3484,7 @@ pub fn verticalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) vo pub fn horizontalWheel(core: *pardes.Pardes, pane: *pardes.Pane, direction: i8) void { if (comptime !enabled) return; - const state = &(pane.pdf orelse return); + const state = pane.pdf orelse return; if (!core.native_images or state.fit != .height) return; const placed = paneGeometry(core, pane) orelse return; const cols: u32 = @intCast(@max(1, config.wheel_cols)); @@ -3499,7 +3503,7 @@ pub fn draw( ) bool { if (comptime !enabled) return false; if (!core.native_images or rect.h <= pane.tag_rows) return false; - const state = &(pane.pdf orelse return false); + const state = pane.pdf orelse return false; const view = paneViewport(core, pane) orelse return false; const key = TintKey{ .mode = state.tint, .colors = tintColors(core) }; applySizes(state, core.pdf_gpa); diff --git a/src/tagline.zig b/src/tagline.zig index 73683c1e..e7194981 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -113,7 +113,7 @@ fn shownDir(arena: std.mem.Allocator, pane: *Pane) ![]const u8 { /// PDF's page, a terminal's directory -- without the space that parts it /// from the tag's own text. It is never stored, and never edited. pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { - if (comptime pdf_enabled) if (pane.pdf) |*pv| + if (comptime pdf_enabled) if (pane.pdf) |pv| return std.fmt.allocPrint(arena, "{s} [{d}/{d}]", .{ pv.path, pv.page + 1, pv.page_count }); // What is drawn: with no graphics, glyph art whatever `Petscii` says. if (pane.image) |*state| return panes.Image.tagPrefix(arena, state, state.glyph_art or !p.native_images); -- cgit v1.3