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