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