1 ;;; mailcap.el --- Functions for displaying MIME parts
2 ;; Copyright (C) 1998 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" . mailcap-save-binary-file)
46 ("type" . "multipart/*")))
49 ("viewer" . ssl-view-site-cert)
50 ("test" . (fboundp 'ssl-view-site-cert))
51 ("type" . "application/x-x509-ca-cert"))
53 ("viewer" . ssl-view-user-cert)
54 ("test" . (fboundp 'ssl-view-user-cert))
55 ("type" . "application/x-x509-user-cert"))
57 ("viewer" . mailcap-save-binary-file)
58 ("type" ."application/octet-stream"))
60 ("viewer" . "open %s")
61 ("type" . "application/dvi")
62 ("test" . (eq (mm-device-type) 'ns)))
64 ("viewer" . "xdvi %s")
65 ("test" . (eq (mm-device-type) 'x))
67 ("type" . "application/dvi"))
69 ("viewer" . "dvitty %s")
70 ("test" . (not (getenv "DISPLAY")))
71 ("type" . "application/dvi"))
73 ("viewer" . mailcap-maybe-eval)
74 ("type" . "application/emacs-lisp"))
76 ("viewer" . mailcap-save-binary-file)
77 ("type" . "application/x-tar"))
80 ("test" . (fboundp 'tex-mode))
81 ("type" . "application/x-latex"))
84 ("test" . (fboundp 'tex-mode))
85 ("type" . "application/x-tex"))
88 ("test" . (fboundp 'tex-mode))
89 ("type" . "application/latex"))
92 ("test" . (fboundp 'tex-mode))
93 ("type" . "application/tex"))
95 ("viewer" . texinfo-mode)
96 ("test" . (fboundp 'texinfo-mode))
97 ("type" . "application/tex"))
99 ("viewer" . mailcap-save-binary-file)
100 ("type" . "application/zip")
103 ("viewer" . "acroread %s")
104 ("type" . "application/pdf"))
106 ("viewer" . "open %s")
107 ("type" . "application/postscript")
108 ("test" . (eq (mm-device-type) 'ns)))
110 ("viewer" . "ghostview %s")
111 ("type" . "application/postscript")
112 ("test" . (eq (mm-device-type) 'x))
115 ("viewer" . "ps2ascii %s")
116 ("type" . "application/postscript")
117 ("test" . (not (getenv "DISPLAY")))
121 ("viewer" . "maplay %s")
122 ("type" . "audio/x-mpeg"))
124 ("viewer" . mailcap-play-sound-file)
125 ("test" . (or (featurep 'nas-sound)
126 (featurep 'native-sound)))
127 ("type" . "audio/*"))
129 ("viewer" . "showaudio")
130 ("type" . "audio/*")))
134 ("test" . (fboundp 'vm-mode))
135 ("type" . "message/rfc-822"))
138 ("test" . (fboundp 'w3-mode))
139 ("type" . "message/rfc-822"))
141 ("viewer" . view-mode)
142 ("test" . (fboundp 'view-mode))
143 ("type" . "message/rfc-822"))
145 ("viewer" . fundamental-mode)
146 ("type" . "message/rfc-822")))
149 ("viewer" . "xwud -in %s")
150 ("type" . "image/x-xwd")
151 ("compose" . "xwd -frame > %s")
152 ("test" . (eq (mm-device-type) 'x))
155 ("viewer" . "xwud -in %s")
156 ("type" . "image/x-xwd")
157 ("compose" . "xwd -frame > %s")
158 ("test" . (eq (mm-device-type) 'x))
161 ("viewer" . "xwud -in %s")
162 ("type" . "image/x-xwd")
163 ("compose" . "xwd -frame > %s")
164 ("test" . (eq (mm-device-type) 'x))
167 ("viewer" . "aopen %s")
169 ("test" . (eq (mm-device-type) 'ns)))
171 ("viewer" . "xv -perfect %s")
173 ("test" . (eq (mm-device-type) 'x))
178 ("test" . (fboundp 'w3-mode))
179 ("type" . "text/plain"))
181 ("viewer" . view-mode)
182 ("test" . (fboundp 'view-mode))
183 ("type" . "text/plain"))
185 ("viewer" . fundamental-mode)
186 ("type" . "text/plain"))
188 ("viewer" . enriched-decode-region)
189 ("test" . (fboundp 'enriched-decode-region))
190 ("type" . "text/enriched"))
192 ("viewer" . w3-prepare-buffer)
193 ("test" . (fboundp 'w3-prepare-buffer))
194 ("type" . "text/html")))
197 ("viewer" . "mpeg_play %s")
198 ("type" . "video/mpeg")
199 ("test" . (eq (mm-device-type) 'x))
203 ("viewer" . "webspace -remote %s -URL %u")
204 ("type" . "x-world/x-vrml")
209 ("viewer" . tar-mode)
210 ("type" . "archive/tar")
211 ("test" . (fboundp 'tar-mode)))))
212 "*The mailcap structure is an assoc list of assoc lists.
213 1st assoc list is keyed on the major content-type
214 2nd assoc list is keyed on the minor content-type (which can be a regexp)
219 (\"postscript\" . <info>))
221 (\"plain\" . <info>)))
223 Where <info> is another assoc list of the various information
224 related to the mailcap RFC. This is keyed on the lowercase
225 attribute name (viewer, test, etc). This looks like:
226 ((\"viewer\" . viewerinfo)
227 (\"test\" . testinfo)
228 (\"xxxx\" . \"string\"))
230 Where viewerinfo specifies how the content-type is viewed. Can be
231 a string, in which case it is run through a shell, with
232 appropriate parameters, or a symbol, in which case the symbol is
233 funcall'd, with the buffer as an argument.
235 testinfo is a list of strings, or nil. If nil, it means the
236 viewer specified is always valid. If it is a list of strings,
237 these are used to determine whether a viewer passes the 'test' or
240 (defvar mailcap-download-directory nil
241 "*Where downloaded files should go by default.")
243 (defvar mailcap-temporary-directory (or (getenv "TMPDIR") "/tmp")
244 "*Where temporary files go.")
247 ;;; Utility functions
250 (defun mailcap-generate-unique-filename (&optional fmt)
251 "Generate a unique filename in mailcap-temporary-directory"
253 (let ((base (format "mailcap-tmp.%d" (user-real-uid)))
256 (setq fname (format "%s%d" base x))
257 (while (file-exists-p
258 (expand-file-name fname mailcap-temporary-directory))
260 fname (concat base (int-to-string x))))
261 (expand-file-name fname mailcap-temporary-directory))
262 (let ((base (concat "mm" (int-to-string (user-real-uid))))
265 (setq fname (format fmt (concat base (int-to-string x))))
266 (while (file-exists-p
267 (expand-file-name fname mailcap-temporary-directory))
269 fname (format fmt (concat base (int-to-string x)))))
270 (expand-file-name fname mailcap-temporary-directory))))
272 (defun mailcap-save-binary-file ()
273 ;; Ok, this is truly fucked. In XEmacs, if you use the mouse to select
274 ;; a URL that gets saved via this function, read-file-name will pop up a
275 ;; dialog box for file selection. For some reason which buffer we are in
276 ;; gets royally screwed (even with save-excursions and the whole nine
277 ;; yards). SO, we just keep the old buffer name around and away we go.
278 (let ((old-buff (current-buffer))
279 (file (read-file-name "Filename to save as: "
280 (or mailcap-download-directory "~/")
281 (file-name-nondirectory (url-view-url t))
283 (file-name-nondirectory (url-view-url t))))
284 (require-final-newline nil))
285 (set-buffer old-buff)
286 (mule-write-region-no-coding-system (point-min) (point-max) file)
287 (kill-buffer (current-buffer))))
289 (defun mailcap-maybe-eval ()
290 "Maybe evaluate a buffer of emacs lisp code"
291 (if (yes-or-no-p "This is emacs-lisp code, evaluate it? ")
292 (eval-buffer (current-buffer))
296 ;;; The mailcap parser
299 (defun mailcap-replace-regexp (regexp to-string)
300 ;; Quiet replace-regexp.
301 (goto-char (point-min))
302 (while (re-search-forward regexp nil t)
303 (replace-match to-string t nil)))
305 (defvar mailcap-parsed-p nil)
307 (defun mailcap-parse-mailcaps (&optional path force)
308 "Parse out all the mailcaps specified in a unix-style path string PATH.
309 If FORCE, re-parse even if already parsed."
310 (when (or (not mailcap-parsed-p)
314 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
315 ((memq system-type '(ms-dos ms-windows windows-nt))
316 (setq path (mapconcat 'expand-file-name '("~/mail.cap" "~/etc/mail.cap")
318 (t (setq path (mapconcat 'expand-file-name
320 "/etc/mailcap:/usr/etc/mailcap"
321 "/usr/local/etc/mailcap") ":"))))
322 (let ((fnames (reverse
324 path (if (memq system-type
325 '(ms-dos ms-windows windows-nt))
330 (setq fname (car fnames))
331 (if (and (file-exists-p fname) (file-readable-p fname))
332 (mailcap-parse-mailcap (car fnames)))
333 (setq fnames (cdr fnames))))
334 (setq mailcap-parsed-p t)))
336 (defun mailcap-parse-mailcap (fname)
337 ;; Parse out the mailcap file specified by FNAME
338 (let (major ; The major mime type (image/audio/etc)
339 minor ; The minor mime type (gif, basic, etc)
340 save-pos ; Misc saved positions used in parsing
341 viewer ; How to view this mime type
342 info ; Misc info about this mime type
345 (insert-file-contents fname)
346 (set-syntax-table mailcap-parse-args-syntax-table)
347 (mailcap-replace-regexp "#.*" "") ; Remove all comments
348 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
349 (mailcap-replace-regexp "\\\\[ \t\n]+" " ") ; And collapse spaces
350 (mailcap-replace-regexp (concat (regexp-quote "\\") "[ \t]*\n") "")
351 (goto-char (point-max))
352 (skip-chars-backward " \t\n")
353 (delete-region (point) (point-max))
354 (goto-char (point-min))
356 (skip-chars-forward " \t\n")
357 (setq save-pos (point)
359 (skip-chars-forward "^/;")
360 (downcase-region save-pos (point))
361 (setq major (buffer-substring save-pos (point)))
362 (skip-chars-forward "/ \t\n")
363 (setq save-pos (point))
364 (skip-chars-forward "^;")
365 (downcase-region save-pos (point))
368 ((= ?* (or (char-after save-pos) 0)) ".*")
369 ((= (point) save-pos) ".*")
370 (t (buffer-substring save-pos (point)))))
371 (skip-chars-forward "; \t\n")
372 ;;; Got the major/minor chunks, now for the viewers/etc
373 ;;; The first item _must_ be a viewer, according to the
374 ;;; RFC for mailcap files (#1343)
375 (skip-chars-forward "; \t\n")
376 (setq save-pos (point))
377 (skip-chars-forward "^;\n")
378 (if (= (or (char-after save-pos) 0) ?')
380 (narrow-to-region (1+ save-pos) (point))
381 (goto-char (point-min))
383 (read (current-buffer))
384 (goto-char (point-max))
386 (setq viewer (buffer-substring save-pos (point))))
387 (setq save-pos (point))
389 (setq info (nconc (list (cons "viewer" viewer)
390 (cons "type" (concat major "/"
391 (if (string= minor ".*")
393 (mailcap-parse-mailcap-extras save-pos (point))))
394 (mailcap-mailcap-entry-passes-test info)
395 (mailcap-add-mailcap-entry major minor info)))))
397 (defun mailcap-parse-mailcap-extras (st nd)
398 ;; Grab all the extra stuff from a mailcap entry
402 results ; Assoc list of results
403 name-pos ; Start of XXXX= position
404 val-pos ; Start of value position
405 done ; Found end of \'d ;s?
408 (narrow-to-region st nd)
409 (goto-char (point-min))
410 (skip-chars-forward " \n\t;")
413 (skip-chars-forward " \";\n\t")
414 (setq name-pos (point))
415 (skip-chars-forward "^ \n\t=")
416 (downcase-region name-pos (point))
417 (setq name (buffer-substring name-pos (point)))
418 (skip-chars-forward " \t\n")
419 (if (/= (or (char-after (point)) 0) ?=) ; There is no value
421 (skip-chars-forward " \t\n=")
422 (setq val-pos (point))
423 (if (memq (char-after val-pos) '(?\" ?'))
425 (setq val-pos (1+ val-pos))
430 (error (goto-char (point-max)))))
432 (skip-chars-forward "^;")
433 (if (= (or (char-after (1- (point))) 0) ?\\ )
435 (subst-char-in-region (1- (point)) (point) ?\\ ? )
436 (skip-chars-forward ";"))
438 (setq value (buffer-substring val-pos (point))))
439 (setq results (cons (cons name value) results)))
442 (defun mailcap-mailcap-entry-passes-test (info)
443 ;; Return t iff a mailcap entry passes its test clause or no test
444 ;; clause is present.
445 (let (status ; Call-process-regions return value
446 (test (assoc "test" info)) ; The test clause
448 (setq status (and test (split-string (cdr test) " ")))
449 (if (and (assoc "needsx11" info) (not (getenv "DISPLAY")))
452 ((and (equal (nth 0 status) "test")
453 (equal (nth 1 status) "-n")
454 (or (equal (nth 2 status) "$DISPLAY")
455 (equal (nth 2 status) "\"$DISPLAY\"")))
456 (setq status (if (getenv "DISPLAY") t nil)))
457 ((and (equal (nth 0 status) "test")
458 (equal (nth 1 status) "-z")
459 (or (equal (nth 2 status) "$DISPLAY")
460 (equal (nth 2 status) "\"$DISPLAY\"")))
461 (setq status (if (getenv "DISPLAY") nil t)))
464 (and test (listp test) (setcdr test status))))
467 ;;; The action routines.
470 (defun mailcap-possible-viewers (major minor)
471 ;; Return a list of possible viewers from MAJOR for minor type MINOR
476 ((equal (car (car major)) minor)
477 (setq exact (cons (cdr (car major)) exact)))
478 ((string-match (car (car major)) minor)
479 (setq wildcard (cons (cdr (car major)) wildcard))))
480 (setq major (cdr major)))
481 (nconc (nreverse exact) (nreverse wildcard))))
483 (defun mailcap-unescape-mime-test (test type-info)
484 (let (save-pos save-chr subst)
486 ((symbolp test) test)
487 ((and (listp test) (symbolp (car test))) test)
489 (and (listp test) (stringp (car test))
490 (setq test (mapconcat 'identity test " "))))
493 (goto-char (point-min))
495 (skip-chars-forward "^%")
497 (progn (skip-chars-backward "\\\\")
499 0) ; It is an escaped %
502 (skip-chars-forward "%."))
503 (setq save-pos (point))
504 (skip-chars-forward "%")
505 (setq save-chr (char-after (point)))
507 ((null save-chr) nil)
509 (delete-region save-pos (progn (forward-char 1) (point)))
510 (insert (or (cdr (assoc "type" type-info)) "\"\"")))
512 (delete-region save-pos (progn (forward-char 1) (point)))
515 (delete-region save-pos (progn (forward-char 1) (point)))
518 (delete-region save-pos (progn (forward-char 1) (point)))
522 (skip-chars-forward "^}")
523 (downcase-region (+ 2 save-pos) (point))
524 (setq subst (buffer-substring (+ 2 save-pos) (point)))
525 (delete-region save-pos (1+ (point)))
526 (insert (or (cdr (assoc subst type-info)) "\"\"")))
529 (t (error "Bad value to mailcap-unescape-mime-test. %s" test)))))
531 (defvar mailcap-viewer-test-cache nil)
533 (defun mailcap-viewer-passes-test (viewer-info type-info)
534 ;; Return non-nil iff the viewer specified by VIEWER-INFO passes its
535 ;; test clause (if any).
536 (let* ((test-info (assoc "test" viewer-info))
537 (test (cdr test-info))
539 (viewer (cdr (assoc "viewer" viewer-info)))
540 (default-directory (expand-file-name "~/"))
541 status parsed-test cache result)
542 (if (setq cache (assoc test mailcap-viewer-test-cache))
547 ((not test-info) t) ; No test clause
548 ((not test) nil) ; Already failed test
549 ((eq test t) t) ; Already passed test
550 ((and (symbolp test) ; Lisp function as test
552 (funcall test type-info))
553 ((and (symbolp test) ; Lisp variable as test
556 ((and (listp test) ; List to be eval'd
557 (symbolp (car test)))
560 (setq test (mailcap-unescape-mime-test test type-info)
561 test (list shell-file-name nil nil nil
562 shell-command-switch test)
563 status (apply 'call-process test))
565 (push (list otest result) mailcap-viewer-test-cache)
568 (defun mailcap-add-mailcap-entry (major minor info)
569 (let ((old-major (assoc major mailcap-mime-data)))
570 (if (null old-major) ; New major area
571 (setq mailcap-mime-data
572 (cons (cons major (list (cons minor info)))
574 (let ((cur-minor (assoc minor old-major)))
576 ((or (null cur-minor) ; New minor area, or
577 (assoc "test" info)) ; Has a test, insert at beginning
578 (setcdr old-major (cons (cons minor info) (cdr old-major))))
579 ((and (not (assoc "test" info)) ; No test info, replace completely
580 (not (assoc "test" cur-minor)))
581 (setcdr cur-minor info))
583 (setcdr old-major (cons (cons minor info) (cdr old-major)))))))))
589 (defun mailcap-viewer-lessp (x y)
590 ;; Return t iff viewer X is more desirable than viewer Y
591 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assoc "type" x)) "")))
592 (y-wild (string-match "[*?]" (or (cdr-safe (assoc "type" y)) "")))
593 (x-lisp (not (stringp (or (cdr-safe (assoc "viewer" x)) ""))))
594 (y-lisp (not (stringp (or (cdr-safe (assoc "viewer" y)) "")))))
596 ((and x-lisp (not y-lisp))
598 ((and (not y-lisp) x-wild (not y-wild))
600 ((and (not x-wild) y-wild)
604 (defun mailcap-mime-info (string &optional request)
605 "Get the MIME viewer command for STRING, return nil if none found.
606 Expects a complete content-type header line as its argument.
608 Second argument REQUEST specifies what information to return. If it is
609 nil or the empty string, the viewer (second field of the mailcap
610 entry) will be returned. If it is a string, then the mailcap field
611 corresponding to that string will be returned (print, description,
612 whatever). If a number, then all the information for this specific
613 viewer is returned. If `all', then all possible viewers for
614 this type is returned."
616 major ; Major encoding (text, etc)
617 minor ; Minor encoding (html, etc)
619 save-pos ; Misc. position during parse
620 major-info ; (assoc major mailcap-mime-data)
621 minor-info ; (assoc minor major-info)
622 test ; current test proc.
623 viewers ; Possible viewers
624 passed ; Viewers that passed the test
625 viewer ; The one and only viewer
628 (setq ctl (drums-parse-content-type (or string "text/plain")))
629 (setq major (split-string (car ctl) "/"))
630 (setq minor (cadr major)
632 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
633 (when (setq viewers (mailcap-possible-viewers major-info minor))
634 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
638 (if (mailcap-viewer-passes-test (car viewers) info)
639 (setq passed (cons (car viewers) passed)))
640 (setq viewers (cdr viewers)))
641 (setq passed (sort (nreverse passed) 'mailcap-viewer-lessp))
642 (setq viewer (car passed))))
643 (when (and (stringp (cdr (assoc "viewer" viewer)))
645 (setq viewer (car passed)))
647 ((and (null viewer) (not (equal major "default")))
648 (mailcap-mime-info "default" request))
649 ((or (null request) (equal request ""))
650 (mailcap-unescape-mime-test (cdr (assoc "viewer" viewer)) info))
652 (if (or (string= request "test") (string= request "viewer"))
653 (mailcap-unescape-mime-test
654 (cdr-safe (assoc request viewer)) info)))
658 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
659 (setq viewer (copy-tree viewer))
660 (let ((view (assoc "viewer" viewer))
661 (test (assoc "test" viewer)))
662 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
663 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
667 ;;; Experimental MIME-types parsing
670 (defvar mailcap-mime-extensions
671 '(("" . "text/plain")
672 (".abs" . "audio/x-mpeg")
673 (".aif" . "audio/aiff")
674 (".aifc" . "audio/aiff")
675 (".aiff" . "audio/aiff")
676 (".ano" . "application/x-annotator")
677 (".au" . "audio/ulaw")
678 (".avi" . "video/x-msvideo")
679 (".bcpio" . "application/x-bcpio")
680 (".bin" . "application/octet-stream")
681 (".cdf" . "application/x-netcdr")
682 (".cpio" . "application/x-cpio")
683 (".csh" . "application/x-csh")
684 (".dvi" . "application/x-dvi")
685 (".el" . "application/emacs-lisp")
686 (".eps" . "application/postscript")
687 (".etx" . "text/x-setext")
688 (".exe" . "application/octet-stream")
689 (".fax" . "image/x-fax")
690 (".gif" . "image/gif")
691 (".hdf" . "application/x-hdf")
692 (".hqx" . "application/mac-binhex40")
693 (".htm" . "text/html")
694 (".html" . "text/html")
695 (".icon" . "image/x-icon")
696 (".ief" . "image/ief")
697 (".jpg" . "image/jpeg")
698 (".macp" . "image/x-macpaint")
699 (".man" . "application/x-troff-man")
700 (".me" . "application/x-troff-me")
701 (".mif" . "application/mif")
702 (".mov" . "video/quicktime")
703 (".movie" . "video/x-sgi-movie")
704 (".mp2" . "audio/x-mpeg")
705 (".mp3" . "audio/x-mpeg")
706 (".mp2a" . "audio/x-mpeg2")
707 (".mpa" . "audio/x-mpeg")
708 (".mpa2" . "audio/x-mpeg2")
709 (".mpe" . "video/mpeg")
710 (".mpeg" . "video/mpeg")
711 (".mpega" . "audio/x-mpeg")
712 (".mpegv" . "video/mpeg")
713 (".mpg" . "video/mpeg")
714 (".mpv" . "video/mpeg")
715 (".ms" . "application/x-troff-ms")
716 (".nc" . "application/x-netcdf")
717 (".nc" . "application/x-netcdf")
718 (".oda" . "application/oda")
719 (".pbm" . "image/x-portable-bitmap")
720 (".pdf" . "application/pdf")
721 (".pgm" . "image/portable-graymap")
722 (".pict" . "image/pict")
723 (".png" . "image/png")
724 (".pnm" . "image/x-portable-anymap")
725 (".ppm" . "image/portable-pixmap")
726 (".ps" . "application/postscript")
727 (".qt" . "video/quicktime")
728 (".ras" . "image/x-raster")
729 (".rgb" . "image/x-rgb")
730 (".rtf" . "application/rtf")
731 (".rtx" . "text/richtext")
732 (".sh" . "application/x-sh")
733 (".sit" . "application/x-stuffit")
734 (".snd" . "audio/basic")
735 (".src" . "application/x-wais-source")
736 (".tar" . "archive/tar")
737 (".tcl" . "application/x-tcl")
738 (".tcl" . "application/x-tcl")
739 (".tex" . "application/x-tex")
740 (".texi" . "application/texinfo")
741 (".tga" . "image/x-targa")
742 (".tif" . "image/tiff")
743 (".tiff" . "image/tiff")
744 (".tr" . "application/x-troff")
745 (".troff" . "application/x-troff")
746 (".tsv" . "text/tab-separated-values")
747 (".txt" . "text/plain")
748 (".vbs" . "video/mpeg")
749 (".vox" . "audio/basic")
750 (".vrml" . "x-world/x-vrml")
751 (".wav" . "audio/x-wav")
752 (".wrl" . "x-world/x-vrml")
753 (".xbm" . "image/xbm")
754 (".xpm" . "image/x-pixmap")
755 (".xwd" . "image/windowdump")
756 (".zip" . "application/zip")
757 (".ai" . "application/postscript")
758 (".jpe" . "image/jpeg")
759 (".jpeg" . "image/jpeg"))
760 "*An assoc list of file extensions and the MIME content-types they
763 (defun mailcap-parse-mimetypes (&optional path)
764 ;; Parse out all the mimetypes specified in a unix-style path string PATH
767 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
768 ((memq system-type '(ms-dos ms-windows windows-nt))
769 (setq path (mapconcat 'expand-file-name
770 '("~/mime.typ" "~/etc/mime.typ") ";")))
771 (t (setq path (mapconcat 'expand-file-name
773 "/etc/mime-types:/usr/etc/mime-types"
774 "/usr/local/etc/mime-types"
775 "/usr/local/www/conf/mime-types") ":"))))
776 (let ((fnames (reverse
778 (if (memq system-type
779 '(ms-dos ms-windows windows-nt))
783 (setq fname (car fnames))
784 (if (and (file-exists-p fname) (file-readable-p fname))
785 (mailcap-parse-mimetype-file (car fnames)))
786 (setq fnames (cdr fnames)))))
788 (defun mailcap-parse-mimetype-file (fname)
789 ;; Parse out a mime-types file
790 (let (type ; The MIME type for this line
791 extns ; The extensions for this line
792 save-pos ; Misc. saved buffer positions
795 (insert-file-contents fname)
796 (mailcap-replace-regexp "#.*" "")
797 (mailcap-replace-regexp "\n+" "\n")
798 (mailcap-replace-regexp "[ \t]+$" "")
799 (goto-char (point-max))
800 (skip-chars-backward " \t\n")
801 (delete-region (point) (point-max))
802 (goto-char (point-min))
804 (skip-chars-forward " \t\n")
805 (setq save-pos (point))
806 (skip-chars-forward "^ \t")
807 (downcase-region save-pos (point))
808 (setq type (buffer-substring save-pos (point)))
810 (skip-chars-forward " \t")
811 (setq save-pos (point))
812 (skip-chars-forward "^ \t\n")
813 (setq extns (cons (buffer-substring save-pos (point)) extns)))
815 (setq mailcap-mime-extensions
817 (cons (if (= (string-to-char (car extns)) ?.)
819 (concat "." (car extns))) type)
820 mailcap-mime-extensions)
821 extns (cdr extns)))))))
823 (defun mailcap-extension-to-mime (extn)
824 "Return the MIME content type of the file extensions EXTN."
825 (if (and (stringp extn)
826 (not (eq (string-to-char extn) ?.)))
827 (setq extn (concat "." extn)))
828 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
832 ;;; mailcap.el ends here