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