From d61ae5ed2a021e53308e656f46d2835d4decd49c Mon Sep 17 00:00:00 2001 From: jao Date: Thu, 25 Sep 2025 22:08:51 +0100 Subject: devonthink as pdf reader --- custom/jao-custom-browse.el | 2 +- custom/jao-custom-pdf.el | 80 ++++---------------------------------------- data/find-devon-url.scpt | Bin 0 -> 3522 bytes init.el | 2 +- lib/doc/jao-pdf.el | 79 ++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 87 insertions(+), 76 deletions(-) create mode 100644 data/find-devon-url.scpt diff --git a/custom/jao-custom-browse.el b/custom/jao-custom-browse.el index 1f7ccc3..ba5ac79 100644 --- a/custom/jao-custom-browse.el +++ b/custom/jao-custom-browse.el @@ -5,7 +5,7 @@ ;;; Browsing ;;;; variables -(defvar jao-browse-doc-use-emacs-p (jao-is-linux)) +(defvar jao-browse-doc-use-emacs-p t) (defvar jao-browse-url-function nil) (defvar jao-browse-url-external-function nil) diff --git a/custom/jao-custom-pdf.el b/custom/jao-custom-pdf.el index 5e2c27d..98a28b1 100644 --- a/custom/jao-custom-pdf.el +++ b/custom/jao-custom-pdf.el @@ -1,84 +1,19 @@ ;; -*- lexical-binding: t -*- ;;;; open pdfs -(jao-when-linux - (use-package saveplace-pdf-view +(use-package saveplace-pdf-view :ensure t :demand t - :after doc-view)) + :after doc-view) -(use-package jao-pdf :demand t) - -(setq jao-open-doc-fun 'jao-find-or-open) -(setq jao-org-open-pdf-fun 'jao-find-or-open) - -(defun jao-zathura-open (file page) - (let ((id (jao-x11-search-window (jao-pdf-zathura-title-rx file)))) - (if (string-blank-p id) - (progn - (when jao-xmonad-enabled (jao-x11-goto-ws 2)) - (jao-shell-exec (jao-pdf-zathura-open-cmd file page))) - (let* ((page (if page (format " && xdotool type %dg" page) "")) - (cmd (format "xdotool windowactivate %s%s" id page))) - (jao-shell-exec cmd t))))) - -(defun jao-x11-zathura-goto-org (&optional title no-ask) - (let ((title (or title (jao-shell-string "xdotool" - "getactivewindow" - "getwindowname")))) - (jao-org-open-from-zathura title no-ask))) - -(defun jao-find-or-open (file &optional page height) - (cond ((and jao-browse-doc-use-emacs-p window-system) - (let* ((buffs (buffer-list)) - (b (catch 'done - (while buffs - (when (string-equal (buffer-file-name (car buffs)) file) - (throw 'done (car buffs))) - (setq buffs (cdr buffs)))))) - (jao-afio-goto-docs) - (if b (pop-to-buffer b) (find-file file)) - (when page (jao-doc-view-goto-page page height)))) - (jao-river-enabled (jao-river-open-with-zathura file page)) - (jao-sway-enabled (jao-sway-open-with-zathura file page)) - (t (jao-zathura-open file page)))) - -(defun jao-open-doc (&optional file page height) - (interactive) - (when-let (file (or file - (read-file-name "Document: " - (concat jao-org-dir "/doc/")))) - (funcall jao-open-doc-fun file page height))) - -(defun jao-select-pdf () - (interactive) - (jao-buffer-same-mode '(pdf-view-mode doc-view-mode) - #'jao-afio-goto-docs)) - -(defun jao-open-with-zathura () - (interactive) - (when-let (f buffer-file-name) - (let ((p (jao-doc-view-current-page))) - (cond (jao-river-enabled (jao-river-open-with-zathura f p)) - (jao-sway-enabled (jao-sway-open-with-zathura f p)) - (t (jao-zathura-open f p)))))) - -;; doc:// links for browse-url -(defun jao-open-doc-url (url &rest _) - (when (string-match "doc://\\([^?]+\\)\\(\\?.*\\)?" url) - (let ((file (match-string 1 url)) - (page (when-let* ((qs (match-string 2 url)) - (long (> (length qs) 1)) - (ps (url-parse-query-string (substring qs 1))) - (pn (cadr (assoc "page" ps)))) - (string-to-number pn)))) - (jao-open-doc (expand-file-name (concat "doc/" file) jao-org-dir) page)))) - -(add-to-list 'browse-url-handlers (cons "^doc://.+" 'jao-open-doc-url)) +(use-package jao-pdf + :demand t + :config + (setq jao-open-doc-fun (jao-d-l 'jao-pdf-open-in-devon 'jao-find-or-open)) + (setq jao-org-open-pdf-fun jao-open-doc-fun)) ;;;; doc-view (use-package doc-view - :if (jao-is-linux) :init (setq doc-view-cache-directory "~/.emacs.d/cache/docview" doc-view-resolution 110 @@ -96,7 +31,6 @@ (use-package jao-doc-session :demand t) (use-package jao-doc-view - :if (jao-is-linux) :demand t :bind (:map doc-view-mode-map ("b" . jao-doc-view-back) diff --git a/data/find-devon-url.scpt b/data/find-devon-url.scpt new file mode 100644 index 0000000..e902d66 Binary files /dev/null and b/data/find-devon-url.scpt differ diff --git a/init.el b/init.el index e1ece4e..666d551 100644 --- a/init.el +++ b/init.el @@ -1312,7 +1312,7 @@ ;;;; doc view &co. -(jao-when-linux (require 'jao-custom-pdf)) +(require 'jao-custom-pdf) ;;;; epub (use-package nov diff --git a/lib/doc/jao-pdf.el b/lib/doc/jao-pdf.el index 1ee74bc..9bf0098 100644 --- a/lib/doc/jao-pdf.el +++ b/lib/doc/jao-pdf.el @@ -1,6 +1,6 @@ ;;; jao-pdf.el --- utilities for pdf files -*- lexical-binding: t; -*- -;; Copyright (C) 2022 jao +;; Copyright (C) 2022, 2025 jao ;; Author: jao ;; Keywords: docs @@ -96,5 +96,82 @@ (defun jao-pdf-zathura-org-link (title) (jao-pdf--zathura-link (jao-pdf-zathura-file-info title))) +(defun jao-zathura-open (file page) + (let ((id (jao-x11-search-window (jao-pdf-zathura-title-rx file)))) + (if (string-blank-p id) + (progn + (when jao-xmonad-enabled (jao-x11-goto-ws 2)) + (jao-shell-exec (jao-pdf-zathura-open-cmd file page))) + (let* ((page (if page (format " && xdotool type %dg" page) "")) + (cmd (format "xdotool windowactivate %s%s" id page))) + (jao-shell-exec cmd t))))) + +;;; DevonThink + +(defun jao-pdf--devon-link (file) + (jao-shell-string "osascript" + (expand-file-name "find-devon-url.scpt" jao-data-dir) + (file-truename file))) + +(defun jao-pdf-open-in-devon (file &optional page height) + (let ((url (jao-pdf--devon-link file))) + (unless (string-empty-p (or url "")) + (let* ((p (if page (format "?page=%s" (- page 1)) "")) + (u (format "%s%s" url p))) + (message "Opening %s ..." u) + (jao-shell-exec* "open" u))))) + +;;; Open doc functions + +(defun jao-find-or-open (file &optional page height) + (cond ((and jao-browse-doc-use-emacs-p window-system) + (let* ((buffs (buffer-list)) + (b (catch 'done + (while buffs + (when (string-equal (buffer-file-name (car buffs)) file) + (throw 'done (car buffs))) + (setq buffs (cdr buffs)))))) + (jao-afio-goto-docs) + (if b (pop-to-buffer b) (find-file file)) + (when page (jao-doc-view-goto-page page height)))) + (jao-river-enabled (jao-river-open-with-zathura file page)) + (jao-sway-enabled (jao-sway-open-with-zathura file page)) + (t (jao-zathura-open file page)))) + +(defun jao-open-doc (&optional file page height) + (interactive) + (when-let (file (or file + (read-file-name "Document: " + (concat jao-org-dir "/doc/")))) + (funcall jao-open-doc-fun file page height))) + +(defun jao-select-pdf () + (interactive) + (jao-buffer-same-mode '(pdf-view-mode doc-view-mode) + #'jao-afio-goto-docs)) + +(defun jao-open-with-zathura () + (interactive) + (when-let (f buffer-file-name) + (let ((p (jao-doc-view-current-page))) + (cond (jao-river-enabled (jao-river-open-with-zathura f p)) + (jao-sway-enabled (jao-sway-open-with-zathura f p)) + (t (jao-zathura-open f p)))))) + +;;; doc:// links for browse-url + +(defun jao-open-doc-url (url &rest _) + (when (string-match "doc://\\([^?]+\\)\\(\\?.*\\)?" url) + (let ((file (match-string 1 url)) + (page (when-let* ((qs (match-string 2 url)) + (long (> (length qs) 1)) + (ps (url-parse-query-string (substring qs 1))) + (pn (cadr (assoc "page" ps)))) + (string-to-number pn)))) + (jao-open-doc (expand-file-name (concat "doc/" file) jao-org-dir) page)))) + +(add-to-list 'browse-url-handlers (cons "^doc://.+" 'jao-open-doc-url)) + + (provide 'jao-pdf) ;;; jao-pdf.el ends here -- cgit v1.2.3