14
|
1 ;;; mm.el,v --- Mailcap parsing routines, and MIME handling
|
|
2 ;; Author: wmperry
|
|
3 ;; Created: 1996/05/28 02:46:51
|
|
4 ;; Version: 1.96
|
|
5 ;; Keywords: mail, news, hypermedia
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
8 ;;; Copyright (c) 1994, 1995, 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
16
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
14
|
10 ;;;
|
|
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
|
27 ;;;
|
|
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
29 ;;; Generalized mailcap parsing and access routines
|
|
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
31 ;;;
|
|
32 ;;; Data structures
|
|
33 ;;; ---------------
|
|
34 ;;; The mailcap structure is an assoc list of assoc lists.
|
|
35 ;;; 1st assoc list is keyed on the major content-type
|
|
36 ;;; 2nd assoc list is keyed on the minor content-type (which can be a regexp)
|
|
37 ;;;
|
|
38 ;;; Which looks like:
|
|
39 ;;; -----------------
|
|
40 ;;; (
|
|
41 ;;; ("application"
|
|
42 ;;; ("postscript" . <info>)
|
|
43 ;;; )
|
|
44 ;;; ("text"
|
|
45 ;;; ("plain" . <info>)
|
|
46 ;;; )
|
|
47 ;;; )
|
|
48 ;;;
|
|
49 ;;; Where <info> is another assoc list of the various information
|
|
50 ;;; related to the mailcap RFC. This is keyed on the lowercase
|
|
51 ;;; attribute name (viewer, test, etc). This looks like:
|
|
52 ;;; (("viewer" . viewerinfo)
|
|
53 ;;; ("test" . testinfo)
|
|
54 ;;; ("xxxx" . "string")
|
|
55 ;;; )
|
|
56 ;;;
|
|
57 ;;; Where viewerinfo specifies how the content-type is viewed. Can be
|
|
58 ;;; a string, in which case it is run through a shell, with
|
|
59 ;;; appropriate parameters, or a symbol, in which case the symbol is
|
|
60 ;;; funcall'd, with the buffer as an argument.
|
|
61 ;;;
|
|
62 ;;; testinfo is a list of strings, or nil. If nil, it means the
|
|
63 ;;; viewer specified is always valid. If it is a list of strings,
|
|
64 ;;; these are used to determine whether a viewer passes the 'test' or
|
|
65 ;;; not.
|
|
66 ;;;
|
|
67 ;;; The main interface to this code is:
|
|
68 ;;;
|
|
69 ;;; To set everything up:
|
|
70 ;;;
|
|
71 ;;; (mm-parse-mailcaps [path])
|
|
72 ;;;
|
|
73 ;;; Where PATH is a unix-style path specification (: separated list
|
|
74 ;;; of strings). If PATH is nil, the environment variable MAILCAPS
|
|
75 ;;; will be consulted. If there is no environment variable, then a
|
|
76 ;;; default list of paths is used.
|
|
77 ;;;
|
|
78 ;;; To retrieve the information:
|
|
79 ;;; (mm-mime-info st [nd] [request])
|
|
80 ;;;
|
|
81 ;;; Where st and nd are positions in a buffer that contain the
|
|
82 ;;; content-type header information of a mail/news/whatever message.
|
|
83 ;;; st can optionally be a string that contains the content-type
|
|
84 ;;; information.
|
|
85 ;;;
|
|
86 ;;; Third argument REQUEST specifies what information to return. If
|
|
87 ;;; it is nil or the empty string, the viewer (second field of the
|
|
88 ;;; mailcap entry) will be returned. If it is a string, then the
|
|
89 ;;; mailcap field corresponding to that string will be returned
|
|
90 ;;; (print, description, whatever). If a number, then all the
|
|
91 ;;; information for this specific viewer is returned.
|
|
92 ;;;
|
|
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
94 ;;; Variables, etc
|
|
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
96 (eval-and-compile
|
|
97 (require 'cl))
|
|
98
|
|
99 (defconst mm-version (let ((x "1.96"))
|
|
100 (if (string-match "Revision: \\([^ \t\n]+\\)" x)
|
|
101 (substring x (match-beginning 1) (match-end 1))
|
|
102 x))
|
|
103 "Version # of MM package")
|
|
104
|
|
105 (defvar mm-parse-args-syntax-table
|
|
106 (copy-syntax-table emacs-lisp-mode-syntax-table)
|
|
107 "A syntax table for parsing sgml attributes.")
|
|
108
|
|
109 (modify-syntax-entry ?' "\"" mm-parse-args-syntax-table)
|
|
110 (modify-syntax-entry ?` "\"" mm-parse-args-syntax-table)
|
|
111 (modify-syntax-entry ?{ "(" mm-parse-args-syntax-table)
|
|
112 (modify-syntax-entry ?} ")" mm-parse-args-syntax-table)
|
|
113
|
|
114 (defvar mm-mime-data
|
|
115 '(
|
|
116 ("multipart" . (
|
|
117 ("alternative". (("viewer" . mm-multipart-viewer)
|
|
118 ("type" . "multipart/alternative")))
|
|
119 ("mixed" . (("viewer" . mm-multipart-viewer)
|
|
120 ("type" . "multipart/mixed")))
|
|
121 (".*" . (("viewer" . mm-save-binary-file)
|
|
122 ("type" . "multipart/*")))
|
|
123 )
|
|
124 )
|
|
125 ("application" . (
|
44
|
126 ("x-x509-ca-cert" . (("viewer" . ssl-view-site-cert)
|
|
127 ("test" . (fboundp 'ssl-view-site-cert))
|
|
128 ("type" . "application/x-x509-ca-cert")))
|
|
129 ("x-x509-user-cert" . (("viewer" . ssl-view-user-cert)
|
|
130 ("test" . (fboundp 'ssl-view-user-cert))
|
|
131 ("type" . "application/x-x509-user-cert")))
|
14
|
132 ("octet-stream" . (("viewer" . mm-save-binary-file)
|
|
133 ("type" ."application/octet-stream")))
|
|
134 ("dvi" . (("viewer" . "open %s")
|
|
135 ("type" . "application/dvi")
|
|
136 ("test" . (eq (device-type) 'ns))))
|
|
137 ("dvi" . (("viewer" . "xdvi %s")
|
|
138 ("test" . (eq (device-type) 'x))
|
|
139 ("needsx11")
|
|
140 ("type" . "application/dvi")))
|
|
141 ("dvi" . (("viewer" . "dvitty %s")
|
|
142 ("test" . (not (getenv "DISPLAY")))
|
|
143 ("type" . "application/dvi")))
|
|
144 ("emacs-lisp" . (("viewer" . mm-maybe-eval)
|
|
145 ("type" . "application/emacs-lisp")))
|
|
146 ; ("x-tar" . (("viewer" . tar-mode)
|
|
147 ; ("test" . (fboundp 'tar-mode))
|
|
148 ; ("type" . "application/x-tar")))
|
|
149 ("x-tar" . (("viewer" . mm-save-binary-file)
|
|
150 ("type" . "application/x-tar")))
|
|
151 ("x-latex" . (("viewer" . tex-mode)
|
|
152 ("test" . (fboundp 'tex-mode))
|
|
153 ("type" . "application/x-latex")))
|
|
154 ("x-tex" . (("viewer" . tex-mode)
|
|
155 ("test" . (fboundp 'tex-mode))
|
|
156 ("type" . "application/x-tex")))
|
|
157 ("latex" . (("viewer" . tex-mode)
|
|
158 ("test" . (fboundp 'tex-mode))
|
|
159 ("type" . "application/latex")))
|
|
160 ("tex" . (("viewer" . tex-mode)
|
|
161 ("test" . (fboundp 'tex-mode))
|
|
162 ("type" . "application/tex")))
|
|
163 ("texinfo" . (("viewer" . texinfo-mode)
|
|
164 ("test" . (fboundp 'texinfo-mode))
|
|
165 ("type" . "application/tex")))
|
|
166 ("zip" . (("viewer" . mm-save-binary-file)
|
|
167 ("type" . "application/zip")
|
|
168 ("copiousoutput")))
|
|
169 ("pdf" . (("viewer" . "acroread %s")
|
|
170 ("type" . "application/pdf")))
|
|
171 ("postscript" . (("viewer" . "open %s")
|
|
172 ("type" . "application/postscript")
|
|
173 ("test" . (eq (device-type) 'ns))))
|
|
174 ("postscript" . (("viewer" . "ghostview %s")
|
|
175 ("type" . "application/postscript")
|
|
176 ("test" . (eq (device-type) 'x))
|
|
177 ("needsx11")))
|
|
178 ("postscript" . (("viewer" . "ps2ascii %s")
|
|
179 ("type" . "application/postscript")
|
|
180 ("test" . (not (getenv "DISPLAY")))
|
|
181 ("copiousoutput")))
|
|
182 ))
|
|
183 ("audio" . (
|
|
184 ("x-mpeg" . (("viewer" . "maplay %s")
|
|
185 ("type" . "audio/x-mpeg")))
|
|
186 (".*" . (("viewer" . mm-play-sound-file)
|
|
187 ("test" . (or (featurep 'nas-sound)
|
|
188 (featurep 'native-sound)))
|
|
189 ("type" . "audio/*")))
|
|
190 (".*" . (("viewer" . "showaudio")
|
|
191 ("type" . "audio/*")))
|
|
192 ))
|
|
193 ("message" . (
|
|
194 ("rfc-*822" . (("viewer" . vm-mode)
|
|
195 ("test" . (fboundp 'vm-mode))
|
|
196 ("type" . "message/rfc-822")))
|
|
197 ("rfc-*822" . (("viewer" . w3-mode)
|
|
198 ("test" . (fboundp 'w3-mode))
|
|
199 ("type" . "message/rfc-822")))
|
|
200 ("rfc-*822" . (("viewer" . view-mode)
|
|
201 ("test" . (fboundp 'view-mode))
|
|
202 ("type" . "message/rfc-822")))
|
|
203 ("rfc-*822" . (("viewer" . fundamental-mode)
|
|
204 ("type" . "message/rfc-822")))
|
|
205 ))
|
|
206 ("image" . (
|
|
207 ("x-xwd" . (("viewer" . "xwud -in %s")
|
|
208 ("type" . "image/x-xwd")
|
|
209 ("compose" . "xwd -frame > %s")
|
|
210 ("test" . (eq (device-type) 'x))
|
|
211 ("needsx11")))
|
|
212 ("x11-dump" . (("viewer" . "xwud -in %s")
|
|
213 ("type" . "image/x-xwd")
|
|
214 ("compose" . "xwd -frame > %s")
|
|
215 ("test" . (eq (device-type) 'x))
|
|
216 ("needsx11")))
|
|
217 ("windowdump" . (("viewer" . "xwud -in %s")
|
|
218 ("type" . "image/x-xwd")
|
|
219 ("compose" . "xwd -frame > %s")
|
|
220 ("test" . (eq (device-type) 'x))
|
|
221 ("needsx11")))
|
|
222 (".*" . (("viewer" . "open %s")
|
|
223 ("type" . "image/*")
|
|
224 ("test" . (eq (device-type) 'ns))))
|
|
225 (".*" . (("viewer" . "xv -perfect %s")
|
|
226 ("type" . "image/*")
|
|
227 ("test" . (eq (device-type) 'x))
|
|
228 ("needsx11")))
|
|
229 ))
|
|
230 ("text" . (
|
|
231 ("plain" . (("viewer" . w3-mode)
|
|
232 ("test" . (fboundp 'w3-mode))
|
|
233 ("type" . "text/plain")))
|
|
234 ("plain" . (("viewer" . view-mode)
|
|
235 ("test" . (fboundp 'view-mode))
|
|
236 ("type" . "text/plain")))
|
|
237 ("plain" . (("viewer" . fundamental-mode)
|
|
238 ("type" . "text/plain")))
|
|
239 ("enriched" . (("viewer" . enriched-decode-region)
|
|
240 ("test" . (fboundp
|
|
241 'enriched-decode-region))
|
|
242 ("type" . "text/enriched")))
|
|
243 ("html" . (("viewer" . w3-prepare-buffer)
|
|
244 ("test" . (fboundp 'w3-prepare-buffer))
|
|
245 ("type" . "text/html")))
|
|
246 ))
|
|
247 ("video" . (
|
|
248 ("mpeg" . (("viewer" . "mpeg_play %s")
|
|
249 ("type" . "video/mpeg")
|
|
250 ("test" . (eq (device-type) 'x))
|
|
251 ("needsx11")))
|
|
252 ))
|
|
253 ("x-world" . (
|
|
254 ("x-vrml" . (("viewer" . "webspace -remote %s -URL %u")
|
|
255 ("type" . "x-world/x-vrml")
|
|
256 ("description"
|
|
257 "VRML document")))))
|
|
258 ("archive" . (
|
|
259 ("tar" . (("viewer" . tar-mode)
|
|
260 ("type" . "archive/tar")
|
|
261 ("test" . (fboundp 'tar-mode))))
|
|
262 ))
|
|
263 )
|
|
264 "*The mailcap structure is an assoc list of assoc lists.
|
|
265 1st assoc list is keyed on the major content-type
|
|
266 2nd assoc list is keyed on the minor content-type (which can be a regexp)
|
|
267
|
|
268 Which looks like:
|
|
269 -----------------
|
|
270 (
|
|
271 (\"application\"
|
|
272 (\"postscript\" . <info>)
|
|
273 )
|
|
274 (\"text\"
|
|
275 (\"plain\" . <info>)
|
|
276 )
|
|
277 )
|
|
278
|
|
279 Where <info> is another assoc list of the various information
|
|
280 related to the mailcap RFC. This is keyed on the lowercase
|
|
281 attribute name (viewer, test, etc). This looks like:
|
|
282 ((\"viewer\" . viewerinfo)
|
|
283 (\"test\" . testinfo)
|
|
284 (\"xxxx\" . \"string\")
|
|
285 )
|
|
286
|
|
287 Where viewerinfo specifies how the content-type is viewed. Can be
|
|
288 a string, in which case it is run through a shell, with
|
|
289 appropriate parameters, or a symbol, in which case the symbol is
|
|
290 funcall'd, with the buffer as an argument.
|
|
291
|
|
292 testinfo is a list of strings, or nil. If nil, it means the
|
|
293 viewer specified is always valid. If it is a list of strings,
|
|
294 these are used to determine whether a viewer passes the 'test' or
|
|
295 not.")
|
|
296
|
|
297 (defvar mm-content-transfer-encodings
|
16
|
298 '(("base64" . base64-decode-region)
|
14
|
299 ("7bit" . ignore)
|
|
300 ("8bit" . ignore)
|
|
301 ("binary" . ignore)
|
|
302 ("x-compress" . ("uncompress" "-c"))
|
|
303 ("x-gzip" . ("gzip" "-dc"))
|
|
304 ("compress" . ("uncompress" "-c"))
|
|
305 ("gzip" . ("gzip" "-dc"))
|
|
306 ("x-hqx" . ("mcvert" "-P" "-s" "-S"))
|
|
307 ("quoted-printable" . mm-decode-quoted-printable)
|
|
308 )
|
|
309 "*An assoc list of content-transfer-encodings and how to decode them.")
|
|
310
|
|
311 (defvar mm-download-directory nil
|
|
312 "*Where downloaded files should go by default.")
|
|
313
|
36
|
314 (defvar mm-temporary-directory (or (getenv "TMPDIR") "/tmp")
|
14
|
315 "*Where temporary files go.")
|
|
316
|
|
317
|
|
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
319 ;;; A few things from w3 and url, just in case this is used without them
|
|
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
321
|
|
322 (defun mm-generate-unique-filename (&optional fmt)
|
|
323 "Generate a unique filename in mm-temporary-directory"
|
|
324 (if (not fmt)
|
|
325 (let ((base (format "mm-tmp.%d" (user-real-uid)))
|
|
326 (fname "")
|
|
327 (x 0))
|
|
328 (setq fname (format "%s%d" base x))
|
|
329 (while (file-exists-p
|
|
330 (expand-file-name fname mm-temporary-directory))
|
|
331 (setq x (1+ x)
|
|
332 fname (concat base (int-to-string x))))
|
|
333 (expand-file-name fname mm-temporary-directory))
|
|
334 (let ((base (concat "mm" (int-to-string (user-real-uid))))
|
|
335 (fname "")
|
|
336 (x 0))
|
|
337 (setq fname (format fmt (concat base (int-to-string x))))
|
|
338 (while (file-exists-p
|
|
339 (expand-file-name fname mm-temporary-directory))
|
|
340 (setq x (1+ x)
|
|
341 fname (format fmt (concat base (int-to-string x)))))
|
|
342 (expand-file-name fname mm-temporary-directory))))
|
|
343
|
|
344 (if (and (fboundp 'copy-tree)
|
|
345 (subrp (symbol-function 'copy-tree)))
|
|
346 (fset 'mm-copy-tree 'copy-tree)
|
|
347 (defun mm-copy-tree (tree)
|
|
348 (if (consp tree)
|
|
349 (cons (mm-copy-tree (car tree))
|
|
350 (mm-copy-tree (cdr tree)))
|
|
351 (if (vectorp tree)
|
|
352 (let* ((new (copy-sequence tree))
|
|
353 (i (1- (length new))))
|
|
354 (while (>= i 0)
|
|
355 (aset new i (mm-copy-tree (aref new i)))
|
|
356 (setq i (1- i)))
|
|
357 new)
|
|
358 tree))))
|
|
359
|
|
360 (require 'mule-sysdp)
|
|
361
|
|
362 (if (not (fboundp 'w3-save-binary-file))
|
|
363 (defun mm-save-binary-file ()
|
|
364 ;; Ok, this is truly fucked. In XEmacs, if you use the mouse to select
|
|
365 ;; a URL that gets saved via this function, read-file-name will pop up a
|
|
366 ;; dialog box for file selection. For some reason which buffer we are in
|
|
367 ;; gets royally screwed (even with save-excursions and the whole nine
|
|
368 ;; yards). SO, we just keep the old buffer name around and away we go.
|
|
369 (let ((old-buff (current-buffer))
|
|
370 (file (read-file-name "Filename to save as: "
|
|
371 (or mm-download-directory "~/")
|
|
372 (file-name-nondirectory (url-view-url t))
|
|
373 nil
|
|
374 (file-name-nondirectory (url-view-url t))))
|
|
375 (require-final-newline nil))
|
|
376 (set-buffer old-buff)
|
|
377 (mule-write-region-no-coding-system (point-min) (point-max) file)
|
|
378 (kill-buffer (current-buffer))))
|
|
379 (fset 'mm-save-binary-file 'w3-save-binary-file))
|
|
380
|
|
381 (defun mm-maybe-eval ()
|
|
382 "Maybe evaluate a buffer of emacs lisp code"
|
|
383 (if (yes-or-no-p "This is emacs-lisp code, evaluate it? ")
|
|
384 (eval-buffer (current-buffer))
|
|
385 (emacs-lisp-mode)))
|
|
386
|
|
387
|
|
388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
389 ;;; The mailcap parser
|
|
390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
391 (defun mm-viewer-unescape (format &optional filename url)
|
|
392 (save-excursion
|
|
393 (set-buffer (get-buffer-create " *mm-parse*"))
|
|
394 (erase-buffer)
|
|
395 (insert format)
|
|
396 (goto-char (point-min))
|
|
397 (while (re-search-forward "%\\(.\\)" nil t)
|
|
398 (let ((escape (aref (match-string 1) 0)))
|
|
399 (replace-match "" t t)
|
|
400 (case escape
|
|
401 (?% (insert "%"))
|
|
402 (?s (insert (or filename "\"\"")))
|
|
403 (?u (insert (or url "\"\""))))))
|
|
404 (buffer-string)))
|
|
405
|
|
406 (defun mm-in-assoc (elt list)
|
|
407 ;; Check to see if ELT matches any of the regexps in the car elements of LIST
|
|
408 (let (rslt)
|
|
409 (while (and list (not rslt))
|
|
410 (and (car (car list))
|
|
411 (string-match (car (car list)) elt)
|
|
412 (setq rslt (car list)))
|
|
413 (setq list (cdr list)))
|
|
414 rslt))
|
|
415
|
|
416 (defun mm-replace-regexp (regexp to-string)
|
|
417 ;; Quiet replace-regexp.
|
|
418 (goto-char (point-min))
|
|
419 (while (re-search-forward regexp nil t)
|
|
420 (replace-match to-string t nil)))
|
|
421
|
|
422 (defun mm-parse-mailcaps (&optional path)
|
|
423 ;; Parse out all the mailcaps specified in a unix-style path string PATH
|
|
424 (cond
|
|
425 (path nil)
|
|
426 ((getenv "MAILCAPS") (setq path (getenv "MAILCAPS")))
|
|
427 ((memq system-type '(ms-dos ms-windows windows-nt))
|
|
428 (setq path (mapconcat 'expand-file-name '("~/mail.cap" "~/etc/mail.cap")
|
|
429 ";")))
|
|
430 (t (setq path (concat "/etc/mailcap:/usr/etc/mailcap:"
|
|
431 "/usr/local/etc/mailcap:"
|
|
432 (expand-file-name "~/.mailcap")))))
|
|
433 (let ((fnames (mm-string-to-tokens path
|
|
434 (if (memq system-type
|
|
435 '(ms-dos ms-windows windows-nt))
|
|
436 ?;
|
|
437 ?:))) fname)
|
|
438 (while fnames
|
|
439 (setq fname (car fnames))
|
|
440 (if (and (file-exists-p fname) (file-readable-p fname))
|
|
441 (mm-parse-mailcap (car fnames)))
|
|
442 (setq fnames (cdr fnames)))))
|
|
443
|
|
444 (defun mm-parse-mailcap (fname)
|
|
445 ;; Parse out the mailcap file specified by FNAME
|
|
446 (let (major ; The major mime type (image/audio/etc)
|
|
447 minor ; The minor mime type (gif, basic, etc)
|
|
448 save-pos ; Misc saved positions used in parsing
|
|
449 viewer ; How to view this mime type
|
|
450 info ; Misc info about this mime type
|
|
451 )
|
|
452 (save-excursion
|
|
453 (set-buffer (get-buffer-create " *mailcap*"))
|
|
454 (erase-buffer)
|
|
455 (insert-file-contents fname)
|
|
456 (set-syntax-table mm-parse-args-syntax-table)
|
|
457 (mm-replace-regexp "#.*" "") ; Remove all comments
|
|
458 (mm-replace-regexp "\n+" "\n") ; And blank lines
|
|
459 (mm-replace-regexp "\\\\[ \t\n]+" " ") ; And collapse spaces
|
|
460 (mm-replace-regexp (concat (regexp-quote "\\") "[ \t]*\n") "")
|
|
461 (goto-char (point-max))
|
|
462 (skip-chars-backward " \t\n")
|
|
463 (delete-region (point) (point-max))
|
|
464 (goto-char (point-min))
|
|
465 (while (not (eobp))
|
|
466 (skip-chars-forward " \t\n")
|
|
467 (setq save-pos (point)
|
|
468 info nil)
|
|
469 (skip-chars-forward "^/;")
|
|
470 (downcase-region save-pos (point))
|
|
471 (setq major (buffer-substring save-pos (point)))
|
|
472 (skip-chars-forward "/ \t\n")
|
|
473 (setq save-pos (point))
|
|
474 (skip-chars-forward "^;")
|
|
475 (downcase-region save-pos (point))
|
|
476 (setq minor
|
|
477 (cond
|
|
478 ((= ?* (or (char-after save-pos) 0)) ".*")
|
|
479 ((= (point) save-pos) ".*")
|
|
480 (t (buffer-substring save-pos (point)))))
|
|
481 (skip-chars-forward "; \t\n")
|
|
482 ;;; Got the major/minor chunks, now for the viewers/etc
|
|
483 ;;; The first item _must_ be a viewer, according to the
|
|
484 ;;; RFC for mailcap files (#1343)
|
|
485 (skip-chars-forward "; \t\n")
|
|
486 (setq save-pos (point))
|
|
487 (skip-chars-forward "^;\n")
|
|
488 (if (= (or (char-after save-pos) 0) ?')
|
|
489 (setq viewer (progn
|
|
490 (narrow-to-region (1+ save-pos) (point))
|
|
491 (goto-char (point-min))
|
|
492 (prog1
|
|
493 (read (current-buffer))
|
|
494 (goto-char (point-max))
|
|
495 (widen))))
|
|
496 (setq viewer (buffer-substring save-pos (point))))
|
|
497 (setq save-pos (point))
|
|
498 (end-of-line)
|
|
499 (setq info (nconc (list (cons "viewer" viewer)
|
|
500 (cons "type" (concat major "/"
|
|
501 (if (string= minor ".*")
|
|
502 "*" minor))))
|
|
503 (mm-parse-mailcap-extras save-pos (point))))
|
|
504 (mm-mailcap-entry-passes-test info)
|
|
505 (mm-add-mailcap-entry major minor info)))))
|
|
506
|
|
507 (defun mm-parse-mailcap-extras (st nd)
|
|
508 ;; Grab all the extra stuff from a mailcap entry
|
|
509 (let (
|
|
510 name ; From name=
|
|
511 value ; its value
|
|
512 results ; Assoc list of results
|
|
513 name-pos ; Start of XXXX= position
|
|
514 val-pos ; Start of value position
|
|
515 done ; Found end of \'d ;s?
|
|
516 )
|
|
517 (save-restriction
|
|
518 (narrow-to-region st nd)
|
|
519 (goto-char (point-min))
|
|
520 (skip-chars-forward " \n\t;")
|
|
521 (while (not (eobp))
|
|
522 (setq done nil)
|
|
523 (skip-chars-forward " \";\n\t")
|
|
524 (setq name-pos (point))
|
|
525 (skip-chars-forward "^ \n\t=")
|
|
526 (downcase-region name-pos (point))
|
|
527 (setq name (buffer-substring name-pos (point)))
|
|
528 (skip-chars-forward " \t\n")
|
|
529 (if (/= (or (char-after (point)) 0) ?=) ; There is no value
|
|
530 (setq value nil)
|
|
531 (skip-chars-forward " \t\n=")
|
|
532 (setq val-pos (point))
|
|
533 (if (memq (char-after val-pos) '(?\" ?'))
|
|
534 (progn
|
|
535 (setq val-pos (1+ val-pos))
|
|
536 (condition-case nil
|
|
537 (progn
|
|
538 (forward-sexp 1)
|
|
539 (backward-char 1))
|
|
540 (error (goto-char (point-max)))))
|
|
541 (while (not done)
|
|
542 (skip-chars-forward "^;")
|
|
543 (if (= (or (char-after (1- (point))) 0) ?\\ )
|
|
544 (progn
|
|
545 (subst-char-in-region (1- (point)) (point) ?\\ ? )
|
|
546 (skip-chars-forward ";"))
|
|
547 (setq done t))))
|
|
548 (setq value (buffer-substring val-pos (point))))
|
|
549 (setq results (cons (cons name value) results)))
|
|
550 results)))
|
|
551
|
|
552 (defun mm-string-to-tokens (str &optional delim)
|
|
553 "Return a list of words from the string STR"
|
|
554 (setq delim (or delim ? ))
|
|
555 (let (results y)
|
|
556 (mapcar
|
|
557 (function
|
|
558 (lambda (x)
|
|
559 (cond
|
|
560 ((and (= x delim) y) (setq results (cons y results) y nil))
|
|
561 ((/= x delim) (setq y (concat y (char-to-string x))))
|
|
562 (t nil)))) str)
|
|
563 (nreverse (cons y results))))
|
|
564
|
|
565 (defun mm-mailcap-entry-passes-test (info)
|
|
566 ;; Return t iff a mailcap entry passes its test clause or no test
|
|
567 ;; clause is present.
|
|
568 (let (status ; Call-process-regions return value
|
|
569 (test (assoc "test" info)); The test clause
|
|
570 )
|
|
571 (setq status (and test (mm-string-to-tokens (cdr test))))
|
|
572 (if (and (assoc "needsx11" info) (not (getenv "DISPLAY")))
|
|
573 (setq status nil)
|
|
574 (cond
|
|
575 ((and (equal (nth 0 status) "test")
|
|
576 (equal (nth 1 status) "-n")
|
|
577 (or (equal (nth 2 status) "$DISPLAY")
|
|
578 (equal (nth 2 status) "\"$DISPLAY\"")))
|
|
579 (setq status (if (getenv "DISPLAY") t nil)))
|
|
580 ((and (equal (nth 0 status) "test")
|
|
581 (equal (nth 1 status) "-z")
|
|
582 (or (equal (nth 2 status) "$DISPLAY")
|
|
583 (equal (nth 2 status) "\"$DISPLAY\"")))
|
|
584 (setq status (if (getenv "DISPLAY") nil t)))
|
|
585 (test nil)
|
|
586 (t nil)))
|
|
587 (and test (listp test) (setcdr test status))))
|
|
588
|
|
589 (defun mm-parse-args (st &optional nd nodowncase)
|
|
590 ;; Return an assoc list of attribute/value pairs from an RFC822-type string
|
|
591 (let (
|
|
592 name ; From name=
|
|
593 value ; its value
|
|
594 results ; Assoc list of results
|
|
595 name-pos ; Start of XXXX= position
|
|
596 val-pos ; Start of value position
|
|
597 )
|
|
598 (save-excursion
|
|
599 (if (stringp st)
|
|
600 (progn
|
|
601 (set-buffer (get-buffer-create " *mm-temp*"))
|
|
602 (set-syntax-table mm-parse-args-syntax-table)
|
|
603 (erase-buffer)
|
|
604 (insert st)
|
|
605 (setq st (point-min)
|
|
606 nd (point-max)))
|
|
607 (set-syntax-table mm-parse-args-syntax-table))
|
|
608 (save-restriction
|
|
609 (narrow-to-region st nd)
|
|
610 (goto-char (point-min))
|
|
611 (while (not (eobp))
|
|
612 (skip-chars-forward "; \n\t")
|
|
613 (setq name-pos (point))
|
|
614 (skip-chars-forward "^ \n\t=;")
|
|
615 (if (not nodowncase)
|
|
616 (downcase-region name-pos (point)))
|
|
617 (setq name (buffer-substring name-pos (point)))
|
|
618 (skip-chars-forward " \t\n")
|
|
619 (if (/= (or (char-after (point)) 0) ?=) ; There is no value
|
|
620 (setq value nil)
|
|
621 (skip-chars-forward " \t\n=")
|
|
622 (setq val-pos (point)
|
|
623 value
|
|
624 (cond
|
|
625 ((or (= (or (char-after val-pos) 0) ?\")
|
|
626 (= (or (char-after val-pos) 0) ?'))
|
|
627 (buffer-substring (1+ val-pos)
|
|
628 (condition-case ()
|
|
629 (prog2
|
|
630 (forward-sexp 1)
|
|
631 (1- (point))
|
|
632 (skip-chars-forward "\""))
|
|
633 (error
|
|
634 (skip-chars-forward "^ \t\n")
|
|
635 (point)))))
|
|
636 (t
|
|
637 (buffer-substring val-pos
|
|
638 (progn
|
|
639 (skip-chars-forward "^;")
|
|
640 (skip-chars-backward " \t")
|
|
641 (point)))))))
|
|
642 (setq results (cons (cons name value) results))
|
|
643 (skip-chars-forward "; \n\t"))
|
|
644 results))))
|
|
645
|
|
646 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
647 ;;; The action routines.
|
|
648 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
649 (defun mm-possible-viewers (major minor)
|
|
650 ;; Return a list of possible viewers from MAJOR for minor type MINOR
|
|
651 (let ((exact '())
|
|
652 (wildcard '()))
|
|
653 (while major
|
|
654 (cond
|
|
655 ((equal (car (car major)) minor)
|
|
656 (setq exact (cons (cdr (car major)) exact)))
|
|
657 ((string-match (car (car major)) minor)
|
|
658 (setq wildcard (cons (cdr (car major)) wildcard))))
|
|
659 (setq major (cdr major)))
|
|
660 (nconc (nreverse exact) (nreverse wildcard))))
|
|
661
|
|
662 (defun mm-unescape-mime-test (test type-info)
|
|
663 (let ((buff (get-buffer-create " *unescape*"))
|
|
664 save-pos save-chr subst)
|
|
665 (cond
|
|
666 ((symbolp test) test)
|
|
667 ((and (listp test) (symbolp (car test))) test)
|
|
668 ((or (stringp test)
|
|
669 (and (listp test) (stringp (car test))
|
|
670 (setq test (mapconcat 'identity test " "))))
|
|
671 (save-excursion
|
|
672 (set-buffer buff)
|
|
673 (erase-buffer)
|
|
674 (insert test)
|
|
675 (goto-char (point-min))
|
|
676 (while (not (eobp))
|
|
677 (skip-chars-forward "^%")
|
|
678 (if (/= (- (point)
|
|
679 (progn (skip-chars-backward "\\\\")
|
|
680 (point)))
|
|
681 0) ; It is an escaped %
|
|
682 (progn
|
|
683 (delete-char 1)
|
|
684 (skip-chars-forward "%."))
|
|
685 (setq save-pos (point))
|
|
686 (skip-chars-forward "%")
|
|
687 (setq save-chr (char-after (point)))
|
|
688 (cond
|
|
689 ((null save-chr) nil)
|
|
690 ((= save-chr ?t)
|
|
691 (delete-region save-pos (progn (forward-char 1) (point)))
|
|
692 (insert (or (cdr (assoc "type" type-info)) "\"\"")))
|
|
693 ((= save-chr ?M)
|
|
694 (delete-region save-pos (progn (forward-char 1) (point)))
|
|
695 (insert "\"\""))
|
|
696 ((= save-chr ?n)
|
|
697 (delete-region save-pos (progn (forward-char 1) (point)))
|
|
698 (insert "\"\""))
|
|
699 ((= save-chr ?F)
|
|
700 (delete-region save-pos (progn (forward-char 1) (point)))
|
|
701 (insert "\"\""))
|
|
702 ((= save-chr ?{)
|
|
703 (forward-char 1)
|
|
704 (skip-chars-forward "^}")
|
|
705 (downcase-region (+ 2 save-pos) (point))
|
|
706 (setq subst (buffer-substring (+ 2 save-pos) (point)))
|
|
707 (delete-region save-pos (1+ (point)))
|
|
708 (insert (or (cdr (assoc subst type-info)) "\"\"")))
|
|
709 (t nil))))
|
|
710 (buffer-string)))
|
|
711 (t (error "Bad value to mm-unescape-mime-test. %s" test)))))
|
|
712
|
|
713 (defun mm-viewer-passes-test (viewer-info type-info)
|
|
714 ;; Return non-nil iff the viewer specified by VIEWER-INFO passes its
|
|
715 ;; test clause (if any).
|
|
716 (let* ((test-info (assoc "test" viewer-info))
|
|
717 (test (cdr test-info))
|
|
718 (viewer (cdr (assoc "viewer" viewer-info)))
|
|
719 status
|
|
720 parsed-test
|
|
721 )
|
|
722 (cond
|
|
723 ((not test-info) t) ; No test clause
|
|
724 ((not test) nil) ; Already failed test
|
|
725 ((eq test t) t) ; Already passed test
|
|
726 ((and (symbolp test) ; Lisp function as test
|
|
727 (fboundp test))
|
|
728 (funcall test type-info))
|
|
729 ((and (symbolp test) ; Lisp variable as test
|
|
730 (boundp test))
|
|
731 (symbol-value test))
|
|
732 ((and (listp test) ; List to be eval'd
|
|
733 (symbolp (car test)))
|
|
734 (eval test))
|
|
735 (t
|
|
736 (setq test (mm-unescape-mime-test test type-info)
|
32
|
737 test (list shell-file-name nil nil nil shell-command-switch test)
|
14
|
738 status (apply 'call-process test))
|
|
739 (= 0 status)))))
|
|
740
|
|
741 (defun mm-add-mailcap-entry (major minor info)
|
|
742 (let ((old-major (assoc major mm-mime-data)))
|
|
743 (if (null old-major) ; New major area
|
|
744 (setq mm-mime-data
|
|
745 (cons (cons major (list (cons minor info)))
|
|
746 mm-mime-data))
|
|
747 (let ((cur-minor (assoc minor old-major)))
|
|
748 (cond
|
|
749 ((or (null cur-minor) ; New minor area, or
|
|
750 (assoc "test" info)) ; Has a test, insert at beginning
|
|
751 (setcdr old-major (cons (cons minor info) (cdr old-major))))
|
|
752 ((and (not (assoc "test" info)); No test info, replace completely
|
|
753 (not (assoc "test" cur-minor)))
|
|
754 (setcdr cur-minor info))
|
|
755 (t
|
|
756 (setcdr old-major (cons (cons minor info) (cdr old-major)))))))))
|
|
757
|
|
758
|
|
759 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
760 ;;; The main whabbo
|
|
761 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
762 (defun mm-viewer-lessp (x y)
|
|
763 ;; Return t iff viewer X is more desirable than viewer Y
|
|
764 (let ((x-wild (string-match "[*?]" (or (cdr-safe (assoc "type" x)) "")))
|
|
765 (y-wild (string-match "[*?]" (or (cdr-safe (assoc "type" y)) "")))
|
|
766 (x-lisp (not (stringp (or (cdr-safe (assoc "viewer" x)) ""))))
|
|
767 (y-lisp (not (stringp (or (cdr-safe (assoc "viewer" y)) "")))))
|
|
768 (cond
|
|
769 ((and x-lisp (not y-lisp))
|
|
770 t)
|
|
771 ((and (not y-lisp) x-wild (not y-wild))
|
|
772 t)
|
|
773 ((and (not x-wild) y-wild)
|
|
774 t)
|
|
775 (t nil))))
|
|
776
|
|
777 (defun mm-mime-info (st &optional nd request)
|
|
778 "Get the mime viewer command for HEADERLINE, return nil if none found.
|
|
779 Expects a complete content-type header line as its argument. This can
|
|
780 be simple like text/html, or complex like text/plain; charset=blah; foo=bar
|
|
781
|
|
782 Third argument REQUEST specifies what information to return. If it is
|
|
783 nil or the empty string, the viewer (second field of the mailcap
|
|
784 entry) will be returned. If it is a string, then the mailcap field
|
|
785 corresponding to that string will be returned (print, description,
|
|
786 whatever). If a number, then all the information for this specific
|
|
787 viewer is returned."
|
|
788 (let (
|
|
789 major ; Major encoding (text, etc)
|
|
790 minor ; Minor encoding (html, etc)
|
|
791 info ; Other info
|
|
792 save-pos ; Misc. position during parse
|
|
793 major-info ; (assoc major mm-mime-data)
|
|
794 minor-info ; (assoc minor major-info)
|
|
795 test ; current test proc.
|
|
796 viewers ; Possible viewers
|
|
797 passed ; Viewers that passed the test
|
|
798 viewer ; The one and only viewer
|
|
799 )
|
|
800 (save-excursion
|
|
801 (cond
|
|
802 ((null st)
|
|
803 (set-buffer (get-buffer-create " *mimeparse*"))
|
|
804 (erase-buffer)
|
|
805 (insert "text/plain")
|
|
806 (setq st (point-min)))
|
|
807 ((stringp st)
|
|
808 (set-buffer (get-buffer-create " *mimeparse*"))
|
|
809 (erase-buffer)
|
|
810 (insert st)
|
|
811 (setq st (point-min)))
|
|
812 ((null nd)
|
|
813 (narrow-to-region st (progn (goto-char st) (end-of-line) (point))))
|
|
814 (t (narrow-to-region st nd)))
|
|
815 (goto-char st)
|
|
816 (skip-chars-forward ": \t\n")
|
|
817 (buffer-enable-undo)
|
|
818 (setq viewer
|
|
819 (catch 'mm-exit
|
|
820 (setq save-pos (point))
|
|
821 (skip-chars-forward "^/")
|
|
822 (downcase-region save-pos (point))
|
|
823 (setq major (buffer-substring save-pos (point)))
|
|
824 (if (not (setq major-info (cdr (assoc major mm-mime-data))))
|
|
825 (throw 'mm-exit nil))
|
|
826 (skip-chars-forward "/ \t\n")
|
|
827 (setq save-pos (point))
|
|
828 (skip-chars-forward "^ \t\n;")
|
|
829 (downcase-region save-pos (point))
|
|
830 (setq minor (buffer-substring save-pos (point)))
|
|
831 (if (not
|
|
832 (setq viewers (mm-possible-viewers major-info minor)))
|
|
833 (throw 'mm-exit nil))
|
|
834 (skip-chars-forward "; \t")
|
|
835 (if (eolp)
|
|
836 nil ; No qualifiers
|
|
837 (setq save-pos (point))
|
|
838 (end-of-line)
|
|
839 (setq info (mm-parse-args save-pos (point)))
|
|
840 )
|
|
841 (while viewers
|
|
842 (if (mm-viewer-passes-test (car viewers) info)
|
|
843 (setq passed (cons (car viewers) passed)))
|
|
844 (setq viewers (cdr viewers)))
|
|
845 (setq passed (sort (nreverse passed) 'mm-viewer-lessp))
|
|
846 (car passed)))
|
|
847 (if (and (stringp (cdr (assoc "viewer" viewer)))
|
|
848 passed)
|
|
849 (setq viewer (car passed)))
|
|
850 (widen)
|
|
851 (cond
|
|
852 ((and (null viewer) (not (equal major "default")))
|
|
853 (mm-mime-info "default" nil request))
|
|
854 ((or (null request) (equal request ""))
|
|
855 (mm-unescape-mime-test (cdr (assoc "viewer" viewer)) info))
|
|
856 ((stringp request)
|
|
857 (if (or (string= request "test") (string= request "viewer"))
|
|
858 (mm-unescape-mime-test (cdr-safe (assoc request viewer)) info)))
|
|
859 (t
|
|
860 ;; MUST make a copy *sigh*, else we modify mm-mime-data
|
|
861 (setq viewer (mm-copy-tree viewer))
|
|
862 (let ((view (assoc "viewer" viewer))
|
|
863 (test (assoc "test" viewer)))
|
|
864 (if view (setcdr view (mm-unescape-mime-test (cdr view) info)))
|
|
865 (if test (setcdr test (mm-unescape-mime-test (cdr test) info))))
|
|
866 viewer)))))
|
|
867
|
|
868
|
|
869 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
870 ;;; Experimental MIME-types parsing
|
|
871 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
872 (defvar mm-mime-extensions
|
|
873 '(
|
|
874 ("" . "text/plain")
|
|
875 (".abs" . "audio/x-mpeg")
|
|
876 (".aif" . "audio/aiff")
|
|
877 (".aifc" . "audio/aiff")
|
|
878 (".aiff" . "audio/aiff")
|
|
879 (".ano" . "application/x-annotator")
|
|
880 (".au" . "audio/ulaw")
|
|
881 (".avi" . "video/x-msvideo")
|
|
882 (".bcpio" . "application/x-bcpio")
|
|
883 (".bin" . "application/octet-stream")
|
|
884 (".cdf" . "application/x-netcdr")
|
|
885 (".cpio" . "application/x-cpio")
|
|
886 (".csh" . "application/x-csh")
|
|
887 (".dvi" . "application/x-dvi")
|
|
888 (".el" . "application/emacs-lisp")
|
|
889 (".eps" . "application/postscript")
|
|
890 (".etx" . "text/x-setext")
|
|
891 (".exe" . "application/octet-stream")
|
|
892 (".fax" . "image/x-fax")
|
|
893 (".gif" . "image/gif")
|
|
894 (".hdf" . "application/x-hdf")
|
|
895 (".hqx" . "application/mac-binhex40")
|
|
896 (".htm" . "text/html")
|
|
897 (".html" . "text/html")
|
|
898 (".icon" . "image/x-icon")
|
|
899 (".ief" . "image/ief")
|
|
900 (".jpg" . "image/jpeg")
|
|
901 (".macp" . "image/x-macpaint")
|
|
902 (".man" . "application/x-troff-man")
|
|
903 (".me" . "application/x-troff-me")
|
|
904 (".mif" . "application/mif")
|
|
905 (".mov" . "video/quicktime")
|
|
906 (".movie" . "video/x-sgi-movie")
|
|
907 (".mp2" . "audio/x-mpeg")
|
|
908 (".mp2a" . "audio/x-mpeg2")
|
|
909 (".mpa" . "audio/x-mpeg")
|
|
910 (".mpa2" . "audio/x-mpeg2")
|
|
911 (".mpe" . "video/mpeg")
|
|
912 (".mpeg" . "video/mpeg")
|
|
913 (".mpega" . "audio/x-mpeg")
|
|
914 (".mpegv" . "video/mpeg")
|
|
915 (".mpg" . "video/mpeg")
|
|
916 (".mpv" . "video/mpeg")
|
|
917 (".ms" . "application/x-troff-ms")
|
|
918 (".nc" . "application/x-netcdf")
|
|
919 (".nc" . "application/x-netcdf")
|
|
920 (".oda" . "application/oda")
|
|
921 (".pbm" . "image/x-portable-bitmap")
|
|
922 (".pdf" . "application/pdf")
|
|
923 (".pgm" . "image/portable-graymap")
|
|
924 (".pict" . "image/pict")
|
16
|
925 (".png" . "image/png")
|
14
|
926 (".pnm" . "image/x-portable-anymap")
|
|
927 (".ppm" . "image/portable-pixmap")
|
|
928 (".ps" . "application/postscript")
|
|
929 (".qt" . "video/quicktime")
|
|
930 (".ras" . "image/x-raster")
|
|
931 (".rgb" . "image/x-rgb")
|
|
932 (".rtf" . "application/rtf")
|
|
933 (".rtx" . "text/richtext")
|
|
934 (".sh" . "application/x-sh")
|
|
935 (".sit" . "application/x-stuffit")
|
|
936 (".snd" . "audio/basic")
|
|
937 (".src" . "application/x-wais-source")
|
|
938 (".tar" . "archive/tar")
|
|
939 (".tcl" . "application/x-tcl")
|
|
940 (".tcl" . "application/x-tcl")
|
|
941 (".tex" . "application/x-tex")
|
|
942 (".texi" . "application/texinfo")
|
|
943 (".tga" . "image/x-targa")
|
|
944 (".tif" . "image/tiff")
|
|
945 (".tiff" . "image/tiff")
|
|
946 (".tr" . "application/x-troff")
|
|
947 (".troff" . "application/x-troff")
|
|
948 (".tsv" . "text/tab-separated-values")
|
|
949 (".txt" . "text/plain")
|
|
950 (".vbs" . "video/mpeg")
|
|
951 (".vox" . "audio/basic")
|
|
952 (".vrml" . "x-world/x-vrml")
|
|
953 (".wav" . "audio/x-wav")
|
|
954 (".wrl" . "x-world/x-vrml")
|
|
955 (".xbm" . "image/xbm")
|
|
956 (".xpm" . "image/x-pixmap")
|
|
957 (".xwd" . "image/windowdump")
|
|
958 (".zip" . "application/zip")
|
|
959 (".ai" . "application/postscript")
|
|
960 (".jpe" . "image/jpeg")
|
|
961 (".jpeg" . "image/jpeg")
|
|
962 )
|
|
963 "*An assoc list of file extensions and the MIME content-types they
|
|
964 correspond to.")
|
|
965
|
|
966 (defun mm-parse-mimetypes (&optional path)
|
|
967 ;; Parse out all the mimetypes specified in a unix-style path string PATH
|
|
968 (cond
|
|
969 (path nil)
|
|
970 ((getenv "MIMETYPES") (setq path (getenv "MIMETYPES")))
|
|
971 ((memq system-type '(ms-dos ms-windows windows-nt))
|
|
972 (setq path (mapconcat 'expand-file-name
|
|
973 '("~/mime.typ" "~/etc/mime.typ") ";")))
|
|
974 (t (setq path (concat (expand-file-name "~/.mime-types") ":"
|
|
975 "/etc/mime-types:/usr/etc/mime-types:"
|
|
976 "/usr/local/etc/mime-types:"
|
|
977 "/usr/local/www/conf/mime-types"))))
|
|
978 (let ((fnames (mm-string-to-tokens path
|
|
979 (if (memq system-type
|
|
980 '(ms-dos ms-windows windows-nt))
|
|
981 ?;
|
|
982 ?:))) fname)
|
|
983 (while fnames
|
|
984 (setq fname (car fnames))
|
|
985 (if (and (file-exists-p fname) (file-readable-p fname))
|
|
986 (mm-parse-mimetype-file (car fnames)))
|
|
987 (setq fnames (cdr fnames)))))
|
|
988
|
|
989 (defun mm-parse-mimetype-file (fname)
|
|
990 ;; Parse out a mime-types file
|
|
991 (let (type ; The MIME type for this line
|
|
992 extns ; The extensions for this line
|
|
993 save-pos ; Misc. saved buffer positions
|
|
994 )
|
|
995 (save-excursion
|
|
996 (set-buffer (get-buffer-create " *mime-types*"))
|
|
997 (erase-buffer)
|
|
998 (insert-file-contents fname)
|
|
999 (mm-replace-regexp "#.*" "")
|
|
1000 (mm-replace-regexp "\n+" "\n")
|
|
1001 (mm-replace-regexp "[ \t]+$" "")
|
|
1002 (goto-char (point-max))
|
|
1003 (skip-chars-backward " \t\n")
|
|
1004 (delete-region (point) (point-max))
|
|
1005 (goto-char (point-min))
|
|
1006 (while (not (eobp))
|
|
1007 (skip-chars-forward " \t\n")
|
|
1008 (setq save-pos (point))
|
|
1009 (skip-chars-forward "^ \t")
|
|
1010 (downcase-region save-pos (point))
|
|
1011 (setq type (buffer-substring save-pos (point)))
|
|
1012 (while (not (eolp))
|
|
1013 (skip-chars-forward " \t")
|
|
1014 (setq save-pos (point))
|
|
1015 (skip-chars-forward "^ \t\n")
|
|
1016 (setq extns (cons (buffer-substring save-pos (point)) extns)))
|
|
1017 (while extns
|
|
1018 (setq mm-mime-extensions
|
|
1019 (cons
|
|
1020 (cons (if (= (string-to-char (car extns)) ?.)
|
|
1021 (car extns)
|
|
1022 (concat "." (car extns))) type) mm-mime-extensions)
|
|
1023 extns (cdr extns)))))))
|
|
1024
|
|
1025 (defun mm-extension-to-mime (extn)
|
|
1026 "Return the MIME content type of the file extensions EXTN"
|
|
1027 (if (and (stringp extn)
|
|
1028 (not (= (string-to-char extn) ?.)))
|
|
1029 (setq extn (concat "." extn)))
|
|
1030 (cdr (assoc (downcase extn) mm-mime-extensions)))
|
|
1031
|
|
1032
|
|
1033 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1034 ;;; Editing/Composition of body parts
|
|
1035 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1036 (defun mm-compose-type (type)
|
|
1037 ;; Compose a body section of MIME-type TYPE.
|
|
1038 (let* ((info (mm-mime-info type nil 5))
|
|
1039 (fnam (mm-generate-unique-filename))
|
|
1040 (comp (or (cdr (assoc "compose" info))))
|
|
1041 (ctyp (cdr (assoc "composetyped" info)))
|
|
1042 (buff (get-buffer-create " *mimecompose*"))
|
|
1043 (typeit (not ctyp))
|
|
1044 (retval "")
|
|
1045 (usef nil))
|
|
1046 (setq comp (mm-unescape-mime-test (or comp ctyp) info))
|
|
1047 (while (string-match "\\([^\\\\]\\)%s" comp)
|
|
1048 (setq comp (concat (substring comp 0 (match-end 1)) fnam
|
|
1049 (substring comp (match-end 0) nil))
|
|
1050 usef t))
|
32
|
1051 (call-process shell-file-name nil
|
|
1052 (if usef nil buff)
|
|
1053 nil shell-command-switch comp)
|
14
|
1054 (setq retval
|
|
1055 (concat
|
|
1056 (if typeit (concat "Content-type: " type "\r\n\r\n") "")
|
|
1057 (if usef
|
|
1058 (save-excursion
|
|
1059 (set-buffer buff)
|
|
1060 (erase-buffer)
|
|
1061 (insert-file-contents fnam)
|
|
1062 (buffer-string))
|
|
1063 (save-excursion
|
|
1064 (set-buffer buff)
|
|
1065 (buffer-string)))
|
|
1066 "\r\n"))
|
|
1067 retval))
|
|
1068
|
|
1069 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1070 ;;; Misc.
|
|
1071 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1072 (defun mm-type-to-file (type)
|
|
1073 "Return the file extension for content-type TYPE"
|
|
1074 (rassoc type mm-mime-extensions))
|
|
1075
|
|
1076
|
|
1077 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1078 ;;; Miscellaneous MIME viewers written in elisp
|
|
1079 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1080 (defun mm-play-sound-file (&optional buff)
|
|
1081 "Play a sound file in buffer BUFF (defaults to current buffer)"
|
|
1082 (setq buff (or buff (current-buffer)))
|
|
1083 (let ((fname (mm-generate-unique-filename "%s.au"))
|
|
1084 (synchronous-sounds t)) ; Play synchronously
|
|
1085 (mm-write-region-no-coding-system (point-min) (point-max) fname)
|
|
1086 (kill-buffer (current-buffer))
|
|
1087 (play-sound-file fname)
|
|
1088 (condition-case ()
|
|
1089 (delete-file fname)
|
|
1090 (error nil))))
|
|
1091
|
|
1092 (defun mm-parse-mime-headers (&optional no-delete)
|
|
1093 "Return a list of the MIME headers at the top of this buffer. If
|
|
1094 optional argument NO-DELETE is non-nil, don't delete the headers."
|
|
1095 (let* ((st (point-min))
|
|
1096 (nd (progn
|
|
1097 (goto-char (point-min))
|
|
1098 (skip-chars-forward " \t\n")
|
|
1099 (if (re-search-forward "^\r*$" nil t)
|
|
1100 (1+ (point))
|
|
1101 (point-max))))
|
|
1102 save-pos
|
|
1103 status
|
|
1104 hname
|
|
1105 hvalu
|
|
1106 result
|
|
1107 )
|
|
1108 (narrow-to-region st nd)
|
|
1109 (goto-char (point-min))
|
|
1110 (while (not (eobp))
|
|
1111 (skip-chars-forward " \t\n\r")
|
|
1112 (setq save-pos (point))
|
|
1113 (skip-chars-forward "^:\n\r")
|
|
1114 (downcase-region save-pos (point))
|
|
1115 (setq hname (buffer-substring save-pos (point)))
|
|
1116 (skip-chars-forward ": \t ")
|
|
1117 (setq save-pos (point))
|
|
1118 (skip-chars-forward "^\n\r")
|
|
1119 (setq hvalu (buffer-substring save-pos (point))
|
|
1120 result (cons (cons hname hvalu) result)))
|
|
1121 (or no-delete (delete-region st nd))
|
|
1122 result))
|
|
1123
|
|
1124 (defun mm-find-available-multiparts (separator &optional buf)
|
|
1125 "Return a list of mime-headers for the various body parts of a
|
|
1126 multipart message in buffer BUF with separator SEPARATOR.
|
|
1127 The different multipart specs are put in `mm-temporary-directory'."
|
|
1128 (let ((sep (concat "^--" separator "\r*$"))
|
|
1129 headers
|
|
1130 fname
|
|
1131 results)
|
|
1132 (save-excursion
|
|
1133 (and buf (set-buffer buf))
|
|
1134 (goto-char (point-min))
|
|
1135 (while (re-search-forward sep nil t)
|
|
1136 (let ((st (set-marker (make-marker)
|
|
1137 (progn
|
|
1138 (forward-line 1)
|
|
1139 (beginning-of-line)
|
|
1140 (point))))
|
|
1141 (nd (set-marker (make-marker)
|
|
1142 (if (re-search-forward sep nil t)
|
|
1143 (1- (match-beginning 0))
|
|
1144 (point-max)))))
|
|
1145 (narrow-to-region st nd)
|
|
1146 (goto-char st)
|
|
1147 (if (looking-at "^\r*$")
|
|
1148 (insert "Content-type: text/plain\n"
|
|
1149 "Content-length: " (int-to-string (- nd st)) "\n"))
|
|
1150 (setq headers (mm-parse-mime-headers)
|
|
1151 fname (mm-generate-unique-filename))
|
|
1152 (let ((x (or (cdr (assoc "content-type" headers)) "text/plain")))
|
|
1153 (if (string-match "name=\"*\\([^ \"]+\\)\"*" x)
|
|
1154 (setq fname (expand-file-name
|
|
1155 (substring x (match-beginning 1)
|
|
1156 (match-end 1))
|
|
1157 mm-temporary-directory))))
|
|
1158 (widen)
|
|
1159 (if (assoc "content-transfer-encoding" headers)
|
|
1160 (let ((coding (cdr
|
|
1161 (assoc "content-transfer-encoding" headers)))
|
|
1162 (cmd nil))
|
|
1163 (setq coding (and coding (downcase coding))
|
|
1164 cmd (or (cdr (assoc coding
|
|
1165 mm-content-transfer-encodings))
|
|
1166 (read-string
|
|
1167 (concat "How shall I decode " coding "? ")
|
|
1168 "cat")))
|
|
1169 (if (string= cmd "") (setq cmd "cat"))
|
|
1170 (if (stringp cmd)
|
|
1171 (shell-command-on-region st nd cmd t)
|
|
1172 (funcall cmd st nd))
|
|
1173 (set-marker nd (point))))
|
|
1174 (write-region st nd fname nil 5)
|
|
1175 (delete-region st nd)
|
|
1176 (setq results (cons
|
|
1177 (cons
|
|
1178 (cons "mm-filename" fname) headers) results)))))
|
|
1179 results))
|
|
1180
|
|
1181 (defun mm-format-multipart-as-html (&optional buf type)
|
|
1182 (if buf (set-buffer buf))
|
|
1183 (let* ((boundary (if (string-match
|
|
1184 "boundary[ \t]*=[ \t\"]*\\([^ \"\t\n]+\\)"
|
|
1185 type)
|
|
1186 (regexp-quote
|
|
1187 (substring type (match-beginning 1) (match-end 1)))))
|
|
1188 (parts (mm-find-available-multiparts boundary)))
|
|
1189 (erase-buffer)
|
|
1190 (insert "<html>\n"
|
|
1191 " <head>\n"
|
|
1192 " <title>Multipart Message</title>\n"
|
|
1193 " </head>\n"
|
|
1194 " <body>\n"
|
|
1195 " <h1> Multipart message encountered </h1>\n"
|
|
1196 " <p> I have encountered a multipart MIME message.\n"
|
|
1197 " The following parts have been detected. Please\n"
|
|
1198 " select which one you want to view.\n"
|
|
1199 " </p>\n"
|
|
1200 " <ul>\n"
|
|
1201 (mapconcat
|
|
1202 (function (lambda (x)
|
|
1203 (concat " <li> <a href=\"file:"
|
|
1204 (cdr (assoc "mm-filename" x))
|
|
1205 "\">"
|
|
1206 (or (cdr (assoc "content-description" x)) "")
|
|
1207 "--"
|
|
1208 (or (cdr (assoc "content-type" x))
|
|
1209 "unknown type")
|
|
1210 "</a> </li>")))
|
|
1211 parts "\n")
|
|
1212 " </ul>\n"
|
|
1213 " </body>\n"
|
|
1214 "</html>\n"
|
|
1215 "<!-- Automatically generated by MM v" mm-version "-->\n")))
|
|
1216
|
|
1217 (defun mm-multipart-viewer ()
|
|
1218 (mm-format-multipart-as-html
|
|
1219 (current-buffer)
|
|
1220 (cdr (assoc "content-type" url-current-mime-headers)))
|
|
1221 (let ((w3-working-buffer (current-buffer)))
|
|
1222 (w3-prepare-buffer)))
|
|
1223
|
|
1224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1225 ;;; Transfer encodings we can decrypt automatically
|
|
1226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1227 (defun mm-decode-quoted-printable (&optional st nd)
|
|
1228 (interactive)
|
|
1229 (setq st (or st (point-min))
|
|
1230 nd (or nd (point-max)))
|
|
1231 (save-restriction
|
|
1232 (narrow-to-region st nd)
|
|
1233 (save-excursion
|
|
1234 (let ((buffer-read-only nil))
|
|
1235 (goto-char (point-min))
|
|
1236 (while (re-search-forward "=[0-9A-F][0-9A-F]" nil t)
|
|
1237 (replace-match
|
|
1238 (char-to-string
|
|
1239 (+
|
|
1240 (* 16 (mm-hex-char-to-integer
|
|
1241 (char-after (1+ (match-beginning 0)))))
|
|
1242 (mm-hex-char-to-integer
|
|
1243 (char-after (1- (match-end 0))))))))))))
|
|
1244
|
|
1245 ;; Taken from hexl.el.
|
|
1246 (defun mm-hex-char-to-integer (character)
|
|
1247 "Take a char and return its value as if it was a hex digit."
|
|
1248 (if (and (>= character ?0) (<= character ?9))
|
|
1249 (- character ?0)
|
|
1250 (let ((ch (logior character 32)))
|
|
1251 (if (and (>= ch ?a) (<= ch ?f))
|
|
1252 (- ch (- ?a 10))
|
|
1253 (error (format "Invalid hex digit `%c'." ch))))))
|
|
1254
|
|
1255
|
|
1256 (require 'base64)
|
|
1257 (provide 'mm)
|