(ls-lisp-insert-directory): Make -B work
[emacs.git] / lisp / gnus / mailcap.el
blob609a8f4d64b5b64f5a4906649ef67b0a82f69287
1 ;;; mailcap.el --- MIME media types configuration
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: William M. Perry <wmperry@aventail.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail, multimedia
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Provides configuration of MIME media types from directly from Lisp
27 ;; and via the usual mailcap mechanism (RFC 1524). Deals with
28 ;; mime.types similarly.
30 ;;; Code:
32 (eval-when-compile (require 'cl))
33 (autoload 'mail-header-parse-content-type "mail-parse")
35 (defgroup mailcap nil
36 "Definition of viewers for MIME types."
37 :version "21.1"
38 :group 'mime)
40 (defvar mailcap-parse-args-syntax-table
41 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
42 (modify-syntax-entry ?' "\"" table)
43 (modify-syntax-entry ?` "\"" table)
44 (modify-syntax-entry ?{ "(" table)
45 (modify-syntax-entry ?} ")" table)
46 table)
47 "A syntax table for parsing SGML attributes.")
49 (defvar mailcap-print-command
50 (mapconcat 'identity
51 (cons (if (boundp 'lpr-command)
52 lpr-command
53 "lpr")
54 (when (boundp 'lpr-switches)
55 (if (stringp lpr-switches)
56 (list lpr-switches)
57 lpr-switches)))
58 " ")
59 "Shell command (including switches) used to print PostScript files.")
61 ;; Postpone using defcustom for this as it's so big and we essentially
62 ;; have to have two copies of the data around then. Perhaps just
63 ;; customize the Lisp viewers and rely on the normal configuration
64 ;; files for the rest? -- fx
65 (defvar mailcap-mime-data
66 `(("application"
67 ("vnd\\.ms-excel"
68 (viewer . "gnumeric %s")
69 (test . (getenv "DISPLAY"))
70 (type . "application/vnd.ms-excel"))
71 ("x-x509-ca-cert"
72 (viewer . ssl-view-site-cert)
73 (type . "application/x-x509-ca-cert"))
74 ("x-x509-user-cert"
75 (viewer . ssl-view-user-cert)
76 (type . "application/x-x509-user-cert"))
77 ("octet-stream"
78 (viewer . mailcap-save-binary-file)
79 (non-viewer . t)
80 (type . "application/octet-stream"))
81 ("dvi"
82 (viewer . "xdvi -safer %s")
83 (test . (eq window-system 'x))
84 ("needsx11")
85 (type . "application/dvi")
86 ("print" . "dvips -qRP %s"))
87 ("dvi"
88 (viewer . "dvitty %s")
89 (test . (not (getenv "DISPLAY")))
90 (type . "application/dvi")
91 ("print" . "dvips -qRP %s"))
92 ("emacs-lisp"
93 (viewer . mailcap-maybe-eval)
94 (type . "application/emacs-lisp"))
95 ("x-emacs-lisp"
96 (viewer . mailcap-maybe-eval)
97 (type . "application/x-emacs-lisp"))
98 ("x-tar"
99 (viewer . mailcap-save-binary-file)
100 (non-viewer . t)
101 (type . "application/x-tar"))
102 ("x-latex"
103 (viewer . tex-mode)
104 (type . "application/x-latex"))
105 ("x-tex"
106 (viewer . tex-mode)
107 (type . "application/x-tex"))
108 ("latex"
109 (viewer . tex-mode)
110 (type . "application/latex"))
111 ("tex"
112 (viewer . tex-mode)
113 (type . "application/tex"))
114 ("texinfo"
115 (viewer . texinfo-mode)
116 (type . "application/tex"))
117 ("zip"
118 (viewer . mailcap-save-binary-file)
119 (non-viewer . t)
120 (type . "application/zip")
121 ("copiousoutput"))
122 ("pdf"
123 (viewer . pdf-view-mode)
124 (type . "application/pdf")
125 (test . (eq window-system 'x)))
126 ("pdf"
127 (viewer . doc-view-mode)
128 (type . "application/pdf")
129 (test . (eq window-system 'x)))
130 ("pdf"
131 (viewer . "gv -safer %s")
132 (type . "application/pdf")
133 (test . window-system)
134 ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command)))
135 ("pdf"
136 (viewer . "gpdf %s")
137 (type . "application/pdf")
138 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
139 (test . (eq window-system 'x)))
140 ("pdf"
141 (viewer . "xpdf %s")
142 (type . "application/pdf")
143 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
144 (test . (eq window-system 'x)))
145 ("pdf"
146 (viewer . ,(concat "pdftotext %s -"))
147 (type . "application/pdf")
148 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
149 ("copiousoutput"))
150 ("postscript"
151 (viewer . "gv -safer %s")
152 (type . "application/postscript")
153 (test . window-system)
154 ("print" . ,(concat mailcap-print-command " %s"))
155 ("needsx11"))
156 ("postscript"
157 (viewer . "ghostview -dSAFER %s")
158 (type . "application/postscript")
159 (test . (eq window-system 'x))
160 ("print" . ,(concat mailcap-print-command " %s"))
161 ("needsx11"))
162 ("postscript"
163 (viewer . "ps2ascii %s")
164 (type . "application/postscript")
165 (test . (not (getenv "DISPLAY")))
166 ("print" . ,(concat mailcap-print-command " %s"))
167 ("copiousoutput"))
168 ("sieve"
169 (viewer . sieve-mode)
170 (type . "application/sieve"))
171 ("pgp-keys"
172 (viewer . "gpg --import --interactive --verbose")
173 (type . "application/pgp-keys")
174 ("needsterminal")))
175 ("audio"
176 ("x-mpeg"
177 (viewer . "maplay %s")
178 (type . "audio/x-mpeg"))
179 (".*"
180 (viewer . "showaudio")
181 (type . "audio/*")))
182 ("message"
183 ("rfc-*822"
184 (viewer . mm-view-message)
185 (test . (and (featurep 'gnus)
186 (gnus-alive-p)))
187 (type . "message/rfc822"))
188 ("rfc-*822"
189 (viewer . vm-mode)
190 (type . "message/rfc822"))
191 ("rfc-*822"
192 (viewer . view-mode)
193 (type . "message/rfc822")))
194 ("image"
195 ("x-xwd"
196 (viewer . "xwud -in %s")
197 (type . "image/x-xwd")
198 ("compose" . "xwd -frame > %s")
199 (test . (eq window-system 'x))
200 ("needsx11"))
201 ("x11-dump"
202 (viewer . "xwud -in %s")
203 (type . "image/x-xwd")
204 ("compose" . "xwd -frame > %s")
205 (test . (eq window-system 'x))
206 ("needsx11"))
207 ("windowdump"
208 (viewer . "xwud -in %s")
209 (type . "image/x-xwd")
210 ("compose" . "xwd -frame > %s")
211 (test . (eq window-system 'x))
212 ("needsx11"))
213 (".*"
214 (viewer . "display %s")
215 (type . "image/*")
216 (test . (eq window-system 'x))
217 ("needsx11"))
218 (".*"
219 (viewer . "ee %s")
220 (type . "image/*")
221 (test . (eq window-system 'x))
222 ("needsx11")))
223 ("text"
224 ("plain"
225 (viewer . view-mode)
226 (type . "text/plain"))
227 ("plain"
228 (viewer . fundamental-mode)
229 (type . "text/plain"))
230 ("enriched"
231 (viewer . enriched-decode)
232 (type . "text/enriched"))
233 ("dns"
234 (viewer . dns-mode)
235 (type . "text/dns")))
236 ("video"
237 ("mpeg"
238 (viewer . "mpeg_play %s")
239 (type . "video/mpeg")
240 (test . (eq window-system 'x))
241 ("needsx11")))
242 ("x-world"
243 ("x-vrml"
244 (viewer . "webspace -remote %s -URL %u")
245 (type . "x-world/x-vrml")
246 ("description"
247 "VRML document")))
248 ("archive"
249 ("tar"
250 (viewer . tar-mode)
251 (type . "archive/tar"))))
252 "The mailcap structure is an assoc list of assoc lists.
253 1st assoc list is keyed on the major content-type
254 2nd assoc list is keyed on the minor content-type (which can be a regexp)
256 Which looks like:
257 -----------------
258 ((\"application\"
259 (\"postscript\" . <info>))
260 (\"text\"
261 (\"plain\" . <info>)))
263 Where <info> is another assoc list of the various information
264 related to the mailcap RFC 1524. This is keyed on the lowercase
265 attribute name (viewer, test, etc). This looks like:
266 ((viewer . VIEWERINFO)
267 (test . TESTINFO)
268 (xxxx . \"STRING\")
269 FLAG)
271 Where VIEWERINFO specifies how the content-type is viewed. Can be
272 a string, in which case it is run through a shell, with appropriate
273 parameters, or a symbol, in which case the symbol is `funcall'ed if
274 and only if it exists as a function, with the buffer as an argument.
276 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
277 means the viewer is always valid. If it is a Lisp function, it is
278 called with a list of items from any extra fields from the
279 Content-Type header as argument to return a boolean value for the
280 validity. Otherwise, if it is a non-function Lisp symbol or list
281 whose car is a symbol, it is `eval'led to yield the validity. If it
282 is a string or list of strings, it represents a shell command to run
283 to return a true or false shell value for the validity.")
284 (put 'mailcap-mime-data 'risky-local-variable t)
286 (defcustom mailcap-download-directory nil
287 "*Directory to which `mailcap-save-binary-file' downloads files by default.
288 nil means your home directory."
289 :type '(choice (const :tag "Home directory" nil)
290 directory)
291 :group 'mailcap)
293 (defvar mailcap-poor-system-types
294 '(ms-dos windows-nt)
295 "Systems that don't have a Unix-like directory hierarchy.")
298 ;;; Utility functions
301 (defun mailcap-save-binary-file ()
302 (goto-char (point-min))
303 (unwind-protect
304 (let ((file (read-file-name
305 "Filename to save as: "
306 (or mailcap-download-directory "~/")))
307 (require-final-newline nil))
308 (write-region (point-min) (point-max) file))
309 (kill-buffer (current-buffer))))
311 (defvar mailcap-maybe-eval-warning
312 "*** WARNING ***
314 This MIME part contains untrusted and possibly harmful content.
315 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
316 things can happen. Please examine the code very carefully before you
317 instruct Emacs to evaluate it. You can browse the buffer containing
318 the code using \\[scroll-other-window].
320 If you are unsure what to do, please answer \"no\"."
321 "Text of warning message displayed by `mailcap-maybe-eval'.
322 Make sure that this text consists only of few text lines. Otherwise,
323 Gnus might fail to display all of it.")
325 (defun mailcap-maybe-eval ()
326 "Maybe evaluate a buffer of Emacs Lisp code."
327 (let ((lisp-buffer (current-buffer)))
328 (goto-char (point-min))
329 (when
330 (save-window-excursion
331 (delete-other-windows)
332 (let ((buffer (get-buffer-create (generate-new-buffer-name
333 "*Warning*"))))
334 (unwind-protect
335 (with-current-buffer buffer
336 (insert (substitute-command-keys
337 mailcap-maybe-eval-warning))
338 (goto-char (point-min))
339 (display-buffer buffer)
340 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
341 (kill-buffer buffer))))
342 (eval-buffer (current-buffer)))
343 (when (buffer-live-p lisp-buffer)
344 (with-current-buffer lisp-buffer
345 (emacs-lisp-mode)))))
349 ;;; The mailcap parser
352 (defun mailcap-replace-regexp (regexp to-string)
353 ;; Quiet replace-regexp.
354 (goto-char (point-min))
355 (while (re-search-forward regexp nil t)
356 (replace-match to-string t nil)))
358 (defvar mailcap-parsed-p nil)
360 (defun mailcap-parse-mailcaps (&optional path force)
361 "Parse out all the mailcaps specified in a path string PATH.
362 Components of PATH are separated by the `path-separator' character
363 appropriate for this system. If FORCE, re-parse even if already
364 parsed. If PATH is omitted, use the value of environment variable
365 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
366 /usr/local/etc/mailcap."
367 (interactive (list nil t))
368 (when (or (not mailcap-parsed-p)
369 force)
370 (cond
371 (path nil)
372 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
373 ((memq system-type mailcap-poor-system-types)
374 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
375 (t (setq path
376 ;; This is per RFC 1524, specifically
377 ;; with /usr before /usr/local.
378 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
379 "/usr/local/etc/mailcap"))))
380 (let ((fnames (reverse
381 (if (stringp path)
382 (split-string path path-separator t)
383 path)))
384 fname)
385 (while fnames
386 (setq fname (car fnames))
387 (if (and (file-readable-p fname)
388 (file-regular-p fname))
389 (mailcap-parse-mailcap fname))
390 (setq fnames (cdr fnames))))
391 (setq mailcap-parsed-p t)))
393 (defun mailcap-parse-mailcap (fname)
394 "Parse out the mailcap file specified by FNAME."
395 (let (major ; The major mime type (image/audio/etc)
396 minor ; The minor mime type (gif, basic, etc)
397 save-pos ; Misc saved positions used in parsing
398 viewer ; How to view this mime type
399 info ; Misc info about this mime type
401 (with-temp-buffer
402 (insert-file-contents fname)
403 (set-syntax-table mailcap-parse-args-syntax-table)
404 (mailcap-replace-regexp "#.*" "") ; Remove all comments
405 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
406 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
407 (goto-char (point-max))
408 (skip-chars-backward " \t\n")
409 (delete-region (point) (point-max))
410 (while (not (bobp))
411 (skip-chars-backward " \t\n")
412 (beginning-of-line)
413 (setq save-pos (point)
414 info nil)
415 (skip-chars-forward "^/; \t\n")
416 (downcase-region save-pos (point))
417 (setq major (buffer-substring save-pos (point)))
418 (skip-chars-forward " \t")
419 (setq minor "")
420 (when (eq (char-after) ?/)
421 (forward-char)
422 (skip-chars-forward " \t")
423 (setq save-pos (point))
424 (skip-chars-forward "^; \t\n")
425 (downcase-region save-pos (point))
426 (setq minor
427 (cond
428 ((eq ?* (or (char-after save-pos) 0)) ".*")
429 ((= (point) save-pos) ".*")
430 (t (regexp-quote (buffer-substring save-pos (point)))))))
431 (skip-chars-forward " \t")
432 ;;; Got the major/minor chunks, now for the viewers/etc
433 ;;; The first item _must_ be a viewer, according to the
434 ;;; RFC for mailcap files (#1524)
435 (setq viewer "")
436 (when (eq (char-after) ?\;)
437 (forward-char)
438 (skip-chars-forward " \t")
439 (setq save-pos (point))
440 (skip-chars-forward "^;\n")
441 ;; skip \;
442 (while (eq (char-before) ?\\)
443 (backward-delete-char 1)
444 (forward-char)
445 (skip-chars-forward "^;\n"))
446 (if (eq (or (char-after save-pos) 0) ?')
447 (setq viewer (progn
448 (narrow-to-region (1+ save-pos) (point))
449 (goto-char (point-min))
450 (prog1
451 (read (current-buffer))
452 (goto-char (point-max))
453 (widen))))
454 (setq viewer (buffer-substring save-pos (point)))))
455 (setq save-pos (point))
456 (end-of-line)
457 (unless (equal viewer "")
458 (setq info (nconc (list (cons 'viewer viewer)
459 (cons 'type (concat major "/"
460 (if (string= minor ".*")
461 "*" minor))))
462 (mailcap-parse-mailcap-extras save-pos (point))))
463 (mailcap-mailcap-entry-passes-test info)
464 (mailcap-add-mailcap-entry major minor info))
465 (beginning-of-line)))))
467 (defun mailcap-parse-mailcap-extras (st nd)
468 "Grab all the extra stuff from a mailcap entry."
469 (let (
470 name ; From name=
471 value ; its value
472 results ; Assoc list of results
473 name-pos ; Start of XXXX= position
474 val-pos ; Start of value position
475 done ; Found end of \'d ;s?
477 (save-restriction
478 (narrow-to-region st nd)
479 (goto-char (point-min))
480 (skip-chars-forward " \n\t;")
481 (while (not (eobp))
482 (setq done nil)
483 (setq name-pos (point))
484 (skip-chars-forward "^ \n\t=;")
485 (downcase-region name-pos (point))
486 (setq name (buffer-substring name-pos (point)))
487 (skip-chars-forward " \t\n")
488 (if (not (eq (char-after (point)) ?=)) ; There is no value
489 (setq value t)
490 (skip-chars-forward " \t\n=")
491 (setq val-pos (point))
492 (if (memq (char-after val-pos) '(?\" ?'))
493 (progn
494 (setq val-pos (1+ val-pos))
495 (condition-case nil
496 (progn
497 (forward-sexp 1)
498 (backward-char 1))
499 (error (goto-char (point-max)))))
500 (while (not done)
501 (skip-chars-forward "^;")
502 (if (eq (char-after (1- (point))) ?\\ )
503 (progn
504 (subst-char-in-region (1- (point)) (point) ?\\ ? )
505 (skip-chars-forward ";"))
506 (setq done t))))
507 (setq value (buffer-substring val-pos (point))))
508 ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
509 ;; strings
510 (setq results (cons (cons (if (string-equal name "test")
511 'test
512 name)
513 value) results))
514 (skip-chars-forward " \";\n\t"))
515 results)))
517 (defun mailcap-mailcap-entry-passes-test (info)
518 "Replace the test clause of INFO itself with a boolean for some cases.
519 This function supports only `test -n $DISPLAY' and `test -z $DISPLAY',
520 replaces them with t or nil. As for others or if INFO has a interactive
521 spec (needsterm, needsterminal, or needsx11) but DISPLAY is not set,
522 the test clause will be unchanged."
523 (let ((test (assq 'test info)) ; The test clause
524 status)
525 (setq status (and test (split-string (cdr test) " ")))
526 (if (and (or (assoc "needsterm" info)
527 (assoc "needsterminal" info)
528 (assoc "needsx11" info))
529 (not (getenv "DISPLAY")))
530 (setq status nil)
531 (cond
532 ((and (equal (nth 0 status) "test")
533 (equal (nth 1 status) "-n")
534 (or (equal (nth 2 status) "$DISPLAY")
535 (equal (nth 2 status) "\"$DISPLAY\"")))
536 (setq status (if (getenv "DISPLAY") t nil)))
537 ((and (equal (nth 0 status) "test")
538 (equal (nth 1 status) "-z")
539 (or (equal (nth 2 status) "$DISPLAY")
540 (equal (nth 2 status) "\"$DISPLAY\"")))
541 (setq status (if (getenv "DISPLAY") nil t)))
542 (test nil)
543 (t nil)))
544 (and test (listp test) (setcdr test status))))
547 ;;; The action routines.
550 (defun mailcap-possible-viewers (major minor)
551 "Return a list of possible viewers from MAJOR for minor type MINOR."
552 (let ((exact '())
553 (wildcard '()))
554 (while major
555 (cond
556 ((equal (car (car major)) minor)
557 (setq exact (cons (cdr (car major)) exact)))
558 ((and minor (string-match (concat "^" (car (car major)) "$") minor))
559 (setq wildcard (cons (cdr (car major)) wildcard))))
560 (setq major (cdr major)))
561 (nconc exact wildcard)))
563 (defun mailcap-unescape-mime-test (test type-info)
564 (let (save-pos save-chr subst)
565 (cond
566 ((symbolp test) test)
567 ((and (listp test) (symbolp (car test))) test)
568 ((or (stringp test)
569 (and (listp test) (stringp (car test))
570 (setq test (mapconcat 'identity test " "))))
571 (with-temp-buffer
572 (insert test)
573 (goto-char (point-min))
574 (while (not (eobp))
575 (skip-chars-forward "^%")
576 (if (/= (- (point)
577 (progn (skip-chars-backward "\\\\")
578 (point)))
579 0) ; It is an escaped %
580 (progn
581 (delete-char 1)
582 (skip-chars-forward "%."))
583 (setq save-pos (point))
584 (skip-chars-forward "%")
585 (setq save-chr (char-after (point)))
586 ;; Escapes:
587 ;; %s: name of a file for the body data
588 ;; %t: content-type
589 ;; %{<parameter name}: value of parameter in mailcap entry
590 ;; %n: number of sub-parts for multipart content-type
591 ;; %F: a set of content-type/filename pairs for multiparts
592 (cond
593 ((null save-chr) nil)
594 ((= save-chr ?t)
595 (delete-region save-pos (progn (forward-char 1) (point)))
596 (insert (or (cdr (assq 'type type-info)) "\"\"")))
597 ((memq save-chr '(?M ?n ?F))
598 (delete-region save-pos (progn (forward-char 1) (point)))
599 (insert "\"\""))
600 ((= save-chr ?{)
601 (forward-char 1)
602 (skip-chars-forward "^}")
603 (downcase-region (+ 2 save-pos) (point))
604 (setq subst (buffer-substring (+ 2 save-pos) (point)))
605 (delete-region save-pos (1+ (point)))
606 (insert (or (cdr (assoc subst type-info)) "\"\"")))
607 (t nil))))
608 (buffer-string)))
609 (t (error "Bad value to mailcap-unescape-mime-test: %s" test)))))
611 (defvar mailcap-viewer-test-cache nil)
613 (defun mailcap-viewer-passes-test (viewer-info type-info)
614 "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
615 Also return non-nil if it has no test clause. TYPE-INFO is an argument
616 to supply to the test."
617 (let* ((test-info (assq 'test viewer-info))
618 (test (cdr test-info))
619 (otest test)
620 (viewer (cdr (assq 'viewer viewer-info)))
621 (default-directory (expand-file-name "~/"))
622 status parsed-test cache result)
623 (cond ((not (or (stringp viewer) (fboundp viewer)))
624 nil) ; Non-existent Lisp function
625 ((setq cache (assoc test mailcap-viewer-test-cache))
626 (cadr cache))
627 ((not test-info) t) ; No test clause
629 (setq
630 result
631 (cond
632 ((not test) nil) ; Already failed test
633 ((eq test t) t) ; Already passed test
634 ((functionp test) ; Lisp function as test
635 (funcall test type-info))
636 ((and (symbolp test) ; Lisp variable as test
637 (boundp test))
638 (symbol-value test))
639 ((and (listp test) ; List to be eval'd
640 (symbolp (car test)))
641 (eval test))
643 (setq test (mailcap-unescape-mime-test test type-info)
644 test (list shell-file-name nil nil nil
645 shell-command-switch test)
646 status (apply 'call-process test))
647 (eq 0 status))))
648 (push (list otest result) mailcap-viewer-test-cache)
649 result))))
651 (defun mailcap-add-mailcap-entry (major minor info)
652 (let ((old-major (assoc major mailcap-mime-data)))
653 (if (null old-major) ; New major area
654 (setq mailcap-mime-data
655 (cons (cons major (list (cons minor info)))
656 mailcap-mime-data))
657 (let ((cur-minor (assoc minor old-major)))
658 (cond
659 ((or (null cur-minor) ; New minor area, or
660 (assq 'test info)) ; Has a test, insert at beginning
661 (setcdr old-major (cons (cons minor info) (cdr old-major))))
662 ((and (not (assq 'test info)) ; No test info, replace completely
663 (not (assq 'test cur-minor))
664 (equal (assq 'viewer info) ; Keep alternative viewer
665 (assq 'viewer cur-minor)))
666 (setcdr cur-minor info))
668 (setcdr old-major (cons (cons minor info) (cdr old-major))))))
671 (defun mailcap-add (type viewer &optional test)
672 "Add VIEWER as a handler for TYPE.
673 If TEST is not given, it defaults to t."
674 (let ((tl (split-string type "/")))
675 (when (or (not (car tl))
676 (not (cadr tl)))
677 (error "%s is not a valid MIME type" type))
678 (mailcap-add-mailcap-entry
679 (car tl) (cadr tl)
680 `((viewer . ,viewer)
681 (test . ,(if test test t))
682 (type . ,type)))))
685 ;;; The main whabbo
688 (defun mailcap-viewer-lessp (x y)
689 "Return t if viewer X is more desirable than viewer Y."
690 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) "")))
691 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) "")))
692 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) ""))))
693 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) "")))))
694 (cond
695 ((and x-wild (not y-wild))
696 nil)
697 ((and (not x-wild) y-wild)
699 ((and (not y-lisp) x-lisp)
701 (t nil))))
703 (defun mailcap-mime-info (string &optional request no-decode)
704 "Get the MIME viewer command for STRING, return nil if none found.
705 Expects a complete content-type header line as its argument.
707 Second argument REQUEST specifies what information to return. If it is
708 nil or the empty string, the viewer (second field of the mailcap
709 entry) will be returned. If it is a string, then the mailcap field
710 corresponding to that string will be returned (print, description,
711 whatever). If a number, then all the information for this specific
712 viewer is returned. If `all', then all possible viewers for
713 this type is returned.
715 If NO-DECODE is non-nil, don't decode STRING."
716 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
717 ;; `mail-parse.el'
718 (let (
719 major ; Major encoding (text, etc)
720 minor ; Minor encoding (html, etc)
721 info ; Other info
722 save-pos ; Misc. position during parse
723 major-info ; (assoc major mailcap-mime-data)
724 minor-info ; (assoc minor major-info)
725 test ; current test proc.
726 viewers ; Possible viewers
727 passed ; Viewers that passed the test
728 viewer ; The one and only viewer
729 ctl)
730 (save-excursion
731 (setq ctl
732 (if no-decode
733 (list (or string "text/plain"))
734 (mail-header-parse-content-type (or string "text/plain"))))
735 (setq major (split-string (car ctl) "/"))
736 (setq minor (cadr major)
737 major (car major))
738 (when (setq major-info (cdr (assoc major mailcap-mime-data)))
739 (when (setq viewers (mailcap-possible-viewers major-info minor))
740 (setq info (mapcar (lambda (a) (cons (symbol-name (car a))
741 (cdr a)))
742 (cdr ctl)))
743 (while viewers
744 (if (mailcap-viewer-passes-test (car viewers) info)
745 (setq passed (cons (car viewers) passed)))
746 (setq viewers (cdr viewers)))
747 (setq passed (sort passed 'mailcap-viewer-lessp))
748 (setq viewer (car passed))))
749 (when (and (stringp (cdr (assq 'viewer viewer)))
750 passed)
751 (setq viewer (car passed)))
752 (cond
753 ((and (null viewer) (not (equal major "default")) request)
754 (mailcap-mime-info "default" request no-decode))
755 ((or (null request) (equal request ""))
756 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
757 ((stringp request)
758 (mailcap-unescape-mime-test
759 (cdr-safe (assoc request viewer)) info))
760 ((eq request 'all)
761 passed)
763 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
764 (setq viewer (copy-sequence viewer))
765 (let ((view (assq 'viewer viewer))
766 (test (assq 'test viewer)))
767 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info)))
768 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info))))
769 viewer)))))
772 ;;; Experimental MIME-types parsing
775 (defvar mailcap-mime-extensions
776 '(("" . "text/plain")
777 (".1" . "text/plain") ;; Manual pages
778 (".3" . "text/plain")
779 (".8" . "text/plain")
780 (".abs" . "audio/x-mpeg")
781 (".aif" . "audio/aiff")
782 (".aifc" . "audio/aiff")
783 (".aiff" . "audio/aiff")
784 (".ano" . "application/x-annotator")
785 (".au" . "audio/ulaw")
786 (".avi" . "video/x-msvideo")
787 (".bcpio" . "application/x-bcpio")
788 (".bin" . "application/octet-stream")
789 (".cdf" . "application/x-netcdr")
790 (".cpio" . "application/x-cpio")
791 (".csh" . "application/x-csh")
792 (".css" . "text/css")
793 (".dvi" . "application/x-dvi")
794 (".diff" . "text/x-patch")
795 (".dpatch". "test/x-patch")
796 (".el" . "application/emacs-lisp")
797 (".eps" . "application/postscript")
798 (".etx" . "text/x-setext")
799 (".exe" . "application/octet-stream")
800 (".fax" . "image/x-fax")
801 (".gif" . "image/gif")
802 (".hdf" . "application/x-hdf")
803 (".hqx" . "application/mac-binhex40")
804 (".htm" . "text/html")
805 (".html" . "text/html")
806 (".icon" . "image/x-icon")
807 (".ief" . "image/ief")
808 (".jpg" . "image/jpeg")
809 (".macp" . "image/x-macpaint")
810 (".man" . "application/x-troff-man")
811 (".me" . "application/x-troff-me")
812 (".mif" . "application/mif")
813 (".mov" . "video/quicktime")
814 (".movie" . "video/x-sgi-movie")
815 (".mp2" . "audio/x-mpeg")
816 (".mp3" . "audio/x-mpeg")
817 (".mp2a" . "audio/x-mpeg2")
818 (".mpa" . "audio/x-mpeg")
819 (".mpa2" . "audio/x-mpeg2")
820 (".mpe" . "video/mpeg")
821 (".mpeg" . "video/mpeg")
822 (".mpega" . "audio/x-mpeg")
823 (".mpegv" . "video/mpeg")
824 (".mpg" . "video/mpeg")
825 (".mpv" . "video/mpeg")
826 (".ms" . "application/x-troff-ms")
827 (".nc" . "application/x-netcdf")
828 (".nc" . "application/x-netcdf")
829 (".oda" . "application/oda")
830 (".patch" . "text/x-patch")
831 (".pbm" . "image/x-portable-bitmap")
832 (".pdf" . "application/pdf")
833 (".pgm" . "image/portable-graymap")
834 (".pict" . "image/pict")
835 (".png" . "image/png")
836 (".pnm" . "image/x-portable-anymap")
837 (".pod" . "text/plain")
838 (".ppm" . "image/portable-pixmap")
839 (".ps" . "application/postscript")
840 (".qt" . "video/quicktime")
841 (".ras" . "image/x-raster")
842 (".rgb" . "image/x-rgb")
843 (".rtf" . "application/rtf")
844 (".rtx" . "text/richtext")
845 (".sh" . "application/x-sh")
846 (".sit" . "application/x-stuffit")
847 (".siv" . "application/sieve")
848 (".snd" . "audio/basic")
849 (".soa" . "text/dns")
850 (".src" . "application/x-wais-source")
851 (".tar" . "archive/tar")
852 (".tcl" . "application/x-tcl")
853 (".tex" . "application/x-tex")
854 (".texi" . "application/texinfo")
855 (".tga" . "image/x-targa")
856 (".tif" . "image/tiff")
857 (".tiff" . "image/tiff")
858 (".tr" . "application/x-troff")
859 (".troff" . "application/x-troff")
860 (".tsv" . "text/tab-separated-values")
861 (".txt" . "text/plain")
862 (".vbs" . "video/mpeg")
863 (".vox" . "audio/basic")
864 (".vrml" . "x-world/x-vrml")
865 (".wav" . "audio/x-wav")
866 (".xls" . "application/vnd.ms-excel")
867 (".wrl" . "x-world/x-vrml")
868 (".xbm" . "image/xbm")
869 (".xpm" . "image/xpm")
870 (".xwd" . "image/windowdump")
871 (".zip" . "application/zip")
872 (".ai" . "application/postscript")
873 (".jpe" . "image/jpeg")
874 (".jpeg" . "image/jpeg")
875 (".org" . "text/x-org"))
876 "An alist of file extensions and corresponding MIME content-types.
877 This exists for you to customize the information in Lisp. It is
878 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
880 (defvar mailcap-mimetypes-parsed-p nil)
882 (defun mailcap-parse-mimetypes (&optional path force)
883 "Parse out all the mimetypes specified in a Unix-style path string PATH.
884 Components of PATH are separated by the `path-separator' character
885 appropriate for this system. If PATH is omitted, use the value of
886 environment variable MIMETYPES if set; otherwise use a default path.
887 If FORCE, re-parse even if already parsed."
888 (interactive (list nil t))
889 (when (or (not mailcap-mimetypes-parsed-p)
890 force)
891 (cond
892 (path nil)
893 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
894 ((memq system-type mailcap-poor-system-types)
895 (setq path '("~/mime.typ" "~/etc/mime.typ")))
896 (t (setq path
897 ;; mime.types seems to be the normal name, definitely so
898 ;; on current GNUish systems. The search order follows
899 ;; that for mailcap.
900 '("~/.mime.types"
901 "/etc/mime.types"
902 "/usr/etc/mime.types"
903 "/usr/local/etc/mime.types"
904 "/usr/local/www/conf/mime.types"
905 "~/.mime-types"
906 "/etc/mime-types"
907 "/usr/etc/mime-types"
908 "/usr/local/etc/mime-types"
909 "/usr/local/www/conf/mime-types"))))
910 (let ((fnames (reverse (if (stringp path)
911 (split-string path path-separator t)
912 path)))
913 fname)
914 (while fnames
915 (setq fname (car fnames))
916 (if (and (file-readable-p fname))
917 (mailcap-parse-mimetype-file fname))
918 (setq fnames (cdr fnames))))
919 (setq mailcap-mimetypes-parsed-p t)))
921 (defun mailcap-parse-mimetype-file (fname)
922 "Parse out a mime-types file FNAME."
923 (let (type ; The MIME type for this line
924 extns ; The extensions for this line
925 save-pos ; Misc. saved buffer positions
927 (with-temp-buffer
928 (insert-file-contents fname)
929 (mailcap-replace-regexp "#.*" "")
930 (mailcap-replace-regexp "\n+" "\n")
931 (mailcap-replace-regexp "[ \t]+$" "")
932 (goto-char (point-max))
933 (skip-chars-backward " \t\n")
934 (delete-region (point) (point-max))
935 (goto-char (point-min))
936 (while (not (eobp))
937 (skip-chars-forward " \t\n")
938 (setq save-pos (point))
939 (skip-chars-forward "^ \t\n")
940 (downcase-region save-pos (point))
941 (setq type (buffer-substring save-pos (point)))
942 (while (not (eolp))
943 (skip-chars-forward " \t")
944 (setq save-pos (point))
945 (skip-chars-forward "^ \t\n")
946 (setq extns (cons (buffer-substring save-pos (point)) extns)))
947 (while extns
948 (setq mailcap-mime-extensions
949 (cons
950 (cons (if (= (string-to-char (car extns)) ?.)
951 (car extns)
952 (concat "." (car extns))) type)
953 mailcap-mime-extensions)
954 extns (cdr extns)))))))
956 (defun mailcap-extension-to-mime (extn)
957 "Return the MIME content type of the file extensions EXTN."
958 (mailcap-parse-mimetypes)
959 (if (and (stringp extn)
960 (not (eq (string-to-char extn) ?.)))
961 (setq extn (concat "." extn)))
962 (cdr (assoc (downcase extn) mailcap-mime-extensions)))
964 ;; Unused?
965 (defalias 'mailcap-command-p 'executable-find)
967 (defun mailcap-mime-types ()
968 "Return a list of MIME media types."
969 (mailcap-parse-mimetypes)
970 (delete-dups
971 (nconc
972 (mapcar 'cdr mailcap-mime-extensions)
973 (apply
974 'nconc
975 (mapcar
976 (lambda (l)
977 (delq nil
978 (mapcar
979 (lambda (m)
980 (let ((type (cdr (assq 'type (cdr m)))))
981 (if (equal (cadr (split-string type "/"))
982 "*")
984 type)))
985 (cdr l))))
986 mailcap-mime-data)))))
989 ;;; Useful supplementary functions
992 (defun mailcap-file-default-commands (files)
993 "Return a list of default commands for FILES."
994 (mailcap-parse-mailcaps)
995 (mailcap-parse-mimetypes)
996 (let* ((all-mime-type
997 ;; All unique MIME types from file extensions
998 (delete-dups
999 (mapcar (lambda (file)
1000 (mailcap-extension-to-mime
1001 (file-name-extension file t)))
1002 files)))
1003 (all-mime-info
1004 ;; All MIME info lists
1005 (delete-dups
1006 (mapcar (lambda (mime-type)
1007 (mailcap-mime-info mime-type 'all))
1008 all-mime-type)))
1009 (common-mime-info
1010 ;; Intersection of mime-infos from different mime-types;
1011 ;; or just the first MIME info for a single MIME type
1012 (if (cdr all-mime-info)
1013 (delq nil (mapcar (lambda (mi1)
1014 (unless (memq nil (mapcar
1015 (lambda (mi2)
1016 (member mi1 mi2))
1017 (cdr all-mime-info)))
1018 mi1))
1019 (car all-mime-info)))
1020 (car all-mime-info)))
1021 (commands
1022 ;; Command strings from `viewer' field of the MIME info
1023 (delete-dups
1024 (delq nil (mapcar
1025 (lambda (mime-info)
1026 (let ((command (cdr (assoc 'viewer mime-info))))
1027 (if (stringp command)
1028 (replace-regexp-in-string
1029 ;; Replace mailcap's `%s' placeholder
1030 ;; with dired's `?' placeholder
1031 "%s" "?"
1032 (replace-regexp-in-string
1033 ;; Remove the final filename placeholder
1034 "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" ""
1035 command nil t)
1036 nil t))))
1037 common-mime-info)))))
1038 commands))
1040 (defun mailcap-view-mime (type)
1041 "View the data in the current buffer that has MIME type TYPE.
1042 `mailcap-mime-data' determines the method to use."
1043 (let ((method (mailcap-mime-info type)))
1044 (if (stringp method)
1045 (shell-command-on-region (point-min) (point-max)
1046 ;; Use stdin as the "%s".
1047 (format method "-")
1048 (current-buffer)
1050 (funcall method))))
1052 (provide 'mailcap)
1054 ;;; mailcap.el ends here