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 ;; Postpone using defcustom for this as it's so big and we essentially
62 ;; have to have two copies of the data around then. Perhaps just
63 ;; customize the Lisp viewers and rely on the normal configuration
64 ;; files for the rest? -- fx
65 (defvar mailcap-mime-data
68 (viewer .
"gnumeric %s")
69 (test .
(getenv "DISPLAY"))
70 (type .
"application/vnd.ms-excel"))
72 (viewer . ssl-view-site-cert
)
73 (type .
"application/x-x509-ca-cert"))
75 (viewer . ssl-view-user-cert
)
76 (type .
"application/x-x509-user-cert"))
78 (viewer . mailcap-save-binary-file
)
80 (type .
"application/octet-stream"))
82 (viewer .
"xdvi -safer %s")
83 (test .
(eq window-system
'x
))
85 (type .
"application/dvi")
86 ("print" .
"dvips -qRP %s"))
88 (viewer .
"dvitty %s")
89 (test .
(not (getenv "DISPLAY")))
90 (type .
"application/dvi")
91 ("print" .
"dvips -qRP %s"))
93 (viewer . mailcap-maybe-eval
)
94 (type .
"application/emacs-lisp"))
96 (viewer . mailcap-maybe-eval
)
97 (type .
"application/x-emacs-lisp"))
99 (viewer . mailcap-save-binary-file
)
101 (type .
"application/x-tar"))
104 (type .
"application/x-latex"))
107 (type .
"application/x-tex"))
110 (type .
"application/latex"))
113 (type .
"application/tex"))
115 (viewer . texinfo-mode
)
116 (type .
"application/tex"))
118 (viewer . mailcap-save-binary-file
)
120 (type .
"application/zip")
123 (viewer . pdf-view-mode
)
124 (type .
"application/pdf")
125 (test .
(eq window-system
'x
)))
127 (viewer . doc-view-mode
)
128 (type .
"application/pdf")
129 (test .
(eq window-system
'x
)))
131 (viewer .
"gv -safer %s")
132 (type .
"application/pdf")
133 (test . window-system
)
134 ("print" .
,(concat "pdf2ps %s - | " mailcap-print-command
)))
137 (type .
"application/pdf")
138 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
139 (test .
(eq window-system
'x
)))
142 (type .
"application/pdf")
143 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
144 (test .
(eq window-system
'x
)))
146 (viewer .
,(concat "pdftotext %s -"))
147 (type .
"application/pdf")
148 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
151 (viewer .
"gv -safer %s")
152 (type .
"application/postscript")
153 (test . window-system
)
154 ("print" .
,(concat mailcap-print-command
" %s"))
157 (viewer .
"ghostview -dSAFER %s")
158 (type .
"application/postscript")
159 (test .
(eq window-system
'x
))
160 ("print" .
,(concat mailcap-print-command
" %s"))
163 (viewer .
"ps2ascii %s")
164 (type .
"application/postscript")
165 (test .
(not (getenv "DISPLAY")))
166 ("print" .
,(concat mailcap-print-command
" %s"))
169 (viewer . sieve-mode
)
170 (type .
"application/sieve"))
172 (viewer .
"gpg --import --interactive --verbose")
173 (type .
"application/pgp-keys")
177 (viewer .
"maplay %s")
178 (type .
"audio/x-mpeg"))
180 (viewer .
"showaudio")
184 (viewer . mm-view-message
)
185 (test .
(and (featurep 'gnus
)
187 (type .
"message/rfc822"))
190 (type .
"message/rfc822"))
193 (type .
"message/rfc822")))
196 (viewer .
"xwud -in %s")
197 (type .
"image/x-xwd")
198 ("compose" .
"xwd -frame > %s")
199 (test .
(eq window-system
'x
))
202 (viewer .
"xwud -in %s")
203 (type .
"image/x-xwd")
204 ("compose" .
"xwd -frame > %s")
205 (test .
(eq window-system
'x
))
208 (viewer .
"xwud -in %s")
209 (type .
"image/x-xwd")
210 ("compose" .
"xwd -frame > %s")
211 (test .
(eq window-system
'x
))
214 (viewer .
"display %s")
216 (test .
(eq window-system
'x
))
221 (test .
(eq window-system
'x
))
226 (type .
"text/plain"))
228 (viewer . fundamental-mode
)
229 (type .
"text/plain"))
231 (viewer . enriched-decode
)
232 (type .
"text/enriched"))
235 (type .
"text/dns")))
238 (viewer .
"mpeg_play %s")
239 (type .
"video/mpeg")
240 (test .
(eq window-system
'x
))
244 (viewer .
"webspace -remote %s -URL %u")
245 (type .
"x-world/x-vrml")
251 (type .
"archive/tar"))))
252 "The mailcap structure is an assoc list of assoc lists.
253 1st assoc list is keyed on the major content-type
254 2nd assoc list is keyed on the minor content-type (which can be a regexp)
259 (\"postscript\" . <info>))
261 (\"plain\" . <info>)))
263 Where <info> is another assoc list of the various information
264 related to the mailcap RFC 1524. This is keyed on the lowercase
265 attribute name (viewer, test, etc). This looks like:
266 ((viewer . VIEWERINFO)
271 Where VIEWERINFO specifies how the content-type is viewed. Can be
272 a string, in which case it is run through a shell, with appropriate
273 parameters, or a symbol, in which case the symbol is `funcall'ed if
274 and only if it exists as a function, with the buffer as an argument.
276 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
277 means the viewer is always valid. If it is a Lisp function, it is
278 called with a list of items from any extra fields from the
279 Content-Type header as argument to return a boolean value for the
280 validity. Otherwise, if it is a non-function Lisp symbol or list
281 whose car is a symbol, it is `eval'led to yield the validity. If it
282 is a string or list of strings, it represents a shell command to run
283 to return a true or false shell value for the validity.")
284 (put 'mailcap-mime-data
'risky-local-variable t
)
286 (defcustom mailcap-download-directory nil
287 "*Directory to which `mailcap-save-binary-file' downloads files by default.
288 nil means your home directory."
289 :type
'(choice (const :tag
"Home directory" nil
)
293 (defvar mailcap-poor-system-types
295 "Systems that don't have a Unix-like directory hierarchy.")
298 ;;; Utility functions
301 (defun mailcap-save-binary-file ()
302 (goto-char (point-min))
304 (let ((file (read-file-name
305 "Filename to save as: "
306 (or mailcap-download-directory
"~/")))
307 (require-final-newline nil
))
308 (write-region (point-min) (point-max) file
))
309 (kill-buffer (current-buffer))))
311 (defvar mailcap-maybe-eval-warning
314 This MIME part contains untrusted and possibly harmful content.
315 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
316 things can happen. Please examine the code very carefully before you
317 instruct Emacs to evaluate it. You can browse the buffer containing
318 the code using \\[scroll-other-window].
320 If you are unsure what to do, please answer \"no\"."
321 "Text of warning message displayed by `mailcap-maybe-eval'.
322 Make sure that this text consists only of few text lines. Otherwise,
323 Gnus might fail to display all of it.")
325 (defun mailcap-maybe-eval ()
326 "Maybe evaluate a buffer of Emacs Lisp code."
327 (let ((lisp-buffer (current-buffer)))
328 (goto-char (point-min))
330 (save-window-excursion
331 (delete-other-windows)
332 (let ((buffer (get-buffer-create (generate-new-buffer-name
335 (with-current-buffer buffer
336 (insert (substitute-command-keys
337 mailcap-maybe-eval-warning
))
338 (goto-char (point-min))
339 (display-buffer buffer
)
340 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
341 (kill-buffer buffer
))))
342 (eval-buffer (current-buffer)))
343 (when (buffer-live-p lisp-buffer
)
344 (with-current-buffer lisp-buffer
345 (emacs-lisp-mode)))))
349 ;;; The mailcap parser
352 (defun mailcap-replace-regexp (regexp to-string
)
353 ;; Quiet replace-regexp.
354 (goto-char (point-min))
355 (while (re-search-forward regexp nil t
)
356 (replace-match to-string t nil
)))
358 (defvar mailcap-parsed-p nil
)
360 (defun mailcap-parse-mailcaps (&optional path force
)
361 "Parse out all the mailcaps specified in a path string PATH.
362 Components of PATH are separated by the `path-separator' character
363 appropriate for this system. If FORCE, re-parse even if already
364 parsed. If PATH is omitted, use the value of environment variable
365 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
366 /usr/local/etc/mailcap."
367 (interactive (list nil t
))
368 (when (or (not mailcap-parsed-p
)
372 ((getenv "MAILCAPS") (setq path
(getenv "MAILCAPS")))
373 ((memq system-type mailcap-poor-system-types
)
374 (setq path
'("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
376 ;; This is per RFC 1524, specifically
377 ;; with /usr before /usr/local.
378 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
379 "/usr/local/etc/mailcap"))))
380 (let ((fnames (reverse
382 (split-string path path-separator t
)
386 (setq fname
(car fnames
))
387 (if (and (file-readable-p fname
)
388 (file-regular-p fname
))
389 (mailcap-parse-mailcap fname
))
390 (setq fnames
(cdr fnames
))))
391 (setq mailcap-parsed-p t
)))
393 (defun mailcap-parse-mailcap (fname)
394 "Parse out the mailcap file specified by FNAME."
395 (let (major ; The major mime type (image/audio/etc)
396 minor
; The minor mime type (gif, basic, etc)
397 save-pos
; Misc saved positions used in parsing
398 viewer
; How to view this mime type
399 info
; Misc info about this mime type
402 (insert-file-contents fname
)
403 (set-syntax-table mailcap-parse-args-syntax-table
)
404 (mailcap-replace-regexp "#.*" "") ; Remove all comments
405 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
406 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
407 (goto-char (point-max))
408 (skip-chars-backward " \t\n")
409 (delete-region (point) (point-max))
411 (skip-chars-backward " \t\n")
413 (setq save-pos
(point)
415 (skip-chars-forward "^/; \t\n")
416 (downcase-region save-pos
(point))
417 (setq major
(buffer-substring save-pos
(point)))
418 (skip-chars-forward " \t")
420 (when (eq (char-after) ?
/)
422 (skip-chars-forward " \t")
423 (setq save-pos
(point))
424 (skip-chars-forward "^; \t\n")
425 (downcase-region save-pos
(point))
428 ((eq ?
* (or (char-after save-pos
) 0)) ".*")
429 ((= (point) save-pos
) ".*")
430 (t (regexp-quote (buffer-substring save-pos
(point)))))))
431 (skip-chars-forward " \t")
432 ;;; Got the major/minor chunks, now for the viewers/etc
433 ;;; The first item _must_ be a viewer, according to the
434 ;;; RFC for mailcap files (#1524)
436 (when (eq (char-after) ?\
;)
438 (skip-chars-forward " \t")
439 (setq save-pos
(point))
440 (skip-chars-forward "^;\n")
442 (while (eq (char-before) ?
\\)
443 (backward-delete-char 1)
445 (skip-chars-forward "^;\n"))
446 (if (eq (or (char-after save-pos
) 0) ?
')
448 (narrow-to-region (1+ save-pos
) (point))
449 (goto-char (point-min))
451 (read (current-buffer))
452 (goto-char (point-max))
454 (setq viewer
(buffer-substring save-pos
(point)))))
455 (setq save-pos
(point))
457 (unless (equal viewer
"")
458 (setq info
(nconc (list (cons 'viewer viewer
)
459 (cons 'type
(concat major
"/"
460 (if (string= minor
".*")
462 (mailcap-parse-mailcap-extras save-pos
(point))))
463 (mailcap-mailcap-entry-passes-test info
)
464 (mailcap-add-mailcap-entry major minor info
))
465 (beginning-of-line)))))
467 (defun mailcap-parse-mailcap-extras (st nd
)
468 "Grab all the extra stuff from a mailcap entry."
472 results
; Assoc list of results
473 name-pos
; Start of XXXX= position
474 val-pos
; Start of value position
475 done
; Found end of \'d ;s?
478 (narrow-to-region st nd
)
479 (goto-char (point-min))
480 (skip-chars-forward " \n\t;")
483 (setq name-pos
(point))
484 (skip-chars-forward "^ \n\t=;")
485 (downcase-region name-pos
(point))
486 (setq name
(buffer-substring name-pos
(point)))
487 (skip-chars-forward " \t\n")
488 (if (not (eq (char-after (point)) ?
=)) ; There is no value
490 (skip-chars-forward " \t\n=")
491 (setq val-pos
(point))
492 (if (memq (char-after val-pos
) '(?
\" ?
'))
494 (setq val-pos
(1+ val-pos
))
499 (error (goto-char (point-max)))))
501 (skip-chars-forward "^;")
502 (if (eq (char-after (1- (point))) ?
\\ )
504 (subst-char-in-region (1- (point)) (point) ?
\\ ?
)
505 (skip-chars-forward ";"))
507 (setq value
(buffer-substring val-pos
(point))))
508 ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
510 (setq results
(cons (cons (if (string-equal name
"test")
514 (skip-chars-forward " \";\n\t"))
517 (defun mailcap-mailcap-entry-passes-test (info)
518 "Replace the test clause of INFO itself with a boolean for some cases.
519 This function supports only `test -n $DISPLAY' and `test -z $DISPLAY',
520 replaces them with t or nil. As for others or if INFO has a interactive
521 spec (needsterm, needsterminal, or needsx11) but DISPLAY is not set,
522 the test clause will be unchanged."
523 (let ((test (assq 'test info
)) ; The test clause
525 (setq status
(and test
(split-string (cdr test
) " ")))
526 (if (and (or (assoc "needsterm" info
)
527 (assoc "needsterminal" info
)
528 (assoc "needsx11" info
))
529 (not (getenv "DISPLAY")))
532 ((and (equal (nth 0 status
) "test")
533 (equal (nth 1 status
) "-n")
534 (or (equal (nth 2 status
) "$DISPLAY")
535 (equal (nth 2 status
) "\"$DISPLAY\"")))
536 (setq status
(if (getenv "DISPLAY") t nil
)))
537 ((and (equal (nth 0 status
) "test")
538 (equal (nth 1 status
) "-z")
539 (or (equal (nth 2 status
) "$DISPLAY")
540 (equal (nth 2 status
) "\"$DISPLAY\"")))
541 (setq status
(if (getenv "DISPLAY") nil t
)))
544 (and test
(listp test
) (setcdr test status
))))
547 ;;; The action routines.
550 (defun mailcap-possible-viewers (major minor
)
551 "Return a list of possible viewers from MAJOR for minor type MINOR."
556 ((equal (car (car major
)) minor
)
557 (setq exact
(cons (cdr (car major
)) exact
)))
558 ((and minor
(string-match (concat "^" (car (car major
)) "$") minor
))
559 (setq wildcard
(cons (cdr (car major
)) wildcard
))))
560 (setq major
(cdr major
)))
561 (nconc exact wildcard
)))
563 (defun mailcap-unescape-mime-test (test type-info
)
564 (let (save-pos save-chr subst
)
566 ((symbolp test
) test
)
567 ((and (listp test
) (symbolp (car test
))) test
)
569 (and (listp test
) (stringp (car test
))
570 (setq test
(mapconcat 'identity test
" "))))
573 (goto-char (point-min))
575 (skip-chars-forward "^%")
577 (progn (skip-chars-backward "\\\\")
579 0) ; It is an escaped %
582 (skip-chars-forward "%."))
583 (setq save-pos
(point))
584 (skip-chars-forward "%")
585 (setq save-chr
(char-after (point)))
587 ;; %s: name of a file for the body data
589 ;; %{<parameter name}: value of parameter in mailcap entry
590 ;; %n: number of sub-parts for multipart content-type
591 ;; %F: a set of content-type/filename pairs for multiparts
593 ((null save-chr
) nil
)
595 (delete-region save-pos
(progn (forward-char 1) (point)))
596 (insert (or (cdr (assq 'type type-info
)) "\"\"")))
597 ((memq save-chr
'(?M ?n ?F
))
598 (delete-region save-pos
(progn (forward-char 1) (point)))
602 (skip-chars-forward "^}")
603 (downcase-region (+ 2 save-pos
) (point))
604 (setq subst
(buffer-substring (+ 2 save-pos
) (point)))
605 (delete-region save-pos
(1+ (point)))
606 (insert (or (cdr (assoc subst type-info
)) "\"\"")))
609 (t (error "Bad value to mailcap-unescape-mime-test: %s" test
)))))
611 (defvar mailcap-viewer-test-cache nil
)
613 (defun mailcap-viewer-passes-test (viewer-info type-info
)
614 "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
615 Also return non-nil if it has no test clause. TYPE-INFO is an argument
616 to supply to the test."
617 (let* ((test-info (assq 'test viewer-info
))
618 (test (cdr test-info
))
620 (viewer (cdr (assq 'viewer viewer-info
)))
621 (default-directory (expand-file-name "~/"))
622 status parsed-test cache result
)
623 (cond ((not (or (stringp viewer
) (fboundp viewer
)))
624 nil
) ; Non-existent Lisp function
625 ((setq cache
(assoc test mailcap-viewer-test-cache
))
627 ((not test-info
) t
) ; No test clause
632 ((not test
) nil
) ; Already failed test
633 ((eq test t
) t
) ; Already passed test
634 ((functionp test
) ; Lisp function as test
635 (funcall test type-info
))
636 ((and (symbolp test
) ; Lisp variable as test
639 ((and (listp test
) ; List to be eval'd
640 (symbolp (car test
)))
643 (setq test
(mailcap-unescape-mime-test test type-info
)
644 test
(list shell-file-name nil nil nil
645 shell-command-switch test
)
646 status
(apply 'call-process test
))
648 (push (list otest result
) mailcap-viewer-test-cache
)
651 (defun mailcap-add-mailcap-entry (major minor info
)
652 (let ((old-major (assoc major mailcap-mime-data
)))
653 (if (null old-major
) ; New major area
654 (setq mailcap-mime-data
655 (cons (cons major
(list (cons minor info
)))
657 (let ((cur-minor (assoc minor old-major
)))
659 ((or (null cur-minor
) ; New minor area, or
660 (assq 'test info
)) ; Has a test, insert at beginning
661 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))
662 ((and (not (assq 'test info
)) ; No test info, replace completely
663 (not (assq 'test cur-minor
))
664 (equal (assq 'viewer info
) ; Keep alternative viewer
665 (assq 'viewer cur-minor
)))
666 (setcdr cur-minor info
))
668 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))))
671 (defun mailcap-add (type viewer
&optional test
)
672 "Add VIEWER as a handler for TYPE.
673 If TEST is not given, it defaults to t."
674 (let ((tl (split-string type
"/")))
675 (when (or (not (car tl
))
677 (error "%s is not a valid MIME type" type
))
678 (mailcap-add-mailcap-entry
681 (test .
,(if test test t
))
688 (defun mailcap-viewer-lessp (x y
)
689 "Return t if viewer X is more desirable than viewer Y."
690 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x
)) "")))
691 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y
)) "")))
692 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x
)) ""))))
693 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y
)) "")))))
695 ((and x-wild
(not y-wild
))
697 ((and (not x-wild
) y-wild
)
699 ((and (not y-lisp
) x-lisp
)
703 (defun mailcap-mime-info (string &optional request no-decode
)
704 "Get the MIME viewer command for STRING, return nil if none found.
705 Expects a complete content-type header line as its argument.
707 Second argument REQUEST specifies what information to return. If it is
708 nil or the empty string, the viewer (second field of the mailcap
709 entry) will be returned. If it is a string, then the mailcap field
710 corresponding to that string will be returned (print, description,
711 whatever). If a number, then all the information for this specific
712 viewer is returned. If `all', then all possible viewers for
713 this type is returned.
715 If NO-DECODE is non-nil, don't decode STRING."
716 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
719 major
; Major encoding (text, etc)
720 minor
; Minor encoding (html, etc)
722 save-pos
; Misc. position during parse
723 major-info
; (assoc major mailcap-mime-data)
724 minor-info
; (assoc minor major-info)
725 test
; current test proc.
726 viewers
; Possible viewers
727 passed
; Viewers that passed the test
728 viewer
; The one and only viewer
733 (list (or string
"text/plain"))
734 (mail-header-parse-content-type (or string
"text/plain"))))
735 (setq major
(split-string (car ctl
) "/"))
736 (setq minor
(cadr major
)
738 (when (setq major-info
(cdr (assoc major mailcap-mime-data
)))
739 (when (setq viewers
(mailcap-possible-viewers major-info minor
))
740 (setq info
(mapcar (lambda (a) (cons (symbol-name (car a
))
744 (if (mailcap-viewer-passes-test (car viewers
) info
)
745 (setq passed
(cons (car viewers
) passed
)))
746 (setq viewers
(cdr viewers
)))
747 (setq passed
(sort passed
'mailcap-viewer-lessp
))
748 (setq viewer
(car passed
))))
749 (when (and (stringp (cdr (assq 'viewer viewer
)))
751 (setq viewer
(car passed
)))
753 ((and (null viewer
) (not (equal major
"default")) request
)
754 (mailcap-mime-info "default" request no-decode
))
755 ((or (null request
) (equal request
""))
756 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer
)) info
))
758 (mailcap-unescape-mime-test
759 (cdr-safe (assoc request viewer
)) info
))
763 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
764 (setq viewer
(copy-sequence viewer
))
765 (let ((view (assq 'viewer viewer
))
766 (test (assq 'test viewer
)))
767 (if view
(setcdr view
(mailcap-unescape-mime-test (cdr view
) info
)))
768 (if test
(setcdr test
(mailcap-unescape-mime-test (cdr test
) info
))))
772 ;;; Experimental MIME-types parsing
775 (defvar mailcap-mime-extensions
776 '(("" .
"text/plain")
777 (".1" .
"text/plain") ;; Manual pages
778 (".3" .
"text/plain")
779 (".8" .
"text/plain")
780 (".abs" .
"audio/x-mpeg")
781 (".aif" .
"audio/aiff")
782 (".aifc" .
"audio/aiff")
783 (".aiff" .
"audio/aiff")
784 (".ano" .
"application/x-annotator")
785 (".au" .
"audio/ulaw")
786 (".avi" .
"video/x-msvideo")
787 (".bcpio" .
"application/x-bcpio")
788 (".bin" .
"application/octet-stream")
789 (".cdf" .
"application/x-netcdr")
790 (".cpio" .
"application/x-cpio")
791 (".csh" .
"application/x-csh")
792 (".css" .
"text/css")
793 (".dvi" .
"application/x-dvi")
794 (".diff" .
"text/x-patch")
795 (".dpatch".
"test/x-patch")
796 (".el" .
"application/emacs-lisp")
797 (".eps" .
"application/postscript")
798 (".etx" .
"text/x-setext")
799 (".exe" .
"application/octet-stream")
800 (".fax" .
"image/x-fax")
801 (".gif" .
"image/gif")
802 (".hdf" .
"application/x-hdf")
803 (".hqx" .
"application/mac-binhex40")
804 (".htm" .
"text/html")
805 (".html" .
"text/html")
806 (".icon" .
"image/x-icon")
807 (".ief" .
"image/ief")
808 (".jpg" .
"image/jpeg")
809 (".macp" .
"image/x-macpaint")
810 (".man" .
"application/x-troff-man")
811 (".me" .
"application/x-troff-me")
812 (".mif" .
"application/mif")
813 (".mov" .
"video/quicktime")
814 (".movie" .
"video/x-sgi-movie")
815 (".mp2" .
"audio/x-mpeg")
816 (".mp3" .
"audio/x-mpeg")
817 (".mp2a" .
"audio/x-mpeg2")
818 (".mpa" .
"audio/x-mpeg")
819 (".mpa2" .
"audio/x-mpeg2")
820 (".mpe" .
"video/mpeg")
821 (".mpeg" .
"video/mpeg")
822 (".mpega" .
"audio/x-mpeg")
823 (".mpegv" .
"video/mpeg")
824 (".mpg" .
"video/mpeg")
825 (".mpv" .
"video/mpeg")
826 (".ms" .
"application/x-troff-ms")
827 (".nc" .
"application/x-netcdf")
828 (".nc" .
"application/x-netcdf")
829 (".oda" .
"application/oda")
830 (".patch" .
"text/x-patch")
831 (".pbm" .
"image/x-portable-bitmap")
832 (".pdf" .
"application/pdf")
833 (".pgm" .
"image/portable-graymap")
834 (".pict" .
"image/pict")
835 (".png" .
"image/png")
836 (".pnm" .
"image/x-portable-anymap")
837 (".pod" .
"text/plain")
838 (".ppm" .
"image/portable-pixmap")
839 (".ps" .
"application/postscript")
840 (".qt" .
"video/quicktime")
841 (".ras" .
"image/x-raster")
842 (".rgb" .
"image/x-rgb")
843 (".rtf" .
"application/rtf")
844 (".rtx" .
"text/richtext")
845 (".sh" .
"application/x-sh")
846 (".sit" .
"application/x-stuffit")
847 (".siv" .
"application/sieve")
848 (".snd" .
"audio/basic")
849 (".soa" .
"text/dns")
850 (".src" .
"application/x-wais-source")
851 (".tar" .
"archive/tar")
852 (".tcl" .
"application/x-tcl")
853 (".tex" .
"application/x-tex")
854 (".texi" .
"application/texinfo")
855 (".tga" .
"image/x-targa")
856 (".tif" .
"image/tiff")
857 (".tiff" .
"image/tiff")
858 (".tr" .
"application/x-troff")
859 (".troff" .
"application/x-troff")
860 (".tsv" .
"text/tab-separated-values")
861 (".txt" .
"text/plain")
862 (".vbs" .
"video/mpeg")
863 (".vox" .
"audio/basic")
864 (".vrml" .
"x-world/x-vrml")
865 (".wav" .
"audio/x-wav")
866 (".xls" .
"application/vnd.ms-excel")
867 (".wrl" .
"x-world/x-vrml")
868 (".xbm" .
"image/xbm")
869 (".xpm" .
"image/xpm")
870 (".xwd" .
"image/windowdump")
871 (".zip" .
"application/zip")
872 (".ai" .
"application/postscript")
873 (".jpe" .
"image/jpeg")
874 (".jpeg" .
"image/jpeg")
875 (".org" .
"text/x-org"))
876 "An alist of file extensions and corresponding MIME content-types.
877 This exists for you to customize the information in Lisp. It is
878 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
880 (defvar mailcap-mimetypes-parsed-p nil
)
882 (defun mailcap-parse-mimetypes (&optional path force
)
883 "Parse out all the mimetypes specified in a Unix-style path string PATH.
884 Components of PATH are separated by the `path-separator' character
885 appropriate for this system. If PATH is omitted, use the value of
886 environment variable MIMETYPES if set; otherwise use a default path.
887 If FORCE, re-parse even if already parsed."
888 (interactive (list nil t
))
889 (when (or (not mailcap-mimetypes-parsed-p
)
893 ((getenv "MIMETYPES") (setq path
(getenv "MIMETYPES")))
894 ((memq system-type mailcap-poor-system-types
)
895 (setq path
'("~/mime.typ" "~/etc/mime.typ")))
897 ;; mime.types seems to be the normal name, definitely so
898 ;; on current GNUish systems. The search order follows
902 "/usr/etc/mime.types"
903 "/usr/local/etc/mime.types"
904 "/usr/local/www/conf/mime.types"
907 "/usr/etc/mime-types"
908 "/usr/local/etc/mime-types"
909 "/usr/local/www/conf/mime-types"))))
910 (let ((fnames (reverse (if (stringp path
)
911 (split-string path path-separator t
)
915 (setq fname
(car fnames
))
916 (if (and (file-readable-p fname
))
917 (mailcap-parse-mimetype-file fname
))
918 (setq fnames
(cdr fnames
))))
919 (setq mailcap-mimetypes-parsed-p t
)))
921 (defun mailcap-parse-mimetype-file (fname)
922 "Parse out a mime-types file FNAME."
923 (let (type ; The MIME type for this line
924 extns
; The extensions for this line
925 save-pos
; Misc. saved buffer positions
928 (insert-file-contents fname
)
929 (mailcap-replace-regexp "#.*" "")
930 (mailcap-replace-regexp "\n+" "\n")
931 (mailcap-replace-regexp "[ \t]+$" "")
932 (goto-char (point-max))
933 (skip-chars-backward " \t\n")
934 (delete-region (point) (point-max))
935 (goto-char (point-min))
937 (skip-chars-forward " \t\n")
938 (setq save-pos
(point))
939 (skip-chars-forward "^ \t\n")
940 (downcase-region save-pos
(point))
941 (setq type
(buffer-substring save-pos
(point)))
943 (skip-chars-forward " \t")
944 (setq save-pos
(point))
945 (skip-chars-forward "^ \t\n")
946 (setq extns
(cons (buffer-substring save-pos
(point)) extns
)))
948 (setq mailcap-mime-extensions
950 (cons (if (= (string-to-char (car extns
)) ?.
)
952 (concat "." (car extns
))) type
)
953 mailcap-mime-extensions
)
954 extns
(cdr extns
)))))))
956 (defun mailcap-extension-to-mime (extn)
957 "Return the MIME content type of the file extensions EXTN."
958 (mailcap-parse-mimetypes)
959 (if (and (stringp extn
)
960 (not (eq (string-to-char extn
) ?.
)))
961 (setq extn
(concat "." extn
)))
962 (cdr (assoc (downcase extn
) mailcap-mime-extensions
)))
965 (defalias 'mailcap-command-p
'executable-find
)
967 (defun mailcap-mime-types ()
968 "Return a list of MIME media types."
969 (mailcap-parse-mimetypes)
972 (mapcar 'cdr mailcap-mime-extensions
)
980 (let ((type (cdr (assq 'type
(cdr m
)))))
981 (if (equal (cadr (split-string type
"/"))
986 mailcap-mime-data
)))))
989 ;;; Useful supplementary functions
992 (defun mailcap-file-default-commands (files)
993 "Return a list of default commands for FILES."
994 (mailcap-parse-mailcaps)
995 (mailcap-parse-mimetypes)
996 (let* ((all-mime-type
997 ;; All unique MIME types from file extensions
999 (mapcar (lambda (file)
1000 (mailcap-extension-to-mime
1001 (file-name-extension file t
)))
1004 ;; All MIME info lists
1006 (mapcar (lambda (mime-type)
1007 (mailcap-mime-info mime-type
'all
))
1010 ;; Intersection of mime-infos from different mime-types;
1011 ;; or just the first MIME info for a single MIME type
1012 (if (cdr all-mime-info
)
1013 (delq nil
(mapcar (lambda (mi1)
1014 (unless (memq nil
(mapcar
1017 (cdr all-mime-info
)))
1019 (car all-mime-info
)))
1020 (car all-mime-info
)))
1022 ;; Command strings from `viewer' field of the MIME info
1026 (let ((command (cdr (assoc 'viewer mime-info
))))
1027 (if (stringp command
)
1028 (replace-regexp-in-string
1029 ;; Replace mailcap's `%s' placeholder
1030 ;; with dired's `?' placeholder
1032 (replace-regexp-in-string
1033 ;; Remove the final filename placeholder
1034 "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" ""
1037 common-mime-info
)))))
1040 (defun mailcap-view-mime (type)
1041 "View the data in the current buffer that has MIME type TYPE.
1042 `mailcap-mime-data' determines the method to use."
1043 (let ((method (mailcap-mime-info type
)))
1044 (if (stringp method
)
1045 (shell-command-on-region (point-min) (point-max)
1046 ;; Use stdin as the "%s".
1054 ;;; mailcap.el ends here