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