+(defvar tm-vm/yank:message-to-restore nil
+ "For internal use by tm-vm only.")
+
+(defun vm-yank-message (&optional message)
+ "Yank message number N into the current buffer at point.
+When called interactively N is always read from the minibuffer. When
+called non-interactively the first argument is expected to be a
+message struct.
+
+This function originally provided by vm-reply has been patched for TM
+in order to provide better citation of MIME messages : if a MIME
+Preview buffer exists for the message then its contents are inserted
+instead of the raw message.
+
+This command is meant to be used in VM created Mail mode buffers; the
+yanked message comes from the mail buffer containing the message you
+are replying to, forwarding, or invoked VM's mail command from.
+
+All message headers are yanked along with the text. Point is
+left before the inserted text, the mark after. Any hook
+functions bound to mail-citation-hook are run, after inserting
+the text and setting point and mark. For backward compatibility,
+if mail-citation-hook is set to nil, `mail-yank-hooks' is run
+instead.
+
+If mail-citation-hook and mail-yank-hooks are both nil, this
+default action is taken: the yanked headers are trimmed as
+specified by vm-included-text-headers and
+vm-included-text-discard-header-regexp, and the value of
+vm-included-text-prefix is prepended to every yanked line."
+ (interactive
+ (list
+ ;; What we really want for the first argument is a message struct,
+ ;; but if called interactively, we let the user type in a message
+ ;; number instead.
+ (let (mp default
+ (result 0)
+ prompt
+ (last-command last-command)
+ (this-command this-command))
+ (if (bufferp vm-mail-buffer)
+ (save-excursion
+ (vm-select-folder-buffer)
+ (setq default (and vm-message-pointer
+ (vm-number-of (car vm-message-pointer)))
+ prompt (if default
+ (format "Yank message number: (default %s) "
+ default)
+ "Yank message number: "))
+ (while (zerop result)
+ (setq result (read-string prompt))
+ (and (string= result "") default (setq result default))
+ (setq result (string-to-int result)))
+ (if (null (setq mp (nthcdr (1- result) vm-message-list)))
+ (error "No such message."))
+ (setq tm-vm/yank:message-to-restore (string-to-int default))
+ (save-selected-window
+ (vm-goto-message result))
+ (car mp))
+ nil))))
+ (if (null message)
+ (if mail-reply-buffer
+ (tm-vm/yank-content)
+ (error "This is not a VM Mail mode buffer."))
+ (if (null (buffer-name vm-mail-buffer))
+ (error "The folder buffer containing message %d has been killed."
+ (vm-number-of message)))
+ (vm-display nil nil '(vm-yank-message)
+ '(vm-yank-message composing-message))
+ (let ((b (current-buffer)) (start (point)) end)
+ (save-restriction
+ (widen)
+ (save-excursion
+ (set-buffer (vm-buffer-of message))
+ (let* ((mbuf (current-buffer))
+ (pbuf (and mime::article/preview-buffer
+ ; is there a preview buffer alive ?
+ (get-buffer mime::article/preview-buffer)
+ ; rebuild preview to ensure it
+ ; corresponds to the current message
+ (save-excursion
+ (save-selected-window
+ (save-window-excursion
+ (tm-vm/view-message))))
+ (get-buffer mime::article/preview-buffer))))
+ (if pbuf
+ (if running-xemacs
+ (let ((tmp (generate-new-buffer "tm-vm/tmp")))
+ (set-buffer pbuf)
+ (append-to-buffer tmp (point-min) (point-max))
+ (set-buffer tmp)
+ (map-extents
+ '(lambda (ext maparg)
+ (set-extent-property ext 'begin-glyph nil)))
+ (append-to-buffer b (point-min) (point-max))
+ (setq end (vm-marker
+ (+ start (length (buffer-string))) b))
+ (kill-buffer tmp))
+ (set-buffer pbuf)
+ (append-to-buffer b (point-min) (point-max))
+ (setq end (vm-marker
+ (+ start (length (buffer-string))) b)))
+ (save-restriction
+ (setq message (vm-real-message-of message))
+ (set-buffer (vm-buffer-of message))
+ (widen)
+ (append-to-buffer
+ b (vm-headers-of message) (vm-text-end-of message))
+ (setq end
+ (vm-marker (+ start (- (vm-text-end-of message)
+ (vm-headers-of message))) b))))))
+ (push-mark end)
+ (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
+ (mail-yank-hooks (run-hooks 'mail-yank-hooks))
+ (t (vm-mail-yank-default message)))
+ ))
+ (if tm-vm/yank:message-to-restore
+ (save-selected-window
+ (vm-goto-message tm-vm/yank:message-to-restore)
+ (setq tm-vm/yank:message-to-restore nil)))
+ ))
+
+\f
+;;; @ for tm-view
+;;;
+
+;;; based on vm-do-reply [vm-reply.el]
+(defun tm-vm/do-reply (buf to-all include-text)
+ (save-excursion
+ (set-buffer buf)
+ (let ((dir default-directory)
+ to cc subject mp in-reply-to references newsgroups)
+ (cond ((setq to
+ (let ((reply-to (std11-field-body "Reply-To")))
+ (if (vm-ignored-reply-to reply-to)
+ nil
+ reply-to))))
+ ((setq to (std11-field-body "From")))
+ ;; (t (error "No From: or Reply-To: header in message"))
+ )
+ (if to-all
+ (setq cc (delq nil (cons cc (std11-field-bodies '("To" "Cc"))))
+ cc (mapconcat 'identity cc ","))
+ )
+ (setq subject (std11-field-body "Subject"))
+ (and subject vm-reply-subject-prefix
+ (let ((case-fold-search t))
+ (not
+ (equal
+ (string-match (regexp-quote vm-reply-subject-prefix)
+ subject)
+ 0)))
+ (setq subject (concat vm-reply-subject-prefix subject)))
+ (setq in-reply-to (std11-field-body "Message-Id")
+ references (nconc
+ (std11-field-bodies '("References" "In-Reply-To"))
+ (list in-reply-to))
+ newsgroups (list (or (and to-all
+ (std11-field-body "Followup-To"))
+ (std11-field-body "Newsgroups"))))
+ (setq to (vm-parse-addresses to)
+ cc (vm-parse-addresses cc))
+ (if vm-reply-ignored-addresses
+ (setq to (vm-strip-ignored-addresses to)
+ cc (vm-strip-ignored-addresses cc)))
+ (setq to (vm-delete-duplicates to nil t))
+ (setq cc (vm-delete-duplicates
+ (append (vm-delete-duplicates cc nil t)
+ to (copy-sequence to))
+ t t))
+ (and to (setq to (mapconcat 'identity to ",\n ")))
+ (and cc (setq cc (mapconcat 'identity cc ",\n ")))
+ (and (null to) (setq to cc cc nil))
+ (setq references (delq nil references)
+ references (mapconcat 'identity references " ")
+ references (vm-parse references "[^<]*\\(<[^>]+>\\)")
+ references (vm-delete-duplicates references)
+ references (if references (mapconcat 'identity references "\n\t")))
+ (setq newsgroups (delq nil newsgroups)
+ newsgroups (mapconcat 'identity newsgroups ",")
+ newsgroups (vm-parse newsgroups "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)")
+ newsgroups (vm-delete-duplicates newsgroups)
+ newsgroups (if newsgroups (mapconcat 'identity newsgroups ",")))
+ (vm-mail-internal
+ (if to
+ (format "reply to %s%s"
+ (std11-full-name-string
+ (car (std11-parse-address-string to)))
+ (if cc ", ..." "")))
+ to subject in-reply-to cc references newsgroups)
+ (setq mail-reply-buffer buf
+ ;; vm-system-state 'replying
+ default-directory dir))
+ (if include-text
+ (save-excursion
+ (goto-char (point-min))
+ (let ((case-fold-search nil))
+ (re-search-forward
+ (concat "^" (regexp-quote mail-header-separator) "$") nil 0))
+ (forward-char 1)
+ (tm-vm/yank-content)))
+ (run-hooks 'vm-reply-hook)
+ (run-hooks 'vm-mail-mode-hook)
+ ))
+
+(defun tm-vm/following-method (buf)
+ (tm-vm/do-reply buf 'to-all 'include-text)
+ )
+
+(defun tm-vm/yank-content ()
+ (interactive)
+ (let ((this-command 'vm-yank-message))
+ (vm-display nil nil '(vm-yank-message)
+ '(vm-yank-message composing-message))
+ (save-restriction
+ (narrow-to-region (point)(point))
+ (insert-buffer mail-reply-buffer)
+ (goto-char (point-max))
+ (push-mark)
+ (goto-char (point-min)))
+ (cond (mail-citation-hook (run-hooks 'mail-citation-hook))
+ (mail-yank-hooks (run-hooks 'mail-yank-hooks))
+ (t (mail-indent-citation)))
+ ))
+
+(set-alist 'mime-viewer/following-method-alist
+ 'vm-mode
+ (function tm-vm/following-method))
+(set-alist 'mime-viewer/following-method-alist
+ 'vm-virtual-mode
+ (function tm-vm/following-method))
+
+
+(defun tm-vm/quit-view-message ()
+ "Quit MIME-viewer and go back to VM.
+This function is called by `mime-viewer/quit' command via
+`mime-viewer/quitting-method-alist'."
+ (if (get-buffer mime/output-buffer-name)
+ (vm-undisplay-buffer mime/output-buffer-name))
+ (if (and tm-vm/automatic-mime-preview
+ (save-excursion
+ (set-buffer mime::preview/article-buffer)
+ vm-summary-buffer))
+ (switch-to-buffer mime::preview/article-buffer)
+ (mime-viewer/kill-buffer)
+ (vm-select-folder-buffer)
+ (setq tm-vm/system-state nil))
+ (vm-display (current-buffer) t (list this-command)
+ (list this-command 'reading-message))
+ (tm-vm/display-preview-buffer)
+ )