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