emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] Changes to emacs/lisp/doc-view.el,v


From: Stefan Monnier
Subject: [Emacs-diffs] Changes to emacs/lisp/doc-view.el,v
Date: Wed, 12 Mar 2008 14:50:41 +0000

CVSROOT:        /sources/emacs
Module name:    emacs
Changes by:     Stefan Monnier <monnier>        08/03/12 14:50:40

Index: doc-view.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/doc-view.el,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -b -r1.53 -r1.54
--- doc-view.el 12 Mar 2008 12:04:59 -0000      1.53
+++ doc-view.el 12 Mar 2008 14:50:35 -0000      1.54
@@ -103,7 +103,7 @@
 ;; - share more code with image-mode.
 ;; - better menu.
 ;; - Bind slicing to a drag event.
-;; - doc-view-fit-doc-to-window and doc-view-fit-window-to-doc.
+;; - doc-view-fit-doc-to-window and doc-view-fit-window-to-doc?
 ;; - zoom the region around the cursor (like xdvi).
 ;; - get rid of the silly arrow in the fringe.
 ;; - improve anti-aliasing (pdf-utils gets it better).
@@ -214,6 +214,7 @@
   (let ((ol (image-mode-window-get 'overlay winprops)))
     (if ol
         (setq ol (copy-overlay ol))
+      (assert (not (get-char-property (point-min) 'display (car winprops))))
       (setq ol (make-overlay (point-min) (point-max) nil t))
       (overlay-put ol 'doc-view t))
     (overlay-put ol 'window (car winprops))
@@ -248,6 +249,10 @@
 the (uncompressed, extracted) file residing in
 `doc-view-cache-directory'.")
 
+(defvar doc-view-doc-type nil
+  "The type of document in the current buffer.
+Can be `dvi', `pdf', or `ps'.")
+
 ;;;; DocView Keymaps
 
 (defvar doc-view-mode-map
@@ -615,13 +620,13 @@
   "Convert the current document to text and call CALLBACK with CB-ARGS when 
done."
   (make-directory (doc-view-current-cache-dir))
   (let ((ext (file-name-extension doc-view-buffer-file-name)))
-    (cond
-     ((string= ext "pdf")
+    (case doc-view-doc-type
+     (pdf
       ;; Doc is a PDF, so convert it to TXT
       (if cb-args
          (doc-view-pdf->txt doc-view-buffer-file-name txt callback cb-args)
        (doc-view-pdf->txt doc-view-buffer-file-name txt callback)))
-     ((string= ext "ps")
+     (ps
       ;; Doc is a PS, so convert it to PDF (which will be converted to
       ;; TXT thereafter).
       (let ((pdf (expand-file-name "doc.pdf"
@@ -633,7 +638,7 @@
          (doc-view-ps->pdf doc-view-buffer-file-name pdf
                            'doc-view-pdf->txt
                            pdf txt callback))))
-     ((string= ext "dvi")
+     (dvi
       ;; Doc is a DVI.  This means that a doc.pdf already exists in its
       ;; cache subdirectory.
       (if cb-args
@@ -683,14 +688,16 @@
   (let ((png-file (expand-file-name "page-%d.png"
                                     (doc-view-current-cache-dir))))
     (make-directory (doc-view-current-cache-dir))
-    (if (not (string= (file-name-extension doc-view-buffer-file-name) "dvi"))
-       ;; Convert to PNG images.
-       (doc-view-pdf/ps->png doc-view-buffer-file-name png-file)
+    (case doc-view-doc-type
+      (dvi
       ;; DVI files have to be converted to PDF before Ghostscript can process
       ;; it.
       (let ((pdf (expand-file-name "doc.pdf" doc-view-current-cache-dir)))
        (doc-view-dvi->pdf doc-view-buffer-file-name
-                          pdf 'doc-view-pdf/ps->png pdf png-file)))))
+                            pdf 'doc-view-pdf/ps->png pdf png-file)))
+      (t
+       ;; Convert to PNG images.
+       (doc-view-pdf/ps->png doc-view-buffer-file-name png-file)))))
 
 ;;;; Slicing
 
@@ -1023,6 +1030,11 @@
   (remove-overlays (point-min) (point-max) 'doc-view t)
   (if (consp image-mode-winprops-alist) (setq image-mode-winprops-alist nil)))
 
+(defun doc-view-intersection (l1 l2)
+  (let ((l ()))
+    (dolist (x l1) (if (memq x l2) (push x l)))
+    l))
+
 ;;;###autoload
 (defun doc-view-mode ()
   "Major mode in DocView buffers.
@@ -1037,6 +1049,30 @@
     (kill-all-local-variables)
     (set (make-local-variable 'doc-view-previous-major-mode) prev-major-mode))
 
+  ;; Figure out the document type.
+  (let ((name-types
+         (when buffer-file-name
+           (cdr (assoc (file-name-extension buffer-file-name)
+                       '(("dvi" dvi)
+                         ("pdf" pdf)
+                         ("epdf" pdf)
+                         ("ps" ps)
+                         ("eps" ps))))))
+        (content-types
+         (save-excursion
+           (goto-char (point-min))
+           (cond
+            ((looking-at "%!") '(ps))
+            ((looking-at "%PDF") '(pdf))
+            ((looking-at "\367\002") '(dvi))))))
+    (set (make-local-variable 'doc-view-doc-type)
+         (car (or (doc-view-intersection name-types content-types)
+                  (when (and name-types content-types)
+                    (error "Conflicting types: name says %s but content says 
%s"
+                           name-types content-types))
+                  name-types content-types
+                  (error "Cannot determine the document type")))))
+                  
   (doc-view-make-safe-dir doc-view-cache-directory)
   ;; Handle compressed files, remote files, files inside archives
   (set (make-local-variable 'doc-view-buffer-file-name)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]