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 . (eq (mm-device-type) 'x))
193 (test . (fboundp 'w3-mode))
194 (type . "text/plain"))
197 (test . (fboundp 'view-mode))
198 (type . "text/plain"))
200 (viewer . fundamental-mode)
201 (type . "text/plain"))
203 (viewer . enriched-decode-region)
204 (test . (fboundp 'enriched-decode))
205 (type . "text/enriched"))
207 (viewer . mm-w3-prepare-buffer)
208 (test . (fboundp 'w3-prepare-buffer))
209 (type . "text/html")))
212 (viewer . "mpeg_play %s")
213 (type . "video/mpeg")
214 (test . (eq (mm-device-type) 'x))
218 (viewer . "webspace -remote %s -URL %u")
219 (type . "x-world/x-vrml")
225 (type . "archive/tar")
226 (test . (fboundp 'tar-mode)))))
227 "The mailcap structure is an assoc list of assoc lists.
228 1st assoc list is keyed on the major content-type
229 2nd assoc list is keyed on the minor content-type (which can be a regexp)
234 (\"postscript\" . <info>))
236 (\"plain\" . <info>)))
238 Where <info> is another assoc list of the various information
239 related to the mailcap RFC. This is keyed on the lowercase
240 attribute name (viewer, test, etc). This looks like:
241 ((viewer . viewerinfo)
245 Where viewerinfo specifies how the content-type is viewed. Can be
246 a string, in which case it is run through a shell, with
247 appropriate parameters, or a symbol, in which case the symbol is
248 funcall'd, with the buffer as an argument.
250 testinfo is a list of strings, or nil. If nil, it means the
251 viewer specified is always valid. If it is a list of strings,
252 these are used to determine whether a viewer passes the 'test' or
255 (defvar mailcap-download-directory nil
256 "*Where downloaded files should go by default.")
258 (defvar mailcap-temporary-directory
259 (cond ((fboundp 'temp-directory) (temp-directory))
260 ((boundp 'temporary-file-directory) temporary-file-directory)
262 "*Where temporary files go.")
265 ;;; Utility functions
268 (defun mailcap-generate-unique-filename (&optional fmt)
269 "Generate a unique filename in mailcap-temporary-directory"
271 (let ((base (format "mailcap-tmp.%d" (user-real-uid)))
274 (setq fname (format "%s%d" base x))
275 (while (file-exists-p
276 (expand-file-name fname mailcap-temporary-directory))
278 fname (concat base (int-to-string x))))
279 (expand-file-name fname mailcap-temporary-directory))
280 (let ((base (concat "mm" (int-to-string (user-real-uid))))
283 (setq fname (format fmt (concat base (int-to-string x))))
284 (while (file-exists-p
285 (expand-file-name fname mailcap-temporary-directory))
287 fname (format fmt (concat base (int-to-string x)))))
288 (expand-file-name fname mailcap-temporary-directory))))
290 (defun mailcap-save-binary-file ()
291 (goto-char (point-min))
293 (let ((file (read-file-name
294 "Filename to save as: "
295 (or mailcap-download-directory "~/")))
296 (require-final-newline nil))
297 (write-region (point-min) (point-max) file))
298 (kill-buffer (current-buffer))))
300 (defun mailcap-maybe-eval ()
301 "Maybe evaluate a buffer of emacs lisp code"
302 (if (yes-or-no-p "This is emacs-lisp code, evaluate it? ")
303 (eval-buffer (current-buffer))
307 ;;; The mailcap parser
310 (defun mailcap-replace-regexp (regexp to-string)
311 ;; Quiet replace-regexp.
312 (goto-char (point-min))
313 (while (re-search-forward regexp nil t)
314 (replace-match to-string t nil)))
316 (defvar mailcap-parsed-p nil)
318 (defun mailcap-parse-mailcaps (&optional path force)
319 "Parse out all the mailcaps specified in a unix-style path string PATH.
320 If FORCE, re-parse even if already parsed."
321 (interactive (list nil t))
322 (when (or (not mailcap-parsed-p)
326 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
327 ((memq system-type '(ms-dos ms-windows windows-nt))
328 (setq path (mapconcat 'expand-file-name
329 '("~/mail.cap" "~/etc/mail.cap" "~/.mailcap")
331 (t (setq path (mapconcat 'expand-file-name
333 "/etc/mailcap:/usr/etc/mailcap"
334 "/usr/local/etc/mailcap") ":"))))
335 (let ((fnames (reverse
337 path (if (memq system-type
338 '(ms-dos ms-windows windows-nt))
343 (setq fname (car fnames))
344 (if (and (file-exists-p fname) (file-readable-p fname)
345 (file-regular-p fname))
346 (mailcap-parse-mailcap (car fnames)))
347 (setq fnames (cdr fnames))))
348 (setq mailcap-parsed-p t)))
350 (defun mailcap-parse-mailcap (fname)
351 ;; Parse out the mailcap file specified by FNAME
352 (let (major ; The major mime type (image/audio/etc)
353 minor ; The minor mime type (gif, basic, etc)
354 save-pos ; Misc saved positions used in parsing
355 viewer ; How to view this mime type
356 info ; Misc info about this mime type
359 (insert-file-contents fname)
360 (set-syntax-table mailcap-parse-args-syntax-table)
361 (mailcap-replace-regexp "#.*" "") ; Remove all comments
362 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
363 (mailcap-replace-regexp "\\\\[ \t\n]+" " ") ; And collapse spaces
364 (mailcap-replace-regexp (concat (regexp-quote "\\") "[ \t]*\n") "")
365 (goto-char (point-max))
366 (skip-chars-backward " \t\n")
367 (delete-region (point) (point-max))
368 (goto-char (point-min))
370 (skip-chars-forward " \t\n")
371 (setq save-pos (point)
373 (skip-chars-forward "^/;")
374 (downcase-region save-pos (point))
375 (setq major (buffer-substring save-pos (point)))
376 (skip-chars-forward "/ \t\n")
377 (setq save-pos (point))
378 (skip-chars-forward "^;")
379 (downcase-region save-pos (point))
382 ((eq ?* (or (char-after save-pos) 0)) ".*")
383 ((= (point) save-pos) ".*")
384 (t (buffer-substring save-pos (point)))))
385 (skip-chars-forward "; \t\n")
386 ;;; Got the major/minor chunks, now for the viewers/etc
387 ;;; The first item _must_ be a viewer, according to the
388 ;;; RFC for mailcap files (#1343)
389 (skip-chars-forward "; \t\n")
390 (setq save-pos (point))
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 (skip-chars-forward " \";\n\t")
428 (setq name-pos (point))
429 (skip-chars-forward "^ \n\t=")
430 (downcase-region name-pos (point))
431 (setq name (buffer-substring name-pos (point)))
432 (skip-chars-forward " \t\n")
433 (if (not (eq (char-after (point)) ?=)) ; There is no value
435 (skip-chars-forward " \t\n=")
436 (setq val-pos (point))
437 (if (memq (char-after val-pos) '(?\" ?'))
439 (setq val-pos (1+ val-pos))
444 (error (goto-char (point-max)))))
446 (skip-chars-forward "^;")
447 (if (eq (char-after (1- (point))) ?\\ )
449 (subst-char-in-region (1- (point)) (point) ?\\ ? )
450 (skip-chars-forward ";"))
452 (setq value (buffer-substring val-pos (point))))
453 (setq results (cons (cons name value) results)))
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 "needsx11" info))
465 (not (getenv "DISPLAY")))
468 ((and (equal (nth 0 status) "test")
469 (equal (nth 1 status) "-n")
470 (or (equal (nth 2 status) "$DISPLAY")
471 (equal (nth 2 status) "\"$DISPLAY\"")))
472 (setq status (if (getenv "DISPLAY") t nil)))
473 ((and (equal (nth 0 status) "test")
474 (equal (nth 1 status) "-z")
475 (or (equal (nth 2 status) "$DISPLAY")
476 (equal (nth 2 status) "\"$DISPLAY\"")))
477 (setq status (if (getenv "DISPLAY") nil t)))
480 (and test (listp test) (setcdr test status))))
483 ;;; The action routines.
486 (defun mailcap-possible-viewers (major minor)
487 ;; Return a list of possible viewers from MAJOR for minor type MINOR
492 ((equal (car (car major)) minor)
493 (setq exact (cons (cdr (car major)) exact)))
494 ((and minor (string-match (car (car major)) minor))
495 (setq wildcard (cons (cdr (car major)) wildcard))))
496 (setq major (cdr major)))
497 (nconc (nreverse exact) (nreverse wildcard))))
499 (defun mailcap-unescape-mime-test (test type-info)
500 (let (save-pos save-chr subst)
502 ((symbolp test) test)
503 ((and (listp test) (symbolp (car test))) test)
505 (and (listp test) (stringp (car test))
506 (setq test (mapconcat 'identity test " "))))
509 (goto-char (point-min))
511 (skip-chars-forward "^%")
513 (progn (skip-chars-backward "\\\\")
515 0) ; It is an escaped %
518 (skip-chars-forward "%."))
519 (setq save-pos (point))
520 (skip-chars-forward "%")
521 (setq save-chr (char-after (point)))
523 ((null save-chr) nil)
525 (delete-region save-pos (progn (forward-char 1) (point)))
526 (insert (or (cdr (assq 'type type-info)) "\"\"")))
528 (delete-region save-pos (progn (forward-char 1) (point)))
531 (delete-region save-pos (progn (forward-char 1) (point)))
534 (delete-region save-pos (progn (forward-char 1) (point)))
538 (skip-chars-forward "^}")
539 (downcase-region (+ 2 save-pos) (point))
540 (setq subst (buffer-substring (+ 2 save-pos) (point)))
541 (delete-region save-pos (1+ (point)))
542 (insert (or (cdr (assoc subst type-info)) "\"\"")))
545 (t (error "Bad value to mailcap-unescape-mime-test. %s" test)))))
547 (defvar mailcap-viewer-test-cache nil)
549 (defun mailcap-viewer-passes-test (viewer-info type-info)
550 ;; Return non-nil iff the viewer specified by VIEWER-INFO passes its
551 ;; test clause (if any).
552 (let* ((test-info (assq 'test viewer-info))
553 (test (cdr test-info))
555 (viewer (cdr (assoc 'viewer viewer-info)))
556 (default-directory (expand-file-name "~/"))
557 status parsed-test cache result)
558 (if (setq cache (assoc test mailcap-viewer-test-cache))
563 ((not test-info) t) ; No test clause
564 ((not test) nil) ; Already failed test
565 ((eq test t) t) ; Already passed test
566 ((and (symbolp test) ; Lisp function as test
568 (funcall test type-info))
569 ((and (symbolp test) ; Lisp variable as test
572 ((and (listp test) ; List to be eval'd
573 (symbolp (car test)))
576 (setq test (mailcap-unescape-mime-test test type-info)
577 test (list shell-file-name nil nil nil
578 shell-command-switch test)
579 status (apply 'call-process test))
581 (push (list otest result) mailcap-viewer-test-cache)
584 (defun mailcap-add-mailcap-entry (major minor info)
585 (let ((old-major (assoc major mailcap-mime-data)))
586 (if (null old-major) ; New major area
587 (setq mailcap-mime-data
588 (cons (cons major (list (cons minor info)))
590 (let ((cur-minor (assoc minor old-major)))
592 ((or (null cur-minor) ; New minor area, or
593 (assq 'test info)) ; Has a test, insert at beginning
594 (setcdr old-major (cons (cons minor info) (cdr old-major))))
595 ((and (not (assq 'test info)) ; No test info, replace completely
596 (not (assq 'test cur-minor)))
597 (setcdr cur-minor info))
599 (setcdr old-major (cons (cons minor info) (cdr old-major)))))))))
601 (defun mailcap-add (type viewer &optional test)
602 "Add VIEWER as a handler for TYPE.
603 If TEST is not given, it defaults to t."
604 (let ((tl (split-string type "/")))
605 (when (or (not (car tl))
607 (error "%s is not a valid MIME type" type))
608 (mailcap-add-mailcap-entry
611 (test . ,(if test test t))
618 (defun mailcap-viewer-lessp (x y)
619 ;; Return t iff viewer X is more desirable than viewer Y
620 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
621 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
622 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
623 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
625 ((and x-lisp (not y-lisp))
627 ((and (not y-lisp) x-wild (not y-wild))
629 ((and (not x-wild) y-wild)
633 (defun mailcap-mime-info (string &optional request)
634 "Get the MIME viewer command for STRING, return nil if none found.
635 Expects a complete content-type header line as its argument.
637 Second argument REQUEST specifies what information to return. If it is
638 nil or the empty string, the viewer (second field of the mailcap
639 entry) will be returned. If it is a string, then the mailcap field
640 corresponding to that string will be returned (print, description,
641 whatever). If a number, then all the information for this specific
642 viewer is returned. If `all', then all possible viewers for
643 this type is returned."
645 major ; Major encoding (text, etc)
646 minor ; Minor encoding (html, etc)
648 save-pos ; Misc. position during parse
649 major-info ; (assoc major mailcap-mime-data)
650 minor-info ; (assoc minor major-info)
651 test ; current test proc.
652 viewers ; Possible viewers
653 passed ; Viewers that passed the test
654 viewer ; The one and only viewer
657 (setq ctl (mail-header-parse-content-type (or string "text/plain")))
658 (setq major (split-string (car ctl) "/"))
659 (setq minor (cadr major)
661 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
662 (when (setq viewers (mailcap-possible-viewers major-info minor))
663 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
667 (if (mailcap-viewer-passes-test (car viewers) info)
668 (setq passed (cons (car viewers) passed)))
669 (setq viewers (cdr viewers)))
670 (setq passed (sort (nreverse passed) 'mailcap-viewer-lessp))
671 (setq viewer (car passed))))
672 (when (and (stringp (cdr (assq 'viewer viewer)))
674 (setq viewer (car passed)))
676 ((and (null viewer) (not (equal major "default")) request)
677 (mailcap-mime-info "default" request))
678 ((or (null request) (equal request ""))
679 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
681 (if (or (eq request 'test) (eq request 'viewer))
682 (mailcap-unescape-mime-test
683 (cdr-safe (assoc request viewer)) info)))
687 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
688 (setq viewer (copy-tree viewer))
689 (let ((view (assq 'viewer viewer))
690 (test (assq 'test viewer)))
691 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
692 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
696 ;;; Experimental MIME-types parsing
699 (defvar mailcap-mime-extensions
700 '(("" . "text/plain")
701 (".abs" . "audio/x-mpeg")
702 (".aif" . "audio/aiff")
703 (".aifc" . "audio/aiff")
704 (".aiff" . "audio/aiff")
705 (".ano" . "application/x-annotator")
706 (".au" . "audio/ulaw")
707 (".avi" . "video/x-msvideo")
708 (".bcpio" . "application/x-bcpio")
709 (".bin" . "application/octet-stream")
710 (".cdf" . "application/x-netcdr")
711 (".cpio" . "application/x-cpio")
712 (".csh" . "application/x-csh")
713 (".dvi" . "application/x-dvi")
714 (".el" . "application/emacs-lisp")
715 (".eps" . "application/postscript")
716 (".etx" . "text/x-setext")
717 (".exe" . "application/octet-stream")
718 (".fax" . "image/x-fax")
719 (".gif" . "image/gif")
720 (".hdf" . "application/x-hdf")
721 (".hqx" . "application/mac-binhex40")
722 (".htm" . "text/html")
723 (".html" . "text/html")
724 (".icon" . "image/x-icon")
725 (".ief" . "image/ief")
726 (".jpg" . "image/jpeg")
727 (".macp" . "image/x-macpaint")
728 (".man" . "application/x-troff-man")
729 (".me" . "application/x-troff-me")
730 (".mif" . "application/mif")
731 (".mov" . "video/quicktime")
732 (".movie" . "video/x-sgi-movie")
733 (".mp2" . "audio/x-mpeg")
734 (".mp3" . "audio/x-mpeg")
735 (".mp2a" . "audio/x-mpeg2")
736 (".mpa" . "audio/x-mpeg")
737 (".mpa2" . "audio/x-mpeg2")
738 (".mpe" . "video/mpeg")
739 (".mpeg" . "video/mpeg")
740 (".mpega" . "audio/x-mpeg")
741 (".mpegv" . "video/mpeg")
742 (".mpg" . "video/mpeg")
743 (".mpv" . "video/mpeg")
744 (".ms" . "application/x-troff-ms")
745 (".nc" . "application/x-netcdf")
746 (".nc" . "application/x-netcdf")
747 (".oda" . "application/oda")
748 (".patch" . "application/x-patch")
749 (".pbm" . "image/x-portable-bitmap")
750 (".pdf" . "application/pdf")
751 (".pgm" . "image/portable-graymap")
752 (".pict" . "image/pict")
753 (".png" . "image/png")
754 (".pnm" . "image/x-portable-anymap")
755 (".ppm" . "image/portable-pixmap")
756 (".ps" . "application/postscript")
757 (".qt" . "video/quicktime")
758 (".ras" . "image/x-raster")
759 (".rgb" . "image/x-rgb")
760 (".rtf" . "application/rtf")
761 (".rtx" . "text/richtext")
762 (".sh" . "application/x-sh")
763 (".sit" . "application/x-stuffit")
764 (".snd" . "audio/basic")
765 (".src" . "application/x-wais-source")
766 (".tar" . "archive/tar")
767 (".tcl" . "application/x-tcl")
768 (".tcl" . "application/x-tcl")
769 (".tex" . "application/x-tex")
770 (".texi" . "application/texinfo")
771 (".tga" . "image/x-targa")
772 (".tif" . "image/tiff")
773 (".tiff" . "image/tiff")
774 (".tr" . "application/x-troff")
775 (".troff" . "application/x-troff")
776 (".tsv" . "text/tab-separated-values")
777 (".txt" . "text/plain")
778 (".vbs" . "video/mpeg")
779 (".vox" . "audio/basic")
780 (".vrml" . "x-world/x-vrml")
781 (".wav" . "audio/x-wav")
782 (".wrl" . "x-world/x-vrml")
783 (".xbm" . "image/xbm")
784 (".xpm" . "image/xpm")
785 (".xwd" . "image/windowdump")
786 (".zip" . "application/zip")
787 (".ai" . "application/postscript")
788 (".jpe" . "image/jpeg")
789 (".jpeg" . "image/jpeg"))
790 "An assoc list of file extensions and corresponding MIME content-types.")
792 (defun mailcap-parse-mimetypes (&optional path)
793 ;; Parse out all the mimetypes specified in a unix-style path string PATH
796 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
797 ((memq system-type '(ms-dos ms-windows windows-nt))
798 (setq path (mapconcat 'expand-file-name
799 '("~/mime.typ" "~/etc/mime.typ") ";")))
800 (t (setq path (mapconcat 'expand-file-name
802 "/etc/mime-types:/usr/etc/mime-types"
803 "/usr/local/etc/mime-types"
804 "/usr/local/www/conf/mime-types") ":"))))
805 (let ((fnames (reverse
807 (if (memq system-type
808 '(ms-dos ms-windows windows-nt))
812 (setq fname (car fnames))
813 (if (and (file-exists-p fname) (file-readable-p fname))
814 (mailcap-parse-mimetype-file (car fnames)))
815 (setq fnames (cdr fnames)))))
817 (defun mailcap-parse-mimetype-file (fname)
818 ;; Parse out a mime-types file
819 (let (type ; The MIME type for this line
820 extns ; The extensions for this line
821 save-pos ; Misc. saved buffer positions
824 (insert-file-contents fname)
825 (mailcap-replace-regexp "#.*" "")
826 (mailcap-replace-regexp "\n+" "\n")
827 (mailcap-replace-regexp "[ \t]+$" "")
828 (goto-char (point-max))
829 (skip-chars-backward " \t\n")
830 (delete-region (point) (point-max))
831 (goto-char (point-min))
833 (skip-chars-forward " \t\n")
834 (setq save-pos (point))
835 (skip-chars-forward "^ \t")
836 (downcase-region save-pos (point))
837 (setq type (buffer-substring save-pos (point)))
839 (skip-chars-forward " \t")
840 (setq save-pos (point))
841 (skip-chars-forward "^ \t\n")
842 (setq extns (cons (buffer-substring save-pos (point)) extns)))
844 (setq mailcap-mime-extensions
846 (cons (if (= (string-to-char (car extns)) ?.)
848 (concat "." (car extns))) type)
849 mailcap-mime-extensions)
850 extns (cdr extns)))))))
852 (defun mailcap-extension-to-mime (extn)
853 "Return the MIME content type of the file extensions EXTN."
854 (if (and (stringp extn)
855 (not (eq (string-to-char extn) ?.)))
856 (setq extn (concat "." extn)))
857 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
859 (defvar mailcap-binary-suffixes
860 (if (memq system-type '(ms-dos windows-nt))
861 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
864 (defun mailcap-command-p (command)
865 "Say whether COMMAND is in the exec path.
866 The path of COMMAND will be returned iff COMMAND is a command."
867 (let ((path (if (file-name-absolute-p command) '(nil) exec-path))
870 (while (setq dir (pop path))
871 (let ((suffixes mailcap-binary-suffixes))
873 (when (and (file-executable-p
874 (setq file (expand-file-name
875 (concat command (pop suffixes))
877 (not (file-directory-p file)))
878 (throw 'found file))))))))
882 ;;; mailcap.el ends here