+ (if (fboundp 'glyph-width)
+ ;; XEmacs' glyphs can actually tell us about their width, so
+ ;; lets be nice and smart about them.
+ (or mm-inline-large-images
+ (and (< (glyph-width image) (window-pixel-width))
+ (< (glyph-height image) (window-pixel-height))))
+ (let* ((size (image-size image))
+ (w (car size))
+ (h (cdr size)))
+ (or mm-inline-large-images
+ (and (< h (1- (window-height))) ; Don't include mode line.
+ (< w (window-width))))))))
+
+(defun mm-valid-image-format-p (format)
+ "Say whether FORMAT can be displayed natively by Emacs."
+ (cond
+ ;; Handle XEmacs
+ ((fboundp 'valid-image-instantiator-format-p)
+ (valid-image-instantiator-format-p format))
+ ;; Handle Emacs 21
+ ((fboundp 'image-type-available-p)
+ (and (display-graphic-p)
+ (image-type-available-p format)))
+ ;; Nobody else can do images yet.
+ (t
+ nil)))
+
+(defun mm-valid-and-fit-image-p (format handle)
+ "Say whether FORMAT can be displayed natively and HANDLE fits the window."
+ (and (mm-valid-image-format-p format)
+ (mm-image-fit-p handle)))
+
+(defun mm-find-part-by-type (handles type &optional notp)
+ (let (handle)
+ (while handles
+ (if (if notp
+ (not (equal (mm-handle-media-type (car handles)) type))
+ (equal (mm-handle-media-type (car handles)) type))
+ (setq handle (car handles)
+ handles nil))
+ (setq handles (cdr handles)))
+ handle))
+
+(defun mm-find-raw-part-by-type (ctl type &optional notp)
+ (goto-char (point-min))
+ (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
+ (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
+ start
+ (end (save-excursion
+ (goto-char (point-max))
+ (if (re-search-backward close-delimiter nil t)
+ (match-beginning 0)
+ (point-max))))
+ result)
+ (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
+ (while (and (not result)
+ (re-search-forward boundary end t))
+ (goto-char (match-beginning 0))
+ (when start
+ (save-excursion
+ (save-restriction
+ (narrow-to-region start (point))
+ (when (let ((ctl (ignore-errors
+ (mail-header-parse-content-type
+ (mail-fetch-field "content-type")))))
+ (if notp
+ (not (equal (car ctl) type))
+ (equal (car ctl) type)))
+ (setq result (buffer-substring (point-min) (point-max)))))))
+ (forward-line 2)
+ (setq start (point)))
+ (when (and (not result) start)
+ (save-excursion
+ (save-restriction
+ (narrow-to-region start end)
+ (when (let ((ctl (ignore-errors
+ (mail-header-parse-content-type
+ (mail-fetch-field "content-type")))))
+ (if notp
+ (not (equal (car ctl) type))
+ (equal (car ctl) type)))
+ (setq result (buffer-substring (point-min) (point-max)))))))
+ result))
+
+(defun mm-possibly-verify-or-decrypt (parts ctl)
+ (let ((subtype (cadr (split-string (car ctl) "/")))
+ protocol func)
+ (cond
+ ((equal subtype "signed")
+ (setq protocol (mail-content-type-get ctl 'protocol))
+ (setq func (cdr (assoc protocol mm-verify-function-alist)))
+ (if (cond
+ ((eq mm-verify-option 'never) nil)
+ ((eq mm-verify-option 'always) t)
+ ((eq mm-verify-option 'known) func)
+ (t (y-or-n-p
+ (format "Verify signed part(protocol=%s)?" protocol))))
+ (condition-case err
+ (save-excursion
+ (if func
+ (funcall func parts ctl)
+ (error (format "Unknown sign protocol(%s)" protocol))))
+ (error
+ (unless (y-or-n-p (format "%s, continue?" err))
+ (error "Verify failure."))))))
+ ((equal subtype "encrypted")
+ (setq protocol (mail-content-type-get ctl 'protocol))
+ (setq func (cdr (assoc protocol mm-decrypt-function-alist)))
+ (if (cond
+ ((eq mm-decrypt-option 'never) nil)
+ ((eq mm-decrypt-option 'always) t)
+ ((eq mm-decrypt-option 'known) func)
+ (t (y-or-n-p
+ (format "Decrypt part (protocol=%s)?" protocol))))
+ (condition-case err
+ (save-excursion
+ (if func
+ (setq parts (funcall func parts ctl))
+ (error (format "Unknown encrypt protocol(%s)" protocol))))
+ (error
+ (unless (y-or-n-p (format "%s, continue?" err))
+ (error "Decrypt failure."))))))
+ (t nil))
+ parts))