+ (search-forward-regexp "^\n" nil t)
+ (setq beg (point))
+ (set-buffer (generate-new-buffer " *mm*"))
+ (insert-buffer-substring obuf beg)
+ (current-buffer))))
+
+(defun mm-display-part (handle &optional no-default)
+ "Display the MIME part represented by HANDLE.
+Returns nil if the part is removed; inline if displayed inline;
+external if displayed external."
+ (save-excursion
+ (mailcap-parse-mailcaps)
+ (if (mm-handle-displayed-p handle)
+ (mm-remove-part handle)
+ (let* ((type (car (mm-handle-type handle)))
+ (method (mailcap-mime-info type)))
+ (if (mm-inlined-p type)
+ (progn
+ (forward-line 1)
+ (mm-display-inline handle)
+ 'inline)
+ (when (or method
+ (not no-default))
+ (if (and (not method)
+ (equal "text" (car (split-string type))))
+ (progn
+ (forward-line 1)
+ (mm-insert-inline handle (mm-get-part handle))
+ 'inline)
+ (mm-display-external
+ handle (or method 'mailcap-save-binary-file))
+ 'external)))))))
+
+(defun mm-display-external (handle method)
+ "Display HANDLE using METHOD."
+ (mm-with-unibyte-buffer
+ (if (functionp method)
+ (let ((cur (current-buffer)))
+ (if (eq method 'mailcap-save-binary-file)
+ (progn
+ (set-buffer (generate-new-buffer "*mm*"))
+ (setq method nil))
+ (mm-insert-part handle)
+ (let ((win (get-buffer-window cur t)))
+ (when win
+ (select-window win)))
+ (switch-to-buffer (generate-new-buffer "*mm*")))
+ (buffer-disable-undo)
+ (mm-set-buffer-file-coding-system mm-binary-coding-system)
+ (insert-buffer-substring cur)
+ (message "Viewing with %s" method)
+ (let ((mm (current-buffer))
+ (non-viewer (assoc "non-viewer"
+ (mailcap-mime-info
+ (car (mm-handle-type handle)) t))))
+ (unwind-protect
+ (if method
+ (funcall method)
+ (mm-save-part handle))
+ (when (and (not non-viewer)
+ method)
+ (mm-handle-set-undisplayer handle mm)))))
+ ;; The function is a string to be executed.
+ (mm-insert-part handle)
+ (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
+ (filename (mail-content-type-get
+ (mm-handle-disposition handle) 'filename))
+ (needsterm (assoc "needsterm"
+ (mailcap-mime-info
+ (car (mm-handle-type handle)) t)))
+ process file buffer)
+ ;; We create a private sub-directory where we store our files.
+ (make-directory dir)
+ (set-file-modes dir 448)
+ (if filename
+ (setq file (expand-file-name (file-name-nondirectory filename)
+ dir))
+ (setq file (make-temp-name (expand-file-name "mm." dir))))
+ (write-region (point-min) (point-max) file nil 'nomesg)
+ (message "Viewing with %s" method)
+ (unwind-protect
+ (setq process
+ (if needsterm
+ (start-process "*display*" nil
+ "xterm"
+ "-e" shell-file-name "-c"
+ (format method
+ (mm-quote-arg file)))
+ (start-process "*display*"
+ (setq buffer (generate-new-buffer "*mm*"))
+ shell-file-name
+ "-c" (format method
+ (mm-quote-arg file)))))
+ (mm-handle-set-undisplayer handle (cons file buffer)))
+ (message "Displaying %s..." (format method file))))))
+
+(defun mm-remove-parts (handles)
+ "Remove the displayed MIME parts represented by HANDLE."
+ (if (and (listp handles)
+ (bufferp (car handles)))
+ (mm-remove-part handles)
+ (let (handle)
+ (while (setq handle (pop handles))
+ (cond
+ ((stringp handle)
+ )
+ ((and (listp handle)
+ (stringp (car handle)))
+ (mm-remove-parts (cdr handle)))
+ (t
+ (mm-remove-part handle)))))))
+
+(defun mm-destroy-parts (handles)
+ "Remove the displayed MIME parts represented by HANDLE."
+ (if (and (listp handles)
+ (bufferp (car handles)))
+ (mm-destroy-part handles)
+ (let (handle)
+ (while (setq handle (pop handles))
+ (cond
+ ((stringp handle)
+ )
+ ((and (listp handle)
+ (stringp (car handle)))
+ (mm-destroy-parts (cdr handle)))
+ (t
+ (mm-destroy-part handle)))))))
+
+(defun mm-remove-part (handle)
+ "Remove the displayed MIME part represented by HANDLE."
+ (when (listp handle)
+ (let ((object (mm-handle-undisplayer handle)))
+ (ignore-errors
+ (cond
+ ;; Internally displayed part.
+ ((mm-annotationp object)
+ (delete-annotation object))
+ ((or (functionp object)
+ (and (listp object)
+ (eq (car object) 'lambda)))
+ (funcall object))
+ ;; Externally displayed part.
+ ((consp object)
+ (ignore-errors (delete-file (car object)))
+ (ignore-errors (delete-directory (file-name-directory (car object))))
+ (ignore-errors (kill-buffer (cdr object))))
+ ((bufferp object)
+ (when (buffer-live-p object)
+ (kill-buffer object)))))
+ (mm-handle-set-undisplayer handle nil))))
+
+(defun mm-display-inline (handle)
+ (let* ((type (car (mm-handle-type handle)))
+ (function (cadr (assoc type mm-inline-media-tests))))
+ (funcall function handle)
+ (goto-char (point-min))))
+
+(defun mm-inlinable-p (type)
+ "Say whether TYPE can be displayed inline."
+ (let ((alist mm-inline-media-tests)
+ test)
+ (while alist
+ (when (equal type (caar alist))
+ (setq test (caddar alist)
+ alist nil)
+ (setq test (eval test)))
+ (pop alist))
+ test))
+
+(defun mm-automatic-display-p (type)
+ "Say whether the user wants TYPE to be displayed automatically."
+ (let ((methods mm-automatic-display)
+ method result)
+ (while (setq method (pop methods))
+ (when (and (string-match method type)
+ (mm-inlinable-p type))
+ (setq result t
+ methods nil)))
+ result))
+
+(defun mm-inlined-p (type)
+ "Say whether the user wants TYPE to be displayed automatically."
+ (let ((methods mm-inlined-types)
+ method result)
+ (while (setq method (pop methods))
+ (when (and (string-match method type)
+ (mm-inlinable-p type))
+ (setq result t
+ methods nil)))
+ result))
+
+(defun mm-attachment-override-p (type)
+ "Say whether TYPE should have attachment behavior overridden."
+ (let ((types mm-attachment-override-types)
+ ty)
+ (catch 'found
+ (while (setq ty (pop types))
+ (when (and (string-match ty type)
+ (mm-inlinable-p type))
+ (throw 'found t))))))
+
+(defun mm-automatic-external-display-p (type)
+ "Return the user-defined method for TYPE."
+ (let ((methods mm-automatic-external-display)
+ method result)
+ (while (setq method (pop methods))
+ (when (string-match method type)
+ (setq result t
+ methods nil)))
+ result))
+
+(defun mm-destroy-part (handle)
+ "Destroy the data structures connected to HANDLE."
+ (when (listp handle)
+ (mm-remove-part handle)
+ (when (buffer-live-p (mm-handle-buffer handle))
+ (kill-buffer (mm-handle-buffer handle)))))
+
+(defun mm-handle-displayed-p (handle)
+ "Say whether HANDLE is displayed or not."
+ (mm-handle-undisplayer handle))
+
+;;;
+;;; Functions for outputting parts
+;;;
+
+(defun mm-get-part (handle)
+ "Return the contents of HANDLE as a string."
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ (buffer-string)))
+
+(defun mm-insert-part (handle)
+ "Insert the contents of HANDLE in the current buffer."
+ (let ((cur (current-buffer)))
+ (save-excursion
+ (if (member (car (split-string (car (mm-handle-type handle)) "/"))
+ '("text" "message"))
+ (with-temp-buffer
+ (insert-buffer-substring (mm-handle-buffer handle))
+ (mm-decode-content-transfer-encoding
+ (mm-handle-encoding handle)
+ (car (mm-handle-type handle)))
+ (let ((temp (current-buffer)))
+ (set-buffer cur)
+ (insert-buffer-substring temp)))
+ (mm-with-unibyte-buffer
+ (insert-buffer-substring (mm-handle-buffer handle))
+ (mm-decode-content-transfer-encoding
+ (mm-handle-encoding handle)
+ (car (mm-handle-type handle)))
+ (let ((temp (current-buffer)))
+ (set-buffer cur)
+ (insert-buffer-substring temp)))))))
+
+(defvar mm-default-directory nil)
+
+(defun mm-save-part (handle)
+ "Write HANDLE to a file."
+ (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
+ (filename (mail-content-type-get
+ (mm-handle-disposition handle) 'filename))
+ file)
+ (when filename
+ (setq filename (file-name-nondirectory filename)))
+ (setq file
+ (read-file-name "Save MIME part to: "
+ (expand-file-name
+ (or filename name "")
+ (or mm-default-directory default-directory))))
+ (setq mm-default-directory (file-name-directory file))
+ (when (or (not (file-exists-p file))
+ (yes-or-no-p (format "File %s already exists; overwrite? "
+ file)))
+ (mm-save-part-to-file handle file))))
+
+(defun mm-save-part-to-file (handle file)
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ ;; Now every coding system is 100% binary within mm-with-unibyte-buffer
+ ;; Is text still special?
+ (let ((coding-system-for-write
+ (if (equal "text" (car (split-string
+ (car (mm-handle-type handle)) "/")))
+ buffer-file-coding-system
+ 'binary))
+ ;; Don't re-compress .gz & al. Arguably we should make
+ ;; `file-name-handler-alist' nil, but that would chop
+ ;; ange-ftp which it's reasonable to use here.
+ (inhibit-file-name-operation 'write-region)
+ (inhibit-file-name-handlers
+ (if (equal (car (mm-handle-type handle))
+ "application/octet-stream")
+ (cons 'jka-compr-handler inhibit-file-name-handlers)
+ inhibit-file-name-handlers)))
+ (write-region (point-min) (point-max) file))))
+
+(defun mm-pipe-part (handle)
+ "Pipe HANDLE to a process."
+ (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
+ (command
+ (read-string "Shell command on MIME part: " mm-last-shell-command)))
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ (shell-command-on-region (point-min) (point-max) command nil))))
+
+(defun mm-interactively-view-part (handle)
+ "Display HANDLE using METHOD."
+ (let* ((type (car (mm-handle-type handle)))
+ (methods
+ (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
+ (mailcap-mime-info type 'all)))
+ (method (completing-read "Viewer: " methods)))
+ (mm-display-external (copy-sequence handle) method)))
+
+(defun mm-preferred-alternative (handles &optional preferred)
+ "Say which of HANDLES are preferred."
+ (let ((prec (if preferred (list preferred)
+ (mm-preferred-alternative-precedence handles)))
+ p h result type handle)
+ (while (setq p (pop prec))
+ (setq h handles)
+ (while h
+ (setq type
+ (if (stringp (caar h))
+ (caar h)
+ (car (mm-handle-type (car h)))))
+ (setq handle (car h))
+ (when (and (equal p type)
+ (mm-automatic-display-p type)
+ (or (stringp (caar h))
+ (not (mm-handle-disposition (car h)))
+ (equal (car (mm-handle-disposition (car h)))
+ "inline")))
+ (setq result (car h)
+ h nil
+ prec nil))
+ (pop h)))
+ result))
+
+(defun mm-preferred-alternative-precedence (handles)
+ "Return the precedence based on HANDLES and mm-discouraged-alternatives."
+ (let ((seq (nreverse (mapcar (lambda (h)
+ (car (mm-handle-type h))) handles))))
+ (dolist (disc (reverse mm-discouraged-alternatives))
+ (dolist (elem (copy-sequence seq))
+ (when (string-match disc elem)
+ (setq seq (nconc (delete elem seq) (list elem))))))
+ seq))
+
+(defun mm-get-content-id (id)
+ "Return the handle(s) referred to by ID."
+ (cdr (assoc id mm-content-id-alist)))
+
+(defun mm-get-image (handle)
+ "Return an image instance based on HANDLE."
+ (let ((type (cadr (split-string (car (mm-handle-type handle)) "/")))
+ spec)
+ ;; Allow some common translations.
+ (setq type
+ (cond
+ ((equal type "x-pixmap")
+ "xpm")
+ ((equal type "x-xbitmap")
+ "xbm")
+ (t type)))
+ (or (mm-handle-cache handle)
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ (prog1
+ (setq spec
+ (ignore-errors
+ (cond
+ ((equal type "xbm")
+ ;; xbm images require special handling, since
+ ;; the only way to create glyphs from these
+ ;; (without a ton of work) is to write them
+ ;; out to a file, and then create a file
+ ;; specifier.
+ (let ((file (make-temp-name
+ (expand-file-name "emm.xbm"
+ mm-tmp-directory))))
+ (unwind-protect
+ (progn
+ (write-region (point-min) (point-max) file)
+ (make-glyph (list (cons 'x file))))
+ (ignore-errors
+ (delete-file file)))))
+ (t
+ (make-glyph
+ (vector (intern type) :data (buffer-string)))))))
+ (mm-handle-set-cache handle spec))))))
+
+(defun mm-image-fit-p (handle)
+ "Say whether the image in HANDLE will fit the current window."
+ (let ((image (mm-get-image handle)))
+ (or mm-all-images-fit
+ (and (< (glyph-width image) (window-pixel-width))
+ (< (glyph-height image) (window-pixel-height))))))
+
+(defun mm-valid-image-format-p (format)
+ "Say whether FORMAT can be displayed natively by Emacs."
+ (and (fboundp 'valid-image-instantiator-format-p)
+ (valid-image-instantiator-format-p format)))
+
+(defun mm-valid-and-fit-image-p (format handle)
+ "Say whether FORMAT can be displayed natively and HANDLE fits the window."
+ (and window-system
+ (mm-valid-image-format-p format)
+ (mm-image-fit-p handle)))