Importing Pterodactyl Gnus v0.93.
[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              (needsterm (assoc "needsterm"
334                                (mailcap-mime-info
335                                 (mm-handle-media-type handle)) t))
336              process file buffer)
337         ;; We create a private sub-directory where we store our files.
338         (make-directory dir)
339         (set-file-modes dir 448)
340         (if filename
341             (setq file (expand-file-name (file-name-nondirectory filename)
342                                          dir))
343           (setq file (make-temp-name (expand-file-name "mm." dir))))
344         (write-region (point-min) (point-max) file nil 'nomesg)
345         (message "Viewing with %s" method)
346         (unwind-protect
347             (setq process
348                   (if needsterm
349                       (start-process "*display*" nil
350                                      "xterm"
351                                      "-e" shell-file-name "-c"
352                                      (format method
353                                              (mm-quote-arg file)))
354                     (start-process "*display*"
355                                    (setq buffer (generate-new-buffer "*mm*"))
356                                    shell-file-name
357                                    "-c" (format method
358                                                 (mm-quote-arg file)))))
359           (mm-handle-set-undisplayer handle (cons file buffer)))
360         (message "Displaying %s..." (format method file))))))
361
362 (defun mm-remove-parts (handles)
363   "Remove the displayed MIME parts represented by HANDLE."
364   (if (and (listp handles)
365            (bufferp (car handles)))
366       (mm-remove-part handles)
367     (let (handle)
368       (while (setq handle (pop handles))
369         (cond
370          ((stringp handle)
371           )
372          ((and (listp handle)
373                (stringp (car handle)))
374           (mm-remove-parts (cdr handle)))
375          (t
376           (mm-remove-part handle)))))))
377
378 (defun mm-destroy-parts (handles)
379   "Remove the displayed MIME parts represented by HANDLE."
380   (if (and (listp handles)
381            (bufferp (car handles)))
382       (mm-destroy-part handles)
383     (let (handle)
384       (while (setq handle (pop handles))
385         (cond
386          ((stringp handle)
387           )
388          ((and (listp handle)
389                (stringp (car handle)))
390           (mm-destroy-parts (cdr handle)))
391          (t
392           (mm-destroy-part handle)))))))
393
394 (defun mm-remove-part (handle)
395   "Remove the displayed MIME part represented by HANDLE."
396   (when (listp handle)
397     (let ((object (mm-handle-undisplayer handle)))
398       (ignore-errors
399         (cond
400          ;; Internally displayed part.
401          ((mm-annotationp object)
402           (delete-annotation object))
403          ((or (functionp object)
404               (and (listp object)
405                    (eq (car object) 'lambda)))
406           (funcall object))
407          ;; Externally displayed part.
408          ((consp object)
409           (ignore-errors (delete-file (car object)))
410           (ignore-errors (delete-directory (file-name-directory (car object))))
411           (ignore-errors (kill-buffer (cdr object))))
412          ((bufferp object)
413           (when (buffer-live-p object)
414             (kill-buffer object)))))
415       (mm-handle-set-undisplayer handle nil))))
416
417 (defun mm-display-inline (handle)
418   (let* ((type (mm-handle-media-type handle))
419          (function (cadr (assoc type mm-inline-media-tests))))
420     (funcall function handle)
421     (goto-char (point-min))))
422
423 (defun mm-inlinable-p (handle)
424   "Say whether HANDLE can be displayed inline."
425   (let ((alist mm-inline-media-tests)
426         (type (mm-handle-media-type handle))
427         test)
428     (while alist
429       (when (equal type (caar alist))
430         (setq test (caddar alist)
431               alist nil)
432         (setq test (funcall test handle)))
433       (pop alist))
434     test))
435
436 (defun mm-automatic-display-p (handle)
437   "Say whether the user wants HANDLE to be displayed automatically."
438   (let ((methods mm-automatic-display)
439         (type (mm-handle-media-type handle))
440         method result)
441     (while (setq method (pop methods))
442       (when (and (string-match method type)
443                  (mm-inlinable-p handle))
444         (setq result t
445               methods nil)))
446     result))
447
448 (defun mm-inlined-p (handle)
449   "Say whether the user wants HANDLE to be displayed automatically."
450   (let ((methods mm-inlined-types)
451         (type (mm-handle-media-type handle))
452         method result)
453     (while (setq method (pop methods))
454       (when (and (string-match method type)
455                  (mm-inlinable-p handle))
456         (setq result t
457               methods nil)))
458     result))
459
460 (defun mm-attachment-override-p (handle)
461   "Say whether HANDLE should have attachment behavior overridden."
462   (let ((types mm-attachment-override-types)
463         (type (mm-handle-media-type handle))
464         ty)
465     (catch 'found
466       (while (setq ty (pop types))
467         (when (and (string-match ty type)
468                    (mm-inlinable-p handle))
469           (throw 'found t))))))
470
471 (defun mm-automatic-external-display-p (type)
472   "Return the user-defined method for TYPE."
473   (let ((methods mm-automatic-external-display)
474         method result)
475     (while (setq method (pop methods))
476       (when (string-match method type)
477         (setq result t
478               methods nil)))
479     result))
480
481 (defun mm-destroy-part (handle)
482   "Destroy the data structures connected to HANDLE."
483   (when (listp handle)
484     (mm-remove-part handle)
485     (when (buffer-live-p (mm-handle-buffer handle))
486       (kill-buffer (mm-handle-buffer handle)))))
487
488 (defun mm-handle-displayed-p (handle)
489   "Say whether HANDLE is displayed or not."
490   (mm-handle-undisplayer handle))
491
492 ;;;
493 ;;; Functions for outputting parts
494 ;;;
495
496 (defun mm-get-part (handle)
497   "Return the contents of HANDLE as a string."
498   (mm-with-unibyte-buffer
499     (mm-insert-part handle)
500     (buffer-string)))
501
502 (defun mm-insert-part (handle)
503   "Insert the contents of HANDLE in the current buffer."
504   (let ((cur (current-buffer)))
505     (save-excursion
506       (if (member (mm-handle-media-supertype handle) '("text" "message"))
507           (with-temp-buffer
508             (insert-buffer-substring (mm-handle-buffer handle))
509             (mm-decode-content-transfer-encoding
510              (mm-handle-encoding handle)
511              (mm-handle-media-type handle))
512             (let ((temp (current-buffer)))
513               (set-buffer cur)
514               (insert-buffer-substring temp)))
515         (mm-with-unibyte-buffer
516           (insert-buffer-substring (mm-handle-buffer handle))
517           (mm-decode-content-transfer-encoding
518            (mm-handle-encoding handle)
519            (mm-handle-media-type handle))
520           (let ((temp (current-buffer)))
521             (set-buffer cur)
522             (insert-buffer-substring temp)))))))
523
524 (defvar mm-default-directory nil)
525
526 (defun mm-save-part (handle)
527   "Write HANDLE to a file."
528   (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
529          (filename (mail-content-type-get
530                     (mm-handle-disposition handle) 'filename))
531          file)
532     (when filename
533       (setq filename (file-name-nondirectory filename)))
534     (setq file
535           (read-file-name "Save MIME part to: "
536                           (expand-file-name
537                            (or filename name "")
538                            (or mm-default-directory default-directory))))
539     (setq mm-default-directory (file-name-directory file))
540     (when (or (not (file-exists-p file))
541               (yes-or-no-p (format "File %s already exists; overwrite? "
542                                    file)))
543       (mm-save-part-to-file handle file))))
544
545 (defun mm-save-part-to-file (handle file)
546   (mm-with-unibyte-buffer
547     (mm-insert-part handle)
548     ;; Now every coding system is 100% binary within mm-with-unibyte-buffer
549     ;; Is text still special?
550     (let ((coding-system-for-write
551            (if (equal "text" (mm-handle-media-supertype handle))
552                buffer-file-coding-system
553              'binary))
554           ;; Don't re-compress .gz & al.  Arguably we should make
555           ;; `file-name-handler-alist' nil, but that would chop
556           ;; ange-ftp which it's reasonable to use here.
557           (inhibit-file-name-operation 'write-region)
558           (inhibit-file-name-handlers
559            (if (equal (mm-handle-media-type handle)
560                       "application/octet-stream")
561                (cons 'jka-compr-handler inhibit-file-name-handlers)
562              inhibit-file-name-handlers)))
563       (write-region (point-min) (point-max) file))))
564
565 (defun mm-pipe-part (handle)
566   "Pipe HANDLE to a process."
567   (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
568          (command
569           (read-string "Shell command on MIME part: " mm-last-shell-command)))
570     (mm-with-unibyte-buffer
571       (mm-insert-part handle)
572       (shell-command-on-region (point-min) (point-max) command nil))))
573
574 (defun mm-interactively-view-part (handle)
575   "Display HANDLE using METHOD."
576   (let* ((type (mm-handle-media-type handle))
577          (methods
578           (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
579                   (mailcap-mime-info type 'all)))
580          (method (completing-read "Viewer: " methods)))
581     (mm-display-external (copy-sequence handle) method)))
582
583 (defun mm-preferred-alternative (handles &optional preferred)
584   "Say which of HANDLES are preferred."
585   (let ((prec (if preferred (list preferred)
586                 (mm-preferred-alternative-precedence handles)))
587         p h result type handle)
588     (while (setq p (pop prec))
589       (setq h handles)
590       (while h
591         (setq handle (car h))
592         (setq type (mm-handle-media-type handle))
593         (when (and (equal p type)
594                    (mm-automatic-display-p handle)
595                    (or (stringp (car handle))
596                        (not (mm-handle-disposition handle))
597                        (equal (car (mm-handle-disposition handle))
598                               "inline")))
599           (setq result handle
600                 h nil
601                 prec nil))
602         (pop h)))
603     result))
604
605 (defun mm-preferred-alternative-precedence (handles)
606   "Return the precedence based on HANDLES and mm-discouraged-alternatives."
607   (let ((seq (nreverse (mapcar (lambda (h)
608                                  (mm-handle-media-type h))
609                                handles))))
610     (dolist (disc (reverse mm-discouraged-alternatives))
611       (dolist (elem (copy-sequence seq))
612         (when (string-match disc elem)
613           (setq seq (nconc (delete elem seq) (list elem))))))
614     seq))
615
616 (defun mm-get-content-id (id)
617   "Return the handle(s) referred to by ID."
618   (cdr (assoc id mm-content-id-alist)))
619
620 (defun mm-get-image (handle)
621   "Return an image instance based on HANDLE."
622   (let ((type (mm-handle-media-subtype handle))
623         spec)
624     ;; Allow some common translations.
625     (setq type
626           (cond
627            ((equal type "x-pixmap")
628             "xpm")
629            ((equal type "x-xbitmap")
630             "xbm")
631            (t type)))
632     (or (mm-handle-cache handle)
633         (mm-with-unibyte-buffer
634           (mm-insert-part handle)
635           (prog1
636               (setq spec
637                     (ignore-errors
638                       (cond
639                        ((equal type "xbm")
640                         ;; xbm images require special handling, since
641                         ;; the only way to create glyphs from these
642                         ;; (without a ton of work) is to write them
643                         ;; out to a file, and then create a file
644                         ;; specifier.
645                         (let ((file (make-temp-name
646                                      (expand-file-name "emm.xbm"
647                                                        mm-tmp-directory))))
648                           (unwind-protect
649                               (progn
650                                 (write-region (point-min) (point-max) file)
651                                 (make-glyph (list (cons 'x file))))
652                             (ignore-errors
653                               (delete-file file)))))
654                        (t
655                         (make-glyph
656                          (vector (intern type) :data (buffer-string)))))))
657             (mm-handle-set-cache handle spec))))))
658
659 (defun mm-image-fit-p (handle)
660   "Say whether the image in HANDLE will fit the current window."
661   (let ((image (mm-get-image handle)))
662     (or mm-inline-large-images
663         (and (< (glyph-width image) (window-pixel-width))
664              (< (glyph-height image) (window-pixel-height))))))
665
666 (defun mm-valid-image-format-p (format)
667   "Say whether FORMAT can be displayed natively by Emacs."
668   (and (fboundp 'valid-image-instantiator-format-p)
669        (valid-image-instantiator-format-p format)))
670
671 (defun mm-valid-and-fit-image-p (format handle)
672   "Say whether FORMAT can be displayed natively and HANDLE fits the window."
673   (and window-system
674        (mm-valid-image-format-p format)
675        (mm-image-fit-p handle)))
676
677 (provide 'mm-decode)
678
679 ;; mm-decode.el ends here