1 ;;; mailcap.el --- MIME media types configuration
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: William M. Perry <wmperry@aventail.com>
5 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news, mail, multimedia
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Provides configuration of MIME media types from directly from Lisp
28 ;; and via the usual mailcap mechanism (RFC 1524). Deals with
29 ;; mime.types similarly.
33 (eval-when-compile (require 'cl))
38 "Definition of viewers for MIME types."
42 (defvar mailcap-parse-args-syntax-table
43 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
44 (modify-syntax-entry ?' "\"" table)
45 (modify-syntax-entry ?` "\"" table)
46 (modify-syntax-entry ?{ "(" table)
47 (modify-syntax-entry ?} ")" table)
49 "A syntax table for parsing sgml attributes.")
51 ;; Postpone using defcustom for this as it's so big and we essentially
52 ;; have to have two copies of the data around then. Perhaps just
53 ;; customize the Lisp viewers and rely on the normal configuration
54 ;; files for the rest? -- fx
55 (defvar mailcap-mime-data
58 (viewer . "gnumeric %s")
59 (test . (getenv "DISPLAY"))
60 (type . "application/vnd.ms-excel"))
62 (viewer . ssl-view-site-cert)
63 (test . (fboundp 'ssl-view-site-cert))
64 (type . "application/x-x509-ca-cert"))
66 (viewer . ssl-view-user-cert)
67 (test . (fboundp 'ssl-view-user-cert))
68 (type . "application/x-x509-user-cert"))
70 (viewer . mailcap-save-binary-file)
72 (type . "application/octet-stream"))
73 ;;; XEmacs says `ns' device-type not implemented.
75 ;; (viewer . "open %s")
76 ;; (type . "application/dvi")
77 ;; (test . (eq (mm-device-type) 'ns)))
80 (test . (eq (mm-device-type) 'x))
82 (type . "application/dvi"))
84 (viewer . "dvitty %s")
85 (test . (not (getenv "DISPLAY")))
86 (type . "application/dvi"))
88 (viewer . mailcap-maybe-eval)
89 (type . "application/emacs-lisp"))
91 (viewer . mailcap-save-binary-file)
93 (type . "application/x-tar"))
96 (test . (fboundp 'tex-mode))
97 (type . "application/x-latex"))
100 (test . (fboundp 'tex-mode))
101 (type . "application/x-tex"))
104 (test . (fboundp 'tex-mode))
105 (type . "application/latex"))
108 (test . (fboundp 'tex-mode))
109 (type . "application/tex"))
111 (viewer . texinfo-mode)
112 (test . (fboundp 'texinfo-mode))
113 (type . "application/tex"))
115 (viewer . mailcap-save-binary-file)
117 (type . "application/zip")
119 ;; Prefer free viewers.
122 (type . "application/pdf")
123 (test . window-system))
126 (type . "application/pdf")
127 (test . (eq (mm-device-type) 'x)))
129 (viewer . "acroread %s")
130 (type . "application/pdf"))
131 ;;; XEmacs says `ns' device-type not implemented.
133 ;; (viewer . "open %s")
134 ;; (type . "application/postscript")
135 ;; (test . (eq (mm-device-type) 'ns)))
137 (viewer . "gv -safer %s")
138 (type . "application/postscript")
139 (test . window-system)
142 (viewer . "ghostview -dSAFER %s")
143 (type . "application/postscript")
144 (test . (eq (mm-device-type) 'x))
147 (viewer . "ps2ascii %s")
148 (type . "application/postscript")
149 (test . (not (getenv "DISPLAY")))
152 (viewer . sieve-mode)
153 (test . (fboundp 'sieve-mode))
154 (type . "application/sieve")))
157 (viewer . "maplay %s")
158 (type . "audio/x-mpeg"))
160 (viewer . "showaudio")
164 (viewer . mm-view-message)
165 (test . (and (featurep 'gnus)
167 (type . "message/rfc822"))
170 (test . (fboundp 'vm-mode))
171 (type . "message/rfc822"))
174 (test . (fboundp 'w3-mode))
175 (type . "message/rfc822"))
178 (type . "message/rfc822")))
181 (viewer . "xwud -in %s")
182 (type . "image/x-xwd")
183 ("compose" . "xwd -frame > %s")
184 (test . (eq (mm-device-type) 'x))
187 (viewer . "xwud -in %s")
188 (type . "image/x-xwd")
189 ("compose" . "xwd -frame > %s")
190 (test . (eq (mm-device-type) 'x))
193 (viewer . "xwud -in %s")
194 (type . "image/x-xwd")
195 ("compose" . "xwd -frame > %s")
196 (test . (eq (mm-device-type) 'x))
198 ;;; XEmacs says `ns' device-type not implemented.
200 ;; (viewer . "aopen %s")
201 ;; (type . "image/*")
202 ;; (test . (eq (mm-device-type) 'ns)))
204 (viewer . "display %s")
206 (test . (eq (mm-device-type) 'x))
211 (test . (eq (mm-device-type) 'x))
216 (test . (fboundp 'w3-mode))
217 (type . "text/plain"))
220 (test . (fboundp 'view-mode))
221 (type . "text/plain"))
223 (viewer . fundamental-mode)
224 (type . "text/plain"))
226 (viewer . enriched-decode-region)
227 (test . (fboundp 'enriched-decode))
228 (type . "text/enriched"))
230 (viewer . mm-w3-prepare-buffer)
231 (test . (fboundp 'w3-prepare-buffer))
232 (type . "text/html")))
235 (viewer . "mpeg_play %s")
236 (type . "video/mpeg")
237 (test . (eq (mm-device-type) 'x))
241 (viewer . "webspace -remote %s -URL %u")
242 (type . "x-world/x-vrml")
248 (type . "archive/tar")
249 (test . (fboundp 'tar-mode)))))
250 "The mailcap structure is an assoc list of assoc lists.
251 1st assoc list is keyed on the major content-type
252 2nd assoc list is keyed on the minor content-type (which can be a regexp)
257 (\"postscript\" . <info>))
259 (\"plain\" . <info>)))
261 Where <info> is another assoc list of the various information
262 related to the mailcap RFC 1524. This is keyed on the lowercase
263 attribute name (viewer, test, etc). This looks like:
264 ((viewer . VIEWERINFO)
269 Where VIEWERINFO specifies how the content-type is viewed. Can be
270 a string, in which case it is run through a shell, with
271 appropriate parameters, or a symbol, in which case the symbol is
272 `funcall'ed, with the buffer as an argument.
274 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
275 means the viewer is always valid. If it is a Lisp function, it is
276 called with a list of items from any extra fields from the
277 Content-Type header as argument to return a boolean value for the
278 validity. Otherwise, if it is a non-function Lisp symbol or list
279 whose car is a symbol, it is `eval'led to yield the validity. If it
280 is a string or list of strings, it represents a shell command to run
281 to return a true or false shell value for the validity.")
283 (defcustom mailcap-download-directory nil
284 "*Directory to which `mailcap-save-binary-file' downloads files by default.
285 Nil means your home directory."
286 :type '(choice (const :tag "Home directory" nil)
290 (defvar mailcap-poor-system-types
291 '(ms-dos ms-windows windows-nt win32 w32 mswindows)
292 "Systems that don't have a Unix-like directory hierarchy.")
295 ;;; Utility functions
298 (defun mailcap-save-binary-file ()
299 (goto-char (point-min))
301 (let ((file (read-file-name
302 "Filename to save as: "
303 (or mailcap-download-directory "~/")))
304 (require-final-newline nil))
305 (write-region (point-min) (point-max) file))
306 (kill-buffer (current-buffer))))
308 (defvar mailcap-maybe-eval-warning
311 This MIME part contains untrusted and possibly harmful content.
312 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
313 things can happen. Please examine the code very carefully before you
314 instruct Emacs to evaluate it. You can browse the buffer containing
315 the code using \\[scroll-other-window].
317 If you are unsure what to do, please answer \"no\"."
318 "Text of warning message displayed by `mailcap-maybe-eval'.
319 Make sure that this text consists only of few text lines. Otherwise,
320 Gnus might fail to display all of it.")
322 (defun mailcap-maybe-eval ()
323 "Maybe evaluate a buffer of Emacs Lisp code."
324 (let ((lisp-buffer (current-buffer)))
325 (goto-char (point-min))
327 (save-window-excursion
328 (delete-other-windows)
329 (let ((buffer (get-buffer-create (generate-new-buffer-name
332 (with-current-buffer buffer
333 (insert (substitute-command-keys
334 mailcap-maybe-eval-warning))
335 (goto-char (point-min))
336 (display-buffer buffer)
337 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
338 (kill-buffer buffer))))
339 (eval-buffer (current-buffer)))
340 (when (buffer-live-p lisp-buffer)
341 (with-current-buffer lisp-buffer
342 (emacs-lisp-mode)))))
346 ;;; The mailcap parser
349 (defun mailcap-replace-regexp (regexp to-string)
350 ;; Quiet replace-regexp.
351 (goto-char (point-min))
352 (while (re-search-forward regexp nil t)
353 (replace-match to-string t nil)))
355 (defvar mailcap-parsed-p nil)
357 (defun mailcap-parse-mailcaps (&optional path force)
358 "Parse out all the mailcaps specified in a path string PATH.
359 Components of PATH are separated by the `path-separator' character
360 appropriate for this system. If FORCE, re-parse even if already
361 parsed. If PATH is omitted, use the value of environment variable
362 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
363 /usr/local/etc/mailcap."
364 (interactive (list nil t))
365 (when (or (not mailcap-parsed-p)
369 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
370 ((memq system-type mailcap-poor-system-types)
371 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
373 ;; This is per RFC 1524, specifically
374 ;; with /usr before /usr/local.
375 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
376 "/usr/local/etc/mailcap"))))
377 (let ((fnames (reverse
379 (delete "" (split-string path path-separator))
383 (setq fname (car fnames))
384 (if (and (file-readable-p fname)
385 (file-regular-p fname))
386 (mailcap-parse-mailcap fname))
387 (setq fnames (cdr fnames))))
388 (setq mailcap-parsed-p t)))
390 (defun mailcap-parse-mailcap (fname)
391 "Parse out the mailcap file specified by FNAME."
392 (let (major ; The major mime type (image/audio/etc)
393 minor ; The minor mime type (gif, basic, etc)
394 save-pos ; Misc saved positions used in parsing
395 viewer ; How to view this mime type
396 info ; Misc info about this mime type
399 (insert-file-contents fname)
400 (set-syntax-table mailcap-parse-args-syntax-table)
401 (mailcap-replace-regexp "#.*" "") ; Remove all comments
402 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
403 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
404 (goto-char (point-max))
405 (skip-chars-backward " \t\n")
406 (delete-region (point) (point-max))
408 (skip-chars-backward " \t\n")
410 (setq save-pos (point)
412 (skip-chars-forward "^/; \t\n")
413 (downcase-region save-pos (point))
414 (setq major (buffer-substring save-pos (point)))
415 (skip-chars-forward " \t")
417 (when (eq (char-after) ?/)
419 (skip-chars-forward " \t")
420 (setq save-pos (point))
421 (skip-chars-forward "^; \t\n")
422 (downcase-region save-pos (point))
425 ((eq ?* (or (char-after save-pos) 0)) ".*")
426 ((= (point) save-pos) ".*")
427 (t (regexp-quote (buffer-substring save-pos (point)))))))
428 (skip-chars-forward " \t")
429 ;;; Got the major/minor chunks, now for the viewers/etc
430 ;;; The first item _must_ be a viewer, according to the
431 ;;; RFC for mailcap files (#1524)
433 (when (eq (char-after) ?\;)
435 (skip-chars-forward " \t")
436 (setq save-pos (point))
437 (skip-chars-forward "^;\n")
439 (while (eq (char-before) ?\\)
440 (backward-delete-char 1)
442 (skip-chars-forward "^;\n"))
443 (if (eq (or (char-after save-pos) 0) ?')
445 (narrow-to-region (1+ save-pos) (point))
446 (goto-char (point-min))
448 (read (current-buffer))
449 (goto-char (point-max))
451 (setq viewer (buffer-substring save-pos (point)))))
452 (setq save-pos (point))
454 (unless (equal viewer "")
455 (setq info (nconc (list (cons 'viewer viewer)
456 (cons 'type (concat major "/"
457 (if (string= minor ".*")
459 (mailcap-parse-mailcap-extras save-pos (point))))
460 (mailcap-mailcap-entry-passes-test info)
461 (mailcap-add-mailcap-entry major minor info))
462 (beginning-of-line)))))
464 (defun mailcap-parse-mailcap-extras (st nd)
465 "Grab all the extra stuff from a mailcap entry."
469 results ; Assoc list of results
470 name-pos ; Start of XXXX= position
471 val-pos ; Start of value position
472 done ; Found end of \'d ;s?
475 (narrow-to-region st nd)
476 (goto-char (point-min))
477 (skip-chars-forward " \n\t;")
480 (setq name-pos (point))
481 (skip-chars-forward "^ \n\t=;")
482 (downcase-region name-pos (point))
483 (setq name (buffer-substring name-pos (point)))
484 (skip-chars-forward " \t\n")
485 (if (not (eq (char-after (point)) ?=)) ; There is no value
487 (skip-chars-forward " \t\n=")
488 (setq val-pos (point))
489 (if (memq (char-after val-pos) '(?\" ?'))
491 (setq val-pos (1+ val-pos))
496 (error (goto-char (point-max)))))
498 (skip-chars-forward "^;")
499 (if (eq (char-after (1- (point))) ?\\ )
501 (subst-char-in-region (1- (point)) (point) ?\\ ? )
502 (skip-chars-forward ";"))
504 (setq value (buffer-substring val-pos (point))))
505 (setq results (cons (cons name value) results))
506 (skip-chars-forward " \";\n\t"))
509 (defun mailcap-mailcap-entry-passes-test (info)
510 "Return non-nil iff mailcap entry INFO passes its test clause.
511 Also return non-nil if no test clause is present."
512 (let ((test (assq 'test info)) ; The test clause
514 (setq status (and test (split-string (cdr test) " ")))
515 (if (and (or (assoc "needsterm" info)
516 (assoc "needsterminal" info)
517 (assoc "needsx11" info))
518 (not (getenv "DISPLAY")))
521 ((and (equal (nth 0 status) "test")
522 (equal (nth 1 status) "-n")
523 (or (equal (nth 2 status) "$DISPLAY")
524 (equal (nth 2 status) "\"$DISPLAY\"")))
525 (setq status (if (getenv "DISPLAY") t nil)))
526 ((and (equal (nth 0 status) "test")
527 (equal (nth 1 status) "-z")
528 (or (equal (nth 2 status) "$DISPLAY")
529 (equal (nth 2 status) "\"$DISPLAY\"")))
530 (setq status (if (getenv "DISPLAY") nil t)))
533 (and test (listp test) (setcdr test status))))
536 ;;; The action routines.
539 (defun mailcap-possible-viewers (major minor)
540 "Return a list of possible viewers from MAJOR for minor type MINOR."
545 ((equal (car (car major)) minor)
546 (setq exact (cons (cdr (car major)) exact)))
547 ((and minor (string-match (concat "^" (car (car major)) "$") minor))
548 (setq wildcard (cons (cdr (car major)) wildcard))))
549 (setq major (cdr major)))
550 (nconc exact wildcard)))
552 (defun mailcap-unescape-mime-test (test type-info)
553 (let (save-pos save-chr subst)
555 ((symbolp test) test)
556 ((and (listp test) (symbolp (car test))) test)
558 (and (listp test) (stringp (car test))
559 (setq test (mapconcat 'identity test " "))))
562 (goto-char (point-min))
564 (skip-chars-forward "^%")
566 (progn (skip-chars-backward "\\\\")
568 0) ; It is an escaped %
571 (skip-chars-forward "%."))
572 (setq save-pos (point))
573 (skip-chars-forward "%")
574 (setq save-chr (char-after (point)))
576 ;; %s: name of a file for the body data
578 ;; %{<parameter name}: value of parameter in mailcap entry
579 ;; %n: number of sub-parts for multipart content-type
580 ;; %F: a set of content-type/filename pairs for multiparts
582 ((null save-chr) nil)
584 (delete-region save-pos (progn (forward-char 1) (point)))
585 (insert (or (cdr (assq 'type type-info)) "\"\"")))
586 ((memq save-chr '(?M ?n ?F))
587 (delete-region save-pos (progn (forward-char 1) (point)))
591 (skip-chars-forward "^}")
592 (downcase-region (+ 2 save-pos) (point))
593 (setq subst (buffer-substring (+ 2 save-pos) (point)))
594 (delete-region save-pos (1+ (point)))
595 (insert (or (cdr (assoc subst type-info)) "\"\"")))
598 (t (error "Bad value to mailcap-unescape-mime-test: %s" test)))))
600 (defvar mailcap-viewer-test-cache nil)
602 (defun mailcap-viewer-passes-test (viewer-info type-info)
603 "Return non-nil iff viewer specified by VIEWER-INFO passes its test clause.
604 Also retun non-nil if it has no test clause. TYPE-INFO is an argument
605 to supply to the test."
606 (let* ((test-info (assq 'test viewer-info))
607 (test (cdr test-info))
609 (viewer (cdr (assoc 'viewer viewer-info)))
610 (default-directory (expand-file-name "~/"))
611 status parsed-test cache result)
612 (if (setq cache (assoc test mailcap-viewer-test-cache))
617 ((not test-info) t) ; No test clause
618 ((not test) nil) ; Already failed test
619 ((eq test t) t) ; Already passed test
620 ((functionp test) ; Lisp function as test
621 (funcall test type-info))
622 ((and (symbolp test) ; Lisp variable as test
625 ((and (listp test) ; List to be eval'd
626 (symbolp (car test)))
629 (setq test (mailcap-unescape-mime-test test type-info)
630 test (list shell-file-name nil nil nil
631 shell-command-switch test)
632 status (apply 'call-process test))
634 (push (list otest result) mailcap-viewer-test-cache)
637 (defun mailcap-add-mailcap-entry (major minor info)
638 (let ((old-major (assoc major mailcap-mime-data)))
639 (if (null old-major) ; New major area
640 (setq mailcap-mime-data
641 (cons (cons major (list (cons minor info)))
643 (let ((cur-minor (assoc minor old-major)))
645 ((or (null cur-minor) ; New minor area, or
646 (assq 'test info)) ; Has a test, insert at beginning
647 (setcdr old-major (cons (cons minor info) (cdr old-major))))
648 ((and (not (assq 'test info)) ; No test info, replace completely
649 (not (assq 'test cur-minor))
650 (equal (assq 'viewer info) ; Keep alternative viewer
651 (assq 'viewer cur-minor)))
652 (setcdr cur-minor info))
654 (setcdr old-major (cons (cons minor info) (cdr old-major))))))
657 (defun mailcap-add (type viewer &optional test)
658 "Add VIEWER as a handler for TYPE.
659 If TEST is not given, it defaults to t."
660 (let ((tl (split-string type "/")))
661 (when (or (not (car tl))
663 (error "%s is not a valid MIME type" type))
664 (mailcap-add-mailcap-entry
667 (test . ,(if test test t))
674 (defun mailcap-viewer-lessp (x y)
675 "Return t iff viewer X is more desirable than viewer Y."
676 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
677 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
678 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
679 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
681 ((and x-wild (not y-wild))
683 ((and (not x-wild) y-wild)
685 ((and (not y-lisp) x-lisp)
689 (defun mailcap-mime-info (string &optional request)
690 "Get the MIME viewer command for STRING, return nil if none found.
691 Expects a complete content-type header line as its argument.
693 Second argument REQUEST specifies what information to return. If it is
694 nil or the empty string, the viewer (second field of the mailcap
695 entry) will be returned. If it is a string, then the mailcap field
696 corresponding to that string will be returned (print, description,
697 whatever). If a number, then all the information for this specific
698 viewer is returned. If `all', then all possible viewers for
699 this type is returned."
701 major ; Major encoding (text, etc)
702 minor ; Minor encoding (html, etc)
704 save-pos ; Misc. position during parse
705 major-info ; (assoc major mailcap-mime-data)
706 minor-info ; (assoc minor major-info)
707 test ; current test proc.
708 viewers ; Possible viewers
709 passed ; Viewers that passed the test
710 viewer ; The one and only viewer
713 (setq ctl (mail-header-parse-content-type (or string "text/plain")))
714 (setq major (split-string (car ctl) "/"))
715 (setq minor (cadr major)
717 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
718 (when (setq viewers (mailcap-possible-viewers major-info minor))
719 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
723 (if (mailcap-viewer-passes-test (car viewers) info)
724 (setq passed (cons (car viewers) passed)))
725 (setq viewers (cdr viewers)))
726 (setq passed (sort passed 'mailcap-viewer-lessp))
727 (setq viewer (car passed))))
728 (when (and (stringp (cdr (assq 'viewer viewer)))
730 (setq viewer (car passed)))
732 ((and (null viewer) (not (equal major "default")) request)
733 (mailcap-mime-info "default" request))
734 ((or (null request) (equal request ""))
735 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
737 (if (or (eq request 'test) (eq request 'viewer))
738 (mailcap-unescape-mime-test
739 (cdr-safe (assoc request viewer)) info)))
743 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
744 (setq viewer (copy-sequence viewer))
745 (let ((view (assq 'viewer viewer))
746 (test (assq 'test viewer)))
747 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
748 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
752 ;;; Experimental MIME-types parsing
755 (defvar mailcap-mime-extensions
756 '(("" . "text/plain")
757 (".abs" . "audio/x-mpeg")
758 (".aif" . "audio/aiff")
759 (".aifc" . "audio/aiff")
760 (".aiff" . "audio/aiff")
761 (".ano" . "application/x-annotator")
762 (".au" . "audio/ulaw")
763 (".avi" . "video/x-msvideo")
764 (".bcpio" . "application/x-bcpio")
765 (".bin" . "application/octet-stream")
766 (".cdf" . "application/x-netcdr")
767 (".cpio" . "application/x-cpio")
768 (".csh" . "application/x-csh")
769 (".css" . "text/css")
770 (".dvi" . "application/x-dvi")
771 (".diff" . "text/x-patch")
772 (".el" . "application/emacs-lisp")
773 (".eps" . "application/postscript")
774 (".etx" . "text/x-setext")
775 (".exe" . "application/octet-stream")
776 (".fax" . "image/x-fax")
777 (".gif" . "image/gif")
778 (".hdf" . "application/x-hdf")
779 (".hqx" . "application/mac-binhex40")
780 (".htm" . "text/html")
781 (".html" . "text/html")
782 (".icon" . "image/x-icon")
783 (".ief" . "image/ief")
784 (".jpg" . "image/jpeg")
785 (".macp" . "image/x-macpaint")
786 (".man" . "application/x-troff-man")
787 (".me" . "application/x-troff-me")
788 (".mif" . "application/mif")
789 (".mov" . "video/quicktime")
790 (".movie" . "video/x-sgi-movie")
791 (".mp2" . "audio/x-mpeg")
792 (".mp3" . "audio/x-mpeg")
793 (".mp2a" . "audio/x-mpeg2")
794 (".mpa" . "audio/x-mpeg")
795 (".mpa2" . "audio/x-mpeg2")
796 (".mpe" . "video/mpeg")
797 (".mpeg" . "video/mpeg")
798 (".mpega" . "audio/x-mpeg")
799 (".mpegv" . "video/mpeg")
800 (".mpg" . "video/mpeg")
801 (".mpv" . "video/mpeg")
802 (".ms" . "application/x-troff-ms")
803 (".nc" . "application/x-netcdf")
804 (".nc" . "application/x-netcdf")
805 (".oda" . "application/oda")
806 (".patch" . "text/x-patch")
807 (".pbm" . "image/x-portable-bitmap")
808 (".pdf" . "application/pdf")
809 (".pgm" . "image/portable-graymap")
810 (".pict" . "image/pict")
811 (".png" . "image/png")
812 (".pnm" . "image/x-portable-anymap")
813 (".ppm" . "image/portable-pixmap")
814 (".ps" . "application/postscript")
815 (".qt" . "video/quicktime")
816 (".ras" . "image/x-raster")
817 (".rgb" . "image/x-rgb")
818 (".rtf" . "application/rtf")
819 (".rtx" . "text/richtext")
820 (".sh" . "application/x-sh")
821 (".sit" . "application/x-stuffit")
822 (".siv" . "application/sieve")
823 (".snd" . "audio/basic")
824 (".src" . "application/x-wais-source")
825 (".tar" . "archive/tar")
826 (".tcl" . "application/x-tcl")
827 (".tex" . "application/x-tex")
828 (".texi" . "application/texinfo")
829 (".tga" . "image/x-targa")
830 (".tif" . "image/tiff")
831 (".tiff" . "image/tiff")
832 (".tr" . "application/x-troff")
833 (".troff" . "application/x-troff")
834 (".tsv" . "text/tab-separated-values")
835 (".txt" . "text/plain")
836 (".vbs" . "video/mpeg")
837 (".vox" . "audio/basic")
838 (".vrml" . "x-world/x-vrml")
839 (".wav" . "audio/x-wav")
840 (".xls" . "application/vnd.ms-excel")
841 (".wrl" . "x-world/x-vrml")
842 (".xbm" . "image/xbm")
843 (".xpm" . "image/xpm")
844 (".xwd" . "image/windowdump")
845 (".zip" . "application/zip")
846 (".ai" . "application/postscript")
847 (".jpe" . "image/jpeg")
848 (".jpeg" . "image/jpeg"))
849 "An alist of file extensions and corresponding MIME content-types.
850 This exists for you to customize the information in Lisp. It is
851 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
853 (defvar mailcap-mimetypes-parsed-p nil)
855 (defun mailcap-parse-mimetypes (&optional path force)
856 "Parse out all the mimetypes specified in a Unix-style path string PATH.
857 Components of PATH are separated by the `path-separator' character
858 appropriate for this system. If PATH is omitted, use the value of
859 environment variable MIMETYPES if set; otherwise use a default path.
860 If FORCE, re-parse even if already parsed."
861 (interactive (list nil t))
862 (when (or (not mailcap-mimetypes-parsed-p)
866 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
867 ((memq system-type mailcap-poor-system-types)
868 (setq path '("~/mime.typ" "~/etc/mime.typ")))
870 ;; mime.types seems to be the normal name, definitely so
871 ;; on current GNUish systems. The search order follows
875 "/usr/etc/mime.types"
876 "/usr/local/etc/mime.types"
877 "/usr/local/www/conf/mime.types"
880 "/usr/etc/mime-types"
881 "/usr/local/etc/mime-types"
882 "/usr/local/www/conf/mime-types"))))
883 (let ((fnames (reverse (if (stringp path)
884 (delete "" (split-string path path-separator))
888 (setq fname (car fnames))
889 (if (and (file-readable-p fname))
890 (mailcap-parse-mimetype-file fname))
891 (setq fnames (cdr fnames))))
892 (setq mailcap-mimetypes-parsed-p t)))
894 (defun mailcap-parse-mimetype-file (fname)
895 "Parse out a mime-types file FNAME."
896 (let (type ; The MIME type for this line
897 extns ; The extensions for this line
898 save-pos ; Misc. saved buffer positions
901 (insert-file-contents fname)
902 (mailcap-replace-regexp "#.*" "")
903 (mailcap-replace-regexp "\n+" "\n")
904 (mailcap-replace-regexp "[ \t]+$" "")
905 (goto-char (point-max))
906 (skip-chars-backward " \t\n")
907 (delete-region (point) (point-max))
908 (goto-char (point-min))
910 (skip-chars-forward " \t\n")
911 (setq save-pos (point))
912 (skip-chars-forward "^ \t\n")
913 (downcase-region save-pos (point))
914 (setq type (buffer-substring save-pos (point)))
916 (skip-chars-forward " \t")
917 (setq save-pos (point))
918 (skip-chars-forward "^ \t\n")
919 (setq extns (cons (buffer-substring save-pos (point)) extns)))
921 (setq mailcap-mime-extensions
923 (cons (if (= (string-to-char (car extns)) ?.)
925 (concat "." (car extns))) type)
926 mailcap-mime-extensions)
927 extns (cdr extns)))))))
929 (defun mailcap-extension-to-mime (extn)
930 "Return the MIME content type of the file extensions EXTN."
931 (mailcap-parse-mimetypes)
932 (if (and (stringp extn)
933 (not (eq (string-to-char extn) ?.)))
934 (setq extn (concat "." extn)))
935 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
938 (defalias 'mailcap-command-p 'executable-find)
940 (defun mailcap-mime-types ()
941 "Return a list of MIME media types."
942 (mailcap-parse-mimetypes)
943 (mm-delete-duplicates
945 (mapcar 'cdr mailcap-mime-extensions)
953 (let ((type (cdr (assq 'type (cdr m)))))
954 (if (equal (cadr (split-string type "/"))
959 mailcap-mime-data)))))
963 ;;; mailcap.el ends here