summaryrefslogtreecommitdiff
path: root/src/ninep
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-29 12:29:43 -0300
committerGabriel Schneider <[email protected]>2026-10-01 00:12:16 -0300
commit549cfa40e99c3d1937b742a0dba043281c687c83 (patch)
tree51966e9ee0e49ddd87738f0f89ce52a279eb0c6f /src/ninep
parent54aaa0bce75e1f231d68647a9d7c0f052ed92f8e (diff)
downloadpardes-549cfa40e99c3d1937b742a0dba043281c687c83.tar.gz
pardes-549cfa40e99c3d1937b742a0dba043281c687c83.zip
A PDF's body reads the text layer of the page it shows
The body of a PDF pane read as empty. It now reads MuPDF's text extraction of the page shown, the text search and n/N already use, read-only (a write is refused, as round 22's body refusal made it). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/ninep')
-rw-r--r--src/ninep/ctl.zig21
-rw-r--r--src/ninep/pane.zig17
2 files changed, 37 insertions, 1 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index db058ff2..be90bb6f 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -2115,6 +2115,27 @@ test "a language server question that finds nothing, cannot be answered, or is a
try testing.expectEqual(E.INVAL, wr(p, Node.of(img.serial, .body), "text\n").errno());
}
+test "a PDF's body reads its page's text layer, and takes no write" {
+ if (comptime !pardes.pdf_enabled) return;
+ const p = try withFile(testing.allocator, "x\n");
+ defer p.deinit();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const bytes = try @import("mupdf").makeLinksTestPdf(testing.allocator);
+ defer testing.allocator.free(bytes);
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "doc.pdf", .data = bytes });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)];
+ var line: [4200]u8 = undefined;
+ _ = wr(p, root_look, try std.fmt.bufPrint(&line, "{s}/doc.pdf\n", .{dir}));
+ p.sync();
+ const doc = p.panes[p.active].?;
+ try testing.expect(doc.hasPdf());
+ const body = Node.of(doc.serial, .body);
+ try testing.expect(std.mem.indexOf(u8, rd(p, body, 0, 4096).bytes, "target.txt") != null);
+ try testing.expectEqual(E.INVAL, wr(p, body, "more\n").errno());
+}
+
test "every EINVAL a write gets says why, in its err record too; DEL is a control character in a line" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index 9e832df6..311a2429 100644
--- a/src/ninep/pane.zig
+++ b/src/ninep/pane.zig
@@ -257,7 +257,7 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 {
const pf = &pane.fs;
return switch (f) {
// A terminal's body reads its screen and history, as text.
- .body => if (pane.file == null and pane.isTerminal()) panes.Terminal.screenTextLen(pane) else bodyOf(pane).len,
+ .body => if (pane.file == null and pane.isTerminal()) panes.Terminal.screenTextLen(pane) else if (pdfText(p, pane)) |t| t.len else bodyOf(pane).len,
.data, .xdata => bodyOf(pane).len,
.tag => tagOf(p, pane).len,
.name => nameOf(pane).len + 1,
@@ -367,6 +367,14 @@ fn readFlag(p: *Pardes, req: Req, on: bool) Reply {
return tree.stagedReply(p, req);
}
+/// The text layer of the page a PDF pane shows (MuPDF's extraction, kept
+/// 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);
+ return pv.ensureText(p.pdf_gpa);
+}
+
fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (pane.file != null) {
const text = bodyOf(pane);
@@ -379,6 +387,13 @@ fn readBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
.len = clip(n),
} } };
}
+ // A PDF's body is its text layer, the page shown's: read-only.
+ if (pdfText(p, pane)) |text| {
+ const off = @min(req.off, text.len);
+ const out = p.fs.stage(p.gpa);
+ out.appendSlice(p.gpa, text[off..][0..@min(text.len - off, req.size)]) catch return Reply.fail(req.tag, E.NOMEM);
+ return tree.stagedReply(p, req);
+ }
if (req.handle != 0 and pane.isTerminal()) return screen.readSnapshot(p, req, pane);
const text = panes.Terminal.screenTextAlloc(pane, p.gpa) catch
return Reply.fail(req.tag, E.NOMEM);