1 ;;; mailcap.el --- Functions for displaying MIME parts
2 ;; Copyright (C) 1998,99 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.
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 . mailcap-save-binary-file)
125 (test . (or (featurep 'nas-sound)
126 (featurep 'native-sound)))
129 (viewer . "showaudio")
133 (viewer . mm-view-message)
134 (test . (and (featurep 'gnus)
136 (type . "message/rfc822"))
139 (test . (fboundp 'vm-mode))
140 (type . "message/rfc822"))
143 (test . (fboundp 'w3-mode))
144 (type . "message/rfc822"))
147 (test . (fboundp 'view-mode))
148 (type . "message/rfc822"))
150 (viewer . fundamental-mode)
151 (type . "message/rfc822")))
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 . "xwud -in %s")
167 (type . "image/x-xwd")
168 ("compose" . "xwd -frame > %s")
169 (test . (eq (mm-device-type) 'x))
172 (viewer . "aopen %s")
174 (test . (eq (mm-device-type) 'ns)))
176 (viewer . "display %s")
178 (test . (eq (mm-device-type) 'x))
183 (test . (eq (mm-device-type) 'x))
188 (test . (fboundp 'w3-mode))
189 (type . "text/plain"))
192 (test . (fboundp 'view-mode))
193 (type . "text/plain"))
195 (viewer . fundamental-mode)
196 (type . "text/plain"))
198 (viewer . enriched-decode-region)
199 (test . (fboundp 'enriched-decode))
200 (type . "text/enriched"))
202 (viewer . mm-w3-prepare-buffer)
203 (test . (fboundp 'w3-prepare-buffer))
204 (type . "text/html")))
207 (viewer . "mpeg_play %s")
208 (type . "video/mpeg")
209 (test . (eq (mm-device-type) 'x))
213 (viewer . "webspace -remote %s -URL %u")
214 (type . "x-world/x-vrml")
220 (type . "archive/tar")
221 (test . (fboundp 'tar-mode)))))
222 "The mailcap structure is an assoc list of assoc lists.
223 1st assoc list is keyed on the major content-type
224 2nd assoc list is keyed on the minor content-type (which can be a regexp)
229 (\"postscript\" . <info>))
231 (\"plain\" . <info>)))
233 Where <info> is another assoc list of the various information
234 related to the mailcap RFC. This is keyed on the lowercase
235 attribute name (viewer, test, etc). This looks like:
236 ((viewer . viewerinfo)
240 Where viewerinfo specifies how the content-type is viewed. Can be
241 a string, in which case it is run through a shell, with
242 appropriate parameters, or a symbol, in which case the symbol is
243 funcall'd, with the buffer as an argument.
245 testinfo is a list of strings, or nil. If nil, it means the
246 viewer specified is always valid. If it is a list of strings,
247 these are used to determine whether a viewer passes the 'test' or
250 (defvar mailcap-download-directory nil
251 "*Where downloaded files should go by default.")
253 (defvar mailcap-temporary-directory
254 (cond ((fboundp 'temp-directory) (temp-directory))
255 ((boundp 'temporary-file-directory) temporary-file-directory)
257 "*Where temporary files go.")
260 ;;; Utility functions
263 (defun mailcap-generate-unique-filename (&optional fmt)
264 "Generate a unique filename in mailcap-temporary-directory"
266 (let ((base (format "mailcap-tmp.%d" (user-real-uid)))
269 (setq fname (format "%s%d" base x))
270 (while (file-exists-p
271 (expand-file-name fname mailcap-temporary-directory))
273 fname (concat base (int-to-string x))))
274 (expand-file-name fname mailcap-temporary-directory))
275 (let ((base (concat "mm" (int-to-string (user-real-uid))))
278 (setq fname (format fmt (concat base (int-to-string x))))
279 (while (file-exists-p
280 (expand-file-name fname mailcap-temporary-directory))
282 fname (format fmt (concat base (int-to-string x)))))
283 (expand-file-name fname mailcap-temporary-directory))))
285 (defun mailcap-save-binary-file ()
286 (goto-char (point-min))
288 (let ((file (read-file-name
289 "Filename to save as: "
290 (or mailcap-download-directory "~/")))
291 (require-final-newline nil))
292 (write-region (point-min) (point-max) file))
293 (kill-buffer (current-buffer))))
295 (defun mailcap-maybe-eval ()
296 "Maybe evaluate a buffer of emacs lisp code"
297 (if (yes-or-no-p "This is emacs-lisp code, evaluate it? ")
298 (eval-buffer (current-buffer))
302 ;;; The mailcap parser
305 (defun mailcap-replace-regexp (regexp to-string)
306 ;; Quiet replace-regexp.
307 (goto-char (point-min))
308 (while (re-search-forward regexp nil t)
309 (replace-match to-string t nil)))
311 (defvar mailcap-parsed-p nil)
313 (defun mailcap-parse-mailcaps (&optional path force)
314 "Parse out all the mailcaps specified in a unix-style path string PATH.
315 If FORCE, re-parse even if already parsed."
316 (interactive (list nil t))
317 (when (or (not mailcap-parsed-p)
321 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
322 ((memq system-type '(ms-dos ms-windows windows-nt))
323 (setq path (mapconcat 'expand-file-name
324 '("~/mail.cap" "~/etc/mail.cap" "~/.mailcap")
326 (t (setq path (mapconcat 'expand-file-name
328 "/etc/mailcap:/usr/etc/mailcap"
329 "/usr/local/etc/mailcap") ":"))))
330 (let ((fnames (reverse
332 path (if (memq system-type
333 '(ms-dos ms-windows windows-nt))
338 (setq fname (car fnames))
339 (if (and (file-exists-p fname) (file-readable-p fname)
340 (file-regular-p fname))
341 (mailcap-parse-mailcap (car fnames)))
342 (setq fnames (cdr fnames))))
343 (setq mailcap-parsed-p t)))
345 (defun mailcap-parse-mailcap (fname)
346 ;; Parse out the mailcap file specified by FNAME
347 (let (major ; The major mime type (image/audio/etc)
348 minor ; The minor mime type (gif, basic, etc)
349 save-pos ; Misc saved positions used in parsing
350 viewer ; How to view this mime type
351 info ; Misc info about this mime type
354 (insert-file-contents fname)
355 (set-syntax-table mailcap-parse-args-syntax-table)
356 (mailcap-replace-regexp "#.*" "") ; Remove all comments
357 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
358 (mailcap-replace-regexp "\\\\[ \t\n]+" " ") ; And collapse spaces
359 (mailcap-replace-regexp (concat (regexp-quote "\\") "[ \t]*\n") "")
360 (goto-char (point-max))
361 (skip-chars-backward " \t\n")
362 (delete-region (point) (point-max))
363 (goto-char (point-min))
365 (skip-chars-forward " \t\n")
366 (setq save-pos (point)
368 (skip-chars-forward "^/;")
369 (downcase-region save-pos (point))
370 (setq major (buffer-substring save-pos (point)))
371 (skip-chars-forward "/ \t\n")
372 (setq save-pos (point))
373 (skip-chars-forward "^;")
374 (downcase-region save-pos (point))
377 ((eq ?* (or (char-after save-pos) 0)) ".*")
378 ((= (point) save-pos) ".*")
379 (t (regexp-quote (buffer-substring save-pos (point))))))
380 (skip-chars-forward "; \t\n")
381 ;;; Got the major/minor chunks, now for the viewers/etc
382 ;;; The first item _must_ be a viewer, according to the
383 ;;; RFC for mailcap files (#1343)
384 (skip-chars-forward "; \t\n")
385 (setq save-pos (point))
386 (skip-chars-forward "^;\n")
388 (while (eq (char-before) ?\\)
389 (backward-delete-char 1)
390 (skip-chars-forward ";")
391 (skip-chars-forward "^;\n"))
392 (if (eq (or (char-after save-pos) 0) ?')
394 (narrow-to-region (1+ save-pos) (point))
395 (goto-char (point-min))
397 (read (current-buffer))
398 (goto-char (point-max))
400 (setq viewer (buffer-substring save-pos (point))))
401 (setq save-pos (point))
403 (setq info (nconc (list (cons 'viewer viewer)
404 (cons 'type (concat major "/"
405 (if (string= minor ".*")
407 (mailcap-parse-mailcap-extras save-pos (point))))
408 (mailcap-mailcap-entry-passes-test info)
409 (mailcap-add-mailcap-entry major minor info)))))
411 (defun mailcap-parse-mailcap-extras (st nd)
412 ;; Grab all the extra stuff from a mailcap entry
416 results ; Assoc list of results
417 name-pos ; Start of XXXX= position
418 val-pos ; Start of value position
419 done ; Found end of \'d ;s?
422 (narrow-to-region st nd)
423 (goto-char (point-min))
424 (skip-chars-forward " \n\t;")
427 (setq name-pos (point))
428 (skip-chars-forward "^ \n\t=;")
429 (downcase-region name-pos (point))
430 (setq name (buffer-substring name-pos (point)))
431 (skip-chars-forward " \t\n")
432 (if (not (eq (char-after (point)) ?=)) ; There is no value
434 (skip-chars-forward " \t\n=")
435 (setq val-pos (point))
436 (if (memq (char-after val-pos) '(?\" ?'))
438 (setq val-pos (1+ val-pos))
443 (error (goto-char (point-max)))))
445 (skip-chars-forward "^;")
446 (if (eq (char-after (1- (point))) ?\\ )
448 (subst-char-in-region (1- (point)) (point) ?\\ ? )
449 (skip-chars-forward ";"))
451 (setq value (buffer-substring val-pos (point))))
452 (setq results (cons (cons name value) results))
453 (skip-chars-forward " \";\n\t"))
456 (defun mailcap-mailcap-entry-passes-test (info)
457 ;; Return t iff a mailcap entry passes its test clause or no test
458 ;; clause is present.
459 (let (status ; Call-process-regions return value
460 (test (assq 'test info)) ; The test clause
462 (setq status (and test (split-string (cdr test) " ")))
463 (if (and (or (assoc "needsterm" info)
464 (assoc "needsterminal" info)
465 (assoc "needsx11" info))
466 (not (getenv "DISPLAY")))
469 ((and (equal (nth 0 status) "test")
470 (equal (nth 1 status) "-n")
471 (or (equal (nth 2 status) "$DISPLAY")
472 (equal (nth 2 status) "\"$DISPLAY\"")))
473 (setq status (if (getenv "DISPLAY") t nil)))
474 ((and (equal (nth 0 status) "test")
475 (equal (nth 1 status) "-z")
476 (or (equal (nth 2 status) "$DISPLAY")
477 (equal (nth 2 status) "\"$DISPLAY\"")))
478 (setq status (if (getenv "DISPLAY") nil t)))
481 (and test (listp test) (setcdr test status))))
484 ;;; The action routines.
487 (defun mailcap-possible-viewers (major minor)
488 ;; Return a list of possible viewers from MAJOR for minor type MINOR
493 ((equal (car (car major)) minor)
494 (setq exact (cons (cdr (car major)) exact)))
495 ((and minor (string-match (car (car major)) minor))
496 (setq wildcard (cons (cdr (car major)) wildcard))))
497 (setq major (cdr major)))
498 (nconc (nreverse exact) (nreverse wildcard))))
500 (defun mailcap-unescape-mime-test (test type-info)
501 (let (save-pos save-chr subst)
503 ((symbolp test) test)
504 ((and (listp test) (symbolp (car test))) test)
506 (and (listp test) (stringp (car test))
507 (setq test (mapconcat 'identity test " "))))
510 (goto-char (point-min))
512 (skip-chars-forward "^%")
514 (progn (skip-chars-backward "\\\\")
516 0) ; It is an escaped %
519 (skip-chars-forward "%."))
520 (setq save-pos (point))
521 (skip-chars-forward "%")
522 (setq save-chr (char-after (point)))
524 ((null save-chr) nil)
526 (delete-region save-pos (progn (forward-char 1) (point)))
527 (insert (or (cdr (assq 'type type-info)) "\"\"")))
529 (delete-region save-pos (progn (forward-char 1) (point)))
532 (delete-region save-pos (progn (forward-char 1) (point)))
535 (delete-region save-pos (progn (forward-char 1) (point)))
539 (skip-chars-forward "^}")
540 (downcase-region (+ 2 save-pos) (point))
541 (setq subst (buffer-substring (+ 2 save-pos) (point)))
542 (delete-region save-pos (1+ (point)))
543 (insert (or (cdr (assoc subst type-info)) "\"\"")))
546 (t (error "Bad value to mailcap-unescape-mime-test. %s" test)))))
548 (defvar mailcap-viewer-test-cache nil)
550 (defun mailcap-viewer-passes-test (viewer-info type-info)
551 ;; Return non-nil iff the viewer specified by VIEWER-INFO passes its
552 ;; test clause (if any).
553 (let* ((test-info (assq 'test viewer-info))
554 (test (cdr test-info))
556 (viewer (cdr (assoc 'viewer viewer-info)))
557 (default-directory (expand-file-name "~/"))
558 status parsed-test cache result)
559 (if (setq cache (assoc test mailcap-viewer-test-cache))
564 ((not test-info) t) ; No test clause
565 ((not test) nil) ; Already failed test
566 ((eq test t) t) ; Already passed test
567 ((and (symbolp test) ; Lisp function as test
569 (funcall test type-info))
570 ((and (symbolp test) ; Lisp variable as test
573 ((and (listp test) ; List to be eval'd
574 (symbolp (car test)))
577 (setq test (mailcap-unescape-mime-test test type-info)
578 test (list shell-file-name nil nil nil
579 shell-command-switch test)
580 status (apply 'call-process test))
582 (push (list otest result) mailcap-viewer-test-cache)
585 (defun mailcap-add-mailcap-entry (major minor info)
586 (let ((old-major (assoc major mailcap-mime-data)))
587 (if (null old-major) ; New major area
588 (setq mailcap-mime-data
589 (cons (cons major (list (cons minor info)))
591 (let ((cur-minor (assoc minor old-major)))
593 ((or (null cur-minor) ; New minor area, or
594 (assq 'test info)) ; Has a test, insert at beginning
595 (setcdr old-major (cons (cons minor info) (cdr old-major))))
596 ((and (not (assq 'test info)) ; No test info, replace completely
597 (not (assq 'test cur-minor)))
598 (setcdr cur-minor info))
600 (setcdr old-major (cons (cons minor info) (cdr old-major)))))))))
602 (defun mailcap-add (type viewer &optional test)
603 "Add VIEWER as a handler for TYPE.
604 If TEST is not given, it defaults to t."
605 (let ((tl (split-string type "/")))
606 (when (or (not (car tl))
608 (error "%s is not a valid MIME type" type))
609 (mailcap-add-mailcap-entry
612 (test . ,(if test test t))
619 (defun mailcap-viewer-lessp (x y)
620 ;; Return t iff viewer X is more desirable than viewer Y
621 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
622 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
623 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
624 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
626 ((and x-lisp (not y-lisp))
628 ((and (not y-lisp) x-wild (not y-wild))
630 ((and (not x-wild) y-wild)
634 (defun mailcap-mime-info (string &optional request)
635 "Get the MIME viewer command for STRING, return nil if none found.
636 Expects a complete content-type header line as its argument.
638 Second argument REQUEST specifies what information to return. If it is
639 nil or the empty string, the viewer (second field of the mailcap
640 entry) will be returned. If it is a string, then the mailcap field
641 corresponding to that string will be returned (print, description,
642 whatever). If a number, then all the information for this specific
643 viewer is returned. If `all', then all possible viewers for
644 this type is returned."
646 major ; Major encoding (text, etc)
647 minor ; Minor encoding (html, etc)
649 save-pos ; Misc. position during parse
650 major-info ; (assoc major mailcap-mime-data)
651 minor-info ; (assoc minor major-info)
652 test ; current test proc.
653 viewers ; Possible viewers
654 passed ; Viewers that passed the test
655 viewer ; The one and only viewer
658 (setq ctl (mail-header-parse-content-type (or string "text/plain")))
659 (setq major (split-string (car ctl) "/"))
660 (setq minor (cadr major)
662 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
663 (when (setq viewers (mailcap-possible-viewers major-info minor))
664 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
668 (if (mailcap-viewer-passes-test (car viewers) info)
669 (setq passed (cons (car viewers) passed)))
670 (setq viewers (cdr viewers)))
671 (setq passed (sort (nreverse passed) 'mailcap-viewer-lessp))
672 (setq viewer (car passed))))
673 (when (and (stringp (cdr (assq 'viewer viewer)))
675 (setq viewer (car passed)))
677 ((and (null viewer) (not (equal major "default")) request)
678 (mailcap-mime-info "default" request))
679 ((or (null request) (equal request ""))
680 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
682 (if (or (eq request 'test) (eq request 'viewer))
683 (mailcap-unescape-mime-test
684 (cdr-safe (assoc request viewer)) info)))
688 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
689 (setq viewer (copy-tree viewer))
690 (let ((view (assq 'viewer viewer))
691 (test (assq 'test viewer)))
692 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
693 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
697 ;;; Experimental MIME-types parsing
700 (defvar mailcap-mime-extensions
701 '(("" . "text/plain")
702 (".abs" . "audio/x-mpeg")
703 (".aif" . "audio/aiff")
704 (".aifc" . "audio/aiff")
705 (".aiff" . "audio/aiff")
706 (".ano" . "application/x-annotator")
707 (".au" . "audio/ulaw")
708 (".avi" . "video/x-msvideo")
709 (".bcpio" . "application/x-bcpio")
710 (".bin" . "application/octet-stream")
711 (".cdf" . "application/x-netcdr")
712 (".cpio" . "application/x-cpio")
713 (".csh" . "application/x-csh")
714 (".dvi" . "application/x-dvi")
715 (".el" . "application/emacs-lisp")
716 (".eps" . "application/postscript")
717 (".etx" . "text/x-setext")
718 (".exe" . "application/octet-stream")
719 (".fax" . "image/x-fax")
720 (".gif" . "image/gif")
721 (".hdf" . "application/x-hdf")
722 (".hqx" . "application/mac-binhex40")
723 (".htm" . "text/html")
724 (".html" . "text/html")
725 (".icon" . "image/x-icon")
726 (".ief" . "image/ief")
727 (".jpg" . "image/jpeg")
728 (".macp" . "image/x-macpaint")
729 (".man" . "application/x-troff-man")
730 (".me" . "application/x-troff-me")
731 (".mif" . "application/mif")
732 (".mov" . "video/quicktime")
733 (".movie" . "video/x-sgi-movie")
734 (".mp2" . "audio/x-mpeg")
735 (".mp3" . "audio/x-mpeg")
736 (".mp2a" . "audio/x-mpeg2")
737 (".mpa" . "audio/x-mpeg")
738 (".mpa2" . "audio/x-mpeg2")
739 (".mpe" . "video/mpeg")
740 (".mpeg" . "video/mpeg")
741 (".mpega" . "audio/x-mpeg")
742 (".mpegv" . "video/mpeg")
743 (".mpg" . "video/mpeg")
744 (".mpv" . "video/mpeg")
745 (".ms" . "application/x-troff-ms")
746 (".nc" . "application/x-netcdf")
747 (".nc" . "application/x-netcdf")
748 (".oda" . "application/oda")
749 (".patch" . "text/x-patch")
750 (".pbm" . "image/x-portable-bitmap")
751 (".pdf" . "application/pdf")
752 (".pgm" . "image/portable-graymap")
753 (".pict" . "image/pict")
754 (".png" . "image/png")
755 (".pnm" . "image/x-portable-anymap")
756 (".ppm" . "image/portable-pixmap")
757 (".ps" . "application/postscript")
758 (".qt" . "video/quicktime")
759 (".ras" . "image/x-raster")
760 (".rgb" . "image/x-rgb")
761 (".rtf" . "application/rtf")
762 (".rtx" . "text/richtext")
763 (".sh" . "application/x-sh")
764 (".sit" . "application/x-stuffit")
765 (".snd" . "audio/basic")
766 (".src" . "application/x-wais-source")
767 (".tar" . "archive/tar")
768 (".tcl" . "application/x-tcl")
769 (".tcl" . "application/x-tcl")
770 (".tex" . "application/x-tex")
771 (".texi" . "application/texinfo")
772 (".tga" . "image/x-targa")
773 (".tif" . "image/tiff")
774 (".tiff" . "image/tiff")
775 (".tr" . "application/x-troff")
776 (".troff" . "application/x-troff")
777 (".tsv" . "text/tab-separated-values")
778 (".txt" . "text/plain")
779 (".vbs" . "video/mpeg")
780 (".vox" . "audio/basic")
781 (".vrml" . "x-world/x-vrml")
782 (".wav" . "audio/x-wav")
783 (".wrl" . "x-world/x-vrml")
784 (".xbm" . "image/xbm")
785 (".xpm" . "image/xpm")
786 (".xwd" . "image/windowdump")
787 (".zip" . "application/zip")
788 (".ai" . "application/postscript")
789 (".jpe" . "image/jpeg")
790 (".jpeg" . "image/jpeg"))
791 "An assoc list of file extensions and corresponding MIME content-types.")
793 (defun mailcap-parse-mimetypes (&optional path)
794 ;; Parse out all the mimetypes specified in a unix-style path string PATH
797 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
798 ((memq system-type '(ms-dos ms-windows windows-nt))
799 (setq path (mapconcat 'expand-file-name
800 '("~/mime.typ" "~/etc/mime.typ") ";")))
801 (t (setq path (mapconcat 'expand-file-name
803 "/etc/mime-types:/usr/etc/mime-types"
804 "/usr/local/etc/mime-types"
805 "/usr/local/www/conf/mime-types") ":"))))
806 (let ((fnames (reverse
808 (if (memq system-type
809 '(ms-dos ms-windows windows-nt))
813 (setq fname (car fnames))
814 (if (and (file-exists-p fname) (file-readable-p fname))
815 (mailcap-parse-mimetype-file (car fnames)))
816 (setq fnames (cdr fnames)))))
818 (defun mailcap-parse-mimetype-file (fname)
819 ;; Parse out a mime-types file
820 (let (type ; The MIME type for this line
821 extns ; The extensions for this line
822 save-pos ; Misc. saved buffer positions
825 (insert-file-contents fname)
826 (mailcap-replace-regexp "#.*" "")
827 (mailcap-replace-regexp "\n+" "\n")
828 (mailcap-replace-regexp "[ \t]+$" "")
829 (goto-char (point-max))
830 (skip-chars-backward " \t\n")
831 (delete-region (point) (point-max))
832 (goto-char (point-min))
834 (skip-chars-forward " \t\n")
835 (setq save-pos (point))
836 (skip-chars-forward "^ \t")
837 (downcase-region save-pos (point))
838 (setq type (buffer-substring save-pos (point)))
840 (skip-chars-forward " \t")
841 (setq save-pos (point))
842 (skip-chars-forward "^ \t\n")
843 (setq extns (cons (buffer-substring save-pos (point)) extns)))
845 (setq mailcap-mime-extensions
847 (cons (if (= (string-to-char (car extns)) ?.)
849 (concat "." (car extns))) type)
850 mailcap-mime-extensions)
851 extns (cdr extns)))))))
853 (defun mailcap-extension-to-mime (extn)
854 "Return the MIME content type of the file extensions EXTN."
855 (if (and (stringp extn)
856 (not (eq (string-to-char extn) ?.)))
857 (setq extn (concat "." extn)))
858 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
860 (defvar mailcap-binary-suffixes
861 (if (memq system-type '(ms-dos windows-nt))
862 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
865 (defun mailcap-command-p (command)
866 "Say whether COMMAND is in the exec path.
867 The path of COMMAND will be returned iff COMMAND is a command."
868 (let ((path (if (file-name-absolute-p command) '(nil) exec-path))
871 (while (setq dir (pop path))
872 (let ((suffixes mailcap-binary-suffixes))
874 (when (and (file-executable-p
875 (setq file (expand-file-name
876 (concat command (pop suffixes))
878 (not (file-directory-p file)))
879 (throw 'found file))))))))
881 (defun mailcap-mime-types ()
882 "Return a list of MIME media types."
883 (delete-duplicates (mapcar 'cdr mailcap-mime-extensions)))
887 ;;; mailcap.el ends here