[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Re: View PDF, PS within emacs

>>>>> In [emacs-w3m : No.10034] Katsumi Yamaoka wrote:
> I tried a quick hack.  It works, however it has to be much improved.

I've improved the `doc-view-mode' support for emacs-w3m.  A patch
for the CVS trunk is below.  It works asynchronously and requires
no temp file.  In this version, the new `w3m-use-doc-view-mode'
variable controls whether to use the `doc-view-mode'.

| w3m-use-doc-view-mode is a variable defined in `w3m.elc'.
| Its value is 
| ("application/dvi" "application/postscript" "application/pdf")
| Documentation:
| List of content types for which to use `doc-view-mode' to view contents.
| This overrides `w3m-content-type-alist'.

For remote files, the content type of a file is compared with
the value of this variable after retrieving the contents.  And
the file is displayed using `doc-view-mode' if its content type
matches one of the value.

But it can be altered to the way letting `w3m-content-type-alist'
control whether to use the `doc-view-mode'.  Which do you think
to be good?

On the other hand, for local files (e.g. file:///dir/file.pdf)
`auto-mode-alist' controls how to display them, because they
will be open by a `find-file-*' function which is the default
value of `w3m-local-find-file-function'.

(I noticed there is no way to view file:///dir/file.pdf using
the external viewer such as acroread.  Oops!)
--- w3m.el~	2008-02-20 08:08:23 +0000
+++ w3m.el	2008-02-29 10:05:28 +0000
@@ -161,6 +161,8 @@
 ;; Avoid byte-compile warnings.
+  (autoload 'doc-view-mode "doc-view" nil t)
+  (autoload 'doc-view-mode-p "doc-view")
   (autoload 'rfc2368-parse-mailto-url "rfc2368")
   (autoload 'widget-convert-button "wid-edit")
   (autoload 'widget-forward "wid-edit" nil t)
@@ -395,6 +397,18 @@
   :group 'w3m
   :type 'boolean)
+(defcustom w3m-use-doc-view-mode
+  (condition-case nil
+      (delq nil (mapcar (lambda (type)
+			  (if (doc-view-mode-p type)
+			      (format "application/%s" type)))
+			'(dvi postscript pdf)))
+    (error nil))
+  "List of content types for which to use `doc-view-mode' to view contents.
+This overrides `w3m-content-type-alist'."
+  :group 'w3m
+  :type '(repeat (string :tag "Type" :value "application/")))
 (defcustom w3m-imitate-widget-button '(eq major-mode 'gnus-article-mode)
   "*If non-nil, imitate the widget buttons on link (anchor) buttons.
 It is useful for moving about in a Gnus article buffer using TAB key.
@@ -5710,6 +5724,8 @@
     (w3m-create-text-page url type charset page-buffer))
    ((string-match "\\`image/" type)
     (w3m-create-image-page url type charset page-buffer))
+   ((member type w3m-use-doc-view-mode)
+    (w3m-doc-view url))
     (with-current-buffer page-buffer
       (w3m-external-view url)
@@ -6877,7 +6893,9 @@
 	(w3m-quit force)
       (setq cur (current-buffer))
       (if (w3m-use-tab-p)
-	  (w3m-next-buffer -1)
+	  (save-window-excursion
+	    (select-window (or (get-buffer-window cur t) (selected-window)))
+	    (w3m-next-buffer -1))
 	;; List buffers being shown in the other windows of the current frame.
 	  (walk-windows (lambda (window)
@@ -8062,6 +8080,39 @@
 	(copy-file ftp filename)
 	(message "Wrote %s" filename)))))
+(defun w3m-doc-view (url)
+  "View PDF/PostScript/DVI files using `doc-view-mode'.
+`w3m-pop-up-windows' and `w3m-pop-up-frames' control how the document
+window turns up."
+  (let* ((basename (file-name-nondirectory (w3m-url-strip-query url)))
+	 (regexp (concat "\\`" (regexp-quote basename) "\\(?:<[0-9]+>\\)?\\'"))
+	 (buffers (buffer-list))
+	 buffer data case-fold-search)
+    (save-current-buffer
+      (while buffers
+	(setq buffer (pop buffers))
+	(if (and (string-match regexp (buffer-name buffer))
+		 (progn
+		   (set-buffer buffer)
+		   (eq major-mode 'doc-view-mode))
+		 (equal buffer-file-name url))
+	    (setq buffers nil)
+	  (setq buffer nil))))
+    (unless (prog1
+		buffer
+	      (unless buffer
+		(setq buffer (generate-new-buffer basename)
+		      data (buffer-string)))
+	      (let ((pop-up-windows w3m-pop-up-windows)
+		    (pop-up-frames w3m-pop-up-frames))
+		(pop-to-buffer buffer)))
+      (set-buffer-multibyte nil)
+      (insert data)
+      (set-buffer-modified-p nil)
+      (setq buffer-file-name url)
+      (doc-view-mode)
+      'internal-view)))
   (unless (fboundp 'w3m-add-local-hook)
     ;; Silence the byte compiler; `w3m-add-local-hook' will be defined