diff options
Diffstat (limited to 'lib/wm/jao-exwm-zathura.el')
| -rw-r--r-- | lib/wm/jao-exwm-zathura.el | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/lib/wm/jao-exwm-zathura.el b/lib/wm/jao-exwm-zathura.el new file mode 100644 index 0000000..9da0a25 --- /dev/null +++ b/lib/wm/jao-exwm-zathura.el @@ -0,0 +1,152 @@ +;;; jao-exwm-zathura.el --- running zathura as doc manager -*- lexical-binding: t; -*- + +;; Copyright (C) 2026 jao + +;; Author: jao <mail@jao.io> +;; Keywords: docs + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <https://www.gnu.org/licenses/>. + +(require 'jao-exwm) +(require 'jao-pdf) +(require 'jao-doc-view) +(require 'jao-afio) +(require 'jao-org-notes) + +;;; Zathura support +(defun jao-zathura--buffer-p (b) + (string= "Zathura" (or (buffer-local-value 'exwm-class-name b) ""))) + +(defun jao-zathura--buffers () + (seq-filter #'jao-zathura--buffer-p (buffer-list))) + +(defun jao-zathura--file-info (b) + (with-current-buffer b + (jao-pdf-zathura-file-info (or exwm-title "")))) + +(defun jao-zathura-goto-page (page-no) + (jao-exwm-send-str (format "%sg" page-no))) + +;;;###autoload +(defun jao-zathura-open-doc (&optional file-name page-no _height) + (interactive) + (let* ((file-name (expand-file-name (or file-name (buffer-file-name)))) + (buffer (seq-find `(lambda (b) + (string= ,file-name + (car (jao-zathura--file-info b)))) + (jao-zathura--buffers))) + (page-no (or page-no (jao-doc-view-current-page)))) + (when jao-exwm-use-afio (jao-afio-goto-docs)) + (if (not buffer) + (jao-exwm-run (if page-no + (format "zathura -P %s '%s'" page-no file-name) + (format "zathura '%s'" file-name))) + (pop-to-buffer buffer) + (when page-no (jao-zathura-goto-page page-no))) + (current-buffer))) + +(defun jao-exwm--zathura-setup () + (when (and (string= exwm-class-name "Zathura") + (not jao-pdf--outline)) + (let ((info (jao-zathura--file-info (current-buffer)))) + (jao-doc-session-mark (car info)) + (jao-doc-session-save) + (doc-view-imenu-index (car info) #'jao-zathura-goto-page)))) + +(add-hook 'exwm-update-title-hook #'jao-exwm--zathura-setup t) + +;;;###autoload +(defun jao-exwm-pdf-zathura-close-all () + (interactive) + (dolist (b (jao-zathura--buffers)) + (ignore-errors + (switch-to-buffer b) + (jao-exwm-send-str "q"))) + t) + +;;;###autoload +(defun jao-exwm-zathura-goto-org (&optional arg) + (interactive "P") + (when-let* ((info (jao-zathura--file-info (current-buffer))) + (file (jao-org-find-for-pdf (car info)))) + (let ((newp (not (file-exists-p file)))) + (when (or arg newp) (org-store-link nil t)) + (find-file-other-window file) + (when newp + (jao-org-insert-doc-skeleton) + (org-insert-link))))) + +;;;###autoload +(defun jao-exwm-zathura-goto-org* () + (interactive) + (jao-exwm-zathura-goto-org t)) + +;;;###autoload +(defun jao-exwm-org-store-zathura-link () + (when-let* ((info (jao-zathura--file-info (current-buffer)))) + (let* ((file-name (car info)) + (page (cadr info)) + (desc (jao-pdf-section-title page file-name))) + (jao-org-links-store-pdf-link file-name page desc)))) + +;;;###autoload +(defun jao-exwm-pdf-enable-zathura () + (interactive) + (add-hook 'kill-emacs-query-functions #'jao-exwm-pdf-zathura-close-all t) + (setq jao-pdf-open-in-emacs nil) + (setq jao-org-open-pdf-fun #'jao-zathura-open-doc) + (setq jao-org-links-pdf-store-fun #'jao-exwm-org-store-zathura-link) + (setq jao-open-doc-fun #'jao-zathura-open-doc)) + +;;;###autoload +(defun jao-exwm-pdf-disable-zathura () + (interactive) + (remove-hook 'kill-emacs-query-functions #'jao-exwm-pdf-zathura-close-all) + (setq jao-pdf-open-in-emacs t) + (setq jao-org-open-pdf-fun #'jao-find-or-open) + (setq jao-org-links-pdf-store-fun nil) + (setq jao-open-doc-fun #'jao-find-or-open)) + +;;;###autoload +(defun jao-exwm-org-to-pdf-file () + (expand-file-name (concat "doc/" (file-name-base buffer-file-name) ".pdf") + (file-name-directory jao-org-notes-dir))) + +;;;###autoload +(defun jao-exwm-zathura-goto-pdf () + (interactive) + (if jao-pdf-open-in-emacs + (jao-org-goto-pdf) + (when-let* ((pdf (jao-exwm-org-to-pdf-file))) + (jao-zathura-open-doc pdf)))) + +(with-eval-after-load "org" + (define-key org-mode-map (kbd "C-c o") #'jao-exwm-zathura-goto-pdf)) + +(when (not jao-pdf-open-in-emacs) + (jao-exwm-pdf-enable-zathura)) + +(defun jao-exwm-select-pdf () + (interactive) + (let ((b (read-buffer "Document: " nil t + (lambda (b) + (let ((b (cdr b))) + (or (jao-zathura--buffer-p b) + (member (buffer-local-value 'major-mode b) + '(pdf-view-mode doc-view-mode)))))))) + (jao-afio-goto-docs) + (pop-to-buffer b))) + +(provide 'jao-exwm-zathura) +;;; jao-exwm-zathura.el ends here |
