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