1 ;;; mailcap.el --- MIME media types configuration
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Provides configuration of MIME media types from directly from Lisp
29 ;; and via the usual mailcap mechanism (RFC 1524). Deals with
30 ;; mime.types similarly.
34 (eval-when-compile (require 'cl))
39 "Definition of viewers for MIME types."
43 (defvar mailcap-parse-args-syntax-table
44 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
45 (modify-syntax-entry ?' "\"" table)
46 (modify-syntax-entry ?` "\"" table)
47 (modify-syntax-entry ?{ "(" table)
48 (modify-syntax-entry ?} ")" table)
50 "A syntax table for parsing SGML attributes.")
53 (when (featurep 'xemacs)
58 (defvar mailcap-print-command
60 (cons (if (boundp 'lpr-command)
63 (when (boundp 'lpr-switches)
64 (if (stringp lpr-switches)
68 "Shell command (including switches) used to print Postscript files.")
70 ;; Postpone using defcustom for this as it's so big and we essentially
71 ;; have to have two copies of the data around then. Perhaps just
72 ;; customize the Lisp viewers and rely on the normal configuration
73 ;; files for the rest? -- fx
74 (defvar mailcap-mime-data
77 (viewer . "gnumeric %s")
78 (test . (getenv "DISPLAY"))
79 (type . "application/vnd.ms-excel"))
81 (viewer . ssl-view-site-cert)
82 (test . (fboundp 'ssl-view-site-cert))
83 (type . "application/x-x509-ca-cert"))
85 (viewer . ssl-view-user-cert)
86 (test . (fboundp 'ssl-view-user-cert))
87 (type . "application/x-x509-user-cert"))
89 (viewer . mailcap-save-binary-file)
91 (type . "application/octet-stream"))
93 (viewer . "xdvi -safer %s")
94 (test . (eq window-system 'x))
96 (type . "application/dvi")
97 ("print" . "dvips -qRP %s"))
99 (viewer . "dvitty %s")
100 (test . (not (getenv "DISPLAY")))
101 (type . "application/dvi")
102 ("print" . "dvips -qRP %s"))
104 (viewer . mailcap-maybe-eval)
105 (type . "application/emacs-lisp"))
107 (viewer . mailcap-maybe-eval)
108 (type . "application/x-emacs-lisp"))
110 (viewer . mailcap-save-binary-file)
112 (type . "application/x-tar"))
115 (test . (fboundp 'tex-mode))
116 (type . "application/x-latex"))
119 (test . (fboundp 'tex-mode))
120 (type . "application/x-tex"))
123 (test . (fboundp 'tex-mode))
124 (type . "application/latex"))
127 (test . (fboundp 'tex-mode))
128 (type . "application/tex"))
130 (viewer . texinfo-mode)
131 (test . (fboundp 'texinfo-mode))
132 (type . "application/tex"))
134 (viewer . mailcap-save-binary-file)
136 (type . "application/zip")
139 (viewer . "gv -safer %s")
140 (type . "application/pdf")
141 (test . window-system)
142 ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command)))
145 (type . "application/pdf")
146 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
147 (test . (eq window-system 'x)))
150 (type . "application/pdf")
151 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
152 (test . (eq window-system 'x)))
154 (viewer . ,(concat "pdftotext %s -"))
155 (type . "application/pdf")
156 ("print" . ,(concat "pdftops %s - | " mailcap-print-command))
159 (viewer . "gv -safer %s")
160 (type . "application/postscript")
161 (test . window-system)
162 ("print" . ,(concat mailcap-print-command " %s"))
165 (viewer . "ghostview -dSAFER %s")
166 (type . "application/postscript")
167 (test . (eq window-system 'x))
168 ("print" . ,(concat mailcap-print-command " %s"))
171 (viewer . "ps2ascii %s")
172 (type . "application/postscript")
173 (test . (not (getenv "DISPLAY")))
174 ("print" . ,(concat mailcap-print-command " %s"))
177 (viewer . sieve-mode)
178 (test . (fboundp 'sieve-mode))
179 (type . "application/sieve"))
181 (viewer . "gpg --import --interactive --verbose")
182 (type . "application/pgp-keys")
186 (viewer . "maplay %s")
187 (type . "audio/x-mpeg"))
189 (viewer . "showaudio")
193 (viewer . mm-view-message)
194 (test . (and (featurep 'gnus)
196 (type . "message/rfc822"))
199 (test . (fboundp 'vm-mode))
200 (type . "message/rfc822"))
203 (test . (fboundp 'w3-mode))
204 (type . "message/rfc822"))
207 (type . "message/rfc822")))
210 (viewer . "xwud -in %s")
211 (type . "image/x-xwd")
212 ("compose" . "xwd -frame > %s")
213 (test . (eq window-system 'x))
216 (viewer . "xwud -in %s")
217 (type . "image/x-xwd")
218 ("compose" . "xwd -frame > %s")
219 (test . (eq window-system 'x))
222 (viewer . "xwud -in %s")
223 (type . "image/x-xwd")
224 ("compose" . "xwd -frame > %s")
225 (test . (eq window-system 'x))
228 (viewer . "display %s")
230 (test . (eq window-system 'x))
235 (test . (eq window-system 'x))
240 (test . (fboundp 'w3-mode))
241 (type . "text/plain"))
244 (test . (fboundp 'view-mode))
245 (type . "text/plain"))
247 (viewer . fundamental-mode)
248 (type . "text/plain"))
250 (viewer . enriched-decode)
251 (test . (fboundp 'enriched-decode))
252 (type . "text/enriched"))
254 (viewer . mm-w3-prepare-buffer)
255 (test . (fboundp 'w3-prepare-buffer))
256 (type . "text/html"))
259 (test . (fboundp 'dns-mode))
260 (type . "text/dns")))
263 (viewer . "mpeg_play %s")
264 (type . "video/mpeg")
265 (test . (eq window-system 'x))
269 (viewer . "webspace -remote %s -URL %u")
270 (type . "x-world/x-vrml")
276 (type . "archive/tar")
277 (test . (fboundp 'tar-mode)))))
278 "The mailcap structure is an assoc list of assoc lists.
279 1st assoc list is keyed on the major content-type
280 2nd assoc list is keyed on the minor content-type (which can be a regexp)
285 (\"postscript\" . <info>))
287 (\"plain\" . <info>)))
289 Where <info> is another assoc list of the various information
290 related to the mailcap RFC 1524. This is keyed on the lowercase
291 attribute name (viewer, test, etc). This looks like:
292 ((viewer . VIEWERINFO)
297 Where VIEWERINFO specifies how the content-type is viewed. Can be
298 a string, in which case it is run through a shell, with
299 appropriate parameters, or a symbol, in which case the symbol is
300 `funcall'ed, with the buffer as an argument.
302 TESTINFO is a test for the viewer's applicability, or nil. If nil, it
303 means the viewer is always valid. If it is a Lisp function, it is
304 called with a list of items from any extra fields from the
305 Content-Type header as argument to return a boolean value for the
306 validity. Otherwise, if it is a non-function Lisp symbol or list
307 whose car is a symbol, it is `eval'led to yield the validity. If it
308 is a string or list of strings, it represents a shell command to run
309 to return a true or false shell value for the validity.")
310 (put 'mailcap-mime-data 'risky-local-variable t)
312 (defcustom mailcap-download-directory nil
313 "*Directory to which `mailcap-save-binary-file' downloads files by default.
314 nil means your home directory."
315 :type '(choice (const :tag "Home directory" nil)
319 (defvar mailcap-poor-system-types
320 '(ms-dos ms-windows windows-nt win32 w32 mswindows)
321 "Systems that don't have a Unix-like directory hierarchy.")
324 ;;; Utility functions
327 (defun mailcap-save-binary-file ()
328 (goto-char (point-min))
330 (let ((file (read-file-name
331 "Filename to save as: "
332 (or mailcap-download-directory "~/")))
333 (require-final-newline nil))
334 (write-region (point-min) (point-max) file))
335 (kill-buffer (current-buffer))))
337 (defvar mailcap-maybe-eval-warning
340 This MIME part contains untrusted and possibly harmful content.
341 If you evaluate the Emacs Lisp code contained in it, a lot of nasty
342 things can happen. Please examine the code very carefully before you
343 instruct Emacs to evaluate it. You can browse the buffer containing
344 the code using \\[scroll-other-window].
346 If you are unsure what to do, please answer \"no\"."
347 "Text of warning message displayed by `mailcap-maybe-eval'.
348 Make sure that this text consists only of few text lines. Otherwise,
349 Gnus might fail to display all of it.")
351 (defun mailcap-maybe-eval ()
352 "Maybe evaluate a buffer of Emacs Lisp code."
353 (let ((lisp-buffer (current-buffer)))
354 (goto-char (point-min))
356 (save-window-excursion
357 (delete-other-windows)
358 (let ((buffer (get-buffer-create (generate-new-buffer-name
361 (with-current-buffer buffer
362 (insert (substitute-command-keys
363 mailcap-maybe-eval-warning))
364 (goto-char (point-min))
365 (display-buffer buffer)
366 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? "))
367 (kill-buffer buffer))))
368 (eval-buffer (current-buffer)))
369 (when (buffer-live-p lisp-buffer)
370 (with-current-buffer lisp-buffer
371 (emacs-lisp-mode)))))
375 ;;; The mailcap parser
378 (defun mailcap-replace-regexp (regexp to-string)
379 ;; Quiet replace-regexp.
380 (goto-char (point-min))
381 (while (re-search-forward regexp nil t)
382 (replace-match to-string t nil)))
384 (defvar mailcap-parsed-p nil)
386 (defun mailcap-parse-mailcaps (&optional path force)
387 "Parse out all the mailcaps specified in a path string PATH.
388 Components of PATH are separated by the `path-separator' character
389 appropriate for this system. If FORCE, re-parse even if already
390 parsed. If PATH is omitted, use the value of environment variable
391 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus
392 /usr/local/etc/mailcap."
393 (interactive (list nil t))
394 (when (or (not mailcap-parsed-p)
398 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
399 ((memq system-type mailcap-poor-system-types)
400 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap")))
402 ;; This is per RFC 1524, specifically
403 ;; with /usr before /usr/local.
404 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap"
405 "/usr/local/etc/mailcap"))))
406 (let ((fnames (reverse
408 (delete "" (split-string path path-separator))
412 (setq fname (car fnames))
413 (if (and (file-readable-p fname)
414 (file-regular-p fname))
415 (mailcap-parse-mailcap fname))
416 (setq fnames (cdr fnames))))
417 (setq mailcap-parsed-p t)))
419 (defun mailcap-parse-mailcap (fname)
420 "Parse out the mailcap file specified by FNAME."
421 (let (major ; The major mime type (image/audio/etc)
422 minor ; The minor mime type (gif, basic, etc)
423 save-pos ; Misc saved positions used in parsing
424 viewer ; How to view this mime type
425 info ; Misc info about this mime type
428 (insert-file-contents fname)
429 (set-syntax-table mailcap-parse-args-syntax-table)
430 (mailcap-replace-regexp "#.*" "") ; Remove all comments
431 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces
432 (mailcap-replace-regexp "\n+" "\n") ; And blank lines
433 (goto-char (point-max))
434 (skip-chars-backward " \t\n")
435 (delete-region (point) (point-max))
437 (skip-chars-backward " \t\n")
439 (setq save-pos (point)
441 (skip-chars-forward "^/; \t\n")
442 (downcase-region save-pos (point))
443 (setq major (buffer-substring save-pos (point)))
444 (skip-chars-forward " \t")
446 (when (eq (char-after) ?/)
448 (skip-chars-forward " \t")
449 (setq save-pos (point))
450 (skip-chars-forward "^; \t\n")
451 (downcase-region save-pos (point))
454 ((eq ?* (or (char-after save-pos) 0)) ".*")
455 ((= (point) save-pos) ".*")
456 (t (regexp-quote (buffer-substring save-pos (point)))))))
457 (skip-chars-forward " \t")
458 ;;; Got the major/minor chunks, now for the viewers/etc
459 ;;; The first item _must_ be a viewer, according to the
460 ;;; RFC for mailcap files (#1524)
462 (when (eq (char-after) ?\;)
464 (skip-chars-forward " \t")
465 (setq save-pos (point))
466 (skip-chars-forward "^;\n")
468 (while (eq (char-before) ?\\)
469 (backward-delete-char 1)
471 (skip-chars-forward "^;\n"))
472 (if (eq (or (char-after save-pos) 0) ?')
474 (narrow-to-region (1+ save-pos) (point))
475 (goto-char (point-min))
477 (read (current-buffer))
478 (goto-char (point-max))
480 (setq viewer (buffer-substring save-pos (point)))))
481 (setq save-pos (point))
483 (unless (equal viewer "")
484 (setq info (nconc (list (cons 'viewer viewer)
485 (cons 'type (concat major "/"
486 (if (string= minor ".*")
488 (mailcap-parse-mailcap-extras save-pos (point))))
489 (mailcap-mailcap-entry-passes-test info)
490 (mailcap-add-mailcap-entry major minor info))
491 (beginning-of-line)))))
493 (defun mailcap-parse-mailcap-extras (st nd)
494 "Grab all the extra stuff from a mailcap entry."