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