aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTassilo Horn2008-03-12 12:05:30 +0000
committerTassilo Horn2008-03-12 12:05:30 +0000
commit515357c252ac584f16bde96c251c832fe88411ef (patch)
tree6f7c45f4f53694b9604a29001746205a9edf9db7
parent85efdcf3408bbf270642f64baa16307ef409b66e (diff)
downloademacs-515357c252ac584f16bde96c251c832fe88411ef.tar.gz
emacs-515357c252ac584f16bde96c251c832fe88411ef.zip
(doc-view-doc->txt, doc-view-open-text)
(doc-view-already-converted-p): New functions. (doc-view-clear-cache): Don't recreate doc-view-cache-directory. (doc-view-mode-map): Bind C-c C-t to doc-view-open-text. (doc-view-dvi->pdf-sentinel, doc-view-dvi->pdf) (doc-view-pdf->txt-sentinel, doc-view-pdf->txt) (doc-view-ps->pdf-sentinel, doc-view-ps->pdf) (doc-view-convert-current-doc): Don't hardwire the functions the sentinels call. Now they're provided by two args CALLBACK and CB-ARGS to the functions. (doc-view-search): Use doc-view-doc->txt. (doc-view-initiate-display): Use doc-view-already-converted-p. Mention new binding C-c C-t if doc-view-mode doesn't work.
-rw-r--r--lisp/ChangeLog16
-rw-r--r--lisp/doc-view.el163
2 files changed, 116 insertions, 63 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index cc9d3c1566f..2deb3d4c792 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,19 @@
12008-03-12 Tassilo Horn <tassilo@member.fsf.org>
2
3 * lisp/doc-view.el (doc-view-doc->txt, doc-view-open-text)
4 (doc-view-already-converted-p): New functions.
5 (doc-view-clear-cache): Don't recreate doc-view-cache-directory.
6 (doc-view-mode-map): Bind C-c C-t to doc-view-open-text.
7 (doc-view-dvi->pdf-sentinel, doc-view-dvi->pdf)
8 (doc-view-pdf->txt-sentinel, doc-view-pdf->txt)
9 (doc-view-ps->pdf-sentinel, doc-view-ps->pdf)
10 (doc-view-convert-current-doc): Don't hardwire the functions the
11 sentinels call. Now they're provided by two args CALLBACK and
12 CB-ARGS to the functions.
13 (doc-view-search): Use doc-view-doc->txt.
14 (doc-view-initiate-display): Use doc-view-already-converted-p.
15 Mention new binding C-c C-t if doc-view-mode doesn't work.
16
12008-03-12 Dan Nicolaescu <dann@ics.uci.edu> 172008-03-12 Dan Nicolaescu <dann@ics.uci.edu>
2 18
3 * diff-mode.el (diff-refine-change): Adjust colors to be more visible. 19 * diff-mode.el (diff-refine-change): Adjust colors to be more visible.
diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index bb98b4a8af1..57a07eb9a93 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -291,6 +291,8 @@ the (uncompressed, extracted) file residing in
291 (define-key map (kbd "C-t") 'doc-view-show-tooltip) 291 (define-key map (kbd "C-t") 'doc-view-show-tooltip)
292 ;; Toggle between text and image display or editing 292 ;; Toggle between text and image display or editing
293 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display) 293 (define-key map (kbd "C-c C-c") 'doc-view-toggle-display)
294 ;; Open a new buffer with doc's text contents
295 (define-key map (kbd "C-c C-t") 'doc-view-open-text)
294 ;; Reconvert the current document 296 ;; Reconvert the current document
295 (define-key map (kbd "g") 'revert-buffer) 297 (define-key map (kbd "g") 'revert-buffer)
296 (define-key map (kbd "r") 'revert-buffer) 298 (define-key map (kbd "r") 'revert-buffer)
@@ -523,16 +525,14 @@ Should be invoked when the cached images aren't up-to-date."
523 (if (not (string-match "finished" event)) 525 (if (not (string-match "finished" event))
524 (message "DocView: dvi->pdf process changed status to %s." event) 526 (message "DocView: dvi->pdf process changed status to %s." event)
525 (with-current-buffer (process-get proc 'buffer) 527 (with-current-buffer (process-get proc 'buffer)
526 (setq doc-view-current-converter-process nil 528 (let ((callback (process-get proc 'callback))
527 mode-line-process nil) 529 (cb-args (process-get proc 'cb-args)))
528 ;; Now go on converting this PDF to a set of PNG files. 530 (setq doc-view-current-converter-process nil
529 (let* ((pdf (process-get proc 'pdf-file)) 531 mode-line-process nil)
530 (png (expand-file-name "page-%d.png" 532 (apply callback cb-args)))))
531 (doc-view-current-cache-dir)))) 533
532 (doc-view-pdf/ps->png pdf png))))) 534(defun doc-view-dvi->pdf (dvi pdf callback &rest cb-args)
533 535 "Convert DVI to PDF asynchronously and call CALLBACK with CB-ARGS when finished."
534(defun doc-view-dvi->pdf (dvi pdf)
535 "Convert DVI to PDF asynchronously."
536 (setq doc-view-current-converter-process 536 (setq doc-view-current-converter-process
537 (start-process "dvi->pdf" doc-view-conversion-buffer 537 (start-process "dvi->pdf" doc-view-conversion-buffer
538 doc-view-dvipdfm-program 538 doc-view-dvipdfm-program
@@ -541,7 +541,8 @@ Should be invoked when the cached images aren't up-to-date."
541 (set-process-sentinel doc-view-current-converter-process 541 (set-process-sentinel doc-view-current-converter-process
542 'doc-view-dvi->pdf-sentinel) 542 'doc-view-dvi->pdf-sentinel)
543 (process-put doc-view-current-converter-process 'buffer (current-buffer)) 543 (process-put doc-view-current-converter-process 'buffer (current-buffer))
544 (process-put doc-view-current-converter-process 'pdf-file pdf)) 544 (process-put doc-view-current-converter-process 'callback callback)
545 (process-put doc-view-current-converter-process 'cb-args cb-args))
545 546
546(defun doc-view-pdf/ps->png-sentinel (proc event) 547(defun doc-view-pdf/ps->png-sentinel (proc event)
547 "If PDF/PS->PNG conversion was successful, update the display." 548 "If PDF/PS->PNG conversion was successful, update the display."
@@ -588,15 +589,17 @@ Should be invoked when the cached images aren't up-to-date."
588 (let ((current-buffer (current-buffer)) 589 (let ((current-buffer (current-buffer))
589 (proc-buffer (process-get proc 'buffer))) 590 (proc-buffer (process-get proc 'buffer)))
590 (with-current-buffer proc-buffer 591 (with-current-buffer proc-buffer
591 (setq doc-view-current-converter-process nil 592 (let ((callback (process-get doc-view-current-converter-process 'callback))
592 mode-line-process nil) 593 (cb-args (process-get doc-view-current-converter-process 'cb-args)))
593 ;; If the user looks at the DocView buffer where the conversion was 594 (setq doc-view-current-converter-process nil
594 ;; performed, search anew. This time it will be queried for a regexp. 595 mode-line-process nil)
595 (when (eq current-buffer proc-buffer) 596 ;; If the user looks at the DocView buffer where the conversion was
596 (doc-view-search nil)))))) 597 ;; performed, call callback.
597 598 (when (eq current-buffer proc-buffer)
598(defun doc-view-pdf->txt (pdf txt) 599 (apply callback cb-args)))))))
599 "Convert PDF to TXT asynchronously." 600
601(defun doc-view-pdf->txt (pdf txt callback &rest cb-args)
602 "Convert PDF to TXT asynchronously and call CALLBACK with CB-ARGS when finished."
600 (setq doc-view-current-converter-process 603 (setq doc-view-current-converter-process
601 (start-process "pdf->txt" doc-view-conversion-buffer 604 (start-process "pdf->txt" doc-view-conversion-buffer
602 doc-view-pdftotext-program "-raw" 605 doc-view-pdftotext-program "-raw"
@@ -604,21 +607,54 @@ Should be invoked when the cached images aren't up-to-date."
604 mode-line-process (list (format ":%s" doc-view-current-converter-process))) 607 mode-line-process (list (format ":%s" doc-view-current-converter-process)))
605 (set-process-sentinel doc-view-current-converter-process 608 (set-process-sentinel doc-view-current-converter-process
606 'doc-view-pdf->txt-sentinel) 609 'doc-view-pdf->txt-sentinel)
607 (process-put doc-view-current-converter-process 'buffer (current-buffer))) 610 (process-put doc-view-current-converter-process 'buffer (current-buffer))
611 (process-put doc-view-current-converter-process 'callback callback)
612 (process-put doc-view-current-converter-process 'cb-args cb-args))
613
614(defun doc-view-doc->txt (callback &rest cb-args)
615 "Convert the current document to text and call CALLBACK with CB-ARGS when done."
616 (make-directory (doc-view-current-cache-dir))
617 (let ((ext (file-name-extension doc-view-buffer-file-name)))
618 (cond
619 ((string= ext "pdf")
620 ;; Doc is a PDF, so convert it to TXT
621 (if cb-args
622 (doc-view-pdf->txt doc-view-buffer-file-name txt callback cb-args)
623 (doc-view-pdf->txt doc-view-buffer-file-name txt callback)))
624 ((string= ext "ps")
625 ;; Doc is a PS, so convert it to PDF (which will be converted to
626 ;; TXT thereafter).
627 (let ((pdf (expand-file-name "doc.pdf"
628 (doc-view-current-cache-dir))))
629 (if cb-args
630 (doc-view-ps->pdf doc-view-buffer-file-name pdf
631 'doc-view-pdf->txt
632 pdf txt callback cb-args)
633 (doc-view-ps->pdf doc-view-buffer-file-name pdf
634 'doc-view-pdf->txt
635 pdf txt callback))))
636 ((string= ext "dvi")
637 ;; Doc is a DVI. This means that a doc.pdf already exists in its
638 ;; cache subdirectory.
639 (if cb-args
640 (doc-view-pdf->txt (expand-file-name "doc.pdf"
641 (doc-view-current-cache-dir))
642 txt callback cb-args)
643 (doc-view-pdf->txt (expand-file-name "doc.pdf"
644 (doc-view-current-cache-dir))
645 txt callback)))
646 (t (error "DocView doesn't know what to do")))))
608 647
609(defun doc-view-ps->pdf-sentinel (proc event) 648(defun doc-view-ps->pdf-sentinel (proc event)
610 (if (not (string-match "finished" event)) 649 (if (not (string-match "finished" event))
611 (message "DocView: converter process changed status to %s." event) 650 (message "DocView: converter process changed status to %s." event)
612 (with-current-buffer (process-get proc 'buffer) 651 (with-current-buffer (process-get proc 'buffer)
613 (setq doc-view-current-converter-process nil 652 (setq doc-view-current-converter-process nil
614 mode-line-process nil) 653 mode-line-process nil)
615 ;; Now we can transform to plain text. 654 (apply (process-get proc 'callback) (process-get proc 'cb-args)))))
616 (doc-view-pdf->txt (process-get proc 'pdf-file) 655
617 (expand-file-name "doc.txt" 656(defun doc-view-ps->pdf (ps pdf callback &rest cb-args)
618 (doc-view-current-cache-dir)))))) 657 "Convert PS to PDF asynchronously and call CALLBACK with CB-ARGS when finished."
619
620(defun doc-view-ps->pdf (ps pdf)
621 "Convert PS to PDF asynchronously."
622 (setq doc-view-current-converter-process 658 (setq doc-view-current-converter-process
623 (start-process "ps->pdf" doc-view-conversion-buffer 659 (start-process "ps->pdf" doc-view-conversion-buffer
624 doc-view-ps2pdf-program 660 doc-view-ps2pdf-program
@@ -631,7 +667,8 @@ Should be invoked when the cached images aren't up-to-date."
631 (set-process-sentinel doc-view-current-converter-process 667 (set-process-sentinel doc-view-current-converter-process
632 'doc-view-ps->pdf-sentinel) 668 'doc-view-ps->pdf-sentinel)
633 (process-put doc-view-current-converter-process 'buffer (current-buffer)) 669 (process-put doc-view-current-converter-process 'buffer (current-buffer))
634 (process-put doc-view-current-converter-process 'pdf-file pdf)) 670 (process-put doc-view-current-converter-process 'callback callback)
671 (process-put doc-view-current-converter-process 'cb-args cb-args))
635 672
636(defun doc-view-convert-current-doc () 673(defun doc-view-convert-current-doc ()
637 "Convert `doc-view-buffer-file-name' to a set of png files, one file per page. 674 "Convert `doc-view-buffer-file-name' to a set of png files, one file per page.
@@ -651,9 +688,9 @@ Those files are saved in the directory given by the function
651 (doc-view-pdf/ps->png doc-view-buffer-file-name png-file) 688 (doc-view-pdf/ps->png doc-view-buffer-file-name png-file)
652 ;; DVI files have to be converted to PDF before Ghostscript can process 689 ;; DVI files have to be converted to PDF before Ghostscript can process
653 ;; it. 690 ;; it.
654 (doc-view-dvi->pdf doc-view-buffer-file-name 691 (let ((pdf (expand-file-name "doc.pdf" doc-view-current-cache-dir)))
655 (expand-file-name "doc.pdf" 692 (doc-view-dvi->pdf doc-view-buffer-file-name
656 doc-view-current-cache-dir))))) 693 pdf 'doc-view-pdf/ps->png pdf png-file)))))
657 694
658;;;; Slicing 695;;;; Slicing
659 696
@@ -796,8 +833,19 @@ For now these keys are useful:
796 (interactive) 833 (interactive)
797 (tooltip-show (doc-view-current-info))) 834 (tooltip-show (doc-view-current-info)))
798 835
836(defun doc-view-open-text ()
837 "Open a buffer with the current doc's contents as text."
838 (interactive)
839 (if doc-view-current-converter-process
840 (message "DocView: please wait till conversion finished.")
841 (let ((txt (expand-file-name "doc.txt" (doc-view-current-cache-dir))))
842 (if (file-readable-p txt)
843 (find-file txt)
844 (doc-view-doc->txt 'doc-view-open-text)))))
845
799;;;;; Toggle between editing and viewing 846;;;;; Toggle between editing and viewing
800 847
848
801(defun doc-view-toggle-display () 849(defun doc-view-toggle-display ()
802 "Toggle between editing a document as text or viewing it." 850 "Toggle between editing a document as text or viewing it."
803 (interactive) 851 (interactive)
@@ -806,8 +854,8 @@ For now these keys are useful:
806 (progn 854 (progn
807 (doc-view-kill-proc) 855 (doc-view-kill-proc)
808 (setq buffer-read-only nil) 856 (setq buffer-read-only nil)
809 (remove-overlays (point-min) (point-max) 'doc-view t) 857 (remove-overlays (point-min) (point-max) 'doc-view t)
810 (set (make-local-variable 'image-mode-winprops-alist) t) 858 (set (make-local-variable 'image-mode-winprops-alist) t)
811 ;; Switch to the previously used major mode or fall back to fundamental 859 ;; Switch to the previously used major mode or fall back to fundamental
812 ;; mode. 860 ;; mode.
813 (if doc-view-previous-major-mode 861 (if doc-view-previous-major-mode
@@ -822,6 +870,7 @@ For now these keys are useful:
822 870
823;;;; Searching 871;;;; Searching
824 872
873
825(defun doc-view-search-internal (regexp file) 874(defun doc-view-search-internal (regexp file)
826 "Return a list of FILE's pages that contain text matching REGEXP. 875 "Return a list of FILE's pages that contain text matching REGEXP.
827The value is an alist of the form (PAGE CONTEXTS) where PAGE is 876The value is an alist of the form (PAGE CONTEXTS) where PAGE is
@@ -837,10 +886,10 @@ the pagenumber and CONTEXTS are all lines of text containing a match."
837 (when (match-string 2) 886 (when (match-string 2)
838 (if (/= page lastpage) 887 (if (/= page lastpage)
839 (push (cons page 888 (push (cons page
840 (list (buffer-substring 889 (list (buffer-substring
841 (line-beginning-position) 890 (line-beginning-position)
842 (line-end-position)))) 891 (line-end-position))))
843 matches) 892 matches)
844 (setq matches (cons 893 (setq matches (cons
845 (append 894 (append
846 (or 895 (or
@@ -895,24 +944,7 @@ If BACKWARD is non-nil, jump to the previous match."
895 ;; We must convert to TXT first! 944 ;; We must convert to TXT first!
896 (if doc-view-current-converter-process 945 (if doc-view-current-converter-process
897 (message "DocView: please wait till conversion finished.") 946 (message "DocView: please wait till conversion finished.")
898 (let ((ext (file-name-extension doc-view-buffer-file-name))) 947 (doc-view-doc->txt 'doc-view-search nil))))))
899 (cond
900 ((string= ext "pdf")
901 ;; Doc is a PDF, so convert it to TXT
902 (doc-view-pdf->txt doc-view-buffer-file-name txt))
903 ((string= ext "ps")
904 ;; Doc is a PS, so convert it to PDF (which will be converted to
905 ;; TXT thereafter).
906 (doc-view-ps->pdf doc-view-buffer-file-name
907 (expand-file-name "doc.pdf"
908 (doc-view-current-cache-dir))))
909 ((string= ext "dvi")
910 ;; Doc is a DVI. This means that a doc.pdf already exists in its
911 ;; cache subdirectory.
912 (doc-view-pdf->txt (expand-file-name "doc.pdf"
913 (doc-view-current-cache-dir))
914 txt))
915 (t (error "DocView doesn't know what to do")))))))))
916 948
917(defun doc-view-search-next-match (arg) 949(defun doc-view-search-next-match (arg)
918 "Go to the ARGth next matching page." 950 "Go to the ARGth next matching page."
@@ -946,13 +978,18 @@ If BACKWARD is non-nil, jump to the previous match."
946 978
947;; (put 'doc-view-mode 'mode-class 'special) 979;; (put 'doc-view-mode 'mode-class 'special)
948 980
981(defun doc-view-already-converted-p ()
982 "Return non-nil if the current doc was already converted."
983 (and (file-exists-p (doc-view-current-cache-dir))
984 (> 0 (length (directory-files (doc-view-current-cache-dir) nil "\\.png$")))))
985
949(defun doc-view-initiate-display () 986(defun doc-view-initiate-display ()
950 ;; Switch to image display if possible 987 ;; Switch to image display if possible
951 (if (doc-view-mode-p (intern (file-name-extension doc-view-buffer-file-name))) 988 (if (doc-view-mode-p (intern (file-name-extension doc-view-buffer-file-name)))
952 (progn 989 (progn
953 (doc-view-buffer-message) 990 (doc-view-buffer-message)
954 (setf (doc-view-current-page) (or (doc-view-current-page) 1)) 991 (setf (doc-view-current-page) (or (doc-view-current-page) 1))
955 (if (file-exists-p (doc-view-current-cache-dir)) 992 (if (doc-view-already-converted-p)
956 (progn 993 (progn
957 (message "DocView: using cached files!") 994 (message "DocView: using cached files!")
958 (doc-view-display (current-buffer) 'force)) 995 (doc-view-display (current-buffer) 'force))
@@ -967,7 +1004,8 @@ If BACKWARD is non-nil, jump to the previous match."
967 (substitute-command-keys 1004 (substitute-command-keys
968 (concat "No image (png) support available or some conversion utility for " 1005 (concat "No image (png) support available or some conversion utility for "
969 (file-name-extension doc-view-buffer-file-name)" files is missing. " 1006 (file-name-extension doc-view-buffer-file-name)" files is missing. "
970 "Type \\[doc-view-toggle-display] to switch to an editing mode."))))) 1007 "Type \\[doc-view-toggle-display] to switch to an editing mode or "
1008 "\\[doc-view-open-text] to open a buffer showing the doc as text.")))))
971 1009
972(defvar bookmark-make-record-function) 1010(defvar bookmark-make-record-function)
973 1011
@@ -978,7 +1016,7 @@ If BACKWARD is non-nil, jump to the previous match."
978 ;; for each clone), but that means that clones need to collaborate a bit. 1016 ;; for each clone), but that means that clones need to collaborate a bit.
979 ;; I guess it mostly means: detect when a reconversion process is already 1017 ;; I guess it mostly means: detect when a reconversion process is already
980 ;; running, and run the sentinel in all clones. 1018 ;; running, and run the sentinel in all clones.
981 ;; 1019 ;;
982 ;; Maybe the clones should really have a separate /tmp directory 1020 ;; Maybe the clones should really have a separate /tmp directory
983 ;; so they could have a different resolution and you could use clones 1021 ;; so they could have a different resolution and you could use clones
984 ;; for zooming. 1022 ;; for zooming.
@@ -1037,7 +1075,7 @@ toggle between displaying the document or editing it as text.
1037 (add-hook 'image-mode-new-window-functions 1075 (add-hook 'image-mode-new-window-functions
1038 'doc-view-new-window-function nil t) 1076 'doc-view-new-window-function nil t)
1039 (image-mode-setup-winprops) 1077 (image-mode-setup-winprops)
1040 1078
1041 (set (make-local-variable 'mode-line-position) 1079 (set (make-local-variable 'mode-line-position)
1042 '(" P" (:eval (number-to-string (doc-view-current-page))) 1080 '(" P" (:eval (number-to-string (doc-view-current-page)))
1043 "/" (:eval (number-to-string (length doc-view-current-files))))) 1081 "/" (:eval (number-to-string (length doc-view-current-files)))))
@@ -1071,8 +1109,7 @@ See the command `doc-view-mode' for more information on this mode."
1071(defun doc-view-clear-cache () 1109(defun doc-view-clear-cache ()
1072 "Delete the whole cache (`doc-view-cache-directory')." 1110 "Delete the whole cache (`doc-view-cache-directory')."
1073 (interactive) 1111 (interactive)
1074 (dired-delete-file doc-view-cache-directory 'always) 1112 (dired-delete-file doc-view-cache-directory 'always))
1075 (make-directory doc-view-cache-directory))
1076 1113
1077(defun doc-view-dired-cache () 1114(defun doc-view-dired-cache ()
1078 "Open `dired' in `doc-view-cache-directory'." 1115 "Open `dired' in `doc-view-cache-directory'."