Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / doc-view.el
blobf7cabbe42a7e8ef903dc5cb4875042a8aa60af73
1 ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Tassilo Horn <tassilo@member.fsf.org>
6 ;; Maintainer: Tassilo Horn <tassilo@member.fsf.org>
7 ;; Keywords: files, pdf, ps, dvi
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Requirements:
28 ;; doc-view.el requires GNU Emacs 22.1 or newer. You also need Ghostscript,
29 ;; `dvipdf' (comes with Ghostscript) or `dvipdfm' (comes with teTeX or TeXLive)
30 ;; and `pdftotext', which comes with xpdf (http://www.foolabs.com/xpdf/) or
31 ;; poppler (http://poppler.freedesktop.org/).
33 ;;; Commentary:
35 ;; DocView is a document viewer for Emacs. It converts PDF, PS and DVI files
36 ;; to a set of PNG files, one PNG for each page, and displays the PNG images
37 ;; inside an Emacs buffer. This buffer uses `doc-view-mode' which provides
38 ;; convenient key bindings for browsing the document.
40 ;; To use it simply open a document file with
42 ;; C-x C-f ~/path/to/document RET
44 ;; and the document will be converted and displayed, if your emacs supports png
45 ;; images. With `C-c C-c' you can toggle between the rendered images
46 ;; representation and the source text representation of the document.
48 ;; Since conversion may take some time all the PNG images are cached in a
49 ;; subdirectory of `doc-view-cache-directory' and reused when you want to view
50 ;; that file again. To reconvert a document hit `g' (`doc-view-reconvert-doc')
51 ;; when displaying the document. To delete all cached files use
52 ;; `doc-view-clear-cache'. To open the cache with dired, so that you can tidy
53 ;; it out use `doc-view-dired-cache'.
55 ;; When conversion in underway the first page will be displayed as soon as it
56 ;; is available and the available pages are refreshed every
57 ;; `doc-view-conversion-refresh-interval' seconds. If that variable is nil the
58 ;; pages won't be displayed before conversion of the document finished
59 ;; completely.
61 ;; DocView lets you select a slice of the displayed pages. This slice will be
62 ;; remembered and applied to all pages of the current document. This enables
63 ;; you to cut away the margins of a document to save some space. To select a
64 ;; slice you can use `doc-view-set-slice' (bound to `s s') which will query you
65 ;; for the coordinates of the slice's top-left corner and its width and height.
66 ;; A much more convenient way to do the same is offered by the command
67 ;; `doc-view-set-slice-using-mouse' (bound to `s m'). After invokation you
68 ;; only have to press mouse-1 at the top-left corner and drag it to the
69 ;; bottom-right corner of the desired slice. To reset the slice use
70 ;; `doc-view-reset-slice' (bound to `s r').
72 ;; You can also search within the document. The command `doc-view-search'
73 ;; (bound to `C-s') queries for a search regexp and initializes a list of all
74 ;; matching pages and messages how many match-pages were found. After that you
75 ;; can jump to the next page containing a match with an additional `C-s'. With
76 ;; `C-r' you can do the same, but backwards. To search for a new regexp give a
77 ;; prefix arg to one of the search functions, e.g. by typing `C-u C-s'. The
78 ;; searching works by using a plain text representation of the document. If
79 ;; that doesn't already exist the first invokation of `doc-view-search' (or
80 ;; `doc-view-search-backward') starts the conversion. When that finishes and
81 ;; you're still viewing the document (i.e. you didn't switch to another buffer)
82 ;; you're queried for the regexp then.
84 ;; Dired users can simply hit `v' on a document file. If it's a PS, PDF or DVI
85 ;; it will be opened using `doc-view-mode'.
88 ;;; Configuration:
90 ;; If the images are too small or too big you should set the "-rXXX" option in
91 ;; `doc-view-ghostscript-options' to another value. (The bigger your screen,
92 ;; the higher the value.)
94 ;; This and all other options can be set with the customization interface.
95 ;; Simply do
97 ;; M-x customize-group RET doc-view RET
99 ;; and modify them to your needs.
101 ;;; Todo:
103 ;; - add print command.
104 ;; - share more code with image-mode.
105 ;; - better menu.
106 ;; - Bind slicing to a drag event.
107 ;; - doc-view-fit-doc-to-window and doc-view-fit-window-to-doc?
108 ;; - zoom the region around the cursor (like xdvi).
109 ;; - get rid of the silly arrow in the fringe.
110 ;; - improve anti-aliasing (pdf-utils gets it better).
112 ;;;; About isearch support
114 ;; I tried implementing isearch by setting
115 ;; `isearch-search-fun-function' buffer-locally, but that didn't
116 ;; work too good. The function doing the real search was called
117 ;; endlessly somehow. But even if we'd get that working no real
118 ;; isearch feeling comes up due to the missing match highlighting.
119 ;; Currently I display all lines containing a match in a tooltip and
120 ;; each C-s or C-r jumps directly to the next/previous page with a
121 ;; match. With isearch we could only display the current match. So
122 ;; we had to decide if another C-s jumps to the next page with a
123 ;; match (thus only the first match in a page will be displayed in a
124 ;; tooltip) or to the next match, which would do nothing visible
125 ;; (except the tooltip) if the next match is on the same page.
127 ;; And it's much slower than the current search facility, because
128 ;; isearch really searches for each step forward or backward wheras
129 ;; the current approach searches once and then it knows to which
130 ;; pages to jump.
132 ;; Anyway, if someone with better isearch knowledge wants to give it a try,
133 ;; feel free to do it. --Tassilo
135 ;;; Code:
137 (eval-when-compile (require 'cl))
138 (require 'dired)
139 (require 'image-mode)
140 (require 'jka-compr)
142 ;;;; Customization Options
144 (defgroup doc-view nil
145 "In-buffer viewer for PDF, PostScript and DVI files."
146 :link '(function-link doc-view)
147 :version "22.2"
148 :group 'applications
149 :group 'multimedia
150 :prefix "doc-view-")
152 (defcustom doc-view-ghostscript-program (executable-find "gs")
153 "Program to convert PS and PDF files to PNG."
154 :type 'file
155 :group 'doc-view)
157 (defcustom doc-view-ghostscript-options
158 '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
159 ;; sources.
160 "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
161 "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
162 "A list of options to give to ghostscript."
163 :type '(repeat string)
164 :group 'doc-view)
166 (defcustom doc-view-resolution 100
167 "Dots per inch resolution used to render the documents.
168 Higher values result in larger images."
169 :type 'number
170 :group 'doc-view)
172 (defcustom doc-view-dvipdfm-program (executable-find "dvipdfm")
173 "Program to convert DVI files to PDF.
175 DVI file will be converted to PDF before the resulting PDF is
176 converted to PNG.
178 If this and `doc-view-dvipdf-program' are set,
179 `doc-view-dvipdf-program' will be preferred."
180 :type 'file
181 :group 'doc-view)
183 (defcustom doc-view-dvipdf-program (executable-find "dvipdf")
184 "Program to convert DVI files to PDF.
186 DVI file will be converted to PDF before the resulting PDF is
187 converted to PNG.
189 If this and `doc-view-dvipdfm-program' are set,
190 `doc-view-dvipdf-program' will be preferred."
191 :type 'file
192 :group 'doc-view)
194 (defcustom doc-view-ps2pdf-program (executable-find "ps2pdf")
195 "Program to convert PS files to PDF.
197 PS files will be converted to PDF before searching is possible."
198 :type 'file
199 :group 'doc-view)
201 (defcustom doc-view-pdftotext-program (executable-find "pdftotext")
202 "Program to convert PDF files to plain text.
204 Needed for searching."
205 :type 'file
206 :group 'doc-view)
208 (defcustom doc-view-cache-directory
209 (expand-file-name (format "docview%d" (user-uid))
210 temporary-file-directory)
211 "The base directory, where the PNG images will be saved."
212 :type 'directory
213 :group 'doc-view)
215 (defvar doc-view-conversion-buffer " *doc-view conversion output*"
216 "The buffer where messages from the converter programs go to.")
218 (defcustom doc-view-conversion-refresh-interval 1
219 "Interval in seconds between refreshes of the DocView buffer while converting.
220 After such a refresh newly converted pages will be available for
221 viewing. If set to nil there won't be any refreshes and the
222 pages won't be displayed before conversion of the whole document
223 has finished."
224 :type 'integer
225 :group 'doc-view)
227 ;;;; Internal Variables
229 (defun doc-view-new-window-function (winprops)
230 (let ((ol (image-mode-window-get 'overlay winprops)))
231 (if ol
232 (setq ol (copy-overlay ol))
233 (assert (not (get-char-property (point-min) 'display)))
234 (setq ol (make-overlay (point-min) (point-max) nil t))
235 (overlay-put ol 'doc-view t))
236 (overlay-put ol 'window (car winprops))
237 (image-mode-window-put 'overlay ol winprops)))
239 (defvar doc-view-current-files nil
240 "Only used internally.")
241 (make-variable-buffer-local 'doc-view-current-files)
243 (defvar doc-view-current-converter-processes nil
244 "Only used internally.")
245 (make-variable-buffer-local 'doc-view-current-converter-processes)
247 (defvar doc-view-current-timer nil
248 "Only used internally.")
249 (make-variable-buffer-local 'doc-view-current-timer)
251 (defvar doc-view-current-cache-dir nil
252 "Only used internally.")
253 (make-variable-buffer-local 'doc-view-current-cache-dir)
255 (defvar doc-view-current-search-matches nil
256 "Only used internally.")
257 (make-variable-buffer-local 'doc-view-current-search-matches)
259 (defvar doc-view-pending-cache-flush nil
260 "Only used internally.")
262 (defvar doc-view-previous-major-mode nil
263 "Only used internally.")
265 (defvar doc-view-buffer-file-name nil
266 "Only used internally.
267 The file name used for conversion. Normally it's the same as
268 `buffer-file-name', but for remote files, compressed files and
269 files inside an archive it is a temporary copy of
270 the (uncompressed, extracted) file residing in
271 `doc-view-cache-directory'.")
273 (defvar doc-view-doc-type nil
274 "The type of document in the current buffer.
275 Can be `dvi', `pdf', or `ps'.")
277 ;;;; DocView Keymaps
279 (defvar doc-view-mode-map
280 (let ((map (make-sparse-keymap)))
281 (suppress-keymap map)
282 ;; Navigation in the document
283 (define-key map (kbd "n") 'doc-view-next-page)
284 (define-key map (kbd "p") 'doc-view-previous-page)
285 (define-key map (kbd "<next>") 'forward-page)
286 (define-key map (kbd "<prior>") 'backward-page)
287 (define-key map [remap forward-page] 'doc-view-next-page)
288 (define-key map [remap backward-page] 'doc-view-previous-page)
289 (define-key map (kbd "SPC") 'doc-view-scroll-up-or-next-page)
290 (define-key map (kbd "DEL") 'doc-view-scroll-down-or-previous-page)
291 (define-key map (kbd "M-<") 'doc-view-first-page)
292 (define-key map (kbd "M->") 'doc-view-last-page)
293 (define-key map [remap goto-line] 'doc-view-goto-page)
294 (define-key map [remap scroll-up] 'image-scroll-up)
295 (define-key map [remap scroll-down] 'image-scroll-down)
296 ;; Zoom in/out.
297 (define-key map "+" 'doc-view-enlarge)
298 (define-key map "-" 'doc-view-shrink)
299 ;; Killing/burying the buffer (and the process)
300 (define-key map (kbd "q") 'bury-buffer)
301 (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer)
302 (define-key map (kbd "K") 'doc-view-kill-proc)
303 ;; Slicing the image
304 (define-key map (kbd "s s") 'doc-view-set-slice)
305 (define-key map (kbd "s m") 'doc-view-set-slice-using-mouse)
306 (define-key map (kbd "s r") 'doc-view-reset-slice)
307 ;; Searching
308 (define-key map (kbd "C-s") 'doc-view-search)
309 (define-key map (kbd "<find>") 'doc-view-search)
310 (define-key map (kbd "C-r") 'doc-view-search-backward)
311 ;; Scrolling
312 (define-key map [remap forward-char] 'image-forward-hscroll)
313 (define-key map [remap backward-char] 'image-backward-hscroll)
314 (define-key map [remap move-end-of-line] 'image-eol)
315 (define-key map [remap move-beginning-of-line] 'image-bol)
316 (define-key map [remap next-line] 'image-next-line)
317 (define-key map [remap previous-line] 'image-previous-line)
318 ;; Show the tooltip
319 (define-key map (kbd "C-t") 'doc-view-show-tooltip)
320 ;; Toggle between text and image display or editing
321 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
322 ;; Open a new buffer with doc's text contents
323 (define-key map (kbd "C-c C-t") 'doc-view-open-text)
324 ;; Reconvert the current document
325 (define-key map (kbd "g") 'revert-buffer)
326 (define-key map (kbd "r") 'revert-buffer)
327 map)
328 "Keymap used by `doc-view-mode' when displaying a doc as a set of images.")
330 (easy-menu-define doc-view-menu doc-view-mode-map
331 "Menu for Doc View mode."
332 '("DocView"
333 ["Set Slice" doc-view-set-slice-using-mouse]
334 ["Set Slice (manual)" doc-view-set-slice]
335 ["Reset Slice" doc-view-reset-slice]
336 "---"
337 ["Search" doc-view-search]
338 ["Search Backwards" doc-view-search-backward]
339 ["Toggle display" doc-view-toggle-display]
342 (defvar doc-view-minor-mode-map
343 (let ((map (make-sparse-keymap)))
344 ;; Toggle between text and image display or editing
345 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
346 map)
347 "Keymap used by `doc-minor-view-mode'.")
349 ;;;; Navigation Commands
351 (defmacro doc-view-current-page (&optional win)
352 `(image-mode-window-get 'page ,win))
353 (defmacro doc-view-current-info () `(image-mode-window-get 'info))
354 (defmacro doc-view-current-overlay () `(image-mode-window-get 'overlay))
355 (defmacro doc-view-current-image () `(image-mode-window-get 'image))
356 (defmacro doc-view-current-slice () `(image-mode-window-get 'slice))
358 (defun doc-view-goto-page (page)
359 "View the page given by PAGE."
360 (interactive "nPage: ")
361 (let ((len (length doc-view-current-files)))
362 (if (< page 1)
363 (setq page 1)
364 (when (and (> page len)
365 ;; As long as the converter is running, we don't know
366 ;; how many pages will be available.
367 (null doc-view-current-converter-processes))
368 (setq page len)))
369 (setf (doc-view-current-page) page
370 (doc-view-current-info)
371 (concat
372 (propertize
373 (format "Page %d of %d." page len) 'face 'bold)
374 ;; Tell user if converting isn't finished yet
375 (if doc-view-current-converter-processes
376 " (still converting...)\n"
377 "\n")
378 ;; Display context infos if this page matches the last search
379 (when (and doc-view-current-search-matches
380 (assq page doc-view-current-search-matches))
381 (concat (propertize "Search matches:\n" 'face 'bold)
382 (let ((contexts ""))
383 (dolist (m (cdr (assq page
384 doc-view-current-search-matches)))
385 (setq contexts (concat contexts " - \"" m "\"\n")))
386 contexts)))))
387 ;; Update the buffer
388 ;; We used to find the file name from doc-view-current-files but
389 ;; that's not right if the pages are not generated sequentially
390 ;; or if the page isn't in doc-view-current-files yet.
391 (let ((file (expand-file-name (format "page-%d.png" page)
392 (doc-view-current-cache-dir))))
393 (doc-view-insert-image file :pointer 'arrow)
394 (when (and (not (file-exists-p file))
395 doc-view-current-converter-processes)
396 ;; The PNG file hasn't been generated yet.
397 (doc-view-pdf->png-1 doc-view-buffer-file-name file page
398 (lexical-let ((page page)
399 (win (selected-window)))
400 (lambda ()
401 (and (eq (current-buffer) (window-buffer win))
402 ;; If we changed page in the mean
403 ;; time, don't mess things up.
404 (eq (doc-view-current-page win) page)
405 (with-selected-window win
406 (doc-view-goto-page page))))))))
407 (overlay-put (doc-view-current-overlay)
408 'help-echo (doc-view-current-info))))
410 (defun doc-view-next-page (&optional arg)
411 "Browse ARG pages forward."
412 (interactive "p")
413 (doc-view-goto-page (+ (doc-view-current-page) (or arg 1))))
415 (defun doc-view-previous-page (&optional arg)
416 "Browse ARG pages backward."
417 (interactive "p")
418 (doc-view-goto-page (- (doc-view-current-page) (or arg 1))))
420 (defun doc-view-first-page ()
421 "View the first page."
422 (interactive)
423 (doc-view-goto-page 1))
425 (defun doc-view-last-page ()
426 "View the last page."
427 (interactive)
428 (doc-view-goto-page (length doc-view-current-files)))
430 (defun doc-view-scroll-up-or-next-page ()
431 "Scroll page up if possible, else goto next page."
432 (interactive)
433 (when (= (window-vscroll) (image-scroll-up nil))
434 (let ((cur-page (doc-view-current-page)))
435 (doc-view-next-page)
436 (when (/= cur-page (doc-view-current-page))
437 (image-scroll-down nil)))))
439 (defun doc-view-scroll-down-or-previous-page ()
440 "Scroll page down if possible, else goto previous page."
441 (interactive)
442 (when (= (window-vscroll) (image-scroll-down nil))
443 (let ((cur-page (doc-view-current-page)))
444 (doc-view-previous-page)
445 (when (/= cur-page (doc-view-current-page))
446 (image-scroll-up nil)))))
448 ;;;; Utility Functions
450 (defun doc-view-kill-proc ()
451 "Kill the current converter process(es)."
452 (interactive)
453 (while doc-view-current-converter-processes
454 (ignore-errors ;; Maybe it's dead already?
455 (kill-process (pop doc-view-current-converter-processes))))
456 (when doc-view-current-timer
457 (cancel-timer doc-view-current-timer)
458 (setq doc-view-current-timer nil))
459 (setq mode-line-process nil))
461 (defun doc-view-kill-proc-and-buffer ()
462 "Kill the current converter process and buffer."
463 (interactive)
464 (doc-view-kill-proc)
465 (when (eq major-mode 'doc-view-mode)
466 (kill-buffer (current-buffer))))
468 (defun doc-view-make-safe-dir (dir)
469 (condition-case nil
470 (let ((umask (default-file-modes)))
471 (unwind-protect
472 (progn
473 ;; Create temp files with strict access rights. It's easy to
474 ;; loosen them later, whereas it's impossible to close the
475 ;; time-window of loose permissions otherwise.
476 (set-default-file-modes #o0700)
477 (make-directory dir))
478 ;; Reset the umask.
479 (set-default-file-modes umask)))
480 (file-already-exists
481 (if (file-symlink-p dir)
482 (error "Danger: %s points to a symbolic link" dir))
483 ;; In case it was created earlier with looser rights.
484 ;; We could check the mode info returned by file-attributes, but it's
485 ;; a pain to parse and it may not tell you what we want under
486 ;; non-standard file-systems. So let's just say what we want and let
487 ;; the underlying C code and file-system figure it out.
488 ;; This also ends up checking a bunch of useful conditions: it makes
489 ;; sure we have write-access to the directory and that we own it, thus
490 ;; closing a bunch of security holes.
491 (set-file-modes dir #o0700))))
493 (defun doc-view-current-cache-dir ()
494 "Return the directory where the png files of the current doc should be saved.
495 It's a subdirectory of `doc-view-cache-directory'."
496 (if doc-view-current-cache-dir
497 doc-view-current-cache-dir
498 ;; Try and make sure doc-view-cache-directory exists and is safe.
499 (doc-view-make-safe-dir doc-view-cache-directory)
500 ;; Now compute the subdirectory to use.
501 (setq doc-view-current-cache-dir
502 (file-name-as-directory
503 (expand-file-name
504 (concat (file-name-nondirectory buffer-file-name)
506 (let ((file doc-view-buffer-file-name))
507 (with-temp-buffer
508 (set-buffer-multibyte nil)
509 (insert-file-contents-literally file)
510 (md5 (current-buffer)))))
511 doc-view-cache-directory)))))
513 (defun doc-view-remove-if (predicate list)
514 "Return LIST with all items removed that satisfy PREDICATE."
515 (let (new-list)
516 (dolist (item list (nreverse new-list))
517 (when (not (funcall predicate item))
518 (setq new-list (cons item new-list))))))
520 ;;;###autoload
521 (defun doc-view-mode-p (type)
522 "Return non-nil if image type TYPE is available for `doc-view'.
523 Image types are symbols like `dvi', `postscript' or `pdf'."
524 (and (display-graphic-p)
525 (image-type-available-p 'png)
526 (cond
527 ((eq type 'dvi)
528 (and (doc-view-mode-p 'pdf)
529 (or (and doc-view-dvipdf-program
530 (executable-find doc-view-dvipdf-program))
531 (and doc-view-dvipdfm-program
532 (executable-find doc-view-dvipdfm-program)))))
533 ((or (eq type 'postscript) (eq type 'ps) (eq type 'eps)
534 (eq type 'pdf))
535 (and doc-view-ghostscript-program
536 (executable-find doc-view-ghostscript-program)))
537 (t ;; unknown image type
538 nil))))
540 ;;;; Conversion Functions
542 (defvar doc-view-shrink-factor 1.125)
544 (defun doc-view-enlarge (factor)
545 "Enlarge the document."
546 (interactive (list doc-view-shrink-factor))
547 (set (make-local-variable 'doc-view-resolution)
548 (* factor doc-view-resolution))
549 (doc-view-reconvert-doc))
551 (defun doc-view-shrink (factor)
552 "Shrink the document."
553 (interactive (list doc-view-shrink-factor))
554 (doc-view-enlarge (/ 1.0 factor)))
556 (defun doc-view-reconvert-doc ()
557 "Reconvert the current document.
558 Should be invoked when the cached images aren't up-to-date."
559 (interactive)
560 (doc-view-kill-proc)
561 ;; Clear the old cached files
562 (when (file-exists-p (doc-view-current-cache-dir))
563 (dired-delete-file (doc-view-current-cache-dir) 'always))
564 (doc-view-initiate-display))
566 (defun doc-view-sentinel (proc event)
567 "Generic sentinel for doc-view conversion processes."
568 (if (not (string-match "finished" event))
569 (message "DocView: process %s changed status to %s."
570 (process-name proc) event)
571 (when (buffer-live-p (process-get proc 'buffer))
572 (with-current-buffer (process-get proc 'buffer)
573 (setq doc-view-current-converter-processes
574 (delq proc doc-view-current-converter-processes))
575 (setq mode-line-process
576 (if doc-view-current-converter-processes
577 (format ":%s" (car doc-view-current-converter-processes))))
578 (funcall (process-get proc 'callback))))))
580 (defun doc-view-start-process (name program args callback)
581 ;; Make sure the process is started in an existing directory, (rather than
582 ;; some file-name-handler-managed dir, for example).
583 (let* ((default-directory (if (file-readable-p default-directory)
584 default-directory
585 (expand-file-name "~/")))
586 (proc (apply 'start-process name doc-view-conversion-buffer
587 program args)))
588 (push proc doc-view-current-converter-processes)
589 (setq mode-line-process (list (format ":%s" proc)))
590 (set-process-sentinel proc 'doc-view-sentinel)
591 (process-put proc 'buffer (current-buffer))
592 (process-put proc 'callback callback)))
594 (defun doc-view-dvi->pdf (dvi pdf callback)
595 "Convert DVI to PDF asynchronously and call CALLBACK when finished."
596 ;; Prefer dvipdf over dvipdfm, because the latter has problems if the DVI
597 ;; references and includes other PS files.
598 (if (and doc-view-dvipdf-program
599 (executable-find doc-view-dvipdf-program))
600 (doc-view-start-process "dvi->pdf" doc-view-dvipdf-program
601 (list dvi pdf)
602 callback)
603 (doc-view-start-process "dvi->pdf" doc-view-dvipdfm-program
604 (list "-o" pdf dvi)
605 callback)))
608 (defun doc-view-pdf/ps->png (pdf-ps png)
609 "Convert PDF-PS to PNG asynchronously."
610 (doc-view-start-process
611 "pdf/ps->png" doc-view-ghostscript-program
612 (append doc-view-ghostscript-options
613 (list (format "-r%d" (round doc-view-resolution))
614 (concat "-sOutputFile=" png)
615 pdf-ps))
616 (lambda ()
617 (when doc-view-current-timer
618 (cancel-timer doc-view-current-timer)
619 (setq doc-view-current-timer nil))
620 (doc-view-display (current-buffer) 'force)))
621 ;; Update the displayed pages as soon as they're done generating.
622 (when doc-view-conversion-refresh-interval
623 (setq doc-view-current-timer
624 (run-at-time "1 secs" doc-view-conversion-refresh-interval
625 'doc-view-display
626 (current-buffer)))))
628 (defun doc-view-pdf->png-1 (pdf png page callback)
629 "Convert a PAGE of a PDF file to PNG asynchronously.
630 Call CALLBACK with no arguments when done."
631 (doc-view-start-process
632 "pdf->png-1" doc-view-ghostscript-program
633 (append doc-view-ghostscript-options
634 (list (format "-r%d" (round doc-view-resolution))
635 ;; Sadly, `gs' only supports the page-range
636 ;; for PDF files.
637 (format "-dFirstPage=%d" page)
638 (format "-dLastPage=%d" page)
639 (concat "-sOutputFile=" png)
640 pdf))
641 callback))
643 (defun doc-view-pdf->png (pdf png pages)
644 "Convert a PDF file to PNG asynchronously.
645 Start by converting PAGES, and then the rest."
646 (if (null pages)
647 (doc-view-pdf/ps->png pdf png)
648 ;; We could render several `pages' with a single process if they're
649 ;; (almost) consecutive, but since in 99% of the cases, there'll be only
650 ;; a single page anyway, and of the remaining 1%, few cases will have
651 ;; consecutive pages, it's not worth the trouble.
652 (lexical-let ((pdf pdf) (png png) (rest (cdr pages)))
653 (doc-view-pdf->png-1
654 pdf (format png (car pages)) (car pages)
655 (lambda ()
656 (if rest
657 (doc-view-pdf->png pdf png rest)
658 ;; Yippie, the important pages are done, update the display.
659 (clear-image-cache)
660 ;; Convert the rest of the pages.
661 (doc-view-pdf/ps->png pdf png)))))))
663 (defun doc-view-pdf->txt (pdf txt callback)
664 "Convert PDF to TXT asynchronously and call CALLBACK when finished."
665 (doc-view-start-process "pdf->txt" doc-view-pdftotext-program
666 (list "-raw" pdf txt)
667 callback))
669 (defun doc-view-doc->txt (txt callback)
670 "Convert the current document to text and call CALLBACK when done."
671 (make-directory (doc-view-current-cache-dir) t)
672 (case doc-view-doc-type
673 (pdf
674 ;; Doc is a PDF, so convert it to TXT
675 (doc-view-pdf->txt doc-view-buffer-file-name txt callback))
677 ;; Doc is a PS, so convert it to PDF (which will be converted to
678 ;; TXT thereafter).
679 (lexical-let ((pdf (expand-file-name "doc.pdf"
680 (doc-view-current-cache-dir)))
681 (txt txt)
682 (callback callback))
683 (doc-view-ps->pdf doc-view-buffer-file-name pdf
684 (lambda () (doc-view-pdf->txt pdf txt callback)))))
685 (dvi
686 ;; Doc is a DVI. This means that a doc.pdf already exists in its
687 ;; cache subdirectory.
688 (doc-view-pdf->txt (expand-file-name "doc.pdf"
689 (doc-view-current-cache-dir))
690 txt callback))
691 (t (error "DocView doesn't know what to do"))))
693 (defun doc-view-ps->pdf (ps pdf callback)
694 "Convert PS to PDF asynchronously and call CALLBACK when finished."
695 (doc-view-start-process "ps->pdf" doc-view-ps2pdf-program
696 (list
697 ;; Avoid security problems when rendering files from
698 ;; untrusted sources.
699 "-dSAFER"
700 ;; in-file and out-file
701 ps pdf)
702 callback))
704 (defun doc-view-active-pages ()
705 (let ((pages ()))
706 (dolist (win (get-buffer-window-list (current-buffer) nil 'visible))
707 (let ((page (image-mode-window-get 'page win)))
708 (unless (memq page pages) (push page pages))))
709 pages))
711 (defun doc-view-convert-current-doc ()
712 "Convert `doc-view-buffer-file-name' to a set of png files, one file per page.
713 Those files are saved in the directory given by the function
714 `doc-view-current-cache-dir'."
715 ;; Let stale files still display while we recompute the new ones, so only
716 ;; flush the cache when the conversion is over. One of the reasons why it
717 ;; is important to keep displaying the stale page is so that revert-buffer
718 ;; preserves the horizontal/vertical scroll settings (which are otherwise
719 ;; resets during the redisplay).
720 (setq doc-view-pending-cache-flush t)
721 (let ((png-file (expand-file-name "page-%d.png"
722 (doc-view-current-cache-dir))))
723 (make-directory (doc-view-current-cache-dir) t)
724 (case doc-view-doc-type
725 (dvi
726 ;; DVI files have to be converted to PDF before Ghostscript can process
727 ;; it.
728 (lexical-let
729 ((pdf (expand-file-name "doc.pdf" doc-view-current-cache-dir))
730 (png-file png-file))
731 (doc-view-dvi->pdf doc-view-buffer-file-name pdf
732 (lambda () (doc-view-pdf/ps->png pdf png-file)))))
733 (pdf
734 (let ((pages (doc-view-active-pages)))
735 ;; Convert PDF to PNG images starting with the active pages.
736 (doc-view-pdf->png doc-view-buffer-file-name png-file pages)))
738 ;; Convert to PNG images.
739 (doc-view-pdf/ps->png doc-view-buffer-file-name png-file)))))
741 ;;;; Slicing
743 (defun doc-view-set-slice (x y width height)
744 "Set the slice of the images that should be displayed.
745 You can use this function to tell doc-view not to display the
746 margins of the document. It prompts for the top-left corner (X
747 and Y) of the slice to display and its WIDTH and HEIGHT.
749 See `doc-view-set-slice-using-mouse' for a more convenient way to
750 do that. To reset the slice use `doc-view-reset-slice'."
751 (interactive
752 (let* ((size (image-size (doc-view-current-image) t))
753 (a (read-number (format "Top-left X (0..%d): " (car size))))
754 (b (read-number (format "Top-left Y (0..%d): " (cdr size))))
755 (c (read-number (format "Width (0..%d): " (- (car size) a))))
756 (d (read-number (format "Height (0..%d): " (- (cdr size) b)))))
757 (list a b c d)))
758 (setf (doc-view-current-slice) (list x y width height))
759 ;; Redisplay
760 (doc-view-goto-page (doc-view-current-page)))
762 (defun doc-view-set-slice-using-mouse ()
763 "Set the slice of the images that should be displayed.
764 You set the slice by pressing mouse-1 at its top-left corner and
765 dragging it to its bottom-right corner. See also
766 `doc-view-set-slice' and `doc-view-reset-slice'."
767 (interactive)
768 (let (x y w h done)
769 (while (not done)
770 (let ((e (read-event
771 (concat "Press mouse-1 at the top-left corner and "
772 "drag it to the bottom-right corner!"))))
773 (when (eq (car e) 'drag-mouse-1)
774 (setq x (car (posn-object-x-y (event-start e))))
775 (setq y (cdr (posn-object-x-y (event-start e))))
776 (setq w (- (car (posn-object-x-y (event-end e))) x))
777 (setq h (- (cdr (posn-object-x-y (event-end e))) y))
778 (setq done t))))
779 (doc-view-set-slice x y w h)))
781 (defun doc-view-reset-slice ()
782 "Reset the current slice.
783 After calling this function whole pages will be visible again."
784 (interactive)
785 (setf (doc-view-current-slice) nil)
786 ;; Redisplay
787 (doc-view-goto-page (doc-view-current-page)))
789 ;;;; Display
791 (defun doc-view-insert-image (file &rest args)
792 "Insert the given png FILE.
793 ARGS is a list of image descriptors."
794 (when doc-view-pending-cache-flush
795 (clear-image-cache)
796 (setq doc-view-pending-cache-flush nil))
797 (let ((ol (doc-view-current-overlay))
798 (image (if (and file (file-readable-p file))
799 (apply 'create-image file 'png nil args)))
800 (slice (doc-view-current-slice)))
801 (setf (doc-view-current-image) image)
802 (move-overlay ol (point-min) (point-max))
803 (overlay-put ol 'display
804 (cond
805 (image
806 (if slice
807 (list (cons 'slice slice) image)
808 image))
809 ;; We're trying to display a page that doesn't exist.
810 (doc-view-current-converter-processes
811 ;; Maybe the page doesn't exist *yet*.
812 "Cannot display this page (yet)!")
814 ;; Typically happens if the conversion process somehow
815 ;; failed. Better not signal an error here because it
816 ;; could prevent a subsequent reconversion from fixing
817 ;; the problem.
818 (concat "Cannot display this page!\n"
819 "Maybe because of a conversion failure!"))))
820 (let ((win (overlay-get ol 'window)))
821 (if (stringp (overlay-get ol 'display))
822 (progn ;Make sure the text is not scrolled out of view.
823 (set-window-hscroll win 0)
824 (set-window-vscroll win 0))
825 (let ((hscroll (image-mode-window-get 'hscroll win))
826 (vscroll (image-mode-window-get 'vscroll win)))
827 ;; Reset scroll settings, in case they were changed.
828 (if hscroll (set-window-hscroll win hscroll))
829 (if vscroll (set-window-vscroll win vscroll)))))))
831 (defun doc-view-sort (a b)
832 "Return non-nil if A should be sorted before B.
833 Predicate for sorting `doc-view-current-files'."
834 (or (< (length a) (length b))
835 (and (= (length a) (length b))
836 (string< a b))))
838 (defun doc-view-display (buffer &optional force)
839 "Start viewing the document in BUFFER.
840 If FORCE is non-nil, start viewing even if the document does not
841 have the page we want to view."
842 (with-current-buffer buffer
843 (let ((prev-pages doc-view-current-files))
844 (setq doc-view-current-files
845 (sort (directory-files (doc-view-current-cache-dir) t
846 "page-[0-9]+\\.png" t)
847 'doc-view-sort))
848 (dolist (win (or (get-buffer-window-list buffer nil t)
849 (list (selected-window))))
850 (let* ((page (doc-view-current-page win))
851 (pagefile (expand-file-name (format "page-%d.png" page)
852 (doc-view-current-cache-dir))))
853 (when (or force
854 (and (not (member pagefile prev-pages))
855 (member pagefile doc-view-current-files)))
856 (with-selected-window win
857 (assert (eq (current-buffer) buffer))
858 (doc-view-goto-page page))))))))
860 (defun doc-view-buffer-message ()
861 ;; Only show this message initially, not when refreshing the buffer (in which
862 ;; case it's better to keep displaying the "stale" page while computing
863 ;; the fresh new ones).
864 (unless (overlay-get (doc-view-current-overlay) 'display)
865 (overlay-put (doc-view-current-overlay) 'display
866 (concat (propertize "Welcome to DocView!" 'face 'bold)
867 "\n"
869 If you see this buffer it means that the document you want to view is being
870 converted to PNG and the conversion of the first page hasn't finished yet or
871 `doc-view-conversion-refresh-interval' is set to nil.
873 For now these keys are useful:
875 `q' : Bury this buffer. Conversion will go on in background.
876 `k' : Kill the conversion process and this buffer.
877 `K' : Kill the conversion process.\n"))))
879 (defun doc-view-show-tooltip ()
880 (interactive)
881 (tooltip-show (doc-view-current-info)))
883 (defun doc-view-open-text ()
884 "Open a buffer with the current doc's contents as text."
885 (interactive)
886 (if doc-view-current-converter-processes
887 (message "DocView: please wait till conversion finished.")
888 (let ((txt (expand-file-name "doc.txt" (doc-view-current-cache-dir))))
889 (if (file-readable-p txt)
890 (find-file txt)
891 (doc-view-doc->txt txt 'doc-view-open-text)))))
893 ;;;;; Toggle between editing and viewing
896 (defun doc-view-toggle-display ()
897 "Toggle between editing a document as text or viewing it."
898 (interactive)
899 (if (eq major-mode 'doc-view-mode)
900 ;; Switch to editing mode
901 (progn
902 (doc-view-kill-proc)
903 (setq buffer-read-only nil)
904 (remove-overlays (point-min) (point-max) 'doc-view t)
905 (set (make-local-variable 'image-mode-winprops-alist) t)
906 ;; Switch to the previously used major mode or fall back to fundamental
907 ;; mode.
908 (if doc-view-previous-major-mode
909 (funcall doc-view-previous-major-mode)
910 (fundamental-mode))
911 (doc-view-minor-mode 1))
912 ;; Switch to doc-view-mode
913 (when (and (buffer-modified-p)
914 (y-or-n-p "The buffer has been modified. Save the changes? "))
915 (save-buffer))
916 (doc-view-mode)))
918 ;;;; Searching
921 (defun doc-view-search-internal (regexp file)
922 "Return a list of FILE's pages that contain text matching REGEXP.
923 The value is an alist of the form (PAGE CONTEXTS) where PAGE is
924 the pagenumber and CONTEXTS are all lines of text containing a match."
925 (with-temp-buffer
926 (insert-file-contents file)
927 (let ((page 1)
928 (lastpage 1)
929 matches)
930 (while (re-search-forward (concat "\\(?:\\([\f]\\)\\|\\("
931 regexp "\\)\\)") nil t)
932 (when (match-string 1) (setq page (1+ page)))
933 (when (match-string 2)
934 (if (/= page lastpage)
935 (push (cons page
936 (list (buffer-substring
937 (line-beginning-position)
938 (line-end-position))))
939 matches)
940 (setq matches (cons
941 (append
943 ;; This page already is a match.
944 (car matches)
945 ;; This is the first match on page.
946 (list page))
947 (list (buffer-substring
948 (line-beginning-position)
949 (line-end-position))))
950 (cdr matches))))
951 (setq lastpage page)))
952 (nreverse matches))))
954 (defun doc-view-search-no-of-matches (list)
955 "Extract the number of matches from the search result LIST."
956 (let ((no 0))
957 (dolist (p list)
958 (setq no (+ no (1- (length p)))))
959 no))
961 (defun doc-view-search-backward (new-query)
962 "Call `doc-view-search' for backward search.
963 If prefix NEW-QUERY is given, ask for a new regexp."
964 (interactive "P")
965 (doc-view-search new-query t))
967 (defun doc-view-search (new-query &optional backward)
968 "Jump to the next match or initiate a new search if NEW-QUERY is given.
969 If the current document hasn't been transformed to plain text
970 till now do that first.
971 If BACKWARD is non-nil, jump to the previous match."
972 (interactive "P")
973 (if (and (not new-query)
974 doc-view-current-search-matches)
975 (if backward
976 (doc-view-search-previous-match 1)
977 (doc-view-search-next-match 1))
978 ;; New search, so forget the old results.
979 (setq doc-view-current-search-matches nil)
980 (let ((txt (expand-file-name "doc.txt"
981 (doc-view-current-cache-dir))))
982 (if (file-readable-p txt)
983 (progn
984 (setq doc-view-current-search-matches
985 (doc-view-search-internal
986 (read-from-minibuffer "Regexp: ")
987 txt))
988 (message "DocView: search yielded %d matches."
989 (doc-view-search-no-of-matches
990 doc-view-current-search-matches)))
991 ;; We must convert to TXT first!
992 (if doc-view-current-converter-processes
993 (message "DocView: please wait till conversion finished.")
994 (doc-view-doc->txt txt (lambda () (doc-view-search nil))))))))
996 (defun doc-view-search-next-match (arg)
997 "Go to the ARGth next matching page."
998 (interactive "p")
999 (let* ((next-pages (doc-view-remove-if
1000 (lambda (i) (<= (car i) (doc-view-current-page)))
1001 doc-view-current-search-matches))
1002 (page (car (nth (1- arg) next-pages))))
1003 (if page
1004 (doc-view-goto-page page)
1005 (when (and
1006 doc-view-current-search-matches
1007 (y-or-n-p "No more matches after current page. Wrap to first match? "))
1008 (doc-view-goto-page (caar doc-view-current-search-matches))))))
1010 (defun doc-view-search-previous-match (arg)
1011 "Go to the ARGth previous matching page."
1012 (interactive "p")
1013 (let* ((prev-pages (doc-view-remove-if
1014 (lambda (i) (>= (car i) (doc-view-current-page)))
1015 doc-view-current-search-matches))
1016 (page (car (nth (1- arg) (nreverse prev-pages)))))
1017 (if page
1018 (doc-view-goto-page page)
1019 (when (and
1020 doc-view-current-search-matches
1021 (y-or-n-p "No more matches before current page. Wrap to last match? "))
1022 (doc-view-goto-page (caar (last doc-view-current-search-matches)))))))
1024 ;;;; User interface commands and the mode
1026 ;; (put 'doc-view-mode 'mode-class 'special)
1028 (defun doc-view-already-converted-p ()
1029 "Return non-nil if the current doc was already converted."
1030 (and (file-exists-p (doc-view-current-cache-dir))
1031 (> (length (directory-files (doc-view-current-cache-dir) nil "\\.png$")) 0)))
1033 (defun doc-view-initiate-display ()
1034 ;; Switch to image display if possible
1035 (if (doc-view-mode-p doc-view-doc-type)
1036 (progn
1037 (doc-view-buffer-message)
1038 (setf (doc-view-current-page) (or (doc-view-current-page) 1))
1039 (if (doc-view-already-converted-p)
1040 (progn
1041 (message "DocView: using cached files!")
1042 (doc-view-display (current-buffer) 'force))
1043 (doc-view-convert-current-doc))
1044 (message
1045 "%s"
1046 (substitute-command-keys
1047 (concat "Type \\[doc-view-toggle-display] to toggle between "
1048 "editing or viewing the document."))))
1049 (message
1050 "%s"
1051 (substitute-command-keys
1052 (concat "No image (png) support available or some conversion utility for "
1053 (file-name-extension doc-view-buffer-file-name)" files is missing. "
1054 "Type \\[doc-view-toggle-display] to switch to an editing mode or "
1055 "\\[doc-view-open-text] to open a buffer showing the doc as text.")))))
1057 (defvar bookmark-make-record-function)
1059 (defun doc-view-clone-buffer-hook ()
1060 ;; FIXME: There are several potential problems linked with reconversion
1061 ;; and auto-revert when we have indirect buffers because they share their
1062 ;; /tmp cache directory. This sharing is good (you'd rather not reconvert
1063 ;; for each clone), but that means that clones need to collaborate a bit.
1064 ;; I guess it mostly means: detect when a reconversion process is already
1065 ;; running, and run the sentinel in all clones.
1067 ;; Maybe the clones should really have a separate /tmp directory
1068 ;; so they could have a different resolution and you could use clones
1069 ;; for zooming.
1070 (remove-overlays (point-min) (point-max) 'doc-view t)
1071 (if (consp image-mode-winprops-alist) (setq image-mode-winprops-alist nil)))
1073 (defun doc-view-intersection (l1 l2)
1074 (let ((l ()))
1075 (dolist (x l1) (if (memq x l2) (push x l)))
1078 ;;;###autoload
1079 (defun doc-view-mode ()
1080 "Major mode in DocView buffers.
1081 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to
1082 toggle between displaying the document or editing it as text.
1083 \\{doc-view-mode-map}"
1084 (interactive)
1086 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode)
1087 doc-view-previous-major-mode
1088 major-mode)))
1089 (kill-all-local-variables)
1090 (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode))
1092 ;; Figure out the document type.
1093 (let ((name-types
1094 (when buffer-file-name
1095 (cdr (assoc (file-name-extension buffer-file-name)
1096 '(("dvi" dvi)
1097 ("pdf" pdf)
1098 ("epdf" pdf)
1099 ("ps" ps)
1100 ("eps" ps))))))
1101 (content-types
1102 (save-excursion
1103 (goto-char (point-min))
1104 (cond
1105 ((looking-at "%!") '(ps))
1106 ((looking-at "%PDF") '(pdf))
1107 ((looking-at "\367\002") '(dvi))))))
1108 (set (make-local-variable 'doc-view-doc-type)
1109 (car (or (doc-view-intersection name-types content-types)
1110 (when (and name-types content-types)
1111 (error "Conflicting types: name says %s but content says %s"
1112 name-types content-types))
1113 name-types content-types
1114 (error "Cannot determine the document type")))))
1116 (doc-view-make-safe-dir doc-view-cache-directory)
1117 ;; Handle compressed files, remote files, files inside archives
1118 (set (make-local-variable 'doc-view-buffer-file-name)
1119 (cond
1120 (jka-compr-really-do-compress
1121 (expand-file-name
1122 (file-name-nondirectory
1123 (file-name-sans-extension buffer-file-name))
1124 doc-view-cache-directory))
1125 ;; Is the file readable by local processes?
1126 ;; We used to use `file-remote-p' but it's unclear what it's
1127 ;; supposed to return nil for things like local files accessed via
1128 ;; `su' or via file://...
1129 ((let ((file-name-handler-alist nil))
1130 (not (file-readable-p buffer-file-name)))
1131 (expand-file-name
1132 (file-name-nondirectory buffer-file-name)
1133 doc-view-cache-directory))
1134 (t buffer-file-name)))
1135 (when (not (string= doc-view-buffer-file-name buffer-file-name))
1136 (write-region nil nil doc-view-buffer-file-name))
1138 (add-hook 'change-major-mode-hook
1139 (lambda ()
1140 (doc-view-kill-proc)
1141 (remove-overlays (point-min) (point-max) 'doc-view t))
1142 nil t)
1143 (add-hook 'clone-indirect-buffer-hook 'doc-view-clone-buffer-hook nil t)
1144 (add-hook 'kill-buffer 'doc-view-kill-proc nil t)
1146 (remove-overlays (point-min) (point-max) 'doc-view t) ;Just in case.
1147 ;; Keep track of display info ([vh]scroll, page number, overlay, ...)
1148 ;; for each window in which this document is shown.
1149 (add-hook 'image-mode-new-window-functions
1150 'doc-view-new-window-function nil t)
1151 (image-mode-setup-winprops)
1153 (set (make-local-variable 'mode-line-position)
1154 '(" P" (:eval (number-to-string (doc-view-current-page)))
1155 "/" (:eval (number-to-string (length doc-view-current-files)))))
1156 ;; Don't scroll unless the user specifically asked for it.
1157 (set (make-local-variable 'auto-hscroll-mode) nil)
1158 (set (make-local-variable 'cursor-type) nil)
1159 (use-local-map doc-view-mode-map)
1160 (set (make-local-variable 'after-revert-hook) 'doc-view-reconvert-doc)
1161 (set (make-local-variable 'bookmark-make-record-function)
1162 'doc-view-bookmark-make-record)
1163 (setq mode-name "DocView"
1164 buffer-read-only t
1165 major-mode 'doc-view-mode)
1166 (doc-view-initiate-display)
1167 (run-mode-hooks 'doc-view-mode-hook))
1169 ;;;###autoload
1170 (define-minor-mode doc-view-minor-mode
1171 "Toggle Doc view minor mode.
1172 With arg, turn Doc view minor mode on if arg is positive, off otherwise.
1173 See the command `doc-view-mode' for more information on this mode."
1174 nil " DocView" doc-view-minor-mode-map
1175 :group 'doc-view
1176 (when doc-view-minor-mode
1177 (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
1178 (message
1179 "%s"
1180 (substitute-command-keys
1181 "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document."))))
1183 (defun doc-view-clear-cache ()
1184 "Delete the whole cache (`doc-view-cache-directory')."
1185 (interactive)
1186 (dired-delete-file doc-view-cache-directory 'always))
1188 (defun doc-view-dired-cache ()
1189 "Open `dired' in `doc-view-cache-directory'."
1190 (interactive)
1191 (dired doc-view-cache-directory))
1194 ;;;; Bookmark integration
1196 (declare-function bookmark-buffer-file-name "bookmark" ())
1197 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
1199 (defun doc-view-bookmark-make-record ()
1200 `((filename . ,(bookmark-buffer-file-name))
1201 (page . ,(doc-view-current-page))
1202 (handler . doc-view-bookmark-jump)))
1205 ;;;###autoload
1206 (defun doc-view-bookmark-jump (bmk)
1207 ;; This implements the `handler' function interface for record type
1208 ;; returned by `doc-view-bookmark-make-record', which see.
1209 (let ((filename (bookmark-prop-get bmk 'filename))
1210 (page (bookmark-prop-get bmk 'page)))
1211 (with-current-buffer (find-file-noselect filename)
1212 (when (not (eq major-mode 'doc-view-mode))
1213 (doc-view-toggle-display))
1214 (with-selected-window
1215 (or (get-buffer-window (current-buffer) 0)
1216 (selected-window))
1217 (doc-view-goto-page page))
1218 `((buffer ,(current-buffer)) (position ,1)))))
1221 (provide 'doc-view)
1223 ;; Local Variables:
1224 ;; mode: outline-minor
1225 ;; End:
1227 ;; arch-tag: 5d6e5c5e-095f-489e-b4e4-1ca90a7d79be
1228 ;;; doc-view.el ends here