summaryrefslogtreecommitdiffhomepage
path: root/custom/jao-custom-pdf.el
diff options
context:
space:
mode:
Diffstat (limited to 'custom/jao-custom-pdf.el')
-rw-r--r--custom/jao-custom-pdf.el144
1 files changed, 144 insertions, 0 deletions
diff --git a/custom/jao-custom-pdf.el b/custom/jao-custom-pdf.el
new file mode 100644
index 0000000..4ebd7f1
--- /dev/null
+++ b/custom/jao-custom-pdf.el
@@ -0,0 +1,144 @@
+;; -*- lexical-binding: t -*-
+
+;;;; open pdfs
+(jao-when-linux
+ (use-package saveplace-pdf-view
+ :ensure t
+ :demand t
+ :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))
+
+;;;; doc-view
+(use-package doc-view
+ :init
+ (setq doc-view-cache-directory "~/.emacs.d/cache/docview"
+ doc-view-resolution 110
+ doc-view-continuous t
+ doc-view-conversion-refresh-interval 1
+ doc-view-mupdf-use-svg t)
+ :hook ((doc-view-mode . jao-doc-session-mark))
+ :bind (:map doc-view-mode-map
+ ("j" . doc-view-next-line-or-next-page)
+ ("J" . doc-view-search-next-match)
+ ("k" . doc-view-previous-line-or-previous-page)
+ ("K" . doc-view-search-previous-match)
+ ("z" . jao-open-with-zathura)))
+
+(use-package jao-doc-session :demand t)
+
+(use-package jao-doc-view
+ :demand t
+ :bind (:map doc-view-mode-map
+ ("b" . jao-doc-view-back)
+ ("B" . jao-doc-view-forward)
+ ("S" . jao-doc-session-save)
+ ("u" . jao-doc-view-visit-url)))
+
+;;;; transient
+(defun jao-org-pdf-goto-org-linking ()
+ (interactive)
+ (jao-org-pdf-goto-org 4))
+
+(jao-transient-major-mode doc-view
+ ["Notes"
+ ("o" "notes file" jao-org-pdf-goto-org)
+ ("O" "notes file, linking" jao-org-pdf-goto-org-linking)]
+ ["Navigation"
+ ("b" "back jump" jao-doc-view-back)
+ ("B" "forward jump" jao-doc-view-back)
+ ("u" "visit URL" jao-doc-view-visit-url)]
+ ["Slices"
+ ("cb" "bounding box" doc-view-set-slice-from-bounding-box)
+ ("cm" "using mouse" doc-view-set-slice-using-mouse)]
+ ["Session"
+ ("s" "load session" jao-afio-open-pdf-session)
+ ("S" "save session" jao-doc-session-save)
+ ("d" "visit cache directory" doc-view-dired-cache)]
+ ["External viewers"
+ ("z" "open with zathura" jao-open-with-zathura)])
+
+(with-eval-after-load "pdf-view"
+ (jao-transient-major-mode pdf-view
+ ["Notes"
+ ("o" "notes file" jao-org-pdf-goto-org)
+ ("O" "notes file, linking" jao-org-pdf-goto-org-linking)]
+ ["Navigation"
+ ("b" "back jump" pdf-history-backward)
+ ("f" "forward jump" pdf-history-forward)]
+ ["Session"
+ ("s" "load session" jao-afio-open-pdf-session)
+ ("S" "save session" jao-doc-session-save)]
+ ["External viewers"
+ ("z" "open with zathura" jao-open-with-zathura)]))
+
+;; (transient-get-suffix 'jao-transient-pdf-view '(0 -1))
+
+(provide 'jao-custom-pdf)