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