Mercurial > emacs
annotate lisp/gnus/mm-decode.el @ 34909:421a33797ea8
(delete-key-deletes-forward-mode): Bind backspace
and delete in isearch-mode-map.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Thu, 28 Dec 2000 15:11:53 +0000 |
parents | aaf69bc74739 |
children | 0b4c50eb2002 |
rev | line source |
---|---|
31717 | 1 ;;; mm-decode.el --- Functions for decoding MIME things |
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp> | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 2, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 ;; Boston, MA 02111-1307, USA. | |
22 | |
23 ;;; Commentary: | |
24 | |
25 ;;; Code: | |
26 | |
27 (require 'mail-parse) | |
28 (require 'mailcap) | |
29 (require 'mm-bodies) | |
30 (eval-when-compile (require 'cl)) | |
31 | |
32 (eval-and-compile | |
33296 | 33 (autoload 'mm-inline-partial "mm-partial") |
34 (autoload 'mm-insert-inline "mm-view")) | |
31717 | 35 |
36 (defgroup mime-display () | |
37 "Display of MIME in mail and news articles." | |
38 :link '(custom-manual "(emacs-mime)Customization") | |
39 :version "21.1" | |
40 :group 'mail | |
41 :group 'news | |
42 :group 'multimedia) | |
43 | |
44 ;;; Convenience macros. | |
45 | |
46 (defmacro mm-handle-buffer (handle) | |
47 `(nth 0 ,handle)) | |
48 (defmacro mm-handle-type (handle) | |
49 `(nth 1 ,handle)) | |
50 (defsubst mm-handle-media-type (handle) | |
51 (if (stringp (car handle)) | |
52 (car handle) | |
53 (car (mm-handle-type handle)))) | |
54 (defsubst mm-handle-media-supertype (handle) | |
55 (car (split-string (mm-handle-media-type handle) "/"))) | |
56 (defsubst mm-handle-media-subtype (handle) | |
57 (cadr (split-string (mm-handle-media-type handle) "/"))) | |
58 (defmacro mm-handle-encoding (handle) | |
59 `(nth 2 ,handle)) | |
60 (defmacro mm-handle-undisplayer (handle) | |
61 `(nth 3 ,handle)) | |
62 (defmacro mm-handle-set-undisplayer (handle function) | |
63 `(setcar (nthcdr 3 ,handle) ,function)) | |
64 (defmacro mm-handle-disposition (handle) | |
65 `(nth 4 ,handle)) | |
66 (defmacro mm-handle-description (handle) | |
67 `(nth 5 ,handle)) | |
68 (defmacro mm-handle-cache (handle) | |
69 `(nth 6 ,handle)) | |
70 (defmacro mm-handle-set-cache (handle contents) | |
71 `(setcar (nthcdr 6 ,handle) ,contents)) | |
72 (defmacro mm-handle-id (handle) | |
73 `(nth 7 ,handle)) | |
74 (defmacro mm-make-handle (&optional buffer type encoding undisplayer | |
75 disposition description cache | |
76 id) | |
77 `(list ,buffer ,type ,encoding ,undisplayer | |
78 ,disposition ,description ,cache ,id)) | |
79 | |
80 (defcustom mm-inline-media-tests | |
81 '(("image/jpeg" | |
82 mm-inline-image | |
83 (lambda (handle) | |
84 (mm-valid-and-fit-image-p 'jpeg handle))) | |
85 ("image/png" | |
86 mm-inline-image | |
87 (lambda (handle) | |
88 (mm-valid-and-fit-image-p 'png handle))) | |
89 ("image/gif" | |
90 mm-inline-image | |
91 (lambda (handle) | |
92 (mm-valid-and-fit-image-p 'gif handle))) | |
93 ("image/tiff" | |
94 mm-inline-image | |
95 (lambda (handle) | |
96 (mm-valid-and-fit-image-p 'tiff handle)) ) | |
97 ("image/xbm" | |
98 mm-inline-image | |
99 (lambda (handle) | |
100 (mm-valid-and-fit-image-p 'xbm handle))) | |
101 ("image/x-xbitmap" | |
102 mm-inline-image | |
103 (lambda (handle) | |
104 (mm-valid-and-fit-image-p 'xbm handle))) | |
105 ("image/xpm" | |
106 mm-inline-image | |
107 (lambda (handle) | |
108 (mm-valid-and-fit-image-p 'xpm handle))) | |
109 ("image/x-pixmap" | |
110 mm-inline-image | |
111 (lambda (handle) | |
112 (mm-valid-and-fit-image-p 'xpm handle))) | |
113 ("image/bmp" | |
114 mm-inline-image | |
115 (lambda (handle) | |
116 (mm-valid-and-fit-image-p 'bmp handle))) | |
117 ("text/plain" mm-inline-text identity) | |
118 ("text/enriched" mm-inline-text identity) | |
119 ("text/richtext" mm-inline-text identity) | |
120 ("text/x-patch" mm-display-patch-inline | |
121 (lambda (handle) | |
122 (locate-library "diff-mode"))) | |
31764 | 123 ("application/emacs-lisp" mm-display-elisp-inline identity) |
31717 | 124 ("text/html" |
125 mm-inline-text | |
126 (lambda (handle) | |
127 (locate-library "w3"))) | |
128 ("text/x-vcard" | |
129 mm-inline-text | |
130 (lambda (handle) | |
131 (or (featurep 'vcard) | |
132 (locate-library "vcard")))) | |
133 ("message/delivery-status" mm-inline-text identity) | |
134 ("message/rfc822" mm-inline-message identity) | |
135 ("message/partial" mm-inline-partial identity) | |
136 ("text/.*" mm-inline-text identity) | |
137 ("audio/wav" mm-inline-audio | |
138 (lambda (handle) | |
139 (and (or (featurep 'nas-sound) (featurep 'native-sound)) | |
140 (device-sound-enabled-p)))) | |
141 ("audio/au" | |
142 mm-inline-audio | |
143 (lambda (handle) | |
144 (and (or (featurep 'nas-sound) (featurep 'native-sound)) | |
145 (device-sound-enabled-p)))) | |
146 ("application/pgp-signature" ignore identity) | |
147 ("multipart/alternative" ignore identity) | |
148 ("multipart/mixed" ignore identity) | |
149 ("multipart/related" ignore identity)) | |
150 "Alist of media types/tests saying whether types can be displayed inline." | |
151 :type '(repeat (list (string :tag "MIME type") | |
152 (function :tag "Display function") | |
153 (function :tag "Display test"))) | |
154 :group 'mime-display) | |
155 | |
156 (defcustom mm-inlined-types | |
157 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822" | |
31764 | 158 "message/partial" "application/emacs-lisp" |
31717 | 159 "application/pgp-signature") |
160 "List of media types that are to be displayed inline." | |
161 :type '(repeat string) | |
162 :group 'mime-display) | |
163 | |
164 (defcustom mm-automatic-display | |
165 '("text/plain" "text/enriched" "text/richtext" "text/html" | |
166 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*" | |
31764 | 167 "message/rfc822" "text/x-patch" "application/pgp-signature" |
168 "application/emacs-lisp") | |
31717 | 169 "A list of MIME types to be displayed automatically." |
170 :type '(repeat string) | |
171 :group 'mime-display) | |
172 | |
173 (defcustom mm-attachment-override-types '("text/x-vcard") | |
174 "Types to have \"attachment\" ignored if they can be displayed inline." | |
175 :type '(repeat string) | |
176 :group 'mime-display) | |
177 | |
178 (defcustom mm-inline-override-types nil | |
179 "Types to be treated as attachments even if they can be displayed inline." | |
180 :type '(repeat string) | |
181 :group 'mime-display) | |
182 | |
183 (defcustom mm-automatic-external-display nil | |
184 "List of MIME type regexps that will be displayed externally automatically." | |
185 :type '(repeat string) | |
186 :group 'mime-display) | |
187 | |
188 (defcustom mm-discouraged-alternatives nil | |
189 "List of MIME types that are discouraged when viewing multipart/alternative. | |
190 Viewing agents are supposed to view the last possible part of a message, | |
191 as that is supposed to be the richest. However, users may prefer other | |
192 types instead, and this list says what types are most unwanted. If, | |
193 for instance, text/html parts are very unwanted, and text/richtext are | |
194 somewhat unwanted, then the value of this variable should be set | |
195 to: | |
196 | |
197 (\"text/html\" \"text/richtext\")" | |
198 :type '(repeat string) | |
199 :group 'mime-display) | |
200 | |
201 (defvar mm-tmp-directory | |
202 (cond ((fboundp 'temp-directory) (temp-directory)) | |
203 ((boundp 'temporary-file-directory) temporary-file-directory) | |
204 ("/tmp/")) | |
205 "Where mm will store its temporary files.") | |
206 | |
207 (defcustom mm-inline-large-images nil | |
208 "If non-nil, then all images fit in the buffer." | |
209 :type 'boolean | |
210 :group 'mime-display) | |
211 | |
212 ;;; Internal variables. | |
213 | |
214 (defvar mm-dissection-list nil) | |
215 (defvar mm-last-shell-command "") | |
216 (defvar mm-content-id-alist nil) | |
217 | |
218 ;; According to RFC2046, in particular, in a digest, the default | |
219 ;; Content-Type value for a body part is changed from "text/plain" to | |
220 ;; "message/rfc822". | |
221 (defvar mm-dissect-default-type "text/plain") | |
222 | |
32962 | 223 (defvar mm-viewer-completion-map |
224 (let ((map (make-sparse-keymap 'mm-viewer-completion-map))) | |
225 (set-keymap-parent map minibuffer-local-completion-map) | |
226 map) | |
227 "Keymap for input viewer with completion.") | |
228 | |
229 ;; Should we bind other key to minibuffer-complete-word? | |
230 (define-key mm-viewer-completion-map " " 'self-insert-command) | |
231 | |
31717 | 232 ;;; The functions. |
233 | |
234 (defun mm-dissect-buffer (&optional no-strict-mime) | |
235 "Dissect the current buffer and return a list of MIME handles." | |
236 (save-excursion | |
237 (let (ct ctl type subtype cte cd description id result) | |
238 (save-restriction | |
239 (mail-narrow-to-head) | |
240 (when (or no-strict-mime | |
241 (mail-fetch-field "mime-version")) | |
242 (setq ct (mail-fetch-field "content-type") | |
243 ctl (ignore-errors (mail-header-parse-content-type ct)) | |
244 cte (mail-fetch-field "content-transfer-encoding") | |
245 cd (mail-fetch-field "content-disposition") | |
246 description (mail-fetch-field "content-description") | |
247 id (mail-fetch-field "content-id")))) | |
248 (when cte | |
249 (setq cte (mail-header-strip cte))) | |
250 (if (or (not ctl) | |
251 (not (string-match "/" (car ctl)))) | |
252 (mm-dissect-singlepart | |
253 (list mm-dissect-default-type) | |
254 (and cte (intern (downcase (mail-header-remove-whitespace | |
255 (mail-header-remove-comments | |
256 cte))))) | |
257 no-strict-mime | |
258 (and cd (ignore-errors (mail-header-parse-content-disposition cd))) | |
259 description) | |
260 (setq type (split-string (car ctl) "/")) | |
261 (setq subtype (cadr type) | |
262 type (pop type)) | |
263 (setq | |
264 result | |
265 (cond | |
266 ((equal type "multipart") | |
267 (let ((mm-dissect-default-type (if (equal subtype "digest") | |
268 "message/rfc822" | |
269 "text/plain"))) | |
270 (cons (car ctl) (mm-dissect-multipart ctl)))) | |
271 (t | |
272 (mm-dissect-singlepart | |
273 ctl | |
274 (and cte (intern (downcase (mail-header-remove-whitespace | |
275 (mail-header-remove-comments | |
276 cte))))) | |
277 no-strict-mime | |
278 (and cd (ignore-errors (mail-header-parse-content-disposition cd))) | |
279 description id)))) | |
280 (when id | |
281 (when (string-match " *<\\(.*\\)> *" id) | |
282 (setq id (match-string 1 id))) | |
283 (push (cons id result) mm-content-id-alist)) | |
284 result)))) | |
285 | |
286 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id) | |
287 (when (or force | |
288 (if (equal "text/plain" (car ctl)) | |
289 (assoc 'format ctl) | |
290 t)) | |
291 (let ((res (mm-make-handle | |
292 (mm-copy-to-buffer) ctl cte nil cdl description nil id))) | |
293 (push (car res) mm-dissection-list) | |
294 res))) | |
295 | |
296 (defun mm-remove-all-parts () | |
297 "Remove all MIME handles." | |
298 (interactive) | |
299 (mapcar 'mm-remove-part mm-dissection-list) | |
300 (setq mm-dissection-list nil)) | |
301 | |
302 (defun mm-dissect-multipart (ctl) | |
303 (goto-char (point-min)) | |
304 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary))) | |
305 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$")) | |
306 start parts | |
307 (end (save-excursion | |
308 (goto-char (point-max)) | |
309 (if (re-search-backward close-delimiter nil t) | |
310 (match-beginning 0) | |
311 (point-max))))) | |
312 (setq boundary (concat (regexp-quote boundary) "[ \t]*$")) | |
34833
aaf69bc74739
* gnus-art.el (gnus-article-check-hidden-text): Return t.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33342
diff
changeset
|
313 (while (and (< (point) end) (re-search-forward boundary end t)) |
31717 | 314 (goto-char (match-beginning 0)) |
315 (when start | |
316 (save-excursion | |
317 (save-restriction | |
318 (narrow-to-region start (point)) | |
319 (setq parts (nconc (list (mm-dissect-buffer t)) parts))))) | |
320 (forward-line 2) | |
321 (setq start (point))) | |
34833
aaf69bc74739
* gnus-art.el (gnus-article-check-hidden-text): Return t.
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
33342
diff
changeset
|
322 (when (and start (< start end)) |
31717 | 323 (save-excursion |
324 (save-restriction | |
325 (narrow-to-region start end) | |
326 (setq parts (nconc (list (mm-dissect-buffer t)) parts))))) | |
327 (nreverse parts))) | |
328 | |
329 (defun mm-copy-to-buffer () | |
330 "Copy the contents of the current buffer to a fresh buffer." | |
331 (save-excursion | |
332 (let ((obuf (current-buffer)) | |
333 beg) | |
334 (goto-char (point-min)) | |
335 (search-forward-regexp "^\n" nil t) | |
336 (setq beg (point)) | |
337 (set-buffer (generate-new-buffer " *mm*")) | |
338 (insert-buffer-substring obuf beg) | |
339 (current-buffer)))) | |
340 | |
341 (defun mm-display-part (handle &optional no-default) | |
342 "Display the MIME part represented by HANDLE. | |
343 Returns nil if the part is removed; inline if displayed inline; | |
344 external if displayed external." | |
345 (save-excursion | |
346 (mailcap-parse-mailcaps) | |
347 (if (mm-handle-displayed-p handle) | |
348 (mm-remove-part handle) | |
349 (let* ((type (mm-handle-media-type handle)) | |
350 (method (mailcap-mime-info type))) | |
351 (if (mm-inlined-p handle) | |
352 (progn | |
353 (forward-line 1) | |
354 (mm-display-inline handle) | |
355 'inline) | |
356 (when (or method | |
357 (not no-default)) | |
358 (if (and (not method) | |
359 (equal "text" (car (split-string type)))) | |
360 (progn | |
361 (forward-line 1) | |
362 (mm-insert-inline handle (mm-get-part handle)) | |
363 'inline) | |
364 (mm-display-external | |
365 handle (or method 'mailcap-save-binary-file))))))))) | |
366 | |
367 (defun mm-display-external (handle method) | |
368 "Display HANDLE using METHOD." | |
369 (let ((outbuf (current-buffer))) | |
370 (mm-with-unibyte-buffer | |
371 (if (functionp method) | |
372 (let ((cur (current-buffer))) | |
373 (if (eq method 'mailcap-save-binary-file) | |
374 (progn | |
33174
702845b072b7
(mm-display-external): Space prefix temp buffer
Dave Love <fx@gnu.org>
parents:
32962
diff
changeset
|
375 (set-buffer (generate-new-buffer " *mm*")) |
31717 | 376 (setq method nil)) |
377 (mm-insert-part handle) | |
378 (let ((win (get-buffer-window cur t))) | |
379 (when win | |
380 (select-window win))) | |
33174
702845b072b7
(mm-display-external): Space prefix temp buffer
Dave Love <fx@gnu.org>
parents:
32962
diff
changeset
|
381 (switch-to-buffer (generate-new-buffer " *mm*"))) |
31717 | 382 (mm-set-buffer-file-coding-system mm-binary-coding-system) |
383 (insert-buffer-substring cur) | |
384 (goto-char (point-min)) | |
385 (message "Viewing with %s" method) | |
386 (let ((mm (current-buffer)) | |
387 (non-viewer (assq 'non-viewer | |
388 (mailcap-mime-info | |
389 (mm-handle-media-type handle) t)))) | |
390 (unwind-protect | |
391 (if method | |
392 (funcall method) | |
393 (mm-save-part handle)) | |
394 (when (and (not non-viewer) | |
395 method) | |
396 (mm-handle-set-undisplayer handle mm))))) | |
397 ;; The function is a string to be executed. | |
398 (mm-insert-part handle) | |
399 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory))) | |
400 (filename (mail-content-type-get | |
401 (mm-handle-disposition handle) 'filename)) | |
402 (mime-info (mailcap-mime-info | |
403 (mm-handle-media-type handle) t)) | |
404 (needsterm (or (assoc "needsterm" mime-info) | |
405 (assoc "needsterminal" mime-info))) | |
406 (copiousoutput (assoc "copiousoutput" mime-info)) | |
407 file buffer) | |
408 ;; We create a private sub-directory where we store our files. | |
409 (make-directory dir) | |
410 (set-file-modes dir 448) | |
411 (if filename | |
412 (setq file (expand-file-name (file-name-nondirectory filename) | |
413 dir)) | |
414 (setq file (make-temp-name (expand-file-name "mm." dir)))) | |
415 (let ((coding-system-for-write mm-binary-coding-system)) | |
416 (write-region (point-min) (point-max) file nil 'nomesg)) | |
417 (message "Viewing with %s" method) | |
418 (cond (needsterm | |
419 (unwind-protect | |
420 (start-process "*display*" nil | |
421 "xterm" | |
422 "-e" shell-file-name | |
423 shell-command-switch | |
424 (mm-mailcap-command | |
425 method file (mm-handle-type handle))) | |
426 (mm-handle-set-undisplayer handle (cons file buffer))) | |
427 (message "Displaying %s..." (format method file)) | |
428 'external) | |
429 (copiousoutput | |
430 (with-current-buffer outbuf | |
431 (forward-line 1) | |
432 (mm-insert-inline | |
433 handle | |
434 (unwind-protect | |
435 (progn | |
436 (call-process shell-file-name nil | |
437 (setq buffer | |
438 (generate-new-buffer "*mm*")) | |
439 nil | |
440 shell-command-switch | |
441 (mm-mailcap-command | |
442 method file (mm-handle-type handle))) | |
443 (if (buffer-live-p buffer) | |
444 (save-excursion | |
445 (set-buffer buffer) | |
446 (buffer-string)))) | |
447 (progn | |
448 (ignore-errors (delete-file file)) | |
449 (ignore-errors (delete-directory | |
450 (file-name-directory file))) | |
451 (ignore-errors (kill-buffer buffer)))))) | |
452 'inline) | |
453 (t | |
454 (unwind-protect | |
455 (start-process "*display*" | |
456 (setq buffer | |
457 (generate-new-buffer "*mm*")) | |
458 shell-file-name | |
459 shell-command-switch | |
460 (mm-mailcap-command | |
461 method file (mm-handle-type handle))) | |
462 (mm-handle-set-undisplayer handle (cons file buffer))) | |
463 (message "Displaying %s..." (format method file)) | |
464 'external))))))) | |
465 | |
466 (defun mm-mailcap-command (method file type-list) | |
467 (let ((ctl (cdr type-list)) | |
468 (beg 0) | |
469 (uses-stdin t) | |
470 out sub total) | |
471 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg) | |
472 (push (substring method beg (match-beginning 0)) out) | |
473 (setq beg (match-end 0) | |
474 total (match-string 0 method) | |
475 sub (match-string 1 method)) | |
476 (cond | |
477 ((string= total "%%") | |
478 (push "%" out)) | |
479 ((string= total "%s") | |
480 (setq uses-stdin nil) | |
481 (push (mm-quote-arg file) out)) | |
482 ((string= total "%t") | |
483 (push (mm-quote-arg (car type-list)) out)) | |
484 (t | |
485 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out)))) | |
486 (push (substring method beg (length method)) out) | |
487 (if uses-stdin | |
488 (progn | |
489 (push "<" out) | |
490 (push (mm-quote-arg file) out))) | |
491 (mapconcat 'identity (nreverse out) ""))) | |
492 | |
493 (defun mm-remove-parts (handles) | |
494 "Remove the displayed MIME parts represented by HANDLES." | |
495 (if (and (listp handles) | |
496 (bufferp (car handles))) | |
497 (mm-remove-part handles) | |
498 (let (handle) | |
499 (while (setq handle (pop handles)) | |
500 (cond | |
501 ((stringp handle) | |
502 ;; Do nothing. | |
503 ) | |
504 ((and (listp handle) | |
505 (stringp (car handle))) | |
506 (mm-remove-parts (cdr handle))) | |
507 (t | |
508 (mm-remove-part handle))))))) | |
509 | |
510 (defun mm-destroy-parts (handles) | |
511 "Remove the displayed MIME parts represented by HANDLES." | |
512 (if (and (listp handles) | |
513 (bufferp (car handles))) | |
514 (mm-destroy-part handles) | |
515 (let (handle) | |
516 (while (setq handle (pop handles)) | |
517 (cond | |
518 ((stringp handle) | |
519 ;; Do nothing. | |
520 ) | |
521 ((and (listp handle) | |
522 (stringp (car handle))) | |
523 (mm-destroy-parts (cdr handle))) | |
524 (t | |
525 (mm-destroy-part handle))))))) | |
526 | |
527 (defun mm-remove-part (handle) | |
528 "Remove the displayed MIME part represented by HANDLE." | |
529 (when (listp handle) | |
530 (let ((object (mm-handle-undisplayer handle))) | |
531 (ignore-errors | |
532 (cond | |
533 ;; Internally displayed part. | |
534 ((mm-annotationp object) | |
535 (delete-annotation object)) | |
536 ((or (functionp object) | |
537 (and (listp object) | |
538 (eq (car object) 'lambda))) | |
539 (funcall object)) | |
540 ;; Externally displayed part. | |
541 ((consp object) | |
542 (ignore-errors (delete-file (car object))) | |
543 (ignore-errors (delete-directory (file-name-directory (car object)))) | |
544 (ignore-errors (kill-buffer (cdr object)))) | |
545 ((bufferp object) | |
546 (when (buffer-live-p object) | |
547 (kill-buffer object))))) | |
548 (mm-handle-set-undisplayer handle nil)))) | |
549 | |
550 (defun mm-display-inline (handle) | |
551 (let* ((type (mm-handle-media-type handle)) | |
552 (function (cadr (mm-assoc-string-match mm-inline-media-tests type)))) | |
553 (funcall function handle) | |
554 (goto-char (point-min)))) | |
555 | |
556 (defun mm-assoc-string-match (alist type) | |
557 (dolist (elem alist) | |
558 (when (string-match (car elem) type) | |
559 (return elem)))) | |
560 | |
561 (defun mm-inlinable-p (handle) | |
562 "Say whether HANDLE can be displayed inline." | |
563 (let ((alist mm-inline-media-tests) | |
564 (type (mm-handle-media-type handle)) | |
565 test) | |
566 (while alist | |
567 (when (string-match (caar alist) type) | |
568 (setq test (caddar alist) | |
569 alist nil) | |
570 (setq test (funcall test handle))) | |
571 (pop alist)) | |
572 test)) | |
573 | |
574 (defun mm-automatic-display-p (handle) | |
575 "Say whether the user wants HANDLE to be displayed automatically." | |
576 (let ((methods mm-automatic-display) | |
577 (type (mm-handle-media-type handle)) | |
578 method result) | |
579 (while (setq method (pop methods)) | |
580 (when (and (not (mm-inline-override-p handle)) | |
581 (string-match method type) | |
582 (mm-inlinable-p handle)) | |
583 (setq result t | |
584 methods nil))) | |
585 result)) | |
586 | |
587 (defun mm-inlined-p (handle) | |
588 "Say whether the user wants HANDLE to be displayed automatically." | |
589 (let ((methods mm-inlined-types) | |
590 (type (mm-handle-media-type handle)) | |
591 method result) | |
592 (while (setq method (pop methods)) | |
593 (when (and (not (mm-inline-override-p handle)) | |
594 (string-match method type) | |
595 (mm-inlinable-p handle)) | |
596 (setq result t | |
597 methods nil))) | |
598 result)) | |
599 | |
600 (defun mm-attachment-override-p (handle) | |
601 "Say whether HANDLE should have attachment behavior overridden." | |
602 (let ((types mm-attachment-override-types) | |
603 (type (mm-handle-media-type handle)) | |
604 ty) | |
605 (catch 'found | |
606 (while (setq ty (pop types)) | |
607 (when (and (string-match ty type) | |
608 (mm-inlinable-p handle)) | |
609 (throw 'found t)))))) | |
610 | |
611 (defun mm-inline-override-p (handle) | |
612 "Say whether HANDLE should have inline behavior overridden." | |
613 (let ((types mm-inline-override-types) | |
614 (type (mm-handle-media-type handle)) | |
615 ty) | |
616 (catch 'found | |
617 (while (setq ty (pop types)) | |
618 (when (string-match ty type) | |
619 (throw 'found t)))))) | |
620 | |
621 (defun mm-automatic-external-display-p (type) | |
622 "Return the user-defined method for TYPE." | |
623 (let ((methods mm-automatic-external-display) | |
624 method result) | |
625 (while (setq method (pop methods)) | |
626 (when (string-match method type) | |
627 (setq result t | |
628 methods nil))) | |
629 result)) | |
630 | |
631 (defun mm-destroy-part (handle) | |
632 "Destroy the data structures connected to HANDLE." | |
633 (when (listp handle) | |
634 (mm-remove-part handle) | |
635 (when (buffer-live-p (mm-handle-buffer handle)) | |
636 (kill-buffer (mm-handle-buffer handle))))) | |
637 | |
638 (defun mm-handle-displayed-p (handle) | |
639 "Say whether HANDLE is displayed or not." | |
640 (mm-handle-undisplayer handle)) | |
641 | |
642 ;;; | |
643 ;;; Functions for outputting parts | |
644 ;;; | |
645 | |
646 (defun mm-get-part (handle) | |
647 "Return the contents of HANDLE as a string." | |
648 (mm-with-unibyte-buffer | |
649 (mm-insert-part handle) | |
650 (buffer-string))) | |
651 | |
652 (defun mm-insert-part (handle) | |
653 "Insert the contents of HANDLE in the current buffer." | |
654 (let ((cur (current-buffer))) | |
655 (save-excursion | |
656 (if (member (mm-handle-media-supertype handle) '("text" "message")) | |
657 (with-temp-buffer | |
658 (insert-buffer-substring (mm-handle-buffer handle)) | |
659 (mm-decode-content-transfer-encoding | |
660 (mm-handle-encoding handle) | |
661 (mm-handle-media-type handle)) | |
662 (let ((temp (current-buffer))) | |
663 (set-buffer cur) | |
664 (insert-buffer-substring temp))) | |
665 (mm-with-unibyte-buffer | |
666 (insert-buffer-substring (mm-handle-buffer handle)) | |
667 (mm-decode-content-transfer-encoding | |
668 (mm-handle-encoding handle) | |
669 (mm-handle-media-type handle)) | |
670 (let ((temp (current-buffer))) | |
671 (set-buffer cur) | |
672 (insert-buffer-substring temp))))))) | |
673 | |
674 (defvar mm-default-directory nil) | |
675 | |
676 (defun mm-save-part (handle) | |
677 "Write HANDLE to a file." | |
678 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name)) | |
679 (filename (mail-content-type-get | |
680 (mm-handle-disposition handle) 'filename)) | |
681 file) | |
682 (when filename | |
683 (setq filename (file-name-nondirectory filename))) | |
684 (setq file | |
685 (read-file-name "Save MIME part to: " | |
686 (expand-file-name | |
687 (or filename name "") | |
688 (or mm-default-directory default-directory)))) | |
689 (setq mm-default-directory (file-name-directory file)) | |
690 (when (or (not (file-exists-p file)) | |
691 (yes-or-no-p (format "File %s already exists; overwrite? " | |
692 file))) | |
693 (mm-save-part-to-file handle file)))) | |
694 | |
695 (defun mm-save-part-to-file (handle file) | |
696 (mm-with-unibyte-buffer | |
697 (mm-insert-part handle) | |
698 (let ((coding-system-for-write 'binary) | |
699 ;; Don't re-compress .gz & al. Arguably we should make | |
700 ;; `file-name-handler-alist' nil, but that would chop | |
701 ;; ange-ftp, which is reasonable to use here. | |
702 (inhibit-file-name-operation 'write-region) | |
703 (inhibit-file-name-handlers | |
704 (cons 'jka-compr-handler inhibit-file-name-handlers))) | |
705 (write-region (point-min) (point-max) file)))) | |
706 | |
707 (defun mm-pipe-part (handle) | |
708 "Pipe HANDLE to a process." | |
709 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name)) | |
710 (command | |
711 (read-string "Shell command on MIME part: " mm-last-shell-command))) | |
712 (mm-with-unibyte-buffer | |
713 (mm-insert-part handle) | |
714 (shell-command-on-region (point-min) (point-max) command nil)))) | |
715 | |
716 (defun mm-interactively-view-part (handle) | |
717 "Display HANDLE using METHOD." | |
718 (let* ((type (mm-handle-media-type handle)) | |
719 (methods | |
720 (mapcar (lambda (i) (list (cdr (assoc 'viewer i)))) | |
721 (mailcap-mime-info type 'all))) | |
32962 | 722 (method (let ((minibuffer-local-completion-map |
723 mm-viewer-completion-map)) | |
724 (completing-read "Viewer: " methods)))) | |
31717 | 725 (when (string= method "") |
726 (error "No method given")) | |
727 (if (string-match "^[^% \t]+$" method) | |
728 (setq method (concat method " %s"))) | |
729 (mm-display-external (copy-sequence handle) method))) | |
730 | |
731 (defun mm-preferred-alternative (handles &optional preferred) | |
732 "Say which of HANDLES are preferred." | |
733 (let ((prec (if preferred (list preferred) | |
734 (mm-preferred-alternative-precedence handles))) | |
735 p h result type handle) | |
736 (while (setq p (pop prec)) | |
737 (setq h handles) | |
738 (while h | |
739 (setq handle (car h)) | |
740 (setq type (mm-handle-media-type handle)) | |
741 (when (and (equal p type) | |
742 (mm-automatic-display-p handle) | |
743 (or (stringp (car handle)) | |
744 (not (mm-handle-disposition handle)) | |
745 (equal (car (mm-handle-disposition handle)) | |
746 "inline"))) | |
747 (setq result handle | |
748 h nil | |
749 prec nil)) | |
750 (pop h))) | |
751 result)) | |
752 | |
753 (defun mm-preferred-alternative-precedence (handles) | |
754 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'." | |
755 (let ((seq (nreverse (mapcar #'mm-handle-media-type | |
756 handles)))) | |
757 (dolist (disc (reverse mm-discouraged-alternatives)) | |
758 (dolist (elem (copy-sequence seq)) | |
759 (when (string-match disc elem) | |
760 (setq seq (nconc (delete elem seq) (list elem)))))) | |
761 seq)) | |
762 | |
763 (defun mm-get-content-id (id) | |
764 "Return the handle(s) referred to by ID." | |
765 (cdr (assoc id mm-content-id-alist))) | |
766 | |
767 (defun mm-get-image (handle) | |
768 "Return an image instance based on HANDLE." | |
769 (let ((type (mm-handle-media-subtype handle)) | |
770 spec) | |
771 ;; Allow some common translations. | |
772 (setq type | |
773 (cond | |
774 ((equal type "x-pixmap") | |
775 "xpm") | |
776 ((equal type "x-xbitmap") | |
777 "xbm") | |
778 (t type))) | |
779 (or (mm-handle-cache handle) | |
780 (mm-with-unibyte-buffer | |
781 (mm-insert-part handle) | |
782 (prog1 | |
783 (setq spec | |
784 (ignore-errors | |
785 ;; Avoid testing `make-glyph' since W3 may define | |
786 ;; a bogus version of it. | |
787 (if (fboundp 'create-image) | |
788 (create-image (buffer-string) (intern type) 'data-p) | |
789 (cond | |
790 ((equal type "xbm") | |
791 ;; xbm images require special handling, since | |
792 ;; the only way to create glyphs from these | |
793 ;; (without a ton of work) is to write them | |
794 ;; out to a file, and then create a file | |
795 ;; specifier. | |
796 (let ((file (make-temp-name | |
797 (expand-file-name "emm.xbm" | |
798 mm-tmp-directory)))) | |
799 (unwind-protect | |
800 (progn | |
801 (write-region (point-min) (point-max) file) | |
802 (make-glyph (list (cons 'x file)))) | |
803 (ignore-errors | |
804 (delete-file file))))) | |
805 (t | |
806 (make-glyph | |
807 (vector (intern type) :data (buffer-string)))))))) | |
808 (mm-handle-set-cache handle spec)))))) | |
809 | |
810 (defun mm-image-fit-p (handle) | |
811 "Say whether the image in HANDLE will fit the current window." | |
812 (let ((image (mm-get-image handle))) | |
813 (if (fboundp 'glyph-width) | |
814 ;; XEmacs' glyphs can actually tell us about their width, so | |
815 ;; lets be nice and smart about them. | |
816 (or mm-inline-large-images | |
817 (and (< (glyph-width image) (window-pixel-width)) | |
818 (< (glyph-height image) (window-pixel-height)))) | |
819 (let* ((size (image-size image)) | |
820 (w (car size)) | |
821 (h (cdr size))) | |
822 (or mm-inline-large-images | |
823 (and (< h (1- (window-height))) ; Don't include mode line. | |
824 (< w (window-width)))))))) | |
825 | |
826 (defun mm-valid-image-format-p (format) | |
827 "Say whether FORMAT can be displayed natively by Emacs." | |
828 (cond | |
829 ;; Handle XEmacs | |
830 ((fboundp 'valid-image-instantiator-format-p) | |
831 (valid-image-instantiator-format-p format)) | |
832 ;; Handle Emacs 21 | |
833 ((fboundp 'image-type-available-p) | |
834 (and (display-graphic-p) | |
835 (image-type-available-p format))) | |
836 ;; Nobody else can do images yet. | |
837 (t | |
838 nil))) | |
839 | |
840 (defun mm-valid-and-fit-image-p (format handle) | |
841 "Say whether FORMAT can be displayed natively and HANDLE fits the window." | |
32962 | 842 (and (mm-valid-image-format-p format) |
31717 | 843 (mm-image-fit-p handle))) |
844 | |
845 (provide 'mm-decode) | |
846 | |
847 ;;; mm-decode.el ends here |