summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--custom/jao-custom-exwm.el2
-rw-r--r--lib/doc/jao-doc-view.el21
-rw-r--r--lib/doc/jao-pdf.el46
3 files changed, 35 insertions, 34 deletions
diff --git a/custom/jao-custom-exwm.el b/custom/jao-custom-exwm.el
index 169fb26..a73355a 100644
--- a/custom/jao-custom-exwm.el
+++ b/custom/jao-custom-exwm.el
@@ -320,7 +320,7 @@
(defun jao-exwm--zathura-setup ()
(when (and (string= exwm-class-name "Zathura")
- (not jao-doc-view--imenu-file))
+ (not jao-pdf--outline))
(let ((info (jao-zathura--file-info (current-buffer))))
(jao-doc-session-mark (car info))
(jao-doc-session-save)
diff --git a/lib/doc/jao-doc-view.el b/lib/doc/jao-doc-view.el
index a55d477..cce158e 100644
--- a/lib/doc/jao-doc-view.el
+++ b/lib/doc/jao-doc-view.el
@@ -44,25 +44,12 @@
(defun jao-doc-view--enable-imenu (&optional file-name goto-page)
(setq-local imenu-create-index-function #'jao-doc-view--imenu-create-index
jao-doc-view--imenu-file (or file-name jao-doc-view--imenu-file)
- jao-doc-view--goer (or goto-page 'jao-doc-view-goto-page))
- (imenu-add-to-menubar "PDF outline"))
+ jao-doc-view--goer (or goto-page 'jao-doc-view-goto-page)))
(defun jao-doc-view--imenu-create-index ()
- (let (index)
- (dolist (item (or jao--pdf-outline
- (setq jao--pdf-outline
- (jao-pdf-outline jao-doc-view--imenu-file))))
- (let-alist item
- (let* ((lvl (make-string (max 0 (1- .level)) ?.))
- (title (format "%s%s (%s)" lvl .title .page)))
- (push `(,title 0 jao-doc-view--go ,item) index))))
- (nreverse index)))
-
-(defun jao-doc-view--go (&rest args)
- (when-let (item (car (last args)))
- (let-alist item (funcall jao-doc-view--goer .page))))
-
-(add-hook 'doc-view-mode-hook #'jao-doc-view--enable-imenu)
+ (jao-pdf-imenu-index #'jao-doc-view--goer #jao-doc-view--imenu-file))
+
+(add-hook 'doc-view-mode-hook #'jao-pdf-set-up-imenu)
;;; Page trailing
(defvar-local jao-doc-view--trail-back ())
diff --git a/lib/doc/jao-pdf.el b/lib/doc/jao-pdf.el
index 213d806..0cff06c 100644
--- a/lib/doc/jao-pdf.el
+++ b/lib/doc/jao-pdf.el
@@ -43,25 +43,39 @@
(concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))
(defvar jao-pdf--outline-rx
- ".+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)")
+ "[^\t]+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)")
-(defun jao-pdf-outline (file-name)
+(defun jao-pdf-outline (&optional file-name)
"Return an alist describing the given FILE-NAME (or current if nil).
The result is cached as a local buffer variable."
- (if (derived-mode-p 'pdf-view-mode)
- (pdf-info-outline)
- (let* ((outline nil)
- (fn (or file-name (buffer-file-name)))
- (fn (shell-quote-argument (expand-file-name fn))))
- (with-temp-buffer
- (insert (shell-command-to-string (format "mutool show %s outline" fn)))
- (goto-char (point-min))
- (while (re-search-forward jao-pdf--outline-rx nil t)
- (push `((level . ,(length (match-string 1)))
- (title . ,(match-string 2))
- (page . ,(string-to-number (match-string 3))))
- outline)))
- (nreverse outline))))
+ (let* ((outline nil)
+ (fn (or file-name (buffer-file-name)))
+ (fn (shell-quote-argument (expand-file-name fn))))
+ (with-temp-buffer
+ (insert (shell-command-to-string (format "mutool show %s outline" fn)))
+ (goto-char (point-min))
+ (while (re-search-forward jao-pdf--outline-rx nil t)
+ (push `((level . ,(length (match-string 1)))
+ (title . ,(match-string 2))
+ (page . ,(string-to-number (match-string 3))))
+ outline)))
+ (setq jao-pdf--outline (nreverse outline))))
+
+(defun jao-pdf-imenu-index (&optional goto-page-fn file-name)
+ "Create an imenu index using `jao-pdf-outline'."
+ (let* ((goto (or goto-page-fn 'doc-view-goto-page))
+ (act (lambda (_name _pos page) (funcall goto page)))
+ (index nil))
+ (dolist (item (or jao-pdf--outline (jao-pdf-outline file-name)))
+ (let-alist item
+ (let* ((lvl (make-string (max 0 (1- .level)) ?\ ))
+ (title (format "%s%s (%s)" "" .title .page)))
+ (push `(,title 0 ,act ,.page) index))))
+ (nreverse index)))
+
+(defun jao-pdf-set-up-imenu ()
+ (setq-local imenu-create-index-function #'jao-pdf-imenu-index)
+ (imenu-add-to-menubar "Outline"))
(defun jao-pdf-title (&optional fname)
(if (or fname (not (derived-mode-p 'doc-view-mode 'pdf-view-mode)))