- ;; Decide whether to append to a file or to an Emacs buffer.
- (save-excursion
- (let ((buf (get-file-buffer file-name))
- (cur (current-buffer))
- (beg (1+ (rmail-msgbeg rmail-current-message)))
- (end (1+ (rmail-msgend rmail-current-message))))
- (if (not buf)
- (append-to-file beg end file-name)
- (if (eq buf (current-buffer))
- (error "Can't output message to same file it's already in"))
- ;; File has been visited, in buffer BUF.
- (set-buffer buf)
- (let ((buffer-read-only nil)
- (msg (and (boundp 'rmail-current-message)
- rmail-current-message)))
- ;; If MSG is non-nil, buffer is in RMAIL mode.
- (if msg
- (progn
- (rmail-maybe-set-message-counters)
- (widen)
- (narrow-to-region (point-max) (point-max))
- (insert-buffer-substring cur beg end)
- (goto-char (point-min))
- (widen)
- (search-backward "\n\^_")
- (narrow-to-region (point) (point-max))
- (rmail-count-new-messages t)
- (rmail-show-message msg))
- ;; Output file not in rmail mode => just insert at the end.
- (narrow-to-region (point-min) (1+ (buffer-size)))
- (goto-char (point-max))
- (insert-buffer-substring cur beg end)))))))
- (rmail-set-attribute "filed" t))
- (if redelete (rmail-set-attribute "deleted" t))))
- (setq count (1- count))
- (if rmail-delete-after-output
- (rmail-delete-forward)
- (if (> count 0)
- (rmail-next-undeleted-message 1)))))
-
-(defun rmail-output (count file-name)
- "Append this message to Unix mail file named FILE-NAME.
+ (save-restriction
+ (widen)
+ ;; Decide whether to append to a file or to an Emacs buffer.
+ (save-excursion
+ (let ((buf (find-buffer-visiting file-name))
+ (cur (current-buffer))
+ (beg (1+ (rmail-msgbeg rmail-current-message)))
+ (end (1+ (rmail-msgend rmail-current-message)))
+ (coding-system-for-write
+ (or rmail-file-coding-system
+ 'emacs-mule-unix)))
+ (if (not buf)
+ ;; Output to a file.
+ (if rmail-fields-not-to-output
+ ;; Delete some fields while we output.
+ (let ((obuf (current-buffer)))
+ (set-buffer (get-buffer-create " rmail-out-temp"))
+ (insert-buffer-substring obuf beg end)
+ (rmail-delete-unwanted-fields)
+ (append-to-file (point-min) (point-max) file-name)
+ (set-buffer obuf)
+ (kill-buffer (get-buffer " rmail-out-temp")))
+ (append-to-file beg end file-name))
+ (if (eq buf (current-buffer))
+ (error "Can't output message to same file it's already in"))
+ ;; File has been visited, in buffer BUF.
+ (set-buffer buf)
+ (let ((buffer-read-only nil)
+ (msg (and (boundp 'rmail-current-message)
+ rmail-current-message)))
+ ;; If MSG is non-nil, buffer is in RMAIL mode.
+ (if msg
+ (progn
+ ;; Turn on auto save mode, if it's off in this
+ ;; buffer but enabled by default.
+ (and (not buffer-auto-save-file-name)
+ auto-save-default
+ (auto-save-mode t))
+ (rmail-maybe-set-message-counters)
+ (widen)
+ (narrow-to-region (point-max) (point-max))
+ (insert-buffer-substring cur beg end)
+ (goto-char (point-min))
+ (widen)
+ (search-backward "\n\^_")
+ (narrow-to-region (point) (point-max))
+ (rmail-delete-unwanted-fields)
+ (rmail-count-new-messages t)
+ (if (rmail-summary-exists)
+ (rmail-select-summary
+ (rmail-update-summary)))
+ (rmail-show-message msg))
+ ;; Output file not in rmail mode => just insert at the end.
+ (narrow-to-region (point-min) (1+ (buffer-size)))
+ (goto-char (point-max))
+ (insert-buffer-substring cur beg end)
+ (rmail-delete-unwanted-fields)))))))
+ (rmail-set-attribute "filed" t))
+ (if redelete (rmail-set-attribute "deleted" t))))
+ (setq count (1- count))
+ (if rmail-delete-after-output
+ (unless (rmail-delete-forward) (setq count 0))
+ (if (> count 0)
+ (unless (rmail-next-undeleted-message 1) (setq count 0)))))))
+
+;;;###autoload
+(defcustom rmail-fields-not-to-output nil
+ "*Regexp describing fields to exclude when outputting a message to a file."
+ :type '(choice (const :tag "None" nil)
+ regexp)
+ :group 'rmail-output)
+
+;; Delete from the buffer header fields we don't want output.
+;; NOT-RMAIL if t means this buffer does not have the full header
+;; and *** EOOH *** that a message in an Rmail file has.
+(defun rmail-delete-unwanted-fields (&optional not-rmail)
+ (if rmail-fields-not-to-output
+ (save-excursion
+ (goto-char (point-min))
+ ;; Find the end of the header.
+ (if (and (or not-rmail (search-forward "\n*** EOOH ***\n" nil t))
+ (search-forward "\n\n" nil t))
+ (let ((end (point-marker)))
+ (goto-char (point-min))
+ (while (re-search-forward rmail-fields-not-to-output end t)
+ (beginning-of-line)
+ (delete-region (point)
+ (progn (forward-line 1) (point)))))))))
+
+;;; There are functions elsewhere in Emacs that use this function;
+;;; look at them before you change the calling method.
+;;;###autoload
+(defun rmail-output (file-name &optional count noattribute from-gnus)
+ "Append this message to system-inbox-format mail file named FILE-NAME.