1 ;;; mailcap.el --- MIME media types configuration
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: William M. Perry <wmperry@aventail.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news, mail, multimedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
32 (eval-when-compile (require 'cl
))
33 (autoload 'mail-header-parse-content-type
"mail-parse")
36 "Definition of viewers for MIME types."
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
)
47 "A syntax table for parsing SGML attributes.")
49 (defvar mailcap-print-command
51 (cons (if (boundp 'lpr-command
)
54 (when (boundp 'lpr-switches
)
55 (if (stringp lpr-switches
)
59 "Shell command (including switches) used to print PostScript files.")
61 (defun mailcap--get-user-mime-data (sym)
62 (let ((val (default-value sym
))
65 (setq res
(cons (list (cdr (assq 'viewer entry
))
66 (cdr (assq 'type entry
))
67 (cdr (assq 'test entry
)))
71 (defun mailcap--set-user-mime-data (sym val
)
74 (setq res
(cons `((viewer .
,(car entry
))
75 (type .
,(cadr entry
))
77 `((test .
,(caddr entry
)))))
79 (set-default sym
(nreverse res
))))
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
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.
94 MIME-TYPE is a regular expression being matched against the
95 actual MIME type. It is implicitly surrounded with ^ and $.
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.
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'
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
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
121 (viewer .
"gnumeric %s")
122 (test .
(getenv "DISPLAY"))
123 (type .
"application/vnd.ms-excel"))
125 (viewer . ssl-view-site-cert
)
126 (type .
"application/x-x509-ca-cert"))
128 (viewer . ssl-view-user-cert
)
129 (type .
"application/x-x509-user-cert"))
131 (viewer . mailcap-save-binary-file
)
133 (type .
"application/octet-stream"))
135 (viewer .
"xdvi -safer %s")
136 (test .
(eq window-system
'x
))
138 (type .
"application/dvi")
139 ("print" .
"dvips -qRP %s"))
141 (viewer .
"dvitty %s")
142 (test .
(not (getenv "DISPLAY")))
143 (type .
"application/dvi")
144 ("print" .
"dvips -qRP %s"))
146 (viewer . mailcap-maybe-eval
)
147 (type .
"application/emacs-lisp"))
149 (viewer . mailcap-maybe-eval
)
150 (type .
"application/x-emacs-lisp"))
152 (viewer . mailcap-save-binary-file
)
154 (type .
"application/x-tar"))
157 (type .
"application/x-latex"))
160 (type .
"application/x-tex"))
163 (type .
"application/latex"))
166 (type .
"application/tex"))
168 (viewer . texinfo-mode
)
169 (type .
"application/tex"))
171 (viewer . mailcap-save-binary-file
)
173 (type .
"application/zip")
176 (viewer . pdf-view-mode
)
177 (type .
"application/pdf")
178 (test .
(eq window-system
'x
)))
180 (viewer . doc-view-mode
)
181 (type .
"application/pdf")
182 (test .
(eq window-system
'x
)))
184 (viewer .
"gv -safer %s")
185 (type .
"application/pdf")
186 (test . window-system
)
187 ("print" .
,(concat "pdf2ps %s - | " mailcap-print-command
)))
190 (type .
"application/pdf")
191 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
192 (test .
(eq window-system
'x
)))
195 (type .
"application/pdf")
196 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
197 (test .
(eq window-system
'x
)))
199 (viewer .
,(concat "pdftotext %s -"))
200 (type .
"application/pdf")
201 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
204 (viewer .
"gv -safer %s")
205 (type .
"application/postscript")
206 (test . window-system
)
207 ("print" .
,(concat mailcap-print-command
" %s"))
210 (viewer .
"ghostview -dSAFER %s")
211 (type .
"application/postscript")
212 (test .
(eq window-system
'x
))
213 ("print" .
,(concat mailcap-print-command
" %s"))
216 (viewer .
"ps2ascii %s")
217 (type .
"application/postscript")
218 (test .
(not (getenv "DISPLAY")))
219 ("print" .
,(concat mailcap-print-command
" %s"))
222 (viewer . sieve-mode
)
223 (type .
"application/sieve"))
225 (viewer .
"gpg --import --interactive --verbose")
226 (type .
"application/pgp-keys")
230 (viewer .
"maplay %s")
231 (type .
"audio/x-mpeg"))
233 (viewer .
"showaudio")
237 (viewer . mm-view-message
)
238 (test .
(and (featurep 'gnus
)
240 (type .
"message/rfc822"))
243 (type .
"message/rfc822"))
246 (type .
"message/rfc822")))
249 (viewer .
"xwud -in %s")
250 (type .
"image/x-xwd")
251 ("compose" .
"xwd -frame > %s")
252 (test .
(eq window-system
'x
))
255 (viewer .
"xwud -in %s")
256 (type .
"image/x-xwd")
257 ("compose" .
"xwd -frame > %s")
258 (test .
(eq window-system
'x
))
261 (viewer .
"xwud -in %s")
262 (type .
"image/x-xwd")
263 ("compose" .
"xwd -frame > %s")
264 (test .
(eq window-system
'x
))
267 (viewer .
"display %s")
269 (test .
(eq window-system
'x
))
274 (test .
(eq window-system
'x
))
279 (type .
"text/plain"))
281 (viewer . fundamental-mode
)
282 (type .
"text/plain"))
284 (viewer . enriched-decode
)
285 (type .
"text/enriched"))
288 (type .
"text/dns")))
291 (viewer .
"mpeg_play %s")
292 (type .
"video/mpeg")
293 (test .
(eq window-system
'x
))
297 (viewer .
"webspace -remote %s -URL %u")
298 (type .
"x-world/x-vrml")
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)
312 (\"postscript\" . <info>))
314 (\"plain\" . <info>)))
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)
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.
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
)
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
)
346 (defvar mailcap-poor-system-types
348 "Systems that don't have a Unix-like directory hierarchy.")
351 ;;; Utility functions
354 (defun mailcap-save-binary-file ()
355 (goto-char (point-min))
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))))
364 (defvar mailcap-maybe-eval-warning
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].
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.")
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))
383 (save-window-excursion
384 (delete-other-windows)
385 (let ((buffer (get-buffer-create (generate-new-buffer-name
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)))))
402 ;;; The mailcap parser
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
)))
411 (defvar mailcap-parsed-p nil
)
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
)
425 ((getenv "MAILCAPS") (setq path
(getenv "MAILCAPS")))
426 ((memq system-type mailcap-poor-system-types
)
427 (setq path
'("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
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
435 (split-string path path-separator t
)
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
)))
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
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))
464 (skip-chars-backward " \t\n")
466 (setq save-pos
(point)
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")
473 (when (eq (char-after) ?
/)
475 (skip-chars-forward " \t")
476 (setq save-pos
(point))
477 (skip-chars-forward "^; \t\n")
478 (downcase-region save-pos
(point))
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)
489 (when (eq (char-after) ?\
;)
491 (skip-chars-forward " \t")
492 (setq save-pos
(point))
493 (skip-chars-forward "^;\n")
495 (while (eq (char-before) ?
\\)
496 (backward-delete-char 1)
498 (skip-chars-forward "^;\n"))
499 (if (eq (or (char-after save-pos
) 0) ?
')
501 (narrow-to-region (1+ save-pos
) (point))
502 (goto-char (point-min))
504 (read (current-buffer))
505 (goto-char (point-max))
507 (setq viewer
(buffer-substring save-pos
(point)))))
508 (setq save-pos
(point))
510 (unless (equal viewer
"")
511 (setq info
(nconc (list (cons 'viewer viewer
)
512 (cons 'type
(concat major
"/"
513 (if (string= 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)))))
520 (defun mailcap-parse-mailcap-extras (st nd
)
521 "Grab all the extra stuff from a mailcap entry."
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?
531 (narrow-to-region st nd
)
532 (goto-char (point-min))
533 (skip-chars-forward " \n\t;")
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
543 (skip-chars-forward " \t\n=")
544 (setq val-pos
(point))
545 (if (memq (char-after val-pos
) '(?
\" ?
'))
547 (setq val-pos
(1+ val-pos
))
552 (error (goto-char (point-max)))))
554 (skip-chars-forward "^;")
555 (if (eq (char-after (1- (point))) ?
\\ )
557 (subst-char-in-region (1- (point)) (point) ?
\\ ?
)
558 (skip-chars-forward ";"))
560 (setq value
(buffer-substring val-pos
(point))))
561 ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
563 (setq results
(cons (cons (if (string-equal name
"test")
567 (skip-chars-forward " \";\n\t"))
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
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")))
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
)))
597 (and test
(listp test
) (setcdr test status
))))
600 ;;; The action routines.
603 (defun mailcap-possible-viewers (major minor
)
604 "Return a list of possible viewers from MAJOR for minor type MINOR."
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
)))
616 (defun mailcap-unescape-mime-test (test type-info
)
617 (let (save-pos save-chr subst
)
619 ((symbolp test
) test
)
620 ((and (listp test
) (symbolp (car test
))) test
)
622 (and (listp test
) (stringp (car test
))
623 (setq test
(mapconcat 'identity test
" "))))
626 (goto-char (point-min))
628 (skip-chars-forward "^%")
630 (progn (skip-chars-backward "\\\\")
632 0) ; It is an escaped %
635 (skip-chars-forward "%."))
636 (setq save-pos
(point))
637 (skip-chars-forward "%")
638 (setq save-chr
(char-after (point)))
640 ;; %s: name of a file for the body data
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
646 ((null save-chr
) nil
)
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)))
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
)) "\"\"")))
662 (t (error "Bad value to mailcap-unescape-mime-test: %s" test
)))))
664 (defvar mailcap-viewer-test-cache nil
)
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
))
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
))
680 ((not test-info
) t
) ; No test clause
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
692 ((and (listp test
) ; List to be eval'd
693 (symbolp (car test
)))
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
))
701 (push (list otest result
) mailcap-viewer-test-cache
)
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
)))
710 (let ((cur-minor (assoc minor old-major
)))
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
))
721 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))))
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
))
730 (error "%s is not a valid MIME type" type
))
731 (mailcap-add-mailcap-entry
734 (test .
,(if test test t
))
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
)) "")))))
748 ((and x-wild
(not y-wild
))
750 ((and (not x-wild
) y-wild
)
752 ((and (not y-lisp
) x-lisp
)
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
))
762 (dolist (entry mailcap-user-mime-data
)
763 (when (and (null viewer
)
764 (string-match (concat "^" (cdr (assq 'type entry
)) "$")
766 (mailcap-viewer-passes-test entry info
))
767 (setq viewer entry
)))
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.
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.
782 If NO-DECODE is non-nil, don't decode STRING."
783 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
786 major
; Major encoding (text, etc)
787 minor
; Minor encoding (html, etc)
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
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
))
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
)
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
))
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
)))
824 (setq viewer
(car passed
))))
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
))
831 (mailcap-unescape-mime-test
832 (cdr-safe (assoc request viewer
)) info
))
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
))))
845 ;;; Experimental MIME-types parsing
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'.")
953 (defvar mailcap-mimetypes-parsed-p nil
)
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
)
966 ((getenv "MIMETYPES") (setq path
(getenv "MIMETYPES")))
967 ((memq system-type mailcap-poor-system-types
)
968 (setq path
'("~/mime.typ" "~/etc/mime.typ")))
970 ;; mime.types seems to be the normal name, definitely so
971 ;; on current GNUish systems. The search order follows
975 "/usr/etc/mime.types"
976 "/usr/local/etc/mime.types"
977 "/usr/local/www/conf/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
)
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
)))
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
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))
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)))
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
)))
1021 (setq mailcap-mime-extensions
1023 (cons (if (= (string-to-char (car extns
)) ?.
)
1025 (concat "." (car extns
))) type
)
1026 mailcap-mime-extensions
)
1027 extns
(cdr extns
)))))))
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
)))
1038 (defalias 'mailcap-command-p
'executable-find
)
1040 (defun mailcap-mime-types ()
1041 "Return a list of MIME media types."
1042 (mailcap-parse-mimetypes)
1045 (mapcar 'cdr mailcap-mime-extensions
)
1053 (let ((type (cdr (assq 'type
(cdr m
)))))
1054 (if (equal (cadr (split-string type
"/"))
1059 mailcap-mime-data
)))))
1062 ;;; Useful supplementary functions
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
1072 (mapcar (lambda (file)
1073 (mailcap-extension-to-mime
1074 (file-name-extension file t
)))
1077 ;; All MIME info lists
1079 (mapcar (lambda (mime-type)
1080 (mailcap-mime-info mime-type
'all
))
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
1090 (cdr all-mime-info
)))
1092 (car all-mime-info
)))
1093 (car all-mime-info
)))
1095 ;; Command strings from `viewer' field of the 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
1105 (replace-regexp-in-string
1106 ;; Remove the final filename placeholder
1107 "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" ""
1110 common-mime-info
)))))
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".
1127 ;;; mailcap.el ends here