summaryrefslogtreecommitdiffhomepage
path: root/lib/doc/jao-doc-view.el
blob: d68cae9a05a4addc10340a756508831be7b7ee74 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
;;; jao-doc-view.el -- extensions for doc-view -*- lexical-binding: t; -*-

;; Copyright (c) 2013, 2015, 2017, 2018, 2019, 2021, 2022 Jose Antonio Ortega Ruiz

;; This file 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 file 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 <http://www.gnu.org/licenses/>.

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Fri Feb 15, 2013 01:21

(require 'doc-view)

;;; PDF info

(defvar-local jao--pdf-outline nil)

(defmacro jao-doc-view--pdf-call (a b &rest args)
  `(cond ((derived-mode-p 'pdf-view-mode) (,a ,@args))
         ((derived-mode-p 'doc-view-mode) (,b ,@args))))

(defun jao-doc-view-is-pdf (file) (string-match-p ".*\\.pdf$" file))

(defun jao-doc-view-title->file (title)
  (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))

(defun jao-doc-view-current-page ()
  (jao-doc-view--pdf-call pdf-view-current-page doc-view-current-page))

(defun jao-doc-view-goto-page (page &optional height)
  (when page
    (jao-doc-view--pdf-call pdf-view-goto-page doc-view-goto-page page))
  (when (and height (derived-mode-p 'pdf-view-mode))
    (image-set-window-vscroll
     (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height))))))

(defvar jao-doc-view--outline-rx
  ".+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)")

(defun jao-doc-view-pdf-outline (&optional file-name)
  (if (derived-mode-p 'pdf-view-mode)
      (pdf-info-outline)
    (let* ((outline nil)
           (fn (or file-name (buffer-file-name) jao-doc-view--imenu-file))
           (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-doc-view--outline-rx nil t)
          (push `((level . ,(length (match-string 1)))
                  (title . ,(match-string 2))
                  (page . ,(string-to-number (match-string 3))))
                outline)))
      (nreverse outline))))

(defun jao-doc-view-section-title (&optional page file-name)
  (when (not jao--pdf-outline)
    (setq-local jao--pdf-outline (jao-doc-view-pdf-outline file-name)))
  (let ((page (or page (jao-doc-view-current-page)))
        (outline jao--pdf-outline)
        (cur-page 0)
        (cur-title (jao-doc-view-title (or file-name buffer-file-name "title"))))
    (while (and (car outline) (< cur-page page))
      (setq cur-page (cdr (assoc 'page (car outline))))
      (when (<= cur-page page)
        (setq cur-title (cdr (assoc 'title (car outline)))))
      (setq outline (cdr outline)))
    (replace-regexp-in-string "[[:blank:]]+" " " cur-title)))

(defun jao-doc-view-title (&optional fname)
  (if (or fname (not (derived-mode-p 'doc-view-mode 'pdf-view-mode)))
      (let ((base (file-name-base (or fname (buffer-file-name)))))
        (capitalize (replace-regexp-in-string "-" " " base)))
    (or (jao-doc-view-section-title)
        (when buffer-file-name (jao-doc-view-title buffer-file-name)))))

;;; imenu
(defvar-local jao-doc-view--imenu-file nil)
(defvar-local jao-doc-view--goer 'jao-doc-view-goto-page)

(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"))

(defun jao-doc-view--imenu-create-index ()
  (let (index)
    (dolist (item (or jao--pdf-outline
                      (setq jao--pdf-outline
                            (jao-doc-view-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)

;;; Page trailing
(defvar-local jao-doc-view--trail-back ())
(defvar-local jao-doc-view--trail-fwd ())

(defun jao-doc-view--trail-push (dest-page)
  (when-let (page (jao-doc-view-current-page))
    (unless (eq (car jao-doc-view--trail-back) page)
      (push page jao-doc-view--trail-back))))

(defun jao-doc-view-back ()
  (interactive nil doc-view-mode)
  (if-let (p (pop jao-doc-view--trail-back))
      (progn (push (jao-doc-view-current-page) jao-doc-view--trail-fwd)
             (jao-doc-view-goto-page p))
    (message "No more back marks.")))

(defun jao-doc-view-forward ()
  (interactive nil doc-view-mode)
  (if-let (p (pop jao-doc-view--trail-fwd))
      (progn (push (jao-doc-view-current-page) jao-doc-view--trail-back)
             (jao-doc-view-goto-page p))
    (message "No more forward marks.")))

(advice-add 'doc-view-goto-page :before #'jao-doc-view--trail-push)

;;; Find URLs
(defun jao-doc-view--page-urls (&optional all)
  (if doc-view--current-converter-processes
      (message "DocView: please wait till conversion finished.")
    (let ((txt (expand-file-name "doc.txt" (doc-view--current-cache-dir)))
          (page (doc-view-current-page))
          (pd-rx "^")
          (urls))
      (if (file-readable-p txt)
          (with-current-buffer (find-file-noselect txt)
            (goto-char (point-min))
            (unless all (re-search-forward pd-rx nil t (1- page)))
            (let ((end (save-excursion
                         (if (and (not all) (re-search-forward pd-rx nil t))
                             (point)
                           (point-max)))))
              (while (re-search-forward "https?://" end t)
                (push (thing-at-point-url-at-point) urls))
              urls))
        (doc-view-doc->txt txt (lambda () (jao-doc-view--page-urls all)))
        'wait))))

(defun jao-doc-view-visit-url (all)
  "Visit URL displayed in this page."
  (interactive "P" doc-view-mode)
  (let ((urls (jao-doc-view--page-urls all)))
    (cond ((eq 'wait urls) (message "Extracting text, please wait and retry."))
          ((zerop (length urls)) (message "No URLs in this page"))
          (t (when-let (url (completing-read "URL: " urls nil nil
                                             (when (null (cdr urls)) (car urls))))
               (browse-url url))))))

;;; .
(provide 'jao-doc-view)