1 ;;; mailcap.el --- MIME media types configuration -*- lexical-binding: t -*-
3 ;; Copyright (C) 1998-2017 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 <https://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 (autoload 'mail-header-parse-content-type
"mail-parse")
35 "Definition of viewers for MIME types."
39 (defvar mailcap-parse-args-syntax-table
40 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
41 (modify-syntax-entry ?
' "\"" table
)
42 (modify-syntax-entry ?
` "\"" table
)
43 (modify-syntax-entry ?
{ "(" table
)
44 (modify-syntax-entry ?
} ")" table
)
46 "A syntax table for parsing SGML attributes.")
48 (defvar mailcap-print-command
50 (cons (if (boundp 'lpr-command
)
53 (when (boundp 'lpr-switches
)
54 (if (stringp lpr-switches
)
58 "Shell command (including switches) used to print PostScript files.")
60 (defun mailcap--get-user-mime-data (sym)
61 (let ((val (default-value sym
))
64 (push (list (cdr (assq 'viewer entry
))
65 (cdr (assq 'type entry
))
66 (cdr (assq 'test entry
)))
70 (defun mailcap--set-user-mime-data (sym val
)
72 (pcase-dolist (`(,viewer
,type
,test
) val
)
73 (push `((viewer .
,viewer
)
75 ,@(when test
`((test .
,test
))))
77 (set-default sym
(nreverse res
))))
79 (defcustom mailcap-user-mime-data nil
80 "A list of viewers preferred for different MIME types.
81 The elements of the list are alists of the following structure
87 where VIEWER is either a lisp command, e.g., a major-mode, or a
88 string containing a shell command for viewing files of the
89 defined MIME-TYPE. In case of a shell command, %s will be
90 replaced with the file.
92 MIME-TYPE is a regular expression being matched against the
93 actual MIME type. It is implicitly surrounded with ^ and $.
95 TEST is an lisp form which is evaluated in order to test if the
96 entry should be chosen. The `test' entry is optional.
98 When selecting a viewer for a given MIME type, the first viewer
99 in this list with a matching MIME-TYPE and successful TEST is
100 selected. Only if none matches, the standard `mailcap-mime-data'
104 (choice (function :tag
"Function or mode")
105 (string :tag
"Shell command"))
106 (regexp :tag
"MIME Type")
107 (sexp :tag
"Test (optional)")))
108 :get
#'mailcap--get-user-mime-data
109 :set
#'mailcap--set-user-mime-data
112 ;; Postpone using defcustom for this as it's so big and we essentially
113 ;; have to have two copies of the data around then. Perhaps just
114 ;; customize the Lisp viewers and rely on the normal configuration
115 ;; files for the rest? -- fx
116 (defvar mailcap-mime-data
119 (viewer .
"gnumeric %s")
120 (test .
(getenv "DISPLAY"))
121 (type .
"application/vnd.ms-excel"))
123 (viewer . mailcap-save-binary-file
)
125 (type .
"application/octet-stream"))
127 (viewer .
"xdvi -safer %s")
128 (test .
(eq window-system
'x
))
130 (type .
"application/dvi")
131 ("print" .
"dvips -qRP %s"))
133 (viewer .
"dvitty %s")
134 (test .
(not (getenv "DISPLAY")))
135 (type .
"application/dvi")
136 ("print" .
"dvips -qRP %s"))
138 (viewer . mailcap-maybe-eval
)
139 (type .
"application/emacs-lisp"))
141 (viewer . mailcap-maybe-eval
)
142 (type .
"application/x-emacs-lisp"))
144 (viewer . mailcap-save-binary-file
)
146 (type .
"application/x-tar"))
149 (type .
"application/x-latex"))
152 (type .
"application/x-tex"))
155 (type .
"application/latex"))
158 (type .
"application/tex"))
160 (viewer . texinfo-mode
)
161 (type .
"application/tex"))
163 (viewer . mailcap-save-binary-file
)
165 (type .
"application/zip")
168 (viewer . doc-view-mode
)
169 (type .
"application/pdf")
170 (test .
(eq window-system
'x
)))
172 (viewer .
"gv -safer %s")
173 (type .
"application/pdf")
174 (test . window-system
)
175 ("print" .
,(concat "pdf2ps %s - | " mailcap-print-command
)))
178 (type .
"application/pdf")
179 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
180 (test .
(eq window-system
'x
)))
183 (type .
"application/pdf")
184 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
185 (test .
(eq window-system
'x
)))
187 (viewer .
,(concat "pdftotext %s -"))
188 (type .
"application/pdf")
189 ("print" .
,(concat "pdftops %s - | " mailcap-print-command
))
192 (viewer .
"gv -safer %s")
193 (type .
"application/postscript")
194 (test . window-system
)
195 ("print" .
,(concat mailcap-print-command
" %s"))
198 (viewer .
"ghostview -dSAFER %s")
199 (type .
"application/postscript")
200 (test .
(eq window-system
'x
))
201 ("print" .
,(concat mailcap-print-command
" %s"))
204 (viewer .
"ps2ascii %s")
205 (type .
"application/postscript")
206 (test .
(not (getenv "DISPLAY")))
207 ("print" .
,(concat mailcap-print-command
" %s"))
210 (viewer . sieve-mode
)
211 (type .
"application/sieve"))
213 (viewer .
"gpg --import --interactive --verbose")
214 (type .
"application/pgp-keys")
218 (viewer .
"maplay %s")
219 (type .
"audio/x-mpeg"))
221 (viewer .
"showaudio")
225 (viewer . mm-view-message
)
226 (test .
(and (featurep 'gnus
)
228 (type .
"message/rfc822"))
231 (type .
"message/rfc822"))
234 (type .
"message/rfc822")))
237 (viewer .
"xwud -in %s")
238 (type .
"image/x-xwd")
239 ("compose" .
"xwd -frame > %s")
240 (test .
(eq window-system
'x
))
243 (viewer .
"xwud -in %s")
244 (type .
"image/x-xwd")
245 ("compose" .
"xwd -frame > %s")
246 (test .
(eq window-system
'x
))
249 (viewer .
"xwud -in %s")
250 (type .
"image/x-xwd")
251 ("compose" .
"xwd -frame > %s")
252 (test .
(eq window-system
'x
))
255 (viewer .
"display %s")
257 (test .
(eq window-system
'x
))
262 (test .
(eq window-system
'x
))
267 (type .
"text/plain"))
269 (viewer . fundamental-mode
)
270 (type .
"text/plain"))
272 (viewer . enriched-decode
)
273 (type .
"text/enriched"))
276 (type .
"text/dns")))
279 (viewer .
"mpeg_play %s")
280 (type .
"video/mpeg")
281 (test .
(eq window-system
'x
))
285 (viewer .
"webspace -remote %s -URL %u")
286 (type .
"x-world/x-vrml")
292 (type .
"archive/tar"))))
293 "The mailcap structure is an assoc list of assoc lists.
294 1st assoc list is keyed on the major content-type
295 2nd assoc list is keyed on the minor content-type (which can be a regexp)
300 (\"postscript\" . <info>))
302 (\"plain\" . <info>)))
304 Where <info> is another assoc list of the various information
305 related to the mailcap RFC 1524. This is keyed on the lowercase
306 attribute name (viewer, test, etc). This looks like:
307 ((viewer . VIEWERINFO)
312 Where VIEWERINFO specifies how the content-type is viewed. Can be
313 a string, in which case it is run through a shell, with appropriate
314 parameters, or a symbol, in which case the symbol is `funcall'ed if
315 and only if it exists as a function, with the buffer as an argument.
317 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
318 means the viewer is always valid. If it is a Lisp function, it is
319 called with a list of items from any extra fields from the
320 Content-Type header as argument to return a boolean value for the
321 validity. Otherwise, if it is a non-function Lisp symbol or list
322 whose car is a symbol, it is `eval'led to yield the validity. If it
323 is a string or list of strings, it represents a shell command to run
324 to return a true or false shell value for the validity.")
325 (put 'mailcap-mime-data
'risky-local-variable t
)
327 (defcustom mailcap-download-directory nil
328 "Directory to which `mailcap-save-binary-file' downloads files by default.
329 nil means your home directory."
330 :type
'(choice (const :tag
"Home directory" nil
)
334 (defvar mailcap-poor-system-types
336 "Systems that don't have a Unix-like directory hierarchy.")
339 ;;; Utility functions
342 (defun mailcap-save-binary-file ()
343 (goto-char (point-min))
345 (let ((file (read-file-name
346 "Filename to save as: "
347 (or mailcap-download-directory
"~/")))
348 (require-final-newline nil
))
349 (write-region (point-min) (point-max) file
))
350 (kill-buffer (current-buffer))))
352 (defvar mailcap-maybe-eval-warning
355 This MIME part contains untrusted and possibly harmful content.
356 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
357 things can happen. Please examine the code very carefully before you
358 instruct Emacs to evaluate it. You can browse the buffer containing
359 the code using \\[scroll-other-window].
361 If you are unsure what to do, please answer \"no\"."
362 "Text of warning message displayed by `mailcap-maybe-eval'.
363 Make sure that this text consists only of few text lines. Otherwise,
364 Gnus might fail to display all of it.")
366 (defun mailcap-maybe-eval ()
367 "Maybe evaluate a buffer of Emacs Lisp code."
368 (let ((lisp-buffer (current-buffer)))
369 (goto-char (point-min))
371 (save-window-excursion
372 (delete-other-windows)
373 (let ((buffer (get-buffer-create (generate-new-buffer-name
376 (with-current-buffer buffer
377 (insert (substitute-command-keys
378 mailcap-maybe-eval-warning
))
379 (goto-char (point-min))
380 (display-buffer buffer
)
381 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
382 (kill-buffer buffer
))))
383 (eval-buffer (current-buffer)))
384 (when (buffer-live-p lisp-buffer
)
385 (with-current-buffer lisp-buffer
386 (emacs-lisp-mode)))))
390 ;;; The mailcap parser
393 (defun mailcap-replace-regexp (regexp to-string
)
394 ;; Quiet replace-regexp.
395 (goto-char (point-min))
396 (while (re-search-forward regexp nil t
)
397 (replace-match to-string t nil
)))
399 (defvar mailcap-parsed-p nil
)
401 (defun mailcap-parse-mailcaps (&optional path force
)
402 "Parse out all the mailcaps specified in a path string PATH.
403 Components of PATH are separated by the `path-separator' character
404 appropriate for this system. If FORCE, re-parse even if already
405 parsed. If PATH is omitted, use the value of environment variable
406 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
407 /usr/local/etc/mailcap."
408 (interactive (list nil t
))
409 (when (or (not mailcap-parsed-p
)
413 ((getenv "MAILCAPS") (setq path
(getenv "MAILCAPS")))
414 ((memq system-type mailcap-poor-system-types
)
415 (setq path
'("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
417 ;; This is per RFC 1524, specifically
418 ;; with /usr before /usr/local.
419 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
420 "/usr/local/etc/mailcap"))))
421 (dolist (fname (reverse
423 (split-string path path-separator t
)
425 (when (and (file-readable-p fname
) (file-regular-p fname
))
426 (mailcap-parse-mailcap fname
)))
427 (setq mailcap-parsed-p t
)))
429 (defun mailcap-parse-mailcap (fname)
430 "Parse out the mailcap file specified by FNAME."
431 (let (major ; The major mime type (image/audio/etc)
432 minor
; The minor mime type (gif, basic, etc)
433 save-pos
; Misc saved positions used in parsing
434 viewer
; How to view this mime type
435 info
; Misc info about this mime type
438 (insert-file-contents fname
)
439 (set-syntax-table mailcap-parse-args-syntax-table
)
440 (mailcap-replace-regexp "#.*" "") ; Remove all comments
441 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
442 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
443 (goto-char (point-max))
444 (skip-chars-backward " \t\n")
445 (delete-region (point) (point-max))
447 (skip-chars-backward " \t\n")
449 (setq save-pos
(point)
451 (skip-chars-forward "^/; \t\n")
452 (downcase-region save-pos
(point))
453 (setq major
(buffer-substring save-pos
(point)))
454 (skip-chars-forward " \t")
456 (when (eq (char-after) ?
/)
458 (skip-chars-forward " \t")
459 (setq save-pos
(point))
460 (skip-chars-forward "^; \t\n")
461 (downcase-region save-pos
(point))
464 ((eq ?
* (or (char-after save-pos
) 0)) ".*")
465 ((= (point) save-pos
) ".*")
466 (t (regexp-quote (buffer-substring save-pos
(point)))))))
467 (skip-chars-forward " \t")
468 ;;; Got the major/minor chunks, now for the viewers/etc
469 ;;; The first item _must_ be a viewer, according to the
470 ;;; RFC for mailcap files (#1524)
472 (when (eq (char-after) ?\
;)
474 (skip-chars-forward " \t")
475 (setq save-pos
(point))
476 (skip-chars-forward "^;\n")
478 (while (eq (char-before) ?
\\)
479 (backward-delete-char 1)
481 (skip-chars-forward "^;\n"))
482 (if (eq (or (char-after save-pos
) 0) ?
')
484 (narrow-to-region (1+ save-pos
) (point))
485 (goto-char (point-min))
487 (read (current-buffer))
488 (goto-char (point-max))
490 (setq viewer
(buffer-substring save-pos
(point)))))
491 (setq save-pos
(point))
493 (unless (equal viewer
"")
494 (setq info
(nconc (list (cons 'viewer viewer
)
495 (cons 'type
(concat major
"/"
496 (if (string= minor
".*")
498 (mailcap-parse-mailcap-extras save-pos
(point))))
499 (mailcap-mailcap-entry-passes-test info
)
500 (mailcap-add-mailcap-entry major minor info
))
501 (beginning-of-line)))))
503 (defun mailcap-parse-mailcap-extras (st nd
)
504 "Grab all the extra stuff from a mailcap entry."
508 results
; Assoc list of results
509 name-pos
; Start of XXXX= position
510 val-pos
; Start of value position
511 done
; Found end of \'d ;s?
514 (narrow-to-region st nd
)
515 (goto-char (point-min))
516 (skip-chars-forward " \n\t;")
519 (setq name-pos
(point))
520 (skip-chars-forward "^ \n\t=;")
521 (downcase-region name-pos
(point))
522 (setq name
(buffer-substring name-pos
(point)))
523 (skip-chars-forward " \t\n")
524 (if (not (eq (char-after (point)) ?
=)) ; There is no value
526 (skip-chars-forward " \t\n=")
527 (setq val-pos
(point))
528 (if (memq (char-after val-pos
) '(?
\" ?
'))
530 (setq val-pos
(1+ val-pos
))
535 (error (goto-char (point-max)))))
537 (skip-chars-forward "^;")
538 (if (eq (char-after (1- (point))) ?
\\ )
540 (subst-char-in-region (1- (point)) (point) ?
\\ ?
)
541 (skip-chars-forward ";"))
543 (setq value
(buffer-substring val-pos
(point))))
544 ;; `test' as symbol, others like "copiousoutput" and "needsx11" as
546 (push (cons (if (string-equal name
"test") 'test name
) value
) results
)
547 (skip-chars-forward " \";\n\t"))
550 (defun mailcap-mailcap-entry-passes-test (info)
551 "Replace the test clause of INFO itself with a boolean for some cases.
552 This function supports only `test -n $DISPLAY' and `test -z $DISPLAY',
553 replaces them with t or nil. As for others or if INFO has a interactive
554 spec (needsterm, needsterminal, or needsx11) but DISPLAY is not set,
555 the test clause will be unchanged."
556 (let ((test (assq 'test info
)) ; The test clause
558 (setq status
(and test
(split-string (cdr test
) " ")))
559 (if (and (or (assoc "needsterm" info
)
560 (assoc "needsterminal" info
)
561 (assoc "needsx11" info
))
562 (not (getenv "DISPLAY")))
565 ((and (equal (nth 0 status
) "test")
566 (equal (nth 1 status
) "-n")
567 (or (equal (nth 2 status
) "$DISPLAY")
568 (equal (nth 2 status
) "\"$DISPLAY\"")))
569 (setq status
(if (getenv "DISPLAY") t nil
)))
570 ((and (equal (nth 0 status
) "test")
571 (equal (nth 1 status
) "-z")
572 (or (equal (nth 2 status
) "$DISPLAY")
573 (equal (nth 2 status
) "\"$DISPLAY\"")))
574 (setq status
(if (getenv "DISPLAY") nil t
)))
577 (and test
(listp test
) (setcdr test status
))))
580 ;;; The action routines.
583 (defun mailcap-possible-viewers (major minor
)
584 "Return a list of possible viewers from MAJOR for minor type MINOR."
587 (pcase-dolist (`(,type .
,attrs
) major
)
591 ((and minor
(string-match (concat "^" type
"$") minor
))
592 (push attrs wildcard
))))
593 (nconc exact wildcard
)))
595 (defun mailcap-unescape-mime-test (test type-info
)
596 (let (save-pos save-chr subst
)
598 ((symbolp test
) test
)
599 ((and (listp test
) (symbolp (car test
))) test
)
601 (and (listp test
) (stringp (car test
))
602 (setq test
(mapconcat 'identity test
" "))))
605 (goto-char (point-min))
607 (skip-chars-forward "^%")
609 (progn (skip-chars-backward "\\\\")
611 0) ; It is an escaped %
614 (skip-chars-forward "%."))
615 (setq save-pos
(point))
616 (skip-chars-forward "%")
617 (setq save-chr
(char-after (point)))
619 ;; %s: name of a file for the body data
621 ;; %{<parameter name}: value of parameter in mailcap entry
622 ;; %n: number of sub-parts for multipart content-type
623 ;; %F: a set of content-type/filename pairs for multiparts
625 ((null save-chr
) nil
)
627 (delete-region save-pos
(progn (forward-char 1) (point)))
628 (insert (or (cdr (assq 'type type-info
)) "\"\"")))
629 ((memq save-chr
'(?M ?n ?F
))
630 (delete-region save-pos
(progn (forward-char 1) (point)))
634 (skip-chars-forward "^}")
635 (downcase-region (+ 2 save-pos
) (point))
636 (setq subst
(buffer-substring (+ 2 save-pos
) (point)))
637 (delete-region save-pos
(1+ (point)))
638 (insert (or (cdr (assoc subst type-info
)) "\"\"")))
641 (t (error "Bad value to mailcap-unescape-mime-test: %s" test
)))))
643 (defvar mailcap-viewer-test-cache nil
)
645 (defun mailcap-viewer-passes-test (viewer-info type-info
)
646 "Return non-nil if viewer specified by VIEWER-INFO passes its test clause.
647 Also return non-nil if it has no test clause. TYPE-INFO is an argument
648 to supply to the test."
649 (let* ((test-info (assq 'test viewer-info
))
650 (test (cdr test-info
))
652 (viewer (cdr (assq 'viewer viewer-info
)))
653 (default-directory (expand-file-name "~/"))
655 (cond ((not (or (stringp viewer
) (fboundp viewer
)))
656 nil
) ; Non-existent Lisp function
657 ((setq cache
(assoc test mailcap-viewer-test-cache
))
659 ((not test-info
) t
) ; No test clause
664 ((not test
) nil
) ; Already failed test
665 ((eq test t
) t
) ; Already passed test
666 ((functionp test
) ; Lisp function as test
667 (funcall test type-info
))
668 ((and (symbolp test
) ; Lisp variable as test
671 ((and (listp test
) ; List to be eval'd
672 (symbolp (car test
)))
675 (setq test
(mailcap-unescape-mime-test test type-info
)
676 test
(list shell-file-name nil nil nil
677 shell-command-switch test
)
678 status
(apply 'call-process test
))
680 (push (list otest result
) mailcap-viewer-test-cache
)
683 (defun mailcap-add-mailcap-entry (major minor info
)
684 (let ((old-major (assoc major mailcap-mime-data
)))
685 (if (null old-major
) ; New major area
686 (push (cons major
(list (cons minor info
))) mailcap-mime-data
)
687 (let ((cur-minor (assoc minor old-major
)))
689 ((or (null cur-minor
) ; New minor area, or
690 (assq 'test info
)) ; Has a test, insert at beginning
691 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))
692 ((and (not (assq 'test info
)) ; No test info, replace completely
693 (not (assq 'test cur-minor
))
694 (equal (assq 'viewer info
) ; Keep alternative viewer
695 (assq 'viewer cur-minor
)))
696 (setcdr cur-minor info
))
698 (setcdr old-major
(cons (cons minor info
) (cdr old-major
))))))
701 (defun mailcap-add (type viewer
&optional test
)
702 "Add VIEWER as a handler for TYPE.
703 If TEST is not given, it defaults to t."
704 (let ((tl (split-string type
"/")))
705 (when (or (not (car tl
))
707 (error "%s is not a valid MIME type" type
))
708 (mailcap-add-mailcap-entry
711 (test .
,(if test test t
))
718 (defun mailcap-viewer-lessp (x y
)
719 "Return t if viewer X is more desirable than viewer Y."
720 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x
)) "")))
721 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y
)) "")))
722 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x
)) ""))))
723 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y
)) "")))))
725 ((and x-wild
(not y-wild
))
727 ((and (not x-wild
) y-wild
)
729 ((and (not y-lisp
) x-lisp
)
733 (defun mailcap-select-preferred-viewer (type-info)
734 "Return an applicable viewer entry from `mailcap-user-mime-data'."
735 (let ((info (mapcar (lambda (a) (cons (symbol-name (car a
))
739 (dolist (entry mailcap-user-mime-data
)
740 (when (and (null viewer
)
741 (string-match (concat "^" (cdr (assq 'type entry
)) "$")
743 (mailcap-viewer-passes-test entry info
))
744 (setq viewer entry
)))
747 (defun mailcap-mime-info (string &optional request no-decode
)
748 "Get the MIME viewer command for STRING, return nil if none found.
749 Expects a complete content-type header line as its argument.
751 Second argument REQUEST specifies what information to return. If it is
752 nil or the empty string, the viewer (second field of the mailcap
753 entry) will be returned. If it is a string, then the mailcap field
754 corresponding to that string will be returned (print, description,
755 whatever). If a number, then all the information for this specific
756 viewer is returned. If `all', then all possible viewers for
757 this type is returned.
759 If NO-DECODE is non-nil, don't decode STRING."
760 ;; NO-DECODE avoids calling `mail-header-parse-content-type' from
763 major
; Major encoding (text, etc)
764 minor
; Minor encoding (html, etc)
766 major-info
; (assoc major mailcap-mime-data)
767 viewers
; Possible viewers
768 passed
; Viewers that passed the test
769 viewer
; The one and only viewer
774 (list (or string
"text/plain"))
775 (mail-header-parse-content-type (or string
"text/plain"))))
776 ;; Check if there's a user-defined viewer from `mailcap-user-mime-data'.
777 (setq viewer
(mailcap-select-preferred-viewer ctl
))
779 (setq passed
(list viewer
))
780 ;; None found, so heuristically select some applicable viewer
781 ;; from `mailcap-mime-data'.
782 (setq major
(split-string (car ctl
) "/"))
783 (setq minor
(cadr major
)
785 (when (setq major-info
(cdr (assoc major mailcap-mime-data
)))
786 (when (setq viewers
(mailcap-possible-viewers major-info minor
))
787 (setq info
(mapcar (lambda (a) (cons (symbol-name (car a
))
790 (dolist (entry viewers
)
791 (when (mailcap-viewer-passes-test entry info
)
792 (push entry passed
)))
793 (setq passed
(sort passed
'mailcap-viewer-lessp
))
794 (setq viewer
(car passed
))))
795 (when (and (stringp (cdr (assq 'viewer viewer
)))
797 (setq viewer
(car passed
))))
799 ((and (null viewer
) (not (equal major
"default")) request
)
800 (mailcap-mime-info "default" request no-decode
))
801 ((or (null request
) (equal request
""))
802 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer
)) info
))
804 (mailcap-unescape-mime-test
805 (cdr-safe (assoc request viewer
)) info
))
809 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data
810 (setq viewer
(copy-sequence viewer
))
811 (let ((view (assq 'viewer viewer
))
812 (test (assq 'test viewer
)))
813 (if view
(setcdr view
(mailcap-unescape-mime-test (cdr view
) info
)))
814 (if test
(setcdr test
(mailcap-unescape-mime-test (cdr test
) info
))))
818 ;;; Experimental MIME-types parsing
821 (defvar mailcap-mime-extensions
822 '(("" .
"text/plain")
823 (".1" .
"text/plain") ;; Manual pages
824 (".3" .
"text/plain")
825 (".8" .
"text/plain")
826 (".abs" .
"audio/x-mpeg")
827 (".aif" .
"audio/aiff")
828 (".aifc" .
"audio/aiff")
829 (".aiff" .
"audio/aiff")
830 (".ano" .
"application/x-annotator")
831 (".au" .
"audio/ulaw")
832 (".avi" .
"video/x-msvideo")
833 (".bcpio" .
"application/x-bcpio")
834 (".bin" .
"application/octet-stream")
835 (".cdf" .
"application/x-netcdr")
836 (".cpio" .
"application/x-cpio")
837 (".csh" .
"application/x-csh")
838 (".css" .
"text/css")
839 (".dvi" .
"application/x-dvi")
840 (".diff" .
"text/x-patch")
841 (".dpatch".
"text/x-patch")
842 (".el" .
"application/emacs-lisp")
843 (".eps" .
"application/postscript")
844 (".etx" .
"text/x-setext")
845 (".exe" .
"application/octet-stream")
846 (".fax" .
"image/x-fax")
847 (".gif" .
"image/gif")
848 (".hdf" .
"application/x-hdf")
849 (".hqx" .
"application/mac-binhex40")
850 (".htm" .
"text/html")
851 (".html" .
"text/html")
852 (".icon" .
"image/x-icon")
853 (".ief" .
"image/ief")
854 (".jpg" .
"image/jpeg")
855 (".macp" .
"image/x-macpaint")
856 (".man" .
"application/x-troff-man")
857 (".me" .
"application/x-troff-me")
858 (".mif" .
"application/mif")
859 (".mov" .
"video/quicktime")
860 (".movie" .
"video/x-sgi-movie")
861 (".mp2" .
"audio/x-mpeg")
862 (".mp3" .
"audio/x-mpeg")
863 (".mp2a" .
"audio/x-mpeg2")
864 (".mpa" .
"audio/x-mpeg")
865 (".mpa2" .
"audio/x-mpeg2")
866 (".mpe" .
"video/mpeg")
867 (".mpeg" .
"video/mpeg")
868 (".mpega" .
"audio/x-mpeg")
869 (".mpegv" .
"video/mpeg")
870 (".mpg" .
"video/mpeg")
871 (".mpv" .
"video/mpeg")
872 (".ms" .
"application/x-troff-ms")
873 (".nc" .
"application/x-netcdf")
874 (".nc" .
"application/x-netcdf")
875 (".oda" .
"application/oda")
876 (".patch" .
"text/x-patch")
877 (".pbm" .
"image/x-portable-bitmap")
878 (".pdf" .
"application/pdf")
879 (".pgm" .
"image/portable-graymap")
880 (".pict" .
"image/pict")
881 (".png" .
"image/png")
882 (".pnm" .
"image/x-portable-anymap")
883 (".pod" .
"text/plain")
884 (".ppm" .
"image/portable-pixmap")
885 (".ps" .
"application/postscript")
886 (".qt" .
"video/quicktime")
887 (".ras" .
"image/x-raster")
888 (".rgb" .
"image/x-rgb")
889 (".rtf" .
"application/rtf")
890 (".rtx" .
"text/richtext")
891 (".sh" .
"application/x-sh")
892 (".sit" .
"application/x-stuffit")
893 (".siv" .
"application/sieve")
894 (".snd" .
"audio/basic")
895 (".soa" .
"text/dns")
896 (".src" .
"application/x-wais-source")
897 (".tar" .
"archive/tar")
898 (".tcl" .
"application/x-tcl")
899 (".tex" .
"application/x-tex")
900 (".texi" .
"application/texinfo")
901 (".tga" .
"image/x-targa")
902 (".tif" .
"image/tiff")
903 (".tiff" .
"image/tiff")
904 (".tr" .
"application/x-troff")
905 (".troff" .
"application/x-troff")
906 (".tsv" .
"text/tab-separated-values")
907 (".txt" .
"text/plain")
908 (".vbs" .
"video/mpeg")
909 (".vox" .
"audio/basic")
910 (".vrml" .
"x-world/x-vrml")
911 (".wav" .
"audio/x-wav")
912 (".xls" .
"application/vnd.ms-excel")
913 (".wrl" .
"x-world/x-vrml")
914 (".xbm" .
"image/xbm")
915 (".xpm" .
"image/xpm")
916 (".xwd" .
"image/windowdump")
917 (".zip" .
"application/zip")
918 (".ai" .
"application/postscript")
919 (".jpe" .
"image/jpeg")
920 (".jpeg" .
"image/jpeg")
921 (".org" .
"text/x-org"))
922 "An alist of file extensions and corresponding MIME content-types.
923 This exists for you to customize the information in Lisp. It is
924 merged with values from mailcap files by `mailcap-parse-mimetypes'.")
926 (defvar mailcap-mimetypes-parsed-p nil
)
928 (defun mailcap-parse-mimetypes (&optional path force
)
929 "Parse out all the mimetypes specified in a Unix-style path string PATH.
930 Components of PATH are separated by the `path-separator' character
931 appropriate for this system. If PATH is omitted, use the value of
932 environment variable MIMETYPES if set; otherwise use a default path.
933 If FORCE, re-parse even if already parsed."
934 (interactive (list nil t
))
935 (when (or (not mailcap-mimetypes-parsed-p
)
939 ((getenv "MIMETYPES") (setq path
(getenv "MIMETYPES")))
940 ((memq system-type mailcap-poor-system-types
)
941 (setq path
'("~/mime.typ" "~/etc/mime.typ")))
943 ;; mime.types seems to be the normal name, definitely so
944 ;; on current GNUish systems. The search order follows
948 "/usr/etc/mime.types"
949 "/usr/local/etc/mime.types"
950 "/usr/local/www/conf/mime.types"
953 "/usr/etc/mime-types"
954 "/usr/local/etc/mime-types"
955 "/usr/local/www/conf/mime-types"))))
956 (dolist (fname (reverse (if (stringp path
)
957 (split-string path path-separator t
)
959 (when (file-readable-p fname
)
960 (mailcap-parse-mimetype-file fname
)))
961 (setq mailcap-mimetypes-parsed-p t
)))
963 (defun mailcap-parse-mimetype-file (fname)
964 "Parse out a mime-types file FNAME."
965 (let (type ; The MIME type for this line
966 extns
; The extensions for this line
967 save-pos
; Misc. saved buffer positions
970 (insert-file-contents fname
)
971 (mailcap-replace-regexp "#.*" "")
972 (mailcap-replace-regexp "\n+" "\n")
973 (mailcap-replace-regexp "[ \t]+$" "")
974 (goto-char (point-max))
975 (skip-chars-backward " \t\n")
976 (delete-region (point) (point-max))
977 (goto-char (point-min))
979 (skip-chars-forward " \t\n")
980 (setq save-pos
(point))
981 (skip-chars-forward "^ \t\n")
982 (downcase-region save-pos
(point))
983 (setq type
(buffer-substring save-pos
(point)))
985 (skip-chars-forward " \t")
986 (setq save-pos
(point))
987 (skip-chars-forward "^ \t\n")
988 (setq save-extn
(buffer-substring save-pos
(point)))
989 (push (cons (if (= (string-to-char save-extn
) ?.
)
990 save-extn
(concat "." save-extn
))
993 (setq mailcap-mime-extensions
(append extns mailcap-mime-extensions
)
996 (defun mailcap-extension-to-mime (extn)
997 "Return the MIME content type of the file extensions EXTN."
998 (mailcap-parse-mimetypes)
999 (if (and (stringp extn
)
1000 (not (eq (string-to-char extn
) ?.
)))
1001 (setq extn
(concat "." extn
)))
1002 (cdr (assoc (downcase extn
) mailcap-mime-extensions
)))
1004 (defun mailcap-mime-types ()
1005 "Return a list of MIME media types."
1006 (mailcap-parse-mimetypes)
1009 (mapcar 'cdr mailcap-mime-extensions
)
1011 (dolist (data mailcap-mime-data
)
1012 (dolist (info (cdr data
))
1013 (setq type
(cdr (assq 'type
(cdr info
))))
1014 (unless (string-match-p "\\*" type
)
1019 ;;; Useful supplementary functions
1022 (defun mailcap-file-default-commands (files)
1023 "Return a list of default commands for FILES."
1024 (mailcap-parse-mailcaps)
1025 (mailcap-parse-mimetypes)
1026 (let* ((all-mime-type
1027 ;; All unique MIME types from file extensions
1029 (mapcar (lambda (file)
1030 (mailcap-extension-to-mime
1031 (file-name-extension file t
)))
1034 ;; All MIME info lists
1036 (mapcar (lambda (mime-type)
1037 (mailcap-mime-info mime-type
'all
))
1040 ;; Intersection of mime-infos from different mime-types;
1041 ;; or just the first MIME info for a single MIME type
1042 (if (cdr all-mime-info
)
1044 (dolist (mi1 (car all-mime-info
))
1045 (dolist (mi2 (cdr all-mime-info
))
1046 (when (member mi1 mi2
)
1049 (car all-mime-info
))))
1050 ;; Command strings from `viewer' field of the MIME info
1053 (dolist (mime-info common-mime-info
)
1054 (setq command
(cdr (assq 'viewer mime-info
)))
1055 (when (stringp command
)
1057 (replace-regexp-in-string
1058 ;; Replace mailcap's `%s' placeholder
1059 ;; with dired's `?' placeholder
1061 (replace-regexp-in-string
1062 ;; Remove the final filename placeholder
1063 "[ \t\n]*\\('\\)?%s\\1?[ \t\n]*\\'" ""
1069 (defun mailcap-view-mime (type)
1070 "View the data in the current buffer that has MIME type TYPE.
1071 `mailcap-mime-data' determines the method to use."
1072 (let ((method (mailcap-mime-info type
)))
1073 (if (stringp method
)
1074 (shell-command-on-region (point-min) (point-max)
1075 ;; Use stdin as the "%s".
1083 ;;; mailcap.el ends here