Commit | Line | Data |
---|---|---|
b38b1ec0 SM |
1 | ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs -*- lexical-binding: t -*- |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2007-2014 Free Software Foundation, Inc. |
94dbe99c | 4 | ;; |
db8a5a18 TH |
5 | ;; Author: Tassilo Horn <tsdh@gnu.org> |
6 | ;; Maintainer: Tassilo Horn <tsdh@gnu.org> | |
94dbe99c | 7 | ;; Keywords: files, pdf, ps, dvi |
94dbe99c TTN |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
94dbe99c | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
94dbe99c TTN |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
94dbe99c TTN |
23 | |
24 | ;;; Requirements: | |
25 | ||
4b378e75 | 26 | ;; doc-view.el requires GNU Emacs 22.1 or newer. You also need Ghostscript, |
1ecc9da7 TH |
27 | ;; `dvipdf' (comes with Ghostscript) or `dvipdfm' (comes with teTeX or TeXLive) |
28 | ;; and `pdftotext', which comes with xpdf (http://www.foolabs.com/xpdf/) or | |
29 | ;; poppler (http://poppler.freedesktop.org/). | |
94dbe99c TTN |
30 | |
31 | ;;; Commentary: | |
32 | ||
33 | ;; DocView is a document viewer for Emacs. It converts PDF, PS and DVI files | |
34 | ;; to a set of PNG files, one PNG for each page, and displays the PNG images | |
35 | ;; inside an Emacs buffer. This buffer uses `doc-view-mode' which provides | |
36 | ;; convenient key bindings for browsing the document. | |
37 | ;; | |
640602f7 | 38 | ;; To use it simply open a document file with |
94dbe99c | 39 | ;; |
640602f7 | 40 | ;; C-x C-f ~/path/to/document RET |
94dbe99c | 41 | ;; |
640602f7 RS |
42 | ;; and the document will be converted and displayed, if your emacs supports png |
43 | ;; images. With `C-c C-c' you can toggle between the rendered images | |
1f75e53d | 44 | ;; representation and the source text representation of the document. |
94dbe99c TTN |
45 | ;; |
46 | ;; Since conversion may take some time all the PNG images are cached in a | |
47 | ;; subdirectory of `doc-view-cache-directory' and reused when you want to view | |
640602f7 RS |
48 | ;; that file again. To reconvert a document hit `g' (`doc-view-reconvert-doc') |
49 | ;; when displaying the document. To delete all cached files use | |
94dbe99c TTN |
50 | ;; `doc-view-clear-cache'. To open the cache with dired, so that you can tidy |
51 | ;; it out use `doc-view-dired-cache'. | |
52 | ;; | |
53 | ;; When conversion in underway the first page will be displayed as soon as it | |
54 | ;; is available and the available pages are refreshed every | |
55 | ;; `doc-view-conversion-refresh-interval' seconds. If that variable is nil the | |
56 | ;; pages won't be displayed before conversion of the document finished | |
57 | ;; completely. | |
58 | ;; | |
db8a5a18 TH |
59 | ;; DocView lets you select a slice of the displayed pages. This slice |
60 | ;; will be remembered and applied to all pages of the current | |
61 | ;; document. This enables you to cut away the margins of a document | |
62 | ;; to save some space. To select a slice you can use | |
63 | ;; `doc-view-set-slice' (bound to `s s') which will query you for the | |
64 | ;; coordinates of the slice's top-left corner and its width and | |
65 | ;; height. A much more convenient way to do the same is offered by | |
66 | ;; the command `doc-view-set-slice-using-mouse' (bound to `s m'). | |
67 | ;; After invocation you only have to press mouse-1 at the top-left | |
68 | ;; corner and drag it to the bottom-right corner of the desired slice. | |
69 | ;; Even more accurate and convenient is to use | |
70 | ;; `doc-view-set-slice-from-bounding-box' (bound to `s b') which uses | |
71 | ;; the BoundingBox information of the current page to set an optimal | |
72 | ;; slice. To reset the slice use `doc-view-reset-slice' (bound to `s | |
73 | ;; r'). | |
94dbe99c | 74 | ;; |
94dbe99c TTN |
75 | ;; You can also search within the document. The command `doc-view-search' |
76 | ;; (bound to `C-s') queries for a search regexp and initializes a list of all | |
77 | ;; matching pages and messages how many match-pages were found. After that you | |
7baca0fa JL |
78 | ;; can jump to the next page containing a match with an additional `C-s'. With |
79 | ;; `C-r' you can do the same, but backwards. To search for a new regexp give a | |
80 | ;; prefix arg to one of the search functions, e.g. by typing `C-u C-s'. The | |
81 | ;; searching works by using a plain text representation of the document. If | |
a8ce3d17 | 82 | ;; that doesn't already exist the first invocation of `doc-view-search' (or |
7baca0fa JL |
83 | ;; `doc-view-search-backward') starts the conversion. When that finishes and |
84 | ;; you're still viewing the document (i.e. you didn't switch to another buffer) | |
85 | ;; you're queried for the regexp then. | |
640602f7 RS |
86 | ;; |
87 | ;; Dired users can simply hit `v' on a document file. If it's a PS, PDF or DVI | |
88 | ;; it will be opened using `doc-view-mode'. | |
89 | ;; | |
94dbe99c TTN |
90 | |
91 | ;;; Configuration: | |
92 | ||
640602f7 RS |
93 | ;; If the images are too small or too big you should set the "-rXXX" option in |
94 | ;; `doc-view-ghostscript-options' to another value. (The bigger your screen, | |
95 | ;; the higher the value.) | |
94dbe99c TTN |
96 | ;; |
97 | ;; This and all other options can be set with the customization interface. | |
98 | ;; Simply do | |
99 | ;; | |
100 | ;; M-x customize-group RET doc-view RET | |
101 | ;; | |
102 | ;; and modify them to your needs. | |
103 | ||
34065e5e | 104 | ;;; Todo: |
94dbe99c | 105 | |
56741510 SM |
106 | ;; - add print command. |
107 | ;; - share more code with image-mode. | |
f4c75497 | 108 | ;; - better menu. |
f4c75497 | 109 | ;; - Bind slicing to a drag event. |
cec1df02 | 110 | ;; - zoom the region around the cursor (like xdvi). |
c17587fe SM |
111 | ;; - get rid of the silly arrow in the fringe. |
112 | ;; - improve anti-aliasing (pdf-utils gets it better). | |
f4c75497 | 113 | |
34065e5e JL |
114 | ;;;; About isearch support |
115 | ||
116 | ;; I tried implementing isearch by setting | |
117 | ;; `isearch-search-fun-function' buffer-locally, but that didn't | |
118 | ;; work too good. The function doing the real search was called | |
119 | ;; endlessly somehow. But even if we'd get that working no real | |
120 | ;; isearch feeling comes up due to the missing match highlighting. | |
121 | ;; Currently I display all lines containing a match in a tooltip and | |
122 | ;; each C-s or C-r jumps directly to the next/previous page with a | |
123 | ;; match. With isearch we could only display the current match. So | |
124 | ;; we had to decide if another C-s jumps to the next page with a | |
125 | ;; match (thus only the first match in a page will be displayed in a | |
126 | ;; tooltip) or to the next match, which would do nothing visible | |
127 | ;; (except the tooltip) if the next match is on the same page. | |
128 | ||
129 | ;; And it's much slower than the current search facility, because | |
ee7683eb | 130 | ;; isearch really searches for each step forward or backward whereas |
34065e5e JL |
131 | ;; the current approach searches once and then it knows to which |
132 | ;; pages to jump. | |
133 | ||
134 | ;; Anyway, if someone with better isearch knowledge wants to give it a try, | |
135 | ;; feel free to do it. --Tassilo | |
136 | ||
137 | ;;; Code: | |
138 | ||
539a920c | 139 | (require 'cl-lib) |
94dbe99c | 140 | (require 'dired) |
414dd971 | 141 | (require 'image-mode) |
7baca0fa | 142 | (require 'jka-compr) |
94dbe99c TTN |
143 | |
144 | ;;;; Customization Options | |
145 | ||
146 | (defgroup doc-view nil | |
2b541f9a | 147 | "In-buffer viewer for PDF, PostScript, DVI, and DJVU files." |
94dbe99c TTN |
148 | :link '(function-link doc-view) |
149 | :version "22.2" | |
150 | :group 'applications | |
ff90f4b0 | 151 | :group 'data |
94dbe99c TTN |
152 | :group 'multimedia |
153 | :prefix "doc-view-") | |
154 | ||
d0af9f77 | 155 | (defcustom doc-view-ghostscript-program "gs" |
94dbe99c | 156 | "Program to convert PS and PDF files to PNG." |
c9a9a5e3 | 157 | :type 'file |
94dbe99c TTN |
158 | :group 'doc-view) |
159 | ||
bbc7ff25 SM |
160 | (defcustom doc-view-pdfdraw-program |
161 | (cond | |
162 | ((executable-find "pdfdraw") "pdfdraw") | |
163 | (t "mudraw")) | |
164 | "Name of MuPDF's program to convert PDF files to PNG." | |
85f17e35 EP |
165 | :type 'file |
166 | :version "24.4") | |
167 | ||
168 | (defcustom doc-view-pdf->png-converter-function | |
bbc7ff25 SM |
169 | (if (executable-find doc-view-pdfdraw-program) |
170 | #'doc-view-pdf->png-converter-mupdf | |
171 | #'doc-view-pdf->png-converter-ghostscript) | |
85f17e35 EP |
172 | "Function to call to convert a PDF file into a PNG file." |
173 | :type '(radio | |
174 | (function-item doc-view-pdf->png-converter-ghostscript | |
175 | :doc "Use ghostscript") | |
176 | (function-item doc-view-pdf->png-converter-mupdf | |
177 | :doc "Use mupdf") | |
178 | function) | |
179 | :version "24.4") | |
180 | ||
94dbe99c | 181 | (defcustom doc-view-ghostscript-options |
6a658a30 | 182 | '("-dSAFER" ;; Avoid security problems when rendering files from untrusted |
94d11cb5 | 183 | ;; sources. |
6a658a30 | 184 | "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4" |
05477667 | 185 | "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET") |
4b378e75 | 186 | "A list of options to give to ghostscript." |
c9a9a5e3 | 187 | :type '(repeat string) |
94dbe99c TTN |
188 | :group 'doc-view) |
189 | ||
05477667 SM |
190 | (defcustom doc-view-resolution 100 |
191 | "Dots per inch resolution used to render the documents. | |
192 | Higher values result in larger images." | |
9efa445f DN |
193 | :type 'number |
194 | :group 'doc-view) | |
05477667 | 195 | |
96dd18b1 SM |
196 | (defcustom doc-view-scale-internally t |
197 | "Whether we should try to rescale images ourselves. | |
198 | If nil, the document is re-rendered every time the scaling factor is modified. | |
199 | This only has an effect if the image libraries linked with Emacs support | |
200 | scaling." | |
bb098075 | 201 | :version "24.4" |
96dd18b1 SM |
202 | :type 'boolean) |
203 | ||
875c044a TH |
204 | (defcustom doc-view-image-width 850 |
205 | "Default image width. | |
96dd18b1 SM |
206 | Has only an effect if `doc-view-scale-internally' is non-nil and support for |
207 | scaling is compiled into emacs." | |
2bed3f04 | 208 | :version "24.1" |
875c044a TH |
209 | :type 'number |
210 | :group 'doc-view) | |
211 | ||
d0af9f77 | 212 | (defcustom doc-view-dvipdfm-program "dvipdfm" |
94dbe99c TTN |
213 | "Program to convert DVI files to PDF. |
214 | ||
215 | DVI file will be converted to PDF before the resulting PDF is | |
53d4c024 TH |
216 | converted to PNG. |
217 | ||
218 | If this and `doc-view-dvipdf-program' are set, | |
219 | `doc-view-dvipdf-program' will be preferred." | |
220 | :type 'file | |
221 | :group 'doc-view) | |
222 | ||
d0af9f77 | 223 | (defcustom doc-view-dvipdf-program "dvipdf" |
53d4c024 TH |
224 | "Program to convert DVI files to PDF. |
225 | ||
226 | DVI file will be converted to PDF before the resulting PDF is | |
227 | converted to PNG. | |
228 | ||
229 | If this and `doc-view-dvipdfm-program' are set, | |
230 | `doc-view-dvipdf-program' will be preferred." | |
c9a9a5e3 | 231 | :type 'file |
94dbe99c TTN |
232 | :group 'doc-view) |
233 | ||
f63f9398 TH |
234 | (define-obsolete-variable-alias 'doc-view-unoconv-program |
235 | 'doc-view-odf->pdf-converter-program | |
236 | "24.4") | |
237 | ||
238 | (defcustom doc-view-odf->pdf-converter-program | |
239 | (cond | |
240 | ((executable-find "soffice") "soffice") | |
241 | ((executable-find "unoconv") "unoconv") | |
242 | (t "soffice")) | |
95e16d17 TH |
243 | "Program to convert any file type readable by OpenOffice.org to PDF. |
244 | ||
245 | Needed for viewing OpenOffice.org (and MS Office) files." | |
f63f9398 | 246 | :version "24.4" |
95e16d17 TH |
247 | :type 'file |
248 | :group 'doc-view) | |
249 | ||
f63f9398 TH |
250 | (defcustom doc-view-odf->pdf-converter-function |
251 | (cond | |
252 | ((string-match "unoconv\\'" doc-view-odf->pdf-converter-program) | |
253 | #'doc-view-odf->pdf-converter-unoconv) | |
254 | ((string-match "soffice\\'" doc-view-odf->pdf-converter-program) | |
255 | #'doc-view-odf->pdf-converter-soffice)) | |
256 | "Function to call to convert a ODF file into a PDF file." | |
257 | :type '(radio | |
258 | (function-item doc-view-odf->pdf-converter-unoconv | |
259 | :doc "Use unoconv") | |
260 | (function-item doc-view-odf->pdf-converter-soffice | |
261 | :doc "Use LibreOffice") | |
262 | function) | |
263 | :version "24.4") | |
264 | ||
d0af9f77 | 265 | (defcustom doc-view-ps2pdf-program "ps2pdf" |
94dbe99c TTN |
266 | "Program to convert PS files to PDF. |
267 | ||
268 | PS files will be converted to PDF before searching is possible." | |
c9a9a5e3 | 269 | :type 'file |
94dbe99c TTN |
270 | :group 'doc-view) |
271 | ||
d0af9f77 | 272 | (defcustom doc-view-pdftotext-program "pdftotext" |
94dbe99c TTN |
273 | "Program to convert PDF files to plain text. |
274 | ||
275 | Needed for searching." | |
c9a9a5e3 | 276 | :type 'file |
94dbe99c TTN |
277 | :group 'doc-view) |
278 | ||
c17587fe | 279 | (defcustom doc-view-cache-directory |
8aafd651 | 280 | (expand-file-name (format "docview%d" (user-uid)) |
bce6be12 | 281 | temporary-file-directory) |
94dbe99c | 282 | "The base directory, where the PNG images will be saved." |
c9a9a5e3 | 283 | :type 'directory |
94dbe99c TTN |
284 | :group 'doc-view) |
285 | ||
56741510 SM |
286 | (defvar doc-view-conversion-buffer " *doc-view conversion output*" |
287 | "The buffer where messages from the converter programs go to.") | |
94dbe99c | 288 | |
56741510 | 289 | (defcustom doc-view-conversion-refresh-interval 1 |
1f75e53d GM |
290 | "Interval in seconds between refreshes of the DocView buffer while converting. |
291 | After such a refresh newly converted pages will be available for | |
94dbe99c TTN |
292 | viewing. If set to nil there won't be any refreshes and the |
293 | pages won't be displayed before conversion of the whole document | |
294 | has finished." | |
c9a9a5e3 | 295 | :type 'integer |
94dbe99c TTN |
296 | :group 'doc-view) |
297 | ||
0a745733 | 298 | (defcustom doc-view-continuous nil |
aefcadb6 JL |
299 | "In Continuous mode reaching the page edge advances to next/previous page. |
300 | When non-nil, scrolling a line upward at the bottom edge of the page | |
301 | moves to the next page, and scrolling a line downward at the top edge | |
302 | of the page moves to the previous page." | |
303 | :type 'boolean | |
304 | :group 'doc-view | |
305 | :version "23.2") | |
306 | ||
94dbe99c TTN |
307 | ;;;; Internal Variables |
308 | ||
416f1802 | 309 | (defvar-local doc-view--current-converter-processes nil |
f440830d | 310 | "Only used internally.") |
f440830d | 311 | |
de171465 | 312 | (defun doc-view-new-window-function (winprops) |
83600dc8 SM |
313 | ;; (message "New window %s for buf %s" (car winprops) (current-buffer)) |
314 | (cl-assert (or (eq t (car winprops)) | |
315 | (eq (window-buffer (car winprops)) (current-buffer)))) | |
de171465 SM |
316 | (let ((ol (image-mode-window-get 'overlay winprops))) |
317 | (if ol | |
0bca393f | 318 | (progn |
83600dc8 SM |
319 | (setq ol (copy-overlay ol)) |
320 | ;; `ol' might actually be dead. | |
321 | (move-overlay ol (point-min) (point-max))) | |
de171465 SM |
322 | (setq ol (make-overlay (point-min) (point-max) nil t)) |
323 | (overlay-put ol 'doc-view t)) | |
324 | (overlay-put ol 'window (car winprops)) | |
83600dc8 SM |
325 | (unless (windowp (car winprops)) |
326 | ;; It's a pseudo entry. Let's make sure it's not displayed (the | |
327 | ;; `window' property is only effective if its value is a window). | |
328 | (cl-assert (eq t (car winprops))) | |
329 | (delete-overlay ol)) | |
f35ffe5e | 330 | (image-mode-window-put 'overlay ol winprops) |
50105835 SM |
331 | (when (and (windowp (car winprops)) |
332 | (stringp (overlay-get ol 'display)) | |
416f1802 | 333 | (null doc-view--current-converter-processes)) |
50105835 SM |
334 | ;; We're not displaying an image yet, so let's do so. This happens when |
335 | ;; the buffer is displayed for the first time. | |
336 | ;; Don't do it if there's a conversion is running, since in that case, it | |
337 | ;; will be done later. | |
338 | (with-selected-window (car winprops) | |
e51c8d91 | 339 | (doc-view-goto-page (image-mode-window-get 'page t)))))) |
94dbe99c | 340 | |
416f1802 | 341 | (defvar-local doc-view--current-files nil |
94dbe99c TTN |
342 | "Only used internally.") |
343 | ||
416f1802 | 344 | (defvar-local doc-view--current-timer nil |
94dbe99c TTN |
345 | "Only used internally.") |
346 | ||
416f1802 | 347 | (defvar-local doc-view--current-cache-dir nil |
94dbe99c TTN |
348 | "Only used internally.") |
349 | ||
416f1802 | 350 | (defvar-local doc-view--current-search-matches nil |
94dbe99c TTN |
351 | "Only used internally.") |
352 | ||
416f1802 | 353 | (defvar doc-view--pending-cache-flush nil |
d99abf1b | 354 | "Only used internally.") |
94dbe99c | 355 | |
416f1802 | 356 | (defvar doc-view--previous-major-mode nil |
640602f7 RS |
357 | "Only used internally.") |
358 | ||
416f1802 | 359 | (defvar doc-view--buffer-file-name nil |
39a402e3 TH |
360 | "Only used internally. |
361 | The file name used for conversion. Normally it's the same as | |
362 | `buffer-file-name', but for remote files, compressed files and | |
363 | files inside an archive it is a temporary copy of | |
364 | the (uncompressed, extracted) file residing in | |
365 | `doc-view-cache-directory'.") | |
366 | ||
eb79098b SM |
367 | (defvar doc-view-doc-type nil |
368 | "The type of document in the current buffer. | |
369 | Can be `dvi', `pdf', or `ps'.") | |
370 | ||
2b541f9a EP |
371 | (defvar doc-view-single-page-converter-function nil |
372 | "Function to call to convert a single page of the document to a bitmap file. | |
373 | May operate on the source document or on some intermediate (typically PDF) | |
374 | conversion of it.") | |
375 | ||
bbc7ff25 | 376 | (defvar-local doc-view--image-type nil |
2b541f9a EP |
377 | "The type of image in the current buffer. |
378 | Can be `png' or `tiff'.") | |
379 | ||
bbc7ff25 SM |
380 | (defvar-local doc-view--image-file-pattern nil |
381 | "The `format' pattern of image file names. | |
382 | Typically \"page-%s.png\".") | |
2b541f9a | 383 | |
640602f7 | 384 | ;;;; DocView Keymaps |
94dbe99c TTN |
385 | |
386 | (defvar doc-view-mode-map | |
387 | (let ((map (make-sparse-keymap))) | |
d2d7e96c | 388 | (set-keymap-parent map image-mode-map) |
94dbe99c TTN |
389 | ;; Navigation in the document |
390 | (define-key map (kbd "n") 'doc-view-next-page) | |
391 | (define-key map (kbd "p") 'doc-view-previous-page) | |
eb8d0216 SM |
392 | (define-key map (kbd "<next>") 'forward-page) |
393 | (define-key map (kbd "<prior>") 'backward-page) | |
394 | (define-key map [remap forward-page] 'doc-view-next-page) | |
395 | (define-key map [remap backward-page] 'doc-view-previous-page) | |
94dbe99c | 396 | (define-key map (kbd "SPC") 'doc-view-scroll-up-or-next-page) |
9cec74cf | 397 | (define-key map (kbd "S-SPC") 'doc-view-scroll-down-or-previous-page) |
94dbe99c | 398 | (define-key map (kbd "DEL") 'doc-view-scroll-down-or-previous-page) |
aefcadb6 JL |
399 | (define-key map (kbd "C-n") 'doc-view-next-line-or-next-page) |
400 | (define-key map (kbd "<down>") 'doc-view-next-line-or-next-page) | |
401 | (define-key map (kbd "C-p") 'doc-view-previous-line-or-previous-page) | |
402 | (define-key map (kbd "<up>") 'doc-view-previous-line-or-previous-page) | |
94dbe99c TTN |
403 | (define-key map (kbd "M-<") 'doc-view-first-page) |
404 | (define-key map (kbd "M->") 'doc-view-last-page) | |
7656fe61 | 405 | (define-key map [remap goto-line] 'doc-view-goto-page) |
4376876e | 406 | (define-key map (kbd "RET") 'image-next-line) |
05477667 SM |
407 | ;; Zoom in/out. |
408 | (define-key map "+" 'doc-view-enlarge) | |
0550c95a | 409 | (define-key map "=" 'doc-view-enlarge) |
05477667 | 410 | (define-key map "-" 'doc-view-shrink) |
529fb53f TH |
411 | (define-key map "0" 'doc-view-scale-reset) |
412 | (define-key map [remap text-scale-adjust] 'doc-view-scale-adjust) | |
d7b89879 TH |
413 | ;; Fit the image to the window |
414 | (define-key map "W" 'doc-view-fit-width-to-window) | |
415 | (define-key map "H" 'doc-view-fit-height-to-window) | |
416 | (define-key map "P" 'doc-view-fit-page-to-window) | |
d2d7e96c | 417 | ;; Killing the buffer (and the process) |
94dbe99c | 418 | (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer) |
937cb3fb | 419 | (define-key map (kbd "K") 'doc-view-kill-proc) |
94dbe99c TTN |
420 | ;; Slicing the image |
421 | (define-key map (kbd "s s") 'doc-view-set-slice) | |
422 | (define-key map (kbd "s m") 'doc-view-set-slice-using-mouse) | |
db8a5a18 | 423 | (define-key map (kbd "s b") 'doc-view-set-slice-from-bounding-box) |
94dbe99c TTN |
424 | (define-key map (kbd "s r") 'doc-view-reset-slice) |
425 | ;; Searching | |
426 | (define-key map (kbd "C-s") 'doc-view-search) | |
427 | (define-key map (kbd "<find>") 'doc-view-search) | |
7baca0fa | 428 | (define-key map (kbd "C-r") 'doc-view-search-backward) |
94dbe99c TTN |
429 | ;; Show the tooltip |
430 | (define-key map (kbd "C-t") 'doc-view-show-tooltip) | |
640602f7 RS |
431 | ;; Toggle between text and image display or editing |
432 | (define-key map (kbd "C-c C-c") 'doc-view-toggle-display) | |
515357c2 TH |
433 | ;; Open a new buffer with doc's text contents |
434 | (define-key map (kbd "C-c C-t") 'doc-view-open-text) | |
0bca393f SM |
435 | ;; Reconvert the current document. Don't just use revert-buffer |
436 | ;; because that resets the scale factor, the page number, ... | |
437 | (define-key map (kbd "g") 'doc-view-revert-buffer) | |
438 | (define-key map (kbd "r") 'doc-view-revert-buffer) | |
94dbe99c | 439 | map) |
640602f7 RS |
440 | "Keymap used by `doc-view-mode' when displaying a doc as a set of images.") |
441 | ||
0bca393f SM |
442 | (defun doc-view-revert-buffer (&optional ignore-auto noconfirm) |
443 | "Like `revert-buffer', but preserves the buffer's current modes." | |
444 | ;; FIXME: this should probably be moved to files.el and used for | |
445 | ;; most/all "g" bindings to revert-buffer. | |
446 | (interactive (list (not current-prefix-arg))) | |
447 | (revert-buffer ignore-auto noconfirm 'preserve-modes)) | |
448 | ||
449 | ||
f4c75497 SM |
450 | (easy-menu-define doc-view-menu doc-view-mode-map |
451 | "Menu for Doc View mode." | |
452 | '("DocView" | |
0a745733 JL |
453 | ["Toggle display" doc-view-toggle-display] |
454 | ("Continuous" | |
455 | ["Off" (setq doc-view-continuous nil) | |
456 | :style radio :selected (eq doc-view-continuous nil)] | |
457 | ["On" (setq doc-view-continuous t) | |
458 | :style radio :selected (eq doc-view-continuous t)] | |
459 | "---" | |
460 | ["Save as Default" | |
461 | (customize-save-variable 'doc-view-continuous doc-view-continuous) t] | |
462 | ) | |
463 | "---" | |
f4c75497 | 464 | ["Set Slice" doc-view-set-slice-using-mouse] |
db8a5a18 | 465 | ["Set Slice (BoundingBox)" doc-view-set-slice-from-bounding-box] |
f4c75497 SM |
466 | ["Set Slice (manual)" doc-view-set-slice] |
467 | ["Reset Slice" doc-view-reset-slice] | |
468 | "---" | |
469 | ["Search" doc-view-search] | |
7baca0fa | 470 | ["Search Backwards" doc-view-search-backward] |
f4c75497 SM |
471 | )) |
472 | ||
937cb3fb | 473 | (defvar doc-view-minor-mode-map |
640602f7 RS |
474 | (let ((map (make-sparse-keymap))) |
475 | ;; Toggle between text and image display or editing | |
476 | (define-key map (kbd "C-c C-c") 'doc-view-toggle-display) | |
640602f7 | 477 | map) |
937cb3fb | 478 | "Keymap used by `doc-minor-view-mode'.") |
94dbe99c TTN |
479 | |
480 | ;;;; Navigation Commands | |
481 | ||
160dfe43 SM |
482 | (defmacro doc-view-current-page (&optional win) |
483 | `(image-mode-window-get 'page ,win)) | |
de171465 SM |
484 | (defmacro doc-view-current-info () `(image-mode-window-get 'info)) |
485 | (defmacro doc-view-current-overlay () `(image-mode-window-get 'overlay)) | |
486 | (defmacro doc-view-current-image () `(image-mode-window-get 'image)) | |
487 | (defmacro doc-view-current-slice () `(image-mode-window-get 'slice)) | |
488 | ||
bdd42899 | 489 | (defun doc-view-last-page-number () |
416f1802 | 490 | (length doc-view--current-files)) |
bdd42899 | 491 | |
94dbe99c TTN |
492 | (defun doc-view-goto-page (page) |
493 | "View the page given by PAGE." | |
494 | (interactive "nPage: ") | |
e7a1c32d | 495 | (let ((len (doc-view-last-page-number))) |
94dbe99c | 496 | (if (< page 1) |
1ca678aa | 497 | (setq page 1) |
56741510 SM |
498 | (when (and (> page len) |
499 | ;; As long as the converter is running, we don't know | |
500 | ;; how many pages will be available. | |
416f1802 | 501 | (null doc-view--current-converter-processes)) |
1ca678aa | 502 | (setq page len))) |
655ab9a3 | 503 | (force-mode-line-update) ;To update `current-page'. |
de171465 SM |
504 | (setf (doc-view-current-page) page |
505 | (doc-view-current-info) | |
1ca678aa MC |
506 | (concat |
507 | (propertize | |
de171465 | 508 | (format "Page %d of %d." page len) 'face 'bold) |
1ca678aa | 509 | ;; Tell user if converting isn't finished yet |
416f1802 | 510 | (if doc-view--current-converter-processes |
1ca678aa MC |
511 | " (still converting...)\n" |
512 | "\n") | |
513 | ;; Display context infos if this page matches the last search | |
416f1802 SM |
514 | (when (and doc-view--current-search-matches |
515 | (assq page doc-view--current-search-matches)) | |
1ca678aa MC |
516 | (concat (propertize "Search matches:\n" 'face 'bold) |
517 | (let ((contexts "")) | |
de171465 | 518 | (dolist (m (cdr (assq page |
416f1802 | 519 | doc-view--current-search-matches))) |
1ca678aa MC |
520 | (setq contexts (concat contexts " - \"" m "\"\n"))) |
521 | contexts))))) | |
94dbe99c | 522 | ;; Update the buffer |
416f1802 | 523 | ;; We used to find the file name from doc-view--current-files but |
56741510 | 524 | ;; that's not right if the pages are not generated sequentially |
416f1802 | 525 | ;; or if the page isn't in doc-view--current-files yet. |
2b541f9a | 526 | (let ((file (expand-file-name |
bbc7ff25 | 527 | (format doc-view--image-file-pattern page) |
416f1802 | 528 | (doc-view--current-cache-dir)))) |
ec4853ab SM |
529 | (doc-view-insert-image file :pointer 'arrow) |
530 | (when (and (not (file-exists-p file)) | |
416f1802 | 531 | doc-view--current-converter-processes) |
ec4853ab | 532 | ;; The PNG file hasn't been generated yet. |
2b541f9a | 533 | (funcall doc-view-single-page-converter-function |
416f1802 | 534 | doc-view--buffer-file-name file page |
2b541f9a EP |
535 | (let ((win (selected-window))) |
536 | (lambda () | |
537 | (and (eq (current-buffer) (window-buffer win)) | |
538 | ;; If we changed page in the mean | |
539 | ;; time, don't mess things up. | |
540 | (eq (doc-view-current-page win) page) | |
541 | ;; Make sure we don't infloop. | |
542 | (file-readable-p file) | |
543 | (with-selected-window win | |
544 | (doc-view-goto-page page)))))))) | |
de171465 | 545 | (overlay-put (doc-view-current-overlay) |
f35ffe5e | 546 | 'help-echo (doc-view-current-info)))) |
94dbe99c TTN |
547 | |
548 | (defun doc-view-next-page (&optional arg) | |
549 | "Browse ARG pages forward." | |
550 | (interactive "p") | |
de171465 | 551 | (doc-view-goto-page (+ (doc-view-current-page) (or arg 1)))) |
94dbe99c TTN |
552 | |
553 | (defun doc-view-previous-page (&optional arg) | |
554 | "Browse ARG pages backward." | |
555 | (interactive "p") | |
de171465 | 556 | (doc-view-goto-page (- (doc-view-current-page) (or arg 1)))) |
94dbe99c TTN |
557 | |
558 | (defun doc-view-first-page () | |
559 | "View the first page." | |
560 | (interactive) | |
561 | (doc-view-goto-page 1)) | |
562 | ||
563 | (defun doc-view-last-page () | |
564 | "View the last page." | |
565 | (interactive) | |
bdd42899 | 566 | (doc-view-goto-page (doc-view-last-page-number))) |
94dbe99c | 567 | |
7d6b4d3c JL |
568 | (defun doc-view-scroll-up-or-next-page (&optional arg) |
569 | "Scroll page up ARG lines if possible, else goto next page. | |
0a745733 | 570 | When `doc-view-continuous' is non-nil, scrolling upward |
7d6b4d3c JL |
571 | at the bottom edge of the page moves to the next page. |
572 | Otherwise, goto next page only on typing SPC (ARG is nil)." | |
573 | (interactive "P") | |
0a745733 | 574 | (if (or doc-view-continuous (null arg)) |
7d6b4d3c JL |
575 | (let ((hscroll (window-hscroll)) |
576 | (cur-page (doc-view-current-page))) | |
577 | (when (= (window-vscroll) (image-scroll-up arg)) | |
578 | (doc-view-next-page) | |
579 | (when (/= cur-page (doc-view-current-page)) | |
580 | (image-bob) | |
581 | (image-bol 1)) | |
582 | (set-window-hscroll (selected-window) hscroll))) | |
583 | (image-scroll-up arg))) | |
584 | ||
585 | (defun doc-view-scroll-down-or-previous-page (&optional arg) | |
586 | "Scroll page down ARG lines if possible, else goto previous page. | |
0a745733 | 587 | When `doc-view-continuous' is non-nil, scrolling downward |
7d6b4d3c JL |
588 | at the top edge of the page moves to the previous page. |
589 | Otherwise, goto previous page only on typing DEL (ARG is nil)." | |
590 | (interactive "P") | |
0a745733 | 591 | (if (or doc-view-continuous (null arg)) |
7d6b4d3c JL |
592 | (let ((hscroll (window-hscroll)) |
593 | (cur-page (doc-view-current-page))) | |
594 | (when (= (window-vscroll) (image-scroll-down arg)) | |
595 | (doc-view-previous-page) | |
596 | (when (/= cur-page (doc-view-current-page)) | |
597 | (image-eob) | |
598 | (image-bol 1)) | |
599 | (set-window-hscroll (selected-window) hscroll))) | |
600 | (image-scroll-down arg))) | |
601 | ||
602 | (defun doc-view-next-line-or-next-page (&optional arg) | |
603 | "Scroll upward by ARG lines if possible, else goto next page. | |
0a745733 | 604 | When `doc-view-continuous' is non-nil, scrolling a line upward |
7d6b4d3c | 605 | at the bottom edge of the page moves to the next page." |
aefcadb6 | 606 | (interactive "p") |
0a745733 | 607 | (if doc-view-continuous |
aefcadb6 JL |
608 | (let ((hscroll (window-hscroll)) |
609 | (cur-page (doc-view-current-page))) | |
7d6b4d3c | 610 | (when (= (window-vscroll) (image-next-line arg)) |
aefcadb6 JL |
611 | (doc-view-next-page) |
612 | (when (/= cur-page (doc-view-current-page)) | |
613 | (image-bob) | |
614 | (image-bol 1)) | |
615 | (set-window-hscroll (selected-window) hscroll))) | |
616 | (image-next-line 1))) | |
617 | ||
7d6b4d3c JL |
618 | (defun doc-view-previous-line-or-previous-page (&optional arg) |
619 | "Scroll downward by ARG lines if possible, else goto previous page. | |
0a745733 | 620 | When `doc-view-continuous' is non-nil, scrolling a line downward |
aefcadb6 JL |
621 | at the top edge of the page moves to the previous page." |
622 | (interactive "p") | |
0a745733 | 623 | (if doc-view-continuous |
aefcadb6 JL |
624 | (let ((hscroll (window-hscroll)) |
625 | (cur-page (doc-view-current-page))) | |
7d6b4d3c | 626 | (when (= (window-vscroll) (image-previous-line arg)) |
aefcadb6 JL |
627 | (doc-view-previous-page) |
628 | (when (/= cur-page (doc-view-current-page)) | |
629 | (image-eob) | |
630 | (image-bol 1)) | |
631 | (set-window-hscroll (selected-window) hscroll))) | |
7d6b4d3c | 632 | (image-previous-line arg))) |
aefcadb6 | 633 | |
937cb3fb GM |
634 | ;;;; Utility Functions |
635 | ||
640602f7 | 636 | (defun doc-view-kill-proc () |
ec4853ab | 637 | "Kill the current converter process(es)." |
640602f7 | 638 | (interactive) |
416f1802 | 639 | (while (consp doc-view--current-converter-processes) |
83600dc8 SM |
640 | (ignore-errors ;; Some entries might not be processes, and maybe |
641 | ;; some are dead already? | |
416f1802 SM |
642 | (kill-process (pop doc-view--current-converter-processes)))) |
643 | (when doc-view--current-timer | |
644 | (cancel-timer doc-view--current-timer) | |
645 | (setq doc-view--current-timer nil)) | |
640602f7 RS |
646 | (setq mode-line-process nil)) |
647 | ||
94dbe99c TTN |
648 | (defun doc-view-kill-proc-and-buffer () |
649 | "Kill the current converter process and buffer." | |
650 | (interactive) | |
640602f7 | 651 | (doc-view-kill-proc) |
94dbe99c | 652 | (when (eq major-mode 'doc-view-mode) |
94dbe99c TTN |
653 | (kill-buffer (current-buffer)))) |
654 | ||
c17587fe SM |
655 | (defun doc-view-make-safe-dir (dir) |
656 | (condition-case nil | |
d63d883a GM |
657 | ;; Create temp files with strict access rights. It's easy to |
658 | ;; loosen them later, whereas it's impossible to close the | |
659 | ;; time-window of loose permissions otherwise. | |
660 | (with-file-modes #o0700 (make-directory dir)) | |
c17587fe | 661 | (file-already-exists |
c82f64de LMI |
662 | (when (file-symlink-p dir) |
663 | (error "Danger: %s points to a symbolic link" dir)) | |
c17587fe SM |
664 | ;; In case it was created earlier with looser rights. |
665 | ;; We could check the mode info returned by file-attributes, but it's | |
666 | ;; a pain to parse and it may not tell you what we want under | |
667 | ;; non-standard file-systems. So let's just say what we want and let | |
668 | ;; the underlying C code and file-system figure it out. | |
669 | ;; This also ends up checking a bunch of useful conditions: it makes | |
670 | ;; sure we have write-access to the directory and that we own it, thus | |
671 | ;; closing a bunch of security holes. | |
c82f64de LMI |
672 | (condition-case error |
673 | (set-file-modes dir #o0700) | |
674 | (file-error | |
675 | (error | |
676 | (format "Unable to use temporary directory %s: %s" | |
677 | dir (mapconcat 'identity (cdr error) " ")))))))) | |
c17587fe | 678 | |
416f1802 | 679 | (defun doc-view--current-cache-dir () |
937cb3fb GM |
680 | "Return the directory where the png files of the current doc should be saved. |
681 | It's a subdirectory of `doc-view-cache-directory'." | |
416f1802 SM |
682 | (if doc-view--current-cache-dir |
683 | doc-view--current-cache-dir | |
c17587fe SM |
684 | ;; Try and make sure doc-view-cache-directory exists and is safe. |
685 | (doc-view-make-safe-dir doc-view-cache-directory) | |
686 | ;; Now compute the subdirectory to use. | |
416f1802 | 687 | (setq doc-view--current-cache-dir |
937cb3fb | 688 | (file-name-as-directory |
c17587fe | 689 | (expand-file-name |
4fa60c54 | 690 | (concat (subst-char-in-string ?% ?_ ;; bug#13679 |
416f1802 | 691 | (file-name-nondirectory doc-view--buffer-file-name)) |
39a402e3 | 692 | "-" |
416f1802 | 693 | (let ((file doc-view--buffer-file-name)) |
39a402e3 | 694 | (with-temp-buffer |
86903c81 | 695 | (set-buffer-multibyte nil) |
39a402e3 TH |
696 | (insert-file-contents-literally file) |
697 | (md5 (current-buffer))))) | |
c17587fe | 698 | doc-view-cache-directory))))) |
937cb3fb | 699 | |
789ab9d4 RS |
700 | ;;;###autoload |
701 | (defun doc-view-mode-p (type) | |
95e16d17 TH |
702 | "Return non-nil if document type TYPE is available for `doc-view'. |
703 | Document types are symbols like `dvi', `ps', `pdf', or `odf' (any | |
704 | OpenDocument format)." | |
789ab9d4 | 705 | (and (display-graphic-p) |
c44d54b3 TH |
706 | (or (image-type-available-p 'imagemagick) |
707 | (image-type-available-p 'png)) | |
789ab9d4 RS |
708 | (cond |
709 | ((eq type 'dvi) | |
710 | (and (doc-view-mode-p 'pdf) | |
53d4c024 TH |
711 | (or (and doc-view-dvipdf-program |
712 | (executable-find doc-view-dvipdf-program)) | |
713 | (and doc-view-dvipdfm-program | |
714 | (executable-find doc-view-dvipdfm-program))))) | |
bbc7ff25 | 715 | ((memq type '(postscript ps eps pdf)) |
85f17e35 | 716 | ;; FIXME: allow mupdf here |
789ab9d4 RS |
717 | (and doc-view-ghostscript-program |
718 | (executable-find doc-view-ghostscript-program))) | |
95e16d17 | 719 | ((eq type 'odf) |
f63f9398 TH |
720 | (and doc-view-odf->pdf-converter-program |
721 | (executable-find doc-view-odf->pdf-converter-program) | |
95e16d17 | 722 | (doc-view-mode-p 'pdf))) |
2b541f9a EP |
723 | ((eq type 'djvu) |
724 | (executable-find "ddjvu")) | |
789ab9d4 RS |
725 | (t ;; unknown image type |
726 | nil)))) | |
727 | ||
94dbe99c TTN |
728 | ;;;; Conversion Functions |
729 | ||
05477667 SM |
730 | (defvar doc-view-shrink-factor 1.125) |
731 | ||
732 | (defun doc-view-enlarge (factor) | |
83600dc8 | 733 | "Enlarge the document by FACTOR." |
05477667 | 734 | (interactive (list doc-view-shrink-factor)) |
96dd18b1 SM |
735 | (if (and doc-view-scale-internally |
736 | (eq (plist-get (cdr (doc-view-current-image)) :type) | |
737 | 'imagemagick)) | |
83600dc8 SM |
738 | ;; ImageMagick supports on-the-fly-rescaling. |
739 | (let ((new (ceiling (* factor doc-view-image-width)))) | |
740 | (unless (equal new doc-view-image-width) | |
bbc7ff25 | 741 | (setq-local doc-view-image-width new) |
83600dc8 SM |
742 | (doc-view-insert-image |
743 | (plist-get (cdr (doc-view-current-image)) :file) | |
744 | :width doc-view-image-width))) | |
745 | (let ((new (ceiling (* factor doc-view-resolution)))) | |
746 | (unless (equal new doc-view-resolution) | |
bbc7ff25 | 747 | (setq-local doc-view-resolution new) |
83600dc8 | 748 | (doc-view-reconvert-doc))))) |
05477667 SM |
749 | |
750 | (defun doc-view-shrink (factor) | |
751 | "Shrink the document." | |
752 | (interactive (list doc-view-shrink-factor)) | |
753 | (doc-view-enlarge (/ 1.0 factor))) | |
754 | ||
529fb53f | 755 | (defun doc-view-scale-reset () |
0550c95a TH |
756 | "Reset the document size/zoom level to the initial one." |
757 | (interactive) | |
758 | (if (and doc-view-scale-internally | |
759 | (eq (plist-get (cdr (doc-view-current-image)) :type) | |
760 | 'imagemagick)) | |
761 | (progn | |
762 | (kill-local-variable 'doc-view-image-width) | |
763 | (doc-view-insert-image | |
764 | (plist-get (cdr (doc-view-current-image)) :file) | |
765 | :width doc-view-image-width)) | |
766 | (kill-local-variable 'doc-view-resolution) | |
767 | (doc-view-reconvert-doc))) | |
768 | ||
529fb53f TH |
769 | (defun doc-view-scale-adjust (factor) |
770 | "Adjust the scale of the DocView page images by FACTOR. | |
771 | FACTOR defaults to `doc-view-shrink-factor'. | |
772 | ||
773 | The actual adjustment made depends on the final component of the | |
774 | key-binding used to invoke the command, with all modifiers removed: | |
775 | ||
776 | +, = Increase the image scale by FACTOR | |
777 | - Decrease the image scale by FACTOR | |
778 | 0 Reset the image scale to the initial scale" | |
779 | (interactive (list doc-view-shrink-factor)) | |
780 | (let ((ev last-command-event) | |
781 | (echo-keystrokes nil)) | |
782 | (pcase (event-basic-type ev) | |
783 | ((or ?+ ?=) (doc-view-enlarge factor)) | |
784 | (?- (doc-view-shrink factor)) | |
785 | (?0 (doc-view-scale-reset))))) | |
786 | ||
d7b89879 TH |
787 | (defun doc-view-fit-width-to-window () |
788 | "Fit the image width to the window width." | |
789 | (interactive) | |
790 | (let ((win-width (- (nth 2 (window-inside-pixel-edges)) | |
791 | (nth 0 (window-inside-pixel-edges)))) | |
792 | (slice (doc-view-current-slice))) | |
793 | (if (not slice) | |
794 | (let ((img-width (car (image-display-size | |
795 | (image-get-display-property) t)))) | |
796 | (doc-view-enlarge (/ (float win-width) (float img-width)))) | |
797 | ||
798 | ;; If slice is set | |
799 | (let* ((slice-width (nth 2 slice)) | |
800 | (scale-factor (/ (float win-width) (float slice-width))) | |
801 | (new-slice (mapcar (lambda (x) (ceiling (* scale-factor x))) slice))) | |
802 | ||
803 | (doc-view-enlarge scale-factor) | |
804 | (setf (doc-view-current-slice) new-slice) | |
805 | (doc-view-goto-page (doc-view-current-page)))))) | |
806 | ||
807 | (defun doc-view-fit-height-to-window () | |
808 | "Fit the image height to the window height." | |
809 | (interactive) | |
810 | (let ((win-height (- (nth 3 (window-inside-pixel-edges)) | |
811 | (nth 1 (window-inside-pixel-edges)))) | |
812 | (slice (doc-view-current-slice))) | |
813 | (if (not slice) | |
814 | (let ((img-height (cdr (image-display-size | |
815 | (image-get-display-property) t)))) | |
816 | ;; When users call 'doc-view-fit-height-to-window', | |
817 | ;; they might want to go to next page by typing SPC | |
818 | ;; ONLY once. So I used '(- win-height 1)' instead of | |
819 | ;; 'win-height' | |
820 | (doc-view-enlarge (/ (float (- win-height 1)) (float img-height)))) | |
821 | ||
822 | ;; If slice is set | |
823 | (let* ((slice-height (nth 3 slice)) | |
824 | (scale-factor (/ (float (- win-height 1)) (float slice-height))) | |
825 | (new-slice (mapcar (lambda (x) (ceiling (* scale-factor x))) slice))) | |
826 | ||
827 | (doc-view-enlarge scale-factor) | |
828 | (setf (doc-view-current-slice) new-slice) | |
829 | (doc-view-goto-page (doc-view-current-page)))))) | |
830 | ||
831 | (defun doc-view-fit-page-to-window () | |
832 | "Fit the image to the window. | |
833 | More specifically, this function enlarges image by: | |
834 | ||
835 | min {(window-width / image-width), (window-height / image-height)} times." | |
836 | (interactive) | |
837 | (let ((win-width (- (nth 2 (window-inside-pixel-edges)) | |
838 | (nth 0 (window-inside-pixel-edges)))) | |
839 | (win-height (- (nth 3 (window-inside-pixel-edges)) | |
840 | (nth 1 (window-inside-pixel-edges)))) | |
841 | (slice (doc-view-current-slice))) | |
842 | (if (not slice) | |
843 | (let ((img-width (car (image-display-size | |
844 | (image-get-display-property) t))) | |
845 | (img-height (cdr (image-display-size | |
846 | (image-get-display-property) t)))) | |
847 | (doc-view-enlarge (min (/ (float win-width) (float img-width)) | |
83600dc8 SM |
848 | (/ (float (- win-height 1)) |
849 | (float img-height))))) | |
d7b89879 TH |
850 | ;; If slice is set |
851 | (let* ((slice-width (nth 2 slice)) | |
852 | (slice-height (nth 3 slice)) | |
853 | (scale-factor (min (/ (float win-width) (float slice-width)) | |
83600dc8 SM |
854 | (/ (float (- win-height 1)) |
855 | (float slice-height)))) | |
d7b89879 TH |
856 | (new-slice (mapcar (lambda (x) (ceiling (* scale-factor x))) slice))) |
857 | (doc-view-enlarge scale-factor) | |
858 | (setf (doc-view-current-slice) new-slice) | |
859 | (doc-view-goto-page (doc-view-current-page)))))) | |
860 | ||
c17587fe | 861 | (defun doc-view-reconvert-doc () |
640602f7 RS |
862 | "Reconvert the current document. |
863 | Should be invoked when the cached images aren't up-to-date." | |
864 | (interactive) | |
f4c75497 SM |
865 | (doc-view-kill-proc) |
866 | ;; Clear the old cached files | |
416f1802 SM |
867 | (when (file-exists-p (doc-view--current-cache-dir)) |
868 | (delete-directory (doc-view--current-cache-dir) 'recursive)) | |
83600dc8 | 869 | (kill-local-variable 'doc-view-last-page-number) |
c17587fe | 870 | (doc-view-initiate-display)) |
94dbe99c | 871 | |
b4cb319f SM |
872 | (defun doc-view-sentinel (proc event) |
873 | "Generic sentinel for doc-view conversion processes." | |
94dbe99c | 874 | (if (not (string-match "finished" event)) |
b4cb319f | 875 | (message "DocView: process %s changed status to %s." |
405ee48d CY |
876 | (process-name proc) |
877 | (if (string-match "\\(.+\\)\n?\\'" event) | |
878 | (match-string 1 event) | |
879 | event)) | |
ec4853ab SM |
880 | (when (buffer-live-p (process-get proc 'buffer)) |
881 | (with-current-buffer (process-get proc 'buffer) | |
416f1802 SM |
882 | (setq doc-view--current-converter-processes |
883 | (delq proc doc-view--current-converter-processes)) | |
ec4853ab | 884 | (setq mode-line-process |
416f1802 SM |
885 | (if doc-view--current-converter-processes |
886 | (format ":%s" (car doc-view--current-converter-processes)))) | |
ec4853ab SM |
887 | (funcall (process-get proc 'callback)))))) |
888 | ||
889 | (defun doc-view-start-process (name program args callback) | |
3c03f2ce TH |
890 | ;; Make sure the process is started in an existing directory, (rather than |
891 | ;; some file-name-handler-managed dir, for example). | |
efb3f01d SM |
892 | (let* ((default-directory (or (unhandled-file-name-directory |
893 | default-directory) | |
3c03f2ce | 894 | (expand-file-name "~/"))) |
ec4853ab SM |
895 | (proc (apply 'start-process name doc-view-conversion-buffer |
896 | program args))) | |
416f1802 | 897 | (push proc doc-view--current-converter-processes) |
ec4853ab SM |
898 | (setq mode-line-process (list (format ":%s" proc))) |
899 | (set-process-sentinel proc 'doc-view-sentinel) | |
900 | (process-put proc 'buffer (current-buffer)) | |
901 | (process-put proc 'callback callback))) | |
b4cb319f SM |
902 | |
903 | (defun doc-view-dvi->pdf (dvi pdf callback) | |
904 | "Convert DVI to PDF asynchronously and call CALLBACK when finished." | |
53d4c024 TH |
905 | ;; Prefer dvipdf over dvipdfm, because the latter has problems if the DVI |
906 | ;; references and includes other PS files. | |
907 | (if (and doc-view-dvipdf-program | |
908 | (executable-find doc-view-dvipdf-program)) | |
909 | (doc-view-start-process "dvi->pdf" doc-view-dvipdf-program | |
94d11cb5 IK |
910 | (list dvi pdf) |
911 | callback) | |
53d4c024 TH |
912 | (doc-view-start-process "dvi->pdf" doc-view-dvipdfm-program |
913 | (list "-o" pdf dvi) | |
914 | callback))) | |
ec4853ab | 915 | |
bbc7ff25 SM |
916 | (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback) |
917 | (doc-view-start-process | |
918 | "pdf/ps->png" doc-view-ghostscript-program | |
919 | `(,@doc-view-ghostscript-options | |
920 | ,(format "-r%d" (round doc-view-resolution)) | |
921 | ,@(if page `(,(format "-dFirstPage=%d" page))) | |
922 | ,@(if page `(,(format "-dLastPage=%d" page))) | |
923 | ,(concat "-sOutputFile=" png) | |
924 | ,pdf) | |
925 | callback)) | |
85f17e35 EP |
926 | |
927 | (defalias 'doc-view-ps->png-converter-ghostscript | |
928 | 'doc-view-pdf->png-converter-ghostscript) | |
929 | ||
bbc7ff25 SM |
930 | (defun doc-view-djvu->tiff-converter-ddjvu (djvu tiff page callback) |
931 | "Convert PAGE of a DJVU file to bitmap(s) asynchronously. | |
932 | Call CALLBACK with no arguments when done. | |
933 | If PAGE is nil, convert the whole document." | |
934 | (doc-view-start-process | |
935 | "djvu->tiff" "ddjvu" | |
936 | `("-format=tiff" | |
937 | ;; ddjvu only accepts the range 1-999. | |
938 | ,(format "-scale=%d" (round doc-view-resolution)) | |
939 | ;; -eachpage was only added after djvulibre-3.5.25.3! | |
940 | ,@(unless page '("-eachpage")) | |
941 | ,@(if page `(,(format "-page=%d" page))) | |
942 | ,djvu | |
943 | ,tiff) | |
944 | callback)) | |
945 | ||
946 | (defun doc-view-pdf->png-converter-mupdf (pdf png page callback) | |
947 | (doc-view-start-process | |
948 | "pdf->png" doc-view-pdfdraw-program | |
949 | `(,(concat "-o" png) | |
950 | ,(format "-r%d" (round doc-view-resolution)) | |
951 | ||
952 | ,@(if page `(,(format "%d" page)))) | |
953 | callback)) | |
85f17e35 | 954 | |
f63f9398 | 955 | (defun doc-view-odf->pdf-converter-unoconv (odf callback) |
95e16d17 TH |
956 | "Convert ODF to PDF asynchronously and call CALLBACK when finished. |
957 | The converted PDF is put into the current cache directory, and it | |
958 | is named like ODF with the extension turned to pdf." | |
f63f9398 | 959 | (doc-view-start-process "odf->pdf" doc-view-odf->pdf-converter-program |
416f1802 | 960 | (list "-f" "pdf" "-o" (doc-view--current-cache-dir) odf) |
95e16d17 | 961 | callback)) |
94dbe99c | 962 | |
f63f9398 TH |
963 | (defun doc-view-odf->pdf-converter-soffice (odf callback) |
964 | "Convert ODF to PDF asynchronously and call CALLBACK when finished. | |
965 | The converted PDF is put into the current cache directory, and it | |
966 | is named like ODF with the extension turned to pdf." | |
957a6f26 TH |
967 | ;; FIXME: soffice doesn't work when there's another running |
968 | ;; LibreOffice instance, in which case it returns success without | |
969 | ;; actually doing anything. See LibreOffice bug | |
970 | ;; https://bugs.freedesktop.org/show_bug.cgi?id=37531. A workaround | |
971 | ;; is to start soffice with a separate UserInstallation directory. | |
972 | (let ((tmp-user-install-dir (make-temp-file "libreoffice-docview" t))) | |
973 | (doc-view-start-process "odf->pdf" doc-view-odf->pdf-converter-program | |
974 | (list | |
975 | (concat "-env:UserInstallation=file://" | |
976 | tmp-user-install-dir) | |
977 | "--headless" "--convert-to" "pdf" | |
416f1802 | 978 | "--outdir" (doc-view--current-cache-dir) odf) |
957a6f26 TH |
979 | (lambda () |
980 | (delete-directory tmp-user-install-dir t) | |
981 | (funcall callback))))) | |
f63f9398 | 982 | |
94dbe99c | 983 | (defun doc-view-pdf/ps->png (pdf-ps png) |
2b541f9a | 984 | ;; FIXME: Fix name and docstring to account for djvu&tiff. |
1f75e53d | 985 | "Convert PDF-PS to PNG asynchronously." |
bbc7ff25 SM |
986 | (funcall |
987 | (pcase doc-view-doc-type | |
988 | (`pdf doc-view-pdf->png-converter-function) | |
989 | (`djvu #'doc-view-djvu->tiff-converter-ddjvu) | |
990 | (_ #'doc-view-ps->png-converter-ghostscript)) | |
991 | pdf-ps png nil | |
94d11cb5 | 992 | (let ((resolution doc-view-resolution)) |
bdd42899 SM |
993 | (lambda () |
994 | ;; Only create the resolution file when it's all done, so it also | |
995 | ;; serves as a witness that the conversion is complete. | |
996 | (write-region (prin1-to-string resolution) nil | |
997 | (expand-file-name "resolution.el" | |
416f1802 | 998 | (doc-view--current-cache-dir)) |
bdd42899 | 999 | nil 'silently) |
416f1802 SM |
1000 | (when doc-view--current-timer |
1001 | (cancel-timer doc-view--current-timer) | |
1002 | (setq doc-view--current-timer nil)) | |
bbc7ff25 SM |
1003 | (doc-view-display (current-buffer) 'force)))) |
1004 | ||
ec4853ab | 1005 | ;; Update the displayed pages as soon as they're done generating. |
94dbe99c | 1006 | (when doc-view-conversion-refresh-interval |
416f1802 | 1007 | (setq doc-view--current-timer |
ec4853ab SM |
1008 | (run-at-time "1 secs" doc-view-conversion-refresh-interval |
1009 | 'doc-view-display | |
1010 | (current-buffer))))) | |
1011 | ||
aa360da1 GM |
1012 | (declare-function clear-image-cache "image.c" (&optional filter)) |
1013 | ||
72781fef | 1014 | (defun doc-view-document->bitmap (pdf png pages) |
bbc7ff25 | 1015 | "Convert a document file to bitmap images asynchronously. |
ec4853ab SM |
1016 | Start by converting PAGES, and then the rest." |
1017 | (if (null pages) | |
1018 | (doc-view-pdf/ps->png pdf png) | |
1019 | ;; We could render several `pages' with a single process if they're | |
1020 | ;; (almost) consecutive, but since in 99% of the cases, there'll be only | |
1021 | ;; a single page anyway, and of the remaining 1%, few cases will have | |
1022 | ;; consecutive pages, it's not worth the trouble. | |
94d11cb5 | 1023 | (let ((rest (cdr pages))) |
72781fef | 1024 | (funcall doc-view-single-page-converter-function |
2b541f9a | 1025 | pdf (format png (car pages)) (car pages) |
ec4853ab SM |
1026 | (lambda () |
1027 | (if rest | |
bbc7ff25 | 1028 | (doc-view-document->bitmap pdf png rest) |
ec4853ab SM |
1029 | ;; Yippie, the important pages are done, update the display. |
1030 | (clear-image-cache) | |
bdd42899 SM |
1031 | ;; For the windows that have a message (like "Welcome to |
1032 | ;; DocView") display property, clearing the image cache is | |
1033 | ;; not sufficient. | |
1034 | (dolist (win (get-buffer-window-list (current-buffer) nil 'visible)) | |
1035 | (with-selected-window win | |
2167b7b2 | 1036 | (when (stringp (overlay-get (doc-view-current-overlay) 'display)) |
2b541f9a | 1037 | (doc-view-goto-page (doc-view-current-page))))) |
ec4853ab SM |
1038 | ;; Convert the rest of the pages. |
1039 | (doc-view-pdf/ps->png pdf png))))))) | |
94dbe99c | 1040 | |
b4cb319f SM |
1041 | (defun doc-view-pdf->txt (pdf txt callback) |
1042 | "Convert PDF to TXT asynchronously and call CALLBACK when finished." | |
d0af9f77 | 1043 | (or (executable-find doc-view-pdftotext-program) |
ca32d854 | 1044 | (error "You need the `pdftotext' program to convert a PDF to text")) |
ec4853ab SM |
1045 | (doc-view-start-process "pdf->txt" doc-view-pdftotext-program |
1046 | (list "-raw" pdf txt) | |
1047 | callback)) | |
515357c2 | 1048 | |
c00ebc98 TH |
1049 | (defun doc-view-current-cache-doc-pdf () |
1050 | "Return the name of the doc.pdf in the current cache dir. | |
1051 | This file exists only if the current document isn't a PDF or PS file already." | |
416f1802 | 1052 | (expand-file-name "doc.pdf" (doc-view--current-cache-dir))) |
c00ebc98 | 1053 | |
b4cb319f SM |
1054 | (defun doc-view-doc->txt (txt callback) |
1055 | "Convert the current document to text and call CALLBACK when done." | |
416f1802 | 1056 | (make-directory (doc-view--current-cache-dir) t) |
f58e0fd5 SM |
1057 | (pcase doc-view-doc-type |
1058 | ||
b4cb319f | 1059 | ;; Doc is a PDF, so convert it to TXT |
416f1802 | 1060 | (doc-view-pdf->txt doc-view--buffer-file-name txt callback)) |
f58e0fd5 | 1061 | (`ps |
b4cb319f SM |
1062 | ;; Doc is a PS, so convert it to PDF (which will be converted to |
1063 | ;; TXT thereafter). | |
c00ebc98 | 1064 | (let ((pdf (doc-view-current-cache-doc-pdf))) |
416f1802 | 1065 | (doc-view-ps->pdf doc-view--buffer-file-name pdf |
b4cb319f | 1066 | (lambda () (doc-view-pdf->txt pdf txt callback))))) |
f58e0fd5 | 1067 | (`dvi |
b4cb319f SM |
1068 | ;; Doc is a DVI. This means that a doc.pdf already exists in its |
1069 | ;; cache subdirectory. | |
c00ebc98 | 1070 | (doc-view-pdf->txt (doc-view-current-cache-doc-pdf) txt callback)) |
f58e0fd5 | 1071 | (`odf |
b71b7803 TH |
1072 | ;; Doc is some ODF (or MS Office) doc. This means that a doc.pdf |
1073 | ;; already exists in its cache subdirectory. | |
c00ebc98 | 1074 | (doc-view-pdf->txt (doc-view-current-cache-doc-pdf) txt callback)) |
f58e0fd5 | 1075 | (_ (error "DocView doesn't know what to do")))) |
b4cb319f SM |
1076 | |
1077 | (defun doc-view-ps->pdf (ps pdf callback) | |
1078 | "Convert PS to PDF asynchronously and call CALLBACK when finished." | |
d0af9f77 | 1079 | (or (executable-find doc-view-ps2pdf-program) |
ca32d854 | 1080 | (error "You need the `ps2pdf' program to convert PS to PDF")) |
ec4853ab SM |
1081 | (doc-view-start-process "ps->pdf" doc-view-ps2pdf-program |
1082 | (list | |
1083 | ;; Avoid security problems when rendering files from | |
1084 | ;; untrusted sources. | |
1085 | "-dSAFER" | |
1086 | ;; in-file and out-file | |
1087 | ps pdf) | |
1088 | callback)) | |
1089 | ||
1090 | (defun doc-view-active-pages () | |
1091 | (let ((pages ())) | |
1092 | (dolist (win (get-buffer-window-list (current-buffer) nil 'visible)) | |
1093 | (let ((page (image-mode-window-get 'page win))) | |
1094 | (unless (memq page pages) (push page pages)))) | |
1095 | pages)) | |
94dbe99c | 1096 | |
640602f7 | 1097 | (defun doc-view-convert-current-doc () |
416f1802 | 1098 | "Convert `doc-view--buffer-file-name' to a set of png files, one file per page. |
640602f7 | 1099 | Those files are saved in the directory given by the function |
416f1802 | 1100 | `doc-view--current-cache-dir'." |
c17587fe SM |
1101 | ;; Let stale files still display while we recompute the new ones, so only |
1102 | ;; flush the cache when the conversion is over. One of the reasons why it | |
1103 | ;; is important to keep displaying the stale page is so that revert-buffer | |
1104 | ;; preserves the horizontal/vertical scroll settings (which are otherwise | |
416f1802 SM |
1105 | ;; reset during the redisplay). |
1106 | (setq doc-view--pending-cache-flush t) | |
2b541f9a | 1107 | (let ((png-file (expand-file-name |
bbc7ff25 | 1108 | (format doc-view--image-file-pattern "%d") |
416f1802 SM |
1109 | (doc-view--current-cache-dir)))) |
1110 | (make-directory (doc-view--current-cache-dir) t) | |
f58e0fd5 SM |
1111 | (pcase doc-view-doc-type |
1112 | (`dvi | |
eb79098b SM |
1113 | ;; DVI files have to be converted to PDF before Ghostscript can process |
1114 | ;; it. | |
c00ebc98 | 1115 | (let ((pdf (doc-view-current-cache-doc-pdf))) |
416f1802 | 1116 | (doc-view-dvi->pdf doc-view--buffer-file-name pdf |
b4cb319f | 1117 | (lambda () (doc-view-pdf/ps->png pdf png-file))))) |
f58e0fd5 | 1118 | (`odf |
95e16d17 TH |
1119 | ;; ODF files have to be converted to PDF before Ghostscript can |
1120 | ;; process it. | |
c00ebc98 | 1121 | (let ((pdf (doc-view-current-cache-doc-pdf)) |
2b541f9a | 1122 | (opdf (expand-file-name |
416f1802 | 1123 | (concat (file-name-base doc-view--buffer-file-name) |
2b541f9a | 1124 | ".pdf") |
416f1802 | 1125 | doc-view--current-cache-dir)) |
e95a67dc | 1126 | (png-file png-file)) |
2b541f9a | 1127 | ;; The unoconv tool only supports an output directory, but no |
95e16d17 TH |
1128 | ;; file name. It's named like the input file with the |
1129 | ;; extension replaced by pdf. | |
416f1802 | 1130 | (funcall doc-view-odf->pdf-converter-function doc-view--buffer-file-name |
95e16d17 TH |
1131 | (lambda () |
1132 | ;; Rename to doc.pdf | |
1133 | (rename-file opdf pdf) | |
1134 | (doc-view-pdf/ps->png pdf png-file))))) | |
bbc7ff25 | 1135 | ((or `pdf `djvu) |
2b541f9a | 1136 | (let ((pages (doc-view-active-pages))) |
bbc7ff25 | 1137 | ;; Convert doc to bitmap images starting with the active pages. |
416f1802 | 1138 | (doc-view-document->bitmap doc-view--buffer-file-name png-file pages))) |
f58e0fd5 | 1139 | (_ |
eb79098b | 1140 | ;; Convert to PNG images. |
416f1802 | 1141 | (doc-view-pdf/ps->png doc-view--buffer-file-name png-file))))) |
94dbe99c | 1142 | |
94dbe99c TTN |
1143 | ;;;; Slicing |
1144 | ||
aa360da1 GM |
1145 | (declare-function image-size "image.c" (spec &optional pixels frame)) |
1146 | ||
94dbe99c TTN |
1147 | (defun doc-view-set-slice (x y width height) |
1148 | "Set the slice of the images that should be displayed. | |
1149 | You can use this function to tell doc-view not to display the | |
1150 | margins of the document. It prompts for the top-left corner (X | |
1151 | and Y) of the slice to display and its WIDTH and HEIGHT. | |
1152 | ||
db8a5a18 TH |
1153 | See `doc-view-set-slice-using-mouse' and |
1154 | `doc-view-set-slice-from-bounding-box' for more convenient ways | |
1155 | to do that. To reset the slice use `doc-view-reset-slice'." | |
94dbe99c | 1156 | (interactive |
de171465 | 1157 | (let* ((size (image-size (doc-view-current-image) t)) |
1ca678aa MC |
1158 | (a (read-number (format "Top-left X (0..%d): " (car size)))) |
1159 | (b (read-number (format "Top-left Y (0..%d): " (cdr size)))) | |
1160 | (c (read-number (format "Width (0..%d): " (- (car size) a)))) | |
1161 | (d (read-number (format "Height (0..%d): " (- (cdr size) b))))) | |
94dbe99c | 1162 | (list a b c d))) |
de171465 | 1163 | (setf (doc-view-current-slice) (list x y width height)) |
94dbe99c | 1164 | ;; Redisplay |
de171465 | 1165 | (doc-view-goto-page (doc-view-current-page))) |
94dbe99c TTN |
1166 | |
1167 | (defun doc-view-set-slice-using-mouse () | |
1168 | "Set the slice of the images that should be displayed. | |
1169 | You set the slice by pressing mouse-1 at its top-left corner and | |
1170 | dragging it to its bottom-right corner. See also | |
1171 | `doc-view-set-slice' and `doc-view-reset-slice'." | |
1172 | (interactive) | |
1173 | (let (x y w h done) | |
1174 | (while (not done) | |
1175 | (let ((e (read-event | |
1ca678aa MC |
1176 | (concat "Press mouse-1 at the top-left corner and " |
1177 | "drag it to the bottom-right corner!")))) | |
1178 | (when (eq (car e) 'drag-mouse-1) | |
1179 | (setq x (car (posn-object-x-y (event-start e)))) | |
1180 | (setq y (cdr (posn-object-x-y (event-start e)))) | |
1181 | (setq w (- (car (posn-object-x-y (event-end e))) x)) | |
1182 | (setq h (- (cdr (posn-object-x-y (event-end e))) y)) | |
1183 | (setq done t)))) | |
94dbe99c TTN |
1184 | (doc-view-set-slice x y w h))) |
1185 | ||
db8a5a18 TH |
1186 | (defun doc-view-get-bounding-box () |
1187 | "Get the BoundingBox information of the current page." | |
1188 | (let* ((page (doc-view-current-page)) | |
c00ebc98 TH |
1189 | (doc (let ((cache-doc (doc-view-current-cache-doc-pdf))) |
1190 | (if (file-exists-p cache-doc) | |
1191 | cache-doc | |
416f1802 | 1192 | doc-view--buffer-file-name))) |
db8a5a18 TH |
1193 | (o (shell-command-to-string |
1194 | (concat doc-view-ghostscript-program | |
1195 | " -dSAFER -dBATCH -dNOPAUSE -q -sDEVICE=bbox " | |
1196 | (format "-dFirstPage=%s -dLastPage=%s %s" | |
c00ebc98 | 1197 | page page doc))))) |
db8a5a18 TH |
1198 | (save-match-data |
1199 | (when (string-match (concat "%%BoundingBox: " | |
1200 | "\\([[:digit:]]+\\) \\([[:digit:]]+\\) " | |
1201 | "\\([[:digit:]]+\\) \\([[:digit:]]+\\)") o) | |
1202 | (mapcar #'string-to-number | |
1203 | (list (match-string 1 o) | |
1204 | (match-string 2 o) | |
1205 | (match-string 3 o) | |
1206 | (match-string 4 o))))))) | |
1207 | ||
1208 | (defvar doc-view-paper-sizes | |
1209 | '((a4 595 842) | |
1210 | (a4-landscape 842 595) | |
1211 | (letter 612 792) | |
1212 | (letter-landscape 792 612) | |
1213 | (legal 612 1008) | |
1214 | (legal-landscape 1008 612) | |
1215 | (a3 842 1191) | |
1216 | (a3-landscape 1191 842) | |
1217 | (tabloid 792 1224) | |
1218 | (ledger 1224 792)) | |
1219 | "An alist from paper size names to dimensions.") | |
1220 | ||
1221 | (defun doc-view-guess-paper-size (iw ih) | |
1222 | "Guess the paper size according to the aspect ratio." | |
1223 | (cl-labels ((div (x y) | |
1224 | (round (/ (* 100.0 x) y)))) | |
1225 | (let ((ar (div iw ih)) | |
1226 | (al (mapcar (lambda (l) | |
46624b4f | 1227 | (list (div (nth 1 l) (nth 2 l)) (car l))) |
db8a5a18 TH |
1228 | doc-view-paper-sizes))) |
1229 | (cadr (assoc ar al))))) | |
1230 | ||
1231 | (defun doc-view-scale-bounding-box (ps iw ih bb) | |
46624b4f SM |
1232 | (list (/ (* (nth 0 bb) iw) (nth 1 (assoc ps doc-view-paper-sizes))) |
1233 | (/ (* (nth 1 bb) ih) (nth 2 (assoc ps doc-view-paper-sizes))) | |
1234 | (/ (* (nth 2 bb) iw) (nth 1 (assoc ps doc-view-paper-sizes))) | |
1235 | (/ (* (nth 3 bb) ih) (nth 2 (assoc ps doc-view-paper-sizes))))) | |
db8a5a18 TH |
1236 | |
1237 | (defun doc-view-set-slice-from-bounding-box (&optional force-paper-size) | |
1238 | "Set the slice from the document's BoundingBox information. | |
1239 | The result is that the margins are almost completely cropped, | |
1240 | much more accurate than could be done manually using | |
1241 | `doc-view-set-slice-using-mouse'." | |
1242 | (interactive "P") | |
1243 | (let ((bb (doc-view-get-bounding-box))) | |
1244 | (if (not bb) | |
1245 | (message "BoundingBox couldn't be determined") | |
1246 | (let* ((is (image-size (doc-view-current-image) t)) | |
1247 | (iw (car is)) | |
1248 | (ih (cdr is)) | |
bbc7ff25 SM |
1249 | (ps (or (and (null force-paper-size) |
1250 | (doc-view-guess-paper-size iw ih)) | |
db8a5a18 | 1251 | (intern (completing-read "Paper size: " |
bbc7ff25 | 1252 | doc-view-paper-sizes |
db8a5a18 TH |
1253 | nil t)))) |
1254 | (bb (doc-view-scale-bounding-box ps iw ih bb)) | |
1255 | (x1 (nth 0 bb)) | |
1256 | (y1 (nth 1 bb)) | |
1257 | (x2 (nth 2 bb)) | |
1258 | (y2 (nth 3 bb))) | |
1259 | ;; We keep a 2 pixel margin. | |
1260 | (doc-view-set-slice (- x1 2) (- ih y2 2) | |
1261 | (+ (- x2 x1) 4) (+ (- y2 y1) 4)))))) | |
1262 | ||
94dbe99c | 1263 | (defun doc-view-reset-slice () |
e48a5bf9 | 1264 | "Reset the current slice. |
1f75e53d | 1265 | After calling this function whole pages will be visible again." |
94dbe99c | 1266 | (interactive) |
de171465 | 1267 | (setf (doc-view-current-slice) nil) |
94dbe99c | 1268 | ;; Redisplay |
de171465 | 1269 | (doc-view-goto-page (doc-view-current-page))) |
94dbe99c TTN |
1270 | |
1271 | ;;;; Display | |
1272 | ||
1273 | (defun doc-view-insert-image (file &rest args) | |
1274 | "Insert the given png FILE. | |
e48a5bf9 | 1275 | ARGS is a list of image descriptors." |
416f1802 | 1276 | (when doc-view--pending-cache-flush |
c17587fe | 1277 | (clear-image-cache) |
416f1802 | 1278 | (setq doc-view--pending-cache-flush nil)) |
d35f5864 | 1279 | (let ((ol (doc-view-current-overlay))) |
f35ffe5e TH |
1280 | ;; Only insert the image if the buffer is visible. |
1281 | (when (window-live-p (overlay-get ol 'window)) | |
d35f5864 TH |
1282 | (let* ((image (if (and file (file-readable-p file)) |
1283 | (if (not (and doc-view-scale-internally | |
1284 | (fboundp 'imagemagick-types))) | |
1285 | (apply 'create-image file doc-view--image-type nil args) | |
1286 | (unless (member :width args) | |
1287 | (setq args `(,@args :width ,doc-view-image-width))) | |
1288 | (apply 'create-image file 'imagemagick nil args)))) | |
1289 | (slice (doc-view-current-slice)) | |
1290 | (img-width (and image (car (image-size image)))) | |
1291 | (displayed-img-width (if (and image slice) | |
1292 | (* (/ (float (nth 2 slice)) | |
1293 | (car (image-size image 'pixels))) | |
1294 | img-width) | |
1295 | img-width)) | |
290d5b58 | 1296 | (window-width (window-width))) |
d35f5864 TH |
1297 | (setf (doc-view-current-image) image) |
1298 | (move-overlay ol (point-min) (point-max)) | |
1299 | ;; In case the window is wider than the image, center the image | |
1300 | ;; horizontally. | |
1301 | (overlay-put ol 'before-string | |
1302 | (when (and image (> window-width displayed-img-width)) | |
1303 | (propertize " " 'display | |
1304 | `(space :align-to (+ center (-0.5 . ,displayed-img-width)))))) | |
1305 | (overlay-put ol 'display | |
1306 | (cond | |
1307 | (image | |
1308 | (if slice | |
1309 | (list (cons 'slice slice) image) | |
1310 | image)) | |
1311 | ;; We're trying to display a page that doesn't exist. | |
416f1802 | 1312 | (doc-view--current-converter-processes |
d35f5864 TH |
1313 | ;; Maybe the page doesn't exist *yet*. |
1314 | "Cannot display this page (yet)!") | |
1315 | (t | |
1316 | ;; Typically happens if the conversion process somehow | |
1317 | ;; failed. Better not signal an error here because it | |
1318 | ;; could prevent a subsequent reconversion from fixing | |
1319 | ;; the problem. | |
1320 | (concat "Cannot display this page!\n" | |
1321 | "Maybe because of a conversion failure!")))) | |
1322 | (let ((win (overlay-get ol 'window))) | |
1323 | (if (stringp (overlay-get ol 'display)) | |
1324 | (progn ;Make sure the text is not scrolled out of view. | |
1325 | (set-window-hscroll win 0) | |
1326 | (set-window-vscroll win 0)) | |
1327 | (let ((hscroll (image-mode-window-get 'hscroll win)) | |
1328 | (vscroll (image-mode-window-get 'vscroll win))) | |
1329 | ;; Reset scroll settings, in case they were changed. | |
1330 | (if hscroll (set-window-hscroll win hscroll)) | |
1331 | (if vscroll (set-window-vscroll win vscroll))))))))) | |
94dbe99c TTN |
1332 | |
1333 | (defun doc-view-sort (a b) | |
1334 | "Return non-nil if A should be sorted before B. | |
416f1802 | 1335 | Predicate for sorting `doc-view--current-files'." |
f4c75497 SM |
1336 | (or (< (length a) (length b)) |
1337 | (and (= (length a) (length b)) | |
1338 | (string< a b)))) | |
94dbe99c | 1339 | |
65073003 SM |
1340 | (defun doc-view-display (buffer &optional force) |
1341 | "Start viewing the document in BUFFER. | |
214abdd4 SM |
1342 | If FORCE is non-nil, start viewing even if the document does not |
1343 | have the page we want to view." | |
65073003 | 1344 | (with-current-buffer buffer |
416f1802 SM |
1345 | (let ((prev-pages doc-view--current-files)) |
1346 | (setq doc-view--current-files | |
1347 | (sort (directory-files (doc-view--current-cache-dir) t | |
bbc7ff25 SM |
1348 | (format doc-view--image-file-pattern |
1349 | "[0-9]+") | |
2b541f9a | 1350 | t) |
56741510 | 1351 | 'doc-view-sort)) |
416f1802 | 1352 | (unless (eq (length prev-pages) (length doc-view--current-files)) |
5ad86e34 | 1353 | (force-mode-line-update)) |
83600dc8 SM |
1354 | (dolist (win (or (get-buffer-window-list buffer nil t) |
1355 | (list t))) | |
10b6e7c1 | 1356 | (let* ((page (doc-view-current-page win)) |
2b541f9a | 1357 | (pagefile (expand-file-name |
bbc7ff25 | 1358 | (format doc-view--image-file-pattern page) |
416f1802 | 1359 | (doc-view--current-cache-dir)))) |
10b6e7c1 CY |
1360 | (when (or force |
1361 | (and (not (member pagefile prev-pages)) | |
416f1802 | 1362 | (member pagefile doc-view--current-files))) |
83600dc8 SM |
1363 | (if (windowp win) |
1364 | (with-selected-window win | |
1365 | (cl-assert (eq (current-buffer) buffer) t) | |
1366 | (doc-view-goto-page page)) | |
db8a5a18 | 1367 | (doc-view-goto-page page)))))))) |
94dbe99c TTN |
1368 | |
1369 | (defun doc-view-buffer-message () | |
c17587fe SM |
1370 | ;; Only show this message initially, not when refreshing the buffer (in which |
1371 | ;; case it's better to keep displaying the "stale" page while computing | |
1372 | ;; the fresh new ones). | |
de171465 SM |
1373 | (unless (overlay-get (doc-view-current-overlay) 'display) |
1374 | (overlay-put (doc-view-current-overlay) 'display | |
c17587fe SM |
1375 | (concat (propertize "Welcome to DocView!" 'face 'bold) |
1376 | "\n" | |
1377 | " | |
1f75e53d | 1378 | If you see this buffer it means that the document you want to view is being |
f4c75497 | 1379 | converted to PNG and the conversion of the first page hasn't finished yet or |
94dbe99c TTN |
1380 | `doc-view-conversion-refresh-interval' is set to nil. |
1381 | ||
1382 | For now these keys are useful: | |
1383 | ||
1ca678aa | 1384 | `q' : Bury this buffer. Conversion will go on in background. |
937cb3fb | 1385 | `k' : Kill the conversion process and this buffer. |
c17587fe | 1386 | `K' : Kill the conversion process.\n")))) |
94dbe99c | 1387 | |
aa360da1 GM |
1388 | (declare-function tooltip-show "tooltip" (text &optional use-echo-area)) |
1389 | ||
94dbe99c TTN |
1390 | (defun doc-view-show-tooltip () |
1391 | (interactive) | |
de171465 | 1392 | (tooltip-show (doc-view-current-info))) |
94dbe99c | 1393 | |
515357c2 TH |
1394 | (defun doc-view-open-text () |
1395 | "Open a buffer with the current doc's contents as text." | |
1396 | (interactive) | |
416f1802 | 1397 | (if doc-view--current-converter-processes |
515357c2 | 1398 | (message "DocView: please wait till conversion finished.") |
416f1802 | 1399 | (let ((txt (expand-file-name "doc.txt" (doc-view--current-cache-dir)))) |
515357c2 | 1400 | (if (file-readable-p txt) |
5b355315 TH |
1401 | (let ((name (concat "Text contents of " |
1402 | (file-name-nondirectory buffer-file-name))) | |
1403 | (dir (file-name-directory buffer-file-name))) | |
1404 | (with-current-buffer (find-file txt) | |
1405 | (rename-buffer name) | |
1406 | (setq default-directory dir))) | |
b4cb319f | 1407 | (doc-view-doc->txt txt 'doc-view-open-text))))) |
515357c2 | 1408 | |
937cb3fb | 1409 | ;;;;; Toggle between editing and viewing |
640602f7 | 1410 | |
83600dc8 SM |
1411 | (defvar-local doc-view-saved-settings nil |
1412 | "Doc-view settings saved while in some other mode.") | |
1413 | (put 'doc-view-saved-settings 'permanent-local t) | |
1414 | ||
640602f7 | 1415 | (defun doc-view-toggle-display () |
937cb3fb | 1416 | "Toggle between editing a document as text or viewing it." |
640602f7 | 1417 | (interactive) |
937cb3fb GM |
1418 | (if (eq major-mode 'doc-view-mode) |
1419 | ;; Switch to editing mode | |
1420 | (progn | |
1421 | (doc-view-kill-proc) | |
1422 | (setq buffer-read-only nil) | |
ad727c81 TH |
1423 | ;; Switch to the previously used major mode or fall back to |
1424 | ;; normal mode. | |
291cc045 | 1425 | (doc-view-fallback-mode) |
c17587fe | 1426 | (doc-view-minor-mode 1)) |
937cb3fb GM |
1427 | ;; Switch to doc-view-mode |
1428 | (when (and (buffer-modified-p) | |
1429 | (y-or-n-p "The buffer has been modified. Save the changes? ")) | |
1430 | (save-buffer)) | |
937cb3fb | 1431 | (doc-view-mode))) |
640602f7 | 1432 | |
94dbe99c TTN |
1433 | ;;;; Searching |
1434 | ||
515357c2 | 1435 | |
94dbe99c TTN |
1436 | (defun doc-view-search-internal (regexp file) |
1437 | "Return a list of FILE's pages that contain text matching REGEXP. | |
1ca678aa MC |
1438 | The value is an alist of the form (PAGE CONTEXTS) where PAGE is |
1439 | the pagenumber and CONTEXTS are all lines of text containing a match." | |
94dbe99c TTN |
1440 | (with-temp-buffer |
1441 | (insert-file-contents file) | |
1442 | (let ((page 1) | |
1ca678aa MC |
1443 | (lastpage 1) |
1444 | matches) | |
94dbe99c | 1445 | (while (re-search-forward (concat "\\(?:\\([\f]\\)\\|\\(" |
1ca678aa | 1446 | regexp "\\)\\)") nil t) |
069b4ce3 | 1447 | (when (match-string 1) (setq page (1+ page))) |
1ca678aa MC |
1448 | (when (match-string 2) |
1449 | (if (/= page lastpage) | |
c17587fe | 1450 | (push (cons page |
515357c2 TH |
1451 | (list (buffer-substring |
1452 | (line-beginning-position) | |
1453 | (line-end-position)))) | |
1454 | matches) | |
1ca678aa MC |
1455 | (setq matches (cons |
1456 | (append | |
1457 | (or | |
1458 | ;; This page already is a match. | |
1459 | (car matches) | |
1460 | ;; This is the first match on page. | |
1461 | (list page)) | |
1462 | (list (buffer-substring | |
1463 | (line-beginning-position) | |
1464 | (line-end-position)))) | |
1465 | (cdr matches)))) | |
1466 | (setq lastpage page))) | |
94dbe99c TTN |
1467 | (nreverse matches)))) |
1468 | ||
1469 | (defun doc-view-search-no-of-matches (list) | |
1470 | "Extract the number of matches from the search result LIST." | |
1471 | (let ((no 0)) | |
1472 | (dolist (p list) | |
1473 | (setq no (+ no (1- (length p))))) | |
1474 | no)) | |
1475 | ||
7baca0fa JL |
1476 | (defun doc-view-search-backward (new-query) |
1477 | "Call `doc-view-search' for backward search. | |
1478 | If prefix NEW-QUERY is given, ask for a new regexp." | |
1479 | (interactive "P") | |
da99b369 | 1480 | (doc-view-search new-query t)) |
7baca0fa JL |
1481 | |
1482 | (defun doc-view-search (new-query &optional backward) | |
1483 | "Jump to the next match or initiate a new search if NEW-QUERY is given. | |
94dbe99c | 1484 | If the current document hasn't been transformed to plain text |
7baca0fa JL |
1485 | till now do that first. |
1486 | If BACKWARD is non-nil, jump to the previous match." | |
1487 | (interactive "P") | |
da99b369 | 1488 | (if (and (not new-query) |
416f1802 | 1489 | doc-view--current-search-matches) |
7baca0fa JL |
1490 | (if backward |
1491 | (doc-view-search-previous-match 1) | |
1492 | (doc-view-search-next-match 1)) | |
1493 | ;; New search, so forget the old results. | |
416f1802 | 1494 | (setq doc-view--current-search-matches nil) |
7baca0fa | 1495 | (let ((txt (expand-file-name "doc.txt" |
416f1802 | 1496 | (doc-view--current-cache-dir)))) |
7baca0fa JL |
1497 | (if (file-readable-p txt) |
1498 | (progn | |
416f1802 | 1499 | (setq doc-view--current-search-matches |
7baca0fa JL |
1500 | (doc-view-search-internal |
1501 | (read-from-minibuffer "Regexp: ") | |
1502 | txt)) | |
1503 | (message "DocView: search yielded %d matches." | |
1504 | (doc-view-search-no-of-matches | |
416f1802 | 1505 | doc-view--current-search-matches))) |
7baca0fa | 1506 | ;; We must convert to TXT first! |
416f1802 | 1507 | (if doc-view--current-converter-processes |
7baca0fa | 1508 | (message "DocView: please wait till conversion finished.") |
b4cb319f | 1509 | (doc-view-doc->txt txt (lambda () (doc-view-search nil)))))))) |
94dbe99c TTN |
1510 | |
1511 | (defun doc-view-search-next-match (arg) | |
1512 | "Go to the ARGth next matching page." | |
1513 | (interactive "p") | |
539a920c | 1514 | (let* ((next-pages (cl-remove-if |
de171465 | 1515 | (lambda (i) (<= (car i) (doc-view-current-page))) |
416f1802 | 1516 | doc-view--current-search-matches)) |
1ca678aa | 1517 | (page (car (nth (1- arg) next-pages)))) |
94dbe99c | 1518 | (if page |
1ca678aa | 1519 | (doc-view-goto-page page) |
94dbe99c | 1520 | (when (and |
416f1802 | 1521 | doc-view--current-search-matches |
1ca678aa | 1522 | (y-or-n-p "No more matches after current page. Wrap to first match? ")) |
416f1802 | 1523 | (doc-view-goto-page (caar doc-view--current-search-matches)))))) |
94dbe99c TTN |
1524 | |
1525 | (defun doc-view-search-previous-match (arg) | |
1526 | "Go to the ARGth previous matching page." | |
1527 | (interactive "p") | |
539a920c | 1528 | (let* ((prev-pages (cl-remove-if |
de171465 | 1529 | (lambda (i) (>= (car i) (doc-view-current-page))) |
416f1802 | 1530 | doc-view--current-search-matches)) |
1ca678aa | 1531 | (page (car (nth (1- arg) (nreverse prev-pages))))) |
94dbe99c | 1532 | (if page |
1ca678aa | 1533 | (doc-view-goto-page page) |
94dbe99c | 1534 | (when (and |
416f1802 | 1535 | doc-view--current-search-matches |
1ca678aa | 1536 | (y-or-n-p "No more matches before current page. Wrap to last match? ")) |
416f1802 | 1537 | (doc-view-goto-page (caar (last doc-view--current-search-matches))))))) |
94dbe99c | 1538 | |
640602f7 | 1539 | ;;;; User interface commands and the mode |
94dbe99c | 1540 | |
0d17c4b9 | 1541 | (put 'doc-view-mode 'mode-class 'special) |
c17587fe | 1542 | |
515357c2 TH |
1543 | (defun doc-view-already-converted-p () |
1544 | "Return non-nil if the current doc was already converted." | |
416f1802 | 1545 | (and (file-exists-p (doc-view--current-cache-dir)) |
bdd42899 SM |
1546 | ;; Check that the resolution info is there, otherwise it means |
1547 | ;; the conversion is incomplete. | |
1548 | (file-readable-p (expand-file-name "resolution.el" | |
416f1802 | 1549 | (doc-view--current-cache-dir))) |
2b541f9a | 1550 | (> (length (directory-files |
416f1802 | 1551 | (doc-view--current-cache-dir) |
bbc7ff25 | 1552 | nil (format doc-view--image-file-pattern "[0-9]+"))) |
bdd42899 | 1553 | 0))) |
515357c2 | 1554 | |
c17587fe | 1555 | (defun doc-view-initiate-display () |
2b541f9a | 1556 | ;; Switch to image display if possible. |
322f4559 | 1557 | (if (doc-view-mode-p doc-view-doc-type) |
c17587fe SM |
1558 | (progn |
1559 | (doc-view-buffer-message) | |
de171465 | 1560 | (setf (doc-view-current-page) (or (doc-view-current-page) 1)) |
515357c2 | 1561 | (if (doc-view-already-converted-p) |
c17587fe SM |
1562 | (progn |
1563 | (message "DocView: using cached files!") | |
bbc7ff25 | 1564 | ;; Load the saved resolution. |
416f1802 SM |
1565 | (let* ((res-file |
1566 | (expand-file-name "resolution.el" | |
1567 | (doc-view--current-cache-dir))) | |
bdd42899 SM |
1568 | (res |
1569 | (with-temp-buffer | |
1570 | (when (file-readable-p res-file) | |
1571 | (insert-file-contents res-file) | |
1572 | (read (current-buffer)))))) | |
1573 | (when (numberp res) | |
bbc7ff25 | 1574 | (setq-local doc-view-resolution res))) |
65073003 | 1575 | (doc-view-display (current-buffer) 'force)) |
c17587fe SM |
1576 | (doc-view-convert-current-doc)) |
1577 | (message | |
1578 | "%s" | |
1579 | (substitute-command-keys | |
1580 | (concat "Type \\[doc-view-toggle-display] to toggle between " | |
1581 | "editing or viewing the document.")))) | |
1582 | (message | |
1583 | "%s" | |
0855c2ca | 1584 | (concat "No PNG support is available, or some conversion utility for " |
416f1802 | 1585 | (file-name-extension doc-view--buffer-file-name) |
0855c2ca | 1586 | " files is missing.")) |
1281bd51 TH |
1587 | (when (and (executable-find doc-view-pdftotext-program) |
1588 | (y-or-n-p | |
1589 | "Unable to render file. View extracted text instead? ")) | |
1590 | (doc-view-open-text)) | |
1591 | (doc-view-toggle-display))) | |
94dbe99c | 1592 | |
e0385bf4 | 1593 | (defvar bookmark-make-record-function) |
069b4ce3 | 1594 | |
65073003 SM |
1595 | (defun doc-view-clone-buffer-hook () |
1596 | ;; FIXME: There are several potential problems linked with reconversion | |
1597 | ;; and auto-revert when we have indirect buffers because they share their | |
1598 | ;; /tmp cache directory. This sharing is good (you'd rather not reconvert | |
1599 | ;; for each clone), but that means that clones need to collaborate a bit. | |
1600 | ;; I guess it mostly means: detect when a reconversion process is already | |
1601 | ;; running, and run the sentinel in all clones. | |
515357c2 | 1602 | ;; |
de171465 | 1603 | ;; Maybe the clones should really have a separate /tmp directory |
65073003 SM |
1604 | ;; so they could have a different resolution and you could use clones |
1605 | ;; for zooming. | |
de171465 SM |
1606 | (remove-overlays (point-min) (point-max) 'doc-view t) |
1607 | (if (consp image-mode-winprops-alist) (setq image-mode-winprops-alist nil))) | |
65073003 | 1608 | |
eb79098b SM |
1609 | (defun doc-view-intersection (l1 l2) |
1610 | (let ((l ())) | |
1611 | (dolist (x l1) (if (memq x l2) (push x l))) | |
1612 | l)) | |
1613 | ||
291cc045 TH |
1614 | (defun doc-view-set-doc-type () |
1615 | "Figure out the current document type (`doc-view-doc-type')." | |
1616 | (let ((name-types | |
1617 | (when buffer-file-name | |
005551fe SM |
1618 | (cdr (assoc-ignore-case |
1619 | (file-name-extension buffer-file-name) | |
1620 | '( | |
1621 | ;; DVI | |
1622 | ("dvi" dvi) | |
1623 | ||
1624 | ("pdf" pdf) ("epdf" pdf) | |
1625 | ;; PostScript | |
1626 | ("ps" ps) ("eps" ps) | |
1627 | ;; DjVu | |
1628 | ("djvu" djvu) | |
1629 | ;; OpenDocument formats. | |
1630 | ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf) | |
1631 | ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf) | |
1632 | ("ots" odf) ("otp" odf) ("otg" odf) | |
1633 | ;; Microsoft Office formats (also handled by the odf | |
1634 | ;; conversion chain). | |
1635 | ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf) | |
1636 | ("ppt" odf) ("pps" odf) ("pptx" odf)))))) | |
291cc045 TH |
1637 | (content-types |
1638 | (save-excursion | |
1639 | (goto-char (point-min)) | |
1640 | (cond | |
1641 | ((looking-at "%!") '(ps)) | |
1642 | ((looking-at "%PDF") '(pdf)) | |
2b541f9a EP |
1643 | ((looking-at "\367\002") '(dvi)) |
1644 | ((looking-at "AT&TFORM") '(djvu)))))) | |
416f1802 SM |
1645 | (setq-local |
1646 | doc-view-doc-type | |
1647 | (car (or (doc-view-intersection name-types content-types) | |
1648 | (when (and name-types content-types) | |
1649 | (error "Conflicting types: name says %s but content says %s" | |
1650 | name-types content-types)) | |
1651 | name-types content-types | |
1652 | (error "Cannot determine the document type")))))) | |
291cc045 | 1653 | |
2b541f9a EP |
1654 | (defun doc-view-set-up-single-converter () |
1655 | "Find the right single-page converter for the current document type" | |
1656 | (pcase-let ((`(,conv-function ,type ,extension) | |
1657 | (pcase doc-view-doc-type | |
bbc7ff25 SM |
1658 | (`djvu (list #'doc-view-djvu->tiff-converter-ddjvu 'tiff "tif")) |
1659 | (_ (list doc-view-pdf->png-converter-function 'png "png"))))) | |
2b541f9a EP |
1660 | (setq-local doc-view-single-page-converter-function conv-function) |
1661 | (setq-local doc-view--image-type type) | |
bbc7ff25 | 1662 | (setq-local doc-view--image-file-pattern (concat "page-%s." extension)))) |
2b541f9a | 1663 | |
57b9823e TH |
1664 | ;; desktop.el integration |
1665 | ||
50105835 | 1666 | (defun doc-view-desktop-save-buffer (_desktop-dirname) |
57b9823e TH |
1667 | `((page . ,(doc-view-current-page)) |
1668 | (slice . ,(doc-view-current-slice)))) | |
1669 | ||
50105835 SM |
1670 | (declare-function desktop-restore-file-buffer "desktop" |
1671 | (buffer-filename buffer-name buffer-misc)) | |
1672 | ||
57b9823e TH |
1673 | (defun doc-view-restore-desktop-buffer (file name misc) |
1674 | (let ((page (cdr (assq 'page misc))) | |
1675 | (slice (cdr (assq 'slice misc)))) | |
50105835 | 1676 | (desktop-restore-file-buffer file name misc) |
57b9823e TH |
1677 | (with-selected-window (or (get-buffer-window (current-buffer) 0) |
1678 | (selected-window)) | |
1679 | (doc-view-goto-page page) | |
1680 | (when slice (apply 'doc-view-set-slice slice))))) | |
1681 | ||
416f1802 SM |
1682 | (add-to-list 'desktop-buffer-mode-handlers |
1683 | '(doc-view-mode . doc-view-restore-desktop-buffer)) | |
57b9823e | 1684 | |
640602f7 | 1685 | ;;;###autoload |
937cb3fb | 1686 | (defun doc-view-mode () |
640602f7 | 1687 | "Major mode in DocView buffers. |
06a21f70 TH |
1688 | |
1689 | DocView Mode is an Emacs document viewer. It displays PDF, PS | |
1690 | and DVI files (as PNG images) in Emacs buffers. | |
1691 | ||
640602f7 | 1692 | You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to |
5c1f16b0 SM |
1693 | toggle between displaying the document or editing it as text. |
1694 | \\{doc-view-mode-map}" | |
937cb3fb | 1695 | (interactive) |
f9adf05b | 1696 | |
96fe38a8 | 1697 | (if (= (point-min) (point-max)) |
85699772 | 1698 | ;; The doc is empty or doesn't exist at all, so fallback to |
96fe38a8 SM |
1699 | ;; another mode. We used to also check file-exists-p, but this |
1700 | ;; returns nil for tar members. | |
291cc045 | 1701 | (doc-view-fallback-mode) |
06a21f70 | 1702 | |
83600dc8 | 1703 | (let* ((prev-major-mode (if (derived-mode-p 'doc-view-mode) |
416f1802 | 1704 | doc-view--previous-major-mode |
83600dc8 | 1705 | (unless (eq major-mode 'fundamental-mode) |
ad727c81 | 1706 | major-mode)))) |
06a21f70 | 1707 | (kill-all-local-variables) |
416f1802 | 1708 | (setq-local doc-view--previous-major-mode prev-major-mode)) |
83600dc8 SM |
1709 | |
1710 | (dolist (var doc-view-saved-settings) | |
1711 | (set (make-local-variable (car var)) (cdr var))) | |
bc19637d | 1712 | |
06a21f70 | 1713 | ;; Figure out the document type. |
291cc045 TH |
1714 | (unless doc-view-doc-type |
1715 | (doc-view-set-doc-type)) | |
2b541f9a | 1716 | (doc-view-set-up-single-converter) |
bc19637d | 1717 | |
06a21f70 TH |
1718 | (doc-view-make-safe-dir doc-view-cache-directory) |
1719 | ;; Handle compressed files, remote files, files inside archives | |
416f1802 SM |
1720 | (setq-local doc-view--buffer-file-name |
1721 | (cond | |
1722 | (jka-compr-really-do-compress | |
1723 | ;; FIXME: there's a risk of name conflicts here. | |
1724 | (expand-file-name | |
1725 | (file-name-nondirectory | |
1726 | (file-name-sans-extension buffer-file-name)) | |
1727 | doc-view-cache-directory)) | |
1728 | ;; Is the file readable by local processes? | |
1729 | ;; We used to use `file-remote-p' but it's unclear what it's | |
1730 | ;; supposed to return nil for things like local files accessed | |
1731 | ;; via `su' or via file://... | |
1732 | ((let ((file-name-handler-alist nil)) | |
1733 | (not (and buffer-file-name | |
1734 | (file-readable-p buffer-file-name)))) | |
1735 | ;; FIXME: there's a risk of name conflicts here. | |
1736 | (expand-file-name | |
1737 | (if buffer-file-name | |
1738 | (file-name-nondirectory buffer-file-name) | |
1739 | (buffer-name)) | |
1740 | doc-view-cache-directory)) | |
1741 | (t buffer-file-name))) | |
1742 | (when (not (string= doc-view--buffer-file-name buffer-file-name)) | |
1743 | (write-region nil nil doc-view--buffer-file-name)) | |
bc19637d | 1744 | |
06a21f70 TH |
1745 | (add-hook 'change-major-mode-hook |
1746 | (lambda () | |
1747 | (doc-view-kill-proc) | |
1748 | (remove-overlays (point-min) (point-max) 'doc-view t)) | |
1749 | nil t) | |
1750 | (add-hook 'clone-indirect-buffer-hook 'doc-view-clone-buffer-hook nil t) | |
1751 | (add-hook 'kill-buffer-hook 'doc-view-kill-proc nil t) | |
57b9823e TH |
1752 | (when (and (boundp 'desktop-save-mode) |
1753 | desktop-save-mode) | |
1754 | (setq-local desktop-save-buffer 'doc-view-desktop-save-buffer)) | |
bc19637d | 1755 | |
06a21f70 TH |
1756 | (remove-overlays (point-min) (point-max) 'doc-view t) ;Just in case. |
1757 | ;; Keep track of display info ([vh]scroll, page number, overlay, | |
1758 | ;; ...) for each window in which this document is shown. | |
1759 | (add-hook 'image-mode-new-window-functions | |
1760 | 'doc-view-new-window-function nil t) | |
1761 | (image-mode-setup-winprops) | |
bc19637d | 1762 | |
bbc7ff25 SM |
1763 | (setq-local mode-line-position |
1764 | '(" P" (:eval (number-to-string (doc-view-current-page))) | |
1765 | "/" (:eval (number-to-string (doc-view-last-page-number))))) | |
06a21f70 | 1766 | ;; Don't scroll unless the user specifically asked for it. |
bbc7ff25 | 1767 | (setq-local auto-hscroll-mode nil) |
2f68e157 GM |
1768 | (if (boundp 'mwheel-scroll-up-function) ; not --without-x build |
1769 | (setq-local mwheel-scroll-up-function | |
1770 | #'doc-view-scroll-up-or-next-page)) | |
1771 | (if (boundp 'mwheel-scroll-down-function) | |
1772 | (setq-local mwheel-scroll-down-function | |
1773 | #'doc-view-scroll-down-or-previous-page)) | |
bbc7ff25 | 1774 | (setq-local cursor-type nil) |
06a21f70 | 1775 | (use-local-map doc-view-mode-map) |
bbc7ff25 SM |
1776 | (add-hook 'after-revert-hook 'doc-view-reconvert-doc nil t) |
1777 | (setq-local bookmark-make-record-function | |
1778 | #'doc-view-bookmark-make-record) | |
06a21f70 TH |
1779 | (setq mode-name "DocView" |
1780 | buffer-read-only t | |
1781 | major-mode 'doc-view-mode) | |
1782 | (doc-view-initiate-display) | |
d1d33062 TH |
1783 | ;; Switch off view-mode explicitly, because doc-view-mode is the |
1784 | ;; canonical view mode for PDF/PS/DVI files. This could be | |
1785 | ;; switched on automatically depending on the value of | |
1786 | ;; `view-read-only'. | |
bbc7ff25 | 1787 | (setq-local view-read-only nil) |
06a21f70 | 1788 | (run-mode-hooks 'doc-view-mode-hook))) |
937cb3fb | 1789 | |
291cc045 TH |
1790 | (defun doc-view-fallback-mode () |
1791 | "Fallback to the previous or next best major mode." | |
83600dc8 SM |
1792 | (let ((vars (if (derived-mode-p 'doc-view-mode) |
1793 | (mapcar (lambda (var) (cons var (symbol-value var))) | |
1794 | '(doc-view-resolution | |
1795 | image-mode-winprops-alist))))) | |
e7a1c32d | 1796 | (remove-overlays (point-min) (point-max) 'doc-view t) |
416f1802 SM |
1797 | (if doc-view--previous-major-mode |
1798 | (funcall doc-view--previous-major-mode) | |
83600dc8 SM |
1799 | (let ((auto-mode-alist |
1800 | (rassq-delete-all | |
1801 | 'doc-view-mode-maybe | |
1802 | (rassq-delete-all 'doc-view-mode | |
1803 | (copy-alist auto-mode-alist))))) | |
1804 | (normal-mode))) | |
1805 | (when vars | |
1806 | (setq-local doc-view-saved-settings vars)))) | |
291cc045 TH |
1807 | |
1808 | ;;;###autoload | |
1809 | (defun doc-view-mode-maybe () | |
1810 | "Switch to `doc-view-mode' if possible. | |
1811 | If the required external tools are not available, then fallback | |
1812 | to the next best mode." | |
1813 | (condition-case nil | |
1814 | (doc-view-set-doc-type) | |
1815 | (error (doc-view-fallback-mode))) | |
1816 | (if (doc-view-mode-p doc-view-doc-type) | |
1817 | (doc-view-mode) | |
1818 | (doc-view-fallback-mode))) | |
1819 | ||
937cb3fb GM |
1820 | ;;;###autoload |
1821 | (define-minor-mode doc-view-minor-mode | |
06e21633 CY |
1822 | "Toggle displaying buffer via Doc View (Doc View minor mode). |
1823 | With a prefix argument ARG, enable Doc View minor mode if ARG is | |
1824 | positive, and disable it otherwise. If called from Lisp, enable | |
1825 | the mode if ARG is omitted or nil. | |
1826 | ||
937cb3fb GM |
1827 | See the command `doc-view-mode' for more information on this mode." |
1828 | nil " DocView" doc-view-minor-mode-map | |
1829 | :group 'doc-view | |
1830 | (when doc-view-minor-mode | |
1831 | (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t) | |
1832 | (message | |
1833 | "%s" | |
1834 | (substitute-command-keys | |
1835 | "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document.")))) | |
94dbe99c TTN |
1836 | |
1837 | (defun doc-view-clear-cache () | |
1838 | "Delete the whole cache (`doc-view-cache-directory')." | |
1839 | (interactive) | |
515357c2 | 1840 | (dired-delete-file doc-view-cache-directory 'always)) |
94dbe99c TTN |
1841 | |
1842 | (defun doc-view-dired-cache () | |
1843 | "Open `dired' in `doc-view-cache-directory'." | |
1844 | (interactive) | |
1845 | (dired doc-view-cache-directory)) | |
1846 | ||
94dbe99c | 1847 | |
1666a6b3 TH |
1848 | ;;;; Bookmark integration |
1849 | ||
e44fa724 KF |
1850 | (declare-function bookmark-make-record-default |
1851 | "bookmark" (&optional no-file no-context posn)) | |
a9f8b49b | 1852 | (declare-function bookmark-prop-get "bookmark" (bookmark prop)) |
43f8b275 | 1853 | (declare-function bookmark-default-handler "bookmark" (bmk)) |
a9f8b49b | 1854 | |
32a091dd | 1855 | (defun doc-view-bookmark-make-record () |
43f8b275 SM |
1856 | (nconc (bookmark-make-record-default) |
1857 | `((page . ,(doc-view-current-page)) | |
1858 | (handler . doc-view-bookmark-jump)))) | |
1666a6b3 TH |
1859 | |
1860 | ;;;###autoload | |
1861 | (defun doc-view-bookmark-jump (bmk) | |
03e26a79 | 1862 | ;; This implements the `handler' function interface for record type |
e0385bf4 | 1863 | ;; returned by `doc-view-bookmark-make-record', which see. |
3ccd4eaf TH |
1864 | (let ((page (bookmark-prop-get bmk 'page)) |
1865 | (show-fn-sym (make-symbol "doc-view-bookmark-after-jump-hook"))) | |
1866 | (fset show-fn-sym | |
1867 | (lambda () | |
1868 | (remove-hook 'bookmark-after-jump-hook show-fn-sym) | |
1869 | (when (not (eq major-mode 'doc-view-mode)) | |
1870 | (doc-view-toggle-display)) | |
1871 | (with-selected-window | |
1872 | (or (get-buffer-window (current-buffer) 0) | |
1873 | (selected-window)) | |
1874 | (doc-view-goto-page page)))) | |
1875 | (add-hook 'bookmark-after-jump-hook show-fn-sym) | |
1876 | (bookmark-default-handler bmk))) | |
069b4ce3 GM |
1877 | |
1878 | (provide 'doc-view) | |
1879 | ||
1880 | ;; Local Variables: | |
f0da764a | 1881 | ;; eval: (outline-minor-mode 1) |
069b4ce3 GM |
1882 | ;; End: |
1883 | ||
94dbe99c | 1884 | ;;; doc-view.el ends here |