comparison lisp/doc-view.el @ 91789:0048d198c131

(doc-view-display): Change file arg to buffer arg, so it works also for buffers w/o buffer-file-name. Update callers. (doc-view-clone-buffer-hook): New fun. (doc-view-mode): Use it for indirect clones. Mark the overlays with the `doc-view' property so they can be recognized.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Tue, 12 Feb 2008 02:41:55 +0000
parents 66471668862f
children db743b98a24b
comparison
equal deleted inserted replaced
91788:14206a515e37 91789:0048d198c131
549 mode-line-process nil) 549 mode-line-process nil)
550 (when doc-view-current-timer 550 (when doc-view-current-timer
551 (cancel-timer doc-view-current-timer) 551 (cancel-timer doc-view-current-timer)
552 (setq doc-view-current-timer nil)) 552 (setq doc-view-current-timer nil))
553 ;; Yippie, finished. Update the display! 553 ;; Yippie, finished. Update the display!
554 (doc-view-display buffer-file-name 'force)))) 554 (doc-view-display (current-buffer) 'force))))
555 555
556 (defun doc-view-pdf/ps->png (pdf-ps png) 556 (defun doc-view-pdf/ps->png (pdf-ps png)
557 "Convert PDF-PS to PNG asynchronously." 557 "Convert PDF-PS to PNG asynchronously."
558 (setq doc-view-current-converter-process 558 (setq doc-view-current-converter-process
559 ;; Make sure the process is started in an existing directory, 559 ;; Make sure the process is started in an existing directory,
573 'doc-view-pdf/ps->png-sentinel) 573 'doc-view-pdf/ps->png-sentinel)
574 (when doc-view-conversion-refresh-interval 574 (when doc-view-conversion-refresh-interval
575 (setq doc-view-current-timer 575 (setq doc-view-current-timer
576 (run-at-time "1 secs" doc-view-conversion-refresh-interval 576 (run-at-time "1 secs" doc-view-conversion-refresh-interval
577 'doc-view-display 577 'doc-view-display
578 buffer-file-name)))) 578 (current-buffer)))))
579 579
580 (defun doc-view-pdf->txt-sentinel (proc event) 580 (defun doc-view-pdf->txt-sentinel (proc event)
581 (if (not (string-match "finished" event)) 581 (if (not (string-match "finished" event))
582 (message "DocView: converter process changed status to %s." event) 582 (message "DocView: converter process changed status to %s." event)
583 (let ((current-buffer (current-buffer)) 583 (let ((current-buffer (current-buffer))
729 Predicate for sorting `doc-view-current-files'." 729 Predicate for sorting `doc-view-current-files'."
730 (or (< (length a) (length b)) 730 (or (< (length a) (length b))
731 (and (= (length a) (length b)) 731 (and (= (length a) (length b))
732 (string< a b)))) 732 (string< a b))))
733 733
734 (defun doc-view-display (doc &optional force) 734 (defun doc-view-display (buffer &optional force)
735 "Start viewing the document DOC. 735 "Start viewing the document in BUFFER.
736 If FORCE is non-nil, start viewing even if the document does not 736 If FORCE is non-nil, start viewing even if the document does not
737 have the page we want to view." 737 have the page we want to view."
738 (with-current-buffer (get-file-buffer doc) 738 (with-current-buffer buffer
739 (setq doc-view-current-files 739 (setq doc-view-current-files
740 (sort (directory-files (doc-view-current-cache-dir) t 740 (sort (directory-files (doc-view-current-cache-dir) t
741 "page-[0-9]+\\.png" t) 741 "page-[0-9]+\\.png" t)
742 'doc-view-sort)) 742 'doc-view-sort))
743 (when (or force 743 (when (or force
776 (if (eq major-mode 'doc-view-mode) 776 (if (eq major-mode 'doc-view-mode)
777 ;; Switch to editing mode 777 ;; Switch to editing mode
778 (progn 778 (progn
779 (doc-view-kill-proc) 779 (doc-view-kill-proc)
780 (setq buffer-read-only nil) 780 (setq buffer-read-only nil)
781 (delete-overlay doc-view-current-overlay) 781 (remove-overlays (point-min) (point-max) 'doc-view)
782 ;; Switch to the previously used major mode or fall back to fundamental 782 ;; Switch to the previously used major mode or fall back to fundamental
783 ;; mode. 783 ;; mode.
784 (if doc-view-previous-major-mode 784 (if doc-view-previous-major-mode
785 (funcall doc-view-previous-major-mode) 785 (funcall doc-view-previous-major-mode)
786 (fundamental-mode)) 786 (fundamental-mode))
924 (doc-view-buffer-message) 924 (doc-view-buffer-message)
925 (setq doc-view-current-page (or doc-view-current-page 1)) 925 (setq doc-view-current-page (or doc-view-current-page 1))
926 (if (file-exists-p (doc-view-current-cache-dir)) 926 (if (file-exists-p (doc-view-current-cache-dir))
927 (progn 927 (progn
928 (message "DocView: using cached files!") 928 (message "DocView: using cached files!")
929 (doc-view-display buffer-file-name 'force)) 929 (doc-view-display (current-buffer) 'force))
930 (doc-view-convert-current-doc)) 930 (doc-view-convert-current-doc))
931 (message 931 (message
932 "%s" 932 "%s"
933 (substitute-command-keys 933 (substitute-command-keys
934 (concat "Type \\[doc-view-toggle-display] to toggle between " 934 (concat "Type \\[doc-view-toggle-display] to toggle between "
939 (concat "No image (png) support available or some conversion utility for " 939 (concat "No image (png) support available or some conversion utility for "
940 (file-name-extension doc-view-buffer-file-name)" files is missing. " 940 (file-name-extension doc-view-buffer-file-name)" files is missing. "
941 "Type \\[doc-view-toggle-display] to switch to an editing mode."))))) 941 "Type \\[doc-view-toggle-display] to switch to an editing mode.")))))
942 942
943 (defvar bookmark-make-cell-function) 943 (defvar bookmark-make-cell-function)
944
945 (defun doc-view-clone-buffer-hook ()
946 ;; FIXME: There are several potential problems linked with reconversion
947 ;; and auto-revert when we have indirect buffers because they share their
948 ;; /tmp cache directory. This sharing is good (you'd rather not reconvert
949 ;; for each clone), but that means that clones need to collaborate a bit.
950 ;; I guess it mostly means: detect when a reconversion process is already
951 ;; running, and run the sentinel in all clones.
952 ;; Not sure how important it is to fix it: a better target would be to
953 ;; allow a single buffer (without cloning) to display different pages in
954 ;; different windows.
955 ;; Maybe then the clones should really have a separate /tmp directory
956 ;; so they could have a different resolution and you could use clones
957 ;; for zooming.
958 (dolist (ol (overlays-in (point-min) (point-max)))
959 ;; The overlay was copied by the cloning, so we just need to find it
960 ;; and put it in doc-view-current-overlay.
961 (if (overlay-get ol 'doc-view) (setq doc-view-current-overlay ol))))
944 962
945 ;;;###autoload 963 ;;;###autoload
946 (defun doc-view-mode () 964 (defun doc-view-mode ()
947 "Major mode in DocView buffers. 965 "Major mode in DocView buffers.
948 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to 966 You can use \\<doc-view-mode-map>\\[doc-view-toggle-display] to
986 (make-local-variable 'doc-view-current-cache-dir) 1004 (make-local-variable 'doc-view-current-cache-dir)
987 (make-local-variable 'doc-view-current-info) 1005 (make-local-variable 'doc-view-current-info)
988 (make-local-variable 'doc-view-current-search-matches) 1006 (make-local-variable 'doc-view-current-search-matches)
989 (set (make-local-variable 'doc-view-current-overlay) 1007 (set (make-local-variable 'doc-view-current-overlay)
990 (make-overlay (point-min) (point-max) nil t)) 1008 (make-overlay (point-min) (point-max) nil t))
1009 (overlay-put doc-view-current-overlay 'doc-view t)
991 (add-hook 'change-major-mode-hook 1010 (add-hook 'change-major-mode-hook
992 (lambda () (delete-overlay doc-view-current-overlay)) 1011 (lambda () (remove-overlays (point-min) (point-max) 'doc-view))
993 nil t) 1012 nil t)
1013 (add-hook 'clone-indirect-buffer-hook 'doc-view-clone-buffer-hook nil t)
994 1014
995 ;; Keep track of [vh]scroll when switching buffers 1015 ;; Keep track of [vh]scroll when switching buffers
996 (make-local-variable 'image-mode-current-hscroll) 1016 (make-local-variable 'image-mode-current-hscroll)
997 (make-local-variable 'image-mode-current-vscroll) 1017 (make-local-variable 'image-mode-current-vscroll)
998 (image-set-window-hscroll (selected-window) (window-hscroll)) 1018 (image-set-window-hscroll (selected-window) (window-hscroll))