Mercurial > emacs
annotate lisp/gnus/mailcap.el @ 33853:eb4affa4abc6
(lface_from_face_name): Function comment fix.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Fri, 24 Nov 2000 13:17:42 +0000 |
parents | 61905a6a0029 |
children | 8b1375dbcbc6 |
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.") | |
31717 | 274 |
33821 | 275 (defcustom mailcap-download-directory nil |
276 "*Where downloaded files should go by default." | |
277 :type 'directory | |
278 :group 'mailcap) | |
31717 | 279 |
280 ;;; | |
281 ;;; Utility functions | |
282 ;;; | |
283 | |
284 (defun mailcap-save-binary-file () | |
285 (goto-char (point-min)) | |
286 (unwind-protect | |
287 (let ((file (read-file-name | |
288 "Filename to save as: " | |
289 (or mailcap-download-directory "~/"))) | |
290 (require-final-newline nil)) | |
291 (write-region (point-min) (point-max) file)) | |
292 (kill-buffer (current-buffer)))) | |
293 | |
294 (defvar mailcap-maybe-eval-warning | |
295 "*** WARNING *** | |
296 | |
33821 | 297 This MIME part contains untrusted and possibly harmful content. |
31717 | 298 If you evaluate the Emacs Lisp code contained in it, a lot of nasty |
299 things can happen. Please examine the code very carefully before you | |
300 instruct Emacs to evaluate it. You can browse the buffer containing | |
301 the code using \\[scroll-other-window]. | |
302 | |
303 If you are unsure what to do, please answer \"no\"." | |
304 "Text of warning message displayed by `mailcap-maybe-eval'. | |
305 Make sure that this text consists only of few text lines. Otherwise, | |
306 Gnus might fail to display all of it.") | |
307 | |
308 (defun mailcap-maybe-eval () | |
33821 | 309 "Maybe evaluate a buffer of Emacs Lisp code." |
31717 | 310 (let ((lisp-buffer (current-buffer))) |
311 (goto-char (point-min)) | |
312 (when | |
313 (save-window-excursion | |
314 (delete-other-windows) | |
315 (let ((buffer (get-buffer-create (generate-new-buffer-name | |
316 "*Warning*")))) | |
317 (unwind-protect | |
318 (with-current-buffer buffer | |
33821 | 319 (insert (substitute-command-keys |
31717 | 320 mailcap-maybe-eval-warning)) |
321 (goto-char (point-min)) | |
322 (display-buffer buffer) | |
323 (yes-or-no-p "This is potentially dangerous emacs-lisp code, evaluate it? ")) | |
324 (kill-buffer buffer)))) | |
325 (eval-buffer (current-buffer))) | |
326 (when (buffer-live-p lisp-buffer) | |
327 (with-current-buffer lisp-buffer | |
328 (emacs-lisp-mode))))) | |
329 | |
330 | |
331 ;;; | |
332 ;;; The mailcap parser | |
333 ;;; | |
334 | |
335 (defun mailcap-replace-regexp (regexp to-string) | |
336 ;; Quiet replace-regexp. | |
337 (goto-char (point-min)) | |
338 (while (re-search-forward regexp nil t) | |
339 (replace-match to-string t nil))) | |
340 | |
341 (defvar mailcap-parsed-p nil) | |
342 | |
343 (defun mailcap-parse-mailcaps (&optional path force) | |
344 "Parse out all the mailcaps specified in a path string PATH. | |
345 Components of PATH are separated by the `path-separator' character | |
346 appropriate for this system. If FORCE, re-parse even if already | |
347 parsed. If PATH is omitted, use the value of environment variable | |
348 MAILCAPS if set; otherwise (on Unix) use the path from RFC 1524, plus | |
349 /usr/local/etc/mailcap." | |
350 (interactive (list nil t)) | |
351 (when (or (not mailcap-parsed-p) | |
352 force) | |
353 (cond | |
354 (path nil) | |
355 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS"))) | |
356 ((memq system-type '(ms-dos ms-windows windows-nt)) | |
357 (setq path '("~/.mailcap" "~/mail.cap" "~/etc/mail.cap"))) | |
358 (t (setq path | |
359 ;; This is per RFC 1524, specifically | |
360 ;; with /usr before /usr/local. | |
361 '("~/.mailcap" "/etc/mailcap" "/usr/etc/mailcap" | |
362 "/usr/local/etc/mailcap")))) | |
363 (let ((fnames (reverse | |
364 (if (stringp path) | |
32989
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
365 (delete "" (split-string path path-separator)) |
31717 | 366 path))) |
367 fname) | |
368 (while fnames | |
369 (setq fname (car fnames)) | |
370 (if (and (file-readable-p fname) | |
371 (file-regular-p fname)) | |
372 (mailcap-parse-mailcap fname)) | |
373 (setq fnames (cdr fnames)))) | |
374 (setq mailcap-parsed-p t))) | |
375 | |
376 (defun mailcap-parse-mailcap (fname) | |
33821 | 377 "Parse out the mailcap file specified by FNAME." |
31717 | 378 (let (major ; The major mime type (image/audio/etc) |
379 minor ; The minor mime type (gif, basic, etc) | |
380 save-pos ; Misc saved positions used in parsing | |
381 viewer ; How to view this mime type | |
382 info ; Misc info about this mime type | |
383 ) | |
384 (with-temp-buffer | |
385 (insert-file-contents fname) | |
386 (set-syntax-table mailcap-parse-args-syntax-table) | |
387 (mailcap-replace-regexp "#.*" "") ; Remove all comments | |
388 (mailcap-replace-regexp "\\\\[ \t]*\n" " ") ; And collapse spaces | |
389 (mailcap-replace-regexp "\n+" "\n") ; And blank lines | |
390 (goto-char (point-max)) | |
391 (skip-chars-backward " \t\n") | |
392 (delete-region (point) (point-max)) | |
393 (while (not (bobp)) | |
394 (skip-chars-backward " \t\n") | |
395 (beginning-of-line) | |
396 (setq save-pos (point) | |
397 info nil) | |
398 (skip-chars-forward "^/; \t\n") | |
399 (downcase-region save-pos (point)) | |
400 (setq major (buffer-substring save-pos (point))) | |
401 (skip-chars-forward " \t") | |
402 (setq minor "") | |
403 (when (eq (char-after) ?/) | |
404 (forward-char) | |
405 (skip-chars-forward " \t") | |
406 (setq save-pos (point)) | |
407 (skip-chars-forward "^; \t\n") | |
408 (downcase-region save-pos (point)) | |
409 (setq minor | |
410 (cond | |
411 ((eq ?* (or (char-after save-pos) 0)) ".*") | |
412 ((= (point) save-pos) ".*") | |
413 (t (regexp-quote (buffer-substring save-pos (point))))))) | |
414 (skip-chars-forward " \t") | |
415 ;;; Got the major/minor chunks, now for the viewers/etc | |
416 ;;; The first item _must_ be a viewer, according to the | |
33821 | 417 ;;; RFC for mailcap files (#1524) |
31717 | 418 (setq viewer "") |
33821 | 419 (when (eq (char-after) ?\;) |
31717 | 420 (forward-char) |
421 (skip-chars-forward " \t") | |
422 (setq save-pos (point)) | |
423 (skip-chars-forward "^;\n") | |
424 ;; skip \; | |
425 (while (eq (char-before) ?\\) | |
426 (backward-delete-char 1) | |
427 (forward-char) | |
428 (skip-chars-forward "^;\n")) | |
429 (if (eq (or (char-after save-pos) 0) ?') | |
430 (setq viewer (progn | |
431 (narrow-to-region (1+ save-pos) (point)) | |
432 (goto-char (point-min)) | |
433 (prog1 | |
434 (read (current-buffer)) | |
435 (goto-char (point-max)) | |
436 (widen)))) | |
437 (setq viewer (buffer-substring save-pos (point))))) | |
438 (setq save-pos (point)) | |
439 (end-of-line) | |
33821 | 440 (unless (equal viewer "") |
31717 | 441 (setq info (nconc (list (cons 'viewer viewer) |
442 (cons 'type (concat major "/" | |
443 (if (string= minor ".*") | |
444 "*" minor)))) | |
445 (mailcap-parse-mailcap-extras save-pos (point)))) | |
446 (mailcap-mailcap-entry-passes-test info) | |
447 (mailcap-add-mailcap-entry major minor info)) | |
448 (beginning-of-line))))) | |
449 | |
450 (defun mailcap-parse-mailcap-extras (st nd) | |
33821 | 451 "Grab all the extra stuff from a mailcap entry." |
31717 | 452 (let ( |
453 name ; From name= | |
454 value ; its value | |
455 results ; Assoc list of results | |
456 name-pos ; Start of XXXX= position | |
457 val-pos ; Start of value position | |
458 done ; Found end of \'d ;s? | |
459 ) | |
460 (save-restriction | |
461 (narrow-to-region st nd) | |
462 (goto-char (point-min)) | |
463 (skip-chars-forward " \n\t;") | |
464 (while (not (eobp)) | |
465 (setq done nil) | |
466 (setq name-pos (point)) | |
467 (skip-chars-forward "^ \n\t=;") | |
468 (downcase-region name-pos (point)) | |
469 (setq name (buffer-substring name-pos (point))) | |
470 (skip-chars-forward " \t\n") | |
471 (if (not (eq (char-after (point)) ?=)) ; There is no value | |
472 (setq value t) | |
473 (skip-chars-forward " \t\n=") | |
474 (setq val-pos (point)) | |
475 (if (memq (char-after val-pos) '(?\" ?')) | |
476 (progn | |
477 (setq val-pos (1+ val-pos)) | |
478 (condition-case nil | |
479 (progn | |
480 (forward-sexp 1) | |
481 (backward-char 1)) | |
482 (error (goto-char (point-max))))) | |
483 (while (not done) | |
484 (skip-chars-forward "^;") | |
485 (if (eq (char-after (1- (point))) ?\\ ) | |
486 (progn | |
487 (subst-char-in-region (1- (point)) (point) ?\\ ? ) | |
488 (skip-chars-forward ";")) | |
489 (setq done t)))) | |
490 (setq value (buffer-substring val-pos (point)))) | |
491 (setq results (cons (cons name value) results)) | |
492 (skip-chars-forward " \";\n\t")) | |
493 results))) | |
494 | |
495 (defun mailcap-mailcap-entry-passes-test (info) | |
33821 | 496 "Return non-nil iff mailcap entry INFO passes its test clause. |
497 Also return non-nil if no test clause is present." | |
498 (let ((test (assq 'test info)) ; The test clause | |
499 status) | |
31717 | 500 (setq status (and test (split-string (cdr test) " "))) |
501 (if (and (or (assoc "needsterm" info) | |
502 (assoc "needsterminal" info) | |
503 (assoc "needsx11" info)) | |
504 (not (getenv "DISPLAY"))) | |
505 (setq status nil) | |
506 (cond | |
507 ((and (equal (nth 0 status) "test") | |
508 (equal (nth 1 status) "-n") | |
509 (or (equal (nth 2 status) "$DISPLAY") | |
510 (equal (nth 2 status) "\"$DISPLAY\""))) | |
511 (setq status (if (getenv "DISPLAY") t nil))) | |
512 ((and (equal (nth 0 status) "test") | |
513 (equal (nth 1 status) "-z") | |
514 (or (equal (nth 2 status) "$DISPLAY") | |
515 (equal (nth 2 status) "\"$DISPLAY\""))) | |
516 (setq status (if (getenv "DISPLAY") nil t))) | |
517 (test nil) | |
518 (t nil))) | |
519 (and test (listp test) (setcdr test status)))) | |
520 | |
521 ;;; | |
522 ;;; The action routines. | |
523 ;;; | |
524 | |
525 (defun mailcap-possible-viewers (major minor) | |
33821 | 526 "Return a list of possible viewers from MAJOR for minor type MINOR." |
31717 | 527 (let ((exact '()) |
528 (wildcard '())) | |
529 (while major | |
530 (cond | |
531 ((equal (car (car major)) minor) | |
532 (setq exact (cons (cdr (car major)) exact))) | |
533 ((and minor (string-match (car (car major)) minor)) | |
534 (setq wildcard (cons (cdr (car major)) wildcard)))) | |
535 (setq major (cdr major))) | |
536 (nconc exact wildcard))) | |
537 | |
538 (defun mailcap-unescape-mime-test (test type-info) | |
539 (let (save-pos save-chr subst) | |
540 (cond | |
541 ((symbolp test) test) | |
542 ((and (listp test) (symbolp (car test))) test) | |
543 ((or (stringp test) | |
544 (and (listp test) (stringp (car test)) | |
545 (setq test (mapconcat 'identity test " ")))) | |
546 (with-temp-buffer | |
547 (insert test) | |
548 (goto-char (point-min)) | |
549 (while (not (eobp)) | |
550 (skip-chars-forward "^%") | |
551 (if (/= (- (point) | |
552 (progn (skip-chars-backward "\\\\") | |
553 (point))) | |
554 0) ; It is an escaped % | |
555 (progn | |
556 (delete-char 1) | |
557 (skip-chars-forward "%.")) | |
558 (setq save-pos (point)) | |
559 (skip-chars-forward "%") | |
560 (setq save-chr (char-after (point))) | |
33821 | 561 ;; Escapes: |
562 ;; %s: name of a file for the body data | |
563 ;; %t: content-type | |
564 ;; %{<parameter name}: value of parameter in mailcap entry | |
565 ;; %n: number of sub-parts for multipart content-type | |
566 ;; %F: a set of content-type/filename pairs for multiparts | |
31717 | 567 (cond |
568 ((null save-chr) nil) | |
569 ((= save-chr ?t) | |
570 (delete-region save-pos (progn (forward-char 1) (point))) | |
571 (insert (or (cdr (assq 'type type-info)) "\"\""))) | |
33821 | 572 ((memq save-chr '(?M ?n ?F)) |
31717 | 573 (delete-region save-pos (progn (forward-char 1) (point))) |
574 (insert "\"\"")) | |
575 ((= save-chr ?{) | |
576 (forward-char 1) | |
577 (skip-chars-forward "^}") | |
578 (downcase-region (+ 2 save-pos) (point)) | |
579 (setq subst (buffer-substring (+ 2 save-pos) (point))) | |
580 (delete-region save-pos (1+ (point))) | |
581 (insert (or (cdr (assoc subst type-info)) "\"\""))) | |
582 (t nil)))) | |
583 (buffer-string))) | |
33821 | 584 (t (error "Bad value to mailcap-unescape-mime-test: %s" test))))) |
31717 | 585 |
586 (defvar mailcap-viewer-test-cache nil) | |
587 | |
588 (defun mailcap-viewer-passes-test (viewer-info type-info) | |
33821 | 589 "Return non-nil iff viewer specified by VIEWER-INFO passes its test clause. |
590 Also retun non-nil if it has no test clause. TYPE-INFO is an argument | |
591 to supply to the test." | |
31717 | 592 (let* ((test-info (assq 'test viewer-info)) |
593 (test (cdr test-info)) | |
594 (otest test) | |
595 (viewer (cdr (assoc 'viewer viewer-info))) | |
596 (default-directory (expand-file-name "~/")) | |
597 status parsed-test cache result) | |
598 (if (setq cache (assoc test mailcap-viewer-test-cache)) | |
599 (cadr cache) | |
600 (setq | |
601 result | |
602 (cond | |
603 ((not test-info) t) ; No test clause | |
604 ((not test) nil) ; Already failed test | |
605 ((eq test t) t) ; Already passed test | |
33821 | 606 ((functionp test) ; Lisp function as test |
31717 | 607 (funcall test type-info)) |
608 ((and (symbolp test) ; Lisp variable as test | |
609 (boundp test)) | |
610 (symbol-value test)) | |
611 ((and (listp test) ; List to be eval'd | |
612 (symbolp (car test))) | |
613 (eval test)) | |
614 (t | |
615 (setq test (mailcap-unescape-mime-test test type-info) | |
616 test (list shell-file-name nil nil nil | |
617 shell-command-switch test) | |
618 status (apply 'call-process test)) | |
619 (= 0 status)))) | |
620 (push (list otest result) mailcap-viewer-test-cache) | |
621 result))) | |
622 | |
623 (defun mailcap-add-mailcap-entry (major minor info) | |
624 (let ((old-major (assoc major mailcap-mime-data))) | |
625 (if (null old-major) ; New major area | |
626 (setq mailcap-mime-data | |
627 (cons (cons major (list (cons minor info))) | |
628 mailcap-mime-data)) | |
629 (let ((cur-minor (assoc minor old-major))) | |
630 (cond | |
631 ((or (null cur-minor) ; New minor area, or | |
632 (assq 'test info)) ; Has a test, insert at beginning | |
633 (setcdr old-major (cons (cons minor info) (cdr old-major)))) | |
634 ((and (not (assq 'test info)) ; No test info, replace completely | |
635 (not (assq 'test cur-minor)) | |
636 (equal (assq 'viewer info) ; Keep alternative viewer | |
637 (assq 'viewer cur-minor))) | |
638 (setcdr cur-minor info)) | |
639 (t | |
640 (setcdr old-major (cons (cons minor info) (cdr old-major)))))) | |
641 ))) | |
642 | |
643 (defun mailcap-add (type viewer &optional test) | |
644 "Add VIEWER as a handler for TYPE. | |
645 If TEST is not given, it defaults to t." | |
646 (let ((tl (split-string type "/"))) | |
647 (when (or (not (car tl)) | |
648 (not (cadr tl))) | |
649 (error "%s is not a valid MIME type" type)) | |
650 (mailcap-add-mailcap-entry | |
651 (car tl) (cadr tl) | |
652 `((viewer . ,viewer) | |
653 (test . ,(if test test t)) | |
654 (type . ,type))))) | |
655 | |
656 ;;; | |
657 ;;; The main whabbo | |
658 ;;; | |
659 | |
660 (defun mailcap-viewer-lessp (x y) | |
33821 | 661 "Return t iff viewer X is more desirable than viewer Y." |
31717 | 662 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assq 'type x)) ""))) |
663 (y-wild (string-match "[*?]" (or (cdr-safe (assq 'type y)) ""))) | |
664 (x-lisp (not (stringp (or (cdr-safe (assq 'viewer x)) "")))) | |
665 (y-lisp (not (stringp (or (cdr-safe (assq 'viewer y)) ""))))) | |
666 (cond | |
667 ((and x-wild (not y-wild)) | |
668 nil) | |
669 ((and (not x-wild) y-wild) | |
670 t) | |
671 ((and (not y-lisp) x-lisp) | |
672 t) | |
673 (t nil)))) | |
674 | |
675 (defun mailcap-mime-info (string &optional request) | |
676 "Get the MIME viewer command for STRING, return nil if none found. | |
677 Expects a complete content-type header line as its argument. | |
678 | |
679 Second argument REQUEST specifies what information to return. If it is | |
680 nil or the empty string, the viewer (second field of the mailcap | |
681 entry) will be returned. If it is a string, then the mailcap field | |
682 corresponding to that string will be returned (print, description, | |
683 whatever). If a number, then all the information for this specific | |
684 viewer is returned. If `all', then all possible viewers for | |
685 this type is returned." | |
686 (let ( | |
687 major ; Major encoding (text, etc) | |
688 minor ; Minor encoding (html, etc) | |
689 info ; Other info | |
690 save-pos ; Misc. position during parse | |
691 major-info ; (assoc major mailcap-mime-data) | |
692 minor-info ; (assoc minor major-info) | |
693 test ; current test proc. | |
694 viewers ; Possible viewers | |
695 passed ; Viewers that passed the test | |
696 viewer ; The one and only viewer | |
697 ctl) | |
698 (save-excursion | |
699 (setq ctl (mail-header-parse-content-type (or string "text/plain"))) | |
700 (setq major (split-string (car ctl) "/")) | |
701 (setq minor (cadr major) | |
702 major (car major)) | |
703 (when (setq major-info (cdr (assoc major mailcap-mime-data))) | |
704 (when (setq viewers (mailcap-possible-viewers major-info minor)) | |
705 (setq info (mapcar (lambda (a) (cons (symbol-name (car a)) | |
706 (cdr a))) | |
707 (cdr ctl))) | |
708 (while viewers | |
709 (if (mailcap-viewer-passes-test (car viewers) info) | |
710 (setq passed (cons (car viewers) passed))) | |
711 (setq viewers (cdr viewers))) | |
712 (setq passed (sort passed 'mailcap-viewer-lessp)) | |
713 (setq viewer (car passed)))) | |
714 (when (and (stringp (cdr (assq 'viewer viewer))) | |
715 passed) | |
716 (setq viewer (car passed))) | |
717 (cond | |
718 ((and (null viewer) (not (equal major "default")) request) | |
719 (mailcap-mime-info "default" request)) | |
720 ((or (null request) (equal request "")) | |
721 (mailcap-unescape-mime-test (cdr (assq 'viewer viewer)) info)) | |
722 ((stringp request) | |
723 (if (or (eq request 'test) (eq request 'viewer)) | |
724 (mailcap-unescape-mime-test | |
725 (cdr-safe (assoc request viewer)) info))) | |
726 ((eq request 'all) | |
727 passed) | |
728 (t | |
729 ;; MUST make a copy *sigh*, else we modify mailcap-mime-data | |
730 (setq viewer (copy-sequence viewer)) | |
731 (let ((view (assq 'viewer viewer)) | |
732 (test (assq 'test viewer))) | |
733 (if view (setcdr view (mailcap-unescape-mime-test (cdr view) info))) | |
734 (if test (setcdr test (mailcap-unescape-mime-test (cdr test) info)))) | |
735 viewer))))) | |
736 | |
737 ;;; | |
738 ;;; Experimental MIME-types parsing | |
739 ;;; | |
740 | |
741 (defvar mailcap-mime-extensions | |
33821 | 742 '(("" . "text/plain") |
743 (".abs" . "audio/x-mpeg") | |
744 (".aif" . "audio/aiff") | |
745 (".aifc" . "audio/aiff") | |
746 (".aiff" . "audio/aiff") | |
747 (".ano" . "application/x-annotator") | |
748 (".au" . "audio/ulaw") | |
749 (".avi" . "video/x-msvideo") | |
750 (".bcpio" . "application/x-bcpio") | |
751 (".bin" . "application/octet-stream") | |
752 (".cdf" . "application/x-netcdr") | |
753 (".cpio" . "application/x-cpio") | |
754 (".csh" . "application/x-csh") | |
755 (".css" . "text/css") | |
756 (".dvi" . "application/x-dvi") | |
757 (".diff" . "text/x-patch") | |
758 (".el" . "application/emacs-lisp") | |
759 (".eps" . "application/postscript") | |
760 (".etx" . "text/x-setext") | |
761 (".exe" . "application/octet-stream") | |
762 (".fax" . "image/x-fax") | |
763 (".gif" . "image/gif") | |
764 (".hdf" . "application/x-hdf") | |
765 (".hqx" . "application/mac-binhex40") | |
766 (".htm" . "text/html") | |
767 (".html" . "text/html") | |
768 (".icon" . "image/x-icon") | |
769 (".ief" . "image/ief") | |
770 (".jpg" . "image/jpeg") | |
771 (".macp" . "image/x-macpaint") | |
772 (".man" . "application/x-troff-man") | |
773 (".me" . "application/x-troff-me") | |
774 (".mif" . "application/mif") | |
775 (".mov" . "video/quicktime") | |
776 (".movie" . "video/x-sgi-movie") | |
777 (".mp2" . "audio/x-mpeg") | |
778 (".mp3" . "audio/x-mpeg") | |
779 (".mp2a" . "audio/x-mpeg2") | |
780 (".mpa" . "audio/x-mpeg") | |
781 (".mpa2" . "audio/x-mpeg2") | |
782 (".mpe" . "video/mpeg") | |
783 (".mpeg" . "video/mpeg") | |
784 (".mpega" . "audio/x-mpeg") | |
785 (".mpegv" . "video/mpeg") | |
786 (".mpg" . "video/mpeg") | |
787 (".mpv" . "video/mpeg") | |
788 (".ms" . "application/x-troff-ms") | |
789 (".nc" . "application/x-netcdf") | |
790 (".nc" . "application/x-netcdf") | |
791 (".oda" . "application/oda") | |
792 (".patch" . "text/x-patch") | |
793 (".pbm" . "image/x-portable-bitmap") | |
794 (".pdf" . "application/pdf") | |
795 (".pgm" . "image/portable-graymap") | |
796 (".pict" . "image/pict") | |
797 (".png" . "image/png") | |
798 (".pnm" . "image/x-portable-anymap") | |
799 (".ppm" . "image/portable-pixmap") | |
800 (".ps" . "application/postscript") | |
801 (".qt" . "video/quicktime") | |
802 (".ras" . "image/x-raster") | |
803 (".rgb" . "image/x-rgb") | |
804 (".rtf" . "application/rtf") | |
805 (".rtx" . "text/richtext") | |
806 (".sh" . "application/x-sh") | |
807 (".sit" . "application/x-stuffit") | |
808 (".snd" . "audio/basic") | |
809 (".src" . "application/x-wais-source") | |
810 (".tar" . "archive/tar") | |
811 (".tcl" . "application/x-tcl") | |
812 (".tex" . "application/x-tex") | |
813 (".texi" . "application/texinfo") | |
814 (".tga" . "image/x-targa") | |
815 (".tif" . "image/tiff") | |
816 (".tiff" . "image/tiff") | |
817 (".tr" . "application/x-troff") | |
818 (".troff" . "application/x-troff") | |
819 (".tsv" . "text/tab-separated-values") | |
820 (".txt" . "text/plain") | |
821 (".vbs" . "video/mpeg") | |
822 (".vox" . "audio/basic") | |
823 (".vrml" . "x-world/x-vrml") | |
824 (".wav" . "audio/x-wav") | |
825 (".wrl" . "x-world/x-vrml") | |
826 (".xbm" . "image/xbm") | |
827 (".xpm" . "image/xpm") | |
828 (".xwd" . "image/windowdump") | |
829 (".zip" . "application/zip") | |
830 (".ai" . "application/postscript") | |
831 (".jpe" . "image/jpeg") | |
832 (".jpeg" . "image/jpeg")) | |
833 "An alist of file extensions and corresponding MIME content-types. | |
834 This exists for you to customize the information in Lisp. It is | |
835 merged with values from mailcap files by `mailcap-parse-mimetypes'.") | |
31717 | 836 |
837 (defvar mailcap-mimetypes-parsed-p nil) | |
838 | |
839 (defun mailcap-parse-mimetypes (&optional path force) | |
33821 | 840 "Parse out all the mimetypes specified in a Unix-style path string PATH. |
31717 | 841 Components of PATH are separated by the `path-separator' character |
842 appropriate for this system. If PATH is omitted, use the value of | |
843 environment variable MIMETYPES if set; otherwise use a default path. | |
844 If FORCE, re-parse even if already parsed." | |
845 (interactive (list nil t)) | |
846 (when (or (not mailcap-mimetypes-parsed-p) | |
847 force) | |
848 (cond | |
849 (path nil) | |
850 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES"))) | |
851 ((memq system-type '(ms-dos ms-windows windows-nt)) | |
852 (setq path '("~/mime.typ" "~/etc/mime.typ"))) | |
853 (t (setq path | |
854 ;; mime.types seems to be the normal name, definitely so | |
855 ;; on current GNUish systems. The search order follows | |
856 ;; that for mailcap. | |
857 '("~/.mime.types" | |
858 "/etc/mime.types" | |
859 "/usr/etc/mime.types" | |
860 "/usr/local/etc/mime.types" | |
861 "/usr/local/www/conf/mime.types" | |
862 "~/.mime-types" | |
863 "/etc/mime-types" | |
864 "/usr/etc/mime-types" | |
865 "/usr/local/etc/mime-types" | |
866 "/usr/local/www/conf/mime-types")))) | |
867 (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
|
868 (delete "" (split-string path path-separator)) |
31717 | 869 path))) |
870 fname) | |
871 (while fnames | |
872 (setq fname (car fnames)) | |
873 (if (and (file-readable-p fname)) | |
874 (mailcap-parse-mimetype-file fname)) | |
875 (setq fnames (cdr fnames)))) | |
876 (setq mailcap-mimetypes-parsed-p t))) | |
877 | |
878 (defun mailcap-parse-mimetype-file (fname) | |
33821 | 879 "Parse out a mime-types file FNAME." |
31717 | 880 (let (type ; The MIME type for this line |
881 extns ; The extensions for this line | |
882 save-pos ; Misc. saved buffer positions | |
883 ) | |
884 (with-temp-buffer | |
885 (insert-file-contents fname) | |
886 (mailcap-replace-regexp "#.*" "") | |
887 (mailcap-replace-regexp "\n+" "\n") | |
888 (mailcap-replace-regexp "[ \t]+$" "") | |
889 (goto-char (point-max)) | |
890 (skip-chars-backward " \t\n") | |
891 (delete-region (point) (point-max)) | |
892 (goto-char (point-min)) | |
893 (while (not (eobp)) | |
894 (skip-chars-forward " \t\n") | |
895 (setq save-pos (point)) | |
896 (skip-chars-forward "^ \t\n") | |
897 (downcase-region save-pos (point)) | |
898 (setq type (buffer-substring save-pos (point))) | |
899 (while (not (eolp)) | |
900 (skip-chars-forward " \t") | |
901 (setq save-pos (point)) | |
902 (skip-chars-forward "^ \t\n") | |
903 (setq extns (cons (buffer-substring save-pos (point)) extns))) | |
904 (while extns | |
905 (setq mailcap-mime-extensions | |
906 (cons | |
907 (cons (if (= (string-to-char (car extns)) ?.) | |
908 (car extns) | |
909 (concat "." (car extns))) type) | |
910 mailcap-mime-extensions) | |
911 extns (cdr extns))))))) | |
912 | |
913 (defun mailcap-extension-to-mime (extn) | |
914 "Return the MIME content type of the file extensions EXTN." | |
915 (mailcap-parse-mimetypes) | |
916 (if (and (stringp extn) | |
917 (not (eq (string-to-char extn) ?.))) | |
918 (setq extn (concat "." extn))) | |
919 (cdr (assoc (downcase extn) mailcap-mime-extensions))) | |
920 | |
33821 | 921 ;; Unused? |
922 (defalias 'mailcap-command-p 'executable-find) | |
31717 | 923 |
924 (defun mailcap-mime-types () | |
925 "Return a list of MIME media types." | |
926 (mailcap-parse-mimetypes) | |
32989
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
927 (mm-delete-duplicates |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
928 (nconc |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
929 (mapcar 'cdr mailcap-mime-extensions) |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
930 (apply |
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 |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
933 (lambda (l) |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
934 (delq nil |
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 (m) |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
937 (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
|
938 (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
|
939 "*") |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
940 nil |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
941 type))) |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
942 (cdr l)))) |
74484f2d629a
2000-10-27 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
943 mailcap-mime-data))))) |
31717 | 944 |
945 (provide 'mailcap) | |
946 | |
947 ;;; mailcap.el ends here |