1 ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs
3 ;; Copyright (C) 2007 Free Software Foundation, Inc.
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)
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.
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/).
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
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 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.
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'.
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.
96 ;; M-x customize-group RET doc-view RET
98 ;; and modify them to your needs.
103 ;; - don't use `find-file'.
104 ;; - Bind slicing to a drag event.
105 ;; - zoom (the whole document and/or just the region around the cursor).
106 ;; - get rid of the silly arrow in the fringe.
107 ;; - improve anti-aliasing (pdf-utils gets it better).
109 ;;;; About isearch support
111 ;; I tried implementing isearch by setting
112 ;; `isearch-search-fun-function' buffer-locally, but that didn't
113 ;; work too good. The function doing the real search was called
114 ;; endlessly somehow. But even if we'd get that working no real
115 ;; isearch feeling comes up due to the missing match highlighting.
116 ;; Currently I display all lines containing a match in a tooltip and
117 ;; each C-s or C-r jumps directly to the next/previous page with a
118 ;; match. With isearch we could only display the current match. So
119 ;; we had to decide if another C-s jumps to the next page with a
120 ;; match (thus only the first match in a page will be displayed in a
121 ;; tooltip) or to the next match, which would do nothing visible
122 ;; (except the tooltip) if the next match is on the same page.
124 ;; And it's much slower than the current search facility, because
125 ;; isearch really searches for each step forward or backward wheras
126 ;; the current approach searches once and then it knows to which
129 ;; Anyway, if someone with better isearch knowledge wants to give it a try,
130 ;; feel free to do it. --Tassilo
135 (require 'image-mode)
138 ;;;; Customization Options
140 (defgroup doc-view nil
141 "In-buffer viewer for PDF, PostScript and DVI files."
142 :link '(function-link doc-view)
148 (defcustom doc-view-ghostscript-program (executable-find "gs")
149 "Program to convert PS and PDF files to PNG."
153 (defcustom doc-view-ghostscript-options
154 '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
156 "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
157 "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET" "-r100")
158 "A list of options to give to ghostscript."
159 :type '(repeat string)
162 (defcustom doc-view-dvipdfm-program (executable-find "dvipdfm")
163 "Program to convert DVI files to PDF.
165 DVI file will be converted to PDF before the resulting PDF is
170 (defcustom doc-view-ps2pdf-program (executable-find "ps2pdf")
171 "Program to convert PS files to PDF.
173 PS files will be converted to PDF before searching is possible."
177 (defcustom doc-view-pdftotext-program (executable-find "pdftotext")
178 "Program to convert PDF files to plain text.
180 Needed for searching."
184 (defcustom doc-view-cache-directory
185 (expand-file-name (format "docview%d" (user-uid))
186 temporary-file-directory)
187 "The base directory, where the PNG images will be saved."
191 (defcustom doc-view-conversion-buffer "*doc-view conversion output*"
192 "The buffer where messages from the converter programs go to."
196 (defcustom doc-view-conversion-refresh-interval 3
197 "Interval in seconds between refreshes of the DocView buffer while converting.
198 After such a refresh newly converted pages will be available for
199 viewing. If set to nil there won't be any refreshes and the
200 pages won't be displayed before conversion of the whole document
205 ;;;; Internal Variables
207 (defvar doc-view-current-files nil
208 "Only used internally.")
210 (defvar doc-view-current-page nil
211 "Only used internally.")
213 (defvar doc-view-current-converter-process nil
214 "Only used internally.")
216 (defvar doc-view-current-timer nil
217 "Only used internally.")
219 (defvar doc-view-current-slice nil
220 "Only used internally.")
222 (defvar doc-view-current-cache-dir nil
223 "Only used internally.")
225 (defvar doc-view-current-search-matches nil
226 "Only used internally.")
228 (defvar doc-view-current-image nil
229 "Only used internally.")
230 (defvar doc-view-current-overlay)
231 (defvar doc-view-pending-cache-flush nil)
233 (defvar doc-view-current-info nil
234 "Only used internally.")
236 (defvar doc-view-previous-major-mode nil
237 "Only used internally.")
241 (defvar doc-view-mode-map
242 (let ((map (make-sparse-keymap)))
243 (suppress-keymap map)
244 ;; Navigation in the document
245 (define-key map (kbd "n") 'doc-view-next-page)
246 (define-key map (kbd "p") 'doc-view-previous-page)
247 (define-key map (kbd "<next>") 'forward-page)
248 (define-key map (kbd "<prior>") 'backward-page)
249 (define-key map [remap forward-page] 'doc-view-next-page)
250 (define-key map [remap backward-page] 'doc-view-previous-page)
251 (define-key map (kbd "SPC") 'doc-view-scroll-up-or-next-page)
252 (define-key map (kbd "DEL") 'doc-view-scroll-down-or-previous-page)
253 (define-key map (kbd "M-<") 'doc-view-first-page)
254 (define-key map (kbd "M->") 'doc-view-last-page)
255 (define-key map [remap goto-line] 'doc-view-goto-page)
256 ;; Killing/burying the buffer (and the process)
257 (define-key map (kbd "q") 'bury-buffer)
258 (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer)
259 (define-key map (kbd "K") 'doc-view-kill-proc)
261 (define-key map (kbd "s s") 'doc-view-set-slice)
262 (define-key map (kbd "s m") 'doc-view-set-slice-using-mouse)
263 (define-key map (kbd "s r") 'doc-view-reset-slice)
265 (define-key map (kbd "C-s") 'doc-view-search)
266 (define-key map (kbd "<find>") 'doc-view-search)
267 (define-key map (kbd "C-r") 'doc-view-search-backward)
269 (define-key map [remap forward-char] 'image-forward-hscroll)
270 (define-key map [remap backward-char] 'image-backward-hscroll)
271 (define-key map [remap next-line] 'image-next-line)
272 (define-key map [remap previous-line] 'image-previous-line)
274 (define-key map (kbd "C-t") 'doc-view-show-tooltip)
275 ;; Toggle between text and image display or editing
276 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
277 ;; Reconvert the current document
278 (define-key map (kbd "g") 'revert-buffer)
279 (define-key map (kbd "r") 'revert-buffer)
281 "Keymap used by `doc-view-mode' when displaying a doc as a set of images.")
283 (easy-menu-define doc-view-menu doc-view-mode-map
284 "Menu for Doc View mode."
286 ["Set Slice" doc-view-set-slice-using-mouse]
287 ["Set Slice (manual)" doc-view-set-slice]
288 ["Reset Slice" doc-view-reset-slice]
290 ["Search" doc-view-search]
291 ["Search Backwards" doc-view-search-backward]
292 ["Toggle display" doc-view-toggle-display]
295 (defvar doc-view-minor-mode-map
296 (let ((map (make-sparse-keymap)))
297 ;; Toggle between text and image display or editing
298 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
300 "Keymap used by `doc-minor-view-mode'.")
302 ;;;; Navigation Commands
304 (defun doc-view-goto-page (page)
305 "View the page given by PAGE."
306 (interactive "nPage: ")
307 (let ((len (length doc-view-current-files)))
312 (setq doc-view-current-page page
313 doc-view-current-info
316 (format "Page %d of %d."
317 doc-view-current-page
319 ;; Tell user if converting isn't finished yet
320 (if doc-view-current-converter-process
321 " (still converting...)\n"
323 ;; Display context infos if this page matches the last search
324 (when (and doc-view-current-search-matches
325 (assq doc-view-current-page
326 doc-view-current-search-matches))
327 (concat (propertize "Search matches:\n" 'face 'bold)
329 (dolist (m (cdr (assq doc-view-current-page
330 doc-view-current-search-matches)))
331 (setq contexts (concat contexts " - \"" m "\"\n")))
334 (doc-view-insert-image (nth (1- page) doc-view-current-files)
336 (overlay-put doc-view-current-overlay 'help-echo doc-view-current-info)
337 (goto-char (point-min))
338 ;; This seems to be needed for set-window-hscroll (in
339 ;; image-forward-hscroll) to do something useful, I don't have time to
340 ;; debug this now. :-( --Stef
343 (defun doc-view-next-page (&optional arg)
344 "Browse ARG pages forward."
346 (doc-view-goto-page (+ doc-view-current-page (or arg 1))))
348 (defun doc-view-previous-page (&optional arg)
349 "Browse ARG pages backward."
351 (doc-view-goto-page (- doc-view-current-page (or arg 1))))
353 (defun doc-view-first-page ()
354 "View the first page."
356 (doc-view-goto-page 1))
358 (defun doc-view-last-page ()
359 "View the last page."
361 (doc-view-goto-page (length doc-view-current-files)))
363 (defun doc-view-scroll-up-or-next-page ()
364 "Scroll page up if possible, else goto next page."
368 (error (doc-view-next-page))))
370 (defun doc-view-scroll-down-or-previous-page ()
371 "Scroll page down if possible, else goto previous page."
375 (error (doc-view-previous-page)
376 (goto-char (point-max)))))
378 ;;;; Utility Functions
380 (defun doc-view-kill-proc ()
381 "Kill the current converter process."
383 (when doc-view-current-converter-process
384 (kill-process doc-view-current-converter-process)
385 (setq doc-view-current-converter-process nil))
386 (when doc-view-current-timer
387 (cancel-timer doc-view-current-timer)
388 (setq doc-view-current-timer nil))
389 (setq mode-line-process nil))
391 (defun doc-view-kill-proc-and-buffer ()
392 "Kill the current converter process and buffer."
395 (when (eq major-mode 'doc-view-mode)
396 (kill-buffer (current-buffer))))
398 (defun doc-view-make-safe-dir (dir)
400 (let ((umask (default-file-modes)))
403 ;; Create temp files with strict access rights. It's easy to
404 ;; loosen them later, whereas it's impossible to close the
405 ;; time-window of loose permissions otherwise.
406 (set-default-file-modes #o0700)
407 (make-directory dir))
409 (set-default-file-modes umask)))
411 (if (file-symlink-p dir)
412 (error "Danger: %s points to a symbolic link" dir))
413 ;; In case it was created earlier with looser rights.
414 ;; We could check the mode info returned by file-attributes, but it's
415 ;; a pain to parse and it may not tell you what we want under
416 ;; non-standard file-systems. So let's just say what we want and let
417 ;; the underlying C code and file-system figure it out.
418 ;; This also ends up checking a bunch of useful conditions: it makes
419 ;; sure we have write-access to the directory and that we own it, thus
420 ;; closing a bunch of security holes.
421 (set-file-modes dir #o0700))))
423 (defun doc-view-current-cache-dir ()
424 "Return the directory where the png files of the current doc should be saved.
425 It's a subdirectory of `doc-view-cache-directory'."
426 (if doc-view-current-cache-dir
427 doc-view-current-cache-dir
428 ;; Try and make sure doc-view-cache-directory exists and is safe.
429 (doc-view-make-safe-dir doc-view-cache-directory)
430 ;; Now compute the subdirectory to use.
431 (setq doc-view-current-cache-dir
432 (file-name-as-directory
434 (let ((doc buffer-file-name))
435 (concat (file-name-nondirectory doc)
438 (insert-file-contents-literally doc)
439 (md5 (current-buffer)))))
440 doc-view-cache-directory)))))
442 (defun doc-view-remove-if (predicate list)
443 "Return LIST with all items removed that satisfy PREDICATE."
445 (dolist (item list (nreverse new-list))
446 (when (not (funcall predicate item))
447 (setq new-list (cons item new-list))))))
449 ;;;; Conversion Functions
451 (defun doc-view-reconvert-doc ()
452 "Reconvert the current document.
453 Should be invoked when the cached images aren't up-to-date."
456 ;; Clear the old cached files
457 (when (file-exists-p (doc-view-current-cache-dir))
458 (dired-delete-file (doc-view-current-cache-dir) 'always))
459 (doc-view-initiate-display))
461 (defun doc-view-dvi->pdf-sentinel (proc event)
462 "If DVI->PDF conversion was successful, convert the PDF to PNG now."
463 (if (not (string-match "finished" event))
464 (message "DocView: dvi->pdf 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 ;; Now go on converting this PDF to a set of PNG files.
469 (let* ((pdf (process-get proc 'pdf-file))
470 (png (expand-file-name "page-%d.png"
471 (doc-view-current-cache-dir))))
472 (doc-view-pdf/ps->png pdf png))))
474 (defun doc-view-dvi->pdf (dvi pdf)
475 "Convert DVI to PDF asynchronously."
476 (setq doc-view-current-converter-process
477 (start-process "dvi->pdf" doc-view-conversion-buffer
478 doc-view-dvipdfm-program
480 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
481 (set-process-sentinel doc-view-current-converter-process
482 'doc-view-dvi->pdf-sentinel)
483 (process-put doc-view-current-converter-process 'buffer (current-buffer))
484 (process-put doc-view-current-converter-process 'pdf-file pdf))
486 (defun doc-view-pdf/ps->png-sentinel (proc event)
487 "If PDF/PS->PNG conversion was successful, update the display."
488 (if (not (string-match "finished" event))
489 (message "DocView: converter process changed status to %s." event)
490 (set-buffer (process-get proc 'buffer))
491 (setq doc-view-current-converter-process nil
492 mode-line-process nil)
493 (when doc-view-current-timer
494 (cancel-timer doc-view-current-timer)
495 (setq doc-view-current-timer nil))
496 ;; Yippie, finished. Update the display!
497 (doc-view-display buffer-file-name)))
499 (defun doc-view-pdf/ps->png (pdf-ps png)
500 "Convert PDF-PS to PNG asynchronously."
501 (setq doc-view-current-converter-process
502 (apply 'start-process
503 (append (list "pdf/ps->png" doc-view-conversion-buffer
504 doc-view-ghostscript-program)
505 doc-view-ghostscript-options
506 (list (concat "-sOutputFile=" png))
508 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
509 (process-put doc-view-current-converter-process
510 'buffer (current-buffer))
511 (set-process-sentinel doc-view-current-converter-process
512 'doc-view-pdf/ps->png-sentinel)
513 (when doc-view-conversion-refresh-interval
514 (setq doc-view-current-timer
515 (run-at-time "1 secs" doc-view-conversion-refresh-interval
519 (defun doc-view-pdf->txt-sentinel (proc event)
520 (if (not (string-match "finished" event))
521 (message "DocView: converter process changed status to %s." event)
522 (let ((current-buffer (current-buffer))
523 (proc-buffer (process-get proc 'buffer)))
524 (set-buffer proc-buffer)
525 (setq doc-view-current-converter-process nil
526 mode-line-process nil)
527 ;; If the user looks at the DocView buffer where the conversion was
528 ;; performed, search anew. This time it will be queried for a regexp.
529 (when (eq current-buffer proc-buffer)
530 (doc-view-search nil)))))
532 (defun doc-view-pdf->txt (pdf txt)
533 "Convert PDF to TXT asynchronously."
534 (setq doc-view-current-converter-process
535 (start-process "pdf->txt" doc-view-conversion-buffer
536 doc-view-pdftotext-program "-raw"
538 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
539 (set-process-sentinel doc-view-current-converter-process
540 'doc-view-pdf->txt-sentinel)
541 (process-put doc-view-current-converter-process 'buffer (current-buffer)))
543 (defun doc-view-ps->pdf-sentinel (proc event)
544 (if (not (string-match "finished" event))
545 (message "DocView: converter process changed status to %s." event)
546 (set-buffer (process-get proc 'buffer))
547 (setq doc-view-current-converter-process nil
548 mode-line-process nil)
549 ;; Now we can transform to plain text.
550 (doc-view-pdf->txt (process-get proc 'pdf-file)
551 (expand-file-name "doc.txt"
552 (doc-view-current-cache-dir)))))
554 (defun doc-view-ps->pdf (ps pdf)
555 "Convert PS to PDF asynchronously."
556 (setq doc-view-current-converter-process
557 (start-process "ps->pdf" doc-view-conversion-buffer
558 doc-view-ps2pdf-program
559 ;; Avoid security problems when rendering files from
560 ;; untrusted sources.
562 ;; in-file and out-file
564 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
565 (set-process-sentinel doc-view-current-converter-process
566 'doc-view-ps->pdf-sentinel)
567 (process-put doc-view-current-converter-process 'buffer (current-buffer))
568 (process-put doc-view-current-converter-process 'pdf-file pdf))
570 (defun doc-view-convert-current-doc ()
571 "Convert `buffer-file-name' to a set of png files, one file per page.
572 Those files are saved in the directory given by the function
573 `doc-view-current-cache-dir'."
574 ;; Let stale files still display while we recompute the new ones, so only
575 ;; flush the cache when the conversion is over. One of the reasons why it
576 ;; is important to keep displaying the stale page is so that revert-buffer
577 ;; preserves the horizontal/vertical scroll settings (which are otherwise
578 ;; resets during the redisplay).
579 (setq doc-view-pending-cache-flush t)
580 (let ((png-file (expand-file-name "page-%d.png"
581 (doc-view-current-cache-dir))))
582 (make-directory (doc-view-current-cache-dir))
583 (if (not (string= (file-name-extension buffer-file-name) "dvi"))
584 ;; Convert to PNG images.
585 (doc-view-pdf/ps->png buffer-file-name png-file)
586 ;; DVI files have to be converted to PDF before Ghostscript can process
588 (doc-view-dvi->pdf buffer-file-name
589 (expand-file-name "doc.pdf"
590 doc-view-current-cache-dir)))))
594 (defun doc-view-set-slice (x y width height)
595 "Set the slice of the images that should be displayed.
596 You can use this function to tell doc-view not to display the
597 margins of the document. It prompts for the top-left corner (X
598 and Y) of the slice to display and its WIDTH and HEIGHT.
600 See `doc-view-set-slice-using-mouse' for a more convenient way to
601 do that. To reset the slice use `doc-view-reset-slice'."
603 (let* ((size (image-size doc-view-current-image t))
604 (a (read-number (format "Top-left X (0..%d): " (car size))))
605 (b (read-number (format "Top-left Y (0..%d): " (cdr size))))
606 (c (read-number (format "Width (0..%d): " (- (car size) a))))
607 (d (read-number (format "Height (0..%d): " (- (cdr size) b)))))
609 (setq doc-view-current-slice (list x y width height))
611 (doc-view-goto-page doc-view-current-page))
613 (defun doc-view-set-slice-using-mouse ()
614 "Set the slice of the images that should be displayed.
615 You set the slice by pressing mouse-1 at its top-left corner and
616 dragging it to its bottom-right corner. See also
617 `doc-view-set-slice' and `doc-view-reset-slice'."
622 (concat "Press mouse-1 at the top-left corner and "
623 "drag it to the bottom-right corner!"))))
624 (when (eq (car e) 'drag-mouse-1)
625 (setq x (car (posn-object-x-y (event-start e))))
626 (setq y (cdr (posn-object-x-y (event-start e))))
627 (setq w (- (car (posn-object-x-y (event-end e))) x))
628 (setq h (- (cdr (posn-object-x-y (event-end e))) y))
630 (doc-view-set-slice x y w h)))
632 (defun doc-view-reset-slice ()
633 "Reset the current slice.
634 After calling this function whole pages will be visible again."
636 (setq doc-view-current-slice nil)
638 (doc-view-goto-page doc-view-current-page))
642 (defun doc-view-insert-image (file &rest args)
643 "Insert the given png FILE.
644 ARGS is a list of image descriptors."
645 (when doc-view-pending-cache-flush
647 (setq doc-view-pending-cache-flush nil))
648 (let ((image (apply 'create-image file 'png nil args)))
649 (setq doc-view-current-image image)
650 (move-overlay doc-view-current-overlay (point-min) (point-max))
651 (overlay-put doc-view-current-overlay 'display
652 (if doc-view-current-slice
653 (list (cons 'slice doc-view-current-slice) image)
656 (defun doc-view-sort (a b)
657 "Return non-nil if A should be sorted before B.
658 Predicate for sorting `doc-view-current-files'."
659 (or (< (length a) (length b))
660 (and (= (length a) (length b))
663 (defun doc-view-display (doc)
664 "Start viewing the document DOC."
665 (set-buffer (get-file-buffer doc))
666 (setq doc-view-current-files
667 (sort (directory-files (doc-view-current-cache-dir) t
668 "page-[0-9]+\\.png" t)
670 (when (> (length doc-view-current-files) 0)
671 (doc-view-goto-page doc-view-current-page)))
673 (defun doc-view-buffer-message ()
674 ;; Only show this message initially, not when refreshing the buffer (in which
675 ;; case it's better to keep displaying the "stale" page while computing
676 ;; the fresh new ones).
677 (unless (overlay-get doc-view-current-overlay 'display)
678 (overlay-put doc-view-current-overlay 'display
679 (concat (propertize "Welcome to DocView!" 'face 'bold)
682 If you see this buffer it means that the document you want to view is being
683 converted to PNG and the conversion of the first page hasn't finished yet or
684 `doc-view-conversion-refresh-interval' is set to nil.
686 For now these keys are useful:
688 `q' : Bury this buffer. Conversion will go on in background.
689 `k' : Kill the conversion process and this buffer.
690 `K' : Kill the conversion process.\n"))))
692 (defun doc-view-show-tooltip ()
694 (tooltip-show doc-view-current-info))
696 ;;;;; Toggle between editing and viewing
698 (defun doc-view-toggle-display ()
699 "Toggle between editing a document as text or viewing it."
701 (if (eq major-mode 'doc-view-mode)
702 ;; Switch to editing mode
705 (setq buffer-read-only nil)
706 (delete-overlay doc-view-current-overlay)
707 ;; Switch to the previously used major mode or fall back to fundamental
709 (if doc-view-previous-major-mode
710 (funcall doc-view-previous-major-mode)
712 (doc-view-minor-mode 1))
713 ;; Switch to doc-view-mode
714 (when (and (buffer-modified-p)
715 (y-or-n-p "The buffer has been modified. Save the changes? "))
721 (defun doc-view-search-internal (regexp file)
722 "Return a list of FILE's pages that contain text matching REGEXP.
723 The value is an alist of the form (PAGE CONTEXTS) where PAGE is
724 the pagenumber and CONTEXTS are all lines of text containing a match."
726 (insert-file-contents file)
730 (while (re-search-forward (concat "\\(?:\\([
\f]\\)\\|\\("
731 regexp "\\)\\)") nil t)
732 (when (match-string 1) (incf page))
733 (when (match-string 2)
734 (if (/= page lastpage)
736 (list (buffer-substring
737 (line-beginning-position)
738 (line-end-position))))
743 ;; This page already is a match.
745 ;; This is the first match on page.
747 (list (buffer-substring
748 (line-beginning-position)
749 (line-end-position))))
751 (setq lastpage page)))
752 (nreverse matches))))
754 (defun doc-view-search-no-of-matches (list)
755 "Extract the number of matches from the search result LIST."
758 (setq no (+ no (1- (length p)))))
761 (defun doc-view-search-backward (new-query)
762 "Call `doc-view-search' for backward search.
763 If prefix NEW-QUERY is given, ask for a new regexp."
765 (doc-view-search arg t))
767 (defun doc-view-search (new-query &optional backward)
768 "Jump to the next match or initiate a new search if NEW-QUERY is given.
769 If the current document hasn't been transformed to plain text
770 till now do that first.
771 If BACKWARD is non-nil, jump to the previous match."
774 doc-view-current-search-matches)
776 (doc-view-search-previous-match 1)
777 (doc-view-search-next-match 1))
778 ;; New search, so forget the old results.
779 (setq doc-view-current-search-matches nil)
780 (let ((txt (expand-file-name "doc.txt"
781 (doc-view-current-cache-dir))))
782 (if (file-readable-p txt)
784 (setq doc-view-current-search-matches
785 (doc-view-search-internal
786 (read-from-minibuffer "Regexp: ")
788 (message "DocView: search yielded %d matches."
789 (doc-view-search-no-of-matches
790 doc-view-current-search-matches)))
791 ;; We must convert to TXT first!
792 (if doc-view-current-converter-process
793 (message "DocView: please wait till conversion finished.")
794 (let ((ext (file-name-extension buffer-file-name)))
797 ;; Doc is a PDF, so convert it to TXT
798 (doc-view-pdf->txt buffer-file-name txt))
800 ;; Doc is a PS, so convert it to PDF (which will be converted to
802 (doc-view-ps->pdf buffer-file-name
803 (expand-file-name "doc.pdf"
804 (doc-view-current-cache-dir))))
806 ;; Doc is a DVI. This means that a doc.pdf already exists in its
807 ;; cache subdirectory.
808 (doc-view-pdf->txt (expand-file-name "doc.pdf"
809 (doc-view-current-cache-dir))
811 (t (error "DocView doesn't know what to do")))))))))
813 (defun doc-view-search-next-match (arg)
814 "Go to the ARGth next matching page."
816 (let* ((next-pages (doc-view-remove-if
817 (lambda (i) (<= (car i) doc-view-current-page))
818 doc-view-current-search-matches))
819 (page (car (nth (1- arg) next-pages))))
821 (doc-view-goto-page page)
823 doc-view-current-search-matches
824 (y-or-n-p "No more matches after current page. Wrap to first match? "))
825 (doc-view-goto-page (caar doc-view-current-search-matches))))))
827 (defun doc-view-search-previous-match (arg)
828 "Go to the ARGth previous matching page."
830 (let* ((prev-pages (doc-view-remove-if
831 (lambda (i) (>= (car i) doc-view-current-page))
832 doc-view-current-search-matches))
833 (page (car (nth (1- arg) (nreverse prev-pages)))))
835 (doc-view-goto-page page)
837 doc-view-current-search-matches
838 (y-or-n-p "No more matches before current page. Wrap to last match? "))
839 (doc-view-goto-page (caar (last doc-view-current-search-matches)))))))
841 ;;;; User interface commands and the mode
843 ;; (put 'doc-view-mode 'mode-class 'special)
845 (defun doc-view-initiate-display ()
846 ;; Switch to image display if possible
847 (if (and (display-images-p)
848 (image-type-available-p 'png))
850 (doc-view-buffer-message)
851 (setq doc-view-current-page (or doc-view-current-page 1))
852 (if (file-exists-p (doc-view-current-cache-dir))
854 (message "DocView: using cached files!")
855 (doc-view-display buffer-file-name))
856 (doc-view-convert-current-doc))
859 (substitute-command-keys
860 (concat "Type \\[doc-view-toggle-display] to toggle between "
861 "editing or viewing the document."))))
864 (substitute-command-keys
865 (concat "No image (png) support available. Type \\[doc-view-toggle-display] "
866 "to switch to an editing mode.")))))
869 (defun doc-view-mode ()
870 "Major mode in DocView buffers.
871 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to
872 toggle between displaying the document or editing it as text."
874 (if jka-compr-really-do-compress
875 ;; This is a compressed file uncompressed by auto-compression-mode.
876 (when (y-or-n-p (concat "DocView: Cannot convert compressed file. "
877 "Save it uncompressed first? "))
878 (let ((file (read-file-name
880 (file-name-directory buffer-file-name))))
881 (write-region (point-min) (point-max) file)
885 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode)
886 doc-view-previous-major-mode
888 (kill-all-local-variables)
889 (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode))
890 (make-local-variable 'doc-view-current-files)
891 (make-local-variable 'doc-view-current-image)
892 (make-local-variable 'doc-view-current-page)
893 (make-local-variable 'doc-view-current-converter-process)
894 (make-local-variable 'doc-view-current-timer)
895 (make-local-variable 'doc-view-current-slice)
896 (make-local-variable 'doc-view-current-cache-dir)
897 (make-local-variable 'doc-view-current-info)
898 (make-local-variable 'doc-view-current-search-matches)
899 (set (make-local-variable 'doc-view-current-overlay)
900 (make-overlay (point-min) (point-max) nil t))
901 (add-hook 'change-major-mode-hook
902 (lambda () (delete-overlay doc-view-current-overlay))
904 (set (make-local-variable 'mode-line-position)
905 '(" P" (:eval (number-to-string doc-view-current-page))
906 "/" (:eval (number-to-string (length doc-view-current-files)))))
907 (set (make-local-variable 'cursor-type) nil)
908 (use-local-map doc-view-mode-map)
909 (set (make-local-variable 'after-revert-hook) 'doc-view-reconvert-doc)
910 (setq mode-name "DocView"
912 major-mode 'doc-view-mode)
913 (doc-view-initiate-display)
914 (run-mode-hooks 'doc-view-mode-hook)))
917 (define-minor-mode doc-view-minor-mode
918 "Toggle Doc view minor mode.
919 With arg, turn Doc view minor mode on if arg is positive, off otherwise.
920 See the command `doc-view-mode' for more information on this mode."
921 nil " DocView" doc-view-minor-mode-map
923 (when doc-view-minor-mode
924 (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
927 (substitute-command-keys
928 "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document."))))
930 (defun doc-view-clear-cache ()
931 "Delete the whole cache (`doc-view-cache-directory')."
933 (dired-delete-file doc-view-cache-directory 'always)
934 (make-directory doc-view-cache-directory))
936 (defun doc-view-dired-cache ()
937 "Open `dired' in `doc-view-cache-directory'."
939 (dired doc-view-cache-directory))
944 ;; mode: outline-minor
947 ;; arch-tag: 5d6e5c5e-095f-489e-b4e4-1ca90a7d79be
948 ;;; doc-view.el ends here