1 ;;; mailcap.el --- MIME media types configuration
3 ;; Copyright (C) 1998-2012 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")
35 ;; `mm-delete-duplicates' is an alias for `delete-dups' in Emacs 22.
36 (defalias 'mailcap-delete-duplicates
37 (if (fboundp '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'.
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
))))
56 "Definition of viewers for MIME types."
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
)
67 "A syntax table for parsing SGML attributes.")
70 (when (featurep 'xemacs
)
75 (defvar mailcap-print-command
77 (cons (if (boundp 'lpr-command
)
80 (when (boundp 'lpr-switches
)
81 (if (stringp lpr-switches
)
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
94 (viewer .
"gnumeric %s")
95 (test .
(getenv "DISPLAY"))
96 (type .
"application/vnd.ms-excel"))
98 (viewer . ssl-view-site-cert
)
99 (test .
(fboundp 'ssl-view-site-cert
))
100 (type .
"application/x-x509-ca-cert"))
102 (viewer . ssl-view-user-cert
)
103 (test .
(fboundp 'ssl-view-user-cert
))
104 (type .
"application/x-x509-user-cert"))
106 (viewer . mailcap-save-binary-file
)
108 (type .
"application/octet-stream"))
110 (viewer .
"xdvi -safer %s")
111 (test .
(eq window-system
'x
))
113 (type .
"application/dvi")
114 ("print" .
"dvips -qRP %s"))
116 (viewer .
"dvitty %s")
117 (test .
(not (getenv "DISPLAY")))
118 (type .
"application/dvi")
119 ("print" .
"dvips -qRP %s"))
121 (viewer . mailcap-maybe-eval
)
122 (type .
"application/emacs-lisp"))
124 (viewer . mailcap-maybe-eval
)
125 (type .
"application/x-emacs-lisp"))
127 (viewer . mailcap-save-binary-file
)
129 (type .
"application/x-tar"))
132 (test .
(fboundp 'tex-mode
))
133 (type .
"application/x-latex"))
136 (test .
(fboundp 'tex-mode
))
137 (type .
"application/x-tex"))
140 (test .
(fboundp 'tex-mode
))
141 (type .
"application/latex"))
144 (test .
(fboundp 'tex-mode
))
145 (type .
"application/tex"))
147 (viewer . texinfo-mode
)
148 (test .
(fboundp 'texinfo-mode
))
149 (type .
"application/tex"))
151 (viewer . mailcap-save-binary-file
)
153 (type .
"application/zip")
156 (viewer .
"gv -safer %s")
157 (type .
"application/pdf")
158 (test . window-system
)
159 ("print" .
,(concat "pdf2ps %s - | " mailcap-print-command
)))
162 (type .
"application/pdf")
163 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
164 (test .
(eq window-system
'x
)))
167 (type .
"application/pdf")
168 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
169 (test .
(eq window-system
'x
)))
171 (viewer .
,(concat "pdftotext %s -"))
172 (type .
"application/pdf")
173 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
176 (viewer .
"gv -safer %s")
177 (type .
"application/postscript")
178 (test . window-system
)
179 ("print" .
,(concat mailcap-print-command
" %s"))
182 (viewer .
"ghostview -dSAFER %s")
183 (type .
"application/postscript")
184 (test .
(eq window-system
'x
))
185 ("print" .
,(concat mailcap-print-command
" %s"))
188 (viewer .
"ps2ascii %s")
189 (type .
"application/postscript")
190 (test .
(not (getenv "DISPLAY")))
191 ("print" .
,(concat mailcap-print-command
" %s"))
194 (viewer . sieve-mode
)
195 (test .
(fboundp 'sieve-mode
))
196 (type .
"application/sieve"))
198 (viewer .
"gpg --import --interactive --verbose")
199 (type .
"application/pgp-keys")
203 (viewer .
"maplay %s")
204 (type .
"audio/x-mpeg"))
206 (viewer .
"showaudio")
210 (viewer . mm-view-message
)
211 (test .
(and (featurep 'gnus
)
213 (type .
"message/rfc822"))
216 (test .
(fboundp 'vm-mode
))
217 (type .
"message/rfc822"))
220 (test .
(fboundp 'w3-mode
))
221 (type .
"message/rfc822"))
224 (type .
"message/rfc822")))
227 (viewer .
"xwud -in %s")
228 (type .
"image/x-xwd")
229 ("compose" .
"xwd -frame > %s")
230 (test .
(eq window-system
'x
))
233 (viewer .
"xwud -in %s")
234 (type .
"image/x-xwd")
235 ("compose" .
"xwd -frame > %s")
236 (test .
(eq window-system
'x
))
239 (viewer .
"xwud -in %s")
240 (type .
"image/x-xwd")
241 ("compose" .
"xwd -frame > %s")
242 (test .
(eq window-system
'x
))
245 (viewer .
"display %s")
247 (test .
(eq window-system
'x
))
252 (test .
(eq window-system
'x
))
257 (test .
(fboundp 'w3-mode
))
258 (type .
"text/plain"))
261 (test .
(fboundp 'view-mode
))
262 (type .
"text/plain"))
264 (viewer . fundamental-mode
)
265 (type .
"text/plain"))
267 (viewer . enriched-decode
)
268 (test .
(fboundp 'enriched-decode
))
269 (type .
"text/enriched"))
271 (viewer . mm-w3-prepare-buffer
)
272 (test .
(fboundp 'w3-prepare-buffer
))
273 (type .
"text/html"))
276 (test .
(fboundp 'dns-mode
))
277 (type .
"text/dns")))
280 (viewer .
"mpeg_play %s")
281 (type .
"video/mpeg")
282 (test .
(eq window-system
'x
))
286 (viewer .
"webspace -remote %s -URL %u")
287 (type .
"x-world/x-vrml")
293 (type .
"archive/tar")
294 (test .
(fboundp 'tar-mode
)))))
295 "The mailcap structure is an assoc list of assoc lists.
296 1st assoc list is keyed on the major content-type
297 2nd assoc list is keyed on the minor content-type (which can be a regexp)
302 (\"postscript\" . <info>))
304 (\"plain\" . <info>)))
306 Where <info> is another assoc list of the various information
307 related to the mailcap RFC 1524. This is keyed on the lowercase
308 attribute name (viewer, test, etc). This looks like:
309 ((viewer . VIEWERINFO)
314 Where VIEWERINFO specifies how the content-type is viewed. Can be
315 a string, in which case it is run through a shell, with
316 appropriate parameters, or a symbol, in which case the symbol is
317 `funcall'ed, with the buffer as an argument.
319 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
320 means the viewer is always valid. If it is a Lisp function, it is
321 called with a list of items from any extra fields from the
322 Content-Type header as argument to return a boolean value for the
323 validity. Otherwise, if it is a non-function Lisp symbol or list
324 whose car is a symbol, it is `eval'led to yield the validity. If it
325 is a string or list of strings, it represents a shell command to run
326 to return a true or false shell value for the validity.")
327 (put 'mailcap-mime-data
'risky-local-variable t
)
329 (defcustom mailcap-download-directory nil
330 "*Directory to which `mailcap-save-binary-file' downloads files by default.
331 nil means your home directory."
332 :type
'(choice (const :tag
"Home directory" nil
)
336 (defvar mailcap-poor-system-types
338 "Systems that don't have a Unix-like directory hierarchy.")
341 ;;; Utility functions
344 (defun mailcap-save-binary-file ()
345 (goto-char (point-min))
347 (let ((file (read-file-name
348 "Filename to save as: "
349 (or mailcap-download-directory
"~/")))
350 (require-final-newline nil
))
351 (write-region (point-min) (point-max) file
))
352 (kill-buffer (current-buffer))))
354 (defvar mailcap-maybe-eval-warning
357 This MIME part contains untrusted and possibly harmful content.
358 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
359 things can happen. Please examine the code very carefully before you
360 instruct Emacs to evaluate it. You can browse the buffer containing
361 the code using \\[scroll-other-window].
363 If you are unsure what to do, please answer \"no\"."
364 "Text of warning message displayed by `mailcap-maybe-eval'.
365 Make sure that this text consists only of few text lines. Otherwise,
366 Gnus might fail to display all of it.")
368 (defun mailcap-maybe-eval ()
369 "Maybe evaluate a buffer of Emacs Lisp code."
370 (let ((lisp-buffer (current-buffer)))
371 (goto-char (point-min))
373 (save-window-excursion
374 (delete-other-windows)
375 (let ((buffer (get-buffer-create (generate-new-buffer-name
378 (with-current-buffer buffer
379 (insert (substitute-command-keys
380 mailcap-maybe-eval-warning
))
381 (goto-char (point-min))
382 (display-buffer buffer
)
383 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
384 (kill-buffer buffer
))))
385 (eval-buffer (current-buffer)))
386 (when (buffer-live-p lisp-buffer
)
387 (with-current-buffer lisp-buffer
388 (emacs-lisp-mode)))))
392 ;;; The mailcap parser
395 (defun mailcap-replace-regexp (regexp to-string
)
396 ;; Quiet replace-regexp.
397 (goto-char (point-min))
398 (while (re-search-forward regexp nil t
)
399 (replace-match to-string t nil
)))
401 (defvar mailcap-parsed-p nil
)
403 (defun mailcap-parse-mailcaps (&optional path force
)
404 "Parse out all the mailcaps specified in a path string PATH.
405 Components of PATH are separated by the `path-separator' character
406 appropriate for this system. If FORCE, re-parse even if already
407 parsed. If PATH is omitted, use the value of environment variable
408 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
409 /usr/local/etc/mailcap."
410 (interactive (list nil t
))
411 (when (or (not mailcap-parsed-p
)
415 ((getenv "MAILCAPS") (setq path
(getenv "MAILCAPS")))
416 ((memq system-type mailcap-poor-system-types
)
417 (setq path
'("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
419 ;; This is per RFC 1524, specifically
420 ;; with /usr before /usr/local.
421 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
422 "/usr/local/etc/mailcap"))))
423 (let ((fnames (reverse
425 (split-string path path-separator t
)
429 (setq fname
(car fnames
))
430 (if (and (file-readable-p fname
)
431 (file-regular-p fname
))
432 (mailcap-parse-mailcap fname
))
433 (setq fnames
(cdr fnames
))))
434 (setq mailcap-parsed-p t
)))
436 (defun mailcap-parse-mailcap (fname)
437 "Parse out the mailcap file specified by FNAME."
438 (let (major ; The major mime type (image/audio/etc)
439 minor
; The minor mime type (gif, basic, etc)
440 save-pos
; Misc saved positions used in parsing
441 viewer
; How to view this mime type
442 info
; Misc info about this mime type
445 (insert-file-contents fname
)
446 (set-syntax-table mailcap-parse-args-syntax-table
)
447 (mailcap-replace-regexp "#.*" "") ; Remove all comments
448 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
449 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
450 (goto-char (point-max))
451 (skip-chars-backward " \t\n")
452 (delete-region (point) (point-max))
454 (skip-chars-backward " \t\n")
456 (setq save-pos
(point)
458 (skip-chars-forward "^/; \t\n")
459 (downcase-region save-pos
(point))
460 (setq major
(buffer-substring save-pos
(point)))
461 (skip-chars-forward " \t")
463 (when (eq (char-after) ?
/)
465 (skip-chars-forward " \t")
466 (setq save-pos
(point))
467 (skip-chars-forward "^; \t\n")
468 (downcase-region save-pos
(point))
471 ((eq ?
* (or (char-after save-pos
) 0)) ".*")
472 ((= (point) save-pos
) ".*")
473 (t (regexp-quote (buffer-substring save-pos
(point)))))))
474 (skip-chars-forward " \t")
475 ;;; Got the major/minor chunks, now for the viewers/etc
476 ;;; The first item _must_ be a viewer, according to the
477 ;;; RFC for mailcap files (#1524)
479 (when (eq (char-after) ?\
;)
481 (skip-chars-forward " \t")
482 (setq save-pos
(point))
483 (skip-chars-forward "^;\n")
485 (while (eq (char-before) ?
\\)
486 (backward-delete-char 1)
488 (skip-chars-forward "^;\n"))
489 (if (eq (or (char-after save-pos
) 0) ?
')
491 (narrow-to-region (1+ save-pos
) (point))
492 (goto-char (point-min))
494 (read (current-buffer))
495 (goto-char (point-max))
497 (setq viewer
(buffer-substring save-pos
(point)))))
498 (setq save-pos
(point))
500 (unless (equal viewer
"")
501 (setq info
(nconc (list (cons 'viewer viewer
)
502 (cons 'type
(concat major
"/"
503 (if (string= minor
".*")
505 (mailcap-parse-mailcap-extras save-pos
(point))))
506 (mailcap-mailcap-entry-passes-test info
)
507 (mailcap-add-mailcap-entry major minor info
))
508 (beginning-of-line)))))
510 (defun mailcap-parse-mailcap-extras (st nd
)
511 "Grab all the extra stuff from a mailcap entry."
515 results
; Assoc list of results
516 name-pos
; Start of XXXX= position
517 val-pos
; Start of value position
518 done
; Found end of \'d ;s?
521 (narrow-to-region st nd
)
522 (goto-char (point-min))
523 (skip-chars-forward " \n\t;")
526 (setq name-pos
(point))
527 (skip-chars-forward "^ \n\t=;")
528 (downcase-region name-pos
(point))
529 (setq name
(buffer-substring name-pos
(point)))
530 (skip-chars-forward " \t\n")
531 (if (not (eq (char-after (point)) ?
=)) ; There is no value
533 (skip-chars-forward " \t\n=")
534 (setq val-pos
(point))
535 (if (memq (char-after val-pos
) '(?
\" ?
'))
537 (setq val-pos
(1+ val-pos
))
542 (error (goto-char (point-max)))))
544 (skip-chars-forward "^;")
545 (if (eq (char-after (1- (point))) ?
\\ )
547 (subst-char-in-region (1- (point)) (point) ?
\\ ?
)
548 (skip-chars-forward ";"))
550 (setq value
(buffer-substring val-pos
(point))))
551 ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
553 (setq results
(cons (cons (if (string-equal name
"test")
557 (skip-chars-forward " \";\n\t"))
560 (defun mailcap-mailcap-entry-passes-test (info)
561 "Return non-nil if mailcap entry INFO passes its test clause.
562 Also return non-nil if no test clause is present."
563 (let ((test (assq 'test info
)) ; The test clause
565 (setq status
(and test
(split-string (cdr test
) " ")))
566 (if (and (or (assoc "needsterm" info
)
567 (assoc "needsterminal" info
)
568 (assoc "needsx11" info
))
569 (not (getenv "DISPLAY")))
572 ((and (equal (nth 0 status
) "test")
573 (equal (nth 1 status
) "-n")
574 (or (equal (nth 2 status
) "$DISPLAY")
575 (equal (nth 2 status
) "\"$DISPLAY\"")))
576 (setq status
(if (getenv "DISPLAY") t nil
)))
577 ((and (equal (nth 0 status
) "test")
578 (equal (nth 1 status
) "-z")
579 (or (equal (nth 2 status
) "$DISPLAY")
580 (equal (nth 2 status
) "\"$DISPLAY\"")))
581 (setq status
(if (getenv "DISPLAY") nil t
)))
584 (and test
(listp test
) (setcdr test status
))))
587 ;;; The action routines.
590 (defun mailcap-possible-viewers (major minor
)
591 "Return a list of possible viewers from MAJOR for minor type MINOR."
596 ((equal (car (car major
)) minor
)
597 (setq exact
(cons (cdr (car major
)) exact
)))
598 ((and minor
(string-match (concat "^" (car (car major
)) "$") minor
))
599 (setq wildcard
(cons (cdr (car major
)) wildcard
))))
600 (setq major
(cdr major
)))
601 (nconc exact wildcard
)))
603 (defun mailcap-unescape-mime-test (test type-info
)
604 (let (save-pos save-chr subst
)
606 ((symbolp test
) test
)
607 ((and (listp test
) (symbolp (car test
))) test
)
609 (and (listp test
) (stringp (car test
))
610 (setq test
(mapconcat 'identity test
" "))))
613 (goto-char (point-min))
615 (skip-chars-forward "^%")
617 (progn (skip-chars-backward "\\\\")
619 0) ; It is an escaped %
622 (skip-chars-forward "%."))
623 (setq save-pos
(point))
624 (skip-chars-forward "%")
625 (setq save-chr
(char-after (point)))
627 ;; %s: name of a file for the body data
629 ;; %{<parameter name}: value of parameter in mailcap entry
630 ;; %n: number of sub-parts for multipart content-type
631 ;; %F: a set of content-type/filename pairs for multiparts
633 ((null save-chr
) nil
)
635 (delete-region save-pos
(progn (forward-char 1) (point)))
636 (insert (or (cdr (assq 'type type-info
)) "\"\"")))
637 ((memq save-chr
'(?M ?n ?F
))
638 (delete-region save-pos
(progn (forward-char 1) (point)))
642 (skip-chars-forward "^}")
643 (downcase-region (+ 2 save-pos
) (point))
644 (setq subst
(buffer-substring (+ 2 save-pos
) (point)))
645 (delete-region save-pos
(1+ (point)))
646 (insert (or (cdr (assoc subst type-info
)) "\"\"")))
649 (t (error "Bad value to mailcap-unescape-mime-test: %s" test
)))))
651 (defvar mailcap-viewer-test-cache nil
)
653 (defun mailcap-viewer-passes-test (viewer-info type-info
)
654 "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
655 Also return non-nil if it has no test clause. TYPE-INFO is an argument
656 to supply to the test."
657 (let* ((test-info (assq 'test viewer-info
))
658 (test (cdr test-info
))
660 (viewer (cdr (assoc 'viewer viewer-info
)))
661 (default-directory (expand-file-name "~/"))
662 status parsed-test cache result
)
663 (cond ((setq cache
(assoc test mailcap-viewer-test-cache
))
665 ((not test-info
) t
) ; No test clause
670 ((not test
) nil
) ; Already failed test
671 ((eq test t
) t
) ; Already passed test
672 ((functionp test
) ; Lisp function as test
673 (funcall test type-info
))
674 ((and (symbolp test
) ; Lisp variable as test
677 ((and (listp test
) ; List to be eval'd
678 (symbolp (car test
)))
681 (setq test
(mailcap-unescape-mime-test test type-info
)
682 test
(list shell-file-name nil nil nil
683 shell-command-switch test
)
684 status
(apply 'call-process test
))
686 (push (list otest result
) mailcap-viewer-test-cache
)
689 (defun mailcap-add-mailcap-entry (major minor info
)
690 (let ((old-major (assoc major mailcap-mime-data
)))
691 (if (null old-major
) ; New major area
692 (setq mailcap-mime-data
693 (cons (cons major
(list (cons minor info
)))
695 (let ((cur-minor (assoc minor old-major
)))
697 ((or (null cur-minor
) ; New minor area, or
698 (assq 'test info
)) ; Has a test, insert at beginning
699 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))
700 ((and (not (assq 'test info
)) ; No test info, replace completely
701 (not (assq 'test cur-minor
))
702 (equal (assq 'viewer info
) ; Keep alternative viewer
703 (assq 'viewer cur-minor
)))
704 (setcdr cur-minor info
))
706 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))))
709 (defun mailcap-add (type viewer
&optional test
)
710 "Add VIEWER as a handler for TYPE.
711 If TEST is not given, it defaults to t."
712 (let ((tl (split-string type
"/")))
713 (when (or (not (car tl
))
715 (error "%s is not a valid MIME type" type
))
716 (mailcap-add-mailcap-entry
719 (test .
,(if test test t
))
726 (defun mailcap-viewer-lessp (x y
)
727 "Return t if viewer X is more desirable than viewer Y."
728 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x
)) "")))
729 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y
)) "")))
730 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x
)) ""))))
731 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y
)) "")))))
733 ((and x-wild
(not y-wild
))
735 ((and (not x-wild
) y-wild
)
737 ((and (not y-lisp
) x-lisp
)
741 (defun mailcap-mime-info (string &optional request no-decode
)
742 "Get the MIME viewer command for STRING, return nil if none found.
743 Expects a complete content-type header line as its argument.
745 Second argument REQUEST specifies what information to return. If it is
746 nil or the empty string, the viewer (second field of the mailcap
747 entry) will be returned. If it is a string, then the mailcap field
748 corresponding to that string will be returned (print, description,
749 whatever). If a number, then all the information for this specific
750 viewer is returned. If `all', then all possible viewers for
751 this type is returned.
753 If NO-DECODE is non-nil, don't decode STRING."
754 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
757 major
; Major encoding (text, etc)
758 minor
; Minor encoding (html, etc)
760 save-pos
; Misc. position during parse
761 major-info
; (assoc major mailcap-mime-data)
762 minor-info
; (assoc minor major-info)
763 test
; current test proc.
764 viewers
; Possible viewers
765 passed
; Viewers that passed the test
766 viewer
; The one and only viewer
771 (list (or string
"text/plain"))
772 (mail-header-parse-content-type (or string
"text/plain"))))
773 (setq major
(split-string (car ctl
) "/"))
774 (setq minor
(cadr major
)
776 (when (setq major-info
(cdr (assoc major mailcap-mime-data
)))
777 (when (setq viewers
(mailcap-possible-viewers major-info minor
))
778 (setq info
(mapcar (lambda (a) (cons (symbol-name (car a
))
782 (if (mailcap-viewer-passes-test (car viewers
) info
)
783 (setq passed
(cons (car viewers
) passed
)))
784 (setq viewers
(cdr viewers
)))
785 (setq passed
(sort passed
'mailcap-viewer-lessp
))
786 (setq viewer
(car passed
))))
787 (when (and (stringp (cdr (assq 'viewer viewer
)))
789 (setq viewer
(car passed
)))
791 ((and (null viewer
) (not (equal major
"default")) request
)
792 (mailcap-mime-info "default" request no-decode
))
793 ((or (null request
) (equal request
""))
794 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer
)) info
))
796 (mailcap-unescape-mime-test
797 (cdr-safe (assoc request viewer
)) info
))
801 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
802 (setq viewer
(copy-sequence viewer
))
803 (let ((view (assq 'viewer viewer
))
804 (test (assq 'test viewer
)))
805 (if view
(setcdr view
(mailcap-unescape-mime-test (cdr view
) info
)))
806 (if test
(setcdr test
(mailcap-unescape-mime-test (cdr test
) info
))))
810 ;;; Experimental MIME-types parsing
813 (defvar mailcap-mime-extensions
814 '(("" .
"text/plain")
815 (".1" .
"text/plain") ;; Manual pages
816 (".3" .
"text/plain")
817 (".8" .
"text/plain")
818 (".abs" .
"audio/x-mpeg")
819 (".aif" .
"audio/aiff")
820 (".aifc" .
"audio/aiff")
821 (".aiff" .
"audio/aiff")
822 (".ano" .
"application/x-annotator")
823 (".au" .
"audio/ulaw")
824 (".avi" .
"video/x-msvideo")
825 (".bcpio" .
"application/x-bcpio")
826 (".bin" .
"application/octet-stream")
827 (".cdf" .
"application/x-netcdr")
828 (".cpio" .
"application/x-cpio")
829 (".csh" .
"application/x-csh")
830 (".css" .
"text/css")
831 (".dvi" .
"application/x-dvi")
832 (".diff" .
"text/x-patch")
833 (".dpatch".
"test/x-patch")
834 (".el" .
"application/emacs-lisp")
835 (".eps" .
"application/postscript")
836 (".etx" .
"text/x-setext")
837 (".exe" .
"application/octet-stream")
838 (".fax" .
"image/x-fax")
839 (".gif" .
"image/gif")
840 (".hdf" .
"application/x-hdf")
841 (".hqx" .
"application/mac-binhex40")
842 (".htm" .
"text/html")
843 (".html" .
"text/html")
844 (".icon" .
"image/x-icon")
845 (".ief" .
"image/ief")
846 (".jpg" .
"image/jpeg")
847 (".macp" .
"image/x-macpaint")
848 (".man" .
"application/x-troff-man")
849 (".me" .
"application/x-troff-me")
850 (".mif" .
"application/mif")
851 (".mov" .
"video/quicktime")
852 (".movie" .
"video/x-sgi-movie")
853 (".mp2" .
"audio/x-mpeg")
854 (".mp3" .
"audio/x-mpeg")
855 (".mp2a" .
"audio/x-mpeg2")
856 (".mpa" .
"audio/x-mpeg")
857 (".mpa2" .
"audio/x-mpeg2")
858 (".mpe" .
"video/mpeg")
859 (".mpeg" .
"video/mpeg")
860 (".mpega" .
"audio/x-mpeg")
861 (".mpegv" .
"video/mpeg")
862 (".mpg" .
"video/mpeg")
863 (".mpv" .
"video/mpeg")
864 (".ms" .
"application/x-troff-ms")
865 (".nc" .
"application/x-netcdf")
866 (".nc" .
"application/x-netcdf")
867 (".oda" .
"application/oda")
868 (".patch" .
"text/x-patch")
869 (".pbm" .
"image/x-portable-bitmap")
870 (".pdf" .
"application/pdf")
871 (".pgm" .
"image/portable-graymap")
872 (".pict" .
"image/pict")
873 (".png" .
"image/png")
874 (".pnm" .
"image/x-portable-anymap")
875 (".pod" .
"text/plain")
876 (".ppm" .
"image/portable-pixmap")
877 (".ps" .
"application/postscript")
878 (".qt" .
"video/quicktime")
879 (".ras" .
"image/x-raster")
880 (".rgb" .
"image/x-rgb")
881 (".rtf" .
"application/rtf")
882 (".rtx" .
"text/richtext")
883 (".sh" .
"application/x-sh")
884 (".sit" .
"application/x-stuffit")
885 (".siv" .
"application/sieve")
886 (".snd" .
"audio/basic")
887 (".soa" .
"text/dns")
888 (".src" .
"application/x-wais-source")
889 (".tar" .
"archive/tar")
890 (".tcl" .
"application/x-tcl")
891 (".tex" .
"application/x-tex")
892 (".texi" .
"application/texinfo")
893 (".tga" .
"image/x-targa")
894 (".tif" .
"image/tiff")
895 (".tiff" .
"image/tiff")
896 (".tr" .
"application/x-troff")
897 (".troff" .
"application/x-troff")
898 (".tsv" .
"text/tab-separated-values")
899 (".txt" .
"text/plain")
900 (".vbs" .
"video/mpeg")
901 (".vox" .
"audio/basic")
902 (".vrml" .
"x-world/x-vrml")
903 (".wav" .
"audio/x-wav")
904 (".xls" .
"application/vnd.ms-excel")
905 (".wrl" .
"x-world/x-vrml")
906 (".xbm" .
"image/xbm")
907 (".xpm" .
"image/xpm")
908 (".xwd" .
"image/windowdump")
909 (".zip" .
"application/zip")
910 (".ai" .
"application/postscript")
911 (".jpe" .
"image/jpeg")
912 (".jpeg" .
"image/jpeg")
913 (".org" .
"text/x-org"))
914 "An alist of file extensions and corresponding MIME content-types.
915 This exists for you to customize the information in Lisp. It is
916 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
918 (defvar mailcap-mimetypes-parsed-p nil
)
920 (defun mailcap-parse-mimetypes (&optional path force
)
921 "Parse out all the mimetypes specified in a Unix-style path string PATH.
922 Components of PATH are separated by the `path-separator' character
923 appropriate for this system. If PATH is omitted, use the value of
924 environment variable MIMETYPES if set; otherwise use a default path.
925 If FORCE, re-parse even if already parsed."
926 (interactive (list nil t
))
927 (when (or (not mailcap-mimetypes-parsed-p
)
931 ((getenv "MIMETYPES") (setq path
(getenv "MIMETYPES")))
932 ((memq system-type mailcap-poor-system-types
)
933 (setq path
'("~/mime.typ" "~/etc/mime.typ")))
935 ;; mime.types seems to be the normal name, definitely so
936 ;; on current GNUish systems. The search order follows
940 "/usr/etc/mime.types"
941 "/usr/local/etc/mime.types"
942 "/usr/local/www/conf/mime.types"
945 "/usr/etc/mime-types"
946 "/usr/local/etc/mime-types"
947 "/usr/local/www/conf/mime-types"))))
948 (let ((fnames (reverse (if (stringp path
)
949 (split-string path path-separator t
)
953 (setq fname
(car fnames
))
954 (if (and (file-readable-p fname
))
955 (mailcap-parse-mimetype-file fname
))
956 (setq fnames
(cdr fnames
))))
957 (setq mailcap-mimetypes-parsed-p t
)))
959 (defun mailcap-parse-mimetype-file (fname)
960 "Parse out a mime-types file FNAME."
961 (let (type ; The MIME type for this line
962 extns
; The extensions for this line
963 save-pos
; Misc. saved buffer positions
966 (insert-file-contents fname
)
967 (mailcap-replace-regexp "#.*" "")
968 (mailcap-replace-regexp "\n+" "\n")
969 (mailcap-replace-regexp "[ \t]+$" "")
970 (goto-char (point-max))
971 (skip-chars-backward " \t\n")
972 (delete-region (point) (point-max))
973 (goto-char (point-min))
975 (skip-chars-forward " \t\n")
976 (setq save-pos
(point))
977 (skip-chars-forward "^ \t\n")
978 (downcase-region save-pos
(point))
979 (setq type
(buffer-substring save-pos
(point)))
981 (skip-chars-forward " \t")
982 (setq save-pos
(point))
983 (skip-chars-forward "^ \t\n")
984 (setq extns
(cons (buffer-substring save-pos
(point)) extns
)))
986 (setq mailcap-mime-extensions
988 (cons (if (= (string-to-char (car extns
)) ?.
)
990 (concat "." (car extns
))) type
)
991 mailcap-mime-extensions
)
992 extns
(cdr extns
)))))))
994 (defun mailcap-extension-to-mime (extn)
995 "Return the MIME content type of the file extensions EXTN."
996 (mailcap-parse-mimetypes)
997 (if (and (stringp extn
)
998 (not (eq (string-to-char extn
) ?.
)))
999 (setq extn
(concat "." extn
)))
1000 (cdr (assoc (downcase extn
) mailcap-mime-extensions
)))
1003 (defalias 'mailcap-command-p
'executable-find
)
1005 (defun mailcap-mime-types ()
1006 "Return a list of MIME media types."
1007 (mailcap-parse-mimetypes)
1008 (mailcap-delete-duplicates
1010 (mapcar 'cdr mailcap-mime-extensions
)
1018 (let ((type (cdr (assq 'type
(cdr m
)))))
1019 (if (equal (cadr (split-string type
"/"))
1024 mailcap-mime-data
)))))
1027 ;;; Useful supplementary functions
1030 (defun mailcap-file-default-commands (files)
1031 "Return a list of default commands for FILES."
1032 (mailcap-parse-mailcaps)
1033 (mailcap-parse-mimetypes)
1034 (let* ((all-mime-type
1035 ;; All unique MIME types from file extensions
1036 (mailcap-delete-duplicates
1037 (mapcar (lambda (file)
1038 (mailcap-extension-to-mime
1039 (file-name-extension file t
)))
1042 ;; All MIME info lists
1043 (mailcap-delete-duplicates
1044 (mapcar (lambda (mime-type)
1045 (mailcap-mime-info mime-type
'all
))
1048 ;; Intersection of mime-infos from different mime-types;
1049 ;; or just the first MIME info for a single MIME type
1050 (if (cdr all-mime-info
)
1051 (delq nil
(mapcar (lambda (mi1)
1052 (unless (memq nil
(mapcar
1055 (cdr all-mime-info
)))
1057 (car all-mime-info
)))
1058 (car all-mime-info
)))
1060 ;; Command strings from `viewer' field of the MIME info
1061 (mailcap-delete-duplicates
1062 (delq nil
(mapcar (lambda (mime-info)
1063 (let ((command (cdr (assoc 'viewer mime-info
))))
1064 (if (stringp command
)
1065 (mailcap-replace-in-string
1066 ;; Replace mailcap's `%s' placeholder
1067 ;; with dired's `?' placeholder
1068 (mailcap-replace-in-string
1069 ;; Remove the final filename placeholder
1070 command
"[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" "" t
)
1072 common-mime-info
)))))
1077 ;;; mailcap.el ends here