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