Merge from emacs-24; up to 2012-12-10T20:27:33Z!eggert@cs.ucla.edu
[emacs.git] / lisp / doc-view.el
blobfa54eef828de840114bc342d07c1fc41706f716a
1 ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs -*- lexical-binding: t -*-
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
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Requirements:
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/).
32 ;;; Commentary:
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.
39 ;; To use it simply open a document file with
41 ;; C-x C-f ~/path/to/document RET
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.
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'.
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.
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').
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.
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'.
92 ;;; Configuration:
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.)
98 ;; This and all other options can be set with the customization interface.
99 ;; Simply do
101 ;; M-x customize-group RET doc-view RET
103 ;; and modify them to your needs.
105 ;;; Todo:
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).
115 ;;;; About isearch support
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.
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.
135 ;; Anyway, if someone with better isearch knowledge wants to give it a try,
136 ;; feel free to do it. --Tassilo
138 ;;; Code:
140 (eval-when-compile (require 'cl-lib))
141 (require 'dired)
142 (require 'image-mode)
143 (require 'jka-compr)
145 ;;;; Customization Options
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-")
156 (defcustom doc-view-ghostscript-program "gs"
157 "Program to convert PS and PDF files to PNG."
158 :type 'file
159 :group 'doc-view)
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")
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")
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)
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)
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)
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)
212 (defcustom doc-view-dvipdfm-program "dvipdfm"
213 "Program to convert DVI files to PDF.
215 DVI file will be converted to PDF before the resulting PDF is
216 converted to PNG.
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)
223 (defcustom doc-view-dvipdf-program "dvipdf"
224 "Program to convert DVI files to PDF.
226 DVI file will be converted to PDF before the resulting PDF is
227 converted to PNG.
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)
234 (defcustom doc-view-unoconv-program "unoconv"
235 "Program to convert any file type readable by OpenOffice.org to PDF.
237 Needed for viewing OpenOffice.org (and MS Office) files."
238 :version "24.1"
239 :type 'file
240 :group 'doc-view)
242 (defcustom doc-view-ps2pdf-program "ps2pdf"
243 "Program to convert PS files to PDF.
245 PS files will be converted to PDF before searching is possible."
246 :type 'file
247 :group 'doc-view)
249 (defcustom doc-view-pdftotext-program "pdftotext"
250 "Program to convert PDF files to plain text.
252 Needed for searching."
253 :type 'file
254 :group 'doc-view)
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)
263 (defvar doc-view-conversion-buffer " *doc-view conversion output*"
264 "The buffer where messages from the converter programs go to.")
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)
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")
284 ;;;; Internal Variables
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)))
306 (defvar doc-view-current-files nil
307 "Only used internally.")
308 (make-variable-buffer-local 'doc-view-current-files)
310 (defvar doc-view-current-converter-processes nil
311 "Only used internally.")
312 (make-variable-buffer-local 'doc-view-current-converter-processes)
314 (defvar doc-view-current-timer nil
315 "Only used internally.")
316 (make-variable-buffer-local 'doc-view-current-timer)
318 (defvar doc-view-current-cache-dir nil
319 "Only used internally.")
320 (make-variable-buffer-local 'doc-view-current-cache-dir)
322 (defvar doc-view-current-search-matches nil
323 "Only used internally.")
324 (make-variable-buffer-local 'doc-view-current-search-matches)
326 (defvar doc-view-pending-cache-flush nil
327 "Only used internally.")
329 (defvar doc-view-previous-major-mode nil
330 "Only used internally.")
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'.")
340 (defvar doc-view-doc-type nil
341 "The type of document in the current buffer.
342 Can be `dvi', `pdf', or `ps'.")
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.")
349 (defvar-local doc-view--image-type nil
350 "The type of image in the current buffer.
351 Can be `png' or `tiff'.")
353 (defvar-local doc-view--image-file-pattern nil
354 "The `format' pattern of image file names.
355 Typically \"page-%s.png\".")
357 ;;;; DocView Keymaps
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.")
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))
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]
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]
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'.")
449 ;;;; Navigation Commands
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))
458 (defun doc-view-last-page-number ()
459 (length doc-view-current-files))
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))))
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))))
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))))
528 (defun doc-view-first-page ()
529 "View the first page."
530 (interactive)
531 (doc-view-goto-page 1))
533 (defun doc-view-last-page ()
534 "View the last page."
535 (interactive)
536 (doc-view-goto-page (doc-view-last-page-number)))
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)))
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)))
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)))
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)))
604 ;;;; Utility Functions
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))
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))))
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) " "))))))))
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)
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)))))
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)))
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))))
711 ;;;; Conversion Functions
713 (defvar doc-view-shrink-factor 1.125)
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)))))
733 (defun doc-view-shrink (factor)
734 "Shrink the document."
735 (interactive (list doc-view-shrink-factor))
736 (doc-view-enlarge (/ 1.0 factor)))
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))))
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)))
754 (doc-view-enlarge scale-factor)
755 (setf (doc-view-current-slice) new-slice)
756 (doc-view-goto-page (doc-view-current-page))))))
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))))
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)))
778 (doc-view-enlarge scale-factor)
779 (setf (doc-view-current-slice) new-slice)
780 (doc-view-goto-page (doc-view-current-page))))))
782 (defun doc-view-fit-page-to-window ()
783 "Fit the image to the window.
784 More specifically, this function enlarges image by:
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))))))
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))
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))))))
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)))
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)))
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))
878 (defalias 'doc-view-ps->png-converter-ghostscript
879 'doc-view-pdf->png-converter-ghostscript)
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))
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))
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))
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))))
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)))))
943 (declare-function clear-image-cache "image.c" (&optional filter))
945 (defun doc-view-document->bitmap (pdf png pages)
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 doc-view-single-page-converter-function
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)))))))
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))
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)))
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"))))
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))
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))
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)))
1071 ;; Convert to PNG images.
1072 (doc-view-pdf/ps->png doc-view-buffer-file-name png-file)))))
1074 ;;;; Slicing
1076 (declare-function image-size "image.c" (spec &optional pixels frame))
1078 (defun doc-view-set-slice (x y width height)
1079 "Set the slice of the images that should be displayed.
1080 You can use this function to tell doc-view not to display the
1081 margins of the document. It prompts for the top-left corner (X
1082 and Y) of the slice to display and its WIDTH and HEIGHT.
1084 See `doc-view-set-slice-using-mouse' and
1085 `doc-view-set-slice-from-bounding-box' for more convenient ways
1086 to do that. To reset the slice use `doc-view-reset-slice'."
1087 (interactive
1088 (let* ((size (image-size (doc-view-current-image) t))
1089 (a (read-number (format "Top-left X (0..%d): " (car size))))
1090 (b (read-number (format "Top-left Y (0..%d): " (cdr size))))
1091 (c (read-number (format "Width (0..%d): " (- (car size) a))))
1092 (d (read-number (format "Height (0..%d): " (- (cdr size) b)))))
1093 (list a b c d)))
1094 (setf (doc-view-current-slice) (list x y width height))
1095 ;; Redisplay
1096 (doc-view-goto-page (doc-view-current-page)))
1098 (defun doc-view-set-slice-using-mouse ()
1099 "Set the slice of the images that should be displayed.
1100 You set the slice by pressing mouse-1 at its top-left corner and
1101 dragging it to its bottom-right corner. See also
1102 `doc-view-set-slice' and `doc-view-reset-slice'."
1103 (interactive)
1104 (let (x y w h done)
1105 (while (not done)
1106 (let ((e (read-event
1107 (concat "Press mouse-1 at the top-left corner and "
1108 "drag it to the bottom-right corner!"))))
1109 (when (eq (car e) 'drag-mouse-1)
1110 (setq x (car (posn-object-x-y (event-start e))))
1111 (setq y (cdr (posn-object-x-y (event-start e))))
1112 (setq w (- (car (posn-object-x-y (event-end e))) x))
1113 (setq h (- (cdr (posn-object-x-y (event-end e))) y))
1114 (setq done t))))
1115 (doc-view-set-slice x y w h)))
1117 (defun doc-view-get-bounding-box ()
1118 "Get the BoundingBox information of the current page."
1119 (let* ((page (doc-view-current-page))
1120 (doc (let ((cache-doc (doc-view-current-cache-doc-pdf)))
1121 (if (file-exists-p cache-doc)
1122 cache-doc
1123 doc-view-buffer-file-name)))
1124 (o (shell-command-to-string
1125 (concat doc-view-ghostscript-program
1126 " -dSAFER -dBATCH -dNOPAUSE -q -sDEVICE=bbox "
1127 (format "-dFirstPage=%s -dLastPage=%s %s"
1128 page page doc)))))
1129 (save-match-data
1130 (when (string-match (concat "%%BoundingBox: "
1131 "\\([[:digit:]]+\\) \\([[:digit:]]+\\) "
1132 "\\([[:digit:]]+\\) \\([[:digit:]]+\\)") o)
1133 (mapcar #'string-to-number
1134 (list (match-string 1 o)
1135 (match-string 2 o)
1136 (match-string 3 o)
1137 (match-string 4 o)))))))
1139 (defvar doc-view-paper-sizes
1140 '((a4 595 842)
1141 (a4-landscape 842 595)
1142 (letter 612 792)
1143 (letter-landscape 792 612)
1144 (legal 612 1008)
1145 (legal-landscape 1008 612)
1146 (a3 842 1191)
1147 (a3-landscape 1191 842)
1148 (tabloid 792 1224)
1149 (ledger 1224 792))
1150 "An alist from paper size names to dimensions.")
1152 (defun doc-view-guess-paper-size (iw ih)
1153 "Guess the paper size according to the aspect ratio."
1154 (cl-labels ((div (x y)
1155 (round (/ (* 100.0 x) y))))
1156 (let ((ar (div iw ih))
1157 (al (mapcar (lambda (l)
1158 (list (div (nth 1 l) (nth 2 l)) (car l)))
1159 doc-view-paper-sizes)))
1160 (cadr (assoc ar al)))))
1162 (defun doc-view-scale-bounding-box (ps iw ih bb)
1163 (list (/ (* (nth 0 bb) iw) (nth 1 (assoc ps doc-view-paper-sizes)))
1164 (/ (* (nth 1 bb) ih) (nth 2 (assoc ps doc-view-paper-sizes)))
1165 (/ (* (nth 2 bb) iw) (nth 1 (assoc ps doc-view-paper-sizes)))
1166 (/ (* (nth 3 bb) ih) (nth 2 (assoc ps doc-view-paper-sizes)))))
1168 (defun doc-view-set-slice-from-bounding-box (&optional force-paper-size)
1169 "Set the slice from the document's BoundingBox information.
1170 The result is that the margins are almost completely cropped,
1171 much more accurate than could be done manually using
1172 `doc-view-set-slice-using-mouse'."
1173 (interactive "P")
1174 (let ((bb (doc-view-get-bounding-box)))
1175 (if (not bb)
1176 (message "BoundingBox couldn't be determined")
1177 (let* ((is (image-size (doc-view-current-image) t))
1178 (iw (car is))
1179 (ih (cdr is))
1180 (ps (or (and (null force-paper-size)
1181 (doc-view-guess-paper-size iw ih))
1182 (intern (completing-read "Paper size: "
1183 doc-view-paper-sizes
1184 nil t))))
1185 (bb (doc-view-scale-bounding-box ps iw ih bb))
1186 (x1 (nth 0 bb))
1187 (y1 (nth 1 bb))
1188 (x2 (nth 2 bb))
1189 (y2 (nth 3 bb)))
1190 ;; We keep a 2 pixel margin.
1191 (doc-view-set-slice (- x1 2) (- ih y2 2)
1192 (+ (- x2 x1) 4) (+ (- y2 y1) 4))))))
1194 (defun doc-view-reset-slice ()
1195 "Reset the current slice.
1196 After calling this function whole pages will be visible again."
1197 (interactive)
1198 (setf (doc-view-current-slice) nil)
1199 ;; Redisplay
1200 (doc-view-goto-page (doc-view-current-page)))
1202 ;;;; Display
1204 (defun doc-view-insert-image (file &rest args)
1205 "Insert the given png FILE.
1206 ARGS is a list of image descriptors."
1207 (when doc-view-pending-cache-flush
1208 (clear-image-cache)
1209 (setq doc-view-pending-cache-flush nil))
1210 (let ((ol (doc-view-current-overlay))
1211 (image (if (and file (file-readable-p file))
1212 (if (not (and doc-view-scale-internally
1213 (fboundp 'imagemagick-types)))
1214 (apply 'create-image file doc-view--image-type nil args)
1215 (unless (member :width args)
1216 (setq args `(,@args :width ,doc-view-image-width)))
1217 (apply 'create-image file 'imagemagick nil args))))
1218 (slice (doc-view-current-slice)))
1219 (setf (doc-view-current-image) image)
1220 (move-overlay ol (point-min) (point-max))
1221 (overlay-put ol 'display
1222 (cond
1223 (image
1224 (if slice
1225 (list (cons 'slice slice) image)
1226 image))
1227 ;; We're trying to display a page that doesn't exist.
1228 (doc-view-current-converter-processes
1229 ;; Maybe the page doesn't exist *yet*.
1230 "Cannot display this page (yet)!")
1232 ;; Typically happens if the conversion process somehow
1233 ;; failed. Better not signal an error here because it
1234 ;; could prevent a subsequent reconversion from fixing
1235 ;; the problem.
1236 (concat "Cannot display this page!\n"
1237 "Maybe because of a conversion failure!"))))
1238 (let ((win (overlay-get ol 'window)))
1239 (if (stringp (overlay-get ol 'display))
1240 (progn ;Make sure the text is not scrolled out of view.
1241 (set-window-hscroll win 0)
1242 (set-window-vscroll win 0))
1243 (let ((hscroll (image-mode-window-get 'hscroll win))
1244 (vscroll (image-mode-window-get 'vscroll win)))
1245 ;; Reset scroll settings, in case they were changed.
1246 (if hscroll (set-window-hscroll win hscroll))
1247 (if vscroll (set-window-vscroll win vscroll)))))))
1249 (defun doc-view-sort (a b)
1250 "Return non-nil if A should be sorted before B.
1251 Predicate for sorting `doc-view-current-files'."
1252 (or (< (length a) (length b))
1253 (and (= (length a) (length b))
1254 (string< a b))))
1256 (defun doc-view-display (buffer &optional force)
1257 "Start viewing the document in BUFFER.
1258 If FORCE is non-nil, start viewing even if the document does not
1259 have the page we want to view."
1260 (with-current-buffer buffer
1261 (let ((prev-pages doc-view-current-files))
1262 (setq doc-view-current-files
1263 (sort (directory-files (doc-view-current-cache-dir) t
1264 (format doc-view--image-file-pattern
1265 "[0-9]+")
1267 'doc-view-sort))
1268 (dolist (win (or (get-buffer-window-list buffer nil t)
1269 (list t)))
1270 (let* ((page (doc-view-current-page win))
1271 (pagefile (expand-file-name
1272 (format doc-view--image-file-pattern page)
1273 (doc-view-current-cache-dir))))
1274 (when (or force
1275 (and (not (member pagefile prev-pages))
1276 (member pagefile doc-view-current-files)))
1277 (if (windowp win)
1278 (with-selected-window win
1279 (cl-assert (eq (current-buffer) buffer) t)
1280 (doc-view-goto-page page))
1281 (doc-view-goto-page page))))))))
1283 (defun doc-view-buffer-message ()
1284 ;; Only show this message initially, not when refreshing the buffer (in which
1285 ;; case it's better to keep displaying the "stale" page while computing
1286 ;; the fresh new ones).
1287 (unless (overlay-get (doc-view-current-overlay) 'display)
1288 (overlay-put (doc-view-current-overlay) 'display
1289 (concat (propertize "Welcome to DocView!" 'face 'bold)
1290 "\n"
1292 If you see this buffer it means that the document you want to view is being
1293 converted to PNG and the conversion of the first page hasn't finished yet or
1294 `doc-view-conversion-refresh-interval' is set to nil.
1296 For now these keys are useful:
1298 `q' : Bury this buffer. Conversion will go on in background.
1299 `k' : Kill the conversion process and this buffer.
1300 `K' : Kill the conversion process.\n"))))
1302 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
1304 (defun doc-view-show-tooltip ()
1305 (interactive)
1306 (tooltip-show (doc-view-current-info)))
1308 (defun doc-view-open-text ()
1309 "Open a buffer with the current doc's contents as text."
1310 (interactive)
1311 (if doc-view-current-converter-processes
1312 (message "DocView: please wait till conversion finished.")
1313 (let ((txt (expand-file-name "doc.txt" (doc-view-current-cache-dir))))
1314 (if (file-readable-p txt)
1315 (let ((name (concat "Text contents of "
1316 (file-name-nondirectory buffer-file-name)))
1317 (dir (file-name-directory buffer-file-name)))
1318 (with-current-buffer (find-file txt)
1319 (rename-buffer name)
1320 (setq default-directory dir)))
1321 (doc-view-doc->txt txt 'doc-view-open-text)))))
1323 ;;;;; Toggle between editing and viewing
1325 (defvar-local doc-view-saved-settings nil
1326 "Doc-view settings saved while in some other mode.")
1327 (put 'doc-view-saved-settings 'permanent-local t)
1329 (defun doc-view-toggle-display ()
1330 "Toggle between editing a document as text or viewing it."
1331 (interactive)
1332 (if (eq major-mode 'doc-view-mode)
1333 ;; Switch to editing mode
1334 (progn
1335 (doc-view-kill-proc)
1336 (setq buffer-read-only nil)
1337 (remove-overlays (point-min) (point-max) 'doc-view t)
1338 (setq-local image-mode-winprops-alist t)
1339 ;; Switch to the previously used major mode or fall back to
1340 ;; normal mode.
1341 (doc-view-fallback-mode)
1342 (doc-view-minor-mode 1))
1343 ;; Switch to doc-view-mode
1344 (when (and (buffer-modified-p)
1345 (y-or-n-p "The buffer has been modified. Save the changes? "))
1346 (save-buffer))
1347 (doc-view-mode)))
1349 ;;;; Searching
1352 (defun doc-view-search-internal (regexp file)
1353 "Return a list of FILE's pages that contain text matching REGEXP.
1354 The value is an alist of the form (PAGE CONTEXTS) where PAGE is
1355 the pagenumber and CONTEXTS are all lines of text containing a match."
1356 (with-temp-buffer
1357 (insert-file-contents file)
1358 (let ((page 1)
1359 (lastpage 1)
1360 matches)
1361 (while (re-search-forward (concat "\\(?:\\([\f]\\)\\|\\("
1362 regexp "\\)\\)") nil t)
1363 (when (match-string 1) (setq page (1+ page)))
1364 (when (match-string 2)
1365 (if (/= page lastpage)
1366 (push (cons page
1367 (list (buffer-substring
1368 (line-beginning-position)
1369 (line-end-position))))
1370 matches)
1371 (setq matches (cons
1372 (append
1374 ;; This page already is a match.
1375 (car matches)
1376 ;; This is the first match on page.
1377 (list page))
1378 (list (buffer-substring
1379 (line-beginning-position)
1380 (line-end-position))))
1381 (cdr matches))))
1382 (setq lastpage page)))
1383 (nreverse matches))))
1385 (defun doc-view-search-no-of-matches (list)
1386 "Extract the number of matches from the search result LIST."
1387 (let ((no 0))
1388 (dolist (p list)
1389 (setq no (+ no (1- (length p)))))
1390 no))
1392 (defun doc-view-search-backward (new-query)
1393 "Call `doc-view-search' for backward search.
1394 If prefix NEW-QUERY is given, ask for a new regexp."
1395 (interactive "P")
1396 (doc-view-search new-query t))
1398 (defun doc-view-search (new-query &optional backward)
1399 "Jump to the next match or initiate a new search if NEW-QUERY is given.
1400 If the current document hasn't been transformed to plain text
1401 till now do that first.
1402 If BACKWARD is non-nil, jump to the previous match."
1403 (interactive "P")
1404 (if (and (not new-query)
1405 doc-view-current-search-matches)
1406 (if backward
1407 (doc-view-search-previous-match 1)
1408 (doc-view-search-next-match 1))
1409 ;; New search, so forget the old results.
1410 (setq doc-view-current-search-matches nil)
1411 (let ((txt (expand-file-name "doc.txt"
1412 (doc-view-current-cache-dir))))
1413 (if (file-readable-p txt)
1414 (progn
1415 (setq doc-view-current-search-matches
1416 (doc-view-search-internal
1417 (read-from-minibuffer "Regexp: ")
1418 txt))
1419 (message "DocView: search yielded %d matches."
1420 (doc-view-search-no-of-matches
1421 doc-view-current-search-matches)))
1422 ;; We must convert to TXT first!
1423 (if doc-view-current-converter-processes
1424 (message "DocView: please wait till conversion finished.")
1425 (doc-view-doc->txt txt (lambda () (doc-view-search nil))))))))
1427 (defun doc-view-search-next-match (arg)
1428 "Go to the ARGth next matching page."
1429 (interactive "p")
1430 (let* ((next-pages (doc-view-remove-if
1431 (lambda (i) (<= (car i) (doc-view-current-page)))
1432 doc-view-current-search-matches))
1433 (page (car (nth (1- arg) next-pages))))
1434 (if page
1435 (doc-view-goto-page page)
1436 (when (and
1437 doc-view-current-search-matches
1438 (y-or-n-p "No more matches after current page. Wrap to first match? "))
1439 (doc-view-goto-page (caar doc-view-current-search-matches))))))
1441 (defun doc-view-search-previous-match (arg)
1442 "Go to the ARGth previous matching page."
1443 (interactive "p")
1444 (let* ((prev-pages (doc-view-remove-if
1445 (lambda (i) (>= (car i) (doc-view-current-page)))
1446 doc-view-current-search-matches))
1447 (page (car (nth (1- arg) (nreverse prev-pages)))))
1448 (if page
1449 (doc-view-goto-page page)
1450 (when (and
1451 doc-view-current-search-matches
1452 (y-or-n-p "No more matches before current page. Wrap to last match? "))
1453 (doc-view-goto-page (caar (last doc-view-current-search-matches)))))))
1455 ;;;; User interface commands and the mode
1457 (put 'doc-view-mode 'mode-class 'special)
1459 (defun doc-view-already-converted-p ()
1460 "Return non-nil if the current doc was already converted."
1461 (and (file-exists-p (doc-view-current-cache-dir))
1462 ;; Check that the resolution info is there, otherwise it means
1463 ;; the conversion is incomplete.
1464 (file-readable-p (expand-file-name "resolution.el"
1465 (doc-view-current-cache-dir)))
1466 (> (length (directory-files
1467 (doc-view-current-cache-dir)
1468 nil (format doc-view--image-file-pattern "[0-9]+")))
1469 0)))
1471 (defun doc-view-initiate-display ()
1472 ;; Switch to image display if possible.
1473 (if (doc-view-mode-p doc-view-doc-type)
1474 (progn
1475 (doc-view-buffer-message)
1476 (setf (doc-view-current-page) (or (doc-view-current-page) 1))
1477 (if (doc-view-already-converted-p)
1478 (progn
1479 (message "DocView: using cached files!")
1480 ;; Load the saved resolution.
1481 (let* ((res-file (expand-file-name "resolution.el"
1482 (doc-view-current-cache-dir)))
1483 (res
1484 (with-temp-buffer
1485 (when (file-readable-p res-file)
1486 (insert-file-contents res-file)
1487 (read (current-buffer))))))
1488 (when (numberp res)
1489 (setq-local doc-view-resolution res)))
1490 (doc-view-display (current-buffer) 'force))
1491 (doc-view-convert-current-doc))
1492 (message
1493 "%s"
1494 (substitute-command-keys
1495 (concat "Type \\[doc-view-toggle-display] to toggle between "
1496 "editing or viewing the document."))))
1497 (message
1498 "%s"
1499 (concat "No PNG support is available, or some conversion utility for "
1500 (file-name-extension doc-view-buffer-file-name)
1501 " files is missing."))
1502 (when (and (executable-find doc-view-pdftotext-program)
1503 (y-or-n-p
1504 "Unable to render file. View extracted text instead? "))
1505 (doc-view-open-text))
1506 (doc-view-toggle-display)))
1508 (defvar bookmark-make-record-function)
1510 (defun doc-view-clone-buffer-hook ()
1511 ;; FIXME: There are several potential problems linked with reconversion
1512 ;; and auto-revert when we have indirect buffers because they share their
1513 ;; /tmp cache directory. This sharing is good (you'd rather not reconvert
1514 ;; for each clone), but that means that clones need to collaborate a bit.
1515 ;; I guess it mostly means: detect when a reconversion process is already
1516 ;; running, and run the sentinel in all clones.
1518 ;; Maybe the clones should really have a separate /tmp directory
1519 ;; so they could have a different resolution and you could use clones
1520 ;; for zooming.
1521 (remove-overlays (point-min) (point-max) 'doc-view t)
1522 (if (consp image-mode-winprops-alist) (setq image-mode-winprops-alist nil)))
1524 (defun doc-view-intersection (l1 l2)
1525 (let ((l ()))
1526 (dolist (x l1) (if (memq x l2) (push x l)))
1529 (defun doc-view-set-doc-type ()
1530 "Figure out the current document type (`doc-view-doc-type')."
1531 (let ((name-types
1532 (when buffer-file-name
1533 (cdr (assoc (file-name-extension buffer-file-name)
1535 ;; DVI
1536 ("dvi" dvi)
1537 ;; PDF
1538 ("pdf" pdf) ("epdf" pdf)
1539 ;; PostScript
1540 ("ps" ps) ("eps" ps)
1541 ;; DjVu
1542 ("djvu" djvu)
1543 ;; OpenDocument formats
1544 ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
1545 ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
1546 ("ots" odf) ("otp" odf) ("otg" odf)
1547 ;; Microsoft Office formats (also handled
1548 ;; by the odf conversion chain)
1549 ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
1550 ("ppt" odf) ("pptx" odf))))))
1551 (content-types
1552 (save-excursion
1553 (goto-char (point-min))
1554 (cond
1555 ((looking-at "%!") '(ps))
1556 ((looking-at "%PDF") '(pdf))
1557 ((looking-at "\367\002") '(dvi))
1558 ((looking-at "AT&TFORM") '(djvu))))))
1559 (setq-local doc-view-doc-type
1560 (car (or (doc-view-intersection name-types content-types)
1561 (when (and name-types content-types)
1562 (error "Conflicting types: name says %s but content says %s"
1563 name-types content-types))
1564 name-types content-types
1565 (error "Cannot determine the document type"))))))
1567 (defun doc-view-set-up-single-converter ()
1568 "Find the right single-page converter for the current document type"
1569 (pcase-let ((`(,conv-function ,type ,extension)
1570 (pcase doc-view-doc-type
1571 (`djvu (list #'doc-view-djvu->tiff-converter-ddjvu 'tiff "tif"))
1572 (_ (list doc-view-pdf->png-converter-function 'png "png")))))
1573 (setq-local doc-view-single-page-converter-function conv-function)
1574 (setq-local doc-view--image-type type)
1575 (setq-local doc-view--image-file-pattern (concat "page-%s." extension))))
1577 ;;;###autoload
1578 (defun doc-view-mode ()
1579 "Major mode in DocView buffers.
1581 DocView Mode is an Emacs document viewer. It displays PDF, PS
1582 and DVI files (as PNG images) in Emacs buffers.
1584 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to
1585 toggle between displaying the document or editing it as text.
1586 \\{doc-view-mode-map}"
1587 (interactive)
1589 (if (= (point-min) (point-max))
1590 ;; The doc is empty or doesn't exist at all, so fallback to
1591 ;; another mode. We used to also check file-exists-p, but this
1592 ;; returns nil for tar members.
1593 (doc-view-fallback-mode)
1595 (let* ((prev-major-mode (if (derived-mode-p 'doc-view-mode)
1596 doc-view-previous-major-mode
1597 (unless (eq major-mode 'fundamental-mode)
1598 major-mode))))
1599 (kill-all-local-variables)
1600 (setq-local doc-view-previous-major-mode prev-major-mode))
1602 (dolist (var doc-view-saved-settings)
1603 (set (make-local-variable (car var)) (cdr var)))
1605 ;; Figure out the document type.
1606 (unless doc-view-doc-type
1607 (doc-view-set-doc-type))
1608 (doc-view-set-up-single-converter)
1610 (doc-view-make-safe-dir doc-view-cache-directory)
1611 ;; Handle compressed files, remote files, files inside archives
1612 (setq-local doc-view-buffer-file-name
1613 (cond
1614 (jka-compr-really-do-compress
1615 ;; FIXME: there's a risk of name conflicts here.
1616 (expand-file-name
1617 (file-name-nondirectory
1618 (file-name-sans-extension buffer-file-name))
1619 doc-view-cache-directory))
1620 ;; Is the file readable by local processes?
1621 ;; We used to use `file-remote-p' but it's unclear what it's
1622 ;; supposed to return nil for things like local files accessed via
1623 ;; `su' or via file://...
1624 ((let ((file-name-handler-alist nil))
1625 (not (and buffer-file-name (file-readable-p buffer-file-name))))
1626 ;; FIXME: there's a risk of name conflicts here.
1627 (expand-file-name
1628 (if buffer-file-name
1629 (file-name-nondirectory buffer-file-name)
1630 (buffer-name))
1631 doc-view-cache-directory))
1632 (t buffer-file-name)))
1633 (when (not (string= doc-view-buffer-file-name buffer-file-name))
1634 (write-region nil nil doc-view-buffer-file-name))
1636 (add-hook 'change-major-mode-hook
1637 (lambda ()
1638 (doc-view-kill-proc)
1639 (remove-overlays (point-min) (point-max) 'doc-view t))
1640 nil t)
1641 (add-hook 'clone-indirect-buffer-hook 'doc-view-clone-buffer-hook nil t)
1642 (add-hook 'kill-buffer-hook 'doc-view-kill-proc nil t)
1644 (remove-overlays (point-min) (point-max) 'doc-view t) ;Just in case.
1645 ;; Keep track of display info ([vh]scroll, page number, overlay,
1646 ;; ...) for each window in which this document is shown.
1647 (add-hook 'image-mode-new-window-functions
1648 'doc-view-new-window-function nil t)
1649 (image-mode-setup-winprops)
1651 (setq-local mode-line-position
1652 '(" P" (:eval (number-to-string (doc-view-current-page)))
1653 "/" (:eval (number-to-string (doc-view-last-page-number)))))
1654 ;; Don't scroll unless the user specifically asked for it.
1655 (setq-local auto-hscroll-mode nil)
1656 (setq-local mwheel-scroll-up-function #'doc-view-scroll-up-or-next-page)
1657 (setq-local mwheel-scroll-down-function
1658 #'doc-view-scroll-down-or-previous-page)
1659 (setq-local cursor-type nil)
1660 (use-local-map doc-view-mode-map)
1661 (add-hook 'after-revert-hook 'doc-view-reconvert-doc nil t)
1662 (setq-local bookmark-make-record-function
1663 #'doc-view-bookmark-make-record)
1664 (setq mode-name "DocView"
1665 buffer-read-only t
1666 major-mode 'doc-view-mode)
1667 (doc-view-initiate-display)
1668 ;; Switch off view-mode explicitly, because doc-view-mode is the
1669 ;; canonical view mode for PDF/PS/DVI files. This could be
1670 ;; switched on automatically depending on the value of
1671 ;; `view-read-only'.
1672 (setq-local view-read-only nil)
1673 (run-mode-hooks 'doc-view-mode-hook)))
1675 (defun doc-view-fallback-mode ()
1676 "Fallback to the previous or next best major mode."
1677 (let ((vars (if (derived-mode-p 'doc-view-mode)
1678 (mapcar (lambda (var) (cons var (symbol-value var)))
1679 '(doc-view-resolution
1680 image-mode-winprops-alist)))))
1681 (if doc-view-previous-major-mode
1682 (funcall doc-view-previous-major-mode)
1683 (let ((auto-mode-alist
1684 (rassq-delete-all
1685 'doc-view-mode-maybe
1686 (rassq-delete-all 'doc-view-mode
1687 (copy-alist auto-mode-alist)))))
1688 (normal-mode)))
1689 (when vars
1690 (setq-local doc-view-saved-settings vars))))
1692 ;;;###autoload
1693 (defun doc-view-mode-maybe ()
1694 "Switch to `doc-view-mode' if possible.
1695 If the required external tools are not available, then fallback
1696 to the next best mode."
1697 (condition-case nil
1698 (doc-view-set-doc-type)
1699 (error (doc-view-fallback-mode)))
1700 (if (doc-view-mode-p doc-view-doc-type)
1701 (doc-view-mode)
1702 (doc-view-fallback-mode)))
1704 ;;;###autoload
1705 (define-minor-mode doc-view-minor-mode
1706 "Toggle displaying buffer via Doc View (Doc View minor mode).
1707 With a prefix argument ARG, enable Doc View minor mode if ARG is
1708 positive, and disable it otherwise. If called from Lisp, enable
1709 the mode if ARG is omitted or nil.
1711 See the command `doc-view-mode' for more information on this mode."
1712 nil " DocView" doc-view-minor-mode-map
1713 :group 'doc-view
1714 (when doc-view-minor-mode
1715 (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
1716 (message
1717 "%s"
1718 (substitute-command-keys
1719 "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document."))))
1721 (defun doc-view-clear-cache ()
1722 "Delete the whole cache (`doc-view-cache-directory')."
1723 (interactive)
1724 (dired-delete-file doc-view-cache-directory 'always))
1726 (defun doc-view-dired-cache ()
1727 "Open `dired' in `doc-view-cache-directory'."
1728 (interactive)
1729 (dired doc-view-cache-directory))
1732 ;;;; Bookmark integration
1734 (declare-function bookmark-make-record-default
1735 "bookmark" (&optional no-file no-context posn))
1736 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
1737 (declare-function bookmark-default-handler "bookmark" (bmk))
1739 (defun doc-view-bookmark-make-record ()
1740 (nconc (bookmark-make-record-default)
1741 `((page . ,(doc-view-current-page))
1742 (handler . doc-view-bookmark-jump))))
1745 ;;;###autoload
1746 (defun doc-view-bookmark-jump (bmk)
1747 ;; This implements the `handler' function interface for record type
1748 ;; returned by `doc-view-bookmark-make-record', which see.
1749 (prog1 (bookmark-default-handler bmk)
1750 (let ((page (bookmark-prop-get bmk 'page)))
1751 (when (not (eq major-mode 'doc-view-mode))
1752 (doc-view-toggle-display))
1753 (with-selected-window
1754 (or (get-buffer-window (current-buffer) 0)
1755 (selected-window))
1756 (doc-view-goto-page page)))))
1759 (provide 'doc-view)
1761 ;; Local Variables:
1762 ;; eval: (outline-minor-mode 1)
1763 ;; End:
1765 ;;; doc-view.el ends here