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