summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/look.zig b/src/look.zig
index 9df2b485..4a8cb894 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -16,6 +16,7 @@ const builtin = @import("builtin");
const libc = std.c;
const pardes = @import("pardes.zig");
const config = @import("config.zig");
+const pdf_enabled = @import("pardes_config").mupdf;
const embedded_sources = if (pardes.platform == .web) @import("embedded_sources") else struct {
pub const Source = struct { path: []const u8, contents: []const u8 };
pub const all = [_]Source{};
@@ -164,10 +165,21 @@ test "parsePathLine: spots, ranges, and the paths that merely look like them" {
}
}
+/// A file-like Look target has a rendering kind only in MuPDF builds. The
+/// feature-off enum has no `pdf` tag at all, so `.pdf` is indistinguishable
+/// from any other ordinary file before it reaches the core.
+pub const FileKind = if (pdf_enabled) enum { text, pdf } else enum { text };
+
+pub const FileTarget = struct {
+ path: []const u8,
+ at: Spot,
+ kind: FileKind = .text,
+};
+
pub const Target = union(enum) {
none,
dir: []const u8, // resolved absolute path, in caller's buf
- file: struct { path: []const u8, at: Spot },
+ file: FileTarget,
image: struct { path: []const u8 },
url: []const u8,
/// `@p7:10:5` — pane 7, line 10, column 5 (0 = unspecified). The one
@@ -183,6 +195,36 @@ pub fn isImagePath(path: []const u8) bool {
return false;
}
+pub fn isPdfPath(path: []const u8) bool {
+ if (comptime !pdf_enabled) return false;
+ return std.ascii.endsWithIgnoreCase(path, ".pdf");
+}
+
+test "PDF file kinds exist only in MuPDF-enabled builds" {
+ try std.testing.expectEqual(pdf_enabled, isPdfPath("manual.PDF"));
+ try std.testing.expect(!isPdfPath("manual.pdf.txt"));
+ try std.testing.expectEqual(
+ pdf_enabled,
+ std.meta.stringToEnum(FileKind, "pdf") != null,
+ );
+ try std.testing.expect(std.meta.stringToEnum(std.meta.Tag(Target), "pdf") == null);
+}
+
+test ".pdf Look paths are ordinary files when MuPDF is disabled" {
+ if (!platform_has_fs) return;
+ var realbuf: [4096]u8 = undefined;
+ const target = resolve("docs/design.pdf", ".", &realbuf);
+ switch (target) {
+ .file => |file| {
+ if (comptime pdf_enabled)
+ try std.testing.expectEqual(FileKind.pdf, file.kind)
+ else
+ try std.testing.expectEqual(FileKind.text, file.kind);
+ },
+ else => return error.PdfDidNotResolveAsFile,
+ }
+}
+
/// Resolve a looked-at word against the pane's directory. `realbuf` must
/// outlive the returned Target (native paths point into it; web paths are
/// process-lifetime slices in the embedded source archive).
@@ -217,6 +259,11 @@ pub fn resolve(word_raw: []const u8, cwd: []const u8, realbuf: *[4096]u8) Target
const rp = realpath(jz.ptr, realbuf) orelse return .none;
const resolved = std.mem.span(rp);
if (isDir(rp)) return .{ .dir = resolved };
+ if (comptime pdf_enabled) if (isPdfPath(resolved)) return .{ .file = .{
+ .path = resolved,
+ .at = pl.at,
+ .kind = .pdf,
+ } };
if (isImagePath(resolved)) return .{ .image = .{ .path = resolved } };
return .{ .file = .{ .path = resolved, .at = pl.at } };
} else {