]> code.delx.au - gnu-emacs/blob - lisp/doc-view.el
Add comments about isearch support.
[gnu-emacs] / lisp / doc-view.el
1 ;;; doc-view.el --- View PDF/PostScript/DVI files in Emacs
2
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
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 ;; - better menu.
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).
108
109 ;;;; About isearch support
110
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.
123
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
127 ;; pages to jump.
128
129 ;; Anyway, if someone with better isearch knowledge wants to give it a try,
130 ;; feel free to do it. --Tassilo
131
132 ;;; Code:
133
134 (require 'dired)
135 (require 'image-mode)
136 (require 'jka-compr)
137
138 ;;;; Customization Options
139
140 (defgroup doc-view nil
141 "In-buffer viewer for PDF, PostScript and DVI files."
142 :link '(function-link doc-view)
143 :version "22.2"
144 :group 'applications
145 :group 'multimedia
146 :prefix "doc-view-")
147
148 (defcustom doc-view-ghostscript-program (executable-find "gs")
149 "Program to convert PS and PDF files to PNG."
150 :type 'file
151 :group 'doc-view)
152
153 (defcustom doc-view-ghostscript-options
154 '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
155 ;; sources.
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)
160 :group 'doc-view)
161
162 (defcustom doc-view-dvipdfm-program (executable-find "dvipdfm")
163 "Program to convert DVI files to PDF.
164
165 DVI file will be converted to PDF before the resulting PDF is
166 converted to PNG."
167 :type 'file
168 :group 'doc-view)
169
170 (defcustom doc-view-ps2pdf-program (executable-find "ps2pdf")
171 "Program to convert PS files to PDF.
172
173 PS files will be converted to PDF before searching is possible."
174 :type 'file
175 :group 'doc-view)
176
177 (defcustom doc-view-pdftotext-program (executable-find "pdftotext")
178 "Program to convert PDF files to plain text.
179
180 Needed for searching."
181 :type 'file
182 :group 'doc-view)
183
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."
188 :type 'directory
189 :group 'doc-view)
190
191 (defcustom doc-view-conversion-buffer "*doc-view conversion output*"
192 "The buffer where messages from the converter programs go to."
193 :type 'string
194 :group 'doc-view)
195
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
201 has finished."
202 :type 'integer
203 :group 'doc-view)
204
205 ;;;; Internal Variables
206
207 (defvar doc-view-current-files nil
208 "Only used internally.")
209
210 (defvar doc-view-current-page nil
211 "Only used internally.")
212
213 (defvar doc-view-current-converter-process nil
214 "Only used internally.")
215
216 (defvar doc-view-current-timer nil
217 "Only used internally.")
218
219 (defvar doc-view-current-slice nil
220 "Only used internally.")
221
222 (defvar doc-view-current-cache-dir nil
223 "Only used internally.")
224
225 (defvar doc-view-current-search-matches nil
226 "Only used internally.")
227
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)
232
233 (defvar doc-view-current-info nil
234 "Only used internally.")
235
236 (defvar doc-view-previous-major-mode nil
237 "Only used internally.")
238
239 ;;;; DocView Keymaps
240
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)
260 ;; Slicing the image
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)
264 ;; Searching
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)
268 ;; Scrolling
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)
273 ;; Show the tooltip
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)
280 map)
281 "Keymap used by `doc-view-mode' when displaying a doc as a set of images.")
282
283 (easy-menu-define doc-view-menu doc-view-mode-map
284 "Menu for Doc View mode."
285 '("DocView"
286 ["Set Slice" doc-view-set-slice-using-mouse]
287 ["Set Slice (manual)" doc-view-set-slice]
288 ["Reset Slice" doc-view-reset-slice]
289 "---"
290 ["Search" doc-view-search]
291 ["Search Backwards" doc-view-search-backward]
292 ["Toggle display" doc-view-toggle-display]
293 ))
294
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)
299 map)
300 "Keymap used by `doc-minor-view-mode'.")
301
302 ;;;; Navigation Commands
303
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)))
308 (if (< page 1)
309 (setq page 1)
310 (when (> page len)
311 (setq page len)))
312 (setq doc-view-current-page page
313 doc-view-current-info
314 (concat
315 (propertize
316 (format "Page %d of %d."
317 doc-view-current-page
318 len) 'face 'bold)
319 ;; Tell user if converting isn't finished yet
320 (if doc-view-current-converter-process
321 " (still converting...)\n"
322 "\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)
328 (let ((contexts ""))
329 (dolist (m (cdr (assq doc-view-current-page
330 doc-view-current-search-matches)))
331 (setq contexts (concat contexts " - \"" m "\"\n")))
332 contexts)))))
333 ;; Update the buffer
334 (doc-view-insert-image (nth (1- page) doc-view-current-files)
335 :pointer 'arrow)
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
341 (forward-char)))
342
343 (defun doc-view-next-page (&optional arg)
344 "Browse ARG pages forward."
345 (interactive "p")
346 (doc-view-goto-page (+ doc-view-current-page (or arg 1))))
347
348 (defun doc-view-previous-page (&optional arg)
349 "Browse ARG pages backward."
350 (interactive "p")
351 (doc-view-goto-page (- doc-view-current-page (or arg 1))))
352
353 (defun doc-view-first-page ()
354 "View the first page."
355 (interactive)
356 (doc-view-goto-page 1))
357
358 (defun doc-view-last-page ()
359 "View the last page."
360 (interactive)
361 (doc-view-goto-page (length doc-view-current-files)))
362
363 (defun doc-view-scroll-up-or-next-page ()
364 "Scroll page up if possible, else goto next page."
365 (interactive)
366 (condition-case nil
367 (scroll-up)
368 (error (doc-view-next-page))))
369
370 (defun doc-view-scroll-down-or-previous-page ()
371 "Scroll page down if possible, else goto previous page."
372 (interactive)
373 (condition-case nil
374 (scroll-down)
375 (error (doc-view-previous-page)
376 (goto-char (point-max)))))
377
378 ;;;; Utility Functions
379
380 (defun doc-view-kill-proc ()
381 "Kill the current converter process."
382 (interactive)
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))
390
391 (defun doc-view-kill-proc-and-buffer ()
392 "Kill the current converter process and buffer."
393 (interactive)
394 (doc-view-kill-proc)
395 (when (eq major-mode 'doc-view-mode)
396 (kill-buffer (current-buffer))))
397
398 (defun doc-view-make-safe-dir (dir)
399 (condition-case nil
400 (let ((umask (default-file-modes)))
401 (unwind-protect
402 (progn
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))
408 ;; Reset the umask.
409 (set-default-file-modes umask)))
410 (file-already-exists
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))))
422
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
433 (expand-file-name
434 (let ((doc buffer-file-name))
435 (concat (file-name-nondirectory doc)
436 "-"
437 (with-temp-buffer
438 (insert-file-contents-literally doc)
439 (md5 (current-buffer)))))
440 doc-view-cache-directory)))))
441
442 (defun doc-view-remove-if (predicate list)
443 "Return LIST with all items removed that satisfy PREDICATE."
444 (let (new-list)
445 (dolist (item list (nreverse new-list))
446 (when (not (funcall predicate item))
447 (setq new-list (cons item new-list))))))
448
449 ;;;; Conversion Functions
450
451 (defun doc-view-reconvert-doc ()
452 "Reconvert the current document.
453 Should be invoked when the cached images aren't up-to-date."
454 (interactive)
455 (doc-view-kill-proc)
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))
460
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))))
473
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
479 "-o" pdf dvi)
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))
485
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)))
498
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))
507 (list pdf-ps)))
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
516 'doc-view-display
517 buffer-file-name))))
518
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)))))
531
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"
537 pdf txt)
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)))
542
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)))))
553
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.
561 "-dSAFER"
562 ;; in-file and out-file
563 ps pdf)
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))
569
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
587 ;; it.
588 (doc-view-dvi->pdf buffer-file-name
589 (expand-file-name "doc.pdf"
590 doc-view-current-cache-dir)))))
591
592 ;;;; Slicing
593
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.
599
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'."
602 (interactive
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)))))
608 (list a b c d)))
609 (setq doc-view-current-slice (list x y width height))
610 ;; Redisplay
611 (doc-view-goto-page doc-view-current-page))
612
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'."
618 (interactive)
619 (let (x y w h done)
620 (while (not done)
621 (let ((e (read-event
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))
629 (setq done t))))
630 (doc-view-set-slice x y w h)))
631
632 (defun doc-view-reset-slice ()
633 "Reset the current slice.
634 After calling this function whole pages will be visible again."
635 (interactive)
636 (setq doc-view-current-slice nil)
637 ;; Redisplay
638 (doc-view-goto-page doc-view-current-page))
639
640 ;;;; Display
641
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
646 (clear-image-cache)
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)
654 image))))
655
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))
661 (string< a b))))
662
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)
669 'doc-view-sort))
670 (when (> (length doc-view-current-files) 0)
671 (doc-view-goto-page doc-view-current-page)))
672
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)
680 "\n"
681 "
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.
685
686 For now these keys are useful:
687
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"))))
691
692 (defun doc-view-show-tooltip ()
693 (interactive)
694 (tooltip-show doc-view-current-info))
695
696 ;;;;; Toggle between editing and viewing
697
698 (defun doc-view-toggle-display ()
699 "Toggle between editing a document as text or viewing it."
700 (interactive)
701 (if (eq major-mode 'doc-view-mode)
702 ;; Switch to editing mode
703 (progn
704 (doc-view-kill-proc)
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
708 ;; mode.
709 (if doc-view-previous-major-mode
710 (funcall doc-view-previous-major-mode)
711 (fundamental-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? "))
716 (save-buffer))
717 (doc-view-mode)))
718
719 ;;;; Searching
720
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."
725 (with-temp-buffer
726 (insert-file-contents file)
727 (let ((page 1)
728 (lastpage 1)
729 matches)
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)
735 (push (cons page
736 (list (buffer-substring
737 (line-beginning-position)
738 (line-end-position))))
739 matches)
740 (setq matches (cons
741 (append
742 (or
743 ;; This page already is a match.
744 (car matches)
745 ;; This is the first match on page.
746 (list page))
747 (list (buffer-substring
748 (line-beginning-position)
749 (line-end-position))))
750 (cdr matches))))
751 (setq lastpage page)))
752 (nreverse matches))))
753
754 (defun doc-view-search-no-of-matches (list)
755 "Extract the number of matches from the search result LIST."
756 (let ((no 0))
757 (dolist (p list)
758 (setq no (+ no (1- (length p)))))
759 no))
760
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."
764 (interactive "P")
765 (doc-view-search arg t))
766
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."
772 (interactive "P")
773 (if (and (not arg)
774 doc-view-current-search-matches)
775 (if backward
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)
783 (progn
784 (setq doc-view-current-search-matches
785 (doc-view-search-internal
786 (read-from-minibuffer "Regexp: ")
787 txt))
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)))
795 (cond
796 ((string= ext "pdf")
797 ;; Doc is a PDF, so convert it to TXT
798 (doc-view-pdf->txt buffer-file-name txt))
799 ((string= ext "ps")
800 ;; Doc is a PS, so convert it to PDF (which will be converted to
801 ;; TXT thereafter).
802 (doc-view-ps->pdf buffer-file-name
803 (expand-file-name "doc.pdf"
804 (doc-view-current-cache-dir))))
805 ((string= ext "dvi")
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))
810 txt))
811 (t (error "DocView doesn't know what to do")))))))))
812
813 (defun doc-view-search-next-match (arg)
814 "Go to the ARGth next matching page."
815 (interactive "p")
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))))
820 (if page
821 (doc-view-goto-page page)
822 (when (and
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))))))
826
827 (defun doc-view-search-previous-match (arg)
828 "Go to the ARGth previous matching page."
829 (interactive "p")
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)))))
834 (if page
835 (doc-view-goto-page page)
836 (when (and
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)))))))
840
841 ;;;; User interface commands and the mode
842
843 ;; (put 'doc-view-mode 'mode-class 'special)
844
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))
849 (progn
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))
853 (progn
854 (message "DocView: using cached files!")
855 (doc-view-display buffer-file-name))
856 (doc-view-convert-current-doc))
857 (message
858 "%s"
859 (substitute-command-keys
860 (concat "Type \\[doc-view-toggle-display] to toggle between "
861 "editing or viewing the document."))))
862 (message
863 "%s"
864 (substitute-command-keys
865 (concat "No image (png) support available. Type \\[doc-view-toggle-display] "
866 "to switch to an editing mode.")))))
867
868 ;;;###autoload
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."
873 (interactive)
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
879 "File: "
880 (file-name-directory buffer-file-name))))
881 (write-region (point-min) (point-max) file)
882 (kill-buffer nil)
883 (find-file file)
884 (doc-view-mode)))
885 (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode)
886 doc-view-previous-major-mode
887 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))
903 nil t)
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"
911 buffer-read-only t
912 major-mode 'doc-view-mode)
913 (doc-view-initiate-display)
914 (run-mode-hooks 'doc-view-mode-hook)))
915
916 ;;;###autoload
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
922 :group 'doc-view
923 (when doc-view-minor-mode
924 (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
925 (message
926 "%s"
927 (substitute-command-keys
928 "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document."))))
929
930 (defun doc-view-clear-cache ()
931 "Delete the whole cache (`doc-view-cache-directory')."
932 (interactive)
933 (dired-delete-file doc-view-cache-directory 'always)
934 (make-directory doc-view-cache-directory))
935
936 (defun doc-view-dired-cache ()
937 "Open `dired' in `doc-view-cache-directory'."
938 (interactive)
939 (dired doc-view-cache-directory))
940
941 (provide 'doc-view)
942
943 ;; Local Variables:
944 ;; mode: outline-minor
945 ;; End:
946
947 ;; arch-tag: 5d6e5c5e-095f-489e-b4e4-1ca90a7d79be
948 ;;; doc-view.el ends here