unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* doc-view-set-doc-type's generality
@ 2019-12-04  9:32 Juanma Barranquero
  2019-12-04 13:57 ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Juanma Barranquero @ 2019-12-04  9:32 UTC (permalink / raw)
  To: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 2593 bytes --]

I was perusing doc-view.el and I've been quite puzzled by
doc-view-set-doc-type.

(defun doc-view-set-doc-type ()
  "Figure out the current document type (`doc-view-doc-type')."
  (let ((name-types
         (when buffer-file-name
           (cdr (assoc-string
                 (file-name-extension buffer-file-name)
                 '(
                   ;; DVI
                   ("dvi" dvi)
                   ;; PDF
                   ("pdf" pdf) ("epdf" pdf)
                   ;; PostScript
                   ("ps" ps) ("eps" ps)
                   ;; DjVu
                   ("djvu" djvu)
                   ;; OpenDocument formats.
                   ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
                   ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
                   ("ots" odf) ("otp" odf) ("otg" odf)
                   ;; Microsoft Office formats (also handled by the odf
                   ;; conversion chain).
                   ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
                   ("ppt" odf) ("pps" odf) ("pptx" odf) ("rtf" odf))
                 t))))
        (content-types
         (save-excursion
           (goto-char (point-min))
           (cond
            ((looking-at "%!") '(ps))
            ((looking-at "%PDF") '(pdf))
            ((looking-at "\367\002") '(dvi))
            ((looking-at "AT&TFORM") '(djvu))))))
    (setq-local
     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"))))))

It is written as if, for a given document (in a buffer), there could be
more than a name type (but name-types is derived from the file's
extension), and more than one content type (but content-types is derived
from looking at the few first bytes at the beginning of the buffer). And
then there's a function doc-view-intersection whose only use is to detect
cases of the name-types in the content-types' "list".

I mean, I could see the logic if it the code tried to detect cases of one
extension (let's say, ".doc") that could represent different content-types.
But in fact, currently the situation is the opposite: multiple extensions
associated to one content-type (like .ps / .eps, etc.)

Seems to me generality for generality's sake, as the code has only had
minor alteration (adding djvu and some assoc changes) in nine years.

[-- Attachment #2: Type: text/html, Size: 3521 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: doc-view-set-doc-type's generality
  2019-12-04  9:32 doc-view-set-doc-type's generality Juanma Barranquero
@ 2019-12-04 13:57 ` Stefan Monnier
  2019-12-04 14:05   ` Juanma Barranquero
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2019-12-04 13:57 UTC (permalink / raw)
  To: Juanma Barranquero; +Cc: Emacs developers

> I mean, I could see the logic if it the code tried to detect cases of one
> extension (let's say, ".doc") that could represent different content-types.

AFAICT the main purpose is to detect the case of a pdf content with
a .doc extension and vice-versa.  It's based on the idea that neither
extensions nor "magic content" are 100% reliable.


        Stefan




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: doc-view-set-doc-type's generality
  2019-12-04 13:57 ` Stefan Monnier
@ 2019-12-04 14:05   ` Juanma Barranquero
  0 siblings, 0 replies; 3+ messages in thread
From: Juanma Barranquero @ 2019-12-04 14:05 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 3358 bytes --]

Well, yes, that's what it does. But it doesn't need to build a list of
name-types
based on just one file name and it does not need to build a list of
content-types
based on the beginning of just one buffer...

I mean, AFAICS this would do the same and would not require
`doc-view-intersection',
just eq'ing two symbols.

diff --git i/lisp/doc-view.el w/lisp/doc-view.el
index e5f0e89a36..58f8535328 100644
--- i/lisp/doc-view.el
+++ w/lisp/doc-view.el
@@ -1753,4 +1753,5 @@ doc-view-clone-buffer-hook

 (defun doc-view-intersection (l1 l2)
+  (declare (obsolete nil "28.1"))
   (let ((l ()))
     (dolist (x l1) (if (memq x l2) (push x l)))
@@ -1759,5 +1760,5 @@ doc-view-intersection
 (defun doc-view-set-doc-type ()
   "Figure out the current document type (`doc-view-doc-type')."
-  (let ((name-types
+  (let ((name-type
  (when buffer-file-name
    (cdr (assoc-string
@@ -1765,36 +1766,36 @@ doc-view-set-doc-type
                  '(
                    ;; DVI
-                   ("dvi" dvi)
+                   ("dvi" . dvi)
                    ;; PDF
-                   ("pdf" pdf) ("epdf" pdf)
+                   ("pdf" . pdf) ("epdf" . pdf)
                    ;; PostScript
-                   ("ps" ps) ("eps" ps)
+                   ("ps" . ps) ("eps" . ps)
                    ;; DjVu
-                   ("djvu" djvu)
+                   ("djvu" . djvu)
                    ;; OpenDocument formats.
-                   ("odt" odf) ("ods" odf) ("odp" odf) ("odg" odf)
-                   ("odc" odf) ("odi" odf) ("odm" odf) ("ott" odf)
-                   ("ots" odf) ("otp" odf) ("otg" odf)
+                   ("odt" . odf) ("ods" . odf) ("odp" . odf) ("odg" . odf)
+                   ("odc" . odf) ("odi" . odf) ("odm" . odf) ("ott" . odf)
+                   ("ots" . odf) ("otp" . odf) ("otg" . odf)
                    ;; Microsoft Office formats (also handled by the odf
                    ;; conversion chain).
-                   ("doc" odf) ("docx" odf) ("xls" odf) ("xlsx" odf)
-                   ("ppt" odf) ("pps" odf) ("pptx" odf) ("rtf" odf))
+                   ("doc" . odf) ("docx" . odf) ("xls" . odf) ("xlsx" .
odf)
+                   ("ppt" . odf) ("pps" . odf) ("pptx" . odf) ("rtf" .
odf))
  t))))
- (content-types
+ (content-type
  (save-excursion
    (goto-char (point-min))
    (cond
-    ((looking-at "%!") '(ps))
-    ((looking-at "%PDF") '(pdf))
-    ((looking-at "\367\002") '(dvi))
-    ((looking-at "AT&TFORM") '(djvu))))))
+    ((looking-at "%!") 'ps)
+    ((looking-at "%PDF") 'pdf)
+    ((looking-at "\367\002") 'dvi)
+    ((looking-at "AT&TFORM") 'djvu)))))
     (setq-local
      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"))))))
+     (or (and (eq name-type content-type) name-type)
+         (when (and name-type content-type)
+           (error "Conflicting types: name says %s but content says %s"
+                  name-type content-type))
+         name-type content-type
+         (error "Cannot determine the document type")))))

 (defun doc-view-set-up-single-converter ()

[-- Attachment #2: Type: text/html, Size: 4962 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-04 14:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-04  9:32 doc-view-set-doc-type's generality Juanma Barranquero
2019-12-04 13:57 ` Stefan Monnier
2019-12-04 14:05   ` Juanma Barranquero

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).