Function: rmail-output-as-mbox

rmail-output-as-mbox is a byte-compiled function defined in rmailout.el.gz.

Signature

(rmail-output-as-mbox FILE-NAME NOMSG &optional AS-SEEN)

Documentation

Convert the current buffer's text to mbox and output to FILE-NAME.

Alters the current buffer's text, so it should be a temporary buffer. If a buffer is visiting FILE-NAME, adds the text to that buffer rather than saving the file directly. If the buffer is an Rmail buffer, updates it accordingly. If no buffer is visiting FILE-NAME, appends the text directly to FILE-NAME, and displays a "Wrote file" message unless NOMSG is a symbol (neither nil nor t). AS-SEEN is non-nil if we are copying the message "as seen".

Source Code

;; Defined in /usr/src/emacs/lisp/mail/rmailout.el.gz
(defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
  "Convert the current buffer's text to mbox and output to FILE-NAME.
Alters the current buffer's text, so it should be a temporary buffer.
If a buffer is visiting FILE-NAME, adds the text to that buffer
rather than saving the file directly.  If the buffer is an Rmail buffer,
updates it accordingly.  If no buffer is visiting FILE-NAME, appends
the text directly to FILE-NAME, and displays a \"Wrote file\" message
unless NOMSG is a symbol (neither nil nor t).
AS-SEEN is non-nil if we are copying the message \"as seen\"."
  (let ((case-fold-search t)
        encrypted-file-name
	) ;; from date
    (goto-char (point-min))
    ;; Preserve the Mail-From and MIME-Version fields
    ;; even if they have been pruned.
    (search-forward "\n\n" nil 'move)
    (narrow-to-region (point-min) (point))
    (rmail-delete-unwanted-fields
     (if rmail-enable-mime "Mail-From"
       "Mail-From\\|MIME-Version\\|Content-type"))
    (goto-char (point-min))
    (or (looking-at "From ")
	(insert (mail-mbox-from)))
    (widen)
    ;; Make sure message ends with blank line.
    (goto-char (point-max))
    (rmail-ensure-blank-line)
    (goto-char (point-min))
    (let ((buf (find-buffer-visiting file-name))
	  (tembuf (current-buffer)))
      (when (string-match "[.]gpg\\'" file-name)
        (setq encrypted-file-name file-name
              file-name (substring file-name 0 (match-beginning 0))))
      (if (null buf)
	  (let ((coding-system-for-write 'raw-text-unix)
                (coding-system-for-read 'raw-text-unix))
            ;; If the specified file is encrypted, decrypt it.
            (when encrypted-file-name
              (with-temp-buffer
                (insert-file-contents encrypted-file-name)
                (write-region 1 (point-max) file-name nil 'nomsg)))
	    ;; FIXME should ensure existing file ends with a blank line.
	    (write-region (point-min) (point-max) file-name t
                          (if (or nomsg encrypted-file-name)
                              'nomsg))
            ;; If the specified file was encrypted, re-encrypt it.
            (when encrypted-file-name
              ;; Save the old encrypted file as a backup.
              (rename-file encrypted-file-name
                           (make-backup-file-name encrypted-file-name)
                           t)
              (if (= 0
                     (call-process "gpg" nil nil
                                   "--use-agent" "--batch" "--no-tty"
                                   "--encrypt" "-r"
                                   user-mail-address
                                   file-name))
                  ;; Delete the unencrypted file if encryption succeeded.
                  (delete-file file-name)
                ;; If encrypting failed, put back the original
                ;; encrypted file and signal an error.
                (rename-file (make-backup-file-name encrypted-file-name)
                             encrypted-file-name
                             t)
                (error "Encryption failed; %s unchanged"
                       encrypted-file-name))
              (unless nomsg
                (message "Added to %s" encrypted-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 ((inhibit-read-only t)
	      (msg (and (boundp 'rmail-current-message)
			rmail-current-message)))
	  (and msg as-seen
	       (error "Can't output \"as seen\" to a visited Rmail file"))
	  (if msg
	      (rmail-output-to-rmail-buffer tembuf 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 tembuf)))))))