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