summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-pdf.el
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2022-09-24 19:15:37 +0100
committerjao <jao@gnu.org>2022-09-24 19:15:37 +0100
commit5d84b3cffdb611ddf63cbb44f8cc080ce039da53 (patch)
treea4586958eedb4ea58a3daddb460d453796835f48 /lib/doc/jao-pdf.el
parent4de01c9547049260435826984f316fcb1848a842 (diff)
downloadelibs-5d84b3cffdb611ddf63cbb44f8cc080ce039da53.tar.gz
elibs-5d84b3cffdb611ddf63cbb44f8cc080ce039da53.tar.bz2
fixes for doc view imenu
Diffstat (limited to 'lib/doc/jao-pdf.el')
-rw-r--r--lib/doc/jao-pdf.el46
1 files changed, 30 insertions, 16 deletions
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)))