cc-engine.el (c-beginning-of-statement-1): Enhance to parse case clauses
[emacs.git] / lisp / doc-view.el
blob13ebe3e92f902237b146a1bb2e440bc8298f370f
1 ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs
3 ;; Copyright (C) 2007, 2008, 2009 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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Requirements:
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/).
31 ;;; Commentary:
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.
38 ;; To use it simply open a document file with
40 ;; C-x C-f ~/path/to/document RET
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.
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'.
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.
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').
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.
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'.
86 ;;; Configuration:
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.)
92 ;; This and all other options can be set with the customization interface.
93 ;; Simply do
95 ;; M-x customize-group RET doc-view RET
97 ;; and modify them to your needs.
99 ;;; Todo:
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).
110 ;;;; About isearch support
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.
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.
130 ;; Anyway, if someone with better isearch knowledge wants to give it a try,
131 ;; feel free to do it. --Tassilo
133 ;;; Code:
135 (eval-when-compile (require 'cl))
136 (require 'dired)
137 (require 'image-mode)
138 (require 'jka-compr)
140 ;;;; Customization Options
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-")
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)
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)
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)
170 (defcustom doc-view-dvipdfm-program (executable-find "dvipdfm")
171 "Program to convert DVI files to PDF.
173 DVI file will be converted to PDF before the resulting PDF is
174 converted to PNG.
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)
181 (defcustom doc-view-dvipdf-program (executable-find "dvipdf")
182 "Program to convert DVI files to PDF.
184 DVI file will be converted to PDF before the resulting PDF is
185 converted to PNG.
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)
192 (defcustom doc-view-ps2pdf-program (executable-find "ps2pdf")
193 "Program to convert PS files to PDF.
195 PS files will be converted to PDF before searching is possible."
196 :type 'file
197 :group 'doc-view)
199 (defcustom doc-view-pdftotext-program (executable-find "pdftotext")
200 "Program to convert PDF files to plain text.
202 Needed for searching."
203 :type 'file
204 :group 'doc-view)
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)
213 (defvar doc-view-conversion-buffer " *doc-view conversion output*"
214 "The buffer where messages from the converter programs go to.")
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)
225 ;;;; Internal Variables
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)))
237 (defvar doc-view-current-files nil
238 "Only used internally.")
239 (make-variable-buffer-local 'doc-view-current-files)
241 (defvar doc-view-current-converter-processes nil
242 "Only used internally.")
243 (make-variable-buffer-local 'doc-view-current-converter-processes)
245 (defvar doc-view-current-timer nil
246 "Only used internally.")
247 (make-variable-buffer-local 'doc-view-current-timer)
249 (defvar doc-view-current-cache-dir nil
250 "Only used internally.")
251 (make-variable-buffer-local 'doc-view-current-cache-dir)
253 (defvar doc-view-current-search-matches nil
254 "Only used internally.")
255 (make-variable-buffer-local 'doc-view-current-search-matches)
257 (defvar doc-view-pending-cache-flush nil
258 "Only used internally.")
260 (defvar doc-view-previous-major-mode nil
261 "Only used internally.")
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'.")
271 (defvar doc-view-doc-type nil
272 "The type of document in the current buffer.
273 Can be `dvi', `pdf', or `ps'.")
275 ;;;; DocView Keymaps
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.")
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]
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'.")
337 ;;;; Navigation Commands
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))
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 (hscroll (window-hscroll)))
351 (if (< page 1)
352 (setq page 1)
353 (when (and (> page len)
354 ;; As long as the converter is running, we don't know
355 ;; how many pages will be available.
356 (null doc-view-current-converter-processes))
357 (setq page len)))
358 (setf (doc-view-current-page) page
359 (doc-view-current-info)
360 (concat
361 (propertize
362 (format "Page %d of %d." page len) 'face 'bold)
363 ;; Tell user if converting isn't finished yet
364 (if doc-view-current-converter-processes
365 " (still converting...)\n"
366 "\n")
367 ;; Display context infos if this page matches the last search
368 (when (and doc-view-current-search-matches
369 (assq page doc-view-current-search-matches))
370 (concat (propertize "Search matches:\n" 'face 'bold)
371 (let ((contexts ""))
372 (dolist (m (cdr (assq page
373 doc-view-current-search-matches)))
374 (setq contexts (concat contexts " - \"" m "\"\n")))
375 contexts)))))
376 ;; Update the buffer
377 ;; We used to find the file name from doc-view-current-files but
378 ;; that's not right if the pages are not generated sequentially
379 ;; or if the page isn't in doc-view-current-files yet.
380 (let ((file (expand-file-name (format "page-%d.png" page)
381 (doc-view-current-cache-dir))))
382 (doc-view-insert-image file :pointer 'arrow)
383 (set-window-hscroll (selected-window) hscroll)
384 (when (and (not (file-exists-p file))
385 doc-view-current-converter-processes)
386 ;; The PNG file hasn't been generated yet.
387 (doc-view-pdf->png-1 doc-view-buffer-file-name file page
388 (lexical-let ((page page)
389 (win (selected-window)))
390 (lambda ()
391 (and (eq (current-buffer) (window-buffer win))
392 ;; If we changed page in the mean
393 ;; time, don't mess things up.
394 (eq (doc-view-current-page win) page)
395 (with-selected-window win
396 (doc-view-goto-page page))))))))
397 (overlay-put (doc-view-current-overlay)
398 'help-echo (doc-view-current-info))))
400 (defun doc-view-next-page (&optional arg)
401 "Browse ARG pages forward."
402 (interactive "p")
403 (doc-view-goto-page (+ (doc-view-current-page) (or arg 1))))
405 (defun doc-view-previous-page (&optional arg)
406 "Browse ARG pages backward."
407 (interactive "p")
408 (doc-view-goto-page (- (doc-view-current-page) (or arg 1))))
410 (defun doc-view-first-page ()
411 "View the first page."
412 (interactive)
413 (doc-view-goto-page 1))
415 (defun doc-view-last-page ()
416 "View the last page."
417 (interactive)
418 (doc-view-goto-page (length doc-view-current-files)))
420 (defun doc-view-scroll-up-or-next-page ()
421 "Scroll page up if possible, else goto next page."
422 (interactive)
423 (let ((hscroll (window-hscroll))
424 (cur-page (doc-view-current-page)))
425 (when (= (window-vscroll) (image-scroll-up nil))
426 (doc-view-next-page)
427 (when (/= cur-page (doc-view-current-page))
428 (image-bob)
429 (image-bol 1))
430 (set-window-hscroll (selected-window) hscroll))))
432 (defun doc-view-scroll-down-or-previous-page ()
433 "Scroll page down if possible, else goto previous page."
434 (interactive)
435 (let ((hscroll (window-hscroll))
436 (cur-page (doc-view-current-page)))
437 (when (= (window-vscroll) (image-scroll-down nil))
438 (doc-view-previous-page)
439 (when (/= cur-page (doc-view-current-page))
440 (image-eob)
441 (image-bol 1))
442 (set-window-hscroll (selected-window) hscroll))))
444 ;;;; Utility Functions
446 (defun doc-view-kill-proc ()
447 "Kill the current converter process(es)."
448 (interactive)
449 (while doc-view-current-converter-processes
450 (ignore-errors ;; Maybe it's dead already?
451 (kill-process (pop doc-view-current-converter-processes))))
452 (when doc-view-current-timer
453 (cancel-timer doc-view-current-timer)
454 (setq doc-view-current-timer nil))
455 (setq mode-line-process nil))
457 (defun doc-view-kill-proc-and-buffer ()
458 "Kill the current converter process and buffer."
459 (interactive)
460 (doc-view-kill-proc)
461 (when (eq major-mode 'doc-view-mode)
462 (kill-buffer (current-buffer))))
464 (defun doc-view-make-safe-dir (dir)
465 (condition-case nil
466 (let ((umask (default-file-modes)))
467 (unwind-protect
468 (progn
469 ;; Create temp files with strict access rights. It's easy to
470 ;; loosen them later, whereas it's impossible to close the
471 ;; time-window of loose permissions otherwise.
472 (set-default-file-modes #o0700)
473 (make-directory dir))
474 ;; Reset the umask.
475 (set-default-file-modes umask)))
476 (file-already-exists
477 (if (file-symlink-p dir)
478 (error "Danger: %s points to a symbolic link" dir))
479 ;; In case it was created earlier with looser rights.
480 ;; We could check the mode info returned by file-attributes, but it's
481 ;; a pain to parse and it may not tell you what we want under
482 ;; non-standard file-systems. So let's just say what we want and let
483 ;; the underlying C code and file-system figure it out.
484 ;; This also ends up checking a bunch of useful conditions: it makes
485 ;; sure we have write-access to the directory and that we own it, thus
486 ;; closing a bunch of security holes.
487 (set-file-modes dir #o0700))))
489 (defun doc-view-current-cache-dir ()
490 "Return the directory where the png files of the current doc should be saved.
491 It's a subdirectory of `doc-view-cache-directory'."
492 (if doc-view-current-cache-dir
493 doc-view-current-cache-dir
494 ;; Try and make sure doc-view-cache-directory exists and is safe.
495 (doc-view-make-safe-dir doc-view-cache-directory)
496 ;; Now compute the subdirectory to use.
497 (setq doc-view-current-cache-dir
498 (file-name-as-directory
499 (expand-file-name
500 (concat (file-name-nondirectory buffer-file-name)
502 (let ((file doc-view-buffer-file-name))
503 (with-temp-buffer
504 (set-buffer-multibyte nil)
505 (insert-file-contents-literally file)
506 (md5 (current-buffer)))))
507 doc-view-cache-directory)))))
509 (defun doc-view-remove-if (predicate list)
510 "Return LIST with all items removed that satisfy PREDICATE."
511 (let (new-list)
512 (dolist (item list (nreverse new-list))
513 (when (not (funcall predicate item))
514 (setq new-list (cons item new-list))))))
516 ;;;###autoload
517 (defun doc-view-mode-p (type)
518 "Return non-nil if image type TYPE is available for `doc-view'.
519 Image types are symbols like `dvi', `postscript' or `pdf'."
520 (and (display-graphic-p)
521 (image-type-available-p 'png)
522 (cond
523 ((eq type 'dvi)
524 (and (doc-view-mode-p 'pdf)
525 (or (and doc-view-dvipdf-program
526 (executable-find doc-view-dvipdf-program))
527 (and doc-view-dvipdfm-program
528 (executable-find doc-view-dvipdfm-program)))))
529 ((or (eq type 'postscript) (eq type 'ps) (eq type 'eps)
530 (eq type 'pdf))
531 (and doc-view-ghostscript-program
532 (executable-find doc-view-ghostscript-program)))
533 (t ;; unknown image type
534 nil))))
536 ;;;; Conversion Functions
538 (defvar doc-view-shrink-factor 1.125)
540 (defun doc-view-enlarge (factor)
541 "Enlarge the document."
542 (interactive (list doc-view-shrink-factor))
543 (set (make-local-variable 'doc-view-resolution)
544 (* factor doc-view-resolution))
545 (doc-view-reconvert-doc))
547 (defun doc-view-shrink (factor)
548 "Shrink the document."
549 (interactive (list doc-view-shrink-factor))
550 (doc-view-enlarge (/ 1.0 factor)))
552 (defun doc-view-reconvert-doc ()
553 "Reconvert the current document.
554 Should be invoked when the cached images aren't up-to-date."
555 (interactive)
556 (doc-view-kill-proc)
557 ;; Clear the old cached files
558 (when (file-exists-p (doc-view-current-cache-dir))
559 (dired-delete-file (doc-view-current-cache-dir) 'always))
560 (doc-view-initiate-display))
562 (defun doc-view-sentinel (proc event)
563 "Generic sentinel for doc-view conversion processes."
564 (if (not (string-match "finished" event))
565 (message "DocView: process %s changed status to %s."
566 (process-name proc) event)
567 (when (buffer-live-p (process-get proc 'buffer))
568 (with-current-buffer (process-get proc 'buffer)
569 (setq doc-view-current-converter-processes
570 (delq proc doc-view-current-converter-processes))
571 (setq mode-line-process
572 (if doc-view-current-converter-processes
573 (format ":%s" (car doc-view-current-converter-processes))))
574 (funcall (process-get proc 'callback))))))
576 (defun doc-view-start-process (name program args callback)
577 ;; Make sure the process is started in an existing directory, (rather than
578 ;; some file-name-handler-managed dir, for example).
579 (let* ((default-directory (if (file-readable-p default-directory)
580 default-directory
581 (expand-file-name "~/")))
582 (proc (apply 'start-process name doc-view-conversion-buffer
583 program args)))
584 (push proc doc-view-current-converter-processes)
585 (setq mode-line-process (list (format ":%s" proc)))
586 (set-process-sentinel proc 'doc-view-sentinel)
587 (process-put proc 'buffer (current-buffer))
588 (process-put proc 'callback callback)))
590 (defun doc-view-dvi->pdf (dvi pdf callback)
591 "Convert DVI to PDF asynchronously and call CALLBACK when finished."
592 ;; Prefer dvipdf over dvipdfm, because the latter has problems if the DVI
593 ;; references and includes other PS files.
594 (if (and doc-view-dvipdf-program
595 (executable-find doc-view-dvipdf-program))
596 (doc-view-start-process "dvi->pdf" doc-view-dvipdf-program
597 (list dvi pdf)
598 callback)
599 (doc-view-start-process "dvi->pdf" doc-view-dvipdfm-program
600 (list "-o" pdf dvi)
601 callback)))
604 (defun doc-view-pdf/ps->png (pdf-ps png)
605 "Convert PDF-PS to PNG asynchronously."
606 (doc-view-start-process
607 "pdf/ps->png" doc-view-ghostscript-program
608 (append doc-view-ghostscript-options
609 (list (format "-r%d" (round doc-view-resolution))
610 (concat "-sOutputFile=" png)
611 pdf-ps))
612 (lambda ()
613 (when doc-view-current-timer
614 (cancel-timer doc-view-current-timer)
615 (setq doc-view-current-timer nil))
616 (doc-view-display (current-buffer) 'force)))
617 ;; Update the displayed pages as soon as they're done generating.
618 (when doc-view-conversion-refresh-interval
619 (setq doc-view-current-timer
620 (run-at-time "1 secs" doc-view-conversion-refresh-interval
621 'doc-view-display
622 (current-buffer)))))
624 (defun doc-view-pdf->png-1 (pdf png page callback)
625 "Convert a PAGE of a PDF file to PNG asynchronously.
626 Call CALLBACK with no arguments when done."
627 (doc-view-start-process
628 "pdf->png-1" doc-view-ghostscript-program
629 (append doc-view-ghostscript-options
630 (list (format "-r%d" (round doc-view-resolution))
631 ;; Sadly, `gs' only supports the page-range
632 ;; for PDF files.
633 (format "-dFirstPage=%d" page)
634 (format "-dLastPage=%d" page)
635 (concat "-sOutputFile=" png)
636 pdf))
637 callback))
639 (declare-function clear-image-cache "image.c" (&optional filter))
641 (defun doc-view-pdf->png (pdf png pages)
642 "Convert a PDF file to PNG asynchronously.
643 Start by converting PAGES, and then the rest."
644 (if (null pages)
645 (doc-view-pdf/ps->png pdf png)
646 ;; We could render several `pages' with a single process if they're
647 ;; (almost) consecutive, but since in 99% of the cases, there'll be only
648 ;; a single page anyway, and of the remaining 1%, few cases will have
649 ;; consecutive pages, it's not worth the trouble.
650 (lexical-let ((pdf pdf) (png png) (rest (cdr pages)))
651 (doc-view-pdf->png-1
652 pdf (format png (car pages)) (car pages)
653 (lambda ()
654 (if rest
655 (doc-view-pdf->png pdf png rest)
656 ;; Yippie, the important pages are done, update the display.
657 (clear-image-cache)
658 ;; Convert the rest of the pages.
659 (doc-view-pdf/ps->png pdf png)))))))
661 (defun doc-view-pdf->txt (pdf txt callback)
662 "Convert PDF to TXT asynchronously and call CALLBACK when finished."
663 (or doc-view-pdftotext-program
664 (error "You need the `pdftotext' program to convert a PDF to text"))
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 (or doc-view-ps2pdf-program
696 (error "You need the `ps2pdf' program to convert PS to PDF"))
697 (doc-view-start-process "ps->pdf" doc-view-ps2pdf-program
698 (list
699 ;; Avoid security problems when rendering files from
700 ;; untrusted sources.
701 "-dSAFER"
702 ;; in-file and out-file
703 ps pdf)
704 callback))
706 (defun doc-view-active-pages ()
707 (let ((pages ()))
708 (dolist (win (get-buffer-window-list (current-buffer) nil 'visible))
709 (let ((page (image-mode-window-get 'page win)))
710 (unless (memq page pages) (push page pages))))
711 pages))
713 (defun doc-view-convert-current-doc ()
714 "Convert `doc-view-buffer-file-name' to a set of png files, one file per page.
715 Those files are saved in the directory given by the function
716 `doc-view-current-cache-dir'."
717 ;; Let stale files still display while we recompute the new ones, so only
718 ;; flush the cache when the conversion is over. One of the reasons why it
719 ;; is important to keep displaying the stale page is so that revert-buffer
720 ;; preserves the horizontal/vertical scroll settings (which are otherwise
721 ;; resets during the redisplay).
722 (setq doc-view-pending-cache-flush t)
723 (let ((png-file (expand-file-name "page-%d.png"
724 (doc-view-current-cache-dir))))
725 (make-directory (doc-view-current-cache-dir) t)
726 (case doc-view-doc-type
727 (dvi
728 ;; DVI files have to be converted to PDF before Ghostscript can process
729 ;; it.
730 (lexical-let
731 ((pdf (expand-file-name "doc.pdf" doc-view-current-cache-dir))
732 (png-file png-file))
733 (doc-view-dvi->pdf doc-view-buffer-file-name pdf
734 (lambda () (doc-view-pdf/ps->png pdf png-file)))))
735 (pdf
736 (let ((pages (doc-view-active-pages)))
737 ;; Convert PDF to PNG images starting with the active pages.
738 (doc-view-pdf->png doc-view-buffer-file-name png-file pages)))
740 ;; Convert to PNG images.
741 (doc-view-pdf/ps->png doc-view-buffer-file-name png-file)))))
743 ;;;; Slicing
745 (declare-function image-size "image.c" (spec &optional pixels frame))
747 (defun doc-view-set-slice (x y width height)
748 "Set the slice of the images that should be displayed.
749 You can use this function to tell doc-view not to display the
750 margins of the document. It prompts for the top-left corner (X
751 and Y) of the slice to display and its WIDTH and HEIGHT.
753 See `doc-view-set-slice-using-mouse' for a more convenient way to
754 do that. To reset the slice use `doc-view-reset-slice'."
755 (interactive
756 (let* ((size (image-size (doc-view-current-image) t))
757 (a (read-number (format "Top-left X (0..%d): " (car size))))
758 (b (read-number (format "Top-left Y (0..%d): " (cdr size))))
759 (c (read-number (format "Width (0..%d): " (- (car size) a))))
760 (d (read-number (format "Height (0..%d): " (- (cdr size) b)))))
761 (list a b c d)))
762 (setf (doc-view-current-slice) (list x y width height))
763 ;; Redisplay
764 (doc-view-goto-page (doc-view-current-page)))
766 (defun doc-view-set-slice-using-mouse ()
767 "Set the slice of the images that should be displayed.
768 You set the slice by pressing mouse-1 at its top-left corner and
769 dragging it to its bottom-right corner. See also
770 `doc-view-set-slice' and `doc-view-reset-slice'."
771 (interactive)
772 (let (x y w h done)
773 (while (not done)
774 (let ((e (read-event
775 (concat "Press mouse-1 at the top-left corner and "
776 "drag it to the bottom-right corner!"))))
777 (when (eq (car e) 'drag-mouse-1)
778 (setq x (car (posn-object-x-y (event-start e))))
779 (setq y (cdr (posn-object-x-y (event-start e))))
780 (setq w (- (car (posn-object-x-y (event-end e))) x))
781 (setq h (- (cdr (posn-object-x-y (event-end e))) y))
782 (setq done t))))
783 (doc-view-set-slice x y w h)))
785 (defun doc-view-reset-slice ()
786 "Reset the current slice.
787 After calling this function whole pages will be visible again."
788 (interactive)
789 (setf (doc-view-current-slice) nil)
790 ;; Redisplay
791 (doc-view-goto-page (doc-view-current-page)))
793 ;;;; Display
795 (defun doc-view-insert-image (file &rest args)
796 "Insert the given png FILE.
797 ARGS is a list of image descriptors."
798 (when doc-view-pending-cache-flush
799 (clear-image-cache)
800 (setq doc-view-pending-cache-flush nil))
801 (let ((ol (doc-view-current-overlay))
802 (image (if (and file (file-readable-p file))
803 (apply 'create-image file 'png nil args)))
804 (slice (doc-view-current-slice)))
805 (setf (doc-view-current-image) image)
806 (move-overlay ol (point-min) (point-max))
807 (overlay-put ol 'display
808 (cond
809 (image
810 (if slice
811 (list (cons 'slice slice) image)
812 image))
813 ;; We're trying to display a page that doesn't exist.
814 (doc-view-current-converter-processes
815 ;; Maybe the page doesn't exist *yet*.
816 "Cannot display this page (yet)!")
818 ;; Typically happens if the conversion process somehow
819 ;; failed. Better not signal an error here because it
820 ;; could prevent a subsequent reconversion from fixing
821 ;; the problem.
822 (concat "Cannot display this page!\n"
823 "Maybe because of a conversion failure!"))))
824 (let ((win (overlay-get ol 'window)))
825 (if (stringp (overlay-get ol 'display))
826 (progn ;Make sure the text is not scrolled out of view.
827 (set-window-hscroll win 0)
828 (set-window-vscroll win 0))
829 (let ((hscroll (image-mode-window-get 'hscroll win))
830 (vscroll (image-mode-window-get 'vscroll win)))
831 ;; Reset scroll settings, in case they were changed.
832 (if hscroll (set-window-hscroll win hscroll))
833 (if vscroll (set-window-vscroll win vscroll)))))))
835 (defun doc-view-sort (a b)
836 "Return non-nil if A should be sorted before B.
837 Predicate for sorting `doc-view-current-files'."
838 (or (< (length a) (length b))
839 (and (= (length a) (length b))
840 (string< a b))))
842 (defun doc-view-display (buffer &optional force)
843 "Start viewing the document in BUFFER.
844 If FORCE is non-nil, start viewing even if the document does not
845 have the page we want to view."
846 (with-current-buffer buffer
847 (let ((prev-pages doc-view-current-files))
848 (setq doc-view-current-files
849 (sort (directory-files (doc-view-current-cache-dir) t
850 "page-[0-9]+\\.png" t)
851 'doc-view-sort))
852 (dolist (win (or (get-buffer-window-list buffer nil t)
853 (list (selected-window))))
854 (let* ((page (doc-view-current-page win))
855 (pagefile (expand-file-name (format "page-%d.png" page)
856 (doc-view-current-cache-dir))))
857 (when (or force
858 (and (not (member pagefile prev-pages))
859 (member pagefile doc-view-current-files)))
860 (with-selected-window win
861 (assert (eq (current-buffer) buffer))
862 (doc-view-goto-page page))))))))
864 (defun doc-view-buffer-message ()
865 ;; Only show this message initially, not when refreshing the buffer (in which
866 ;; case it's better to keep displaying the "stale" page while computing
867 ;; the fresh new ones).
868 (unless (overlay-get (doc-view-current-overlay) 'display)
869 (overlay-put (doc-view-current-overlay) 'display
870 (concat (propertize "Welcome to DocView!" 'face 'bold)
871 "\n"
873 If you see this buffer it means that the document you want to view is being
874 converted to PNG and the conversion of the first page hasn't finished yet or
875 `doc-view-conversion-refresh-interval' is set to nil.
877 For now these keys are useful:
879 `q' : Bury this buffer. Conversion will go on in background.
880 `k' : Kill the conversion process and this buffer.
881 `K' : Kill the conversion process.\n"))))
883 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
885 (defun doc-view-show-tooltip ()
886 (interactive)
887 (tooltip-show (doc-view-current-info)))
889 (defun doc-view-open-text ()
890 "Open a buffer with the current doc's contents as text."
891 (interactive)
892 (if doc-view-current-converter-processes
893 (message "DocView: please wait till conversion finished.")
894 (let ((txt (expand-file-name "doc.txt" (doc-view-current-cache-dir))))
895 (if (file-readable-p txt)
896 (find-file txt)
897 (doc-view-doc->txt txt 'doc-view-open-text)))))
899 ;;;;; Toggle between editing and viewing
902 (defun doc-view-toggle-display ()
903 "Toggle between editing a document as text or viewing it."
904 (interactive)
905 (if (eq major-mode 'doc-view-mode)
906 ;; Switch to editing mode
907 (progn
908 (doc-view-kill-proc)
909 (setq buffer-read-only nil)
910 (remove-overlays (point-min) (point-max) 'doc-view t)
911 (set (make-local-variable 'image-mode-winprops-alist) t)
912 ;; Switch to the previously used major mode or fall back to fundamental
913 ;; mode.
914 (if doc-view-previous-major-mode
915 (funcall doc-view-previous-major-mode)
916 (fundamental-mode))
917 (doc-view-minor-mode 1))
918 ;; Switch to doc-view-mode
919 (when (and (buffer-modified-p)
920 (y-or-n-p "The buffer has been modified. Save the changes? "))
921 (save-buffer))
922 (doc-view-mode)))
924 ;;;; Searching
927 (defun doc-view-search-internal (regexp file)
928 "Return a list of FILE's pages that contain text matching REGEXP.
929 The value is an alist of the form (PAGE CONTEXTS) where PAGE is
930 the pagenumber and CONTEXTS are all lines of text containing a match."
931 (with-temp-buffer
932 (insert-file-contents file)
933 (let ((page 1)
934 (lastpage 1)
935 matches)
936 (while (re-search-forward (concat "\\(?:\\([\f]\\)\\|\\("
937 regexp "\\)\\)") nil t)
938 (when (match-string 1) (setq page (1+ page)))
939 (when (match-string 2)
940 (if (/= page lastpage)
941 (push (cons page
942 (list (buffer-substring
943 (line-beginning-position)
944 (line-end-position))))
945 matches)
946 (setq matches (cons
947 (append
949 ;; This page already is a match.
950 (car matches)
951 ;; This is the first match on page.
952 (list page))
953 (list (buffer-substring
954 (line-beginning-position)
955 (line-end-position))))
956 (cdr matches))))
957 (setq lastpage page)))
958 (nreverse matches))))
960 (defun doc-view-search-no-of-matches (list)
961 "Extract the number of matches from the search result LIST."
962 (let ((no 0))
963 (dolist (p list)
964 (setq no (+ no (1- (length p)))))
965 no))
967 (defun doc-view-search-backward (new-query)
968 "Call `doc-view-search' for backward search.
969 If prefix NEW-QUERY is given, ask for a new regexp."
970 (interactive "P")
971 (doc-view-search new-query t))
973 (defun doc-view-search (new-query &optional backward)
974 "Jump to the next match or initiate a new search if NEW-QUERY is given.
975 If the current document hasn't been transformed to plain text
976 till now do that first.
977 If BACKWARD is non-nil, jump to the previous match."
978 (interactive "P")
979 (if (and (not new-query)
980 doc-view-current-search-matches)
981 (if backward
982 (doc-view-search-previous-match 1)
983 (doc-view-search-next-match 1))
984 ;; New search, so forget the old results.
985 (setq doc-view-current-search-matches nil)
986 (let ((txt (expand-file-name "doc.txt"
987 (doc-view-current-cache-dir))))
988 (if (file-readable-p txt)
989 (progn
990 (setq doc-view-current-search-matches
991 (doc-view-search-internal
992 (read-from-minibuffer "Regexp: ")
993 txt))
994 (message "DocView: search yielded %d matches."
995 (doc-view-search-no-of-matches
996 doc-view-current-search-matches)))
997 ;; We must convert to TXT first!
998 (if doc-view-current-converter-processes
999 (message "DocView: please wait till conversion finished.")
1000 (doc-view-doc->txt txt (lambda () (doc-view-search nil))))))))
1002 (defun doc-view-search-next-match (arg)
1003 "Go to the ARGth next matching page."
1004 (interactive "p")
1005 (let* ((next-pages (doc-view-remove-if
1006 (lambda (i) (<= (car i) (doc-view-current-page)))
1007 doc-view-current-search-matches))
1008 (page (car (nth (1- arg) next-pages))))
1009 (if page
1010 (doc-view-goto-page page)
1011 (when (and
1012 doc-view-current-search-matches
1013 (y-or-n-p "No more matches after current page. Wrap to first match? "))
1014 (doc-view-goto-page (caar doc-view-current-search-matches))))))
1016 (defun doc-view-search-previous-match (arg)
1017 "Go to the ARGth previous matching page."
1018 (interactive "p")
1019 (let* ((prev-pages (doc-view-remove-if
1020 (lambda (i) (>= (car i) (doc-view-current-page)))
1021 doc-view-current-search-matches))
1022 (page (car (nth (1- arg) (nreverse prev-pages)))))
1023 (if page
1024 (doc-view-goto-page page)
1025 (when (and
1026 doc-view-current-search-matches
1027 (y-or-n-p "No more matches before current page. Wrap to last match? "))
1028 (doc-view-goto-page (caar (last doc-view-current-search-matches)))))))
1030 ;;;; User interface commands and the mode
1032 ;; (put 'doc-view-mode 'mode-class 'special)
1034 (defun doc-view-already-converted-p ()
1035 "Return non-nil if the current doc was already converted."
1036 (and (file-exists-p (doc-view-current-cache-dir))
1037 (> (length (directory-files (doc-view-current-cache-dir) nil "\\.png$")) 0)))
1039 (defun doc-view-initiate-display ()
1040 ;; Switch to image display if possible
1041 (if (doc-view-mode-p doc-view-doc-type)
1042 (progn
1043 (doc-view-buffer-message)
1044 (setf (doc-view-current-page) (or (doc-view-current-page) 1))
1045 (if (doc-view-already-converted-p)
1046 (progn
1047 (message "DocView: using cached files!")
1048 (doc-view-display (current-buffer) 'force))
1049 (doc-view-convert-current-doc))
1050 (message
1051 "%s"
1052 (substitute-command-keys
1053 (concat "Type \\[doc-view-toggle-display] to toggle between "
1054 "editing or viewing the document."))))
1055 (message
1056 "%s"
1057 (substitute-command-keys
1058 (concat "No PNG support available or some conversion utility for "
1059 (file-name-extension doc-view-buffer-file-name)" files is missing. "
1060 "Type \\[doc-view-toggle-display] to switch to "
1061 (if (eq doc-view-doc-type 'ps)
1062 "ps-mode"
1063 "fundamental-mode")
1064 ", \\[doc-view-open-text] to show the doc as text in a separate buffer "
1065 " or \\[doc-view-kill-proc-and-buffer] to kill this buffer.")))))
1067 (defvar bookmark-make-record-function)
1069 (defun doc-view-clone-buffer-hook ()
1070 ;; FIXME: There are several potential problems linked with reconversion
1071 ;; and auto-revert when we have indirect buffers because they share their
1072 ;; /tmp cache directory. This sharing is good (you'd rather not reconvert
1073 ;; for each clone), but that means that clones need to collaborate a bit.
1074 ;; I guess it mostly means: detect when a reconversion process is already
1075 ;; running, and run the sentinel in all clones.
1077 ;; Maybe the clones should really have a separate /tmp directory
1078 ;; so they could have a different resolution and you could use clones
1079 ;; for zooming.
1080 (remove-overlays (point-min) (point-max) 'doc-view t)
1081 (if (consp image-mode-winprops-alist) (setq image-mode-winprops-alist nil)))
1083 (defun doc-view-intersection (l1 l2)
1084 (let ((l ()))
1085 (dolist (x l1) (if (memq x l2) (push x l)))
1088 ;;;###autoload
1089 (defun doc-view-mode ()
1090 "Major mode in DocView buffers.
1092 DocView Mode is an Emacs document viewer. It displays PDF, PS
1093 and DVI files (as PNG images) in Emacs buffers.
1095 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to
1096 toggle between displaying the document or editing it as text.
1097 \\{doc-view-mode-map}"
1098 (interactive)
1100 (if (or (not (file-exists-p buffer-file-name))
1101 (= (point-min) (point-max)))
1102 ;; The doc is empty or doesn't exist at all, so fallback to
1103 ;; another mode.
1104 (let ((auto-mode-alist (remq (rassq 'doc-view-mode auto-mode-alist)
1105 auto-mode-alist)))
1106 (normal-mode))
1108 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode)
1109 doc-view-previous-major-mode
1110 major-mode)))
1111 (kill-all-local-variables)
1112 (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode))
1114 ;; Figure out the document type.
1115 (let ((name-types
1116 (when buffer-file-name
1117 (cdr (assoc (file-name-extension buffer-file-name)
1118 '(("dvi" dvi)
1119 ("pdf" pdf)
1120 ("epdf" pdf)
1121 ("ps" ps)
1122 ("eps" ps))))))
1123 (content-types
1124 (save-excursion
1125 (goto-char (point-min))
1126 (cond
1127 ((looking-at "%!") '(ps))
1128 ((looking-at "%PDF") '(pdf))
1129 ((looking-at "\367\002") '(dvi))))))
1130 (set (make-local-variable 'doc-view-doc-type)
1131 (car (or (doc-view-intersection name-types content-types)
1132 (when (and name-types content-types)
1133 (error "Conflicting types: name says %s but content says %s"
1134 name-types content-types))
1135 name-types content-types
1136 (error "Cannot determine the document type")))))
1138 (doc-view-make-safe-dir doc-view-cache-directory)
1139 ;; Handle compressed files, remote files, files inside archives
1140 (set (make-local-variable 'doc-view-buffer-file-name)
1141 (cond
1142 (jka-compr-really-do-compress
1143 (expand-file-name
1144 (file-name-nondirectory
1145 (file-name-sans-extension buffer-file-name))
1146 doc-view-cache-directory))
1147 ;; Is the file readable by local processes?
1148 ;; We used to use `file-remote-p' but it's unclear what it's
1149 ;; supposed to return nil for things like local files accessed via
1150 ;; `su' or via file://...
1151 ((let ((file-name-handler-alist nil))
1152 (not (file-readable-p buffer-file-name)))
1153 (expand-file-name
1154 (file-name-nondirectory buffer-file-name)
1155 doc-view-cache-directory))
1156 (t buffer-file-name)))
1157 (when (not (string= doc-view-buffer-file-name buffer-file-name))
1158 (write-region nil nil doc-view-buffer-file-name))
1160 (add-hook 'change-major-mode-hook
1161 (lambda ()
1162 (doc-view-kill-proc)
1163 (remove-overlays (point-min) (point-max) 'doc-view t))
1164 nil t)
1165 (add-hook 'clone-indirect-buffer-hook 'doc-view-clone-buffer-hook nil t)
1166 (add-hook 'kill-buffer-hook 'doc-view-kill-proc nil t)
1168 (remove-overlays (point-min) (point-max) 'doc-view t) ;Just in case.
1169 ;; Keep track of display info ([vh]scroll, page number, overlay,
1170 ;; ...) for each window in which this document is shown.
1171 (add-hook 'image-mode-new-window-functions
1172 'doc-view-new-window-function nil t)
1173 (image-mode-setup-winprops)
1175 (set (make-local-variable 'mode-line-position)
1176 '(" P" (:eval (number-to-string (doc-view-current-page)))
1177 "/" (:eval (number-to-string (length doc-view-current-files)))))
1178 ;; Don't scroll unless the user specifically asked for it.
1179 (set (make-local-variable 'auto-hscroll-mode) nil)
1180 (set (make-local-variable 'cursor-type) nil)
1181 (use-local-map doc-view-mode-map)
1182 (set (make-local-variable 'after-revert-hook) 'doc-view-reconvert-doc)
1183 (set (make-local-variable 'bookmark-make-record-function)
1184 'doc-view-bookmark-make-record)
1185 (setq mode-name "DocView"
1186 buffer-read-only t
1187 major-mode 'doc-view-mode)
1188 (doc-view-initiate-display)
1189 (run-mode-hooks 'doc-view-mode-hook)))
1191 ;;;###autoload
1192 (define-minor-mode doc-view-minor-mode
1193 "Toggle Doc view minor mode.
1194 With arg, turn Doc view minor mode on if arg is positive, off otherwise.
1195 See the command `doc-view-mode' for more information on this mode."
1196 nil " DocView" doc-view-minor-mode-map
1197 :group 'doc-view
1198 (when doc-view-minor-mode
1199 (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
1200 (message
1201 "%s"
1202 (substitute-command-keys
1203 "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document."))))
1205 (defun doc-view-clear-cache ()
1206 "Delete the whole cache (`doc-view-cache-directory')."
1207 (interactive)
1208 (dired-delete-file doc-view-cache-directory 'always))
1210 (defun doc-view-dired-cache ()
1211 "Open `dired' in `doc-view-cache-directory'."
1212 (interactive)
1213 (dired doc-view-cache-directory))
1216 ;;;; Bookmark integration
1218 (declare-function bookmark-make-record-default "bookmark"
1219 (&optional point-only))
1220 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
1221 (declare-function bookmark-default-handler "bookmark" (bmk))
1223 (defun doc-view-bookmark-make-record ()
1224 (nconc (bookmark-make-record-default)
1225 `((page . ,(doc-view-current-page))
1226 (handler . doc-view-bookmark-jump))))
1229 ;;;###autoload
1230 (defun doc-view-bookmark-jump (bmk)
1231 ;; This implements the `handler' function interface for record type
1232 ;; returned by `doc-view-bookmark-make-record', which see.
1233 (prog1 (bookmark-default-handler bmk)
1234 (let ((page (bookmark-prop-get bmk 'page)))
1235 (when (not (eq major-mode 'doc-view-mode))
1236 (doc-view-toggle-display))
1237 (with-selected-window
1238 (or (get-buffer-window (current-buffer) 0)
1239 (selected-window))
1240 (doc-view-goto-page page)))))
1243 (provide 'doc-view)
1245 ;; Local Variables:
1246 ;; mode: outline-minor
1247 ;; End:
1249 ;; arch-tag: 5d6e5c5e-095f-489e-b4e4-1ca90a7d79be
1250 ;;; doc-view.el ends here