]> code.delx.au - gnu-emacs/blobdiff - lisp/gnus/mailcap.el
Clean up nnimap-request-move-article slightly
[gnu-emacs] / lisp / gnus / mailcap.el
index 772024b5dcec94913a250fd9f62e5948130ba99b..609a8f4d64b5b64f5a4906649ef67b0a82f69287 100644 (file)
@@ -1,6 +1,6 @@
 ;;; mailcap.el --- MIME media types configuration
 
-;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
+;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
 
 ;; Author: William M. Perry <wmperry@aventail.com>
 ;;     Lars Magne Ingebrigtsen <larsi@gnus.org>
 (eval-when-compile (require 'cl))
 (autoload 'mail-header-parse-content-type "mail-parse")
 
-;; `mm-delete-duplicates' is an alias for `delete-dups' in Emacs 22.
-(defalias 'mailcap-delete-duplicates
-  (if (fboundp 'delete-dups)
-      'delete-dups
-    (autoload 'mm-delete-duplicates "mm-util")
-    'mm-delete-duplicates))
-
-;; `mailcap-replace-in-string' is an alias like `gnus-replace-in-string'.
-(eval-and-compile
-  (cond
-   ((fboundp 'replace-regexp-in-string)
-    (defun mailcap-replace-in-string  (string regexp newtext &optional literal)
-      "Replace all matches for REGEXP with NEWTEXT in STRING.
-If LITERAL is non-nil, insert NEWTEXT literally.  Return a new
-string containing the replacements.
-This is a compatibility function for different Emacsen."
-      (replace-regexp-in-string regexp newtext string nil literal)))
-   ((fboundp 'replace-in-string)
-    (defalias 'mailcap-replace-in-string 'replace-in-string))))
-
 (defgroup mailcap nil
   "Definition of viewers for MIME types."
   :version "21.1"
@@ -66,12 +46,6 @@ This is a compatibility function for different Emacsen."
     table)
   "A syntax table for parsing SGML attributes.")
 
-(eval-and-compile
-  (when (featurep 'xemacs)
-    (condition-case nil
-       (require 'lpr)
-      (error nil))))
-
 (defvar mailcap-print-command
   (mapconcat 'identity
             (cons (if (boundp 'lpr-command)
@@ -96,11 +70,9 @@ This is a compatibility function for different Emacsen."
       (type . "application/vnd.ms-excel"))
      ("x-x509-ca-cert"
       (viewer . ssl-view-site-cert)
-      (test . (fboundp 'ssl-view-site-cert))
       (type . "application/x-x509-ca-cert"))
      ("x-x509-user-cert"
       (viewer . ssl-view-user-cert)
-      (test . (fboundp 'ssl-view-user-cert))
       (type . "application/x-x509-user-cert"))
      ("octet-stream"
       (viewer . mailcap-save-binary-file)
@@ -129,29 +101,32 @@ This is a compatibility function for different Emacsen."
       (type   . "application/x-tar"))
      ("x-latex"
       (viewer . tex-mode)
-      (test   . (fboundp 'tex-mode))
       (type   . "application/x-latex"))
      ("x-tex"
       (viewer . tex-mode)
-      (test   . (fboundp 'tex-mode))
       (type   . "application/x-tex"))
      ("latex"
       (viewer . tex-mode)
-      (test   . (fboundp 'tex-mode))
       (type   . "application/latex"))
      ("tex"
       (viewer . tex-mode)
-      (test   . (fboundp 'tex-mode))
       (type   . "application/tex"))
      ("texinfo"
       (viewer . texinfo-mode)
-      (test   . (fboundp 'texinfo-mode))
       (type   . "application/tex"))
      ("zip"
       (viewer . mailcap-save-binary-file)
       (non-viewer . t)
       (type   . "application/zip")
       ("copiousoutput"))
+     ("pdf"
+      (viewer . pdf-view-mode)
+      (type . "application/pdf")
+      (test . (eq window-system 'x)))
+     ("pdf"
+      (viewer . doc-view-mode)
+      (type . "application/pdf")
+      (test . (eq window-system 'x)))
      ("pdf"
       (viewer . "gv -safer %s")
       (type . "application/pdf")
@@ -192,7 +167,6 @@ This is a compatibility function for different Emacsen."
       ("copiousoutput"))
      ("sieve"
       (viewer . sieve-mode)
-      (test   . (fboundp 'sieve-mode))
       (type   . "application/sieve"))
      ("pgp-keys"
       (viewer . "gpg --import --interactive --verbose")
@@ -213,11 +187,6 @@ This is a compatibility function for different Emacsen."
       (type   . "message/rfc822"))
      ("rfc-*822"
       (viewer . vm-mode)
-      (test   . (fboundp 'vm-mode))
-      (type   . "message/rfc822"))
-     ("rfc-*822"
-      (viewer . w3-mode)
-      (test   . (fboundp 'w3-mode))
       (type   . "message/rfc822"))
      ("rfc-*822"
       (viewer . view-mode)
@@ -252,28 +221,17 @@ This is a compatibility function for different Emacsen."
       (test   . (eq window-system 'x))
       ("needsx11")))
     ("text"
-     ("plain"
-      (viewer  . w3-mode)
-      (test    . (fboundp 'w3-mode))
-      (type    . "text/plain"))
      ("plain"
       (viewer  . view-mode)
-      (test    . (fboundp 'view-mode))
       (type    . "text/plain"))
      ("plain"
       (viewer  . fundamental-mode)
       (type    . "text/plain"))
      ("enriched"
       (viewer . enriched-decode)
-      (test   . (fboundp 'enriched-decode))
       (type   . "text/enriched"))
-     ("html"
-      (viewer . mm-w3-prepare-buffer)
-      (test   . (fboundp 'w3-prepare-buffer))
-      (type   . "text/html"))
      ("dns"
       (viewer . dns-mode)
-      (test   . (fboundp 'dns-mode))
       (type   . "text/dns")))
     ("video"
      ("mpeg"
@@ -290,8 +248,7 @@ This is a compatibility function for different Emacsen."
     ("archive"
      ("tar"
       (viewer . tar-mode)
-      (type . "archive/tar")
-      (test . (fboundp 'tar-mode)))))
+      (type . "archive/tar"))))
   "The mailcap structure is an assoc list of assoc lists.
 1st assoc list is keyed on the major content-type
 2nd assoc list is keyed on the minor content-type (which can be a regexp)
@@ -312,9 +269,9 @@ attribute name (viewer, test, etc).  This looks like:
   FLAG)
 
 Where VIEWERINFO specifies how the content-type is viewed.  Can be
-a string, in which case it is run through a shell, with
-appropriate parameters, or a symbol, in which case the symbol is
-`funcall'ed, with the buffer as an argument.
+a string, in which case it is run through a shell, with appropriate
+parameters, or a symbol, in which case the symbol is `funcall'ed if
+and only if it exists as a function, with the buffer as an argument.
 
 TESTINFO is a test for the viewer's applicability, or nil.  If nil, it
 means the viewer is always valid.  If it is a Lisp function, it is
@@ -558,8 +515,11 @@ MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
       results)))
 
 (defun mailcap-mailcap-entry-passes-test (info)
-  "Return non-nil if mailcap entry INFO passes its test clause.
-Also return non-nil if no test clause is present."
+  "Replace the test clause of INFO itself with a boolean for some cases.
+This function supports only `test -n $DISPLAY' and `test -z $DISPLAY',
+replaces them with t or nil.  As for others or if INFO has a interactive
+spec (needsterm, needsterminal, or needsx11) but DISPLAY is not set,
+the test clause will be unchanged."
   (let ((test (assq 'test info))       ; The test clause
        status)
     (setq status (and test (split-string (cdr test) " ")))
@@ -657,10 +617,12 @@ to supply to the test."
   (let* ((test-info (assq 'test viewer-info))
         (test (cdr test-info))
         (otest test)
-        (viewer (cdr (assoc 'viewer viewer-info)))
+        (viewer (cdr (assq 'viewer viewer-info)))
         (default-directory (expand-file-name "~/"))
         status parsed-test cache result)
-    (cond ((setq cache (assoc test mailcap-viewer-test-cache))
+    (cond ((not (or (stringp viewer) (fboundp viewer)))
+          nil)                         ; Non-existent Lisp function
+         ((setq cache (assoc test mailcap-viewer-test-cache))
           (cadr cache))
          ((not test-info) t)           ; No test clause
          (t
@@ -1005,7 +967,7 @@ If FORCE, re-parse even if already parsed."
 (defun mailcap-mime-types ()
   "Return a list of MIME media types."
   (mailcap-parse-mimetypes)
-  (mailcap-delete-duplicates
+  (delete-dups
    (nconc
     (mapcar 'cdr mailcap-mime-extensions)
     (apply
@@ -1033,14 +995,14 @@ If FORCE, re-parse even if already parsed."
   (mailcap-parse-mimetypes)
   (let* ((all-mime-type
          ;; All unique MIME types from file extensions
-         (mailcap-delete-duplicates
+         (delete-dups
           (mapcar (lambda (file)
                     (mailcap-extension-to-mime
                      (file-name-extension file t)))
                   files)))
         (all-mime-info
          ;; All MIME info lists
-         (mailcap-delete-duplicates
+         (delete-dups
           (mapcar (lambda (mime-type)
                     (mailcap-mime-info mime-type 'all))
                   all-mime-type)))
@@ -1058,20 +1020,35 @@ If FORCE, re-parse even if already parsed."
            (car all-mime-info)))
         (commands
          ;; Command strings from `viewer' field of the MIME info
-         (mailcap-delete-duplicates
-          (delq nil (mapcar (lambda (mime-info)
-                              (let ((command (cdr (assoc 'viewer mime-info))))
-                                (if (stringp command)
-                                    (mailcap-replace-in-string
-                                     ;; Replace mailcap's `%s' placeholder
-                                     ;; with dired's `?' placeholder
-                                     (mailcap-replace-in-string
-                                      ;; Remove the final filename placeholder
-                                      command "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" "" t)
-                                     "%s" "?" t))))
+         (delete-dups
+          (delq nil (mapcar
+                     (lambda (mime-info)
+                       (let ((command (cdr (assoc 'viewer mime-info))))
+                         (if (stringp command)
+                             (replace-regexp-in-string
+                              ;; Replace mailcap's `%s' placeholder
+                              ;; with dired's `?' placeholder
+                              "%s" "?"
+                              (replace-regexp-in-string
+                               ;; Remove the final filename placeholder
+                               "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" ""
+                               command nil t)
+                              nil t))))
                             common-mime-info)))))
     commands))
 
+(defun mailcap-view-mime (type)
+  "View the data in the current buffer that has MIME type TYPE.
+`mailcap-mime-data' determines the method to use."
+  (let ((method (mailcap-mime-info type)))
+    (if (stringp method)
+       (shell-command-on-region (point-min) (point-max)
+                                ;; Use stdin as the "%s".
+                                (format method "-")
+                                (current-buffer)
+                                t)
+      (funcall method))))
+
 (provide 'mailcap)
 
 ;;; mailcap.el ends here