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