Commit | Line | Data |
---|---|---|
94dbe99c TTN |
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 | |
94dbe99c TTN |
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 | ||
4b378e75 RS |
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 | |
640602f7 | 30 | ;; (http://www.foolabs.com/xpdf/) or poppler (http://poppler.freedesktop.org/). |
94dbe99c TTN |
31 | |
32 | ;;; Commentary: | |
33 | ||
34 | ;; DocView is a document viewer for Emacs. It converts PDF, PS and DVI files | |
35 | ;; to a set of PNG files, one PNG for each page, and displays the PNG images | |
36 | ;; inside an Emacs buffer. This buffer uses `doc-view-mode' which provides | |
37 | ;; convenient key bindings for browsing the document. | |
38 | ;; | |
640602f7 | 39 | ;; To use it simply open a document file with |
94dbe99c | 40 | ;; |
640602f7 | 41 | ;; C-x C-f ~/path/to/document RET |
94dbe99c | 42 | ;; |
640602f7 RS |
43 | ;; and the document will be converted and displayed, if your emacs supports png |
44 | ;; images. With `C-c C-c' you can toggle between the rendered images | |
45 | ;; representation and the source text representation of the document. With | |
46 | ;; `C-c C-e' you can switch to an appropriate editing mode for the document. | |
94dbe99c TTN |
47 | ;; |
48 | ;; Since conversion may take some time all the PNG images are cached in a | |
49 | ;; subdirectory of `doc-view-cache-directory' and reused when you want to view | |
640602f7 RS |
50 | ;; that file again. To reconvert a document hit `g' (`doc-view-reconvert-doc') |
51 | ;; when displaying the document. To delete all cached files use | |
94dbe99c TTN |
52 | ;; `doc-view-clear-cache'. To open the cache with dired, so that you can tidy |
53 | ;; it out use `doc-view-dired-cache'. | |
54 | ;; | |
55 | ;; When conversion in underway the first page will be displayed as soon as it | |
56 | ;; is available and the available pages are refreshed every | |
57 | ;; `doc-view-conversion-refresh-interval' seconds. If that variable is nil the | |
58 | ;; pages won't be displayed before conversion of the document finished | |
59 | ;; completely. | |
60 | ;; | |
61 | ;; DocView lets you select a slice of the displayed pages. This slice will be | |
62 | ;; remembered and applied to all pages of the current document. This enables | |
63 | ;; you to cut away the margins of a document to save some space. To select a | |
64 | ;; slice you can use `doc-view-set-slice' (bound to `s s') which will query you | |
65 | ;; for the coordinates of the slice's top-left corner and its width and height. | |
66 | ;; A much more convenient way to do the same is offered by the command | |
67 | ;; `doc-view-set-slice-using-mouse' (bound to `s m'). After invokation you | |
68 | ;; only have to press mouse-1 at the top-left corner and drag it to the | |
69 | ;; bottom-right corner of the desired slice. To reset the slice use | |
70 | ;; `doc-view-reset-slice' (bound to `s r'). | |
71 | ;; | |
94dbe99c TTN |
72 | ;; You can also search within the document. The command `doc-view-search' |
73 | ;; (bound to `C-s') queries for a search regexp and initializes a list of all | |
74 | ;; matching pages and messages how many match-pages were found. After that you | |
75 | ;; can jump to the next page containing a match with | |
76 | ;; `doc-view-search-next-match' (bound to `C-S-n') or to the previous matching | |
77 | ;; page with `doc-view-search-previous-match' (bound to `C-S-p'). This works | |
78 | ;; by searching a plain text representation of the document. If that doesn't | |
79 | ;; already exist the first invokation of `doc-view-search' starts the | |
80 | ;; conversion. When that finishes and you're still viewing the document | |
81 | ;; (i.e. you didn't switch to another buffer) you're queried for the regexp | |
82 | ;; then. | |
640602f7 RS |
83 | ;; |
84 | ;; Dired users can simply hit `v' on a document file. If it's a PS, PDF or DVI | |
85 | ;; it will be opened using `doc-view-mode'. | |
86 | ;; | |
94dbe99c TTN |
87 | |
88 | ;;; Configuration: | |
89 | ||
640602f7 RS |
90 | ;; If the images are too small or too big you should set the "-rXXX" option in |
91 | ;; `doc-view-ghostscript-options' to another value. (The bigger your screen, | |
92 | ;; the higher the value.) | |
94dbe99c TTN |
93 | ;; |
94 | ;; This and all other options can be set with the customization interface. | |
95 | ;; Simply do | |
96 | ;; | |
97 | ;; M-x customize-group RET doc-view RET | |
98 | ;; | |
99 | ;; and modify them to your needs. | |
100 | ||
101 | ;;; Code: | |
102 | ||
103 | (require 'dired) | |
414dd971 | 104 | (require 'image-mode) |
94dbe99c TTN |
105 | |
106 | ;;;; Customization Options | |
107 | ||
108 | (defgroup doc-view nil | |
109 | "In-buffer viewer for PDF, PostScript and DVI files." | |
110 | :link '(function-link doc-view) | |
111 | :version "22.2" | |
112 | :group 'applications | |
113 | :group 'multimedia | |
114 | :prefix "doc-view-") | |
115 | ||
c9a9a5e3 | 116 | (defcustom doc-view-ghostscript-program (executable-find "gs") |
94dbe99c | 117 | "Program to convert PS and PDF files to PNG." |
c9a9a5e3 | 118 | :type 'file |
94dbe99c TTN |
119 | :group 'doc-view) |
120 | ||
121 | (defcustom doc-view-ghostscript-options | |
6a658a30 RS |
122 | '("-dSAFER" ;; Avoid security problems when rendering files from untrusted |
123 | ;; sources. | |
124 | "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4" | |
125 | "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET" "-r100") | |
4b378e75 | 126 | "A list of options to give to ghostscript." |
c9a9a5e3 | 127 | :type '(repeat string) |
94dbe99c TTN |
128 | :group 'doc-view) |
129 | ||
c9a9a5e3 | 130 | (defcustom doc-view-dvipdfm-program (executable-find "dvipdfm") |
94dbe99c TTN |
131 | "Program to convert DVI files to PDF. |
132 | ||
133 | DVI file will be converted to PDF before the resulting PDF is | |
134 | converted to PNG." | |
c9a9a5e3 | 135 | :type 'file |
94dbe99c TTN |
136 | :group 'doc-view) |
137 | ||
c9a9a5e3 | 138 | (defcustom doc-view-ps2pdf-program (executable-find "ps2pdf") |
94dbe99c TTN |
139 | "Program to convert PS files to PDF. |
140 | ||
141 | PS files will be converted to PDF before searching is possible." | |
c9a9a5e3 | 142 | :type 'file |
94dbe99c TTN |
143 | :group 'doc-view) |
144 | ||
c9a9a5e3 | 145 | (defcustom doc-view-pdftotext-program (executable-find "pdftotext") |
94dbe99c TTN |
146 | "Program to convert PDF files to plain text. |
147 | ||
148 | Needed for searching." | |
c9a9a5e3 | 149 | :type 'file |
94dbe99c TTN |
150 | :group 'doc-view) |
151 | ||
152 | (defcustom doc-view-cache-directory (concat temporary-file-directory | |
1ca678aa | 153 | "doc-view") |
94dbe99c | 154 | "The base directory, where the PNG images will be saved." |
c9a9a5e3 | 155 | :type 'directory |
94dbe99c TTN |
156 | :group 'doc-view) |
157 | ||
158 | (defcustom doc-view-conversion-buffer "*doc-view conversion output*" | |
159 | "The buffer where messages from the converter programs go to." | |
c9a9a5e3 | 160 | :type 'string |
94dbe99c TTN |
161 | :group 'doc-view) |
162 | ||
163 | (defcustom doc-view-conversion-refresh-interval 3 | |
164 | "Every how much seconds the DocView buffer gets refreshed while conversion. | |
165 | After such an refresh newly converted pages will be available for | |
166 | viewing. If set to nil there won't be any refreshes and the | |
167 | pages won't be displayed before conversion of the whole document | |
168 | has finished." | |
c9a9a5e3 | 169 | :type 'integer |
94dbe99c TTN |
170 | :group 'doc-view) |
171 | ||
172 | ;;;; Internal Variables | |
173 | ||
174 | (defvar doc-view-current-files nil | |
175 | "Only used internally.") | |
176 | ||
177 | (defvar doc-view-current-page nil | |
178 | "Only used internally.") | |
179 | ||
180 | (defvar doc-view-current-doc nil | |
181 | "Only used internally.") | |
182 | ||
183 | (defvar doc-view-current-converter-process nil | |
184 | "Only used internally.") | |
185 | ||
186 | (defvar doc-view-current-timer nil | |
187 | "Only used internally.") | |
188 | ||
189 | (defvar doc-view-current-slice nil | |
190 | "Only used internally.") | |
191 | ||
192 | (defvar doc-view-current-cache-dir nil | |
193 | "Only used internally.") | |
194 | ||
195 | (defvar doc-view-current-search-matches nil | |
196 | "Only used internally.") | |
197 | ||
198 | (defvar doc-view-current-image nil | |
199 | "Only used internally.") | |
200 | ||
201 | (defvar doc-view-current-info nil | |
202 | "Only used internally.") | |
203 | ||
937cb3fb | 204 | (defvar doc-view-previous-major-mode nil |
640602f7 RS |
205 | "Only used internally.") |
206 | ||
207 | ;;;; DocView Keymaps | |
94dbe99c TTN |
208 | |
209 | (defvar doc-view-mode-map | |
210 | (let ((map (make-sparse-keymap))) | |
937cb3fb | 211 | (suppress-keymap map) |
94dbe99c TTN |
212 | ;; Navigation in the document |
213 | (define-key map (kbd "n") 'doc-view-next-page) | |
214 | (define-key map (kbd "p") 'doc-view-previous-page) | |
eb8d0216 SM |
215 | (define-key map (kbd "<next>") 'forward-page) |
216 | (define-key map (kbd "<prior>") 'backward-page) | |
217 | (define-key map [remap forward-page] 'doc-view-next-page) | |
218 | (define-key map [remap backward-page] 'doc-view-previous-page) | |
94dbe99c TTN |
219 | (define-key map (kbd "SPC") 'doc-view-scroll-up-or-next-page) |
220 | (define-key map (kbd "DEL") 'doc-view-scroll-down-or-previous-page) | |
221 | (define-key map (kbd "M-<") 'doc-view-first-page) | |
222 | (define-key map (kbd "M->") 'doc-view-last-page) | |
223 | (define-key map (kbd "g") 'doc-view-goto-page) | |
224 | ;; Killing/burying the buffer (and the process) | |
225 | (define-key map (kbd "q") 'bury-buffer) | |
226 | (define-key map (kbd "k") 'doc-view-kill-proc-and-buffer) | |
937cb3fb | 227 | (define-key map (kbd "K") 'doc-view-kill-proc) |
94dbe99c TTN |
228 | ;; Slicing the image |
229 | (define-key map (kbd "s s") 'doc-view-set-slice) | |
230 | (define-key map (kbd "s m") 'doc-view-set-slice-using-mouse) | |
231 | (define-key map (kbd "s r") 'doc-view-reset-slice) | |
232 | ;; Searching | |
233 | (define-key map (kbd "C-s") 'doc-view-search) | |
234 | (define-key map (kbd "<find>") 'doc-view-search) | |
235 | (define-key map (kbd "C-S-n") 'doc-view-search-next-match) | |
236 | (define-key map (kbd "C-S-p") 'doc-view-search-previous-match) | |
237 | ;; Scrolling | |
eb8d0216 SM |
238 | (define-key map [remap forward-char] 'image-forward-hscroll) |
239 | (define-key map [remap backward-char] 'image-backward-hscroll) | |
240 | (define-key map [remap next-line] 'image-next-line) | |
241 | (define-key map [remap previous-line] 'image-previous-line) | |
94dbe99c TTN |
242 | ;; Show the tooltip |
243 | (define-key map (kbd "C-t") 'doc-view-show-tooltip) | |
640602f7 RS |
244 | ;; Toggle between text and image display or editing |
245 | (define-key map (kbd "C-c C-c") 'doc-view-toggle-display) | |
640602f7 RS |
246 | ;; Reconvert the current document |
247 | (define-key map (kbd "g") 'doc-view-reconvert-doc) | |
94dbe99c | 248 | map) |
640602f7 RS |
249 | "Keymap used by `doc-view-mode' when displaying a doc as a set of images.") |
250 | ||
937cb3fb | 251 | (defvar doc-view-minor-mode-map |
640602f7 RS |
252 | (let ((map (make-sparse-keymap))) |
253 | ;; Toggle between text and image display or editing | |
254 | (define-key map (kbd "C-c C-c") 'doc-view-toggle-display) | |
640602f7 | 255 | map) |
937cb3fb | 256 | "Keymap used by `doc-minor-view-mode'.") |
94dbe99c TTN |
257 | |
258 | ;;;; Navigation Commands | |
259 | ||
260 | (defun doc-view-goto-page (page) | |
261 | "View the page given by PAGE." | |
262 | (interactive "nPage: ") | |
263 | (let ((len (length doc-view-current-files))) | |
264 | (if (< page 1) | |
1ca678aa | 265 | (setq page 1) |
94dbe99c | 266 | (when (> page len) |
1ca678aa | 267 | (setq page len))) |
94dbe99c | 268 | (setq doc-view-current-page page |
1ca678aa MC |
269 | doc-view-current-info |
270 | (concat | |
271 | (propertize | |
272 | (format "Page %d of %d." | |
273 | doc-view-current-page | |
274 | len) 'face 'bold) | |
275 | ;; Tell user if converting isn't finished yet | |
276 | (if doc-view-current-converter-process | |
277 | " (still converting...)\n" | |
278 | "\n") | |
279 | ;; Display context infos if this page matches the last search | |
280 | (when (and doc-view-current-search-matches | |
281 | (assq doc-view-current-page | |
282 | doc-view-current-search-matches)) | |
283 | (concat (propertize "Search matches:\n" 'face 'bold) | |
284 | (let ((contexts "")) | |
285 | (dolist (m (cdr (assq doc-view-current-page | |
286 | doc-view-current-search-matches))) | |
287 | (setq contexts (concat contexts " - \"" m "\"\n"))) | |
288 | contexts))))) | |
94dbe99c | 289 | ;; Update the buffer |
640602f7 RS |
290 | (let ((inhibit-read-only t)) |
291 | (erase-buffer) | |
292 | (let ((beg (point))) | |
293 | (doc-view-insert-image (nth (1- page) doc-view-current-files) | |
294 | :pointer 'arrow) | |
295 | (put-text-property beg (point) 'help-echo doc-view-current-info)) | |
296 | (insert "\n" doc-view-current-info) | |
297 | (goto-char (point-min)) | |
298 | (forward-char)) | |
299 | (set-buffer-modified-p nil))) | |
94dbe99c TTN |
300 | |
301 | (defun doc-view-next-page (&optional arg) | |
302 | "Browse ARG pages forward." | |
303 | (interactive "p") | |
304 | (doc-view-goto-page (+ doc-view-current-page (or arg 1)))) | |
305 | ||
306 | (defun doc-view-previous-page (&optional arg) | |
307 | "Browse ARG pages backward." | |
308 | (interactive "p") | |
309 | (doc-view-goto-page (- doc-view-current-page (or arg 1)))) | |
310 | ||
311 | (defun doc-view-first-page () | |
312 | "View the first page." | |
313 | (interactive) | |
314 | (doc-view-goto-page 1)) | |
315 | ||
316 | (defun doc-view-last-page () | |
317 | "View the last page." | |
318 | (interactive) | |
319 | (doc-view-goto-page (length doc-view-current-files))) | |
320 | ||
321 | (defun doc-view-scroll-up-or-next-page () | |
322 | "Scroll page up if possible, else goto next page." | |
323 | (interactive) | |
324 | (condition-case nil | |
325 | (scroll-up) | |
326 | (error (doc-view-next-page)))) | |
327 | ||
328 | (defun doc-view-scroll-down-or-previous-page () | |
329 | "Scroll page down if possible, else goto previous page." | |
330 | (interactive) | |
331 | (condition-case nil | |
332 | (scroll-down) | |
333 | (error (doc-view-previous-page) | |
1ca678aa | 334 | (goto-char (point-max))))) |
94dbe99c | 335 | |
937cb3fb GM |
336 | ;;;; Utility Functions |
337 | ||
640602f7 RS |
338 | (defun doc-view-kill-proc () |
339 | "Kill the current converter process." | |
340 | (interactive) | |
341 | (when doc-view-current-converter-process | |
937cb3fb GM |
342 | (kill-process doc-view-current-converter-process) |
343 | (setq doc-view-current-converter-process nil)) | |
640602f7 RS |
344 | (when doc-view-current-timer |
345 | (cancel-timer doc-view-current-timer) | |
346 | (setq doc-view-current-timer nil)) | |
347 | (setq mode-line-process nil)) | |
348 | ||
94dbe99c TTN |
349 | (defun doc-view-kill-proc-and-buffer () |
350 | "Kill the current converter process and buffer." | |
351 | (interactive) | |
640602f7 | 352 | (doc-view-kill-proc) |
94dbe99c | 353 | (when (eq major-mode 'doc-view-mode) |
94dbe99c TTN |
354 | (kill-buffer (current-buffer)))) |
355 | ||
937cb3fb GM |
356 | (defun doc-view-current-cache-dir () |
357 | "Return the directory where the png files of the current doc should be saved. | |
358 | It's a subdirectory of `doc-view-cache-directory'." | |
359 | (if doc-view-current-cache-dir | |
360 | doc-view-current-cache-dir | |
361 | (setq doc-view-current-cache-dir | |
362 | (file-name-as-directory | |
363 | (concat (file-name-as-directory doc-view-cache-directory) | |
364 | (let ((doc doc-view-current-doc)) | |
365 | (concat (file-name-nondirectory doc) | |
366 | "-" | |
367 | (with-temp-buffer | |
368 | (insert-file-contents-literally doc) | |
369 | (md5 (current-buffer)))))))))) | |
370 | ||
371 | (defun doc-view-remove-if (predicate list) | |
372 | "Return LIST with all items removed that satisfy PREDICATE." | |
373 | (let (new-list) | |
374 | (dolist (item list (nreverse new-list)) | |
375 | (when (not (funcall predicate item)) | |
376 | (setq new-list (cons item new-list)))))) | |
377 | ||
94dbe99c TTN |
378 | ;;;; Conversion Functions |
379 | ||
640602f7 RS |
380 | (defun doc-view-reconvert-doc (&rest args) |
381 | "Reconvert the current document. | |
382 | Should be invoked when the cached images aren't up-to-date." | |
383 | (interactive) | |
384 | (let ((inhibit-read-only t) | |
385 | (doc doc-view-current-doc)) | |
386 | (doc-view-kill-proc) | |
387 | ;; Clear the old cached files | |
388 | (when (file-exists-p (doc-view-current-cache-dir)) | |
389 | (dired-delete-file (doc-view-current-cache-dir) 'always)) | |
937cb3fb | 390 | (doc-view-mode))) |
94dbe99c TTN |
391 | |
392 | (defun doc-view-dvi->pdf-sentinel (proc event) | |
e48a5bf9 | 393 | "If DVI->PDF conversion was successful, convert the PDF to PNG now." |
94dbe99c TTN |
394 | (if (not (string-match "finished" event)) |
395 | (message "DocView: dvi->pdf process changed status to %s." event) | |
396 | (set-buffer (process-get proc 'buffer)) | |
640602f7 RS |
397 | (setq doc-view-current-converter-process nil |
398 | mode-line-process nil) | |
94dbe99c TTN |
399 | ;; Now go on converting this PDF to a set of PNG files. |
400 | (let* ((pdf (process-get proc 'pdf-file)) | |
640602f7 | 401 | (png (concat (doc-view-current-cache-dir) |
1ca678aa | 402 | "page-%d.png"))) |
94dbe99c TTN |
403 | (doc-view-pdf/ps->png pdf png)))) |
404 | ||
405 | (defun doc-view-dvi->pdf (dvi pdf) | |
406 | "Convert DVI to PDF asynchrounously." | |
94dbe99c | 407 | (setq doc-view-current-converter-process |
640602f7 | 408 | (start-process "dvi->pdf" doc-view-conversion-buffer |
1ca678aa | 409 | doc-view-dvipdfm-program |
640602f7 RS |
410 | "-o" pdf dvi) |
411 | mode-line-process (list (format ":%s" doc-view-current-converter-process))) | |
94dbe99c | 412 | (set-process-sentinel doc-view-current-converter-process |
1ca678aa | 413 | 'doc-view-dvi->pdf-sentinel) |
94dbe99c TTN |
414 | (process-put doc-view-current-converter-process 'buffer (current-buffer)) |
415 | (process-put doc-view-current-converter-process 'pdf-file pdf)) | |
416 | ||
417 | (defun doc-view-pdf/ps->png-sentinel (proc event) | |
418 | "If PDF/PS->PNG conversion was successful, update the display." | |
419 | (if (not (string-match "finished" event)) | |
420 | (message "DocView: converter process changed status to %s." event) | |
421 | (set-buffer (process-get proc 'buffer)) | |
640602f7 RS |
422 | (setq doc-view-current-converter-process nil |
423 | mode-line-process nil) | |
94dbe99c TTN |
424 | (when doc-view-current-timer |
425 | (cancel-timer doc-view-current-timer) | |
426 | (setq doc-view-current-timer nil)) | |
94dbe99c TTN |
427 | ;; Yippie, finished. Update the display! |
428 | (doc-view-display doc-view-current-doc))) | |
429 | ||
430 | (defun doc-view-pdf/ps->png (pdf-ps png) | |
431 | "Convert PDF-PS to PNG asynchrounously." | |
94dbe99c | 432 | (setq doc-view-current-converter-process |
1ca678aa | 433 | (apply 'start-process |
640602f7 | 434 | (append (list "pdf/ps->png" doc-view-conversion-buffer |
1ca678aa MC |
435 | doc-view-ghostscript-program) |
436 | doc-view-ghostscript-options | |
437 | (list (concat "-sOutputFile=" png)) | |
640602f7 RS |
438 | (list pdf-ps))) |
439 | mode-line-process (list (format ":%s" doc-view-current-converter-process))) | |
94dbe99c | 440 | (process-put doc-view-current-converter-process |
1ca678aa | 441 | 'buffer (current-buffer)) |
94dbe99c | 442 | (set-process-sentinel doc-view-current-converter-process |
1ca678aa | 443 | 'doc-view-pdf/ps->png-sentinel) |
94dbe99c TTN |
444 | (when doc-view-conversion-refresh-interval |
445 | (setq doc-view-current-timer | |
1ca678aa | 446 | (run-at-time "1 secs" doc-view-conversion-refresh-interval |
937cb3fb | 447 | 'doc-view-display |
1ca678aa | 448 | doc-view-current-doc)))) |
94dbe99c TTN |
449 | |
450 | (defun doc-view-pdf->txt-sentinel (proc event) | |
451 | (if (not (string-match "finished" event)) | |
452 | (message "DocView: converter process changed status to %s." event) | |
453 | (let ((current-buffer (current-buffer)) | |
1ca678aa | 454 | (proc-buffer (process-get proc 'buffer))) |
94dbe99c | 455 | (set-buffer proc-buffer) |
640602f7 RS |
456 | (setq doc-view-current-converter-process nil |
457 | mode-line-process nil) | |
94dbe99c TTN |
458 | ;; If the user looks at the DocView buffer where the conversion was |
459 | ;; performed, search anew. This time it will be queried for a regexp. | |
460 | (when (eq current-buffer proc-buffer) | |
1ca678aa | 461 | (doc-view-search))))) |
94dbe99c TTN |
462 | |
463 | (defun doc-view-pdf->txt (pdf txt) | |
464 | "Convert PDF to TXT asynchrounously." | |
94dbe99c | 465 | (setq doc-view-current-converter-process |
640602f7 | 466 | (start-process "pdf->txt" doc-view-conversion-buffer |
1ca678aa | 467 | doc-view-pdftotext-program "-raw" |
640602f7 RS |
468 | pdf txt) |
469 | mode-line-process (list (format ":%s" doc-view-current-converter-process))) | |
94dbe99c | 470 | (set-process-sentinel doc-view-current-converter-process |
1ca678aa | 471 | 'doc-view-pdf->txt-sentinel) |
94dbe99c TTN |
472 | (process-put doc-view-current-converter-process 'buffer (current-buffer))) |
473 | ||
474 | (defun doc-view-ps->pdf-sentinel (proc event) | |
475 | (if (not (string-match "finished" event)) | |
476 | (message "DocView: converter process changed status to %s." event) | |
477 | (set-buffer (process-get proc 'buffer)) | |
640602f7 RS |
478 | (setq doc-view-current-converter-process nil |
479 | mode-line-process nil) | |
94dbe99c TTN |
480 | ;; Now we can transform to plain text. |
481 | (doc-view-pdf->txt (process-get proc 'pdf-file) | |
640602f7 | 482 | (concat (doc-view-current-cache-dir) |
1ca678aa | 483 | "doc.txt")))) |
94dbe99c TTN |
484 | |
485 | (defun doc-view-ps->pdf (ps pdf) | |
486 | "Convert PS to PDF asynchronously." | |
94dbe99c | 487 | (setq doc-view-current-converter-process |
640602f7 | 488 | (start-process "ps->pdf" doc-view-conversion-buffer |
1ca678aa | 489 | doc-view-ps2pdf-program |
6a658a30 RS |
490 | ;; Avoid security problems when rendering files from |
491 | ;; untrusted sources. | |
937cb3fb GM |
492 | "-dSAFER" |
493 | ;; in-file and out-file | |
494 | ps pdf) | |
640602f7 | 495 | mode-line-process (list (format ":%s" doc-view-current-converter-process))) |
94dbe99c | 496 | (set-process-sentinel doc-view-current-converter-process |
1ca678aa | 497 | 'doc-view-ps->pdf-sentinel) |
94dbe99c TTN |
498 | (process-put doc-view-current-converter-process 'buffer (current-buffer)) |
499 | (process-put doc-view-current-converter-process 'pdf-file pdf)) | |
500 | ||
640602f7 RS |
501 | (defun doc-view-convert-current-doc () |
502 | "Convert `doc-view-current-doc' to a set of png files, one file per page. | |
503 | Those files are saved in the directory given by the function | |
504 | `doc-view-current-cache-dir'." | |
94dbe99c | 505 | (clear-image-cache) |
640602f7 RS |
506 | (let ((png-file (concat (doc-view-current-cache-dir) |
507 | "page-%d.png"))) | |
937cb3fb | 508 | (make-directory (doc-view-current-cache-dir) t) |
640602f7 | 509 | (if (not (string= (file-name-extension doc-view-current-doc) "dvi")) |
1ca678aa | 510 | ;; Convert to PNG images. |
640602f7 | 511 | (doc-view-pdf/ps->png doc-view-current-doc png-file) |
4b378e75 | 512 | ;; DVI files have to be converted to PDF before Ghostscript can process |
94dbe99c | 513 | ;; it. |
640602f7 RS |
514 | (doc-view-dvi->pdf doc-view-current-doc |
515 | (concat (file-name-as-directory doc-view-current-cache-dir) | |
1ca678aa | 516 | "doc.pdf"))))) |
94dbe99c | 517 | |
94dbe99c TTN |
518 | ;;;; Slicing |
519 | ||
520 | (defun doc-view-set-slice (x y width height) | |
521 | "Set the slice of the images that should be displayed. | |
522 | You can use this function to tell doc-view not to display the | |
523 | margins of the document. It prompts for the top-left corner (X | |
524 | and Y) of the slice to display and its WIDTH and HEIGHT. | |
525 | ||
526 | See `doc-view-set-slice-using-mouse' for a more convenient way to | |
527 | do that. To reset the slice use `doc-view-reset-slice'." | |
528 | (interactive | |
529 | (let* ((size (image-size doc-view-current-image t)) | |
1ca678aa MC |
530 | (a (read-number (format "Top-left X (0..%d): " (car size)))) |
531 | (b (read-number (format "Top-left Y (0..%d): " (cdr size)))) | |
532 | (c (read-number (format "Width (0..%d): " (- (car size) a)))) | |
533 | (d (read-number (format "Height (0..%d): " (- (cdr size) b))))) | |
94dbe99c TTN |
534 | (list a b c d))) |
535 | (setq doc-view-current-slice (list x y width height)) | |
536 | ;; Redisplay | |
537 | (doc-view-goto-page doc-view-current-page)) | |
538 | ||
539 | (defun doc-view-set-slice-using-mouse () | |
540 | "Set the slice of the images that should be displayed. | |
541 | You set the slice by pressing mouse-1 at its top-left corner and | |
542 | dragging it to its bottom-right corner. See also | |
543 | `doc-view-set-slice' and `doc-view-reset-slice'." | |
544 | (interactive) | |
545 | (let (x y w h done) | |
546 | (while (not done) | |
547 | (let ((e (read-event | |
1ca678aa MC |
548 | (concat "Press mouse-1 at the top-left corner and " |
549 | "drag it to the bottom-right corner!")))) | |
550 | (when (eq (car e) 'drag-mouse-1) | |
551 | (setq x (car (posn-object-x-y (event-start e)))) | |
552 | (setq y (cdr (posn-object-x-y (event-start e)))) | |
553 | (setq w (- (car (posn-object-x-y (event-end e))) x)) | |
554 | (setq h (- (cdr (posn-object-x-y (event-end e))) y)) | |
555 | (setq done t)))) | |
94dbe99c TTN |
556 | (doc-view-set-slice x y w h))) |
557 | ||
558 | (defun doc-view-reset-slice () | |
e48a5bf9 | 559 | "Reset the current slice. |
94dbe99c TTN |
560 | After calling this function the whole pages will be visible |
561 | again." | |
562 | (interactive) | |
563 | (setq doc-view-current-slice nil) | |
564 | ;; Redisplay | |
565 | (doc-view-goto-page doc-view-current-page)) | |
566 | ||
567 | ;;;; Display | |
568 | ||
569 | (defun doc-view-insert-image (file &rest args) | |
570 | "Insert the given png FILE. | |
e48a5bf9 | 571 | ARGS is a list of image descriptors." |
94dbe99c TTN |
572 | (let ((image (apply 'create-image file 'png nil args))) |
573 | (setq doc-view-current-image image) | |
574 | (insert-image image (concat "[" file "]") nil doc-view-current-slice))) | |
575 | ||
576 | (defun doc-view-sort (a b) | |
577 | "Return non-nil if A should be sorted before B. | |
578 | Predicate for sorting `doc-view-current-files'." | |
579 | (if (< (length a) (length b)) | |
580 | t | |
581 | (if (> (length a) (length b)) | |
1ca678aa | 582 | nil |
94dbe99c TTN |
583 | (string< a b)))) |
584 | ||
585 | (defun doc-view-display (doc) | |
586 | "Start viewing the document DOC." | |
640602f7 RS |
587 | (set-buffer (get-file-buffer doc)) |
588 | (setq doc-view-current-files | |
589 | (sort (directory-files (doc-view-current-cache-dir) t | |
590 | "page-[0-9]+\\.png" t) | |
591 | 'doc-view-sort)) | |
592 | (when (> (length doc-view-current-files) 0) | |
593 | (doc-view-goto-page doc-view-current-page))) | |
94dbe99c TTN |
594 | |
595 | (defun doc-view-buffer-message () | |
94dbe99c | 596 | (insert (propertize "Welcome to DocView!" 'face 'bold) |
1ca678aa MC |
597 | "\n" |
598 | " | |
94dbe99c TTN |
599 | If you see this buffer it means that the document you want to |
600 | view gets converted to PNG now and the conversion of the first | |
601 | page hasn't finished yet or | |
602 | `doc-view-conversion-refresh-interval' is set to nil. | |
603 | ||
604 | For now these keys are useful: | |
605 | ||
1ca678aa | 606 | `q' : Bury this buffer. Conversion will go on in background. |
937cb3fb GM |
607 | `k' : Kill the conversion process and this buffer. |
608 | `K' : Kill the conversion process.\n") | |
640602f7 | 609 | (set-buffer-modified-p nil)) |
94dbe99c TTN |
610 | |
611 | (defun doc-view-show-tooltip () | |
612 | (interactive) | |
613 | (tooltip-show doc-view-current-info)) | |
614 | ||
937cb3fb | 615 | ;;;;; Toggle between editing and viewing |
640602f7 RS |
616 | |
617 | (defun doc-view-toggle-display () | |
937cb3fb | 618 | "Toggle between editing a document as text or viewing it." |
640602f7 | 619 | (interactive) |
937cb3fb GM |
620 | (if (eq major-mode 'doc-view-mode) |
621 | ;; Switch to editing mode | |
622 | (progn | |
623 | (doc-view-kill-proc) | |
624 | (setq buffer-read-only nil) | |
640602f7 | 625 | (erase-buffer) |
937cb3fb GM |
626 | (insert-file-contents buffer-file-name) |
627 | ;; Switch to the previously used major mode or fall back to fundamental | |
628 | ;; mode. | |
629 | (if doc-view-previous-major-mode | |
630 | (funcall doc-view-previous-major-mode) | |
631 | (fundamental-mode)) | |
632 | (doc-view-minor-mode 1) | |
633 | (set-buffer-modified-p nil)) | |
634 | ;; Switch to doc-view-mode | |
635 | (when (and (buffer-modified-p) | |
636 | (y-or-n-p "The buffer has been modified. Save the changes? ")) | |
637 | (save-buffer)) | |
638 | (erase-buffer) | |
639 | (doc-view-mode))) | |
640602f7 | 640 | |
94dbe99c TTN |
641 | ;;;; Searching |
642 | ||
643 | (defun doc-view-search-internal (regexp file) | |
644 | "Return a list of FILE's pages that contain text matching REGEXP. | |
1ca678aa MC |
645 | The value is an alist of the form (PAGE CONTEXTS) where PAGE is |
646 | the pagenumber and CONTEXTS are all lines of text containing a match." | |
94dbe99c TTN |
647 | (with-temp-buffer |
648 | (insert-file-contents file) | |
649 | (let ((page 1) | |
1ca678aa MC |
650 | (lastpage 1) |
651 | matches) | |
94dbe99c | 652 | (while (re-search-forward (concat "\\(?:\\([\f]\\)\\|\\(" |
1ca678aa MC |
653 | regexp "\\)\\)") nil t) |
654 | (when (match-string 1) (incf page)) | |
655 | (when (match-string 2) | |
656 | (if (/= page lastpage) | |
657 | (setq matches (push (cons page | |
658 | (list (buffer-substring | |
659 | (line-beginning-position) | |
660 | (line-end-position)))) | |
661 | matches)) | |
662 | (setq matches (cons | |
663 | (append | |
664 | (or | |
665 | ;; This page already is a match. | |
666 | (car matches) | |
667 | ;; This is the first match on page. | |
668 | (list page)) | |
669 | (list (buffer-substring | |
670 | (line-beginning-position) | |
671 | (line-end-position)))) | |
672 | (cdr matches)))) | |
673 | (setq lastpage page))) | |
94dbe99c TTN |
674 | (nreverse matches)))) |
675 | ||
676 | (defun doc-view-search-no-of-matches (list) | |
677 | "Extract the number of matches from the search result LIST." | |
678 | (let ((no 0)) | |
679 | (dolist (p list) | |
680 | (setq no (+ no (1- (length p))))) | |
681 | no)) | |
682 | ||
683 | (defun doc-view-search () | |
684 | "Query for a regexp and search the current document. | |
685 | If the current document hasn't been transformed to plain text | |
686 | till now do that first. You should try searching anew when the | |
687 | conversion finished." | |
688 | (interactive) | |
689 | ;; New search, so forget the old results. | |
690 | (setq doc-view-current-search-matches nil) | |
640602f7 | 691 | (let ((txt (concat (doc-view-current-cache-dir) |
1ca678aa | 692 | "doc.txt"))) |
94dbe99c | 693 | (if (file-readable-p txt) |
1ca678aa MC |
694 | (progn |
695 | (setq doc-view-current-search-matches | |
696 | (doc-view-search-internal | |
697 | (read-from-minibuffer "Regexp: ") | |
698 | txt)) | |
699 | (message "DocView: search yielded %d matches." | |
700 | (doc-view-search-no-of-matches | |
701 | doc-view-current-search-matches))) | |
94dbe99c TTN |
702 | ;; We must convert to TXT first! |
703 | (if doc-view-current-converter-process | |
1ca678aa MC |
704 | (message "DocView: please wait till conversion finished.") |
705 | (let ((ext (file-name-extension doc-view-current-doc))) | |
706 | (cond | |
707 | ((string= ext "pdf") | |
708 | ;; Doc is a PDF, so convert it to TXT | |
709 | (doc-view-pdf->txt doc-view-current-doc txt)) | |
710 | ((string= ext "ps") | |
711 | ;; Doc is a PS, so convert it to PDF (which will be converted to | |
712 | ;; TXT thereafter). | |
713 | (doc-view-ps->pdf doc-view-current-doc | |
640602f7 | 714 | (concat (doc-view-current-cache-dir) |
1ca678aa MC |
715 | "doc.pdf"))) |
716 | ((string= ext "dvi") | |
717 | ;; Doc is a DVI. This means that a doc.pdf already exists in its | |
718 | ;; cache subdirectory. | |
640602f7 | 719 | (doc-view-pdf->txt (concat (doc-view-current-cache-dir) |
1ca678aa MC |
720 | "doc.pdf") |
721 | txt)) | |
722 | (t (error "DocView doesn't know what to do")))))))) | |
94dbe99c TTN |
723 | |
724 | (defun doc-view-search-next-match (arg) | |
725 | "Go to the ARGth next matching page." | |
726 | (interactive "p") | |
937cb3fb GM |
727 | (let* ((next-pages (doc-view-remove-if |
728 | (lambda (i) (<= (car i) doc-view-current-page)) | |
729 | doc-view-current-search-matches)) | |
1ca678aa | 730 | (page (car (nth (1- arg) next-pages)))) |
94dbe99c | 731 | (if page |
1ca678aa | 732 | (doc-view-goto-page page) |
94dbe99c | 733 | (when (and |
1ca678aa MC |
734 | doc-view-current-search-matches |
735 | (y-or-n-p "No more matches after current page. Wrap to first match? ")) | |
736 | (doc-view-goto-page (caar doc-view-current-search-matches)))))) | |
94dbe99c TTN |
737 | |
738 | (defun doc-view-search-previous-match (arg) | |
739 | "Go to the ARGth previous matching page." | |
740 | (interactive "p") | |
937cb3fb GM |
741 | (let* ((prev-pages (doc-view-remove-if |
742 | (lambda (i) (>= (car i) doc-view-current-page)) | |
743 | doc-view-current-search-matches)) | |
1ca678aa | 744 | (page (car (nth (1- arg) (nreverse prev-pages))))) |
94dbe99c | 745 | (if page |
1ca678aa | 746 | (doc-view-goto-page page) |
94dbe99c | 747 | (when (and |
1ca678aa MC |
748 | doc-view-current-search-matches |
749 | (y-or-n-p "No more matches before current page. Wrap to last match? ")) | |
750 | (doc-view-goto-page (caar (last doc-view-current-search-matches))))))) | |
94dbe99c | 751 | |
640602f7 | 752 | ;;;; User interface commands and the mode |
94dbe99c | 753 | |
640602f7 | 754 | (put 'doc-view-mode 'mode-class 'special) |
94dbe99c | 755 | |
640602f7 | 756 | ;;;###autoload |
937cb3fb | 757 | (defun doc-view-mode () |
640602f7 RS |
758 | "Major mode in DocView buffers. |
759 | You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to | |
937cb3fb GM |
760 | toggle between displaying the document or editing it as text." |
761 | (interactive) | |
762 | (let* ((prev-major-mode (if (eq major-mode 'doc-view-mode) | |
763 | doc-view-previous-major-mode | |
764 | major-mode))) | |
765 | (kill-all-local-variables) | |
766 | (make-local-variable 'doc-view-current-files) | |
767 | (make-local-variable 'doc-view-current-image) | |
768 | (make-local-variable 'doc-view-current-page) | |
769 | (make-local-variable 'doc-view-current-converter-process) | |
770 | (make-local-variable 'doc-view-current-timer) | |
771 | (make-local-variable 'doc-view-current-slice) | |
772 | (make-local-variable 'doc-view-current-cache-dir) | |
773 | (make-local-variable 'doc-view-current-info) | |
774 | (make-local-variable 'doc-view-current-search-matches) | |
775 | (set (make-local-variable 'doc-view-current-doc) buffer-file-name) | |
776 | (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode)) | |
640602f7 | 777 | (insert-file-contents doc-view-current-doc) |
937cb3fb GM |
778 | (use-local-map doc-view-mode-map) |
779 | (setq mode-name "DocView" | |
640602f7 | 780 | buffer-read-only t |
937cb3fb GM |
781 | revert-buffer-function 'doc-view-reconvert-doc |
782 | major-mode 'doc-view-mode) | |
640602f7 RS |
783 | ;; Switch to image display if possible |
784 | (if (and (display-images-p) | |
937cb3fb GM |
785 | (image-type-available-p 'png)) |
786 | (let ((inhibit-read-only t)) | |
787 | (erase-buffer) | |
788 | (doc-view-buffer-message) | |
789 | (setq doc-view-current-page (or doc-view-current-page 1)) | |
790 | (if (file-exists-p (doc-view-current-cache-dir)) | |
791 | (progn | |
792 | (message "DocView: using cached files!") | |
793 | (doc-view-display doc-view-current-doc)) | |
794 | (doc-view-convert-current-doc)) | |
795 | (use-local-map doc-view-mode-map) | |
796 | (message | |
797 | "%s" | |
798 | (substitute-command-keys | |
799 | (concat "Type \\[doc-view-toggle-display] to toggle between " | |
800 | "editing or viewing the document.")))) | |
801 | (message | |
802 | "%s" | |
803 | (substitute-command-keys | |
804 | (concat "No image (png) support available. Type \\[doc-view-toggle-display] " | |
805 | "to switch to an editing mode."))))) | |
806 | ||
807 | ;;;###autoload | |
808 | (define-minor-mode doc-view-minor-mode | |
809 | "Toggle Doc view minor mode. | |
810 | With arg, turn Doc view minor mode on if arg is positive, off otherwise. | |
811 | See the command `doc-view-mode' for more information on this mode." | |
812 | nil " DocView" doc-view-minor-mode-map | |
813 | :group 'doc-view | |
814 | (when doc-view-minor-mode | |
815 | (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t) | |
816 | (message | |
817 | "%s" | |
818 | (substitute-command-keys | |
819 | "Type \\[doc-view-toggle-display] to toggle between editing or viewing the document.")))) | |
94dbe99c TTN |
820 | |
821 | (defun doc-view-clear-cache () | |
822 | "Delete the whole cache (`doc-view-cache-directory')." | |
823 | (interactive) | |
824 | (dired-delete-file doc-view-cache-directory 'always) | |
825 | (make-directory doc-view-cache-directory)) | |
826 | ||
827 | (defun doc-view-dired-cache () | |
828 | "Open `dired' in `doc-view-cache-directory'." | |
829 | (interactive) | |
830 | (dired doc-view-cache-directory)) | |
831 | ||
832 | (provide 'doc-view) | |
833 | ||
834 | ;; Local Variables: | |
835 | ;; mode: outline-minor | |
836 | ;; End: | |
837 | ||
481249ca | 838 | ;; arch-tag: 5d6e5c5e-095f-489e-b4e4-1ca90a7d79be |
94dbe99c | 839 | ;;; doc-view.el ends here |