(msb-mode): Drop unneeded positional args.
[emacs.git] / lisp / gnus / mailcap.el
blob081cf60d735979baa24a9b1498ee5ccd2aeaf930
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 ;; Maintainer: bugs@gnus.org
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; Code:
30 (eval-when-compile (require 'cl))
31 (require 'mail-parse)
32 (require 'mm-util)
34 (defvar mailcap-parse-args-syntax-table
35 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
36 (modify-syntax-entry ?' "\"" table)
37 (modify-syntax-entry ?` "\"" table)
38 (modify-syntax-entry ?{ "(" table)
39 (modify-syntax-entry ?} ")" table)
40 table)
41 "A syntax table for parsing sgml attributes.")
43 (defvar mailcap-mime-data
44 '(("application"
45 ("x-x509-ca-cert"
46 (viewer . ssl-view-site-cert)
47 (test . (fboundp 'ssl-view-site-cert))
48 (type . "application/x-x509-ca-cert"))
49 ("x-x509-user-cert"
50 (viewer . ssl-view-user-cert)
51 (test . (fboundp 'ssl-view-user-cert))
52 (type . "application/x-x509-user-cert"))
53 ("octet-stream"
54 (viewer . mailcap-save-binary-file)
55 (non-viewer . t)
56 (type . "application/octet-stream"))
57 ("dvi"
58 (viewer . "open %s")
59 (type . "application/dvi")
60 (test . (eq (mm-device-type) 'ns)))
61 ("dvi"
62 (viewer . "xdvi %s")
63 (test . (eq (mm-device-type) 'x))
64 ("needsx11")
65 (type . "application/dvi"))
66 ("dvi"
67 (viewer . "dvitty %s")
68 (test . (not (getenv "DISPLAY")))
69 (type . "application/dvi"))
70 ("emacs-lisp"
71 (viewer . mailcap-maybe-eval)
72 (type . "application/emacs-lisp"))
73 ("x-tar"
74 (viewer . mailcap-save-binary-file)
75 (non-viewer . t)
76 (type . "application/x-tar"))
77 ("x-latex"
78 (viewer . tex-mode)
79 (test . (fboundp 'tex-mode))
80 (type . "application/x-latex"))
81 ("x-tex"
82 (viewer . tex-mode)
83 (test . (fboundp 'tex-mode))
84 (type . "application/x-tex"))
85 ("latex"
86 (viewer . tex-mode)
87 (test . (fboundp 'tex-mode))
88 (type . "application/latex"))
89 ("tex"
90 (viewer . tex-mode)
91 (test . (fboundp 'tex-mode))
92 (type . "application/tex"))
93 ("texinfo"
94 (viewer . texinfo-mode)
95 (test . (fboundp 'texinfo-mode))
96 (type . "application/tex"))
97 ("zip"
98 (viewer . mailcap-save-binary-file)
99 (non-viewer . t)
100 (type . "application/zip")
101 ("copiousoutput"))
102 ("pdf"
103 (viewer . "acroread %s")
104 (type . "application/pdf"))
105 ("postscript"
106 (viewer . "open %s")
107 (type . "application/postscript")
108 (test . (eq (mm-device-type) 'ns)))
109 ("postscript"
110 (viewer . "ghostview -dSAFER %s")
111 (type . "application/postscript")
112 (test . (eq (mm-device-type) 'x))
113 ("needsx11"))
114 ("postscript"
115 (viewer . "ps2ascii %s")
116 (type . "application/postscript")
117 (test . (not (getenv "DISPLAY")))
118 ("copiousoutput")))
119 ("audio"
120 ("x-mpeg"
121 (viewer . "maplay %s")
122 (type . "audio/x-mpeg"))
123 (".*"
124 (viewer . "showaudio")
125 (type . "audio/*")))
126 ("message"
127 ("rfc-*822"
128 (viewer . mm-view-message)
129 (test . (and (featurep 'gnus)
130 (gnus-alive-p)))
131 (type . "message/rfc822"))
132 ("rfc-*822"
133 (viewer . vm-mode)
134 (test . (fboundp 'vm-mode))
135 (type . "message/rfc822"))
136 ("rfc-*822"
137 (viewer . w3-mode)
138 (test . (fboundp 'w3-mode))
139 (type . "message/rfc822"))
140 ("rfc-*822"
141 (viewer . view-mode)
142 (test . (fboundp 'view-mode))
143 (type . "message/rfc822"))
144 ("rfc-*822"
145 (viewer . fundamental-mode)
146 (type . "message/rfc822")))
147 ("image"
148 ("x-xwd"
149 (viewer . "xwud -in %s")
150 (type . "image/x-xwd")
151 ("compose" . "xwd -frame > %s")
152 (test . (eq (mm-device-type) 'x))
153 ("needsx11"))
154 ("x11-dump"
155 (viewer . "xwud -in %s")
156 (type . "image/x-xwd")
157 ("compose" . "xwd -frame > %s")
158 (test . (eq (mm-device-type) 'x))
159 ("needsx11"))
160 ("windowdump"
161 (viewer . "xwud -in %s")
162 (type . "image/x-xwd")
163 ("compose" . "xwd -frame > %s")
164 (test . (eq (mm-device-type) 'x))
165 ("needsx11"))
166 (".*"
167 (viewer . "aopen %s")
168 (type . "image/*")
169 (test . (eq (mm-device-type) 'ns)))
170 (".*"
171 (viewer . "display %s")
172 (type . "image/*")
173 (test . (eq (mm-device-type) 'x))
174 ("needsx11"))
175 (".*"
176 (viewer . "ee %s")
177 (type . "image/*")
178 (test . (eq (mm-device-type) 'x))
179 ("needsx11")))
180 ("text"
181 ("plain"
182 (viewer . w3-mode)
183 (test . (fboundp 'w3-mode))
184 (type . "text/plain"))
185 ("plain"
186 (viewer . view-mode)
187 (test . (fboundp 'view-mode))
188 (type . "text/plain"))
189 ("plain"
190 (viewer . fundamental-mode)
191 (type . "text/plain"))
192 ("enriched"
193 (viewer . enriched-decode-region)
194 (test . (fboundp 'enriched-decode))
195 (type . "text/enriched"))
196 ("html"
197 (viewer . mm-w3-prepare-buffer)
198 (test . (fboundp 'w3-prepare-buffer))
199 (type . "text/html")))
200 ("video"
201 ("mpeg"
202 (viewer . "mpeg_play %s")
203 (type . "video/mpeg")
204 (test . (eq (mm-device-type) 'x))
205 ("needsx11")))
206 ("x-world"
207 ("x-vrml"
208 (viewer . "webspace -remote %s -URL %u")
209 (type . "x-world/x-vrml")
210 ("description"
211 "VRML document")))
212 ("archive"
213 ("tar"
214 (viewer . tar-mode)
215 (type . "archive/tar")
216 (test . (fboundp 'tar-mode)))))
217 "The mailcap structure is an assoc list of assoc lists.
218 1st assoc list is keyed on the major content-type
219 2nd assoc list is keyed on the minor content-type (which can be a regexp)
221 Which looks like:
222 -----------------
223 ((\"application\"
224 (\"postscript\" . <info>))
225 (\"text\"
226 (\"plain\" . <info>)))
228 Where <info> is another assoc list of the various information
229 related to the mailcap RFC. This is keyed on the lowercase
230 attribute name (viewer, test, etc). This looks like:
231 ((viewer . viewerinfo)
232 (test . testinfo)
233 (xxxx . \"string\"))
235 Where viewerinfo specifies how the content-type is viewed. Can be
236 a string, in which case it is run through a shell, with
237 appropriate parameters, or a symbol, in which case the symbol is
238 funcall'd, with the buffer as an argument.
240 testinfo is a list of strings, or nil. If nil, it means the
241 viewer specified is always valid. If it is a list of strings,
242 these are used to determine whether a viewer passes the 'test' or
243 not.")
245 (defvar mailcap-download-directory nil
246 "*Where downloaded files should go by default.")
248 (defvar mailcap-temporary-directory
249 (cond ((fboundp 'temp-directory) (temp-directory))
250 ((boundp 'temporary-file-directory) temporary-file-directory)
251 ("/tmp/"))
252 "*Where temporary files go.")
255 ;;; Utility functions
258 (defun mailcap-generate-unique-filename (&optional fmt)
259 "Generate a unique filename in mailcap-temporary-directory."
260 (if (not fmt)
261 (let ((base (format "mailcap-tmp.%d" (user-real-uid)))
262 (fname "")
263 (x 0))
264 (setq fname (format "%s%d" base x))
265 (while (file-exists-p
266 (expand-file-name fname mailcap-temporary-directory))
267 (setq x (1+ x)
268 fname (concat base (int-to-string x))))
269 (expand-file-name fname mailcap-temporary-directory))
270 (let ((base (concat "mm" (int-to-string (user-real-uid))))
271 (fname "")
272 (x 0))
273 (setq fname (format fmt (concat base (int-to-string x))))
274 (while (file-exists-p
275 (expand-file-name fname mailcap-temporary-directory))
276 (setq x (1+ x)
277 fname (format fmt (concat base (int-to-string x)))))
278 (expand-file-name fname mailcap-temporary-directory))))
280 (defun mailcap-save-binary-file ()
281 (goto-char (point-min))
282 (unwind-protect
283 (let ((file (read-file-name
284 "Filename to save as: "
285 (or mailcap-download-directory "~/")))
286 (require-final-newline nil))
287 (write-region (point-min) (point-max) file))
288 (kill-buffer (current-buffer))))
290 (defvar mailcap-maybe-eval-warning
291 "*** WARNING ***
293 This MIME part contains untrusted and possibly harmful content.
294 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
295 things can happen. Please examine the code very carefully before you
296 instruct Emacs to evaluate it. You can browse the buffer containing
297 the code using \\[scroll-other-window].
299 If you are unsure what to do, please answer \"no\"."
300 "Text of warning message displayed by `mailcap-maybe-eval'.
301 Make sure that this text consists only of few text lines. Otherwise,
302 Gnus might fail to display all of it.")
304 (defun mailcap-maybe-eval ()
305 "Maybe evaluate a buffer of emacs lisp code."
306 (let ((lisp-buffer (current-buffer)))
307 (goto-char (point-min))
308 (when
309 (save-window-excursion
310 (delete-other-windows)
311 (let ((buffer (get-buffer-create (generate-new-buffer-name
312 "*Warning*"))))
313 (unwind-protect
314 (with-current-buffer buffer
315 (insert (substitute-command-keys
316 mailcap-maybe-eval-warning))
317 (goto-char (point-min))
318 (display-buffer buffer)
319 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
320 (kill-buffer buffer))))
321 (eval-buffer (current-buffer)))
322 (when (buffer-live-p lisp-buffer)
323 (with-current-buffer lisp-buffer
324 (emacs-lisp-mode)))))
328 ;;; The mailcap parser
331 (defun mailcap-replace-regexp (regexp to-string)
332 ;; Quiet replace-regexp.
333 (goto-char (point-min))
334 (while (re-search-forward regexp nil t)
335 (replace-match to-string t nil)))
337 (defvar mailcap-parsed-p nil)
339 (defun mailcap-parse-mailcaps (&optional path force)
340 "Parse out all the mailcaps specified in a path string PATH.
341 Components of PATH are separated by the `path-separator' character
342 appropriate for this system. If FORCE, re-parse even if already
343 parsed. If PATH is omitted, use the value of environment variable
344 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
345 /usr/local/etc/mailcap."
346 (interactive (list nil t))
347 (when (or (not mailcap-parsed-p)
348 force)
349 (cond
350 (path nil)
351 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
352 ((memq system-type '(ms-dos ms-windows windows-nt))
353 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
354 (t (setq path
355 ;; This is per RFC 1524, specifically
356 ;; with /usr before /usr/local.
357 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
358 "/usr/local/etc/mailcap"))))
359 (let ((fnames (reverse
360 (if (stringp path)
361 (delete "" (split-string path path-separator))
362 path)))
363 fname)
364 (while fnames
365 (setq fname (car fnames))
366 (if (and (file-readable-p fname)
367 (file-regular-p fname))
368 (mailcap-parse-mailcap fname))
369 (setq fnames (cdr fnames))))
370 (setq mailcap-parsed-p t)))
372 (defun mailcap-parse-mailcap (fname)
373 ;; Parse out the mailcap file specified by FNAME
374 (let (major ; The major mime type (image/audio/etc)
375 minor ; The minor mime type (gif, basic, etc)
376 save-pos ; Misc saved positions used in parsing
377 viewer ; How to view this mime type
378 info ; Misc info about this mime type
380 (with-temp-buffer
381 (insert-file-contents fname)
382 (set-syntax-table mailcap-parse-args-syntax-table)
383 (mailcap-replace-regexp "#.*" "") ; Remove all comments
384 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
385 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
386 (goto-char (point-max))
387 (skip-chars-backward " \t\n")
388 (delete-region (point) (point-max))
389 (while (not (bobp))
390 (skip-chars-backward " \t\n")
391 (beginning-of-line)
392 (setq save-pos (point)
393 info nil)
394 (skip-chars-forward "^/; \t\n")
395 (downcase-region save-pos (point))
396 (setq major (buffer-substring save-pos (point)))
397 (skip-chars-forward " \t")
398 (setq minor "")
399 (when (eq (char-after) ?/)
400 (forward-char)
401 (skip-chars-forward " \t")
402 (setq save-pos (point))
403 (skip-chars-forward "^; \t\n")
404 (downcase-region save-pos (point))
405 (setq minor
406 (cond
407 ((eq ?* (or (char-after save-pos) 0)) ".*")
408 ((= (point) save-pos) ".*")
409 (t (regexp-quote (buffer-substring save-pos (point)))))))
410 (skip-chars-forward " \t")
411 ;;; Got the major/minor chunks, now for the viewers/etc
412 ;;; The first item _must_ be a viewer, according to the
413 ;;; RFC for mailcap files (#1343)
414 (setq viewer "")
415 (when (eq (char-after) ?\;)
416 (forward-char)
417 (skip-chars-forward " \t")
418 (setq save-pos (point))
419 (skip-chars-forward "^;\n")
420 ;; skip \;
421 (while (eq (char-before) ?\\)
422 (backward-delete-char 1)
423 (forward-char)
424 (skip-chars-forward "^;\n"))
425 (if (eq (or (char-after save-pos) 0) ?')
426 (setq viewer (progn
427 (narrow-to-region (1+ save-pos) (point))
428 (goto-char (point-min))
429 (prog1
430 (read (current-buffer))
431 (goto-char (point-max))
432 (widen))))
433 (setq viewer (buffer-substring save-pos (point)))))
434 (setq save-pos (point))
435 (end-of-line)
436 (unless (equal viewer "")
437 (setq info (nconc (list (cons 'viewer viewer)
438 (cons 'type (concat major "/"
439 (if (string= minor ".*")
440 "*" minor))))
441 (mailcap-parse-mailcap-extras save-pos (point))))
442 (mailcap-mailcap-entry-passes-test info)
443 (mailcap-add-mailcap-entry major minor info))
444 (beginning-of-line)))))
446 (defun mailcap-parse-mailcap-extras (st nd)
447 ;; Grab all the extra stuff from a mailcap entry
448 (let (
449 name ; From name=
450 value ; its value
451 results ; Assoc list of results
452 name-pos ; Start of XXXX= position
453 val-pos ; Start of value position
454 done ; Found end of \'d ;s?
456 (save-restriction
457 (narrow-to-region st nd)
458 (goto-char (point-min))
459 (skip-chars-forward " \n\t;")
460 (while (not (eobp))
461 (setq done nil)
462 (setq name-pos (point))
463 (skip-chars-forward "^ \n\t=;")
464 (downcase-region name-pos (point))
465 (setq name (buffer-substring name-pos (point)))
466 (skip-chars-forward " \t\n")
467 (if (not (eq (char-after (point)) ?=)) ; There is no value
468 (setq value t)
469 (skip-chars-forward " \t\n=")
470 (setq val-pos (point))
471 (if (memq (char-after val-pos) '(?\" ?'))
472 (progn
473 (setq val-pos (1+ val-pos))
474 (condition-case nil
475 (progn
476 (forward-sexp 1)
477 (backward-char 1))
478 (error (goto-char (point-max)))))
479 (while (not done)
480 (skip-chars-forward "^;")
481 (if (eq (char-after (1- (point))) ?\\ )
482 (progn
483 (subst-char-in-region (1- (point)) (point) ?\\ ? )
484 (skip-chars-forward ";"))
485 (setq done t))))
486 (setq value (buffer-substring val-pos (point))))
487 (setq results (cons (cons name value) results))
488 (skip-chars-forward " \";\n\t"))
489 results)))
491 (defun mailcap-mailcap-entry-passes-test (info)
492 ;; Return t iff a mailcap entry passes its test clause or no test
493 ;; clause is present.
494 (let (status ; Call-process-regions return value
495 (test (assq 'test info)) ; The test clause
497 (setq status (and test (split-string (cdr test) " ")))
498 (if (and (or (assoc "needsterm" info)
499 (assoc "needsterminal" info)
500 (assoc "needsx11" info))
501 (not (getenv "DISPLAY")))
502 (setq status nil)
503 (cond
504 ((and (equal (nth 0 status) "test")
505 (equal (nth 1 status) "-n")
506 (or (equal (nth 2 status) "$DISPLAY")
507 (equal (nth 2 status) "\"$DISPLAY\"")))
508 (setq status (if (getenv "DISPLAY") t nil)))
509 ((and (equal (nth 0 status) "test")
510 (equal (nth 1 status) "-z")
511 (or (equal (nth 2 status) "$DISPLAY")
512 (equal (nth 2 status) "\"$DISPLAY\"")))
513 (setq status (if (getenv "DISPLAY") nil t)))
514 (test nil)
515 (t nil)))
516 (and test (listp test) (setcdr test status))))
519 ;;; The action routines.
522 (defun mailcap-possible-viewers (major minor)
523 ;; Return a list of possible viewers from MAJOR for minor type MINOR
524 (let ((exact '())
525 (wildcard '()))
526 (while major
527 (cond
528 ((equal (car (car major)) minor)
529 (setq exact (cons (cdr (car major)) exact)))
530 ((and minor (string-match (car (car major)) minor))
531 (setq wildcard (cons (cdr (car major)) wildcard))))
532 (setq major (cdr major)))
533 (nconc exact wildcard)))
535 (defun mailcap-unescape-mime-test (test type-info)
536 (let (save-pos save-chr subst)
537 (cond
538 ((symbolp test) test)
539 ((and (listp test) (symbolp (car test))) test)
540 ((or (stringp test)
541 (and (listp test) (stringp (car test))
542 (setq test (mapconcat 'identity test " "))))
543 (with-temp-buffer
544 (insert test)
545 (goto-char (point-min))
546 (while (not (eobp))
547 (skip-chars-forward "^%")
548 (if (/= (- (point)
549 (progn (skip-chars-backward "\\\\")
550 (point)))
551 0) ; It is an escaped %
552 (progn
553 (delete-char 1)
554 (skip-chars-forward "%."))
555 (setq save-pos (point))
556 (skip-chars-forward "%")
557 (setq save-chr (char-after (point)))
558 (cond
559 ((null save-chr) nil)
560 ((= save-chr ?t)
561 (delete-region save-pos (progn (forward-char 1) (point)))
562 (insert (or (cdr (assq 'type type-info)) "\"\"")))
563 ((= save-chr ?M)
564 (delete-region save-pos (progn (forward-char 1) (point)))
565 (insert "\"\""))
566 ((= save-chr ?n)
567 (delete-region save-pos (progn (forward-char 1) (point)))
568 (insert "\"\""))
569 ((= save-chr ?F)
570 (delete-region save-pos (progn (forward-char 1) (point)))
571 (insert "\"\""))
572 ((= save-chr ?{)
573 (forward-char 1)
574 (skip-chars-forward "^}")
575 (downcase-region (+ 2 save-pos) (point))
576 (setq subst (buffer-substring (+ 2 save-pos) (point)))
577 (delete-region save-pos (1+ (point)))
578 (insert (or (cdr (assoc subst type-info)) "\"\"")))
579 (t nil))))
580 (buffer-string)))
581 (t (error "Bad value to mailcap-unescape-mime-test. %s" test)))))
583 (defvar mailcap-viewer-test-cache nil)
585 (defun mailcap-viewer-passes-test (viewer-info type-info)
586 ;; Return non-nil iff the viewer specified by VIEWER-INFO passes its
587 ;; test clause (if any).
588 (let* ((test-info (assq 'test viewer-info))
589 (test (cdr test-info))
590 (otest test)
591 (viewer (cdr (assoc 'viewer viewer-info)))
592 (default-directory (expand-file-name "~/"))
593 status parsed-test cache result)
594 (if (setq cache (assoc test mailcap-viewer-test-cache))
595 (cadr cache)
596 (setq
597 result
598 (cond
599 ((not test-info) t) ; No test clause
600 ((not test) nil) ; Already failed test
601 ((eq test t) t) ; Already passed test
602 ((and (symbolp test) ; Lisp function as test
603 (fboundp test))
604 (funcall test type-info))
605 ((and (symbolp test) ; Lisp variable as test
606 (boundp test))
607 (symbol-value test))
608 ((and (listp test) ; List to be eval'd
609 (symbolp (car test)))
610 (eval test))
612 (setq test (mailcap-unescape-mime-test test type-info)
613 test (list shell-file-name nil nil nil
614 shell-command-switch test)
615 status (apply 'call-process test))
616 (= 0 status))))
617 (push (list otest result) mailcap-viewer-test-cache)
618 result)))
620 (defun mailcap-add-mailcap-entry (major minor info)
621 (let ((old-major (assoc major mailcap-mime-data)))
622 (if (null old-major) ; New major area
623 (setq mailcap-mime-data
624 (cons (cons major (list (cons minor info)))
625 mailcap-mime-data))
626 (let ((cur-minor (assoc minor old-major)))
627 (cond
628 ((or (null cur-minor) ; New minor area, or
629 (assq 'test info)) ; Has a test, insert at beginning
630 (setcdr old-major (cons (cons minor info) (cdr old-major))))
631 ((and (not (assq 'test info)) ; No test info, replace completely
632 (not (assq 'test cur-minor))
633 (equal (assq 'viewer info) ; Keep alternative viewer
634 (assq 'viewer cur-minor)))
635 (setcdr cur-minor info))
637 (setcdr old-major (cons (cons minor info) (cdr old-major))))))
640 (defun mailcap-add (type viewer &optional test)
641 "Add VIEWER as a handler for TYPE.
642 If TEST is not given, it defaults to t."
643 (let ((tl (split-string type "/")))
644 (when (or (not (car tl))
645 (not (cadr tl)))
646 (error "%s is not a valid MIME type" type))
647 (mailcap-add-mailcap-entry
648 (car tl) (cadr tl)
649 `((viewer . ,viewer)
650 (test . ,(if test test t))
651 (type . ,type)))))
654 ;;; The main whabbo
657 (defun mailcap-viewer-lessp (x y)
658 ;; Return t iff viewer X is more desirable than viewer Y
659 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
660 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
661 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
662 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
663 (cond
664 ((and x-wild (not y-wild))
665 nil)
666 ((and (not x-wild) y-wild)
668 ((and (not y-lisp) x-lisp)
670 (t nil))))
672 (defun mailcap-mime-info (string &optional request)
673 "Get the MIME viewer command for STRING, return nil if none found.
674 Expects a complete content-type header line as its argument.
676 Second argument REQUEST specifies what information to return. If it is
677 nil or the empty string, the viewer (second field of the mailcap
678 entry) will be returned. If it is a string, then the mailcap field
679 corresponding to that string will be returned (print, description,
680 whatever). If a number, then all the information for this specific
681 viewer is returned. If `all', then all possible viewers for
682 this type is returned."
683 (let (
684 major ; Major encoding (text, etc)
685 minor ; Minor encoding (html, etc)
686 info ; Other info
687 save-pos ; Misc. position during parse
688 major-info ; (assoc major mailcap-mime-data)
689 minor-info ; (assoc minor major-info)
690 test ; current test proc.
691 viewers ; Possible viewers
692 passed ; Viewers that passed the test
693 viewer ; The one and only viewer
694 ctl)
695 (save-excursion
696 (setq ctl (mail-header-parse-content-type (or string "text/plain")))
697 (setq major (split-string (car ctl) "/"))
698 (setq minor (cadr major)
699 major (car major))
700 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
701 (when (setq viewers (mailcap-possible-viewers major-info minor))
702 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
703 (cdr a)))
704 (cdr ctl)))
705 (while viewers
706 (if (mailcap-viewer-passes-test (car viewers) info)
707 (setq passed (cons (car viewers) passed)))
708 (setq viewers (cdr viewers)))
709 (setq passed (sort passed 'mailcap-viewer-lessp))
710 (setq viewer (car passed))))
711 (when (and (stringp (cdr (assq 'viewer viewer)))
712 passed)
713 (setq viewer (car passed)))
714 (cond
715 ((and (null viewer) (not (equal major "default")) request)
716 (mailcap-mime-info "default" request))
717 ((or (null request) (equal request ""))
718 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
719 ((stringp request)
720 (if (or (eq request 'test) (eq request 'viewer))
721 (mailcap-unescape-mime-test
722 (cdr-safe (assoc request viewer)) info)))
723 ((eq request 'all)
724 passed)
726 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
727 (setq viewer (copy-sequence viewer))
728 (let ((view (assq 'viewer viewer))
729 (test (assq 'test viewer)))
730 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
731 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
732 viewer)))))
735 ;;; Experimental MIME-types parsing
738 (defvar mailcap-mime-extensions
739 '(("" . "text/plain")
740 (".abs" . "audio/x-mpeg")
741 (".aif" . "audio/aiff")
742 (".aifc" . "audio/aiff")
743 (".aiff" . "audio/aiff")
744 (".ano" . "application/x-annotator")
745 (".au" . "audio/ulaw")
746 (".avi" . "video/x-msvideo")
747 (".bcpio" . "application/x-bcpio")
748 (".bin" . "application/octet-stream")
749 (".cdf" . "application/x-netcdr")
750 (".cpio" . "application/x-cpio")
751 (".csh" . "application/x-csh")
752 (".css" . "text/css")
753 (".dvi" . "application/x-dvi")
754 (".diff" . "text/x-patch")
755 (".el" . "application/emacs-lisp")
756 (".eps" . "application/postscript")
757 (".etx" . "text/x-setext")
758 (".exe" . "application/octet-stream")
759 (".fax" . "image/x-fax")
760 (".gif" . "image/gif")
761 (".hdf" . "application/x-hdf")
762 (".hqx" . "application/mac-binhex40")
763 (".htm" . "text/html")
764 (".html" . "text/html")
765 (".icon" . "image/x-icon")
766 (".ief" . "image/ief")
767 (".jpg" . "image/jpeg")
768 (".macp" . "image/x-macpaint")
769 (".man" . "application/x-troff-man")
770 (".me" . "application/x-troff-me")
771 (".mif" . "application/mif")
772 (".mov" . "video/quicktime")
773 (".movie" . "video/x-sgi-movie")
774 (".mp2" . "audio/x-mpeg")
775 (".mp3" . "audio/x-mpeg")
776 (".mp2a" . "audio/x-mpeg2")
777 (".mpa" . "audio/x-mpeg")
778 (".mpa2" . "audio/x-mpeg2")
779 (".mpe" . "video/mpeg")
780 (".mpeg" . "video/mpeg")
781 (".mpega" . "audio/x-mpeg")
782 (".mpegv" . "video/mpeg")
783 (".mpg" . "video/mpeg")
784 (".mpv" . "video/mpeg")
785 (".ms" . "application/x-troff-ms")
786 (".nc" . "application/x-netcdf")
787 (".nc" . "application/x-netcdf")
788 (".oda" . "application/oda")
789 (".patch" . "text/x-patch")
790 (".pbm" . "image/x-portable-bitmap")
791 (".pdf" . "application/pdf")
792 (".pgm" . "image/portable-graymap")
793 (".pict" . "image/pict")
794 (".png" . "image/png")
795 (".pnm" . "image/x-portable-anymap")
796 (".ppm" . "image/portable-pixmap")
797 (".ps" . "application/postscript")
798 (".qt" . "video/quicktime")
799 (".ras" . "image/x-raster")
800 (".rgb" . "image/x-rgb")
801 (".rtf" . "application/rtf")
802 (".rtx" . "text/richtext")
803 (".sh" . "application/x-sh")
804 (".sit" . "application/x-stuffit")
805 (".snd" . "audio/basic")
806 (".src" . "application/x-wais-source")
807 (".tar" . "archive/tar")
808 (".tcl" . "application/x-tcl")
809 (".tcl" . "application/x-tcl")
810 (".tex" . "application/x-tex")
811 (".texi" . "application/texinfo")
812 (".tga" . "image/x-targa")
813 (".tif" . "image/tiff")
814 (".tiff" . "image/tiff")
815 (".tr" . "application/x-troff")
816 (".troff" . "application/x-troff")
817 (".tsv" . "text/tab-separated-values")
818 (".txt" . "text/plain")
819 (".vbs" . "video/mpeg")
820 (".vox" . "audio/basic")
821 (".vrml" . "x-world/x-vrml")
822 (".wav" . "audio/x-wav")
823 (".wrl" . "x-world/x-vrml")
824 (".xbm" . "image/xbm")
825 (".xpm" . "image/xpm")
826 (".xwd" . "image/windowdump")
827 (".zip" . "application/zip")
828 (".ai" . "application/postscript")
829 (".jpe" . "image/jpeg")
830 (".jpeg" . "image/jpeg"))
831 "An assoc list of file extensions and corresponding MIME content-types.")
833 (defvar mailcap-mimetypes-parsed-p nil)
835 (defun mailcap-parse-mimetypes (&optional path force)
836 "Parse out all the mimetypes specified in a unix-style path string PATH.
837 Components of PATH are separated by the `path-separator' character
838 appropriate for this system. If PATH is omitted, use the value of
839 environment variable MIMETYPES if set; otherwise use a default path.
840 If FORCE, re-parse even if already parsed."
841 (interactive (list nil t))
842 (when (or (not mailcap-mimetypes-parsed-p)
843 force)
844 (cond
845 (path nil)
846 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
847 ((memq system-type '(ms-dos ms-windows windows-nt))
848 (setq path '("~/mime.typ" "~/etc/mime.typ")))
849 (t (setq path
850 ;; mime.types seems to be the normal name, definitely so
851 ;; on current GNUish systems. The search order follows
852 ;; that for mailcap.
853 '("~/.mime.types"
854 "/etc/mime.types"
855 "/usr/etc/mime.types"
856 "/usr/local/etc/mime.types"
857 "/usr/local/www/conf/mime.types"
858 "~/.mime-types"
859 "/etc/mime-types"
860 "/usr/etc/mime-types"
861 "/usr/local/etc/mime-types"
862 "/usr/local/www/conf/mime-types"))))
863 (let ((fnames (reverse (if (stringp path)
864 (delete "" (split-string path path-separator))
865 path)))
866 fname)
867 (while fnames
868 (setq fname (car fnames))
869 (if (and (file-readable-p fname))
870 (mailcap-parse-mimetype-file fname))
871 (setq fnames (cdr fnames))))
872 (setq mailcap-mimetypes-parsed-p t)))
874 (defun mailcap-parse-mimetype-file (fname)
875 ;; Parse out a mime-types file
876 (let (type ; The MIME type for this line
877 extns ; The extensions for this line
878 save-pos ; Misc. saved buffer positions
880 (with-temp-buffer
881 (insert-file-contents fname)
882 (mailcap-replace-regexp "#.*" "")
883 (mailcap-replace-regexp "\n+" "\n")
884 (mailcap-replace-regexp "[ \t]+$" "")
885 (goto-char (point-max))
886 (skip-chars-backward " \t\n")
887 (delete-region (point) (point-max))
888 (goto-char (point-min))
889 (while (not (eobp))
890 (skip-chars-forward " \t\n")
891 (setq save-pos (point))
892 (skip-chars-forward "^ \t\n")
893 (downcase-region save-pos (point))
894 (setq type (buffer-substring save-pos (point)))
895 (while (not (eolp))
896 (skip-chars-forward " \t")
897 (setq save-pos (point))
898 (skip-chars-forward "^ \t\n")
899 (setq extns (cons (buffer-substring save-pos (point)) extns)))
900 (while extns
901 (setq mailcap-mime-extensions
902 (cons
903 (cons (if (= (string-to-char (car extns)) ?.)
904 (car extns)
905 (concat "." (car extns))) type)
906 mailcap-mime-extensions)
907 extns (cdr extns)))))))
909 (defun mailcap-extension-to-mime (extn)
910 "Return the MIME content type of the file extensions EXTN."
911 (mailcap-parse-mimetypes)
912 (if (and (stringp extn)
913 (not (eq (string-to-char extn) ?.)))
914 (setq extn (concat "." extn)))
915 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
917 (defvar mailcap-binary-suffixes
918 (if (memq system-type '(ms-dos windows-nt))
919 '(".exe" ".com" ".bat" ".cmd" ".btm" "")
920 '("")))
922 (defun mailcap-command-p (command)
923 "Say whether COMMAND is in the exec path.
924 The path of COMMAND will be returned iff COMMAND is a command."
925 (let ((path (if (file-name-absolute-p command) '(nil) exec-path))
926 file dir)
927 (catch 'found
928 (while (setq dir (pop path))
929 (let ((suffixes mailcap-binary-suffixes))
930 (while suffixes
931 (when (and (file-executable-p
932 (setq file (expand-file-name
933 (concat command (pop suffixes))
934 dir)))
935 (not (file-directory-p file)))
936 (throw 'found file))))))))
938 (defun mailcap-mime-types ()
939 "Return a list of MIME media types."
940 (mailcap-parse-mimetypes)
941 (mm-delete-duplicates
942 (nconc
943 (mapcar 'cdr mailcap-mime-extensions)
944 (apply
945 'nconc
946 (mapcar
947 (lambda (l)
948 (delq nil
949 (mapcar
950 (lambda (m)
951 (let ((type (cdr (assq 'type (cdr m)))))
952 (if (equal (cadr (split-string type "/"))
953 "*")
955 type)))
956 (cdr l))))
957 mailcap-mime-data)))))
959 (provide 'mailcap)
961 ;;; mailcap.el ends here