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