1 ;;; mailcap.el --- MIME media types configuration
3 ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 ;; Author: William M. Perry <wmperry@aventail.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail, multimedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Provides configuration of MIME media types from directly from Lisp
27 ;; and via the usual mailcap mechanism (RFC 1524). Deals with
28 ;; mime.types similarly.
32 (eval-when-compile (require 'cl
))
33 (autoload 'mail-header-parse-content-type
"mail-parse")
35 ;; `mm-delete-duplicates' is an alias for `delete-dups' in Emacs 22.
36 (defalias 'mailcap-delete-duplicates
37 (if (fboundp 'delete-dups
)
39 (autoload 'mm-delete-duplicates
"mm-util")
40 'mm-delete-duplicates
))
42 ;; `mailcap-replace-in-string' is an alias like `gnus-replace-in-string'.
45 ((fboundp 'replace-regexp-in-string
)
46 (defun mailcap-replace-in-string (string regexp newtext
&optional literal
)
47 "Replace all matches for REGEXP with NEWTEXT in STRING.
48 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
49 string containing the replacements.
50 This is a compatibility function for different Emacsen."
51 (replace-regexp-in-string regexp newtext string nil literal
)))
52 ((fboundp 'replace-in-string
)
53 (defalias 'mailcap-replace-in-string
'replace-in-string
))))
56 "Definition of viewers for MIME types."
60 (defvar mailcap-parse-args-syntax-table
61 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
62 (modify-syntax-entry ?
' "\"" table
)
63 (modify-syntax-entry ?
` "\"" table
)
64 (modify-syntax-entry ?
{ "(" table
)
65 (modify-syntax-entry ?
} ")" table
)
67 "A syntax table for parsing SGML attributes.")
70 (when (featurep 'xemacs
)
75 (defvar mailcap-print-command
77 (cons (if (boundp 'lpr-command
)
80 (when (boundp 'lpr-switches
)
81 (if (stringp lpr-switches
)
85 "Shell command (including switches) used to print PostScript files.")
87 ;; Postpone using defcustom for this as it's so big and we essentially
88 ;; have to have two copies of the data around then. Perhaps just
89 ;; customize the Lisp viewers and rely on the normal configuration
90 ;; files for the rest? -- fx
91 (defvar mailcap-mime-data
94 (viewer .
"gnumeric %s")
95 (test .
(getenv "DISPLAY"))
96 (type .
"application/vnd.ms-excel"))
98 (viewer . ssl-view-site-cert
)
99 (test .
(fboundp 'ssl-view-site-cert
))
100 (type .
"application/x-x509-ca-cert"))
102 (viewer . ssl-view-user-cert
)
103 (test .
(fboundp 'ssl-view-user-cert
))
104 (type .
"application/x-x509-user-cert"))
106 (viewer . mailcap-save-binary-file
)
108 (type .
"application/octet-stream"))
110 (viewer .
"xdvi -safer %s")
111 (test .
(eq window-system
'x
))
113 (type .
"application/dvi")
114 ("print" .
"dvips -qRP %s"))
116 (viewer .
"dvitty %s")
117 (test .
(not (getenv "DISPLAY")))
118 (type .
"application/dvi")
119 ("print" .
"dvips -qRP %s"))
121 (viewer . mailcap-maybe-eval
)
122 (type .
"application/emacs-lisp"))
124 (viewer . mailcap-maybe-eval
)
125 (type .
"application/x-emacs-lisp"))
127 (viewer . mailcap-save-binary-file
)
129 (type .
"application/x-tar"))
132 (test .
(fboundp 'tex-mode
))
133 (type .
"application/x-latex"))
136 (test .
(fboundp 'tex-mode
))
137 (type .
"application/x-tex"))
140 (test .
(fboundp 'tex-mode
))
141 (type .
"application/latex"))
144 (test .
(fboundp 'tex-mode
))
145 (type .
"application/tex"))
147 (viewer . texinfo-mode
)
148 (test .
(fboundp 'texinfo-mode
))
149 (type .
"application/tex"))
151 (viewer . mailcap-save-binary-file
)
153 (type .
"application/zip")
156 (viewer . pdf-view-mode
)
157 (type .
"application/pdf")
158 (test .
(and (fboundp 'pdf-view-mode
)
159 (eq window-system
'x
))))
161 (viewer . doc-view-mode
)
162 (type .
"application/pdf")
163 (test .
(eq window-system
'x
)))
165 (viewer .
"gv -safer %s")
166 (type .
"application/pdf")
167 (test . window-system
)
168 ("print" .
,(concat "pdf2ps %s - | " mailcap-print-command
)))
171 (type .
"application/pdf")
172 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
173 (test .
(eq window-system
'x
)))
176 (type .
"application/pdf")
177 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
178 (test .
(eq window-system
'x
)))
180 (viewer .
,(concat "pdftotext %s -"))
181 (type .
"application/pdf")
182 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
185 (viewer .
"gv -safer %s")
186 (type .
"application/postscript")
187 (test . window-system
)
188 ("print" .
,(concat mailcap-print-command
" %s"))
191 (viewer .
"ghostview -dSAFER %s")
192 (type .
"application/postscript")
193 (test .
(eq window-system
'x
))
194 ("print" .
,(concat mailcap-print-command
" %s"))
197 (viewer .
"ps2ascii %s")
198 (type .
"application/postscript")
199 (test .
(not (getenv "DISPLAY")))
200 ("print" .
,(concat mailcap-print-command
" %s"))
203 (viewer . sieve-mode
)
204 (test .
(fboundp 'sieve-mode
))
205 (type .
"application/sieve"))
207 (viewer .
"gpg --import --interactive --verbose")
208 (type .
"application/pgp-keys")
212 (viewer .
"maplay %s")
213 (type .
"audio/x-mpeg"))
215 (viewer .
"showaudio")
219 (viewer . mm-view-message
)
220 (test .
(and (featurep 'gnus
)
222 (type .
"message/rfc822"))
225 (test .
(fboundp 'vm-mode
))
226 (type .
"message/rfc822"))
229 (type .
"message/rfc822")))
232 (viewer .
"xwud -in %s")
233 (type .
"image/x-xwd")
234 ("compose" .
"xwd -frame > %s")
235 (test .
(eq window-system
'x
))
238 (viewer .
"xwud -in %s")
239 (type .
"image/x-xwd")
240 ("compose" .
"xwd -frame > %s")
241 (test .
(eq window-system
'x
))
244 (viewer .
"xwud -in %s")
245 (type .
"image/x-xwd")
246 ("compose" .
"xwd -frame > %s")
247 (test .
(eq window-system
'x
))
250 (viewer .
"display %s")
252 (test .
(eq window-system
'x
))
257 (test .
(eq window-system
'x
))
262 (test .
(fboundp 'view-mode
))
263 (type .
"text/plain"))
265 (viewer . fundamental-mode
)
266 (type .
"text/plain"))
268 (viewer . enriched-decode
)
269 (test .
(fboundp 'enriched-decode
))
270 (type .
"text/enriched"))
273 (test .
(fboundp 'dns-mode
))
274 (type .
"text/dns")))
277 (viewer .
"mpeg_play %s")
278 (type .
"video/mpeg")
279 (test .
(eq window-system
'x
))
283 (viewer .
"webspace -remote %s -URL %u")
284 (type .
"x-world/x-vrml")
290 (type .
"archive/tar")
291 (test .
(fboundp 'tar-mode
)))))
292 "The mailcap structure is an assoc list of assoc lists.
293 1st assoc list is keyed on the major content-type
294 2nd assoc list is keyed on the minor content-type (which can be a regexp)
299 (\"postscript\" . <info>))
301 (\"plain\" . <info>)))
303 Where <info> is another assoc list of the various information
304 related to the mailcap RFC 1524. This is keyed on the lowercase
305 attribute name (viewer, test, etc). This looks like:
306 ((viewer . VIEWERINFO)
311 Where VIEWERINFO specifies how the content-type is viewed. Can be
312 a string, in which case it is run through a shell, with
313 appropriate parameters, or a symbol, in which case the symbol is
314 `funcall'ed, with the buffer as an argument.
316 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
317 means the viewer is always valid. If it is a Lisp function, it is
318 called with a list of items from any extra fields from the
319 Content-Type header as argument to return a boolean value for the
320 validity. Otherwise, if it is a non-function Lisp symbol or list
321 whose car is a symbol, it is `eval'led to yield the validity. If it
322 is a string or list of strings, it represents a shell command to run
323 to return a true or false shell value for the validity.")
324 (put 'mailcap-mime-data
'risky-local-variable t
)
326 (defcustom mailcap-download-directory nil
327 "*Directory to which `mailcap-save-binary-file' downloads files by default.
328 nil means your home directory."
329 :type
'(choice (const :tag
"Home directory" nil
)
333 (defvar mailcap-poor-system-types
335 "Systems that don't have a Unix-like directory hierarchy.")
338 ;;; Utility functions
341 (defun mailcap-save-binary-file ()
342 (goto-char (point-min))
344 (let ((file (read-file-name
345 "Filename to save as: "
346 (or mailcap-download-directory
"~/")))
347 (require-final-newline nil
))
348 (write-region (point-min) (point-max) file
))
349 (kill-buffer (current-buffer))))
351 (defvar mailcap-maybe-eval-warning
354 This MIME part contains untrusted and possibly harmful content.
355 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
356 things can happen. Please examine the code very carefully before you
357 instruct Emacs to evaluate it. You can browse the buffer containing
358 the code using \\[scroll-other-window].
360 If you are unsure what to do, please answer \"no\"."
361 "Text of warning message displayed by `mailcap-maybe-eval'.
362 Make sure that this text consists only of few text lines. Otherwise,
363 Gnus might fail to display all of it.")
365 (defun mailcap-maybe-eval ()
366 "Maybe evaluate a buffer of Emacs Lisp code."
367 (let ((lisp-buffer (current-buffer)))
368 (goto-char (point-min))
370 (save-window-excursion
371 (delete-other-windows)
372 (let ((buffer (get-buffer-create (generate-new-buffer-name
375 (with-current-buffer buffer
376 (insert (substitute-command-keys
377 mailcap-maybe-eval-warning
))
378 (goto-char (point-min))
379 (display-buffer buffer
)
380 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
381 (kill-buffer buffer
))))
382 (eval-buffer (current-buffer)))
383 (when (buffer-live-p lisp-buffer
)
384 (with-current-buffer lisp-buffer
385 (emacs-lisp-mode)))))
389 ;;; The mailcap parser
392 (defun mailcap-replace-regexp (regexp to-string
)
393 ;; Quiet replace-regexp.
394 (goto-char (point-min))
395 (while (re-search-forward regexp nil t
)
396 (replace-match to-string t nil
)))
398 (defvar mailcap-parsed-p nil
)
400 (defun mailcap-parse-mailcaps (&optional path force
)
401 "Parse out all the mailcaps specified in a path string PATH.
402 Components of PATH are separated by the `path-separator' character
403 appropriate for this system. If FORCE, re-parse even if already
404 parsed. If PATH is omitted, use the value of environment variable
405 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
406 /usr/local/etc/mailcap."
407 (interactive (list nil t
))
408 (when (or (not mailcap-parsed-p
)
412 ((getenv "MAILCAPS") (setq path
(getenv "MAILCAPS")))
413 ((memq system-type mailcap-poor-system-types
)
414 (setq path
'("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
416 ;; This is per RFC 1524, specifically
417 ;; with /usr before /usr/local.
418 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
419 "/usr/local/etc/mailcap"))))
420 (let ((fnames (reverse
422 (split-string path path-separator t
)
426 (setq fname
(car fnames
))
427 (if (and (file-readable-p fname
)
428 (file-regular-p fname
))
429 (mailcap-parse-mailcap fname
))
430 (setq fnames
(cdr fnames
))))
431 (setq mailcap-parsed-p t
)))
433 (defun mailcap-parse-mailcap (fname)
434 "Parse out the mailcap file specified by FNAME."
435 (let (major ; The major mime type (image/audio/etc)
436 minor
; The minor mime type (gif, basic, etc)
437 save-pos
; Misc saved positions used in parsing
438 viewer
; How to view this mime type
439 info
; Misc info about this mime type
442 (insert-file-contents fname
)
443 (set-syntax-table mailcap-parse-args-syntax-table
)
444 (mailcap-replace-regexp "#.*" "") ; Remove all comments
445 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
446 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
447 (goto-char (point-max))
448 (skip-chars-backward " \t\n")
449 (delete-region (point) (point-max))
451 (skip-chars-backward " \t\n")
453 (setq save-pos
(point)
455 (skip-chars-forward "^/; \t\n")
456 (downcase-region save-pos
(point))
457 (setq major
(buffer-substring save-pos
(point)))
458 (skip-chars-forward " \t")
460 (when (eq (char-after) ?
/)
462 (skip-chars-forward " \t")
463 (setq save-pos
(point))
464 (skip-chars-forward "^; \t\n")
465 (downcase-region save-pos
(point))
468 ((eq ?
* (or (char-after save-pos
) 0)) ".*")
469 ((= (point) save-pos
) ".*")
470 (t (regexp-quote (buffer-substring save-pos
(point)))))))
471 (skip-chars-forward " \t")
472 ;;; Got the major/minor chunks, now for the viewers/etc
473 ;;; The first item _must_ be a viewer, according to the
474 ;;; RFC for mailcap files (#1524)
476 (when (eq (char-after) ?\
;)
478 (skip-chars-forward " \t")
479 (setq save-pos
(point))
480 (skip-chars-forward "^;\n")
482 (while (eq (char-before) ?
\\)
483 (backward-delete-char 1)
485 (skip-chars-forward "^;\n"))
486 (if (eq (or (char-after save-pos
) 0) ?
')
488 (narrow-to-region (1+ save-pos
) (point))
489 (goto-char (point-min))
491 (read (current-buffer))
492 (goto-char (point-max))
494 (setq viewer
(buffer-substring save-pos
(point)))))
495 (setq save-pos
(point))
497 (unless (equal viewer
"")
498 (setq info
(nconc (list (cons 'viewer viewer
)
499 (cons 'type
(concat major
"/"
500 (if (string= minor
".*")
502 (mailcap-parse-mailcap-extras save-pos
(point))))
503 (mailcap-mailcap-entry-passes-test info
)
504 (mailcap-add-mailcap-entry major minor info
))
505 (beginning-of-line)))))
507 (defun mailcap-parse-mailcap-extras (st nd
)
508 "Grab all the extra stuff from a mailcap entry."
512 results
; Assoc list of results
513 name-pos
; Start of XXXX= position
514 val-pos
; Start of value position
515 done
; Found end of \'d ;s?
518 (narrow-to-region st nd
)
519 (goto-char (point-min))
520 (skip-chars-forward " \n\t;")
523 (setq name-pos
(point))
524 (skip-chars-forward "^ \n\t=;")
525 (downcase-region name-pos
(point))
526 (setq name
(buffer-substring name-pos
(point)))
527 (skip-chars-forward " \t\n")
528 (if (not (eq (char-after (point)) ?
=)) ; There is no value
530 (skip-chars-forward " \t\n=")
531 (setq val-pos
(point))
532 (if (memq (char-after val-pos
) '(?
\" ?
'))
534 (setq val-pos
(1+ val-pos
))
539 (error (goto-char (point-max)))))
541 (skip-chars-forward "^;")
542 (if (eq (char-after (1- (point))) ?
\\ )
544 (subst-char-in-region (1- (point)) (point) ?
\\ ?
)
545 (skip-chars-forward ";"))
547 (setq value
(buffer-substring val-pos
(point))))
548 ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
550 (setq results
(cons (cons (if (string-equal name
"test")
554 (skip-chars-forward " \";\n\t"))
557 (defun mailcap-mailcap-entry-passes-test (info)
558 "Return non-nil if mailcap entry INFO passes its test clause.
559 Also return non-nil if no test clause is present."
560 (let ((test (assq 'test info
)) ; The test clause
562 (setq status
(and test
(split-string (cdr test
) " ")))
563 (if (and (or (assoc "needsterm" info
)
564 (assoc "needsterminal" info
)
565 (assoc "needsx11" info
))
566 (not (getenv "DISPLAY")))
569 ((and (equal (nth 0 status
) "test")
570 (equal (nth 1 status
) "-n")
571 (or (equal (nth 2 status
) "$DISPLAY")
572 (equal (nth 2 status
) "\"$DISPLAY\"")))
573 (setq status
(if (getenv "DISPLAY") t nil
)))
574 ((and (equal (nth 0 status
) "test")
575 (equal (nth 1 status
) "-z")
576 (or (equal (nth 2 status
) "$DISPLAY")
577 (equal (nth 2 status
) "\"$DISPLAY\"")))
578 (setq status
(if (getenv "DISPLAY") nil t
)))
581 (and test
(listp test
) (setcdr test status
))))
584 ;;; The action routines.
587 (defun mailcap-possible-viewers (major minor
)
588 "Return a list of possible viewers from MAJOR for minor type MINOR."
593 ((equal (car (car major
)) minor
)
594 (setq exact
(cons (cdr (car major
)) exact
)))
595 ((and minor
(string-match (concat "^" (car (car major
)) "$") minor
))
596 (setq wildcard
(cons (cdr (car major
)) wildcard
))))
597 (setq major
(cdr major
)))
598 (nconc exact wildcard
)))
600 (defun mailcap-unescape-mime-test (test type-info
)
601 (let (save-pos save-chr subst
)
603 ((symbolp test
) test
)
604 ((and (listp test
) (symbolp (car test
))) test
)
606 (and (listp test
) (stringp (car test
))
607 (setq test
(mapconcat 'identity test
" "))))
610 (goto-char (point-min))
612 (skip-chars-forward "^%")
614 (progn (skip-chars-backward "\\\\")
616 0) ; It is an escaped %
619 (skip-chars-forward "%."))
620 (setq save-pos
(point))
621 (skip-chars-forward "%")
622 (setq save-chr
(char-after (point)))
624 ;; %s: name of a file for the body data
626 ;; %{<parameter name}: value of parameter in mailcap entry
627 ;; %n: number of sub-parts for multipart content-type
628 ;; %F: a set of content-type/filename pairs for multiparts
630 ((null save-chr
) nil
)
632 (delete-region save-pos
(progn (forward-char 1) (point)))
633 (insert (or (cdr (assq 'type type-info
)) "\"\"")))
634 ((memq save-chr
'(?M ?n ?F
))
635 (delete-region save-pos
(progn (forward-char 1) (point)))
639 (skip-chars-forward "^}")
640 (downcase-region (+ 2 save-pos
) (point))
641 (setq subst
(buffer-substring (+ 2 save-pos
) (point)))
642 (delete-region save-pos
(1+ (point)))
643 (insert (or (cdr (assoc subst type-info
)) "\"\"")))
646 (t (error "Bad value to mailcap-unescape-mime-test: %s" test
)))))
648 (defvar mailcap-viewer-test-cache nil
)
650 (defun mailcap-viewer-passes-test (viewer-info type-info
)
651 "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
652 Also return non-nil if it has no test clause. TYPE-INFO is an argument
653 to supply to the test."
654 (let* ((test-info (assq 'test viewer-info
))
655 (test (cdr test-info
))
657 (viewer (cdr (assoc 'viewer viewer-info
)))
658 (default-directory (expand-file-name "~/"))
659 status parsed-test cache result
)
660 (cond ((setq cache
(assoc test mailcap-viewer-test-cache
))
662 ((not test-info
) t
) ; No test clause
667 ((not test
) nil
) ; Already failed test
668 ((eq test t
) t
) ; Already passed test
669 ((functionp test
) ; Lisp function as test
670 (funcall test type-info
))
671 ((and (symbolp test
) ; Lisp variable as test
674 ((and (listp test
) ; List to be eval'd
675 (symbolp (car test
)))
678 (setq test
(mailcap-unescape-mime-test test type-info
)
679 test
(list shell-file-name nil nil nil
680 shell-command-switch test
)
681 status
(apply 'call-process test
))
683 (push (list otest result
) mailcap-viewer-test-cache
)
686 (defun mailcap-add-mailcap-entry (major minor info
)
687 (let ((old-major (assoc major mailcap-mime-data
)))
688 (if (null old-major
) ; New major area
689 (setq mailcap-mime-data
690 (cons (cons major
(list (cons minor info
)))
692 (let ((cur-minor (assoc minor old-major
)))
694 ((or (null cur-minor
) ; New minor area, or
695 (assq 'test info
)) ; Has a test, insert at beginning
696 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))
697 ((and (not (assq 'test info
)) ; No test info, replace completely
698 (not (assq 'test cur-minor
))
699 (equal (assq 'viewer info
) ; Keep alternative viewer
700 (assq 'viewer cur-minor
)))
701 (setcdr cur-minor info
))
703 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))))
706 (defun mailcap-add (type viewer
&optional test
)
707 "Add VIEWER as a handler for TYPE.
708 If TEST is not given, it defaults to t."
709 (let ((tl (split-string type
"/")))
710 (when (or (not (car tl
))
712 (error "%s is not a valid MIME type" type
))
713 (mailcap-add-mailcap-entry
716 (test .
,(if test test t
))
723 (defun mailcap-viewer-lessp (x y
)
724 "Return t if viewer X is more desirable than viewer Y."
725 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x
)) "")))
726 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y
)) "")))
727 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x
)) ""))))
728 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y
)) "")))))
730 ((and x-wild
(not y-wild
))
732 ((and (not x-wild
) y-wild
)
734 ((and (not y-lisp
) x-lisp
)
738 (defun mailcap-mime-info (string &optional request no-decode
)
739 "Get the MIME viewer command for STRING, return nil if none found.
740 Expects a complete content-type header line as its argument.
742 Second argument REQUEST specifies what information to return. If it is
743 nil or the empty string, the viewer (second field of the mailcap
744 entry) will be returned. If it is a string, then the mailcap field
745 corresponding to that string will be returned (print, description,
746 whatever). If a number, then all the information for this specific
747 viewer is returned. If `all', then all possible viewers for
748 this type is returned.
750 If NO-DECODE is non-nil, don't decode STRING."
751 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
754 major
; Major encoding (text, etc)
755 minor
; Minor encoding (html, etc)
757 save-pos
; Misc. position during parse
758 major-info
; (assoc major mailcap-mime-data)
759 minor-info
; (assoc minor major-info)
760 test
; current test proc.
761 viewers
; Possible viewers
762 passed
; Viewers that passed the test
763 viewer
; The one and only viewer
768 (list (or string
"text/plain"))
769 (mail-header-parse-content-type (or string
"text/plain"))))
770 (setq major
(split-string (car ctl
) "/"))
771 (setq minor
(cadr major
)
773 (when (setq major-info
(cdr (assoc major mailcap-mime-data
)))
774 (when (setq viewers
(mailcap-possible-viewers major-info minor
))
775 (setq info
(mapcar (lambda (a) (cons (symbol-name (car a
))
779 (if (mailcap-viewer-passes-test (car viewers
) info
)
780 (setq passed
(cons (car viewers
) passed
)))
781 (setq viewers
(cdr viewers
)))
782 (setq passed
(sort passed
'mailcap-viewer-lessp
))
783 (setq viewer
(car passed
))))
784 (when (and (stringp (cdr (assq 'viewer viewer
)))
786 (setq viewer
(car passed
)))
788 ((and (null viewer
) (not (equal major
"default")) request
)
789 (mailcap-mime-info "default" request no-decode
))
790 ((or (null request
) (equal request
""))
791 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer
)) info
))
793 (mailcap-unescape-mime-test
794 (cdr-safe (assoc request viewer
)) info
))
798 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
799 (setq viewer
(copy-sequence viewer
))
800 (let ((view (assq 'viewer viewer
))
801 (test (assq 'test viewer
)))
802 (if view
(setcdr view
(mailcap-unescape-mime-test (cdr view
) info
)))
803 (if test
(setcdr test
(mailcap-unescape-mime-test (cdr test
) info
))))
807 ;;; Experimental MIME-types parsing
810 (defvar mailcap-mime-extensions
811 '(("" .
"text/plain")
812 (".1" .
"text/plain") ;; Manual pages
813 (".3" .
"text/plain")
814 (".8" .
"text/plain")
815 (".abs" .
"audio/x-mpeg")
816 (".aif" .
"audio/aiff")
817 (".aifc" .
"audio/aiff")
818 (".aiff" .
"audio/aiff")
819 (".ano" .
"application/x-annotator")
820 (".au" .
"audio/ulaw")
821 (".avi" .
"video/x-msvideo")
822 (".bcpio" .
"application/x-bcpio")
823 (".bin" .
"application/octet-stream")
824 (".cdf" .
"application/x-netcdr")
825 (".cpio" .
"application/x-cpio")
826 (".csh" .
"application/x-csh")
827 (".css" .
"text/css")
828 (".dvi" .
"application/x-dvi")
829 (".diff" .
"text/x-patch")
830 (".dpatch".
"test/x-patch")
831 (".el" .
"application/emacs-lisp")
832 (".eps" .
"application/postscript")
833 (".etx" .
"text/x-setext")
834 (".exe" .
"application/octet-stream")
835 (".fax" .
"image/x-fax")
836 (".gif" .
"image/gif")
837 (".hdf" .
"application/x-hdf")
838 (".hqx" .
"application/mac-binhex40")
839 (".htm" .
"text/html")
840 (".html" .
"text/html")
841 (".icon" .
"image/x-icon")
842 (".ief" .
"image/ief")
843 (".jpg" .
"image/jpeg")
844 (".macp" .
"image/x-macpaint")
845 (".man" .
"application/x-troff-man")
846 (".me" .
"application/x-troff-me")
847 (".mif" .
"application/mif")
848 (".mov" .
"video/quicktime")
849 (".movie" .
"video/x-sgi-movie")
850 (".mp2" .
"audio/x-mpeg")
851 (".mp3" .
"audio/x-mpeg")
852 (".mp2a" .
"audio/x-mpeg2")
853 (".mpa" .
"audio/x-mpeg")
854 (".mpa2" .
"audio/x-mpeg2")
855 (".mpe" .
"video/mpeg")
856 (".mpeg" .
"video/mpeg")
857 (".mpega" .
"audio/x-mpeg")
858 (".mpegv" .
"video/mpeg")
859 (".mpg" .
"video/mpeg")
860 (".mpv" .
"video/mpeg")
861 (".ms" .
"application/x-troff-ms")
862 (".nc" .
"application/x-netcdf")
863 (".nc" .
"application/x-netcdf")
864 (".oda" .
"application/oda")
865 (".patch" .
"text/x-patch")
866 (".pbm" .
"image/x-portable-bitmap")
867 (".pdf" .
"application/pdf")
868 (".pgm" .
"image/portable-graymap")
869 (".pict" .
"image/pict")
870 (".png" .
"image/png")
871 (".pnm" .
"image/x-portable-anymap")
872 (".pod" .
"text/plain")
873 (".ppm" .
"image/portable-pixmap")
874 (".ps" .
"application/postscript")
875 (".qt" .
"video/quicktime")
876 (".ras" .
"image/x-raster")
877 (".rgb" .
"image/x-rgb")
878 (".rtf" .
"application/rtf")
879 (".rtx" .
"text/richtext")
880 (".sh" .
"application/x-sh")
881 (".sit" .
"application/x-stuffit")
882 (".siv" .
"application/sieve")
883 (".snd" .
"audio/basic")
884 (".soa" .
"text/dns")
885 (".src" .
"application/x-wais-source")
886 (".tar" .
"archive/tar")
887 (".tcl" .
"application/x-tcl")
888 (".tex" .
"application/x-tex")
889 (".texi" .
"application/texinfo")
890 (".tga" .
"image/x-targa")
891 (".tif" .
"image/tiff")
892 (".tiff" .
"image/tiff")
893 (".tr" .
"application/x-troff")
894 (".troff" .
"application/x-troff")
895 (".tsv" .
"text/tab-separated-values")
896 (".txt" .
"text/plain")
897 (".vbs" .
"video/mpeg")
898 (".vox" .
"audio/basic")
899 (".vrml" .
"x-world/x-vrml")
900 (".wav" .
"audio/x-wav")
901 (".xls" .
"application/vnd.ms-excel")
902 (".wrl" .
"x-world/x-vrml")
903 (".xbm" .
"image/xbm")
904 (".xpm" .
"image/xpm")
905 (".xwd" .
"image/windowdump")
906 (".zip" .
"application/zip")
907 (".ai" .
"application/postscript")
908 (".jpe" .
"image/jpeg")
909 (".jpeg" .
"image/jpeg")
910 (".org" .
"text/x-org"))
911 "An alist of file extensions and corresponding MIME content-types.
912 This exists for you to customize the information in Lisp. It is
913 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
915 (defvar mailcap-mimetypes-parsed-p nil
)
917 (defun mailcap-parse-mimetypes (&optional path force
)
918 "Parse out all the mimetypes specified in a Unix-style path string PATH.
919 Components of PATH are separated by the `path-separator' character
920 appropriate for this system. If PATH is omitted, use the value of
921 environment variable MIMETYPES if set; otherwise use a default path.
922 If FORCE, re-parse even if already parsed."
923 (interactive (list nil t
))
924 (when (or (not mailcap-mimetypes-parsed-p
)
928 ((getenv "MIMETYPES") (setq path
(getenv "MIMETYPES")))
929 ((memq system-type mailcap-poor-system-types
)
930 (setq path
'("~/mime.typ" "~/etc/mime.typ")))
932 ;; mime.types seems to be the normal name, definitely so
933 ;; on current GNUish systems. The search order follows
937 "/usr/etc/mime.types"
938 "/usr/local/etc/mime.types"
939 "/usr/local/www/conf/mime.types"
942 "/usr/etc/mime-types"
943 "/usr/local/etc/mime-types"
944 "/usr/local/www/conf/mime-types"))))
945 (let ((fnames (reverse (if (stringp path
)
946 (split-string path path-separator t
)
950 (setq fname
(car fnames
))
951 (if (and (file-readable-p fname
))
952 (mailcap-parse-mimetype-file fname
))
953 (setq fnames
(cdr fnames
))))
954 (setq mailcap-mimetypes-parsed-p t
)))
956 (defun mailcap-parse-mimetype-file (fname)
957 "Parse out a mime-types file FNAME."
958 (let (type ; The MIME type for this line
959 extns
; The extensions for this line
960 save-pos
; Misc. saved buffer positions
963 (insert-file-contents fname
)
964 (mailcap-replace-regexp "#.*" "")
965 (mailcap-replace-regexp "\n+" "\n")
966 (mailcap-replace-regexp "[ \t]+$" "")
967 (goto-char (point-max))
968 (skip-chars-backward " \t\n")
969 (delete-region (point) (point-max))
970 (goto-char (point-min))
972 (skip-chars-forward " \t\n")
973 (setq save-pos
(point))
974 (skip-chars-forward "^ \t\n")
975 (downcase-region save-pos
(point))
976 (setq type
(buffer-substring save-pos
(point)))
978 (skip-chars-forward " \t")
979 (setq save-pos
(point))
980 (skip-chars-forward "^ \t\n")
981 (setq extns
(cons (buffer-substring save-pos
(point)) extns
)))
983 (setq mailcap-mime-extensions
985 (cons (if (= (string-to-char (car extns
)) ?.
)
987 (concat "." (car extns
))) type
)
988 mailcap-mime-extensions
)
989 extns
(cdr extns
)))))))
991 (defun mailcap-extension-to-mime (extn)
992 "Return the MIME content type of the file extensions EXTN."
993 (mailcap-parse-mimetypes)
994 (if (and (stringp extn
)
995 (not (eq (string-to-char extn
) ?.
)))
996 (setq extn
(concat "." extn
)))
997 (cdr (assoc (downcase extn
) mailcap-mime-extensions
)))
1000 (defalias 'mailcap-command-p
'executable-find
)
1002 (defun mailcap-mime-types ()
1003 "Return a list of MIME media types."
1004 (mailcap-parse-mimetypes)
1005 (mailcap-delete-duplicates
1007 (mapcar 'cdr mailcap-mime-extensions
)
1015 (let ((type (cdr (assq 'type
(cdr m
)))))
1016 (if (equal (cadr (split-string type
"/"))
1021 mailcap-mime-data
)))))
1024 ;;; Useful supplementary functions
1027 (defun mailcap-file-default-commands (files)
1028 "Return a list of default commands for FILES."
1029 (mailcap-parse-mailcaps)
1030 (mailcap-parse-mimetypes)
1031 (let* ((all-mime-type
1032 ;; All unique MIME types from file extensions
1033 (mailcap-delete-duplicates
1034 (mapcar (lambda (file)
1035 (mailcap-extension-to-mime
1036 (file-name-extension file t
)))
1039 ;; All MIME info lists
1040 (mailcap-delete-duplicates
1041 (mapcar (lambda (mime-type)
1042 (mailcap-mime-info mime-type
'all
))
1045 ;; Intersection of mime-infos from different mime-types;
1046 ;; or just the first MIME info for a single MIME type
1047 (if (cdr all-mime-info
)
1048 (delq nil
(mapcar (lambda (mi1)
1049 (unless (memq nil
(mapcar
1052 (cdr all-mime-info
)))
1054 (car all-mime-info
)))
1055 (car all-mime-info
)))
1057 ;; Command strings from `viewer' field of the MIME info
1058 (mailcap-delete-duplicates
1059 (delq nil
(mapcar (lambda (mime-info)
1060 (let ((command (cdr (assoc 'viewer mime-info
))))
1061 (if (stringp command
)
1062 (mailcap-replace-in-string
1063 ;; Replace mailcap's `%s' placeholder
1064 ;; with dired's `?' placeholder
1065 (mailcap-replace-in-string
1066 ;; Remove the final filename placeholder
1067 command
"[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" "" t
)
1069 common-mime-info
)))))
1072 (defun mailcap-view-mime (type)
1073 "View the data in the current buffer that has MIME type TYPE.
1074 `mailcap-mime-data' determines the method to use."
1075 (let ((method (mailcap-mime-info type
)))
1076 (if (stringp method
)
1077 (shell-command-on-region (point-min) (point-max)
1078 ;; Use stdin as the "%s".
1086 ;;; mailcap.el ends here