+(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 (mm-handle-media-type handle))
+ (method (mailcap-mime-info type))
+ (filename (or (mail-content-type-get
+ (mm-handle-disposition handle) 'filename)
+ (mail-content-type-get
+ (mm-handle-type handle) 'name)
+ "<file>"))
+ (external mm-enable-external))
+ (if (and (mm-inlinable-p handle)
+ (mm-inlined-p handle))
+ (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)
+ (if (and method ;; If nil, we always use "save".
+ (stringp method) ;; 'mailcap-save-binary-file
+ (or (eq mm-enable-external t)
+ (and (eq mm-enable-external 'ask)
+ (y-or-n-p
+ (concat
+ "Display part (" type
+ ") using external program"
+ ;; Can non-string method ever happen?
+ (if (stringp method)
+ (concat
+ " \"" (format method filename) "\"")
+ "")
+ "? ")))))
+ (setq external t)
+ (setq external nil))
+ (if external
+ (mm-display-external
+ handle (or method 'mailcap-save-binary-file))
+ (mm-display-external
+ handle 'mailcap-save-binary-file)))))))))
+
+(defun mm-display-external (handle method)
+ "Display HANDLE using METHOD."
+ (let ((outbuf (current-buffer)))
+ (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)
+ (goto-char (point-min))
+ (when method
+ (message "Viewing with %s" method))
+ (let ((mm (current-buffer))
+ (non-viewer (assq 'non-viewer
+ (mailcap-mime-info
+ (mm-handle-media-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 (mm-make-temp-file
+ (expand-file-name "emm." mm-tmp-directory) 'dir))
+ (filename (or
+ (mail-content-type-get
+ (mm-handle-disposition handle) 'filename)
+ (mail-content-type-get
+ (mm-handle-type handle) 'name)))
+ (mime-info (mailcap-mime-info
+ (mm-handle-media-type handle) t))
+ (needsterm (or (assoc "needsterm" mime-info)
+ (assoc "needsterminal" mime-info)))
+ (copiousoutput (assoc "copiousoutput" mime-info))
+ file buffer)
+ ;; We create a private sub-directory where we store our files.
+ (set-file-modes dir 448)
+ (if filename
+ (setq file (expand-file-name
+ (gnus-map-function mm-file-name-rewrite-functions
+ (file-name-nondirectory filename))
+ dir))
+ (setq file (mm-make-temp-file (expand-file-name "mm." dir))))
+ (let ((coding-system-for-write mm-binary-coding-system))
+ (write-region (point-min) (point-max) file nil 'nomesg))
+ (message "Viewing with %s" method)
+ (cond
+ (needsterm
+ (let ((command (mm-mailcap-command
+ method file (mm-handle-type handle))))
+ (unwind-protect
+ (if window-system
+ (start-process "*display*" nil
+ mm-external-terminal-program
+ "-e" shell-file-name
+ shell-command-switch command)
+ (require 'term)
+ (require 'gnus-win)
+ (set-buffer
+ (setq buffer
+ (make-term "display"
+ shell-file-name
+ nil
+ shell-command-switch command)))
+ (term-mode)
+ (term-char-mode)
+ (set-process-sentinel
+ (get-buffer-process buffer)
+ `(lambda (process state)
+ (if (eq 'exit (process-status process))
+ (gnus-configure-windows
+ ',gnus-current-window-configuration))))
+ (gnus-configure-windows 'display-term))
+ (mm-handle-set-external-undisplayer handle (cons file buffer)))
+ (message "Displaying %s..." command))
+ 'external)
+ (copiousoutput
+ (with-current-buffer outbuf
+ (forward-line 1)
+ (mm-insert-inline
+ handle
+ (unwind-protect
+ (progn
+ (call-process shell-file-name nil
+ (setq buffer
+ (generate-new-buffer " *mm*"))
+ nil
+ shell-command-switch
+ (mm-mailcap-command
+ method file (mm-handle-type handle)))
+ (if (buffer-live-p buffer)
+ (save-excursion
+ (set-buffer buffer)
+ (buffer-string))))
+ (progn
+ (ignore-errors (delete-file file))
+ (ignore-errors (delete-directory
+ (file-name-directory file)))
+ (ignore-errors (kill-buffer buffer))))))
+ 'inline)
+ (t
+ (let ((command (mm-mailcap-command
+ method file (mm-handle-type handle))))
+ (unwind-protect
+ (start-process "*display*"
+ (setq buffer
+ (generate-new-buffer " *mm*"))
+ shell-file-name
+ shell-command-switch command)
+ (mm-handle-set-external-undisplayer
+ handle (cons file buffer)))
+ (message "Displaying %s..." command))
+ 'external)))))))
+
+(defun mm-mailcap-command (method file type-list)
+ (let ((ctl (cdr type-list))
+ (beg 0)
+ (uses-stdin t)
+ out sub total)
+ (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
+ method beg)
+ (push (substring method beg (match-beginning 0)) out)
+ (setq beg (match-end 0)
+ total (match-string 0 method)
+ sub (match-string 1 method))
+ (cond
+ ((string= total "%%")
+ (push "%" out))
+ ((or (string= total "%s")
+ ;; We do our own quoting.
+ (string= total "'%s'")
+ (string= total "\"%s\""))
+ (setq uses-stdin nil)
+ (push (shell-quote-argument
+ (gnus-map-function mm-path-name-rewrite-functions file)) out))
+ ((string= total "%t")
+ (push (shell-quote-argument (car type-list)) out))
+ (t
+ (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
+ (push (substring method beg (length method)) out)
+ (when uses-stdin
+ (push "<" out)
+ (push (shell-quote-argument
+ (gnus-map-function mm-path-name-rewrite-functions file))
+ out))
+ (mapconcat 'identity (nreverse out) "")))
+
+(defun mm-remove-parts (handles)
+ "Remove the displayed MIME parts represented by HANDLES."
+ (if (and (listp handles)
+ (bufferp (car handles)))
+ (mm-remove-part handles)
+ (let (handle)
+ (while (setq handle (pop handles))
+ (cond
+ ((stringp handle)
+ (when (buffer-live-p (get-text-property 0 'buffer handle))
+ (kill-buffer (get-text-property 0 'buffer 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 HANDLES."
+ (if (and (listp handles)
+ (bufferp (car handles)))
+ (mm-destroy-part handles)
+ (let (handle)
+ (while (setq handle (pop handles))
+ (cond
+ ((stringp handle)
+ (when (buffer-live-p (get-text-property 0 'buffer handle))
+ (kill-buffer (get-text-property 0 'buffer handle))))
+ ((and (listp handle)
+ (stringp (car handle)))
+ (mm-destroy-parts 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)
+ (condition-case ()
+ (while (get-buffer-process (cdr object))
+ (interrupt-process (get-buffer-process (cdr object)))
+ (message "Waiting for external displayer to die...")
+ (sit-for 1))
+ (quit)
+ (error))
+ (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
+ (message "Waiting for external displayer to die...done")
+ (ignore-errors (delete-file (car object)))
+ (ignore-errors (delete-directory (file-name-directory
+ (car object)))))
+ ((bufferp object)
+ (when (buffer-live-p object)
+ (kill-buffer object)))))
+ (mm-handle-set-undisplayer handle nil))))
+
+(defun mm-display-inline (handle)
+ (let* ((type (mm-handle-media-type handle))
+ (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
+ (funcall function handle)
+ (goto-char (point-min))))
+
+(defun mm-assoc-string-match (alist type)
+ (dolist (elem alist)
+ (when (string-match (car elem) type)
+ (return elem))))
+
+(defun mm-automatic-display-p (handle)
+ "Say whether the user wants HANDLE to be displayed automatically."
+ (let ((methods mm-automatic-display)
+ (type (mm-handle-media-type handle))
+ method result)
+ (while (setq method (pop methods))
+ (when (and (not (mm-inline-override-p handle))
+ (string-match method type))
+ (setq result t
+ methods nil)))
+ result))
+
+(defun mm-inlinable-p (handle)
+ "Say whether HANDLE can be displayed inline."
+ (let ((alist mm-inline-media-tests)
+ (type (mm-handle-media-type handle))
+ test)
+ (while alist
+ (when (string-match (caar alist) type)
+ (setq test (caddar alist)
+ alist nil)
+ (setq test (funcall test handle)))
+ (pop alist))
+ test))
+
+(defun mm-inlined-p (handle)
+ "Say whether the user wants HANDLE to be displayed inline."
+ (let ((methods mm-inlined-types)
+ (type (mm-handle-media-type handle))
+ method result)
+ (while (setq method (pop methods))
+ (when (and (not (mm-inline-override-p handle))
+ (string-match method type))
+ (setq result t
+ methods nil)))
+ result))
+
+(defun mm-attachment-override-p (handle)
+ "Say whether HANDLE should have attachment behavior overridden."
+ (let ((types mm-attachment-override-types)
+ (type (mm-handle-media-type handle))
+ ty)
+ (catch 'found
+ (while (setq ty (pop types))
+ (when (and (string-match ty type)
+ (mm-inlinable-p handle))
+ (throw 'found t))))))
+
+(defun mm-inline-override-p (handle)
+ "Say whether HANDLE should have inline behavior overridden."
+ (let ((types mm-inline-override-types)
+ (type (mm-handle-media-type handle))
+ ty)
+ (catch 'found
+ (while (setq ty (pop types))
+ (when (string-match ty 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
+ (insert (with-current-buffer (mm-handle-buffer handle)
+ (mm-with-unibyte-current-buffer
+ (buffer-string))))
+ (mm-decode-content-transfer-encoding
+ (mm-handle-encoding handle)
+ (mm-handle-media-type 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 (mm-handle-media-supertype handle) '("text" "message"))
+ (with-temp-buffer
+ (insert-buffer-substring (mm-handle-buffer handle))
+ (prog1
+ (mm-decode-content-transfer-encoding
+ (mm-handle-encoding handle)
+ (mm-handle-media-type handle))
+ (let ((temp (current-buffer)))
+ (set-buffer cur)
+ (insert-buffer-substring temp))))
+ (mm-with-unibyte-buffer
+ (insert-buffer-substring (mm-handle-buffer handle))
+ (prog1
+ (mm-decode-content-transfer-encoding
+ (mm-handle-encoding handle)
+ (mm-handle-media-type handle))
+ (let ((temp (current-buffer)))
+ (set-buffer cur)
+ (insert-buffer-substring temp))))))))
+
+(defun mm-file-name-delete-whitespace (file-name)
+ "Remove all whitespace characters from FILE-NAME."
+ (while (string-match "\\s-+" file-name)
+ (setq file-name (replace-match "" t t file-name)))
+ file-name)
+
+(defun mm-file-name-trim-whitespace (file-name)
+ "Remove leading and trailing whitespace characters from FILE-NAME."
+ (when (string-match "\\`\\s-+" file-name)
+ (setq file-name (substring file-name (match-end 0))))
+ (when (string-match "\\s-+\\'" file-name)
+ (setq file-name (substring file-name 0 (match-beginning 0))))
+ file-name)
+
+(defun mm-file-name-collapse-whitespace (file-name)
+ "Collapse multiple whitespace characters in FILE-NAME."
+ (while (string-match "\\s-\\s-+" file-name)
+ (setq file-name (replace-match " " t t file-name)))
+ file-name)
+
+(defun mm-file-name-replace-whitespace (file-name)
+ "Replace whitespace characters in FILE-NAME with underscores.
+Set the option `mm-file-name-replace-whitespace' to any other
+string if you do not like underscores."
+ (let ((s (or mm-file-name-replace-whitespace "_")))
+ (while (string-match "\\s-" file-name)
+ (setq file-name (replace-match s t t file-name))))
+ file-name)
+
+(defun mm-file-name-delete-control (filename)
+ "Delete control characters from FILENAME."
+ (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
+
+(defun mm-file-name-delete-gotchas (filename)
+ "Delete shell gotchas from FILENAME."
+ (setq filename (gnus-replace-in-string filename "[<>|]" ""))
+ (gnus-replace-in-string filename "^[.-]+" ""))
+
+(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 (gnus-map-function mm-file-name-rewrite-functions
+ (file-name-nondirectory filename))))
+ (setq file
+ (mm-with-multibyte
+ (read-file-name "Save MIME part to: "
+ (or mm-default-directory default-directory)
+ nil nil (or filename name ""))))
+ (setq mm-default-directory (file-name-directory file))
+ (and (or (not (file-exists-p file))
+ (yes-or-no-p (format "File %s already exists; overwrite? "
+ file)))
+ (progn
+ (mm-save-part-to-file handle file)
+ file))))
+
+(defun mm-save-part-to-file (handle file)
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ (let ((coding-system-for-write 'binary)
+ (current-file-modes (default-file-modes))
+ ;; Don't re-compress .gz & al. Arguably we should make
+ ;; `file-name-handler-alist' nil, but that would chop
+ ;; ange-ftp, which is reasonable to use here.
+ (inhibit-file-name-operation 'write-region)
+ (inhibit-file-name-handlers
+ (cons 'jka-compr-handler inhibit-file-name-handlers)))
+ (set-default-file-modes mm-attachment-file-modes)
+ (unwind-protect
+ (write-region (point-min) (point-max) file)
+ (set-default-file-modes current-file-modes)))))
+
+(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)
+ (let ((coding-system-for-write 'binary))
+ (shell-command-on-region (point-min) (point-max) command nil)))))
+
+(defun mm-interactively-view-part (handle)
+ "Display HANDLE using METHOD."
+ (let* ((type (mm-handle-media-type handle))
+ (methods
+ (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
+ (mailcap-mime-info type 'all)))
+ (method (let ((minibuffer-local-completion-map
+ mm-viewer-completion-map))
+ (completing-read "Viewer: " methods))))
+ (when (string= method "")
+ (error "No method given"))
+ (if (string-match "^[^% \t]+$" method)
+ (setq method (concat method " %s")))
+ (mm-display-external 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 handle (car h))
+ (setq type (mm-handle-media-type handle))
+ (when (and (equal p type)
+ (mm-automatic-display-p handle)
+ (or (stringp (car handle))
+ (not (mm-handle-disposition handle))
+ (equal (car (mm-handle-disposition handle))
+ "inline")))
+ (setq result handle
+ 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 #'mm-handle-media-type
+ 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)))
+
+(defconst mm-image-type-regexps
+ '(("/\\*.*XPM.\\*/" . xpm)
+ ("P[1-6]" . pbm)
+ ("GIF8" . gif)
+ ("\377\330" . jpeg)
+ ("\211PNG\r\n" . png)
+ ("#define" . xbm)
+ ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
+ ("%!PS" . postscript))
+ "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
+When the first bytes of an image file match REGEXP, it is assumed to
+be of image type IMAGE-TYPE.")
+
+;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
+(defun mm-image-type-from-buffer ()
+ "Determine the image type from data in the current buffer.
+Value is a symbol specifying the image type or nil if type cannot
+be determined."
+ (let ((types mm-image-type-regexps)
+ type)
+ (goto-char (point-min))
+ (while (and types (null type))
+ (let ((regexp (car (car types)))
+ (image-type (cdr (car types))))
+ (when (looking-at regexp)
+ (setq type image-type))
+ (setq types (cdr types))))
+ type))
+
+(defun mm-get-image (handle)
+ "Return an image instance based on HANDLE."
+ (let ((type (mm-handle-media-subtype handle))
+ spec)
+ ;; Allow some common translations.
+ (setq type
+ (cond
+ ((equal type "x-pixmap")
+ "xpm")
+ ((equal type "x-xbitmap")
+ "xbm")
+ ((equal type "x-portable-bitmap")
+ "pbm")
+ (t type)))
+ (or (mm-handle-cache handle)
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ (prog1
+ (setq spec
+ (ignore-errors
+ ;; Avoid testing `make-glyph' since W3 may define
+ ;; a bogus version of it.
+ (if (fboundp 'create-image)
+ (create-image (buffer-string)
+ (or (mm-image-type-from-buffer)
+ (intern type))
+ 'data-p)
+ (mm-create-image-xemacs type))))
+ (mm-handle-set-cache handle spec))))))
+
+(defun mm-create-image-xemacs (type)