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