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