blob: 9da0a256f6793c633784185fd9e4c52d2539c85c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
|