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