Rob Riepel <riepel at networking.stanford.edu>
[emacs.git] / lisp / doc-view.el
bloba03b2e16785e5219ca8b82f01b9180d37160fa23
1 ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs
3 ;; Copyright (C) 2007 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
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Requirements:
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/).
32 ;;; Commentary:
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.
39 ;; To use it simply open a document file with
41 ;; C-x C-f ~/path/to/document RET
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.
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'.
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.
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').
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
75 ;; `doc-view-search-next-match' (bound to `C-S-n') or to the previous matching
76 ;; page with `doc-view-search-previous-match' (bound to `C-S-p'). This works
77 ;; by searching a plain text representation of the document. If that doesn't
78 ;; already exist the first invokation of `doc-view-search' starts the
79 ;; conversion. When that finishes and you're still viewing the document
80 ;; (i.e. you didn't switch to another buffer) you're queried for the regexp
81 ;; then.
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'.
87 ;;; Configuration:
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.)
93 ;; This and all other options can be set with the customization interface.
94 ;; Simply do
96 ;; M-x customize-group RET doc-view RET
98 ;; and modify them to your needs.
100 ;;; Code:
102 ;; Todo:
103 ;; - better menu.
104 ;; - don't use `find-file'.
105 ;; - Bind slicing to a drag event.
106 ;; - zoom (the whole document and/or just the region around the cursor).
107 ;; - get rid of the silly arrow in the fringe.
108 ;; - improve anti-aliasing (pdf-utils gets it better).
110 (require 'dired)
111 (require 'image-mode)
113 ;;;; Customization Options
115 (defgroup doc-view nil
116 "In-buffer viewer for PDF, PostScript and DVI files."
117 :link '(function-link doc-view)
118 :version "22.2"
119 :group 'applications
120 :group 'multimedia
121 :prefix "doc-view-")
123 (defcustom doc-view-ghostscript-program (executable-find "gs")
124 "Program to convert PS and PDF files to PNG."
125 :type 'file
126 :group 'doc-view)
128 (defcustom doc-view-ghostscript-options
129 '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
130 ;; sources.
131 "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
132 "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET" "-r100")
133 "A list of options to give to ghostscript."
134 :type '(repeat string)
135 :group 'doc-view)
137 (defcustom doc-view-dvipdfm-program (executable-find "dvipdfm")
138 "Program to convert DVI files to PDF.
140 DVI file will be converted to PDF before the resulting PDF is
141 converted to PNG."
142 :type 'file
143 :group 'doc-view)
145 (defcustom doc-view-ps2pdf-program (executable-find "ps2pdf")
146 "Program to convert PS files to PDF.
148 PS files will be converted to PDF before searching is possible."
149 :type 'file
150 :group 'doc-view)
152 (defcustom doc-view-pdftotext-program (executable-find "pdftotext")
153 "Program to convert PDF files to plain text.
155 Needed for searching."
156 :type 'file
157 :group 'doc-view)
159 (defcustom doc-view-cache-directory
160 (expand-file-name (format "docview%d" (user-uid))
161 temporary-file-directory)
162 "The base directory, where the PNG images will be saved."
163 :type 'directory
164 :group 'doc-view)
166 (defcustom doc-view-conversion-buffer "*doc-view conversion output*"
167 "The buffer where messages from the converter programs go to."
168 :type 'string
169 :group 'doc-view)
171 (defcustom doc-view-conversion-refresh-interval 3
172 "Interval in seconds between refreshes of the DocView buffer while converting.
173 After such a refresh newly converted pages will be available for
174 viewing. If set to nil there won't be any refreshes and the
175 pages won't be displayed before conversion of the whole document
176 has finished."
177 :type 'integer
178 :group 'doc-view)
180 ;;;; Internal Variables
182 (defvar doc-view-current-files nil
183 "Only used internally.")
185 (defvar doc-view-current-page nil
186 "Only used internally.")
188 (defvar doc-view-current-converter-process nil
189 "Only used internally.")
191 (defvar doc-view-current-timer nil
192 "Only used internally.")
194 (defvar doc-view-current-slice nil
195 "Only used internally.")
197 (defvar doc-view-current-cache-dir nil
198 "Only used internally.")
200 (defvar doc-view-current-search-matches nil
201 "Only used internally.")
203 (defvar doc-view-current-image nil
204 "Only used internally.")
205 (defvar doc-view-current-overlay)
206 (defvar doc-view-pending-cache-flush nil)
208 (defvar doc-view-current-info nil
209 "Only used internally.")
211 (defvar doc-view-previous-major-mode nil
212 "Only used internally.")
214 ;;;; DocView Keymaps
216 (defvar doc-view-mode-map
217 (let ((map (make-sparse-keymap)))
218 (suppress-keymap map)
219 ;; Navigation in the document
220 (define-key map (kbd "n") 'doc-view-next-page)
221 (define-key map (kbd "p") 'doc-view-previous-page)
222 (define-key map (kbd "<next>") 'forward-page)
223 (define-key map (kbd "<prior>") 'backward-page)
224 (define-key map [remap forward-page] 'doc-view-next-page)
225 (define-key map [remap backward-page] 'doc-view-previous-page)
226 (define-key map (kbd "SPC") 'doc-view-scroll-up-or-next-page)
227 (define-key map (kbd "DEL") 'doc-view-scroll-down-or-previous-page)
228 (define-key map (kbd "M-<") 'doc-view-first-page)
229 (define-key map (kbd "M->") 'doc-view-last-page)
230 (define-key map [remap goto-line] 'doc-view-goto-page)
231 ;; Killing/burying the buffer (and the process)
232 (define-key map (kbd "q") 'bury-buffer)
233 (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer)
234 (define-key map (kbd "K") 'doc-view-kill-proc)
235 ;; Slicing the image
236 (define-key map (kbd "s s") 'doc-view-set-slice)
237 (define-key map (kbd "s m") 'doc-view-set-slice-using-mouse)
238 (define-key map (kbd "s r") 'doc-view-reset-slice)
239 ;; Searching
240 (define-key map (kbd "C-s") 'doc-view-search)
241 (define-key map (kbd "<find>") 'doc-view-search)
242 (define-key map (kbd "C-S-n") 'doc-view-search-next-match)
243 (define-key map (kbd "C-S-p") 'doc-view-search-previous-match)
244 ;; Scrolling
245 (define-key map [remap forward-char] 'image-forward-hscroll)
246 (define-key map [remap backward-char] 'image-backward-hscroll)
247 (define-key map [remap next-line] 'image-next-line)
248 (define-key map [remap previous-line] 'image-previous-line)
249 ;; Show the tooltip
250 (define-key map (kbd "C-t") 'doc-view-show-tooltip)
251 ;; Toggle between text and image display or editing
252 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
253 ;; Reconvert the current document
254 (define-key map (kbd "g") 'revert-buffer)
255 (define-key map (kbd "r") 'revert-buffer)
256 map)
257 "Keymap used by `doc-view-mode' when displaying a doc as a set of images.")
259 (easy-menu-define doc-view-menu doc-view-mode-map
260 "Menu for Doc View mode."
261 '("DocView"
262 ["Set Slice" doc-view-set-slice-using-mouse]
263 ["Set Slice (manual)" doc-view-set-slice]
264 ["Reset Slice" doc-view-reset-slice]
265 "---"
266 ["Search" doc-view-search]
267 ["Toggle display" doc-view-toggle-display]
270 (defvar doc-view-minor-mode-map
271 (let ((map (make-sparse-keymap)))
272 ;; Toggle between text and image display or editing
273 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
274 map)
275 "Keymap used by `doc-minor-view-mode'.")
277 ;;;; Navigation Commands
279 (defun doc-view-goto-page (page)
280 "View the page given by PAGE."
281 (interactive "nPage: ")
282 (let ((len (length doc-view-current-files)))
283 (if (< page 1)
284 (setq page 1)
285 (when (> page len)
286 (setq page len)))
287 (setq doc-view-current-page page
288 doc-view-current-info
289 (concat
290 (propertize
291 (format "Page %d of %d."
292 doc-view-current-page
293 len) 'face 'bold)
294 ;; Tell user if converting isn't finished yet
295 (if doc-view-current-converter-process
296 " (still converting...)\n"
297 "\n")
298 ;; Display context infos if this page matches the last search
299 (when (and doc-view-current-search-matches
300 (assq doc-view-current-page
301 doc-view-current-search-matches))
302 (concat (propertize "Search matches:\n" 'face 'bold)
303 (let ((contexts ""))
304 (dolist (m (cdr (assq doc-view-current-page
305 doc-view-current-search-matches)))
306 (setq contexts (concat contexts " - \"" m "\"\n")))
307 contexts)))))
308 ;; Update the buffer
309 (doc-view-insert-image (nth (1- page) doc-view-current-files)
310 :pointer 'arrow)
311 (overlay-put doc-view-current-overlay 'help-echo doc-view-current-info)
312 (goto-char (point-min))
313 ;; This seems to be needed for set-window-hscroll (in
314 ;; image-forward-hscroll) to do something useful, I don't have time to
315 ;; debug this now. :-( --Stef
316 (forward-char)))
318 (defun doc-view-next-page (&optional arg)
319 "Browse ARG pages forward."
320 (interactive "p")
321 (doc-view-goto-page (+ doc-view-current-page (or arg 1))))
323 (defun doc-view-previous-page (&optional arg)
324 "Browse ARG pages backward."
325 (interactive "p")
326 (doc-view-goto-page (- doc-view-current-page (or arg 1))))
328 (defun doc-view-first-page ()
329 "View the first page."
330 (interactive)
331 (doc-view-goto-page 1))
333 (defun doc-view-last-page ()
334 "View the last page."
335 (interactive)
336 (doc-view-goto-page (length doc-view-current-files)))
338 (defun doc-view-scroll-up-or-next-page ()
339 "Scroll page up if possible, else goto next page."
340 (interactive)
341 (condition-case nil
342 (scroll-up)
343 (error (doc-view-next-page))))
345 (defun doc-view-scroll-down-or-previous-page ()
346 "Scroll page down if possible, else goto previous page."
347 (interactive)
348 (condition-case nil
349 (scroll-down)
350 (error (doc-view-previous-page)
351 (goto-char (point-max)))))
353 ;;;; Utility Functions
355 (defun doc-view-kill-proc ()
356 "Kill the current converter process."
357 (interactive)
358 (when doc-view-current-converter-process
359 (kill-process doc-view-current-converter-process)
360 (setq doc-view-current-converter-process nil))
361 (when doc-view-current-timer
362 (cancel-timer doc-view-current-timer)
363 (setq doc-view-current-timer nil))
364 (setq mode-line-process nil))
366 (defun doc-view-kill-proc-and-buffer ()
367 "Kill the current converter process and buffer."
368 (interactive)
369 (doc-view-kill-proc)
370 (when (eq major-mode 'doc-view-mode)
371 (kill-buffer (current-buffer))))
373 (defun doc-view-make-safe-dir (dir)
374 (condition-case nil
375 (let ((umask (default-file-modes)))
376 (unwind-protect
377 (progn
378 ;; Create temp files with strict access rights. It's easy to
379 ;; loosen them later, whereas it's impossible to close the
380 ;; time-window of loose permissions otherwise.
381 (set-default-file-modes #o0700)
382 (make-directory dir))
383 ;; Reset the umask.
384 (set-default-file-modes umask)))
385 (file-already-exists
386 (if (file-symlink-p dir)
387 (error "Danger: %s points to a symbolic link" dir))
388 ;; In case it was created earlier with looser rights.
389 ;; We could check the mode info returned by file-attributes, but it's
390 ;; a pain to parse and it may not tell you what we want under
391 ;; non-standard file-systems. So let's just say what we want and let
392 ;; the underlying C code and file-system figure it out.
393 ;; This also ends up checking a bunch of useful conditions: it makes
394 ;; sure we have write-access to the directory and that we own it, thus
395 ;; closing a bunch of security holes.
396 (set-file-modes dir #o0700))))
398 (defun doc-view-current-cache-dir ()
399 "Return the directory where the png files of the current doc should be saved.
400 It's a subdirectory of `doc-view-cache-directory'."
401 (if doc-view-current-cache-dir
402 doc-view-current-cache-dir
403 ;; Try and make sure doc-view-cache-directory exists and is safe.
404 (doc-view-make-safe-dir doc-view-cache-directory)
405 ;; Now compute the subdirectory to use.
406 (setq doc-view-current-cache-dir
407 (file-name-as-directory
408 (expand-file-name
409 (let ((doc buffer-file-name))
410 (concat (file-name-nondirectory doc)
412 (with-temp-buffer
413 (insert-file-contents-literally doc)
414 (md5 (current-buffer)))))
415 doc-view-cache-directory)))))
417 (defun doc-view-remove-if (predicate list)
418 "Return LIST with all items removed that satisfy PREDICATE."
419 (let (new-list)
420 (dolist (item list (nreverse new-list))
421 (when (not (funcall predicate item))
422 (setq new-list (cons item new-list))))))
424 ;;;; Conversion Functions
426 (defun doc-view-reconvert-doc ()
427 "Reconvert the current document.
428 Should be invoked when the cached images aren't up-to-date."
429 (interactive)
430 (doc-view-kill-proc)
431 ;; Clear the old cached files
432 (when (file-exists-p (doc-view-current-cache-dir))
433 (dired-delete-file (doc-view-current-cache-dir) 'always))
434 (doc-view-initiate-display))
436 (defun doc-view-dvi->pdf-sentinel (proc event)
437 "If DVI->PDF conversion was successful, convert the PDF to PNG now."
438 (if (not (string-match "finished" event))
439 (message "DocView: dvi->pdf process changed status to %s." event)
440 (set-buffer (process-get proc 'buffer))
441 (setq doc-view-current-converter-process nil
442 mode-line-process nil)
443 ;; Now go on converting this PDF to a set of PNG files.
444 (let* ((pdf (process-get proc 'pdf-file))
445 (png (expand-file-name "page-%d.png"
446 (doc-view-current-cache-dir))))
447 (doc-view-pdf/ps->png pdf png))))
449 (defun doc-view-dvi->pdf (dvi pdf)
450 "Convert DVI to PDF asynchronously."
451 (setq doc-view-current-converter-process
452 (start-process "dvi->pdf" doc-view-conversion-buffer
453 doc-view-dvipdfm-program
454 "-o" pdf dvi)
455 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
456 (set-process-sentinel doc-view-current-converter-process
457 'doc-view-dvi->pdf-sentinel)
458 (process-put doc-view-current-converter-process 'buffer (current-buffer))
459 (process-put doc-view-current-converter-process 'pdf-file pdf))
461 (defun doc-view-pdf/ps->png-sentinel (proc event)
462 "If PDF/PS->PNG conversion was successful, update the display."
463 (if (not (string-match "finished" event))
464 (message "DocView: converter process changed status to %s." event)
465 (set-buffer (process-get proc 'buffer))
466 (setq doc-view-current-converter-process nil
467 mode-line-process nil)
468 (when doc-view-current-timer
469 (cancel-timer doc-view-current-timer)
470 (setq doc-view-current-timer nil))
471 ;; Yippie, finished. Update the display!
472 (doc-view-display buffer-file-name)))
474 (defun doc-view-pdf/ps->png (pdf-ps png)
475 "Convert PDF-PS to PNG asynchronously."
476 (setq doc-view-current-converter-process
477 (apply 'start-process
478 (append (list "pdf/ps->png" doc-view-conversion-buffer
479 doc-view-ghostscript-program)
480 doc-view-ghostscript-options
481 (list (concat "-sOutputFile=" png))
482 (list pdf-ps)))
483 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
484 (process-put doc-view-current-converter-process
485 'buffer (current-buffer))
486 (set-process-sentinel doc-view-current-converter-process
487 'doc-view-pdf/ps->png-sentinel)
488 (when doc-view-conversion-refresh-interval
489 (setq doc-view-current-timer
490 (run-at-time "1 secs" doc-view-conversion-refresh-interval
491 'doc-view-display
492 buffer-file-name))))
494 (defun doc-view-pdf->txt-sentinel (proc event)
495 (if (not (string-match "finished" event))
496 (message "DocView: converter process changed status to %s." event)
497 (let ((current-buffer (current-buffer))
498 (proc-buffer (process-get proc 'buffer)))
499 (set-buffer proc-buffer)
500 (setq doc-view-current-converter-process nil
501 mode-line-process nil)
502 ;; If the user looks at the DocView buffer where the conversion was
503 ;; performed, search anew. This time it will be queried for a regexp.
504 (when (eq current-buffer proc-buffer)
505 (doc-view-search)))))
507 (defun doc-view-pdf->txt (pdf txt)
508 "Convert PDF to TXT asynchronously."
509 (setq doc-view-current-converter-process
510 (start-process "pdf->txt" doc-view-conversion-buffer
511 doc-view-pdftotext-program "-raw"
512 pdf txt)
513 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
514 (set-process-sentinel doc-view-current-converter-process
515 'doc-view-pdf->txt-sentinel)
516 (process-put doc-view-current-converter-process 'buffer (current-buffer)))
518 (defun doc-view-ps->pdf-sentinel (proc event)
519 (if (not (string-match "finished" event))
520 (message "DocView: converter process changed status to %s." event)
521 (set-buffer (process-get proc 'buffer))
522 (setq doc-view-current-converter-process nil
523 mode-line-process nil)
524 ;; Now we can transform to plain text.
525 (doc-view-pdf->txt (process-get proc 'pdf-file)
526 (expand-file-name "doc.txt"
527 (doc-view-current-cache-dir)))))
529 (defun doc-view-ps->pdf (ps pdf)
530 "Convert PS to PDF asynchronously."
531 (setq doc-view-current-converter-process
532 (start-process "ps->pdf" doc-view-conversion-buffer
533 doc-view-ps2pdf-program
534 ;; Avoid security problems when rendering files from
535 ;; untrusted sources.
536 "-dSAFER"
537 ;; in-file and out-file
538 ps pdf)
539 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
540 (set-process-sentinel doc-view-current-converter-process
541 'doc-view-ps->pdf-sentinel)
542 (process-put doc-view-current-converter-process 'buffer (current-buffer))
543 (process-put doc-view-current-converter-process 'pdf-file pdf))
545 (defun doc-view-convert-current-doc ()
546 "Convert `buffer-file-name' to a set of png files, one file per page.
547 Those files are saved in the directory given by the function
548 `doc-view-current-cache-dir'."
549 ;; Let stale files still display while we recompute the new ones, so only
550 ;; flush the cache when the conversion is over. One of the reasons why it
551 ;; is important to keep displaying the stale page is so that revert-buffer
552 ;; preserves the horizontal/vertical scroll settings (which are otherwise
553 ;; resets during the redisplay).
554 (setq doc-view-pending-cache-flush t)
555 (let ((png-file (expand-file-name "page-%d.png"
556 (doc-view-current-cache-dir))))
557 (make-directory (doc-view-current-cache-dir))
558 (if (not (string= (file-name-extension buffer-file-name) "dvi"))
559 ;; Convert to PNG images.
560 (doc-view-pdf/ps->png buffer-file-name png-file)
561 ;; DVI files have to be converted to PDF before Ghostscript can process
562 ;; it.
563 (doc-view-dvi->pdf buffer-file-name
564 (expand-file-name "doc.pdf"
565 doc-view-current-cache-dir)))))
567 ;;;; Slicing
569 (defun doc-view-set-slice (x y width height)
570 "Set the slice of the images that should be displayed.
571 You can use this function to tell doc-view not to display the
572 margins of the document. It prompts for the top-left corner (X
573 and Y) of the slice to display and its WIDTH and HEIGHT.
575 See `doc-view-set-slice-using-mouse' for a more convenient way to
576 do that. To reset the slice use `doc-view-reset-slice'."
577 (interactive
578 (let* ((size (image-size doc-view-current-image t))
579 (a (read-number (format "Top-left X (0..%d): " (car size))))
580 (b (read-number (format "Top-left Y (0..%d): " (cdr size))))
581 (c (read-number (format "Width (0..%d): " (- (car size) a))))
582 (d (read-number (format "Height (0..%d): " (- (cdr size) b)))))
583 (list a b c d)))
584 (setq doc-view-current-slice (list x y width height))
585 ;; Redisplay
586 (doc-view-goto-page doc-view-current-page))
588 (defun doc-view-set-slice-using-mouse ()
589 "Set the slice of the images that should be displayed.
590 You set the slice by pressing mouse-1 at its top-left corner and
591 dragging it to its bottom-right corner. See also
592 `doc-view-set-slice' and `doc-view-reset-slice'."
593 (interactive)
594 (let (x y w h done)
595 (while (not done)
596 (let ((e (read-event
597 (concat "Press mouse-1 at the top-left corner and "
598 "drag it to the bottom-right corner!"))))
599 (when (eq (car e) 'drag-mouse-1)
600 (setq x (car (posn-object-x-y (event-start e))))
601 (setq y (cdr (posn-object-x-y (event-start e))))
602 (setq w (- (car (posn-object-x-y (event-end e))) x))
603 (setq h (- (cdr (posn-object-x-y (event-end e))) y))
604 (setq done t))))
605 (doc-view-set-slice x y w h)))
607 (defun doc-view-reset-slice ()
608 "Reset the current slice.
609 After calling this function whole pages will be visible again."
610 (interactive)
611 (setq doc-view-current-slice nil)
612 ;; Redisplay
613 (doc-view-goto-page doc-view-current-page))
615 ;;;; Display
617 (defun doc-view-insert-image (file &rest args)
618 "Insert the given png FILE.
619 ARGS is a list of image descriptors."
620 (when doc-view-pending-cache-flush
621 (clear-image-cache)
622 (setq doc-view-pending-cache-flush nil))
623 (let ((image (apply 'create-image file 'png nil args)))
624 (setq doc-view-current-image image)
625 (move-overlay doc-view-current-overlay (point-min) (point-max))
626 (overlay-put doc-view-current-overlay 'display
627 (if doc-view-current-slice
628 (list (cons 'slice doc-view-current-slice) image)
629 image))))
631 (defun doc-view-sort (a b)
632 "Return non-nil if A should be sorted before B.
633 Predicate for sorting `doc-view-current-files'."
634 (or (< (length a) (length b))
635 (and (= (length a) (length b))
636 (string< a b))))
638 (defun doc-view-display (doc)
639 "Start viewing the document DOC."
640 (set-buffer (get-file-buffer doc))
641 (setq doc-view-current-files
642 (sort (directory-files (doc-view-current-cache-dir) t
643 "page-[0-9]+\\.png" t)
644 'doc-view-sort))
645 (when (> (length doc-view-current-files) 0)
646 (doc-view-goto-page doc-view-current-page)))
648 (defun doc-view-buffer-message ()
649 ;; Only show this message initially, not when refreshing the buffer (in which
650 ;; case it's better to keep displaying the "stale" page while computing
651 ;; the fresh new ones).
652 (unless (overlay-get doc-view-current-overlay 'display)
653 (overlay-put doc-view-current-overlay 'display
654 (concat (propertize "Welcome to DocView!" 'face 'bold)
655 "\n"
657 If you see this buffer it means that the document you want to view is being
658 converted to PNG and the conversion of the first page hasn't finished yet or
659 `doc-view-conversion-refresh-interval' is set to nil.
661 For now these keys are useful:
663 `q' : Bury this buffer. Conversion will go on in background.
664 `k' : Kill the conversion process and this buffer.
665 `K' : Kill the conversion process.\n"))))
667 (defun doc-view-show-tooltip ()
668 (interactive)
669 (tooltip-show doc-view-current-info))
671 ;;;;; Toggle between editing and viewing
673 (defun doc-view-toggle-display ()
674 "Toggle between editing a document as text or viewing it."
675 (interactive)
676 (if (eq major-mode 'doc-view-mode)
677 ;; Switch to editing mode
678 (progn
679 (doc-view-kill-proc)
680 (setq buffer-read-only nil)
681 (delete-overlay doc-view-current-overlay)
682 ;; Switch to the previously used major mode or fall back to fundamental
683 ;; mode.
684 (if doc-view-previous-major-mode
685 (funcall doc-view-previous-major-mode)
686 (fundamental-mode))
687 (doc-view-minor-mode 1))
688 ;; Switch to doc-view-mode
689 (when (and (buffer-modified-p)
690 (y-or-n-p "The buffer has been modified. Save the changes? "))
691 (save-buffer))
692 (doc-view-mode)))
694 ;;;; Searching
696 (defun doc-view-search-internal (regexp file)
697 "Return a list of FILE's pages that contain text matching REGEXP.
698 The value is an alist of the form (PAGE CONTEXTS) where PAGE is
699 the pagenumber and CONTEXTS are all lines of text containing a match."
700 (with-temp-buffer
701 (insert-file-contents file)
702 (let ((page 1)
703 (lastpage 1)
704 matches)
705 (while (re-search-forward (concat "\\(?:\\([\f]\\)\\|\\("
706 regexp "\\)\\)") nil t)
707 (when (match-string 1) (incf page))
708 (when (match-string 2)
709 (if (/= page lastpage)
710 (push (cons page
711 (list (buffer-substring
712 (line-beginning-position)
713 (line-end-position))))
714 matches)
715 (setq matches (cons
716 (append
718 ;; This page already is a match.
719 (car matches)
720 ;; This is the first match on page.
721 (list page))
722 (list (buffer-substring
723 (line-beginning-position)
724 (line-end-position))))
725 (cdr matches))))
726 (setq lastpage page)))
727 (nreverse matches))))
729 (defun doc-view-search-no-of-matches (list)
730 "Extract the number of matches from the search result LIST."
731 (let ((no 0))
732 (dolist (p list)
733 (setq no (+ no (1- (length p)))))
734 no))
736 (defun doc-view-search ()
737 "Query for a regexp and search the current document.
738 If the current document hasn't been transformed to plain text
739 till now do that first. You should try searching anew when the
740 conversion finished."
741 (interactive)
742 ;; New search, so forget the old results.
743 (setq doc-view-current-search-matches nil)
744 (let ((txt (expand-file-name "doc.txt"
745 (doc-view-current-cache-dir))))
746 (if (file-readable-p txt)
747 (progn
748 (setq doc-view-current-search-matches
749 (doc-view-search-internal
750 (read-from-minibuffer "Regexp: ")
751 txt))
752 (message "DocView: search yielded %d matches."
753 (doc-view-search-no-of-matches
754 doc-view-current-search-matches)))
755 ;; We must convert to TXT first!
756 (if doc-view-current-converter-process
757 (message "DocView: please wait till conversion finished.")
758 (let ((ext (file-name-extension buffer-file-name)))
759 (cond
760 ((string= ext "pdf")
761 ;; Doc is a PDF, so convert it to TXT
762 (doc-view-pdf->txt buffer-file-name txt))
763 ((string= ext "ps")
764 ;; Doc is a PS, so convert it to PDF (which will be converted to
765 ;; TXT thereafter).
766 (doc-view-ps->pdf buffer-file-name
767 (expand-file-name "doc.pdf"
768 (doc-view-current-cache-dir))))
769 ((string= ext "dvi")
770 ;; Doc is a DVI. This means that a doc.pdf already exists in its
771 ;; cache subdirectory.
772 (doc-view-pdf->txt (expand-file-name "doc.pdf"
773 (doc-view-current-cache-dir))
774 txt))
775 (t (error "DocView doesn't know what to do"))))))))
777 (defun doc-view-search-next-match (arg)
778 "Go to the ARGth next matching page."
779 (interactive "p")
780 (let* ((next-pages (doc-view-remove-if
781 (lambda (i) (<= (car i) doc-view-current-page))
782 doc-view-current-search-matches))
783 (page (car (nth (1- arg) next-pages))))
784 (if page
785 (doc-view-goto-page page)
786 (when (and
787 doc-view-current-search-matches
788 (y-or-n-p "No more matches after current page. Wrap to first match? "))
789 (doc-view-goto-page (caar doc-view-current-search-matches))))))
791 (defun doc-view-search-previous-match (arg)
792 "Go to the ARGth previous matching page."
793 (interactive "p")
794 (let* ((prev-pages (doc-view-remove-if
795 (lambda (i) (>= (car i) doc-view-current-page))
796 doc-view-current-search-matches))
797 (page (car (nth (1- arg) (nreverse prev-pages)))))
798 (if page
799 (doc-view-goto-page page)
800 (when (and
801 doc-view-current-search-matches
802 (y-or-n-p "No more matches before current page. Wrap to last match? "))
803 (doc-view-goto-page (caar (last doc-view-current-search-matches)))))))
805 ;;;; User interface commands and the mode
807 ;; (put 'doc-view-mode 'mode-class 'special)
809 (defun doc-view-initiate-display ()
810 ;; Switch to image display if possible
811 (if (and (display-images-p)
812 (image-type-available-p 'png))
813 (progn
814 (doc-view-buffer-message)
815 (setq doc-view-current-page (or doc-view-current-page 1))
816 (if (file-exists-p (doc-view-current-cache-dir))
817 (progn
818 (message "DocView: using cached files!")
819 (doc-view-display buffer-file-name))
820 (doc-view-convert-current-doc))
821 (message
822 "%s"
823 (substitute-command-keys
824 (concat "Type \\[doc-view-toggle-display] to toggle between "
825 "editing or viewing the document."))))
826 (message
827 "%s"
828 (substitute-command-keys
829 (concat "No image (png) support available. Type \\[doc-view-toggle-display] "
830 "to switch to an editing mode.")))))
832 ;;;###autoload
833 (defun doc-view-mode ()
834 "Major mode in DocView buffers.
835 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to
836 toggle between displaying the document or editing it as text."
837 (interactive)
838 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode)
839 doc-view-previous-major-mode
840 major-mode)))
841 (kill-all-local-variables)
842 (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode))
843 (make-local-variable 'doc-view-current-files)
844 (make-local-variable 'doc-view-current-image)
845 (make-local-variable 'doc-view-current-page)
846 (make-local-variable 'doc-view-current-converter-process)
847 (make-local-variable 'doc-view-current-timer)
848 (make-local-variable 'doc-view-current-slice)
849 (make-local-variable 'doc-view-current-cache-dir)
850 (make-local-variable 'doc-view-current-info)
851 (make-local-variable 'doc-view-current-search-matches)
852 (set (make-local-variable 'doc-view-current-overlay)
853 (make-overlay (point-min) (point-max) nil t))
854 (add-hook 'change-major-mode-hook
855 (lambda () (delete-overlay doc-view-current-overlay))
856 nil t)
857 (set (make-local-variable 'mode-line-position)
858 '(" P" (:eval (number-to-string doc-view-current-page))
859 "/" (:eval (number-to-string (length doc-view-current-files)))))
860 (set (make-local-variable 'cursor-type) nil)
861 (use-local-map doc-view-mode-map)
862 (set (make-local-variable 'after-revert-hook) 'doc-view-reconvert-doc)
863 (setq mode-name "DocView"
864 buffer-read-only t
865 major-mode 'doc-view-mode)
866 (doc-view-initiate-display)
867 (run-mode-hooks 'doc-view-mode-hook))
869 ;;;###autoload
870 (define-minor-mode doc-view-minor-mode
871 "Toggle Doc view minor mode.
872 With arg, turn Doc view minor mode on if arg is positive, off otherwise.
873 See the command `doc-view-mode' for more information on this mode."
874 nil " DocView" doc-view-minor-mode-map
875 :group 'doc-view
876 (when doc-view-minor-mode
877 (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
878 (message
879 "%s"
880 (substitute-command-keys
881 "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document."))))
883 (defun doc-view-clear-cache ()
884 "Delete the whole cache (`doc-view-cache-directory')."
885 (interactive)
886 (dired-delete-file doc-view-cache-directory 'always)
887 (make-directory doc-view-cache-directory))
889 (defun doc-view-dired-cache ()
890 "Open `dired' in `doc-view-cache-directory'."
891 (interactive)
892 (dired doc-view-cache-directory))
894 (provide 'doc-view)
896 ;; Local Variables:
897 ;; mode: outline-minor
898 ;; End:
900 ;; arch-tag: 5d6e5c5e-095f-489e-b4e4-1ca90a7d79be
901 ;;; doc-view.el ends here