1 ;;; mailcap.el --- Functions for displaying MIME parts
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
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.
29 (eval-when-compile (require 'cl))
33 (defvar mailcap-parse-args-syntax-table
34 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
35 (modify-syntax-entry ?' "\"" table)
36 (modify-syntax-entry ?` "\"" table)
37 (modify-syntax-entry ?{ "(" table)
38 (modify-syntax-entry ?} ")" table)
40 "A syntax table for parsing sgml attributes.")
42 (defvar mailcap-mime-data
45 (viewer . ssl-view-site-cert)
46 (test . (fboundp 'ssl-view-site-cert))
47 (type . "application/x-x509-ca-cert"))
49 (viewer . ssl-view-user-cert)
50 (test . (fboundp 'ssl-view-user-cert))
51 (type . "application/x-x509-user-cert"))
53 (viewer . mailcap-save-binary-file)
55 (type . "application/octet-stream"))
58 (type . "application/dvi")
59 (test . (eq (mm-device-type) 'ns)))
62 (test . (eq (mm-device-type) 'x))
64 (type . "application/dvi"))
66 (viewer . "dvitty %s")
67 (test . (not (getenv "DISPLAY")))
68 (type . "application/dvi"))
70 (viewer . mailcap-maybe-eval)
71 (type . "application/emacs-lisp"))
73 (viewer . mailcap-save-binary-file)
75 (type . "application/x-tar"))
78 (test . (fboundp 'tex-mode))
79 (type . "application/x-latex"))
82 (test . (fboundp 'tex-mode))
83 (type . "application/x-tex"))
86 (test . (fboundp 'tex-mode))
87 (type . "application/latex"))
90 (test . (fboundp 'tex-mode))
91 (type . "application/tex"))
93 (viewer . texinfo-mode)
94 (test . (fboundp 'texinfo-mode))
95 (type . "application/tex"))
97 (viewer . mailcap-save-binary-file)
99 (type . "application/zip")
102 (viewer . "acroread %s")
103 (type . "application/pdf"))
106 (type . "application/postscript")
107 (test . (eq (mm-device-type) 'ns)))
109 (viewer . "ghostview -dSAFER %s")
110 (type . "application/postscript")
111 (test . (eq (mm-device-type) 'x))
114 (viewer . "ps2ascii %s")
115 (type . "application/postscript")
116 (test . (not (getenv "DISPLAY")))
120 (viewer . "maplay %s")
121 (type . "audio/x-mpeg"))
123 (viewer . "showaudio")
127 (viewer . mm-view-message)
128 (test . (and (featurep 'gnus)
130 (type . "message/rfc822"))
133 (test . (fboundp 'vm-mode))
134 (type . "message/rfc822"))
137 (test . (fboundp 'w3-mode))
138 (type . "message/rfc822"))
141 (test . (fboundp 'view-mode))
142 (type . "message/rfc822"))
144 (viewer . fundamental-mode)
145 (type . "message/rfc822")))
148 (viewer . "xwud -in %s")
149 (type . "image/x-xwd")
150 ("compose" . "xwd -frame > %s")
151 (test . (eq (mm-device-type) 'x))
154 (viewer . "xwud -in %s")
155 (type . "image/x-xwd")
156 ("compose" . "xwd -frame > %s")
157 (test . (eq (mm-device-type) 'x))
160 (viewer . "xwud -in %s")
161 (type . "image/x-xwd")
162 ("compose" . "xwd -frame > %s")
163 (test . (eq (mm-device-type) 'x))
166 (viewer . "aopen %s")
168 (test . (eq (mm-device-type) 'ns)))
170 (viewer . "display %s")
172 (test . (eq (mm-device-type) 'x))
177 (test . (eq (mm-device-type) 'x))
182 (test . (fboundp 'w3-mode))
183 (type . "text/plain"))
186 (test . (fboundp 'view-mode))
187 (type . "text/plain"))
189 (viewer . fundamental-mode)
190 (type . "text/plain"))
192 (viewer . enriched-decode-region)
193 (test . (fboundp 'enriched-decode))
194 (type . "text/enriched"))
196 (viewer . mm-w3-prepare-buffer)
197 (test . (fboundp 'w3-prepare-buffer))
198 (type . "text/html")))
201 (viewer . "mpeg_play %s")
202 (type . "video/mpeg")
203 (test . (eq (mm-device-type) 'x))
207 (viewer . "webspace -remote %s -URL %u")
208 (type . "x-world/x-vrml")
214 (type . "archive/tar")
215 (test . (fboundp 'tar-mode)))))
216 "The mailcap structure is an assoc list of assoc lists.
217 1st assoc list is keyed on the major content-type
218 2nd assoc list is keyed on the minor content-type (which can be a regexp)
223 (\"postscript\" . <info>))
225 (\"plain\" . <info>)))
227 Where <info> is another assoc list of the various information
228 related to the mailcap RFC. This is keyed on the lowercase
229 attribute name (viewer, test, etc). This looks like:
230 ((viewer . viewerinfo)
234 Where viewerinfo specifies how the content-type is viewed. Can be
235 a string, in which case it is run through a shell, with
236 appropriate parameters, or a symbol, in which case the symbol is
237 funcall'd, with the buffer as an argument.
239 testinfo is a list of strings, or nil. If nil, it means the
240 viewer specified is always valid. If it is a list of strings,
241 these are used to determine whether a viewer passes the 'test' or
244 (defvar mailcap-download-directory nil
245 "*Where downloaded files should go by default.")
247 (defvar mailcap-temporary-directory
248 (cond ((fboundp 'temp-directory) (temp-directory))
249 ((boundp 'temporary-file-directory) temporary-file-directory)
251 "*Where temporary files go.")
254 ;;; Utility functions
257 (defun mailcap-generate-unique-filename (&optional fmt)
258 "Generate a unique filename in mailcap-temporary-directory."
260 (let ((base (format "mailcap-tmp.%d" (user-real-uid)))
263 (setq fname (format "%s%d" base x))
264 (while (file-exists-p
265 (expand-file-name fname mailcap-temporary-directory))
267 fname (concat base (int-to-string x))))
268 (expand-file-name fname mailcap-temporary-directory))
269 (let ((base (concat "mm" (int-to-string (user-real-uid))))
272 (setq fname (format fmt (concat base (int-to-string x))))
273 (while (file-exists-p
274 (expand-file-name fname mailcap-temporary-directory))
276 fname (format fmt (concat base (int-to-string x)))))
277 (expand-file-name fname mailcap-temporary-directory))))
279 (defun mailcap-save-binary-file ()
280 (goto-char (point-min))
282 (let ((file (read-file-name
283 "Filename to save as: "
284 (or mailcap-download-directory "~/")))
285 (require-final-newline nil))
286 (write-region (point-min) (point-max) file))
287 (kill-buffer (current-buffer))))
289 (defvar mailcap-maybe-eval-warning
292 This MIME part contains untrusted and possibly harmful content.
293 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
294 things can happen. Please examine the code very carefully before you
295 instruct Emacs to evaluate it. You can browse the buffer containing
296 the code using \\[scroll-other-window].
298 If you are unsure what to do, please answer \"no\"."
299 "Text of warning message displayed by `mailcap-maybe-eval'.
300 Make sure that this text consists only of few text lines. Otherwise,
301 Gnus might fail to display all of it.")
303 (defun mailcap-maybe-eval ()
304 "Maybe evaluate a buffer of emacs lisp code."
305 (let ((lisp-buffer (current-buffer)))
306 (goto-char (point-min))
308 (save-window-excursion
309 (delete-other-windows)
310 (let ((buffer (get-buffer-create (generate-new-buffer-name
313 (with-current-buffer buffer
314 (insert (substitute-command-keys
315 mailcap-maybe-eval-warning))
316 (goto-char (point-min))
317 (display-buffer buffer)
318 (yes-or-no-p "This is emacs-lisp code, evaluate it? "))
319 (kill-buffer buffer))))
320 (eval-buffer (current-buffer)))
321 (when (buffer-live-p lisp-buffer)
322 (with-current-buffer lisp-buffer
323 (emacs-lisp-mode)))))
327 ;;; The mailcap parser
330 (defun mailcap-replace-regexp (regexp to-string)
331 ;; Quiet replace-regexp.
332 (goto-char (point-min))
333 (while (re-search-forward regexp nil t)
334 (replace-match to-string t nil)))
336 (defvar mailcap-parsed-p nil)
338 (defun mailcap-parse-mailcaps (&optional path force)
339 "Parse out all the mailcaps specified in a path string PATH.
340 Components of PATH are separated by the `path-separator' character
341 appropriate for this system. If FORCE, re-parse even if already
342 parsed. If PATH is omitted, use the value of environment variable
343 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
344 /usr/local/etc/mailcap."
345 (interactive (list nil t))
346 (when (or (not mailcap-parsed-p)
350 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
351 ((memq system-type '(ms-dos ms-windows windows-nt))
352 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
354 ;; This is per RFC 1524, specifically
355 ;; with /usr before /usr/local.
356 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
357 "/usr/local/etc/mailcap"))))
358 (let ((fnames (reverse
360 (parse-colon-path path)
364 (setq fname (car fnames))
365 (if (and (file-readable-p fname)
366 (file-regular-p fname))
367 (mailcap-parse-mailcap fname))
368 (setq fnames (cdr fnames))))
369 (setq mailcap-parsed-p t)))
371 (defun mailcap-parse-mailcap (fname)
372 ;; Parse out the mailcap file specified by FNAME
373 (let (major ; The major mime type (image/audio/etc)
374 minor ; The minor mime type (gif, basic, etc)
375 save-pos ; Misc saved positions used in parsing
376 viewer ; How to view this mime type
377 info ; Misc info about this mime type
380 (insert-file-contents fname)
381 (set-syntax-table mailcap-parse-args-syntax-table)
382 (mailcap-replace-regexp "#.*" "") ; Remove all comments
383 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
384 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
385 (goto-char (point-max))
386 (skip-chars-backward " \t\n")
387 (delete-region (point) (point-max))
389 (skip-chars-backward " \t\n")
391 (setq save-pos (point)
393 (skip-chars-forward "^/; \t\n")
394 (downcase-region save-pos (point))
395 (setq major (buffer-substring save-pos (point)))
396 (skip-chars-forward " \t")
398 (when (eq (char-after) ?/)
400 (skip-chars-forward " \t")
401 (setq save-pos (point))
402 (skip-chars-forward "^; \t\n")
403 (downcase-region save-pos (point))
406 ((eq ?* (or (char-after save-pos) 0)) ".*")
407 ((= (point) save-pos) ".*")
408 (t (regexp-quote (buffer-substring save-pos (point)))))))
409 (skip-chars-forward " \t")
410 ;;; Got the major/minor chunks, now for the viewers/etc
411 ;;; The first item _must_ be a viewer, according to the
412 ;;; RFC for mailcap files (#1343)
414 (when (eq (char-after) ?\;)
416 (skip-chars-forward " \t")
417 (setq save-pos (point))
418 (skip-chars-forward "^;\n")
420 (while (eq (char-before) ?\\)
421 (backward-delete-char 1)
423 (skip-chars-forward "^;\n"))
424 (if (eq (or (char-after save-pos) 0) ?')
426 (narrow-to-region (1+ save-pos) (point))
427 (goto-char (point-min))
429 (read (current-buffer))
430 (goto-char (point-max))
432 (setq viewer (buffer-substring save-pos (point)))))
433 (setq save-pos (point))
435 (unless (equal viewer "")
436 (setq info (nconc (list (cons 'viewer viewer)
437 (cons 'type (concat major "/"
438 (if (string= minor ".*")
440 (mailcap-parse-mailcap-extras save-pos (point))))
441 (mailcap-mailcap-entry-passes-test info)
442 (mailcap-add-mailcap-entry major minor info))
443 (beginning-of-line)))))
445 (defun mailcap-parse-mailcap-extras (st nd)
446 ;; Grab all the extra stuff from a mailcap entry
450 results ; Assoc list of results
451 name-pos ; Start of XXXX= position
452 val-pos ; Start of value position
453 done ; Found end of \'d ;s?
456 (narrow-to-region st nd)
457 (goto-char (point-min))
458 (skip-chars-forward " \n\t;")
461 (setq name-pos (point))
462 (skip-chars-forward "^ \n\t=;")
463 (downcase-region name-pos (point))
464 (setq name (buffer-substring name-pos (point)))
465 (skip-chars-forward " \t\n")
466 (if (not (eq (char-after (point)) ?=)) ; There is no value
468 (skip-chars-forward " \t\n=")
469 (setq val-pos (point))
470 (if (memq (char-after val-pos) '(?\" ?'))
472 (setq val-pos (1+ val-pos))
477 (error (goto-char (point-max)))))
479 (skip-chars-forward "^;")
480 (if (eq (char-after (1- (point))) ?\\ )
482 (subst-char-in-region (1- (point)) (point) ?\\ ? )
483 (skip-chars-forward ";"))
485 (setq value (buffer-substring val-pos (point))))
486 (setq results (cons (cons name value) results))
487 (skip-chars-forward " \";\n\t"))
490 (defun mailcap-mailcap-entry-passes-test (info)
491 ;; Return t iff a mailcap entry passes its test clause or no test
492 ;; clause is present.
493 (let (status ; Call-process-regions return value
494 (test (assq 'test info)) ; The test clause
496 (setq status (and test (split-string (cdr test) " ")))
497 (if (and (or (assoc "needsterm" info)
498 (assoc "needsterminal" info)
499 (assoc "needsx11" info))
500 (not (getenv "DISPLAY")))
503 ((and (equal (nth 0 status) "test")
504 (equal (nth 1 status) "-n")
505 (or (equal (nth 2 status) "$DISPLAY")
506 (equal (nth 2 status) "\"$DISPLAY\"")))
507 (setq status (if (getenv "DISPLAY") t nil)))
508 ((and (equal (nth 0 status) "test")
509 (equal (nth 1 status) "-z")
510 (or (equal (nth 2 status) "$DISPLAY")
511 (equal (nth 2 status) "\"$DISPLAY\"")))
512 (setq status (if (getenv "DISPLAY") nil t)))
515 (and test (listp test) (setcdr test status))))
518 ;;; The action routines.
521 (defun mailcap-possible-viewers (major minor)
522 ;; Return a list of possible viewers from MAJOR for minor type MINOR
527 ((equal (car (car major)) minor)
528 (setq exact (cons (cdr (car major)) exact)))
529 ((and minor (string-match (car (car major)) minor))
530 (setq wildcard (cons (cdr (car major)) wildcard))))
531 (setq major (cdr major)))
532 (nconc exact wildcard)))
534 (defun mailcap-unescape-mime-test (test type-info)
535 (let (save-pos save-chr subst)
537 ((symbolp test) test)
538 ((and (listp test) (symbolp (car test))) test)
540 (and (listp test) (stringp (car test))
541 (setq test (mapconcat 'identity test " "))))
544 (goto-char (point-min))
546 (skip-chars-forward "^%")
548 (progn (skip-chars-backward "\\\\")
550 0) ; It is an escaped %
553 (skip-chars-forward "%."))
554 (setq save-pos (point))
555 (skip-chars-forward "%")
556 (setq save-chr (char-after (point)))
558 ((null save-chr) nil)
560 (delete-region save-pos (progn (forward-char 1) (point)))
561 (insert (or (cdr (assq 'type type-info)) "\"\"")))
563 (delete-region save-pos (progn (forward-char 1) (point)))
566 (delete-region save-pos (progn (forward-char 1) (point)))
569 (delete-region save-pos (progn (forward-char 1) (point)))
573 (skip-chars-forward "^}")
574 (downcase-region (+ 2 save-pos) (point))
575 (setq subst (buffer-substring (+ 2 save-pos) (point)))
576 (delete-region save-pos (1+ (point)))
577 (insert (or (cdr (assoc subst type-info)) "\"\"")))
580 (t (error "Bad value to mailcap-unescape-mime-test. %s" test)))))
582 (defvar mailcap-viewer-test-cache nil)
584 (defun mailcap-viewer-passes-test (viewer-info type-info)
585 ;; Return non-nil iff the viewer specified by VIEWER-INFO passes its
586 ;; test clause (if any).
587 (let* ((test-info (assq 'test viewer-info))
588 (test (cdr test-info))
590 (viewer (cdr (assoc 'viewer viewer-info)))
591 (default-directory (expand-file-name "~/"))
592 status parsed-test cache result)
593 (if (setq cache (assoc test mailcap-viewer-test-cache))
598 ((not test-info) t) ; No test clause
599 ((not test) nil) ; Already failed test
600 ((eq test t) t) ; Already passed test
601 ((and (symbolp test) ; Lisp function as test
603 (funcall test type-info))
604 ((and (symbolp test) ; Lisp variable as test
607 ((and (listp test) ; List to be eval'd
608 (symbolp (car test)))
611 (setq test (mailcap-unescape-mime-test test type-info)
612 test (list shell-file-name nil nil nil
613 shell-command-switch test)
614 status (apply 'call-process test))
616 (push (list otest result) mailcap-viewer-test-cache)
619 (defun mailcap-add-mailcap-entry (major minor info)
620 (let ((old-major (assoc major mailcap-mime-data)))
621 (if (null old-major) ; New major area
622 (setq mailcap-mime-data
623 (cons (cons major (list (cons minor info)))
625 (let ((cur-minor (assoc minor old-major)))
627 ((or (null cur-minor) ; New minor area, or
628 (assq 'test info)) ; Has a test, insert at beginning
629 (setcdr old-major (cons (cons minor info) (cdr old-major))))
630 ((and (not (assq 'test info)) ; No test info, replace completely
631 (not (assq 'test cur-minor))
632 (equal (assq 'viewer info) ; Keep alternative viewer
633 (assq 'viewer cur-minor)))
634 (setcdr cur-minor info))
636 (setcdr old-major (cons (cons minor info) (cdr old-major))))))
639 (defun mailcap-add (type viewer &optional test)
640 "Add VIEWER as a handler for TYPE.
641 If TEST is not given, it defaults to t."
642 (let ((tl (split-string type "/")))
643 (when (or (not (car tl))
645 (error "%s is not a valid MIME type" type))
646 (mailcap-add-mailcap-entry
649 (test . ,(if test test t))
656 (defun mailcap-viewer-lessp (x y)
657 ;; Return t iff viewer X is more desirable than viewer Y
658 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
659 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
660 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
661 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
663 ((and x-wild (not y-wild))
665 ((and (not x-wild) y-wild)
667 ((and (not y-lisp) x-lisp)
671 (defun mailcap-mime-info (string &optional request)
672 "Get the MIME viewer command for STRING, return nil if none found.
673 Expects a complete content-type header line as its argument.
675 Second argument REQUEST specifies what information to return. If it is
676 nil or the empty string, the viewer (second field of the mailcap
677 entry) will be returned. If it is a string, then the mailcap field
678 corresponding to that string will be returned (print, description,
679 whatever). If a number, then all the information for this specific
680 viewer is returned. If `all', then all possible viewers for
681 this type is returned."
683 major ; Major encoding (text, etc)
684 minor ; Minor encoding (html, etc)
686 save-pos ; Misc. position during parse
687 major-info ; (assoc major mailcap-mime-data)
688 minor-info ; (assoc minor major-info)
689 test ; current test proc.
690 viewers ; Possible viewers
691 passed ; Viewers that passed the test
692 viewer ; The one and only viewer
695 (setq ctl (mail-header-parse-content-type (or string "text/plain")))
696 (setq major (split-string (car ctl) "/"))
697 (setq minor (cadr major)
699 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
700 (when (setq viewers (mailcap-possible-viewers major-info minor))
701 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
705 (if (mailcap-viewer-passes-test (car viewers) info)
706 (setq passed (cons (car viewers) passed)))
707 (setq viewers (cdr viewers)))
708 (setq passed (sort passed 'mailcap-viewer-lessp))
709 (setq viewer (car passed))))
710 (when (and (stringp (cdr (assq 'viewer viewer)))
712 (setq viewer (car passed)))
714 ((and (null viewer) (not (equal major "default")) request)
715 (mailcap-mime-info "default" request))
716 ((or (null request) (equal request ""))
717 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
719 (if (or (eq request 'test) (eq request 'viewer))
720 (mailcap-unescape-mime-test
721 (cdr-safe (assoc request viewer)) info)))
725 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
726 (setq viewer (copy-sequence viewer))
727 (let ((view (assq 'viewer viewer))
728 (test (assq 'test viewer)))
729 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
730 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
734 ;;; Experimental MIME-types parsing
737 (defvar mailcap-mime-extensions
738 '(("" . "text/plain")
739 (".abs" . "audio/x-mpeg")
740 (".aif" . "audio/aiff")
741 (".aifc" . "audio/aiff")
742 (".aiff" . "audio/aiff")
743 (".ano" . "application/x-annotator")
744 (".au" . "audio/ulaw")
745 (".avi" . "video/x-msvideo")
746 (".bcpio" . "application/x-bcpio")
747 (".bin" . "application/octet-stream")
748 (".cdf" . "application/x-netcdr")
749 (".cpio" . "application/x-cpio")
750 (".csh" . "application/x-csh")
751 (".css" . "text/css")
752 (".dvi" . "application/x-dvi")
753 (".diff" . "text/x-patch")
754 (".el" . "application/emacs-lisp")
755 (".eps" . "application/postscript")
756 (".etx" . "text/x-setext")
757 (".exe" . "application/octet-stream")
758 (".fax" . "image/x-fax")
759 (".gif" . "image/gif")
760 (".hdf" . "application/x-hdf")
761 (".hqx" . "application/mac-binhex40")
762 (".htm" . "text/html")
763 (".html" . "text/html")
764 (".icon" . "image/x-icon")
765 (".ief" . "image/ief")
766 (".jpg" . "image/jpeg")
767 (".macp" . "image/x-macpaint")
768 (".man" . "application/x-troff-man")
769 (".me" . "application/x-troff-me")
770 (".mif" . "application/mif")
771 (".mov" . "video/quicktime")
772 (".movie" . "video/x-sgi-movie")
773 (".mp2" . "audio/x-mpeg")
774 (".mp3" . "audio/x-mpeg")
775 (".mp2a" . "audio/x-mpeg2")
776 (".mpa" . "audio/x-mpeg")
777 (".mpa2" . "audio/x-mpeg2")
778 (".mpe" . "video/mpeg")
779 (".mpeg" . "video/mpeg")
780 (".mpega" . "audio/x-mpeg")
781 (".mpegv" . "video/mpeg")
782 (".mpg" . "video/mpeg")
783 (".mpv" . "video/mpeg")
784 (".ms" . "application/x-troff-ms")
785 (".nc" . "application/x-netcdf")
786 (".nc" . "application/x-netcdf")
787 (".oda" . "application/oda")
788 (".patch" . "text/x-patch")
789 (".pbm" . "image/x-portable-bitmap")
790 (".pdf" . "application/pdf")
791 (".pgm" . "image/portable-graymap")
792 (".pict" . "image/pict")
793 (".png" . "image/png")
794 (".pnm" . "image/x-portable-anymap")
795 (".ppm" . "image/portable-pixmap")
796 (".ps" . "application/postscript")
797 (".qt" . "video/quicktime")
798 (".ras" . "image/x-raster")
799 (".rgb" . "image/x-rgb")
800 (".rtf" . "application/rtf")
801 (".rtx" . "text/richtext")
802 (".sh" . "application/x-sh")
803 (".sit" . "application/x-stuffit")
804 (".snd" . "audio/basic")
805 (".src" . "application/x-wais-source")
806 (".tar" . "archive/tar")
807 (".tcl" . "application/x-tcl")
808 (".tcl" . "application/x-tcl")
809 (".tex" . "application/x-tex")
810 (".texi" . "application/texinfo")
811 (".tga" . "image/x-targa")
812 (".tif" . "image/tiff")
813 (".tiff" . "image/tiff")
814 (".tr" . "application/x-troff")
815 (".troff" . "application/x-troff")
816 (".tsv" . "text/tab-separated-values")
817 (".txt" . "text/plain")
818 (".vbs" . "video/mpeg")
819 (".vox" . "audio/basic")
820 (".vrml" . "x-world/x-vrml")
821 (".wav" . "audio/x-wav")
822 (".wrl" . "x-world/x-vrml")
823 (".xbm" . "image/xbm")
824 (".xpm" . "image/xpm")
825 (".xwd" . "image/windowdump")
826 (".zip" . "application/zip")
827 (".ai" . "application/postscript")
828 (".jpe" . "image/jpeg")
829 (".jpeg" . "image/jpeg"))
830 "An assoc list of file extensions and corresponding MIME content-types.")
832 (defun mailcap-parse-mimetypes (&optional path)
833 "Parse out all the mimetypes specified in a unix-style path string PATH.
834 Components of PATH are separated by the `path-separator' character
835 appropriate for this system. If PATH is omitted, use the value of
836 environment variable MIMETYPES if set; otherwise use a default path."
839 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
840 ((memq system-type '(ms-dos ms-windows windows-nt))
841 (setq path '("~/mime.typ" "~/etc/mime.typ")))
843 ;; mime.types seems to be the normal name, definitely so
844 ;; on current GNUish systems. The search order follows
848 "/usr/etc/mime.types"
849 "/usr/local/etc/mime.types"
850 "/usr/local/www/conf/mime.types"
853 "/usr/etc/mime-types"
854 "/usr/local/etc/mime-types"
855 "/usr/local/www/conf/mime-types"))))
856 (let ((fnames (reverse (if (stringp path)
857 (parse-colon-path path)
861 (setq fname (car fnames))
862 (if (and (file-readable-p fname))
863 (mailcap-parse-mimetype-file fname))
864 (setq fnames (cdr fnames)))))
866 (defun mailcap-parse-mimetype-file (fname)
867 ;; Parse out a mime-types file
868 (let (type ; The MIME type for this line
869 extns ; The extensions for this line
870 save-pos ; Misc. saved buffer positions
873 (insert-file-contents fname)
874 (mailcap-replace-regexp "#.*" "")
875 (mailcap-replace-regexp "\n+" "\n")
876 (mailcap-replace-regexp "[ \t]+$" "")
877 (goto-char (point-max))
878 (skip-chars-backward " \t\n")
879 (delete-region (point) (point-max))
880 (goto-char (point-min))
882 (skip-chars-forward " \t\n")
883 (setq save-pos (point))
884 (skip-chars-forward "^ \t\n")
885 (downcase-region save-pos (point))
886 (setq type (buffer-substring save-pos (point)))
888 (skip-chars-forward " \t")
889 (setq save-pos (point))
890 (skip-chars-forward "^ \t\n")
891 (setq extns (cons (buffer-substring save-pos (point)) extns)))
893 (setq mailcap-mime-extensions
895 (cons (if (= (string-to-char (car extns)) ?.)
897 (concat "." (car extns))) type)
898 mailcap-mime-extensions)
899 extns (cdr extns)))))))
901 (defun mailcap-extension-to-mime (extn)
902 "Return the MIME content type of the file extensions EXTN."
903 (if (and (stringp extn)
904 (not (eq (string-to-char extn) ?.)))
905 (setq extn (concat "." extn)))
906 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
908 (defvar mailcap-binary-suffixes
909 (if (memq system-type '(ms-dos windows-nt))
910 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
913 (defun mailcap-command-p (command)
914 "Say whether COMMAND is in the exec path.
915 The path of COMMAND will be returned iff COMMAND is a command."
916 (let ((path (if (file-name-absolute-p command) '(nil) exec-path))
919 (while (setq dir (pop path))
920 (let ((suffixes mailcap-binary-suffixes))
922 (when (and (file-executable-p
923 (setq file (expand-file-name
924 (concat command (pop suffixes))
926 (not (file-directory-p file)))
927 (throw 'found file))))))))
929 (defun mailcap-mime-types ()
930 "Return a list of MIME media types."
931 (mm-delete-duplicates (mapcar 'cdr mailcap-mime-extensions)))
935 ;;; mailcap.el ends here