1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
32 (defgroup mime-display ()
33 "Display of MIME in mail and news articles."
34 :link '(custom-manual "(emacs-mime)Customization")
38 ;;; Convenience macros.
40 (defsubst mm-handle-p (handle)
41 (memq (luna-class-name handle)
42 '(mmgnus-entity mime-gnus-entity)))
43 (defalias 'mm-handle-body 'mmgnus-entity-body-internal)
44 (defalias 'mm-handle-set-body 'mmgnus-entity-set-body-internal)
45 (defsubst mm-handle-multipart/mixed-p (handle)
46 (string= (mime-entity-content-type-internal handle) "multipart/mixed"))
47 (defalias 'mm-handle-type 'mime-entity-content-type-internal)
48 (defsubst mm-handle-type-parameters (handle)
49 (mime-content-type-parameters (mm-handle-type handle)))
50 (defsubst mm-handle-media-type (handle)
51 (mime-type/subtype-string
52 (mime-content-type-primary-type (mm-handle-type handle))
53 (mime-content-type-subtype (mm-handle-type handle))))
54 (defsubst mm-handle-media-supertype (handle)
55 (and (mime-content-type-primary-type (mm-handle-type handle))
56 (symbol-name (mime-content-type-primary-type (mm-handle-type handle)))))
57 (defsubst mm-handle-media-subtype (handle)
58 (and (mime-content-type-subtype (mm-handle-type handle))
59 (symbol-name (mime-content-type-subtype (mm-handle-type handle)))))
60 (defsubst mm-handle-encoding (handle)
61 (and (mime-entity-encoding-internal handle)
62 (intern (mime-entity-encoding-internal handle))))
63 (defalias 'mm-handle-child 'mime-entity-children-internal)
64 (defalias 'mm-handle-set-child 'mime-entity-set-children-internal)
65 (defalias 'mm-handle-parent 'mime-entity-parent-internal)
66 (defalias 'mm-handle-set-parent 'mime-entity-set-parent-internal)
67 (defalias 'mm-handle-undisplayer 'mmgnus-entity-undisplayer-internal)
68 (defalias 'mm-handle-set-undisplayer 'mmgnus-entity-set-undisplayer-internal)
69 (defalias 'mm-handle-disposition 'mime-entity-content-disposition-internal)
70 (defsubst mm-handle-disposition-type (handle)
71 (mime-content-disposition-type (mm-handle-disposition handle)))
72 (defsubst mm-handle-disposition-parameters (handle)
73 (mime-content-disposition-parameters (mm-handle-disposition handle)))
74 (defalias 'mm-handle-description 'mmgnus-entity-content-description-internal)
75 (defalias 'mm-handle-cache 'mmgnus-entity-cache-internal)
76 (defalias 'mm-handle-set-cache 'mmgnus-entity-set-cache-internal)
77 (defalias 'mm-handle-id 'mmgnus-entity-content-id-internal)
78 (defalias 'mm-handle-header 'mmgnus-entity-header-internal)
79 (defalias 'mm-handle-set-header 'mmgnus-entity-set-header-internal)
80 (defsubst mm-make-handle (&optional parent body type encoding undisplayer
81 disposition description cache
83 (luna-make-entity 'mmgnus-entity
87 :encoding (if (and encoding
89 (symbol-name encoding)
91 :undisplayer undisplayer
92 :content-disposition disposition
93 :content-description description
99 (defcustom mm-inline-media-tests
103 (mm-valid-and-fit-image-p 'jpeg handle)))
107 (mm-valid-and-fit-image-p 'png handle)))
111 (mm-valid-and-fit-image-p 'gif handle)))
115 (mm-valid-and-fit-image-p 'tiff handle)) )
119 (mm-valid-and-fit-image-p 'xbm handle)))
123 (mm-valid-and-fit-image-p 'xbm handle)))
127 (mm-valid-and-fit-image-p 'xpm handle)))
131 (mm-valid-and-fit-image-p 'xpm handle)))
135 (mm-valid-and-fit-image-p 'bmp handle)))
136 ("text/plain" mm-inline-text identity)
137 ("text/enriched" mm-inline-text identity)
138 ("text/richtext" mm-inline-text identity)
139 ("text/x-patch" mm-display-patch-inline
141 (locate-library "diff-mode")))
145 (locate-library "w3")))
149 (or (featurep 'vcard)
150 (locate-library "vcard"))))
151 ("message/delivery-status" mm-inline-text identity)
152 ("message/rfc822" mm-inline-message identity)
153 ("text/.*" mm-inline-text identity)
154 ("audio/wav" mm-inline-audio
156 (and (or (featurep 'nas-sound) (featurep 'native-sound))
157 (device-sound-enabled-p))))
161 (and (or (featurep 'nas-sound) (featurep 'native-sound))
162 (device-sound-enabled-p))))
163 ("application/pgp-signature" ignore identity)
164 ("multipart/alternative" ignore identity)
165 ("multipart/mixed" ignore identity)
166 ("multipart/related" ignore identity))
167 "Alist of media types/tests saying whether types can be displayed inline."
168 :type '(repeat (list (string :tag "MIME type")
169 (function :tag "Display function")
170 (function :tag "Display test")))
171 :group 'mime-display)
173 (defcustom mm-inlined-types
174 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
175 "application/pgp-signature")
176 "List of media types that are to be displayed inline."
177 :type '(repeat string)
178 :group 'mime-display)
180 (defcustom mm-automatic-display
181 '("text/plain" "text/enriched" "text/richtext" "text/html"
182 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
183 "message/rfc822" "text/x-patch" "application/pgp-signature")
184 "A list of MIME types to be displayed automatically."
185 :type '(repeat string)
186 :group 'mime-display)
188 (defcustom mm-attachment-override-types '("text/x-vcard")
189 "Types to have \"attachment\" ignored if they can be displayed inline."
190 :type '(repeat string)
191 :group 'mime-display)
193 (defcustom mm-inline-override-types nil
194 "Types to be treated as attachments even if they can be displayed inline."
195 :type '(repeat string)
196 :group 'mime-display)
198 (defcustom mm-automatic-external-display nil
199 "List of MIME type regexps that will be displayed externally automatically."
200 :type '(repeat string)
201 :group 'mime-display)
203 (defcustom mm-discouraged-alternatives nil
204 "List of MIME types that are discouraged when viewing multipart/alternative.
205 Viewing agents are supposed to view the last possible part of a message,
206 as that is supposed to be the richest. However, users may prefer other
207 types instead, and this list says what types are most unwanted. If,
208 for instance, text/html parts are very unwanted, and text/richtech are
209 somewhat unwanted, then the value of this variable should be set
212 (\"text/html\" \"text/richtext\")"
213 :type '(repeat string)
214 :group 'mime-display)
216 (defvar mm-tmp-directory
217 (cond ((fboundp 'temp-directory) (temp-directory))
218 ((boundp 'temporary-file-directory) temporary-file-directory)
220 "Where mm will store its temporary files.")
222 (defcustom mm-inline-large-images nil
223 "If non-nil, then all images fit in the buffer."
225 :group 'mime-display)
227 ;;; Internal variables.
229 (defvar mm-dissection-list nil)
230 (defvar mm-last-shell-command "")
231 (defvar mm-content-id-alist nil)
235 (defun mm-dissect-buffer-header (handle &optional no-strict-mime)
237 (let (ctl type cte cd description id result header-string header-end)
239 (mail-narrow-to-head)
240 (when (or no-strict-mime
241 (mail-fetch-field "mime-version"))
242 (setq ctl (mail-fetch-field "content-type")
243 ctl (ignore-errors (mail-header-parse-content-type ctl))
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 (setq header-end (point-max)
249 header-string (buffer-substring (point-min) header-end)))
251 (setq ctl (mail-header-parse-content-type "text/plain")))
252 (setq cte (and cte (downcase (mail-header-remove-whitespace
253 (mail-header-remove-comments
255 cd (and cd (ignore-errors
256 (mail-header-parse-content-disposition cd))))
259 (mime-entity-set-content-type-internal handle ctl)
260 (mime-entity-set-encoding-internal handle cte)
261 (mime-entity-set-content-disposition-internal handle cd)
262 (mmgnus-entity-set-content-description-internal handle description)
263 (mmgnus-entity-set-header-internal handle header-string)
264 (setq result handle))
265 (setq result (mm-make-handle nil nil ctl cte nil cd
266 description nil id nil header-string)))
268 (when (string-match " *<\\(.*\\)> *" id)
269 (setq id (match-string 1 id)))
270 (mmgnus-entity-set-content-id-internal result id))
273 (defun mm-dissect-buffer (handle &optional no-strict-mime)
274 "Dissect the current buffer and return a list of MIME handles."
276 (let* ((result (mm-dissect-buffer-header handle no-strict-mime))
277 (ctl (mime-entity-content-type-internal result))
278 (type (mime-content-type-primary-type ctl)))
280 ((and (eq gnus-mime-display-part-function
281 'gnus-mime-display-part-with-mime-view)
283 (mm-dissect-message result ctl))
284 ((eq type 'multipart)
285 (mm-dissect-multipart result ctl))
287 (mm-dissect-singlepart result ctl no-strict-mime)))
288 (when (mm-handle-id result)
289 (push (cons (mm-handle-id result) result) mm-content-id-alist))
292 (defun mm-dissect-singlepart (handle ctl &optional force)
293 (mm-handle-set-body handle (mm-copy-to-buffer))
294 (push (mm-handle-body handle) mm-dissection-list)
297 (defun mm-remove-all-parts ()
298 "Remove all MIME handles."
300 (mapcar 'mm-remove-part mm-dissection-list)
301 (setq mm-dissection-list nil))
303 (defun mm-dissect-message (handle ctl)
304 (goto-char (point-min))
307 (when (re-search-forward "\n\n" nil t)
308 (narrow-to-region (point) (point-max))
309 (let ((part (mm-dissect-buffer nil t)))
310 (mm-handle-set-parent part handle)
311 (mm-handle-set-child handle
312 (cons part (mm-handle-child handle))))))))
314 (defun mm-dissect-multipart (handle ctl)
315 (goto-char (point-min))
316 (let* ((node-id (and handle (mime-entity-node-id-internal handle)))
318 (boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
319 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
322 (goto-char (point-max))
323 (if (re-search-backward close-delimiter nil t)
326 (while (search-forward boundary end t)
327 (goto-char (match-beginning 0))
331 (narrow-to-region start (point))
332 (let ((part (mm-make-handle handle nil nil nil nil nil
333 nil nil nil nil nil)))
334 (mime-entity-set-node-id-internal part (cons this-node node-id))
335 (setq this-node (1+ this-node))
336 (mm-dissect-buffer part t)
337 (setq parts (cons part parts))))))
339 (setq start (point)))
343 (narrow-to-region start end)
344 (let ((part (mm-make-handle handle nil nil nil nil nil
345 nil nil nil nil nil)))
346 (mime-entity-set-node-id-internal part (cons this-node node-id))
347 (mm-dissect-buffer part t)
348 (setq parts (cons part parts))))))
349 (mm-handle-set-child handle (nreverse parts))
352 (defun mm-copy-to-buffer ()
353 "Copy the contents of the current buffer to a fresh buffer."
355 (let ((obuf (current-buffer))
357 (goto-char (point-min))
358 (search-forward-regexp "^\n" nil t)
360 (set-buffer (generate-new-buffer " *mm*"))
361 (insert-buffer-substring obuf beg)
364 (defun mm-display-part (handle &optional no-default)
365 "Display the MIME part represented by HANDLE.
366 Returns nil if the part is removed; inline if displayed inline;
367 external if displayed external."
369 (mm-mailcap-parse-mailcaps)
370 (if (mm-handle-displayed-p handle)
371 (mm-remove-part handle)
372 (let* ((type (mm-handle-media-type handle))
373 (method (mm-mailcap-mime-info type)))
374 (if (mm-inlined-p handle)
377 (mm-display-inline handle)
381 (if (and (not method)
382 (equal "text" (mm-handle-media-subtype handle)))
385 (mm-insert-inline handle (mm-get-part handle))
388 handle (or method 'mm-mailcap-save-binary-file)))))))))
390 (defun mm-display-external (handle method)
391 "Display HANDLE using METHOD."
392 (let ((outbuf (current-buffer)))
393 (mm-with-unibyte-buffer
394 (if (functionp method)
395 (let ((cur (current-buffer)))
396 (if (eq method 'mm-mailcap-save-binary-file)
398 (set-buffer (generate-new-buffer "*mm*"))
400 (mm-insert-part handle)
401 (let ((win (get-buffer-window cur t)))
403 (select-window win)))
404 (switch-to-buffer (generate-new-buffer "*mm*")))
405 (buffer-disable-undo)
406 (mm-set-buffer-file-coding-system mm-binary-coding-system)
407 (insert-buffer-substring cur)
408 (message "Viewing with %s" method)
409 (let ((mm (current-buffer))
410 (non-viewer (assq 'non-viewer
411 (mm-mailcap-mime-info
412 (mm-handle-media-type handle) t))))
416 (mm-save-part handle))
417 (when (and (not non-viewer)
419 (mm-handle-set-undisplayer handle mm)))))
420 ;; The function is a string to be executed.
421 (mm-insert-part handle)
422 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
423 (filename (mail-content-type-get
424 (mm-handle-disposition handle) 'filename))
425 (mime-info (mm-mailcap-mime-info
426 (mm-handle-media-type handle) t))
427 (needsterm (or (assoc "needsterm" mime-info)
428 (assoc "needsterminal" mime-info)))
429 (copiousoutput (assoc "copiousoutput" mime-info))
431 ;; We create a private sub-directory where we store our files.
433 (set-file-modes dir 448)
435 (setq file (expand-file-name (file-name-nondirectory filename)
437 (setq file (make-temp-name (expand-file-name "mm." dir))))
438 (let ((coding-system-for-write mm-binary-coding-system))
439 (write-region (point-min) (point-max) file nil 'nomesg))
440 (message "Viewing with %s" method)
443 (start-process "*display*" nil
448 method file (mm-handle-type handle)))
449 (mm-handle-set-undisplayer handle (cons file buffer)))
450 (message "Displaying %s..." (format method file))
453 (with-current-buffer outbuf
459 (call-process shell-file-name nil
461 (generate-new-buffer "*mm*"))
466 method file (mm-handle-type handle)))
467 (if (buffer-live-p buffer)
472 (ignore-errors (delete-file file))
473 (ignore-errors (delete-directory
474 (file-name-directory file)))
475 (ignore-errors (kill-buffer buffer))))))
479 (start-process "*display*"
481 (generate-new-buffer "*mm*"))
485 method file (mm-handle-type handle)))
486 (mm-handle-set-undisplayer handle (cons file buffer)))
487 (message "Displaying %s..." (format method file))
490 (defun mm-mailcap-command (method file type-list)
491 (let ((ctl (cdr type-list))
494 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t" method beg)
495 (push (substring method beg (match-beginning 0)) out)
496 (setq beg (match-end 0)
497 total (match-string 0 method)
498 sub (match-string 1 method))
500 ((string= total "%s")
501 (push (mm-quote-arg file) out))
502 ((string= total "%t")
503 (push (mm-quote-arg (car type-list)) out))
505 (push (mm-quote-arg (or (mime-content-type-parameter sub ctl) ""))
507 (push (substring method beg (length method)) out)
508 (mapconcat 'identity (nreverse out) "")))
510 (defun mm-remove-parts (handles)
511 "Remove the displayed MIME parts represented by HANDLE."
515 (while (setq handle (pop handles))
516 (mm-remove-parts handle))))
517 ((mm-handle-child handles)
518 (mm-remove-parts (mm-handle-child handles))
519 (mm-remove-part handles))
521 (mm-remove-part handles))))
523 (defun mm-destroy-parts (handles)
524 "Remove the displayed MIME parts represented by HANDLE."
528 (while (setq handle (pop handles))
529 (mm-destroy-parts handle))))
530 ((mm-handle-child handles)
531 (mm-destroy-parts (mm-handle-child handles))
532 (mm-destroy-part handles)
533 (mm-handle-set-child handles nil))
535 (mm-destroy-part handles))))
537 (defun mm-remove-part (handle)
538 "Remove the displayed MIME part represented by HANDLE."
539 (when (mm-handle-p handle)
540 (let ((object (mm-handle-undisplayer handle)))
543 ;; Internally displayed part.
544 ((mm-annotationp object)
545 (delete-annotation object))
546 ((or (functionp object)
548 (eq (car object) 'lambda)))
550 ;; Externally displayed part.
552 (ignore-errors (delete-file (car object)))
553 (ignore-errors (delete-directory (file-name-directory (car object))))
554 (ignore-errors (kill-buffer (cdr object))))
556 (when (buffer-live-p object)
557 (kill-buffer object)))))
558 (mm-handle-set-undisplayer handle nil))))
560 (defun mm-display-inline (handle)
561 (let* ((type (mm-handle-media-type handle))
562 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
563 (funcall function handle)
564 (goto-char (point-min))))
566 (defun mm-assoc-string-match (alist type)
568 (when (string-match (car elem) type)
571 (defun mm-inlinable-p (handle)
572 "Say whether HANDLE can be displayed inline."
573 (let ((alist mm-inline-media-tests)
574 (type (mm-handle-media-type handle))
577 (when (string-match (caar alist) type)
578 (setq test (caddar alist)
580 (setq test (funcall test handle)))
584 (defun mm-automatic-display-p (handle)
585 "Say whether the user wants HANDLE to be displayed automatically."
586 (let ((methods mm-automatic-display)
587 (type (mm-handle-media-type handle))
589 (while (setq method (pop methods))
590 (when (and (not (mm-inline-override-p handle))
591 (string-match method type)
592 (mm-inlinable-p handle))
597 (defun mm-inlined-p (handle)
598 "Say whether the user wants HANDLE to be displayed automatically."
599 (let ((methods mm-inlined-types)
600 (type (mm-handle-media-type handle))
602 (while (setq method (pop methods))
603 (when (and (not (mm-inline-override-p handle))
604 (string-match method type)
605 (mm-inlinable-p handle))
610 (defun mm-attachment-override-p (handle)
611 "Say whether HANDLE should have attachment behavior overridden."
612 (let ((types mm-attachment-override-types)
613 (type (mm-handle-media-type handle))
616 (while (setq ty (pop types))
617 (when (and (string-match ty type)
618 (mm-inlinable-p handle))
619 (throw 'found t))))))
621 (defun mm-inline-override-p (handle)
622 "Say whether HANDLE should have inline behavior overridden."
623 (let ((types mm-inline-override-types)
624 (type (mm-handle-media-type handle))
627 (while (setq ty (pop types))
628 (when (string-match ty type)
629 (throw 'found t))))))
631 (defun mm-automatic-external-display-p (type)
632 "Return the user-defined method for TYPE."
633 (let ((methods mm-automatic-external-display)
635 (while (setq method (pop methods))
636 (when (string-match method type)
641 (defun mm-destroy-part (handle)
642 "Destroy the data structures connected to HANDLE."
643 (when (mm-handle-p handle)
644 (mm-remove-part handle)
645 (when (buffer-live-p (mm-handle-body handle))
646 (kill-buffer (mm-handle-body handle))
647 (mm-handle-set-body handle nil))))
649 (defun mm-handle-displayed-p (handle)
650 "Say whether HANDLE is displayed or not."
651 (mm-handle-undisplayer handle))
654 ;;; Functions for outputting parts
657 (defun mm-get-part (handle)
658 "Return the contents of HANDLE as a string."
659 (mm-with-unibyte-buffer
660 (mm-insert-part handle)
663 (defun mm-insert-part (handle)
664 "Insert the contents of HANDLE in the current buffer."
665 (let ((cur (current-buffer)))
667 (if (member (mm-handle-media-supertype handle) '("text" "message"))
669 (insert-buffer-substring (mm-handle-body handle))
670 (mm-decode-content-transfer-encoding
671 (mm-handle-encoding handle)
672 (mm-handle-media-type handle))
673 (let ((temp (current-buffer)))
675 (insert-buffer-substring temp)))
676 (mm-with-unibyte-buffer
677 (insert-buffer-substring (mm-handle-body handle))
678 (mm-decode-content-transfer-encoding
679 (mm-handle-encoding handle)
680 (mm-handle-media-type handle))
681 (let ((temp (current-buffer)))
683 (insert-buffer-substring temp)))))))
685 (defvar mm-default-directory nil)
687 (defun mm-save-part (handle)
688 "Write HANDLE to a file."
689 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
690 (filename (mail-content-type-get
691 (mm-handle-disposition handle) 'filename))
694 (setq filename (file-name-nondirectory filename)))
696 (read-file-name "Save MIME part to: "
698 (or filename name "")
699 (or mm-default-directory default-directory))))
700 (setq mm-default-directory (file-name-directory file))
701 (when (or (not (file-exists-p file))
702 (yes-or-no-p (format "File %s already exists; overwrite? "
704 (mm-save-part-to-file handle file))))
706 (defun mm-save-part-to-file (handle file)
707 (mm-with-unibyte-buffer
708 (mm-insert-part handle)
709 (let ((coding-system-for-write 'binary)
710 ;; Don't re-compress .gz & al. Arguably we should make
711 ;; `file-name-handler-alist' nil, but that would chop
712 ;; ange-ftp, which is reasonable to use here.
713 (inhibit-file-name-operation 'write-region)
714 (inhibit-file-name-handlers
715 (cons 'jka-compr-handler inhibit-file-name-handlers)))
716 (write-region (point-min) (point-max) file))))
718 (defun mm-pipe-part (handle)
719 "Pipe HANDLE to a process."
720 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
722 (read-string "Shell command on MIME part: " mm-last-shell-command)))
723 (mm-with-unibyte-buffer
724 (mm-insert-part handle)
725 (shell-command-on-region (point-min) (point-max) command nil))))
727 (defun mm-interactively-view-part (handle)
728 "Display HANDLE using METHOD."
729 (let* ((type (mm-handle-media-type handle))
731 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
732 (mm-mailcap-mime-info type 'all)))
733 (method (completing-read "Viewer: " methods)))
734 (mm-display-external (copy-sequence handle) method)))
736 (defun mm-preferred-alternative (handles &optional preferred)
737 "Say which of HANDLES are preferred."
738 (let ((prec (if preferred (list preferred)
739 (mm-preferred-alternative-precedence handles)))
740 p h result type handle)
741 (while (setq p (pop prec))
744 (setq handle (car h))
745 (setq type (mm-handle-media-type handle))
746 (when (and (equal p type)
747 (mm-automatic-display-p handle)
748 (or (mm-handle-child handle)
749 (not (mm-handle-disposition handle))
750 (eq (mm-handle-disposition-type handle) 'inline)))
757 (defun mm-preferred-alternative-precedence (handles)
758 "Return the precedence based on HANDLES and mm-discouraged-alternatives."
759 (let ((seq (nreverse (mapcar (lambda (h)
760 (mm-handle-media-type h))
762 (dolist (disc (reverse mm-discouraged-alternatives))
763 (dolist (elem (copy-sequence seq))
764 (when (string-match disc elem)
765 (setq seq (nconc (delete elem seq) (list elem))))))
768 (defun mm-get-content-id (id)
769 "Return the handle(s) referred to by ID."
770 (cdr (assoc id mm-content-id-alist)))
772 (defun mm-get-image (handle)
773 "Return an image instance based on HANDLE."
774 (let ((type (mm-handle-media-subtype handle))
776 ;; Allow some common translations.
779 ((equal type "x-pixmap")
781 ((equal type "x-xbitmap")
784 (or (mm-handle-cache handle)
785 (mm-with-unibyte-buffer
786 (mm-insert-part handle)
792 ;; xbm images require special handling, since
793 ;; the only way to create glyphs from these
794 ;; (without a ton of work) is to write them
795 ;; out to a file, and then create a file
797 (let ((file (make-temp-name
798 (expand-file-name "emm.xbm"
802 (write-region (point-min) (point-max) file)
803 (make-glyph (list (cons 'x file))))
805 (delete-file file)))))
808 (vector (intern type) :data (buffer-string)))))))
809 (mm-handle-set-cache handle spec))))))
811 (defun mm-image-fit-p (handle)
812 "Say whether the image in HANDLE will fit the current window."
813 (let ((image (mm-get-image handle)))
814 (or mm-inline-large-images
815 (and (< (glyph-width image) (window-pixel-width))
816 (< (glyph-height image) (window-pixel-height))))))
818 (defun mm-valid-image-format-p (format)
819 "Say whether FORMAT can be displayed natively by Emacs."
820 (and (fboundp 'valid-image-instantiator-format-p)
821 (valid-image-instantiator-format-p format)))
823 (defun mm-valid-and-fit-image-p (format handle)
824 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
826 (mm-valid-image-format-p format)
827 (mm-image-fit-p handle)))
831 ;; mm-decode.el ends here