Update copyright notices for 2013.
[bpt/emacs.git] / lisp / gnus / mailcap.el
CommitLineData
c0393b5e 1;;; mailcap.el --- MIME media types configuration
e84b4b86 2
ab422c4d 3;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: William M. Perry <wmperry@aventail.com>
6;; Lars Magne Ingebrigtsen <larsi@gnus.org>
c0393b5e 7;; Keywords: news, mail, multimedia
c113de23
GM
8
9;; This file is part of GNU Emacs.
10
5e809f55 11;; GNU Emacs is free software: you can redistribute it and/or modify
c113de23 12;; it under the terms of the GNU General Public License as published by
5e809f55
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
c113de23
GM
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c113de23
GM
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
5e809f55 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c113de23
GM
23
24;;; Commentary:
25
c0393b5e
DL
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.
29
c113de23
GM
30;;; Code:
31
32(eval-when-compile (require 'cl))
b890d447
MB
33(autoload 'mail-header-parse-content-type "mail-parse")
34
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))
c113de23 41
d844ef2f
JL
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.
48If LITERAL is non-nil, insert NEWTEXT literally. Return a new
49string containing the replacements.
50This 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))))
54
c0393b5e
DL
55(defgroup mailcap nil
56 "Definition of viewers for MIME types."
57 :version "21.1"
58 :group 'mime)
59
c113de23
GM
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)
23f87bed
MB
67 "A syntax table for parsing SGML attributes.")
68
69(eval-and-compile
70 (when (featurep 'xemacs)
71 (condition-case nil
72 (require 'lpr)
73 (error nil))))
74
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 " ")
7877f373 85 "Shell command (including switches) used to print PostScript files.")
c113de23 86
c0393b5e
DL
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
c113de23 91(defvar mailcap-mime-data
23f87bed 92 `(("application"
2a9878fd 93 ("vnd\\.ms-excel"
23f87bed
MB
94 (viewer . "gnumeric %s")
95 (test . (getenv "DISPLAY"))
96 (type . "application/vnd.ms-excel"))
c113de23
GM
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"))
c113de23 109 ("dvi"
23f87bed
MB
110 (viewer . "xdvi -safer %s")
111 (test . (eq window-system 'x))
c113de23 112 ("needsx11")
23f87bed
MB
113 (type . "application/dvi")
114 ("print" . "dvips -qRP %s"))
c113de23
GM
115 ("dvi"
116 (viewer . "dvitty %s")
117 (test . (not (getenv "DISPLAY")))
23f87bed
MB
118 (type . "application/dvi")
119 ("print" . "dvips -qRP %s"))
c113de23
GM
120 ("emacs-lisp"
121 (viewer . mailcap-maybe-eval)
122 (type . "application/emacs-lisp"))
23f87bed
MB
123 ("x-emacs-lisp"
124 (viewer . mailcap-maybe-eval)
125 (type . "application/x-emacs-lisp"))
c113de23
GM
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"))
c0393b5e 155 ("pdf"
23f87bed 156 (viewer . "gv -safer %s")
c0393b5e 157 (type . "application/pdf")
23f87bed
MB
158 (test . window-system)
159 ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command)))
c0393b5e 160 ("pdf"
f0096211 161 (viewer . "gpdf %s")
c0393b5e 162 (type . "application/pdf")
23f87bed
MB
163 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
164 (test . (eq window-system 'x)))
c113de23 165 ("pdf"
f0096211
MB
166 (viewer . "xpdf %s")
167 (type . "application/pdf")
168 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
169 (test . (eq window-system 'x)))
23f87bed
MB
170 ("pdf"
171 (viewer . ,(concat "pdftotext %s -"))
172 (type . "application/pdf")
173 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
174 ("copiousoutput"))
c113de23 175 ("postscript"
c0393b5e
DL
176 (viewer . "gv -safer %s")
177 (type . "application/postscript")
178 (test . window-system)
23f87bed 179 ("print" . ,(concat mailcap-print-command " %s"))
c0393b5e 180 ("needsx11"))
c113de23
GM
181 ("postscript"
182 (viewer . "ghostview -dSAFER %s")
183 (type . "application/postscript")
23f87bed
MB
184 (test . (eq window-system 'x))
185 ("print" . ,(concat mailcap-print-command " %s"))
c113de23
GM
186 ("needsx11"))
187 ("postscript"
188 (viewer . "ps2ascii %s")
189 (type . "application/postscript")
190 (test . (not (getenv "DISPLAY")))
23f87bed
MB
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")))
c113de23
GM
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)
c113de23
GM
224 (type . "message/rfc822")))
225 ("image"
226 ("x-xwd"
227 (viewer . "xwud -in %s")
228 (type . "image/x-xwd")
229 ("compose" . "xwd -frame > %s")
23f87bed 230 (test . (eq window-system 'x))
c113de23
GM
231 ("needsx11"))
232 ("x11-dump"
233 (viewer . "xwud -in %s")
234 (type . "image/x-xwd")
235 ("compose" . "xwd -frame > %s")
23f87bed 236 (test . (eq window-system 'x))
c113de23
GM
237 ("needsx11"))
238 ("windowdump"
239 (viewer . "xwud -in %s")
240 (type . "image/x-xwd")
241 ("compose" . "xwd -frame > %s")
23f87bed 242 (test . (eq window-system 'x))
c113de23 243 ("needsx11"))
c113de23
GM
244 (".*"
245 (viewer . "display %s")
246 (type . "image/*")
23f87bed 247 (test . (eq window-system 'x))
c113de23
GM
248 ("needsx11"))
249 (".*"
250 (viewer . "ee %s")
251 (type . "image/*")
23f87bed 252 (test . (eq window-system 'x))
c113de23
GM
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"
23f87bed 267 (viewer . enriched-decode)
c113de23
GM
268 (test . (fboundp 'enriched-decode))
269 (type . "text/enriched"))
270 ("html"
271 (viewer . mm-w3-prepare-buffer)
272 (test . (fboundp 'w3-prepare-buffer))
01c52d31
MB
273 (type . "text/html"))
274 ("dns"
275 (viewer . dns-mode)
276 (test . (fboundp 'dns-mode))
277 (type . "text/dns")))
c113de23
GM
278 ("video"
279 ("mpeg"
280 (viewer . "mpeg_play %s")
281 (type . "video/mpeg")
23f87bed 282 (test . (eq window-system 'x))
c113de23
GM
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.
2961st assoc list is keyed on the major content-type
2972nd assoc list is keyed on the minor content-type (which can be a regexp)
298
299Which looks like:
300-----------------
301 ((\"application\"
302 (\"postscript\" . <info>))
303 (\"text\"
304 (\"plain\" . <info>)))
305
306Where <info> is another assoc list of the various information
c0393b5e 307related to the mailcap RFC 1524. This is keyed on the lowercase
c113de23 308attribute name (viewer, test, etc). This looks like:
c0393b5e
DL
309 ((viewer . VIEWERINFO)
310 (test . TESTINFO)
311 (xxxx . \"STRING\")
312 FLAG)
c113de23 313
c0393b5e 314Where VIEWERINFO specifies how the content-type is viewed. Can be
c113de23
GM
315a string, in which case it is run through a shell, with
316appropriate parameters, or a symbol, in which case the symbol is
c0393b5e
DL
317`funcall'ed, with the buffer as an argument.
318
319TESTINFO is a test for the viewer's applicability, or nil. If nil, it
320means the viewer is always valid. If it is a Lisp function, it is
321called with a list of items from any extra fields from the
322Content-Type header as argument to return a boolean value for the
323validity. Otherwise, if it is a non-function Lisp symbol or list
324whose car is a symbol, it is `eval'led to yield the validity. If it
325is a string or list of strings, it represents a shell command to run
326to return a true or false shell value for the validity.")
f8bbbafb 327(put 'mailcap-mime-data 'risky-local-variable t)
c0393b5e
DL
328
329(defcustom mailcap-download-directory nil
003dabb7 330 "*Directory to which `mailcap-save-binary-file' downloads files by default.
f0529b5b 331nil means your home directory."
003dabb7
DL
332 :type '(choice (const :tag "Home directory" nil)
333 directory)
c0393b5e 334 :group 'mailcap)
c113de23 335
23f87bed 336(defvar mailcap-poor-system-types
f5ec697d 337 '(ms-dos windows-nt)
23f87bed
MB
338 "Systems that don't have a Unix-like directory hierarchy.")
339
c113de23
GM
340;;;
341;;; Utility functions
342;;;
343
c113de23
GM
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))))
353
354(defvar mailcap-maybe-eval-warning
355 "*** WARNING ***
356
c0393b5e 357This MIME part contains untrusted and possibly harmful content.
c113de23
GM
358If you evaluate the Emacs Lisp code contained in it, a lot of nasty
359things can happen. Please examine the code very carefully before you
360instruct Emacs to evaluate it. You can browse the buffer containing
361the code using \\[scroll-other-window].
362
363If you are unsure what to do, please answer \"no\"."
364 "Text of warning message displayed by `mailcap-maybe-eval'.
365Make sure that this text consists only of few text lines. Otherwise,
366Gnus might fail to display all of it.")
a1506d29 367
c113de23 368(defun mailcap-maybe-eval ()
c0393b5e 369 "Maybe evaluate a buffer of Emacs Lisp code."
c113de23
GM
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
c0393b5e 379 (insert (substitute-command-keys
c113de23
GM
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)))))
389
390
391;;;
392;;; The mailcap parser
393;;;
394
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)))
400
401(defvar mailcap-parsed-p nil)
402
403(defun mailcap-parse-mailcaps (&optional path force)
404 "Parse out all the mailcaps specified in a path string PATH.
405Components of PATH are separated by the `path-separator' character
406appropriate for this system. If FORCE, re-parse even if already
407parsed. If PATH is omitted, use the value of environment variable
408MAILCAPS 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")))
23f87bed 416 ((memq system-type mailcap-poor-system-types)
c113de23
GM
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)
4def29e7 425 (split-string path path-separator t)
c113de23
GM
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)))
435
436(defun mailcap-parse-mailcap (fname)
c0393b5e 437 "Parse out the mailcap file specified by FNAME."
c113de23
GM
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
443 )
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
c0393b5e 477 ;;; RFC for mailcap files (#1524)
c113de23 478 (setq viewer "")
c0393b5e 479 (when (eq (char-after) ?\;)
c113de23
GM
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)
c0393b5e 500 (unless (equal viewer "")
c113de23
GM
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)))))
509
510(defun mailcap-parse-mailcap-extras (st nd)
c0393b5e 511 "Grab all the extra stuff from a mailcap entry."
c113de23
GM
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?
519 )
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))))
7347faa8
MB
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))
c113de23
GM
557 (skip-chars-forward " \";\n\t"))
558 results)))
559
560(defun mailcap-mailcap-entry-passes-test (info)
e7f767c2 561 "Return non-nil if mailcap entry INFO passes its test clause.
c0393b5e
DL
562Also return non-nil if no test clause is present."
563 (let ((test (assq 'test info)) ; The test clause
564 status)
c113de23
GM
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))))
585
586;;;
587;;; The action routines.
588;;;
589
590(defun mailcap-possible-viewers (major minor)
c0393b5e 591 "Return a list of possible viewers from MAJOR for minor type MINOR."
c113de23
GM
592 (let ((exact '())
593 (wildcard '()))
594 (while major
595 (cond
596 ((equal (car (car major)) minor)
597 (setq exact (cons (cdr (car major)) exact)))
23f87bed 598 ((and minor (string-match (concat "^" (car (car major)) "$") minor))
c113de23
GM
599 (setq wildcard (cons (cdr (car major)) wildcard))))
600 (setq major (cdr major)))
601 (nconc exact wildcard)))
602
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)))
c0393b5e
DL
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
c113de23
GM
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)) "\"\"")))
c0393b5e 637 ((memq save-chr '(?M ?n ?F))
c113de23
GM
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)))
c0393b5e 649 (t (error "Bad value to mailcap-unescape-mime-test: %s" test)))))
c113de23
GM
650
651(defvar mailcap-viewer-test-cache nil)
652
653(defun mailcap-viewer-passes-test (viewer-info type-info)
e7f767c2 654 "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
23f87bed 655Also return non-nil if it has no test clause. TYPE-INFO is an argument
c0393b5e 656to supply to the test."
c113de23
GM
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)
46e8fe3d
MB
663 (cond ((setq cache (assoc test mailcap-viewer-test-cache))
664 (cadr cache))
665 ((not test-info) t) ; No test clause
666 (t
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))
680 (t
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))))
c113de23
GM
688
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))
23f87bed
MB
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))
c113de23
GM
702 (equal (assq 'viewer info) ; Keep alternative viewer
703 (assq 'viewer cur-minor)))
23f87bed
MB
704 (setcdr cur-minor info))
705 (t
706 (setcdr old-major (cons (cons minor info) (cdr old-major))))))
c113de23
GM
707 )))
708
709(defun mailcap-add (type viewer &optional test)
710 "Add VIEWER as a handler for TYPE.
711If 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)))))
721
722;;;
723;;; The main whabbo
724;;;
725
726(defun mailcap-viewer-lessp (x y)
e7f767c2 727 "Return t if viewer X is more desirable than viewer Y."
c113de23
GM
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)
736 t)
737 ((and (not y-lisp) x-lisp)
738 t)
739 (t nil))))
740
b890d447 741(defun mailcap-mime-info (string &optional request no-decode)
c113de23
GM
742 "Get the MIME viewer command for STRING, return nil if none found.
743Expects a complete content-type header line as its argument.
744
745Second argument REQUEST specifies what information to return. If it is
746nil or the empty string, the viewer (second field of the mailcap
747entry) will be returned. If it is a string, then the mailcap field
748corresponding to that string will be returned (print, description,
749whatever). If a number, then all the information for this specific
750viewer is returned. If `all', then all possible viewers for
b890d447
MB
751this type is returned.
752
753If NO-DECODE is non-nil, don't decode STRING."
754 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
755 ;; `mail-parse.el'
c113de23
GM
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
b890d447
MB
769 (setq ctl
770 (if no-decode
771 (list (or string "text/plain"))
772 (mail-header-parse-content-type (or string "text/plain"))))
c113de23
GM
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)
b890d447 792 (mailcap-mime-info "default" request no-decode))
c113de23
GM
793 ((or (null request) (equal request ""))
794 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info))
795 ((stringp request)
23f87bed
MB
796 (mailcap-unescape-mime-test
797 (cdr-safe (assoc request viewer)) info))
c113de23
GM
798 ((eq request 'all)
799 passed)
800 (t
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)))))
808
809;;;
810;;; Experimental MIME-types parsing
811;;;
812
813(defvar mailcap-mime-extensions
a3f57c41
G
814 '(("" . "text/plain")
815 (".1" . "text/plain") ;; Manual pages
816 (".3" . "text/plain")
817 (".8" . "text/plain")
c0393b5e
DL
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")
a3f57c41 833 (".dpatch". "test/x-patch")
c0393b5e
DL
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")
a3f57c41 875 (".pod" . "text/plain")
c0393b5e
DL
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")
23f87bed 885 (".siv" . "application/sieve")
c0393b5e 886 (".snd" . "audio/basic")
01c52d31 887 (".soa" . "text/dns")
c0393b5e
DL
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")
23f87bed 904 (".xls" . "application/vnd.ms-excel")
c0393b5e
DL
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")
6e3165fb 912 (".jpeg" . "image/jpeg")
9e928ac9 913 (".org" . "text/x-org"))
c0393b5e
DL
914 "An alist of file extensions and corresponding MIME content-types.
915This exists for you to customize the information in Lisp. It is
916merged with values from mailcap files by `mailcap-parse-mimetypes'.")
c113de23
GM
917
918(defvar mailcap-mimetypes-parsed-p nil)
919
920(defun mailcap-parse-mimetypes (&optional path force)
c0393b5e 921 "Parse out all the mimetypes specified in a Unix-style path string PATH.
c113de23
GM
922Components of PATH are separated by the `path-separator' character
923appropriate for this system. If PATH is omitted, use the value of
924environment variable MIMETYPES if set; otherwise use a default path.
925If 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")))
23f87bed 932 ((memq system-type mailcap-poor-system-types)
c113de23
GM
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)
4def29e7 949 (split-string path path-separator t)
c113de23
GM
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)))
958
959(defun mailcap-parse-mimetype-file (fname)
c0393b5e 960 "Parse out a mime-types file FNAME."
c113de23
GM
961 (let (type ; The MIME type for this line
962 extns ; The extensions for this line
963 save-pos ; Misc. saved buffer positions
964 )
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)))))))
993
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)))
1001
c0393b5e
DL
1002;; Unused?
1003(defalias 'mailcap-command-p 'executable-find)
c113de23
GM
1004
1005(defun mailcap-mime-types ()
1006 "Return a list of MIME media types."
1007 (mailcap-parse-mimetypes)
b890d447 1008 (mailcap-delete-duplicates
9b198954
DL
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 "*")
1021 nil
1022 type)))
1023 (cdr l))))
1024 mailcap-mime-data)))))
c113de23 1025
a837fe74
JL
1026;;;
1027;;; Useful supplementary functions
1028;;;
1029
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
d844ef2f
JL
1036 (mailcap-delete-duplicates
1037 (mapcar (lambda (file)
1038 (mailcap-extension-to-mime
1039 (file-name-extension file t)))
1040 files)))
a837fe74
JL
1041 (all-mime-info
1042 ;; All MIME info lists
d844ef2f
JL
1043 (mailcap-delete-duplicates
1044 (mapcar (lambda (mime-type)
1045 (mailcap-mime-info mime-type 'all))
1046 all-mime-type)))
a837fe74
JL
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
d844ef2f 1061 (mailcap-delete-duplicates
a837fe74
JL
1062 (delq nil (mapcar (lambda (mime-info)
1063 (let ((command (cdr (assoc 'viewer mime-info))))
1064 (if (stringp command)
d844ef2f 1065 (mailcap-replace-in-string
a837fe74
JL
1066 ;; Replace mailcap's `%s' placeholder
1067 ;; with dired's `?' placeholder
d844ef2f 1068 (mailcap-replace-in-string
a837fe74 1069 ;; Remove the final filename placeholder
d844ef2f
JL
1070 command "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" "" t)
1071 "%s" "?" t))))
a837fe74
JL
1072 common-mime-info)))))
1073 commands))
1074
c113de23
GM
1075(provide 'mailcap)
1076
1077;;; mailcap.el ends here