Commit | Line | Data |
---|---|---|
c0393b5e | 1 | ;;; mailcap.el --- MIME media types configuration |
e84b4b86 TTN |
2 | |
3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, | |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
c113de23 GM |
5 | |
6 | ;; Author: William M. Perry <wmperry@aventail.com> | |
7 | ;; Lars Magne Ingebrigtsen <larsi@gnus.org> | |
c0393b5e | 8 | ;; Keywords: news, mail, multimedia |
c113de23 GM |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
5a9dffec | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
c113de23 GM |
15 | ;; any later version. |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
3a35cf56 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
c113de23 GM |
26 | |
27 | ;;; Commentary: | |
28 | ||
c0393b5e DL |
29 | ;; Provides configuration of MIME media types from directly from Lisp |
30 | ;; and via the usual mailcap mechanism (RFC 1524). Deals with | |
31 | ;; mime.types similarly. | |
32 | ||
c113de23 GM |
33 | ;;; Code: |
34 | ||
35 | (eval-when-compile (require 'cl)) | |
36 | (require 'mail-parse) | |
37 | (require 'mm-util) | |
38 | ||
c0393b5e DL |
39 | (defgroup mailcap nil |
40 | "Definition of viewers for MIME types." | |
41 | :version "21.1" | |
42 | :group 'mime) | |
43 | ||
c113de23 GM |
44 | (defvar mailcap-parse-args-syntax-table |
45 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
46 | (modify-syntax-entry ?' "\"" table) | |
47 | (modify-syntax-entry ?` "\"" table) | |
48 | (modify-syntax-entry ?{ "(" table) | |
49 | (modify-syntax-entry ?} ")" table) | |
50 | table) | |
23f87bed MB |
51 | "A syntax table for parsing SGML attributes.") |
52 | ||
53 | (eval-and-compile | |
54 | (when (featurep 'xemacs) | |
55 | (condition-case nil | |
56 | (require 'lpr) | |
57 | (error nil)))) | |
58 | ||
59 | (defvar mailcap-print-command | |
60 | (mapconcat 'identity | |
61 | (cons (if (boundp 'lpr-command) | |
62 | lpr-command | |
63 | "lpr") | |
64 | (when (boundp 'lpr-switches) | |
65 | (if (stringp lpr-switches) | |
66 | (list lpr-switches) | |
67 | lpr-switches))) | |
68 | " ") | |
69 | "Shell command (including switches) used to print Postscript files.") | |
c113de23 | 70 | |
c0393b5e DL |
71 | ;; Postpone using defcustom for this as it's so big and we essentially |
72 | ;; have to have two copies of the data around then. Perhaps just | |
73 | ;; customize the Lisp viewers and rely on the normal configuration | |
74 | ;; files for the rest? -- fx | |
c113de23 | 75 | (defvar mailcap-mime-data |
23f87bed MB |
76 | `(("application" |
77 | ("vnd.ms-excel" | |
78 | (viewer . "gnumeric %s") | |
79 | (test . (getenv "DISPLAY")) | |
80 | (type . "application/vnd.ms-excel")) | |
c113de23 GM |
81 | ("x-x509-ca-cert" |
82 | (viewer . ssl-view-site-cert) | |
83 | (test . (fboundp 'ssl-view-site-cert)) | |
84 | (type . "application/x-x509-ca-cert")) | |
85 | ("x-x509-user-cert" | |
86 | (viewer . ssl-view-user-cert) | |
87 | (test . (fboundp 'ssl-view-user-cert)) | |
88 | (type . "application/x-x509-user-cert")) | |
89 | ("octet-stream" | |
90 | (viewer . mailcap-save-binary-file) | |
91 | (non-viewer . t) | |
92 | (type . "application/octet-stream")) | |
c113de23 | 93 | ("dvi" |
23f87bed MB |
94 | (viewer . "xdvi -safer %s") |
95 | (test . (eq window-system 'x)) | |
c113de23 | 96 | ("needsx11") |
23f87bed MB |
97 | (type . "application/dvi") |
98 | ("print" . "dvips -qRP %s")) | |
c113de23 GM |
99 | ("dvi" |
100 | (viewer . "dvitty %s") | |
101 | (test . (not (getenv "DISPLAY"))) | |
23f87bed MB |
102 | (type . "application/dvi") |
103 | ("print" . "dvips -qRP %s")) | |
c113de23 GM |
104 | ("emacs-lisp" |
105 | (viewer . mailcap-maybe-eval) | |
106 | (type . "application/emacs-lisp")) | |
23f87bed MB |
107 | ("x-emacs-lisp" |
108 | (viewer . mailcap-maybe-eval) | |
109 | (type . "application/x-emacs-lisp")) | |
c113de23 GM |
110 | ("x-tar" |
111 | (viewer . mailcap-save-binary-file) | |
112 | (non-viewer . t) | |
113 | (type . "application/x-tar")) | |
114 | ("x-latex" | |
115 | (viewer . tex-mode) | |
116 | (test . (fboundp 'tex-mode)) | |
117 | (type . "application/x-latex")) | |
118 | ("x-tex" | |
119 | (viewer . tex-mode) | |
120 | (test . (fboundp 'tex-mode)) | |
121 | (type . "application/x-tex")) | |
122 | ("latex" | |
123 | (viewer . tex-mode) | |
124 | (test . (fboundp 'tex-mode)) | |
125 | (type . "application/latex")) | |
126 | ("tex" | |
127 | (viewer . tex-mode) | |
128 | (test . (fboundp 'tex-mode)) | |
129 | (type . "application/tex")) | |
130 | ("texinfo" | |
131 | (viewer . texinfo-mode) | |
132 | (test . (fboundp 'texinfo-mode)) | |
133 | (type . "application/tex")) | |
134 | ("zip" | |
135 | (viewer . mailcap-save-binary-file) | |
136 | (non-viewer . t) | |
137 | (type . "application/zip") | |
138 | ("copiousoutput")) | |
c0393b5e | 139 | ("pdf" |
23f87bed | 140 | (viewer . "gv -safer %s") |
c0393b5e | 141 | (type . "application/pdf") |
23f87bed MB |
142 | (test . window-system) |
143 | ("print" . ,(concat "pdf2ps %s - | " mailcap-print-command))) | |
c0393b5e | 144 | ("pdf" |
f0096211 | 145 | (viewer . "gpdf %s") |
c0393b5e | 146 | (type . "application/pdf") |
23f87bed MB |
147 | ("print" . ,(concat "pdftops %s - | " mailcap-print-command)) |
148 | (test . (eq window-system 'x))) | |
c113de23 | 149 | ("pdf" |
f0096211 MB |
150 | (viewer . "xpdf %s") |
151 | (type . "application/pdf") | |
152 | ("print" . ,(concat "pdftops %s - | " mailcap-print-command)) | |
153 | (test . (eq window-system 'x))) | |
23f87bed MB |
154 | ("pdf" |
155 | (viewer . ,(concat "pdftotext %s -")) | |
156 | (type . "application/pdf") | |
157 | ("print" . ,(concat "pdftops %s - | " mailcap-print-command)) | |
158 | ("copiousoutput")) | |
c113de23 | 159 | ("postscript" |
c0393b5e DL |
160 | (viewer . "gv -safer %s") |
161 | (type . "application/postscript") | |
162 | (test . window-system) | |
23f87bed | 163 | ("print" . ,(concat mailcap-print-command " %s")) |
c0393b5e | 164 | ("needsx11")) |
c113de23 GM |
165 | ("postscript" |
166 | (viewer . "ghostview -dSAFER %s") | |
167 | (type . "application/postscript") | |
23f87bed MB |
168 | (test . (eq window-system 'x)) |
169 | ("print" . ,(concat mailcap-print-command " %s")) | |
c113de23 GM |
170 | ("needsx11")) |
171 | ("postscript" | |
172 | (viewer . "ps2ascii %s") | |
173 | (type . "application/postscript") | |
174 | (test . (not (getenv "DISPLAY"))) | |
23f87bed MB |
175 | ("print" . ,(concat mailcap-print-command " %s")) |
176 | ("copiousoutput")) | |
177 | ("sieve" | |
178 | (viewer . sieve-mode) | |
179 | (test . (fboundp 'sieve-mode)) | |
180 | (type . "application/sieve")) | |
181 | ("pgp-keys" | |
182 | (viewer . "gpg --import --interactive --verbose") | |
183 | (type . "application/pgp-keys") | |
184 | ("needsterminal"))) | |
c113de23 GM |
185 | ("audio" |
186 | ("x-mpeg" | |
187 | (viewer . "maplay %s") | |
188 | (type . "audio/x-mpeg")) | |
189 | (".*" | |
190 | (viewer . "showaudio") | |
191 | (type . "audio/*"))) | |
192 | ("message" | |
193 | ("rfc-*822" | |
194 | (viewer . mm-view-message) | |
195 | (test . (and (featurep 'gnus) | |
196 | (gnus-alive-p))) | |
197 | (type . "message/rfc822")) | |
198 | ("rfc-*822" | |
199 | (viewer . vm-mode) | |
200 | (test . (fboundp 'vm-mode)) | |
201 | (type . "message/rfc822")) | |
202 | ("rfc-*822" | |
203 | (viewer . w3-mode) | |
204 | (test . (fboundp 'w3-mode)) | |
205 | (type . "message/rfc822")) | |
206 | ("rfc-*822" | |
207 | (viewer . view-mode) | |
c113de23 GM |
208 | (type . "message/rfc822"))) |
209 | ("image" | |
210 | ("x-xwd" | |
211 | (viewer . "xwud -in %s") | |
212 | (type . "image/x-xwd") | |
213 | ("compose" . "xwd -frame > %s") | |
23f87bed | 214 | (test . (eq window-system 'x)) |
c113de23 GM |
215 | ("needsx11")) |
216 | ("x11-dump" | |
217 | (viewer . "xwud -in %s") | |
218 | (type . "image/x-xwd") | |
219 | ("compose" . "xwd -frame > %s") | |
23f87bed | 220 | (test . (eq window-system 'x)) |
c113de23 GM |
221 | ("needsx11")) |
222 | ("windowdump" | |
223 | (viewer . "xwud -in %s") | |
224 | (type . "image/x-xwd") | |
225 | ("compose" . "xwd -frame > %s") | |
23f87bed | 226 | (test . (eq window-system 'x)) |
c113de23 | 227 | ("needsx11")) |
c113de23 GM |
228 | (".*" |
229 | (viewer . "display %s") | |
230 | (type . "image/*") | |
23f87bed | 231 | (test . (eq window-system 'x)) |
c113de23 GM |
232 | ("needsx11")) |
233 | (".*" | |
234 | (viewer . "ee %s") | |
235 | (type . "image/*") | |
23f87bed | 236 | (test . (eq window-system 'x)) |
c113de23 GM |
237 | ("needsx11"))) |
238 | ("text" | |
239 | ("plain" | |
240 | (viewer . w3-mode) | |
241 | (test . (fboundp 'w3-mode)) | |
242 | (type . "text/plain")) | |
243 | ("plain" | |
244 | (viewer . view-mode) | |
245 | (test . (fboundp 'view-mode)) | |
246 | (type . "text/plain")) | |
247 | ("plain" | |
248 | (viewer . fundamental-mode) | |
249 | (type . "text/plain")) | |
250 | ("enriched" | |
23f87bed | 251 | (viewer . enriched-decode) |
c113de23 GM |
252 | (test . (fboundp 'enriched-decode)) |
253 | (type . "text/enriched")) | |
254 | ("html" | |
255 | (viewer . mm-w3-prepare-buffer) | |
256 | (test . (fboundp 'w3-prepare-buffer)) | |
257 | (type . "text/html"))) | |
258 | ("video" | |
259 | ("mpeg" | |
260 | (viewer . "mpeg_play %s") | |
261 | (type . "video/mpeg") | |
23f87bed | 262 | (test . (eq window-system 'x)) |
c113de23 GM |
263 | ("needsx11"))) |
264 | ("x-world" | |
265 | ("x-vrml" | |
266 | (viewer . "webspace -remote %s -URL %u") | |
267 | (type . "x-world/x-vrml") | |
268 | ("description" | |
269 | "VRML document"))) | |
270 | ("archive" | |
271 | ("tar" | |
272 | (viewer . tar-mode) | |
273 | (type . "archive/tar") | |
274 | (test . (fboundp 'tar-mode))))) | |
275 | "The mailcap structure is an assoc list of assoc lists. | |
276 | 1st assoc list is keyed on the major content-type | |
277 | 2nd assoc list is keyed on the minor content-type (which can be a regexp) | |
278 | ||
279 | Which looks like: | |
280 | ----------------- | |
281 | ((\"application\" | |
282 | (\"postscript\" . <info>)) | |
283 | (\"text\" | |
284 | (\"plain\" . <info>))) | |
285 | ||
286 | Where <info> is another assoc list of the various information | |
c0393b5e | 287 | related to the mailcap RFC 1524. This is keyed on the lowercase |
c113de23 | 288 | attribute name (viewer, test, etc). This looks like: |
c0393b5e DL |
289 | ((viewer . VIEWERINFO) |
290 | (test . TESTINFO) | |
291 | (xxxx . \"STRING\") | |
292 | FLAG) | |
c113de23 | 293 | |
c0393b5e | 294 | Where VIEWERINFO specifies how the content-type is viewed. Can be |
c113de23 GM |
295 | a string, in which case it is run through a shell, with |
296 | appropriate parameters, or a symbol, in which case the symbol is | |
c0393b5e DL |
297 | `funcall'ed, with the buffer as an argument. |
298 | ||
299 | TESTINFO is a test for the viewer's applicability, or nil. If nil, it | |
300 | means the viewer is always valid. If it is a Lisp function, it is | |
301 | called with a list of items from any extra fields from the | |
302 | Content-Type header as argument to return a boolean value for the | |
303 | validity. Otherwise, if it is a non-function Lisp symbol or list | |
304 | whose car is a symbol, it is `eval'led to yield the validity. If it | |
305 | is a string or list of strings, it represents a shell command to run | |
306 | to return a true or false shell value for the validity.") | |
f8bbbafb | 307 | (put 'mailcap-mime-data 'risky-local-variable t) |
c0393b5e DL |
308 | |
309 | (defcustom mailcap-download-directory nil | |
003dabb7 | 310 | "*Directory to which `mailcap-save-binary-file' downloads files by default. |
f0529b5b | 311 | nil means your home directory." |
003dabb7 DL |
312 | :type '(choice (const :tag "Home directory" nil) |
313 | directory) | |
c0393b5e | 314 | :group 'mailcap) |
c113de23 | 315 | |
23f87bed MB |
316 | (defvar mailcap-poor-system-types |
317 | '(ms-dos ms-windows windows-nt win32 w32 mswindows) | |
318 | "Systems that don't have a Unix-like directory hierarchy.") | |
319 | ||
c113de23 GM |
320 | ;;; |
321 | ;;; Utility functions | |
322 | ;;; | |
323 | ||
c113de23 GM |
324 | (defun mailcap-save-binary-file () |
325 | (goto-char (point-min)) | |
326 | (unwind-protect | |
327 | (let ((file (read-file-name | |
328 | "Filename to save as: " | |
329 | (or mailcap-download-directory "~/"))) | |
330 | (require-final-newline nil)) | |
331 | (write-region (point-min) (point-max) file)) | |
332 | (kill-buffer (current-buffer)))) | |
333 | ||
334 | (defvar mailcap-maybe-eval-warning | |
335 | "*** WARNING *** | |
336 | ||
c0393b5e | 337 | This MIME part contains untrusted and possibly harmful content. |
c113de23 GM |
338 | If you evaluate the Emacs Lisp code contained in it, a lot of nasty |
339 | things can happen. Please examine the code very carefully before you | |
340 | instruct Emacs to evaluate it. You can browse the buffer containing | |
341 | the code using \\[scroll-other-window]. | |
342 | ||
343 | If you are unsure what to do, please answer \"no\"." | |
344 | "Text of warning message displayed by `mailcap-maybe-eval'. | |
345 | Make sure that this text consists only of few text lines. Otherwise, | |
346 | Gnus might fail to display all of it.") | |
a1506d29 | 347 | |
c113de23 | 348 | (defun mailcap-maybe-eval () |
c0393b5e | 349 | "Maybe evaluate a buffer of Emacs Lisp code." |
c113de23 GM |
350 | (let ((lisp-buffer (current-buffer))) |
351 | (goto-char (point-min)) | |
352 | (when | |
353 | (save-window-excursion | |
354 | (delete-other-windows) | |
355 | (let ((buffer (get-buffer-create (generate-new-buffer-name | |
356 | "*Warning*")))) | |
357 | (unwind-protect | |
358 | (with-current-buffer buffer | |
c0393b5e | 359 | (insert (substitute-command-keys |
c113de23 GM |
360 | mailcap-maybe-eval-warning)) |
361 | (goto-char (point-min)) | |
362 | (display-buffer buffer) | |
363 | (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? ")) | |
364 | (kill-buffer buffer)))) | |
365 | (eval-buffer (current-buffer))) | |
366 | (when (buffer-live-p lisp-buffer) | |
367 | (with-current-buffer lisp-buffer | |
368 | (emacs-lisp-mode))))) | |
369 | ||
370 | ||
371 | ;;; | |
372 | ;;; The mailcap parser | |
373 | ;;; | |
374 | ||
375 | (defun mailcap-replace-regexp (regexp to-string) | |
376 | ;; Quiet replace-regexp. | |
377 | (goto-char (point-min)) | |
378 | (while (re-search-forward regexp nil t) | |
379 | (replace-match to-string t nil))) | |
380 | ||
381 | (defvar mailcap-parsed-p nil) | |
382 | ||
383 | (defun mailcap-parse-mailcaps (&optional path force) | |
384 | "Parse out all the mailcaps specified in a path string PATH. | |
385 | Components of PATH are separated by the `path-separator' character | |
386 | appropriate for this system. If FORCE, re-parse even if already | |
387 | parsed. If PATH is omitted, use the value of environment variable | |
388 | MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus | |
389 | /usr/local/etc/mailcap." | |
390 | (interactive (list nil t)) | |
391 | (when (or (not mailcap-parsed-p) | |
392 | force) | |
393 | (cond | |
394 | (path nil) | |
395 | ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS"))) | |
23f87bed | 396 | ((memq system-type mailcap-poor-system-types) |
c113de23 GM |
397 | (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap"))) |
398 | (t (setq path | |
399 | ;; This is per RFC 1524, specifically | |
400 | ;; with /usr before /usr/local. | |
401 | '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap" | |
402 | "/usr/local/etc/mailcap")))) | |
403 | (let ((fnames (reverse | |
404 | (if (stringp path) | |
9b198954 | 405 | (delete "" (split-string path path-separator)) |
c113de23 GM |
406 | path))) |
407 | fname) | |
408 | (while fnames | |
409 | (setq fname (car fnames)) | |
410 | (if (and (file-readable-p fname) | |
411 | (file-regular-p fname)) | |
412 | (mailcap-parse-mailcap fname)) | |
413 | (setq fnames (cdr fnames)))) | |
414 | (setq mailcap-parsed-p t))) | |
415 | ||
416 | (defun mailcap-parse-mailcap (fname) | |
c0393b5e | 417 | "Parse out the mailcap file specified by FNAME." |
c113de23 GM |
418 | (let (major ; The major mime type (image/audio/etc) |
419 | minor ; The minor mime type (gif, basic, etc) | |
420 | save-pos ; Misc saved positions used in parsing | |
421 | viewer ; How to view this mime type | |
422 | info ; Misc info about this mime type | |
423 | ) | |
424 | (with-temp-buffer | |
425 | (insert-file-contents fname) | |
426 | (set-syntax-table mailcap-parse-args-syntax-table) | |
427 | (mailcap-replace-regexp "#.*" "") ; Remove all comments | |
428 | (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces | |
429 | (mailcap-replace-regexp "\n+" "\n") ; And blank lines | |
430 | (goto-char (point-max)) | |
431 | (skip-chars-backward " \t\n") | |
432 | (delete-region (point) (point-max)) | |
433 | (while (not (bobp)) | |
434 | (skip-chars-backward " \t\n") | |
435 | (beginning-of-line) | |
436 | (setq save-pos (point) | |
437 | info nil) | |
438 | (skip-chars-forward "^/; \t\n") | |
439 | (downcase-region save-pos (point)) | |
440 | (setq major (buffer-substring save-pos (point))) | |
441 | (skip-chars-forward " \t") | |
442 | (setq minor "") | |
443 | (when (eq (char-after) ?/) | |
444 | (forward-char) | |
445 | (skip-chars-forward " \t") | |
446 | (setq save-pos (point)) | |
447 | (skip-chars-forward "^; \t\n") | |
448 | (downcase-region save-pos (point)) | |
449 | (setq minor | |
450 | (cond | |
451 | ((eq ?* (or (char-after save-pos) 0)) ".*") | |
452 | ((= (point) save-pos) ".*") | |
453 | (t (regexp-quote (buffer-substring save-pos (point))))))) | |
454 | (skip-chars-forward " \t") | |
455 | ;;; Got the major/minor chunks, now for the viewers/etc | |
456 | ;;; The first item _must_ be a viewer, according to the | |
c0393b5e | 457 | ;;; RFC for mailcap files (#1524) |
c113de23 | 458 | (setq viewer "") |
c0393b5e | 459 | (when (eq (char-after) ?\;) |
c113de23 GM |
460 | (forward-char) |
461 | (skip-chars-forward " \t") | |
462 | (setq save-pos (point)) | |
463 | (skip-chars-forward "^;\n") | |
464 | ;; skip \; | |
465 | (while (eq (char-before) ?\\) | |
466 | (backward-delete-char 1) | |
467 | (forward-char) | |
468 | (skip-chars-forward "^;\n")) | |
469 | (if (eq (or (char-after save-pos) 0) ?') | |
470 | (setq viewer (progn | |
471 | (narrow-to-region (1+ save-pos) (point)) | |
472 | (goto-char (point-min)) | |
473 | (prog1 | |
474 | (read (current-buffer)) | |
475 | (goto-char (point-max)) | |
476 | (widen)))) | |
477 | (setq viewer (buffer-substring save-pos (point))))) | |
478 | (setq save-pos (point)) | |
479 | (end-of-line) | |
c0393b5e | 480 | (unless (equal viewer "") |
c113de23 GM |
481 | (setq info (nconc (list (cons 'viewer viewer) |
482 | (cons 'type (concat major "/" | |
483 | (if (string= minor ".*") | |
484 | "*" minor)))) | |
485 | (mailcap-parse-mailcap-extras save-pos (point)))) | |
486 | (mailcap-mailcap-entry-passes-test info) | |
487 | (mailcap-add-mailcap-entry major minor info)) | |
488 | (beginning-of-line))))) | |
489 | ||
490 | (defun mailcap-parse-mailcap-extras (st nd) | |
c0393b5e | 491 | "Grab all the extra stuff from a mailcap entry." |
c113de23 GM |
492 | (let ( |
493 | name ; From name= | |
494 | value ; its value | |
495 | results ; Assoc list of results | |
496 | name-pos ; Start of XXXX= position | |
497 | val-pos ; Start of value position | |
498 | done ; Found end of \'d ;s? | |
499 | ) | |
500 | (save-restriction | |
501 | (narrow-to-region st nd) | |
502 | (goto-char (point-min)) | |
503 | (skip-chars-forward " \n\t;") | |
504 | (while (not (eobp)) | |
505 | (setq done nil) | |
506 | (setq name-pos (point)) | |
507 | (skip-chars-forward "^ \n\t=;") | |
508 | (downcase-region name-pos (point)) | |
509 | (setq name (buffer-substring name-pos (point))) | |
510 | (skip-chars-forward " \t\n") | |
511 | (if (not (eq (char-after (point)) ?=)) ; There is no value | |
512 | (setq value t) | |
513 | (skip-chars-forward " \t\n=") | |
514 | (setq val-pos (point)) | |
515 | (if (memq (char-after val-pos) '(?\" ?')) | |
516 | (progn | |
517 | (setq val-pos (1+ val-pos)) | |
518 | (condition-case nil | |
519 | (progn | |
520 | (forward-sexp 1) | |
521 | (backward-char 1)) | |
522 | (error (goto-char (point-max))))) | |
523 | (while (not done) | |
524 | (skip-chars-forward "^;") | |
525 | (if (eq (char-after (1- (point))) ?\\ ) | |
526 | (progn | |
527 | (subst-char-in-region (1- (point)) (point) ?\\ ? ) | |
528 | (skip-chars-forward ";")) | |
529 | (setq done t)))) | |
530 | (setq value (buffer-substring val-pos (point)))) | |
7347faa8 MB |
531 | ;; `test' as symbol, others like "copiousoutput" and "needsx11" as |
532 | ;; strings | |
533 | (setq results (cons (cons (if (string-equal name "test") | |
534 | 'test | |
535 | name) | |
536 | value) results)) | |
c113de23 GM |
537 | (skip-chars-forward " \";\n\t")) |
538 | results))) | |
539 | ||
540 | (defun mailcap-mailcap-entry-passes-test (info) | |
c0393b5e DL |
541 | "Return non-nil iff mailcap entry INFO passes its test clause. |
542 | Also return non-nil if no test clause is present." | |
543 | (let ((test (assq 'test info)) ; The test clause | |
544 | status) | |
c113de23 GM |
545 | (setq status (and test (split-string (cdr test) " "))) |
546 | (if (and (or (assoc "needsterm" info) | |
547 | (assoc "needsterminal" info) | |
548 | (assoc "needsx11" info)) | |
549 | (not (getenv "DISPLAY"))) | |
550 | (setq status nil) | |
551 | (cond | |
552 | ((and (equal (nth 0 status) "test") | |
553 | (equal (nth 1 status) "-n") | |
554 | (or (equal (nth 2 status) "$DISPLAY") | |
555 | (equal (nth 2 status) "\"$DISPLAY\""))) | |
556 | (setq status (if (getenv "DISPLAY") t nil))) | |
557 | ((and (equal (nth 0 status) "test") | |
558 | (equal (nth 1 status) "-z") | |
559 | (or (equal (nth 2 status) "$DISPLAY") | |
560 | (equal (nth 2 status) "\"$DISPLAY\""))) | |
561 | (setq status (if (getenv "DISPLAY") nil t))) | |
562 | (test nil) | |
563 | (t nil))) | |
564 | (and test (listp test) (setcdr test status)))) | |
565 | ||
566 | ;;; | |
567 | ;;; The action routines. | |
568 | ;;; | |
569 | ||
570 | (defun mailcap-possible-viewers (major minor) | |
c0393b5e | 571 | "Return a list of possible viewers from MAJOR for minor type MINOR." |
c113de23 GM |
572 | (let ((exact '()) |
573 | (wildcard '())) | |
574 | (while major | |
575 | (cond | |
576 | ((equal (car (car major)) minor) | |
577 | (setq exact (cons (cdr (car major)) exact))) | |
23f87bed | 578 | ((and minor (string-match (concat "^" (car (car major)) "$") minor)) |
c113de23 GM |
579 | (setq wildcard (cons (cdr (car major)) wildcard)))) |
580 | (setq major (cdr major))) | |
581 | (nconc exact wildcard))) | |
582 | ||
583 | (defun mailcap-unescape-mime-test (test type-info) | |
584 | (let (save-pos save-chr subst) | |
585 | (cond | |
586 | ((symbolp test) test) | |
587 | ((and (listp test) (symbolp (car test))) test) | |
588 | ((or (stringp test) | |
589 | (and (listp test) (stringp (car test)) | |
590 | (setq test (mapconcat 'identity test " ")))) | |
591 | (with-temp-buffer | |
592 | (insert test) | |
593 | (goto-char (point-min)) | |
594 | (while (not (eobp)) | |
595 | (skip-chars-forward "^%") | |
596 | (if (/= (- (point) | |
597 | (progn (skip-chars-backward "\\\\") | |
598 | (point))) | |
599 | 0) ; It is an escaped % | |
600 | (progn | |
601 | (delete-char 1) | |
602 | (skip-chars-forward "%.")) | |
603 | (setq save-pos (point)) | |
604 | (skip-chars-forward "%") | |
605 | (setq save-chr (char-after (point))) | |
c0393b5e DL |
606 | ;; Escapes: |
607 | ;; %s: name of a file for the body data | |
608 | ;; %t: content-type | |
609 | ;; %{<parameter name}: value of parameter in mailcap entry | |
610 | ;; %n: number of sub-parts for multipart content-type | |
611 | ;; %F: a set of content-type/filename pairs for multiparts | |
c113de23 GM |
612 | (cond |
613 | ((null save-chr) nil) | |
614 | ((= save-chr ?t) | |
615 | (delete-region save-pos (progn (forward-char 1) (point))) | |
616 | (insert (or (cdr (assq 'type type-info)) "\"\""))) | |
c0393b5e | 617 | ((memq save-chr '(?M ?n ?F)) |
c113de23 GM |
618 | (delete-region save-pos (progn (forward-char 1) (point))) |
619 | (insert "\"\"")) | |
620 | ((= save-chr ?{) | |
621 | (forward-char 1) | |
622 | (skip-chars-forward "^}") | |
623 | (downcase-region (+ 2 save-pos) (point)) | |
624 | (setq subst (buffer-substring (+ 2 save-pos) (point))) | |
625 | (delete-region save-pos (1+ (point))) | |
626 | (insert (or (cdr (assoc subst type-info)) "\"\""))) | |
627 | (t nil)))) | |
628 | (buffer-string))) | |
c0393b5e | 629 | (t (error "Bad value to mailcap-unescape-mime-test: %s" test))))) |
c113de23 GM |
630 | |
631 | (defvar mailcap-viewer-test-cache nil) | |
632 | ||
633 | (defun mailcap-viewer-passes-test (viewer-info type-info) | |
c0393b5e | 634 | "Return non-nil iff viewer specified by VIEWER-INFO passes its test clause. |
23f87bed | 635 | Also return non-nil if it has no test clause. TYPE-INFO is an argument |
c0393b5e | 636 | to supply to the test." |
c113de23 GM |
637 | (let* ((test-info (assq 'test viewer-info)) |
638 | (test (cdr test-info)) | |
639 | (otest test) | |
640 | (viewer (cdr (assoc 'viewer viewer-info))) | |
641 | (default-directory (expand-file-name "~/")) | |
642 | status parsed-test cache result) | |
46e8fe3d MB |
643 | (cond ((setq cache (assoc test mailcap-viewer-test-cache)) |
644 | (cadr cache)) | |
645 | ((not test-info) t) ; No test clause | |
646 | (t | |
647 | (setq | |
648 | result | |
649 | (cond | |
650 | ((not test) nil) ; Already failed test | |
651 | ((eq test t) t) ; Already passed test | |
652 | ((functionp test) ; Lisp function as test | |
653 | (funcall test type-info)) | |
654 | ((and (symbolp test) ; Lisp variable as test | |
655 | (boundp test)) | |
656 | (symbol-value test)) | |
657 | ((and (listp test) ; List to be eval'd | |
658 | (symbolp (car test))) | |
659 | (eval test)) | |
660 | (t | |
661 | (setq test (mailcap-unescape-mime-test test type-info) | |
662 | test (list shell-file-name nil nil nil | |
663 | shell-command-switch test) | |
664 | status (apply 'call-process test)) | |
665 | (eq 0 status)))) | |
666 | (push (list otest result) mailcap-viewer-test-cache) | |
667 | result)))) | |
c113de23 GM |
668 | |
669 | (defun mailcap-add-mailcap-entry (major minor info) | |
670 | (let ((old-major (assoc major mailcap-mime-data))) | |
671 | (if (null old-major) ; New major area | |
672 | (setq mailcap-mime-data | |
673 | (cons (cons major (list (cons minor info))) | |
674 | mailcap-mime-data)) | |
23f87bed MB |
675 | (let ((cur-minor (assoc minor old-major))) |
676 | (cond | |
677 | ((or (null cur-minor) ; New minor area, or | |
678 | (assq 'test info)) ; Has a test, insert at beginning | |
679 | (setcdr old-major (cons (cons minor info) (cdr old-major)))) | |
680 | ((and (not (assq 'test info)) ; No test info, replace completely | |
681 | (not (assq 'test cur-minor)) | |
c113de23 GM |
682 | (equal (assq 'viewer info) ; Keep alternative viewer |
683 | (assq 'viewer cur-minor))) | |
23f87bed MB |
684 | (setcdr cur-minor info)) |
685 | (t | |
686 | (setcdr old-major (cons (cons minor info) (cdr old-major)))))) | |
c113de23 GM |
687 | ))) |
688 | ||
689 | (defun mailcap-add (type viewer &optional test) | |
690 | "Add VIEWER as a handler for TYPE. | |
691 | If TEST is not given, it defaults to t." | |
692 | (let ((tl (split-string type "/"))) | |
693 | (when (or (not (car tl)) | |
694 | (not (cadr tl))) | |
695 | (error "%s is not a valid MIME type" type)) | |
696 | (mailcap-add-mailcap-entry | |
697 | (car tl) (cadr tl) | |
698 | `((viewer . ,viewer) | |
699 | (test . ,(if test test t)) | |
700 | (type . ,type))))) | |
701 | ||
702 | ;;; | |
703 | ;;; The main whabbo | |
704 | ;;; | |
705 | ||
706 | (defun mailcap-viewer-lessp (x y) | |
c0393b5e | 707 | "Return t iff viewer X is more desirable than viewer Y." |
c113de23 GM |
708 | (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) ""))) |
709 | (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) ""))) | |
710 | (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) "")))) | |
711 | (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) ""))))) | |
712 | (cond | |
713 | ((and x-wild (not y-wild)) | |
714 | nil) | |
715 | ((and (not x-wild) y-wild) | |
716 | t) | |
717 | ((and (not y-lisp) x-lisp) | |
718 | t) | |
719 | (t nil)))) | |
720 | ||
721 | (defun mailcap-mime-info (string &optional request) | |
722 | "Get the MIME viewer command for STRING, return nil if none found. | |
723 | Expects a complete content-type header line as its argument. | |
724 | ||
725 | Second argument REQUEST specifies what information to return. If it is | |
726 | nil or the empty string, the viewer (second field of the mailcap | |
727 | entry) will be returned. If it is a string, then the mailcap field | |
728 | corresponding to that string will be returned (print, description, | |
729 | whatever). If a number, then all the information for this specific | |
730 | viewer is returned. If `all', then all possible viewers for | |
731 | this type is returned." | |
732 | (let ( | |
733 | major ; Major encoding (text, etc) | |
734 | minor ; Minor encoding (html, etc) | |
735 | info ; Other info | |
736 | save-pos ; Misc. position during parse | |
737 | major-info ; (assoc major mailcap-mime-data) | |
738 | minor-info ; (assoc minor major-info) | |
739 | test ; current test proc. | |
740 | viewers ; Possible viewers | |
741 | passed ; Viewers that passed the test | |
742 | viewer ; The one and only viewer | |
743 | ctl) | |
744 | (save-excursion | |
745 | (setq ctl (mail-header-parse-content-type (or string "text/plain"))) | |
746 | (setq major (split-string (car ctl) "/")) | |
747 | (setq minor (cadr major) | |
748 | major (car major)) | |
749 | (when (setq major-info (cdr (assoc major mailcap-mime-data))) | |
750 | (when (setq viewers (mailcap-possible-viewers major-info minor)) | |
751 | (setq info (mapcar (lambda (a) (cons (symbol-name (car a)) | |
752 | (cdr a))) | |
753 | (cdr ctl))) | |
754 | (while viewers | |
755 | (if (mailcap-viewer-passes-test (car viewers) info) | |
756 | (setq passed (cons (car viewers) passed))) | |
757 | (setq viewers (cdr viewers))) | |
758 | (setq passed (sort passed 'mailcap-viewer-lessp)) | |
759 | (setq viewer (car passed)))) | |
760 | (when (and (stringp (cdr (assq 'viewer viewer))) | |
761 | passed) | |
762 | (setq viewer (car passed))) | |
763 | (cond | |
764 | ((and (null viewer) (not (equal major "default")) request) | |
765 | (mailcap-mime-info "default" request)) | |
766 | ((or (null request) (equal request "")) | |
767 | (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info)) | |
768 | ((stringp request) | |
23f87bed MB |
769 | (mailcap-unescape-mime-test |
770 | (cdr-safe (assoc request viewer)) info)) | |
c113de23 GM |
771 | ((eq request 'all) |
772 | passed) | |
773 | (t | |
774 | ;; MUST make a copy *sigh*, else we modify mailcap-mime-data | |
775 | (setq viewer (copy-sequence viewer)) | |
776 | (let ((view (assq 'viewer viewer)) | |
777 | (test (assq 'test viewer))) | |
778 | (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info))) | |
779 | (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info)))) | |
780 | viewer))))) | |
781 | ||
782 | ;;; | |
783 | ;;; Experimental MIME-types parsing | |
784 | ;;; | |
785 | ||
786 | (defvar mailcap-mime-extensions | |
c0393b5e DL |
787 | '(("" . "text/plain") |
788 | (".abs" . "audio/x-mpeg") | |
789 | (".aif" . "audio/aiff") | |
790 | (".aifc" . "audio/aiff") | |
791 | (".aiff" . "audio/aiff") | |
792 | (".ano" . "application/x-annotator") | |
793 | (".au" . "audio/ulaw") | |
794 | (".avi" . "video/x-msvideo") | |
795 | (".bcpio" . "application/x-bcpio") | |
796 | (".bin" . "application/octet-stream") | |
797 | (".cdf" . "application/x-netcdr") | |
798 | (".cpio" . "application/x-cpio") | |
799 | (".csh" . "application/x-csh") | |
800 | (".css" . "text/css") | |
801 | (".dvi" . "application/x-dvi") | |
802 | (".diff" . "text/x-patch") | |
803 | (".el" . "application/emacs-lisp") | |
804 | (".eps" . "application/postscript") | |
805 | (".etx" . "text/x-setext") | |
806 | (".exe" . "application/octet-stream") | |
807 | (".fax" . "image/x-fax") | |
808 | (".gif" . "image/gif") | |
809 | (".hdf" . "application/x-hdf") | |
810 | (".hqx" . "application/mac-binhex40") | |
811 | (".htm" . "text/html") | |
812 | (".html" . "text/html") | |
813 | (".icon" . "image/x-icon") | |
814 | (".ief" . "image/ief") | |
815 | (".jpg" . "image/jpeg") | |
816 | (".macp" . "image/x-macpaint") | |
817 | (".man" . "application/x-troff-man") | |
818 | (".me" . "application/x-troff-me") | |
819 | (".mif" . "application/mif") | |
820 | (".mov" . "video/quicktime") | |
821 | (".movie" . "video/x-sgi-movie") | |
822 | (".mp2" . "audio/x-mpeg") | |
823 | (".mp3" . "audio/x-mpeg") | |
824 | (".mp2a" . "audio/x-mpeg2") | |
825 | (".mpa" . "audio/x-mpeg") | |
826 | (".mpa2" . "audio/x-mpeg2") | |
827 | (".mpe" . "video/mpeg") | |
828 | (".mpeg" . "video/mpeg") | |
829 | (".mpega" . "audio/x-mpeg") | |
830 | (".mpegv" . "video/mpeg") | |
831 | (".mpg" . "video/mpeg") | |
832 | (".mpv" . "video/mpeg") | |
833 | (".ms" . "application/x-troff-ms") | |
834 | (".nc" . "application/x-netcdf") | |
835 | (".nc" . "application/x-netcdf") | |
836 | (".oda" . "application/oda") | |
837 | (".patch" . "text/x-patch") | |
838 | (".pbm" . "image/x-portable-bitmap") | |
839 | (".pdf" . "application/pdf") | |
840 | (".pgm" . "image/portable-graymap") | |
841 | (".pict" . "image/pict") | |
842 | (".png" . "image/png") | |
843 | (".pnm" . "image/x-portable-anymap") | |
844 | (".ppm" . "image/portable-pixmap") | |
845 | (".ps" . "application/postscript") | |
846 | (".qt" . "video/quicktime") | |
847 | (".ras" . "image/x-raster") | |
848 | (".rgb" . "image/x-rgb") | |
849 | (".rtf" . "application/rtf") | |
850 | (".rtx" . "text/richtext") | |
851 | (".sh" . "application/x-sh") | |
852 | (".sit" . "application/x-stuffit") | |
23f87bed | 853 | (".siv" . "application/sieve") |
c0393b5e DL |
854 | (".snd" . "audio/basic") |
855 | (".src" . "application/x-wais-source") | |
856 | (".tar" . "archive/tar") | |
857 | (".tcl" . "application/x-tcl") | |
858 | (".tex" . "application/x-tex") | |
859 | (".texi" . "application/texinfo") | |
860 | (".tga" . "image/x-targa") | |
861 | (".tif" . "image/tiff") | |
862 | (".tiff" . "image/tiff") | |
863 | (".tr" . "application/x-troff") | |
864 | (".troff" . "application/x-troff") | |
865 | (".tsv" . "text/tab-separated-values") | |
866 | (".txt" . "text/plain") | |
867 | (".vbs" . "video/mpeg") | |
868 | (".vox" . "audio/basic") | |
869 | (".vrml" . "x-world/x-vrml") | |
870 | (".wav" . "audio/x-wav") | |
23f87bed | 871 | (".xls" . "application/vnd.ms-excel") |
c0393b5e DL |
872 | (".wrl" . "x-world/x-vrml") |
873 | (".xbm" . "image/xbm") | |
874 | (".xpm" . "image/xpm") | |
875 | (".xwd" . "image/windowdump") | |
876 | (".zip" . "application/zip") | |
877 | (".ai" . "application/postscript") | |
878 | (".jpe" . "image/jpeg") | |
879 | (".jpeg" . "image/jpeg")) | |
880 | "An alist of file extensions and corresponding MIME content-types. | |
881 | This exists for you to customize the information in Lisp. It is | |
882 | merged with values from mailcap files by `mailcap-parse-mimetypes'.") | |
c113de23 GM |
883 | |
884 | (defvar mailcap-mimetypes-parsed-p nil) | |
885 | ||
886 | (defun mailcap-parse-mimetypes (&optional path force) | |
c0393b5e | 887 | "Parse out all the mimetypes specified in a Unix-style path string PATH. |
c113de23 GM |
888 | Components of PATH are separated by the `path-separator' character |
889 | appropriate for this system. If PATH is omitted, use the value of | |
890 | environment variable MIMETYPES if set; otherwise use a default path. | |
891 | If FORCE, re-parse even if already parsed." | |
892 | (interactive (list nil t)) | |
893 | (when (or (not mailcap-mimetypes-parsed-p) | |
894 | force) | |
895 | (cond | |
896 | (path nil) | |
897 | ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES"))) | |
23f87bed | 898 | ((memq system-type mailcap-poor-system-types) |
c113de23 GM |
899 | (setq path '("~/mime.typ" "~/etc/mime.typ"))) |
900 | (t (setq path | |
901 | ;; mime.types seems to be the normal name, definitely so | |
902 | ;; on current GNUish systems. The search order follows | |
903 | ;; that for mailcap. | |
904 | '("~/.mime.types" | |
905 | "/etc/mime.types" | |
906 | "/usr/etc/mime.types" | |
907 | "/usr/local/etc/mime.types" | |
908 | "/usr/local/www/conf/mime.types" | |
909 | "~/.mime-types" | |
910 | "/etc/mime-types" | |
911 | "/usr/etc/mime-types" | |
912 | "/usr/local/etc/mime-types" | |
913 | "/usr/local/www/conf/mime-types")))) | |
914 | (let ((fnames (reverse (if (stringp path) | |
9b198954 | 915 | (delete "" (split-string path path-separator)) |
c113de23 GM |
916 | path))) |
917 | fname) | |
918 | (while fnames | |
919 | (setq fname (car fnames)) | |
920 | (if (and (file-readable-p fname)) | |
921 | (mailcap-parse-mimetype-file fname)) | |
922 | (setq fnames (cdr fnames)))) | |
923 | (setq mailcap-mimetypes-parsed-p t))) | |
924 | ||
925 | (defun mailcap-parse-mimetype-file (fname) | |
c0393b5e | 926 | "Parse out a mime-types file FNAME." |
c113de23 GM |
927 | (let (type ; The MIME type for this line |
928 | extns ; The extensions for this line | |
929 | save-pos ; Misc. saved buffer positions | |
930 | ) | |
931 | (with-temp-buffer | |
932 | (insert-file-contents fname) | |
933 | (mailcap-replace-regexp "#.*" "") | |
934 | (mailcap-replace-regexp "\n+" "\n") | |
935 | (mailcap-replace-regexp "[ \t]+$" "") | |
936 | (goto-char (point-max)) | |
937 | (skip-chars-backward " \t\n") | |
938 | (delete-region (point) (point-max)) | |
939 | (goto-char (point-min)) | |
940 | (while (not (eobp)) | |
941 | (skip-chars-forward " \t\n") | |
942 | (setq save-pos (point)) | |
943 | (skip-chars-forward "^ \t\n") | |
944 | (downcase-region save-pos (point)) | |
945 | (setq type (buffer-substring save-pos (point))) | |
946 | (while (not (eolp)) | |
947 | (skip-chars-forward " \t") | |
948 | (setq save-pos (point)) | |
949 | (skip-chars-forward "^ \t\n") | |
950 | (setq extns (cons (buffer-substring save-pos (point)) extns))) | |
951 | (while extns | |
952 | (setq mailcap-mime-extensions | |
953 | (cons | |
954 | (cons (if (= (string-to-char (car extns)) ?.) | |
955 | (car extns) | |
956 | (concat "." (car extns))) type) | |
957 | mailcap-mime-extensions) | |
958 | extns (cdr extns))))))) | |
959 | ||
960 | (defun mailcap-extension-to-mime (extn) | |
961 | "Return the MIME content type of the file extensions EXTN." | |
962 | (mailcap-parse-mimetypes) | |
963 | (if (and (stringp extn) | |
964 | (not (eq (string-to-char extn) ?.))) | |
965 | (setq extn (concat "." extn))) | |
966 | (cdr (assoc (downcase extn) mailcap-mime-extensions))) | |
967 | ||
c0393b5e DL |
968 | ;; Unused? |
969 | (defalias 'mailcap-command-p 'executable-find) | |
c113de23 GM |
970 | |
971 | (defun mailcap-mime-types () | |
972 | "Return a list of MIME media types." | |
973 | (mailcap-parse-mimetypes) | |
9b198954 DL |
974 | (mm-delete-duplicates |
975 | (nconc | |
976 | (mapcar 'cdr mailcap-mime-extensions) | |
977 | (apply | |
978 | 'nconc | |
979 | (mapcar | |
980 | (lambda (l) | |
981 | (delq nil | |
982 | (mapcar | |
983 | (lambda (m) | |
984 | (let ((type (cdr (assq 'type (cdr m))))) | |
985 | (if (equal (cadr (split-string type "/")) | |
986 | "*") | |
987 | nil | |
988 | type))) | |
989 | (cdr l)))) | |
990 | mailcap-mime-data))))) | |
c113de23 GM |
991 | |
992 | (provide 'mailcap) | |
993 | ||
ab5796a9 | 994 | ;;; arch-tag: 1fd4f9c9-c305-4d2e-9747-3a4d45baa0bd |
c113de23 | 995 | ;;; mailcap.el ends here |