1 ;;; mailcap.el --- MIME media types configuration
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: William M. Perry <wmperry@aventail.com>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Keywords: news, mail, multimedia
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Provides configuration of MIME media types from directly from Lisp
30 ;; and via the usual mailcap mechanism (RFC 1524). Deals with
31 ;; mime.types similarly.
35 (eval-when-compile (require 'cl))
40 "Definition of viewers for MIME types."
44 (defvar mailcap-parse-args-syntax-table
45 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
46 (modify-syntax-entry ?' "\"" table)
47 (modify-syntax-entry ?` "\"" table)
48 (modify-syntax-entry ?{ "(" table)
49 (modify-syntax-entry ?} ")" table)
51 "A syntax table for parsing SGML attributes.")
54 (when (featurep 'xemacs)
59 (defvar mailcap-print-command
61 (cons (if (boundp 'lpr-command)
64 (when (boundp 'lpr-switches)
65 (if (stringp lpr-switches)
69 "Shell command (including switches) used to print Postscript files.")
71 ;; Postpone using defcustom for this as it's so big and we essentially
72 ;; have to have two copies of the data around then. Perhaps just
73 ;; customize the Lisp viewers and rely on the normal configuration
74 ;; files for the rest? -- fx
75 (defvar mailcap-mime-data
78 (viewer . "gnumeric %s")
79 (test . (getenv "DISPLAY"))
80 (type . "application/vnd.ms-excel"))
82 (viewer . ssl-view-site-cert)
83 (test . (fboundp 'ssl-view-site-cert))
84 (type . "application/x-x509-ca-cert"))
86 (viewer . ssl-view-user-cert)
87 (test . (fboundp 'ssl-view-user-cert))
88 (type . "application/x-x509-user-cert"))
90 (viewer . mailcap-save-binary-file)
92 (type . "application/octet-stream"))
94 (viewer . "xdvi -safer %s")
95 (test . (eq window-system 'x))
97 (type . "application/dvi")
98 ("print" . "dvips -qRP %s"))
100 (viewer . "dvitty %s")
101 (test . (not (getenv "DISPLAY")))
102 (type . "application/dvi")
103 ("print" . "dvips -qRP %s"))
105 (viewer . mailcap-maybe-eval)
106 (type . "application/emacs-lisp"))
108 (viewer . mailcap-maybe-eval)
109 (type . "application/x-emacs-lisp"))
111 (viewer . mailcap-save-binary-file)
113 (type . "application/x-tar"))
116 (test . (fboundp 'tex-mode))
117 (type . "application/x-latex"))
120 (test . (fboundp 'tex-mode))
121 (type . "application/x-tex"))
124 (test . (fboundp 'tex-mode))
125 (type . "application/latex"))
128 (test . (fboundp 'tex-mode))
129 (type . "application/tex"))
131 (viewer . texinfo-mode)
132 (test . (fboundp 'texinfo-mode))
133 (type . "application/tex"))
135 (viewer . mailcap-save-binary-file)
137 (type . "application/zip")
140 (viewer . "gv -safer %s")
141 (type . "application/pdf")
142 (test . window-system)
143 ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command)))
146 (type . "application/pdf")
147 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
148 (test . (eq window-system 'x)))
151 (type . "application/pdf")
152 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
153 (test . (eq window-system 'x)))
155 (viewer . ,(concat "pdftotext %s -"))
156 (type . "application/pdf")
157 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
160 (viewer . "gv -safer %s")
161 (type . "application/postscript")
162 (test . window-system)
163 ("print" . ,(concat mailcap-print-command " %s"))
166 (viewer . "ghostview -dSAFER %s")
167 (type . "application/postscript")
168 (test . (eq window-system 'x))
169 ("print" . ,(concat mailcap-print-command " %s"))
172 (viewer . "ps2ascii %s")
173 (type . "application/postscript")
174 (test . (not (getenv "DISPLAY")))
175 ("print" . ,(concat mailcap-print-command " %s"))
178 (viewer . sieve-mode)
179 (test . (fboundp 'sieve-mode))
180 (type . "application/sieve"))
182 (viewer . "gpg --import --interactive --verbose")
183 (type . "application/pgp-keys")
187 (viewer . "maplay %s")
188 (type . "audio/x-mpeg"))
190 (viewer . "showaudio")
194 (viewer . mm-view-message)
195 (test . (and (featurep 'gnus)
197 (type . "message/rfc822"))
200 (test . (fboundp 'vm-mode))
201 (type . "message/rfc822"))
204 (test . (fboundp 'w3-mode))
205 (type . "message/rfc822"))
208 (type . "message/rfc822")))
211 (viewer . "xwud -in %s")
212 (type . "image/x-xwd")
213 ("compose" . "xwd -frame > %s")
214 (test . (eq window-system 'x))
217 (viewer . "xwud -in %s")
218 (type . "image/x-xwd")
219 ("compose" . "xwd -frame > %s")
220 (test . (eq window-system 'x))
223 (viewer . "xwud -in %s")
224 (type . "image/x-xwd")
225 ("compose" . "xwd -frame > %s")
226 (test . (eq window-system 'x))
229 (viewer . "display %s")
231 (test . (eq window-system 'x))
236 (test . (eq window-system 'x))
241 (test . (fboundp 'w3-mode))
242 (type . "text/plain"))
245 (test . (fboundp 'view-mode))
246 (type . "text/plain"))
248 (viewer . fundamental-mode)
249 (type . "text/plain"))
251 (viewer . enriched-decode)
252 (test . (fboundp 'enriched-decode))
253 (type . "text/enriched"))
255 (viewer . mm-w3-prepare-buffer)
256 (test . (fboundp 'w3-prepare-buffer))
257 (type . "text/html"))
260 (test . (fboundp 'dns-mode))
261 (type . "text/dns")))
264 (viewer . "mpeg_play %s")
265 (type . "video/mpeg")
266 (test . (eq window-system 'x))
270 (viewer . "webspace -remote %s -URL %u")
271 (type . "x-world/x-vrml")
277 (type . "archive/tar")
278 (test . (fboundp 'tar-mode)))))
279 "The mailcap structure is an assoc list of assoc lists.
280 1st assoc list is keyed on the major content-type
281 2nd assoc list is keyed on the minor content-type (which can be a regexp)
286 (\"postscript\" . <info>))
288 (\"plain\" . <info>)))
290 Where <info> is another assoc list of the various information
291 related to the mailcap RFC 1524. This is keyed on the lowercase
292 attribute name (viewer, test, etc). This looks like:
293 ((viewer . VIEWERINFO)
298 Where VIEWERINFO specifies how the content-type is viewed. Can be
299 a string, in which case it is run through a shell, with
300 appropriate parameters, or a symbol, in which case the symbol is
301 `funcall'ed, with the buffer as an argument.
303 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
304 means the viewer is always valid. If it is a Lisp function, it is
305 called with a list of items from any extra fields from the
306 Content-Type header as argument to return a boolean value for the
307 validity. Otherwise, if it is a non-function Lisp symbol or list
308 whose car is a symbol, it is `eval'led to yield the validity. If it
309 is a string or list of strings, it represents a shell command to run
310 to return a true or false shell value for the validity.")
311 (put 'mailcap-mime-data 'risky-local-variable t)
313 (defcustom mailcap-download-directory nil
314 "*Directory to which `mailcap-save-binary-file' downloads files by default.
315 nil means your home directory."
316 :type '(choice (const :tag "Home directory" nil)
320 (defvar mailcap-poor-system-types
321 '(ms-dos ms-windows windows-nt win32 w32 mswindows)
322 "Systems that don't have a Unix-like directory hierarchy.")
325 ;;; Utility functions
328 (defun mailcap-save-binary-file ()
329 (goto-char (point-min))
331 (let ((file (read-file-name
332 "Filename to save as: "
333 (or mailcap-download-directory "~/")))
334 (require-final-newline nil))
335 (write-region (point-min) (point-max) file))
336 (kill-buffer (current-buffer))))
338 (defvar mailcap-maybe-eval-warning
341 This MIME part contains untrusted and possibly harmful content.
342 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
343 things can happen. Please examine the code very carefully before you
344 instruct Emacs to evaluate it. You can browse the buffer containing
345 the code using \\[scroll-other-window].
347 If you are unsure what to do, please answer \"no\"."
348 "Text of warning message displayed by `mailcap-maybe-eval'.
349 Make sure that this text consists only of few text lines. Otherwise,
350 Gnus might fail to display all of it.")
352 (defun mailcap-maybe-eval ()
353 "Maybe evaluate a buffer of Emacs Lisp code."
354 (let ((lisp-buffer (current-buffer)))
355 (goto-char (point-min))
357 (save-window-excursion
358 (delete-other-windows)
359 (let ((buffer (get-buffer-create (generate-new-buffer-name
362 (with-current-buffer buffer
363 (insert (substitute-command-keys
364 mailcap-maybe-eval-warning))
365 (goto-char (point-min))
366 (display-buffer buffer)
367 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
368 (kill-buffer buffer))))
369 (eval-buffer (current-buffer)))
370 (when (buffer-live-p lisp-buffer)
371 (with-current-buffer lisp-buffer
372 (emacs-lisp-mode)))))
376 ;;; The mailcap parser
379 (defun mailcap-replace-regexp (regexp to-string)
380 ;; Quiet replace-regexp.
381 (goto-char (point-min))
382 (while (re-search-forward regexp nil t)
383 (replace-match to-string t nil)))
385 (defvar mailcap-parsed-p nil)
387 (defun mailcap-parse-mailcaps (&optional path force)
388 "Parse out all the mailcaps specified in a path string PATH.
389 Components of PATH are separated by the `path-separator' character
390 appropriate for this system. If FORCE, re-parse even if already
391 parsed. If PATH is omitted, use the value of environment variable
392 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
393 /usr/local/etc/mailcap."
394 (interactive (list nil t))
395 (when (or (not mailcap-parsed-p)
399 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
400 ((memq system-type mailcap-poor-system-types)
401 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
403 ;; This is per RFC 1524, specifically
404 ;; with /usr before /usr/local.
405 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
406 "/usr/local/etc/mailcap"))))
407 (let ((fnames (reverse
409 (delete "" (split-string path path-separator))
413 (setq fname (car fnames))
414 (if (and (file-readable-p fname)
415 (file-regular-p fname))
416 (mailcap-parse-mailcap fname))
417 (setq fnames (cdr fnames))))
418 (setq mailcap-parsed-p t)))
420 (defun mailcap-parse-mailcap (fname)
421 "Parse out the mailcap file specified by FNAME."
422 (let (major ; The major mime type (image/audio/etc)
423 minor ; The minor mime type (gif, basic, etc)
424 save-pos ; Misc saved positions used in parsing
425 viewer ; How to view this mime type
426 info ; Misc info about this mime type
429 (insert-file-contents fname)
430 (set-syntax-table mailcap-parse-args-syntax-table)
431 (mailcap-replace-regexp "#.*" "") ; Remove all comments
432 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
433 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
434 (goto-char (point-max))
435 (skip-chars-backward " \t\n")
436 (delete-region (point) (point-max))
438 (skip-chars-backward " \t\n")
440 (setq save-pos (point)
442 (skip-chars-forward "^/; \t\n")
443 (downcase-region save-pos (point))
444 (setq major (buffer-substring save-pos (point)))
445 (skip-chars-forward " \t")
447 (when (eq (char-after) ?/)
449 (skip-chars-forward " \t")
450 (setq save-pos (point))
451 (skip-chars-forward "^; \t\n")
452 (downcase-region save-pos (point))
455 ((eq ?* (or (char-after save-pos) 0)) ".*")
456 ((= (point) save-pos) ".*")
457 (t (regexp-quote (buffer-substring save-pos (point)))))))
458 (skip-chars-forward " \t")
459 ;;; Got the major/minor chunks, now for the viewers/etc
460 ;;; The first item _must_ be a viewer, according to the
461 ;;; RFC for mailcap files (#1524)
463 (when (eq (char-after) ?\;)
465 (skip-chars-forward " \t")
466 (setq save-pos (point))
467 (skip-chars-forward "^;\n")
469 (while (eq (char-before) ?\\)
470 (backward-delete-char 1)
472 (skip-chars-forward "^;\n"))
473 (if (eq (or (char-after save-pos) 0) ?')
475 (narrow-to-region (1+ save-pos) (point))
476 (goto-char (point-min))
478 (read (current-buffer))
479 (goto-char (point-max))
481 (setq viewer (buffer-substring save-pos (point)))))
482 (setq save-pos (point))
484 (unless (equal viewer "")
485 (setq info (nconc (list (cons 'viewer viewer)
486 (cons 'type (concat major "/"
487 (if (string= minor ".*")
489 (mailcap-parse-mailcap-extras save-pos (point))))
490 (mailcap-mailcap-entry-passes-test info)
491 (mailcap-add-mailcap-entry major minor info))
492 (beginning-of-line)))))
494 (defun mailcap-parse-mailcap-extras (st nd)
495 "Grab all the extra stuff from a mailcap entry."
499 results ; Assoc list of results
500 name-pos ; Start of XXXX= position
501 val-pos ; Start of value position
502 done ; Found end of \'d ;s?
505 (narrow-to-region st nd)
506 (goto-char (point-min))
507 (skip-chars-forward " \n\t;")
510 (setq name-pos (point))
511 (skip-chars-forward "^ \n\t=;")
512 (downcase-region name-pos (point))
513 (setq name (buffer-substring name-pos (point)))
514 (skip-chars-forward " \t\n")
515 (if (not (eq (char-after (point)) ?=)) ; There is no value
517 (skip-chars-forward " \t\n=")
518 (setq val-pos (point))
519 (if (memq (char-after val-pos) '(?\" ?'))
521 (setq val-pos (1+ val-pos))
526 (error (goto-char (point-max)))))
528 (skip-chars-forward "^;")
529 (if (eq (char-after (1- (point))) ?\\ )
531 (subst-char-in-region (1- (point)) (point) ?\\ ? )
532 (skip-chars-forward ";"))
534 (setq value (buffer-substring val-pos (point))))
535 (setq results (cons (cons name value) results))
536 (skip-chars-forward " \";\n\t"))
539 (defun mailcap-mailcap-entry-passes-test (info)
540 "Return non-nil iff mailcap entry INFO passes its test clause.
541 Also return non-nil if no test clause is present."
542 (let ((test (assq 'test info)) ; The test clause
544 (setq status (and test (split-string (cdr test) " ")))
545 (if (and (or (assoc "needsterm" info)
546 (assoc "needsterminal" info)
547 (assoc "needsx11" info))
548 (not (getenv "DISPLAY")))
551 ((and (equal (nth 0 status) "test")
552 (equal (nth 1 status) "-n")
553 (or (equal (nth 2 status) "$DISPLAY")
554 (equal (nth 2 status) "\"$DISPLAY\"")))
555 (setq status (if (getenv "DISPLAY") t nil)))
556 ((and (equal (nth 0 status) "test")
557 (equal (nth 1 status) "-z")
558 (or (equal (nth 2 status) "$DISPLAY")
559 (equal (nth 2 status) "\"$DISPLAY\"")))
560 (setq status (if (getenv "DISPLAY") nil t)))
563 (and test (listp test) (setcdr test status))))
566 ;;; The action routines.
569 (defun mailcap-possible-viewers (major minor)
570 "Return a list of possible viewers from MAJOR for minor type MINOR."
575 ((equal (car (car major)) minor)
576 (setq exact (cons (cdr (car major)) exact)))
577 ((and minor (string-match (concat "^" (car (car major)) "$") minor))
578 (setq wildcard (cons (cdr (car major)) wildcard))))
579 (setq major (cdr major)))
580 (nconc exact wildcard)))
582 (defun mailcap-unescape-mime-test (test type-info)
583 (let (save-pos save-chr subst)
585 ((symbolp test) test)
586 ((and (listp test) (symbolp (car test))) test)
588 (and (listp test) (stringp (car test))
589 (setq test (mapconcat 'identity test " "))))
592 (goto-char (point-min))
594 (skip-chars-forward "^%")
596 (progn (skip-chars-backward "\\\\")
598 0) ; It is an escaped %
601 (skip-chars-forward "%."))
602 (setq save-pos (point))
603 (skip-chars-forward "%")
604 (setq save-chr (char-after (point)))
606 ;; %s: name of a file for the body data
608 ;; %{<parameter name}: value of parameter in mailcap entry
609 ;; %n: number of sub-parts for multipart content-type
610 ;; %F: a set of content-type/filename pairs for multiparts
612 ((null save-chr) nil)
614 (delete-region save-pos (progn (forward-char 1) (point)))
615 (insert (or (cdr (assq 'type type-info)) "\"\"")))
616 ((memq save-chr '(?M ?n ?F))
617 (delete-region save-pos (progn (forward-char 1) (point)))
621 (skip-chars-forward "^}")
622 (downcase-region (+ 2 save-pos) (point))
623 (setq subst (buffer-substring (+ 2 save-pos) (point)))
624 (delete-region save-pos (1+ (point)))
625 (insert (or (cdr (assoc subst type-info)) "\"\"")))
628 (t (error "Bad value to mailcap-unescape-mime-test: %s" test)))))
630 (defvar mailcap-viewer-test-cache nil)
632 (defun mailcap-viewer-passes-test (viewer-info type-info)
633 "Return non-nil iff viewer specified by VIEWER-INFO passes its test clause.
634 Also return non-nil if it has no test clause. TYPE-INFO is an argument
635 to supply to the test."
636 (let* ((test-info (assq 'test viewer-info))
637 (test (cdr test-info))
639 (viewer (cdr (assoc 'viewer viewer-info)))
640 (default-directory (expand-file-name "~/"))
641 status parsed-test cache result)
642 (if (setq cache (assoc test mailcap-viewer-test-cache))
647 ((not test-info) t) ; No test clause
648 ((not test) nil) ; Already failed test
649 ((eq test t) t) ; Already passed test
650 ((functionp test) ; Lisp function as test
651 (funcall test type-info))
652 ((and (symbolp test) ; Lisp variable as test
655 ((and (listp test) ; List to be eval'd
656 (symbolp (car test)))
659 (setq test (mailcap-unescape-mime-test test type-info)
660 test (list shell-file-name nil nil nil
661 shell-command-switch test)
662 status (apply 'call-process test))
664 (push (list otest result) mailcap-viewer-test-cache)
667 (defun mailcap-add-mailcap-entry (major minor info)
668 (let ((old-major (assoc major mailcap-mime-data)))
669 (if (null old-major) ; New major area
670 (setq mailcap-mime-data
671 (cons (cons major (list (cons minor info)))
673 (let ((cur-minor (assoc minor old-major)))
675 ((or (null cur-minor) ; New minor area, or
676 (assq 'test info)) ; Has a test, insert at beginning
677 (setcdr old-major (cons (cons minor info) (cdr old-major))))
678 ((and (not (assq 'test info)) ; No test info, replace completely
679 (not (assq 'test cur-minor))
680 (equal (assq 'viewer info) ; Keep alternative viewer
681 (assq 'viewer cur-minor)))
682 (setcdr cur-minor info))
684 (setcdr old-major (cons (cons minor info) (cdr old-major))))))
687 (defun mailcap-add (type viewer &optional test)
688 "Add VIEWER as a handler for TYPE.
689 If TEST is not given, it defaults to t."
690 (let ((tl (split-string type "/")))
691 (when (or (not (car tl))
693 (error "%s is not a valid MIME type" type))
694 (mailcap-add-mailcap-entry
697 (test . ,(if test test t))
704 (defun mailcap-viewer-lessp (x y)
705 "Return t iff viewer X is more desirable than viewer Y."
706 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
707 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
708 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
709 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
711 ((and x-wild (not y-wild))
713 ((and (not x-wild) y-wild)
715 ((and (not y-lisp) x-lisp)
719 (defun mailcap-mime-info (string &optional request)
720 "Get the MIME viewer command for STRING, return nil if none found.
721 Expects a complete content-type header line as its argument.
723 Second argument REQUEST specifies what information to return. If it is
724 nil or the empty string, the viewer (second field of the mailcap
725 entry) will be returned. If it is a string, then the mailcap field
726 corresponding to that string will be returned (print, description,
727 whatever). If a number, then all the information for this specific
728 viewer is returned. If `all', then all possible viewers for
729 this type is returned."
731 major ; Major encoding (text, etc)
732 minor ; Minor encoding (html, etc)
734 save-pos ; Misc. position during parse
735 major-info ; (assoc major mailcap-mime-data)
736 minor-info ; (assoc minor major-info)
737 test ; current test proc.
738 viewers ; Possible viewers
739 passed ; Viewers that passed the test
740 viewer ; The one and only viewer
743 (setq ctl (mail-header-parse-content-type (or string "text/plain")))
744 (setq major (split-string (car ctl) "/"))
745 (setq minor (cadr major)
747 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
748 (when (setq viewers (mailcap-possible-viewers major-info minor))
749 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
753 (if (mailcap-viewer-passes-test (car viewers) info)
754 (setq passed (cons (car viewers) passed)))
755 (setq viewers (cdr viewers)))
756 (setq passed (sort passed 'mailcap-viewer-lessp))
757 (setq viewer (car passed))))
758 (when (and (stringp (cdr (assq 'viewer viewer)))
760 (setq viewer (car passed)))
762 ((and (null viewer) (not (equal major "default")) request)
763 (mailcap-mime-info "default" request))
764 ((or (null request) (equal request ""))
765 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
767 (mailcap-unescape-mime-test
768 (cdr-safe (assoc request viewer)) info))
772 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
773 (setq viewer (copy-sequence viewer))
774 (let ((view (assq 'viewer viewer))
775 (test (assq 'test viewer)))
776 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
777 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
781 ;;; Experimental MIME-types parsing
784 (defvar mailcap-mime-extensions
785 '(("" . "text/plain")
786 (".abs" . "audio/x-mpeg")
787 (".aif" . "audio/aiff")
788 (".aifc" . "audio/aiff")
789 (".aiff" . "audio/aiff")
790 (".ano" . "application/x-annotator")
791 (".au" . "audio/ulaw")
792 (".avi" . "video/x-msvideo")
793 (".bcpio" . "application/x-bcpio")
794 (".bin" . "application/octet-stream")
795 (".cdf" . "application/x-netcdr")
796 (".cpio" . "application/x-cpio")
797 (".csh" . "application/x-csh")
798 (".css" . "text/css")
799 (".dvi" . "application/x-dvi")
800 (".diff" . "text/x-patch")
801 (".el" . "application/emacs-lisp")
802 (".eps" . "application/postscript")
803 (".etx" . "text/x-setext")
804 (".exe" . "application/octet-stream")
805 (".fax" . "image/x-fax")
806 (".gif" . "image/gif")
807 (".hdf" . "application/x-hdf")
808 (".hqx" . "application/mac-binhex40")
809 (".htm" . "text/html")
810 (".html" . "text/html")
811 (".icon" . "image/x-icon")
812 (".ief" . "image/ief")
813 (".jpg" . "image/jpeg")
814 (".macp" . "image/x-macpaint")
815 (".man" . "application/x-troff-man")
816 (".me" . "application/x-troff-me")
817 (".mif" . "application/mif")
818 (".mov" . "video/quicktime")
819 (".movie" . "video/x-sgi-movie")
820 (".mp2" . "audio/x-mpeg")
821 (".mp3" . "audio/x-mpeg")
822 (".mp2a" . "audio/x-mpeg2")
823 (".mpa" . "audio/x-mpeg")
824 (".mpa2" . "audio/x-mpeg2")
825 (".mpe" . "video/mpeg")
826 (".mpeg" . "video/mpeg")
827 (".mpega" . "audio/x-mpeg")
828 (".mpegv" . "video/mpeg")
829 (".mpg" . "video/mpeg")
830 (".mpv" . "video/mpeg")
831 (".ms" . "application/x-troff-ms")
832 (".nc" . "application/x-netcdf")
833 (".nc" . "application/x-netcdf")
834 (".oda" . "application/oda")
835 (".patch" . "text/x-patch")
836 (".pbm" . "image/x-portable-bitmap")
837 (".pdf" . "application/pdf")
838 (".pgm" . "image/portable-graymap")
839 (".pict" . "image/pict")
840 (".png" . "image/png")
841 (".pnm" . "image/x-portable-anymap")
842 (".ppm" . "image/portable-pixmap")
843 (".ps" . "application/postscript")
844 (".qt" . "video/quicktime")
845 (".ras" . "image/x-raster")
846 (".rgb" . "image/x-rgb")
847 (".rtf" . "application/rtf")
848 (".rtx" . "text/richtext")
849 (".sh" . "application/x-sh")
850 (".sit" . "application/x-stuffit")
851 (".siv" . "application/sieve")
852 (".snd" . "audio/basic")
853 (".soa" . "text/dns")
854 (".src" . "application/x-wais-source")
855 (".tar" . "archive/tar")
856 (".tcl" . "application/x-tcl")
857 (".tex" . "application/x-tex")
858 (".texi" . "application/texinfo")
859 (".tga" . "image/x-targa")
860 (".tif" . "image/tiff")
861 (".tiff" . "image/tiff")
862 (".tr" . "application/x-troff")
863 (".troff" . "application/x-troff")
864 (".tsv" . "text/tab-separated-values")
865 (".txt" . "text/plain")
866 (".vbs" . "video/mpeg")
867 (".vox" . "audio/basic")
868 (".vrml" . "x-world/x-vrml")
869 (".wav" . "audio/x-wav")
870 (".xls" . "application/vnd.ms-excel")
871 (".wrl" . "x-world/x-vrml")
872 (".xbm" . "image/xbm")
873 (".xpm" . "image/xpm")
874 (".xwd" . "image/windowdump")
875 (".zip" . "application/zip")
876 (".ai" . "application/postscript")
877 (".jpe" . "image/jpeg")
878 (".jpeg" . "image/jpeg"))
879 "An alist of file extensions and corresponding MIME content-types.
880 This exists for you to customize the information in Lisp. It is
881 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
883 (defvar mailcap-mimetypes-parsed-p nil)
885 (defun mailcap-parse-mimetypes (&optional path force)
886 "Parse out all the mimetypes specified in a Unix-style path string PATH.
887 Components of PATH are separated by the `path-separator' character
888 appropriate for this system. If PATH is omitted, use the value of
889 environment variable MIMETYPES if set; otherwise use a default path.
890 If FORCE, re-parse even if already parsed."
891 (interactive (list nil t))
892 (when (or (not mailcap-mimetypes-parsed-p)
896 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
897 ((memq system-type mailcap-poor-system-types)
898 (setq path '("~/mime.typ" "~/etc/mime.typ")))
900 ;; mime.types seems to be the normal name, definitely so
901 ;; on current GNUish systems. The search order follows
905 "/usr/etc/mime.types"
906 "/usr/local/etc/mime.types"
907 "/usr/local/www/conf/mime.types"
910 "/usr/etc/mime-types"
911 "/usr/local/etc/mime-types"
912 "/usr/local/www/conf/mime-types"))))
913 (let ((fnames (reverse (if (stringp path)
914 (delete "" (split-string path path-separator))
918 (setq fname (car fnames))
919 (if (and (file-readable-p fname))
920 (mailcap-parse-mimetype-file fname))
921 (setq fnames (cdr fnames))))
922 (setq mailcap-mimetypes-parsed-p t)))
924 (defun mailcap-parse-mimetype-file (fname)
925 "Parse out a mime-types file FNAME."
926 (let (type ; The MIME type for this line
927 extns ; The extensions for this line
928 save-pos ; Misc. saved buffer positions
931 (insert-file-contents fname)
932 (mailcap-replace-regexp "#.*" "")
933 (mailcap-replace-regexp "\n+" "\n")
934 (mailcap-replace-regexp "[ \t]+$" "")
935 (goto-char (point-max))
936 (skip-chars-backward " \t\n")
937 (delete-region (point) (point-max))
938 (goto-char (point-min))
940 (skip-chars-forward " \t\n")
941 (setq save-pos (point))
942 (skip-chars-forward "^ \t\n")
943 (downcase-region save-pos (point))
944 (setq type (buffer-substring save-pos (point)))
946 (skip-chars-forward " \t")
947 (setq save-pos (point))
948 (skip-chars-forward "^ \t\n")
949 (setq extns (cons (buffer-substring save-pos (point)) extns)))
951 (setq mailcap-mime-extensions
953 (cons (if (= (string-to-char (car extns)) ?.)
955 (concat "." (car extns))) type)
956 mailcap-mime-extensions)
957 extns (cdr extns)))))))
959 (defun mailcap-extension-to-mime (extn)
960 "Return the MIME content type of the file extensions EXTN."
961 (mailcap-parse-mimetypes)
962 (if (and (stringp extn)
963 (not (eq (string-to-char extn) ?.)))
964 (setq extn (concat "." extn)))
965 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
968 (defalias 'mailcap-command-p 'executable-find)
970 (defun mailcap-mime-types ()
971 "Return a list of MIME media types."
972 (mailcap-parse-mimetypes)
973 (mm-delete-duplicates
975 (mapcar 'cdr mailcap-mime-extensions)
983 (let ((type (cdr (assq 'type (cdr m)))))
984 (if (equal (cadr (split-string type "/"))
989 mailcap-mime-data)))))
991 (provide 'gnus-mailcap)
993 ;;; mailcap.el ends here