1 ;;; mailcap.el --- MIME media types configuration
2 ;; Copyright (C) 1998, 1999, 2000 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 . ssl-view-site-cert
)
59 (test .
(fboundp 'ssl-view-site-cert
))
60 (type .
"application/x-x509-ca-cert"))
62 (viewer . ssl-view-user-cert
)
63 (test .
(fboundp 'ssl-view-user-cert
))
64 (type .
"application/x-x509-user-cert"))
66 (viewer . mailcap-save-binary-file
)
68 (type .
"application/octet-stream"))
69 ;;; XEmacs says `ns' device-type not implemented.
71 ;; (viewer . "open %s")
72 ;; (type . "application/dvi")
73 ;; (test . (eq (mm-device-type) 'ns)))
76 (test .
(eq (mm-device-type) 'x
))
78 (type .
"application/dvi"))
80 (viewer .
"dvitty %s")
81 (test .
(not (getenv "DISPLAY")))
82 (type .
"application/dvi"))
84 (viewer . mailcap-maybe-eval
)
85 (type .
"application/emacs-lisp"))
87 (viewer . mailcap-save-binary-file
)
89 (type .
"application/x-tar"))
92 (test .
(fboundp 'tex-mode
))
93 (type .
"application/x-latex"))
96 (test .
(fboundp 'tex-mode
))
97 (type .
"application/x-tex"))
100 (test .
(fboundp 'tex-mode
))
101 (type .
"application/latex"))
104 (test .
(fboundp 'tex-mode
))
105 (type .
"application/tex"))
107 (viewer . texinfo-mode
)
108 (test .
(fboundp 'texinfo-mode
))
109 (type .
"application/tex"))
111 (viewer . mailcap-save-binary-file
)
113 (type .
"application/zip")
115 ;; Prefer free viewers.
118 (type .
"application/pdf")
119 (test . window-system
))
122 (type .
"application/pdf")
123 (test .
(eq (mm-device-type) 'x
)))
125 (viewer .
"acroread %s")
126 (type .
"application/pdf"))
127 ;;; XEmacs says `ns' device-type not implemented.
129 ;; (viewer . "open %s")
130 ;; (type . "application/postscript")
131 ;; (test . (eq (mm-device-type) 'ns)))
133 (viewer .
"gv -safer %s")
134 (type .
"application/postscript")
135 (test . window-system
)
138 (viewer .
"ghostview -dSAFER %s")
139 (type .
"application/postscript")
140 (test .
(eq (mm-device-type) 'x
))
143 (viewer .
"ps2ascii %s")
144 (type .
"application/postscript")
145 (test .
(not (getenv "DISPLAY")))
149 (viewer .
"maplay %s")
150 (type .
"audio/x-mpeg"))
152 (viewer .
"showaudio")
156 (viewer . mm-view-message
)
157 (test .
(and (featurep 'gnus
)
159 (type .
"message/rfc822"))
162 (test .
(fboundp 'vm-mode
))
163 (type .
"message/rfc822"))
166 (test .
(fboundp 'w3-mode
))
167 (type .
"message/rfc822"))
170 (type .
"message/rfc822")))
173 (viewer .
"xwud -in %s")
174 (type .
"image/x-xwd")
175 ("compose" .
"xwd -frame > %s")
176 (test .
(eq (mm-device-type) 'x
))
179 (viewer .
"xwud -in %s")
180 (type .
"image/x-xwd")
181 ("compose" .
"xwd -frame > %s")
182 (test .
(eq (mm-device-type) 'x
))
185 (viewer .
"xwud -in %s")
186 (type .
"image/x-xwd")
187 ("compose" .
"xwd -frame > %s")
188 (test .
(eq (mm-device-type) 'x
))
190 ;;; XEmacs says `ns' device-type not implemented.
192 ;; (viewer . "aopen %s")
193 ;; (type . "image/*")
194 ;; (test . (eq (mm-device-type) 'ns)))
196 (viewer .
"display %s")
198 (test .
(eq (mm-device-type) 'x
))
203 (test .
(eq (mm-device-type) 'x
))
208 (test .
(fboundp 'w3-mode
))
209 (type .
"text/plain"))
212 (test .
(fboundp 'view-mode
))
213 (type .
"text/plain"))
215 (viewer . fundamental-mode
)
216 (type .
"text/plain"))
218 (viewer . enriched-decode-region
)
219 (test .
(fboundp 'enriched-decode
))
220 (type .
"text/enriched"))
222 (viewer . mm-w3-prepare-buffer
)
223 (test .
(fboundp 'w3-prepare-buffer
))
224 (type .
"text/html")))
227 (viewer .
"mpeg_play %s")
228 (type .
"video/mpeg")
229 (test .
(eq (mm-device-type) 'x
))
233 (viewer .
"webspace -remote %s -URL %u")
234 (type .
"x-world/x-vrml")
240 (type .
"archive/tar")
241 (test .
(fboundp 'tar-mode
)))))
242 "The mailcap structure is an assoc list of assoc lists.
243 1st assoc list is keyed on the major content-type
244 2nd assoc list is keyed on the minor content-type (which can be a regexp)
249 (\"postscript\" . <info>))
251 (\"plain\" . <info>)))
253 Where <info> is another assoc list of the various information
254 related to the mailcap RFC 1524. This is keyed on the lowercase
255 attribute name (viewer, test, etc). This looks like:
256 ((viewer . VIEWERINFO)
261 Where VIEWERINFO specifies how the content-type is viewed. Can be
262 a string, in which case it is run through a shell, with
263 appropriate parameters, or a symbol, in which case the symbol is
264 `funcall'ed, with the buffer as an argument.
266 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
267 means the viewer is always valid. If it is a Lisp function, it is
268 called with a list of items from any extra fields from the
269 Content-Type header as argument to return a boolean value for the
270 validity. Otherwise, if it is a non-function Lisp symbol or list
271 whose car is a symbol, it is `eval'led to yield the validity. If it
272 is a string or list of strings, it represents a shell command to run
273 to return a true or false shell value for the validity.")
274 (put 'mailcap-mime-data
'risky-local-variable t
)
276 (defcustom mailcap-download-directory nil
277 "*Directory to which `mailcap-save-binary-file' downloads files by default.
278 nil means your home directory."
279 :type
'(choice (const :tag
"Home directory" nil
)
284 ;;; Utility functions
287 (defun mailcap-save-binary-file ()
288 (goto-char (point-min))
290 (let ((file (read-file-name
291 "Filename to save as: "
292 (or mailcap-download-directory
"~/")))
293 (require-final-newline nil
))
294 (write-region (point-min) (point-max) file
))
295 (kill-buffer (current-buffer))))
297 (defvar mailcap-maybe-eval-warning
300 This MIME part contains untrusted and possibly harmful content.
301 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
302 things can happen. Please examine the code very carefully before you
303 instruct Emacs to evaluate it. You can browse the buffer containing
304 the code using \\[scroll-other-window].
306 If you are unsure what to do, please answer \"no\"."
307 "Text of warning message displayed by `mailcap-maybe-eval'.
308 Make sure that this text consists only of few text lines. Otherwise,
309 Gnus might fail to display all of it.")
311 (defun mailcap-maybe-eval ()
312 "Maybe evaluate a buffer of Emacs Lisp code."
313 (let ((lisp-buffer (current-buffer)))
314 (goto-char (point-min))
316 (save-window-excursion
317 (delete-other-windows)
318 (let ((buffer (get-buffer-create (generate-new-buffer-name
321 (with-current-buffer buffer
322 (insert (substitute-command-keys
323 mailcap-maybe-eval-warning
))
324 (goto-char (point-min))
325 (display-buffer buffer
)
326 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
327 (kill-buffer buffer
))))
328 (eval-buffer (current-buffer)))
329 (when (buffer-live-p lisp-buffer
)
330 (with-current-buffer lisp-buffer
331 (emacs-lisp-mode)))))
335 ;;; The mailcap parser
338 (defun mailcap-replace-regexp (regexp to-string
)
339 ;; Quiet replace-regexp.
340 (goto-char (point-min))
341 (while (re-search-forward regexp nil t
)
342 (replace-match to-string t nil
)))
344 (defvar mailcap-parsed-p nil
)
346 (defun mailcap-parse-mailcaps (&optional path force
)
347 "Parse out all the mailcaps specified in a path string PATH.
348 Components of PATH are separated by the `path-separator' character
349 appropriate for this system. If FORCE, re-parse even if already
350 parsed. If PATH is omitted, use the value of environment variable
351 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
352 /usr/local/etc/mailcap."
353 (interactive (list nil t
))
354 (when (or (not mailcap-parsed-p
)
358 ((getenv "MAILCAPS") (setq path
(getenv "MAILCAPS")))
359 ((memq system-type
'(ms-dos ms-windows windows-nt
))
360 (setq path
'("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
362 ;; This is per RFC 1524, specifically
363 ;; with /usr before /usr/local.
364 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
365 "/usr/local/etc/mailcap"))))
366 (let ((fnames (reverse
368 (delete "" (split-string path path-separator
))
372 (setq fname
(car fnames
))
373 (if (and (file-readable-p fname
)
374 (file-regular-p fname
))
375 (mailcap-parse-mailcap fname
))
376 (setq fnames
(cdr fnames
))))
377 (setq mailcap-parsed-p t
)))
379 (defun mailcap-parse-mailcap (fname)
380 "Parse out the mailcap file specified by FNAME."
381 (let (major ; The major mime type (image/audio/etc)
382 minor
; The minor mime type (gif, basic, etc)
383 save-pos
; Misc saved positions used in parsing
384 viewer
; How to view this mime type
385 info
; Misc info about this mime type
388 (insert-file-contents fname
)
389 (set-syntax-table mailcap-parse-args-syntax-table
)
390 (mailcap-replace-regexp "#.*" "") ; Remove all comments
391 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
392 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
393 (goto-char (point-max))
394 (skip-chars-backward " \t\n")
395 (delete-region (point) (point-max))
397 (skip-chars-backward " \t\n")
399 (setq save-pos
(point)
401 (skip-chars-forward "^/; \t\n")
402 (downcase-region save-pos
(point))
403 (setq major
(buffer-substring save-pos
(point)))
404 (skip-chars-forward " \t")
406 (when (eq (char-after) ?
/)
408 (skip-chars-forward " \t")
409 (setq save-pos
(point))
410 (skip-chars-forward "^; \t\n")
411 (downcase-region save-pos
(point))
414 ((eq ?
* (or (char-after save-pos
) 0)) ".*")
415 ((= (point) save-pos
) ".*")
416 (t (regexp-quote (buffer-substring save-pos
(point)))))))
417 (skip-chars-forward " \t")
418 ;;; Got the major/minor chunks, now for the viewers/etc
419 ;;; The first item _must_ be a viewer, according to the
420 ;;; RFC for mailcap files (#1524)
422 (when (eq (char-after) ?\
;)
424 (skip-chars-forward " \t")
425 (setq save-pos
(point))
426 (skip-chars-forward "^;\n")
428 (while (eq (char-before) ?
\\)
429 (backward-delete-char 1)
431 (skip-chars-forward "^;\n"))
432 (if (eq (or (char-after save-pos
) 0) ?
')
434 (narrow-to-region (1+ save-pos
) (point))
435 (goto-char (point-min))
437 (read (current-buffer))
438 (goto-char (point-max))
440 (setq viewer
(buffer-substring save-pos
(point)))))
441 (setq save-pos
(point))
443 (unless (equal viewer
"")
444 (setq info
(nconc (list (cons 'viewer viewer
)
445 (cons 'type
(concat major
"/"
446 (if (string= minor
".*")
448 (mailcap-parse-mailcap-extras save-pos
(point))))
449 (mailcap-mailcap-entry-passes-test info
)
450 (mailcap-add-mailcap-entry major minor info
))
451 (beginning-of-line)))))
453 (defun mailcap-parse-mailcap-extras (st nd
)
454 "Grab all the extra stuff from a mailcap entry."
458 results
; Assoc list of results
459 name-pos
; Start of XXXX= position
460 val-pos
; Start of value position
461 done
; Found end of \'d ;s?
464 (narrow-to-region st nd
)
465 (goto-char (point-min))
466 (skip-chars-forward " \n\t;")
469 (setq name-pos
(point))
470 (skip-chars-forward "^ \n\t=;")
471 (downcase-region name-pos
(point))
472 (setq name
(buffer-substring name-pos
(point)))
473 (skip-chars-forward " \t\n")
474 (if (not (eq (char-after (point)) ?
=)) ; There is no value
476 (skip-chars-forward " \t\n=")
477 (setq val-pos
(point))
478 (if (memq (char-after val-pos
) '(?
\" ?
'))
480 (setq val-pos
(1+ val-pos
))
485 (error (goto-char (point-max)))))
487 (skip-chars-forward "^;")
488 (if (eq (char-after (1- (point))) ?
\\ )
490 (subst-char-in-region (1- (point)) (point) ?
\\ ?
)
491 (skip-chars-forward ";"))
493 (setq value
(buffer-substring val-pos
(point))))
494 (setq results
(cons (cons name value
) results
))
495 (skip-chars-forward " \";\n\t"))
498 (defun mailcap-mailcap-entry-passes-test (info)
499 "Return non-nil iff mailcap entry INFO passes its test clause.
500 Also return non-nil if no test clause is present."
501 (let ((test (assq 'test info
)) ; The test clause
503 (setq status
(and test
(split-string (cdr test
) " ")))
504 (if (and (or (assoc "needsterm" info
)
505 (assoc "needsterminal" info
)
506 (assoc "needsx11" info
))
507 (not (getenv "DISPLAY")))
510 ((and (equal (nth 0 status
) "test")
511 (equal (nth 1 status
) "-n")
512 (or (equal (nth 2 status
) "$DISPLAY")
513 (equal (nth 2 status
) "\"$DISPLAY\"")))
514 (setq status
(if (getenv "DISPLAY") t nil
)))
515 ((and (equal (nth 0 status
) "test")
516 (equal (nth 1 status
) "-z")
517 (or (equal (nth 2 status
) "$DISPLAY")
518 (equal (nth 2 status
) "\"$DISPLAY\"")))
519 (setq status
(if (getenv "DISPLAY") nil t
)))
522 (and test
(listp test
) (setcdr test status
))))
525 ;;; The action routines.
528 (defun mailcap-possible-viewers (major minor
)
529 "Return a list of possible viewers from MAJOR for minor type MINOR."
534 ((equal (car (car major
)) minor
)
535 (setq exact
(cons (cdr (car major
)) exact
)))
536 ((and minor
(string-match (car (car major
)) minor
))
537 (setq wildcard
(cons (cdr (car major
)) wildcard
))))
538 (setq major
(cdr major
)))
539 (nconc exact wildcard
)))
541 (defun mailcap-unescape-mime-test (test type-info
)
542 (let (save-pos save-chr subst
)
544 ((symbolp test
) test
)
545 ((and (listp test
) (symbolp (car test
))) test
)
547 (and (listp test
) (stringp (car test
))
548 (setq test
(mapconcat 'identity test
" "))))
551 (goto-char (point-min))
553 (skip-chars-forward "^%")
555 (progn (skip-chars-backward "\\\\")
557 0) ; It is an escaped %
560 (skip-chars-forward "%."))
561 (setq save-pos
(point))
562 (skip-chars-forward "%")
563 (setq save-chr
(char-after (point)))
565 ;; %s: name of a file for the body data
567 ;; %{<parameter name}: value of parameter in mailcap entry
568 ;; %n: number of sub-parts for multipart content-type
569 ;; %F: a set of content-type/filename pairs for multiparts
571 ((null save-chr
) nil
)
573 (delete-region save-pos
(progn (forward-char 1) (point)))
574 (insert (or (cdr (assq 'type type-info
)) "\"\"")))
575 ((memq save-chr
'(?M ?n ?F
))
576 (delete-region save-pos
(progn (forward-char 1) (point)))
580 (skip-chars-forward "^}")
581 (downcase-region (+ 2 save-pos
) (point))
582 (setq subst
(buffer-substring (+ 2 save-pos
) (point)))
583 (delete-region save-pos
(1+ (point)))
584 (insert (or (cdr (assoc subst type-info
)) "\"\"")))
587 (t (error "Bad value to mailcap-unescape-mime-test: %s" test
)))))
589 (defvar mailcap-viewer-test-cache nil
)
591 (defun mailcap-viewer-passes-test (viewer-info type-info
)
592 "Return non-nil iff viewer specified by VIEWER-INFO passes its test clause.
593 Also retun non-nil if it has no test clause. TYPE-INFO is an argument
594 to supply to the test."
595 (let* ((test-info (assq 'test viewer-info
))
596 (test (cdr test-info
))
598 (viewer (cdr (assoc 'viewer viewer-info
)))
599 (default-directory (expand-file-name "~/"))
600 status parsed-test cache result
)
601 (if (setq cache
(assoc test mailcap-viewer-test-cache
))
606 ((not test-info
) t
) ; No test clause
607 ((not test
) nil
) ; Already failed test
608 ((eq test t
) t
) ; Already passed test
609 ((functionp test
) ; Lisp function as test
610 (funcall test type-info
))
611 ((and (symbolp test
) ; Lisp variable as test
614 ((and (listp test
) ; List to be eval'd
615 (symbolp (car test
)))
618 (setq test
(mailcap-unescape-mime-test test type-info
)
619 test
(list shell-file-name nil nil nil
620 shell-command-switch test
)
621 status
(apply 'call-process test
))
623 (push (list otest result
) mailcap-viewer-test-cache
)
626 (defun mailcap-add-mailcap-entry (major minor info
)
627 (let ((old-major (assoc major mailcap-mime-data
)))
628 (if (null old-major
) ; New major area
629 (setq mailcap-mime-data
630 (cons (cons major
(list (cons minor info
)))
632 (let ((cur-minor (assoc minor old-major
)))
634 ((or (null cur-minor
) ; New minor area, or
635 (assq 'test info
)) ; Has a test, insert at beginning
636 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))
637 ((and (not (assq 'test info
)) ; No test info, replace completely
638 (not (assq 'test cur-minor
))
639 (equal (assq 'viewer info
) ; Keep alternative viewer
640 (assq 'viewer cur-minor
)))
641 (setcdr cur-minor info
))
643 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))))
646 (defun mailcap-add (type viewer
&optional test
)
647 "Add VIEWER as a handler for TYPE.
648 If TEST is not given, it defaults to t."
649 (let ((tl (split-string type
"/")))
650 (when (or (not (car tl
))
652 (error "%s is not a valid MIME type" type
))
653 (mailcap-add-mailcap-entry
656 (test .
,(if test test t
))
663 (defun mailcap-viewer-lessp (x y
)
664 "Return t iff viewer X is more desirable than viewer Y."
665 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x
)) "")))
666 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y
)) "")))
667 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x
)) ""))))
668 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y
)) "")))))
670 ((and x-wild
(not y-wild
))
672 ((and (not x-wild
) y-wild
)
674 ((and (not y-lisp
) x-lisp
)
678 (defun mailcap-mime-info (string &optional request
)
679 "Get the MIME viewer command for STRING, return nil if none found.
680 Expects a complete content-type header line as its argument.
682 Second argument REQUEST specifies what information to return. If it is
683 nil or the empty string, the viewer (second field of the mailcap
684 entry) will be returned. If it is a string, then the mailcap field
685 corresponding to that string will be returned (print, description,
686 whatever). If a number, then all the information for this specific
687 viewer is returned. If `all', then all possible viewers for
688 this type is returned."
690 major
; Major encoding (text, etc)
691 minor
; Minor encoding (html, etc)
693 save-pos
; Misc. position during parse
694 major-info
; (assoc major mailcap-mime-data)
695 minor-info
; (assoc minor major-info)
696 test
; current test proc.
697 viewers
; Possible viewers
698 passed
; Viewers that passed the test
699 viewer
; The one and only viewer
702 (setq ctl
(mail-header-parse-content-type (or string
"text/plain")))
703 (setq major
(split-string (car ctl
) "/"))
704 (setq minor
(cadr major
)
706 (when (setq major-info
(cdr (assoc major mailcap-mime-data
)))
707 (when (setq viewers
(mailcap-possible-viewers major-info minor
))
708 (setq info
(mapcar (lambda (a) (cons (symbol-name (car a
))
712 (if (mailcap-viewer-passes-test (car viewers
) info
)
713 (setq passed
(cons (car viewers
) passed
)))
714 (setq viewers
(cdr viewers
)))
715 (setq passed
(sort passed
'mailcap-viewer-lessp
))
716 (setq viewer
(car passed
))))
717 (when (and (stringp (cdr (assq 'viewer viewer
)))
719 (setq viewer
(car passed
)))
721 ((and (null viewer
) (not (equal major
"default")) request
)
722 (mailcap-mime-info "default" request
))
723 ((or (null request
) (equal request
""))
724 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer
)) info
))
726 (if (or (eq request
'test
) (eq request
'viewer
))
727 (mailcap-unescape-mime-test
728 (cdr-safe (assoc request viewer
)) info
)))
732 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
733 (setq viewer
(copy-sequence viewer
))
734 (let ((view (assq 'viewer viewer
))
735 (test (assq 'test viewer
)))
736 (if view
(setcdr view
(mailcap-unescape-mime-test (cdr view
) info
)))
737 (if test
(setcdr test
(mailcap-unescape-mime-test (cdr test
) info
))))
741 ;;; Experimental MIME-types parsing
744 (defvar mailcap-mime-extensions
745 '(("" .
"text/plain")
746 (".abs" .
"audio/x-mpeg")
747 (".aif" .
"audio/aiff")
748 (".aifc" .
"audio/aiff")
749 (".aiff" .
"audio/aiff")
750 (".ano" .
"application/x-annotator")
751 (".au" .
"audio/ulaw")
752 (".avi" .
"video/x-msvideo")
753 (".bcpio" .
"application/x-bcpio")
754 (".bin" .
"application/octet-stream")
755 (".cdf" .
"application/x-netcdr")
756 (".cpio" .
"application/x-cpio")
757 (".csh" .
"application/x-csh")
758 (".css" .
"text/css")
759 (".dvi" .
"application/x-dvi")
760 (".diff" .
"text/x-patch")
761 (".el" .
"application/emacs-lisp")
762 (".eps" .
"application/postscript")
763 (".etx" .
"text/x-setext")
764 (".exe" .
"application/octet-stream")
765 (".fax" .
"image/x-fax")
766 (".gif" .
"image/gif")
767 (".hdf" .
"application/x-hdf")
768 (".hqx" .
"application/mac-binhex40")
769 (".htm" .
"text/html")
770 (".html" .
"text/html")
771 (".icon" .
"image/x-icon")
772 (".ief" .
"image/ief")
773 (".jpg" .
"image/jpeg")
774 (".macp" .
"image/x-macpaint")
775 (".man" .
"application/x-troff-man")
776 (".me" .
"application/x-troff-me")
777 (".mif" .
"application/mif")
778 (".mov" .
"video/quicktime")
779 (".movie" .
"video/x-sgi-movie")
780 (".mp2" .
"audio/x-mpeg")
781 (".mp3" .
"audio/x-mpeg")
782 (".mp2a" .
"audio/x-mpeg2")
783 (".mpa" .
"audio/x-mpeg")
784 (".mpa2" .
"audio/x-mpeg2")
785 (".mpe" .
"video/mpeg")
786 (".mpeg" .
"video/mpeg")
787 (".mpega" .
"audio/x-mpeg")
788 (".mpegv" .
"video/mpeg")
789 (".mpg" .
"video/mpeg")
790 (".mpv" .
"video/mpeg")
791 (".ms" .
"application/x-troff-ms")
792 (".nc" .
"application/x-netcdf")
793 (".nc" .
"application/x-netcdf")
794 (".oda" .
"application/oda")
795 (".patch" .
"text/x-patch")
796 (".pbm" .
"image/x-portable-bitmap")
797 (".pdf" .
"application/pdf")
798 (".pgm" .
"image/portable-graymap")
799 (".pict" .
"image/pict")
800 (".png" .
"image/png")
801 (".pnm" .
"image/x-portable-anymap")
802 (".ppm" .
"image/portable-pixmap")
803 (".ps" .
"application/postscript")
804 (".qt" .
"video/quicktime")
805 (".ras" .
"image/x-raster")
806 (".rgb" .
"image/x-rgb")
807 (".rtf" .
"application/rtf")
808 (".rtx" .
"text/richtext")
809 (".sh" .
"application/x-sh")
810 (".sit" .
"application/x-stuffit")
811 (".snd" .
"audio/basic")
812 (".src" .
"application/x-wais-source")
813 (".tar" .
"archive/tar")
814 (".tcl" .
"application/x-tcl")
815 (".tex" .
"application/x-tex")
816 (".texi" .
"application/texinfo")
817 (".tga" .
"image/x-targa")
818 (".tif" .
"image/tiff")
819 (".tiff" .
"image/tiff")
820 (".tr" .
"application/x-troff")
821 (".troff" .
"application/x-troff")
822 (".tsv" .
"text/tab-separated-values")
823 (".txt" .
"text/plain")
824 (".vbs" .
"video/mpeg")
825 (".vox" .
"audio/basic")
826 (".vrml" .
"x-world/x-vrml")
827 (".wav" .
"audio/x-wav")
828 (".wrl" .
"x-world/x-vrml")
829 (".xbm" .
"image/xbm")
830 (".xpm" .
"image/xpm")
831 (".xwd" .
"image/windowdump")
832 (".zip" .
"application/zip")
833 (".ai" .
"application/postscript")
834 (".jpe" .
"image/jpeg")
835 (".jpeg" .
"image/jpeg"))
836 "An alist of file extensions and corresponding MIME content-types.
837 This exists for you to customize the information in Lisp. It is
838 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
840 (defvar mailcap-mimetypes-parsed-p nil
)
842 (defun mailcap-parse-mimetypes (&optional path force
)
843 "Parse out all the mimetypes specified in a Unix-style path string PATH.
844 Components of PATH are separated by the `path-separator' character
845 appropriate for this system. If PATH is omitted, use the value of
846 environment variable MIMETYPES if set; otherwise use a default path.
847 If FORCE, re-parse even if already parsed."
848 (interactive (list nil t
))
849 (when (or (not mailcap-mimetypes-parsed-p
)
853 ((getenv "MIMETYPES") (setq path
(getenv "MIMETYPES")))
854 ((memq system-type
'(ms-dos ms-windows windows-nt
))
855 (setq path
'("~/mime.typ" "~/etc/mime.typ")))
857 ;; mime.types seems to be the normal name, definitely so
858 ;; on current GNUish systems. The search order follows
862 "/usr/etc/mime.types"
863 "/usr/local/etc/mime.types"
864 "/usr/local/www/conf/mime.types"
867 "/usr/etc/mime-types"
868 "/usr/local/etc/mime-types"
869 "/usr/local/www/conf/mime-types"))))
870 (let ((fnames (reverse (if (stringp path
)
871 (delete "" (split-string path path-separator
))
875 (setq fname
(car fnames
))
876 (if (and (file-readable-p fname
))
877 (mailcap-parse-mimetype-file fname
))
878 (setq fnames
(cdr fnames
))))
879 (setq mailcap-mimetypes-parsed-p t
)))
881 (defun mailcap-parse-mimetype-file (fname)
882 "Parse out a mime-types file FNAME."
883 (let (type ; The MIME type for this line
884 extns
; The extensions for this line
885 save-pos
; Misc. saved buffer positions
888 (insert-file-contents fname
)
889 (mailcap-replace-regexp "#.*" "")
890 (mailcap-replace-regexp "\n+" "\n")
891 (mailcap-replace-regexp "[ \t]+$" "")
892 (goto-char (point-max))
893 (skip-chars-backward " \t\n")
894 (delete-region (point) (point-max))
895 (goto-char (point-min))
897 (skip-chars-forward " \t\n")
898 (setq save-pos
(point))
899 (skip-chars-forward "^ \t\n")
900 (downcase-region save-pos
(point))
901 (setq type
(buffer-substring save-pos
(point)))
903 (skip-chars-forward " \t")
904 (setq save-pos
(point))
905 (skip-chars-forward "^ \t\n")
906 (setq extns
(cons (buffer-substring save-pos
(point)) extns
)))
908 (setq mailcap-mime-extensions
910 (cons (if (= (string-to-char (car extns
)) ?.
)
912 (concat "." (car extns
))) type
)
913 mailcap-mime-extensions
)
914 extns
(cdr extns
)))))))
916 (defun mailcap-extension-to-mime (extn)
917 "Return the MIME content type of the file extensions EXTN."
918 (mailcap-parse-mimetypes)
919 (if (and (stringp extn
)
920 (not (eq (string-to-char extn
) ?.
)))
921 (setq extn
(concat "." extn
)))
922 (cdr (assoc (downcase extn
) mailcap-mime-extensions
)))
925 (defalias 'mailcap-command-p
'executable-find
)
927 (defun mailcap-mime-types ()
928 "Return a list of MIME media types."
929 (mailcap-parse-mimetypes)
930 (mm-delete-duplicates
932 (mapcar 'cdr mailcap-mime-extensions
)
940 (let ((type (cdr (assq 'type
(cdr m
)))))
941 (if (equal (cadr (split-string type
"/"))
946 mailcap-mime-data
)))))
950 ;;; arch-tag: 1fd4f9c9-c305-4d2e-9747-3a4d45baa0bd
951 ;;; mailcap.el ends here