Merge from emacs-23 branch
[bpt/emacs.git] / lisp / mail / rmailout.el
CommitLineData
537ab246
BG
1;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
2
95df8112
GM
3;; Copyright (C) 1985, 1987, 1993-1994, 2001-2011
4;; Free Software Foundation, Inc.
537ab246
BG
5
6;; Maintainer: FSF
7;; Keywords: mail
bd78fa1d 8;; Package: rmail
537ab246
BG
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;;; Code:
28
29(require 'rmail)
30(provide 'rmailout)
31
537ab246 32(defcustom rmail-output-decode-coding nil
c10782e6 33 "If non-nil, do coding system decoding when outputting message as Babyl."
aab4c09b
GM
34 :type 'boolean
35 :group 'rmail-output)
537ab246 36
7184dd77 37;; FIXME risky?
537ab246 38(defcustom rmail-output-file-alist nil
c10782e6 39 "Alist matching regexps to suggested output Rmail files.
537ab246
BG
40This is a list of elements of the form (REGEXP . NAME-EXP).
41The suggestion is taken if REGEXP matches anywhere in the message buffer.
42NAME-EXP may be a string constant giving the file name to use,
43or more generally it may be any kind of expression that returns
44a file name as a string."
45 :type '(repeat (cons regexp
46 (choice :value ""
47 (string :tag "File Name")
48 sexp)))
49 :group 'rmail-output)
6dc3311d 50;;;###autoload(put 'rmail-output-file-alist 'risky-local-variable t)
537ab246 51
aab4c09b 52(defcustom rmail-fields-not-to-output nil
5b148883
GM
53 "Regexp describing fields to exclude when outputting a message to a file.
54The function `rmail-delete-unwanted-fields' uses this, ignoring case."
aab4c09b
GM
55 :type '(choice (const :tag "None" nil)
56 regexp)
57 :group 'rmail-output)
58
537ab246
BG
59(defun rmail-output-read-file-name ()
60 "Read the file name to use for `rmail-output'.
61Set `rmail-default-file' to this name as well as returning it."
62 (let ((default-file
63 (let (answer tail)
64 (setq tail rmail-output-file-alist)
65 ;; Suggest a file based on a pattern match.
66 (while (and tail (not answer))
67 (save-excursion
68 (goto-char (point-min))
69 (if (re-search-forward (car (car tail)) nil t)
70 (setq answer (eval (cdr (car tail)))))
71 (setq tail (cdr tail))))
72 ;; If no suggestion, use same file as last time.
73 (or answer rmail-default-file))))
74 (let ((read-file
75 (expand-file-name
76 (read-file-name
77 (concat "Output message to mail file (default "
78 (file-name-nondirectory default-file)
79 "): ")
80 (file-name-directory default-file)
81 (abbreviate-file-name default-file))
82 (file-name-directory default-file))))
83 (setq rmail-default-file
84 (if (file-directory-p read-file)
85 (expand-file-name (file-name-nondirectory default-file)
86 read-file)
87 (expand-file-name
88 (or read-file (file-name-nondirectory default-file))
89 (file-name-directory default-file)))))))
90
537ab246 91(defun rmail-delete-unwanted-fields (preserve)
d4653d3c 92 "Delete all headers matching `rmail-fields-not-to-output'.
5b148883
GM
93Retains headers matching the regexp PRESERVE. Ignores case.
94The buffer should be narrowed to just the header."
537ab246
BG
95 (if rmail-fields-not-to-output
96 (save-excursion
97 (goto-char (point-min))
5b148883
GM
98 (let ((case-fold-search t))
99 (while (re-search-forward rmail-fields-not-to-output nil t)
100 (beginning-of-line)
101 (unless (looking-at preserve)
102 (delete-region (point) (line-beginning-position 2))))))))
537ab246
BG
103\f
104(defun rmail-output-as-babyl (file-name nomsg)
105 "Convert the current buffer's text to Babyl and output to FILE-NAME.
d4653d3c
GM
106Alters the current buffer's text, so it should be a temporary buffer.
107If a buffer is visiting FILE-NAME, adds the text to that buffer
108rather than saving the file directly. If the buffer is an Rmail buffer,
109updates it accordingly. If no buffer is visiting FILE-NAME, appends
110the text directly to FILE-NAME, and displays a \"Wrote file\" message
111unless NOMSG is a symbol (neither nil nor t)."
112 (let ((coding-system-for-write 'emacs-mule-unix))
537ab246
BG
113 (save-restriction
114 (goto-char (point-min))
115 (search-forward "\n\n" nil 'move)
116 (narrow-to-region (point-min) (point))
117 (if rmail-fields-not-to-output
118 (rmail-delete-unwanted-fields nil)))
119
120 ;; Convert to Babyl format.
121 (rmail-convert-to-babyl-format)
122 ;; Write it into the file, or its buffer.
123 (let ((buf (find-buffer-visiting file-name))
124 (tembuf (current-buffer)))
125 (if (null buf)
126 (write-region (point-min) (point-max) file-name t nomsg)
127 (if (eq buf (current-buffer))
128 (error "Can't output message to same file it's already in"))
129 ;; File has been visited, in buffer BUF.
130 (set-buffer buf)
131 (let ((inhibit-read-only t)
aab4c09b 132 (msg (bound-and-true-p rmail-current-message)))
537ab246
BG
133 ;; If MSG is non-nil, buffer is in RMAIL mode.
134 (if msg
c10782e6 135 (rmail-output-to-babyl-buffer tembuf msg)
537ab246
BG
136 ;; Output file not in rmail mode => just insert at the end.
137 (narrow-to-region (point-min) (1+ (buffer-size)))
138 (goto-char (point-max))
139 (insert-buffer-substring tembuf)))))))
140
dfeab394
GM
141;; Called only if rmail-summary-exists, which means rmailsum is loaded.
142(declare-function rmail-update-summary "rmailsum" (&rest ignore))
143
c10782e6 144(defun rmail-output-to-babyl-buffer (tembuf msg)
d4653d3c
GM
145 "Copy message in TEMBUF into the current Babyl Rmail buffer.
146Do what is necessary to make Rmail know about the new message, then
147display message number MSG."
148 ;; Turn on Auto Save mode, if it's off in this buffer but enabled by default.
aab4c09b
GM
149 (and (not buffer-auto-save-file-name)
150 auto-save-default
151 (auto-save-mode t))
152 (rmail-maybe-set-message-counters)
153 (widen)
154 (narrow-to-region (point-max) (point-max))
155 (insert-buffer-substring tembuf)
156 (goto-char (point-min))
157 (widen)
158 (search-backward "\n\^_")
159 (narrow-to-region (point) (point-max))
160 (rmail-count-new-messages t)
161 (if (rmail-summary-exists)
d4653d3c 162 (rmail-select-summary (rmail-update-summary)))
aab4c09b 163 (rmail-show-message-1 msg))
537ab246
BG
164\f
165(defun rmail-convert-to-babyl-format ()
d4653d3c 166 "Convert the mbox message in the current buffer to Babyl format."
537ab246
BG
167 (let ((count 0) (start (point-min))
168 (case-fold-search nil)
169 (buffer-undo-list t))
170 (goto-char (point-min))
171 (save-restriction
172 (unless (looking-at "^From ")
173 (error "Invalid mbox message"))
f824857f 174 (insert "\^L\n0,,\n*** EOOH ***\n")
537ab246
BG
175 (rmail-nuke-pinhead-header)
176 ;; Decode base64 or quoted printable contents, Rmail style.
177 (let* ((header-end (save-excursion
178 (and (re-search-forward "\n\n" nil t)
179 (1- (point)))))
180 (case-fold-search t)
181 (quoted-printable-header-field-end
182 (save-excursion
183 (re-search-forward
184 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
185 header-end t)))
186 (base64-header-field-end
187 (and
188 ;; Don't decode non-text data.
189 (save-excursion
190 (re-search-forward
191 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
192 header-end t))
193 (save-excursion
194 (re-search-forward
195 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
196 header-end t)))))
197
198 (goto-char (point-max))
199 (if quoted-printable-header-field-end
200 (save-excursion
201 (unless (mail-unquote-printable-region
202 header-end (point) nil t t)
203 (message "Malformed MIME quoted-printable message"))
204 ;; Change "quoted-printable" to "8bit",
205 ;; to reflect the decoding we just did.
206 (goto-char quoted-printable-header-field-end)
207 (delete-region (point) (search-backward ":"))
208 (insert ": 8bit")))
209 (if base64-header-field-end
210 (save-excursion
211 (when (condition-case nil
212 (progn
213 (base64-decode-region
214 (1+ header-end)
215 (save-excursion
216 ;; Prevent base64-decode-region
217 ;; from removing newline characters.
218 (skip-chars-backward "\n\t ")
219 (point)))
220 t)
221 (error nil))
222 ;; Change "base64" to "8bit", to reflect the
223 ;; decoding we just did.
224 (goto-char base64-header-field-end)
225 (delete-region (point) (search-backward ":"))
226 (insert ": 8bit")))))
227 ;; Transform anything within the message text
228 ;; that might appear to be the end of a Babyl-format message.
229 (save-excursion
230 (save-restriction
231 (narrow-to-region start (point))
232 (goto-char (point-min))
233 (while (search-forward "\n\^_" nil t) ; single char
234 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
235 ;; This is for malformed messages that don't end in newline.
236 ;; There shouldn't be any, but some users say occasionally
237 ;; there are some.
238 (or (bolp) (newline))
239 (insert ?\^_)
240 (setq last-coding-system-used nil)
241 ;; Decode coding system, following specs in the message header,
242 ;; and record what coding system was decoded.
243 (if rmail-output-decode-coding
244 (let ((mime-charset
245 (if (save-excursion
246 (goto-char start)
247 (search-forward "\n\n" nil t)
248 (let ((case-fold-search t))
249 (re-search-backward
250 rmail-mime-charset-pattern
251 start t)))
252 (intern (downcase (match-string 1))))))
253 (rmail-decode-region start (point) mime-charset)))
254 (save-excursion
255 (goto-char start)
256 (forward-line 3)
257 (insert "X-Coding-System: "
258 (symbol-name last-coding-system-used)
259 "\n")))))
260
537ab246 261(defun rmail-nuke-pinhead-header ()
d4653d3c
GM
262 "Delete the \"From \" line in the current mbox message.
263The variable `rmail-unix-mail-delimiter' specifies the From line format.
264Replaces the From line with a \"Mail-from\" header. Adds \"Date\" and
265\"From\" headers if they are not already present."
537ab246
BG
266 (save-excursion
267 (save-restriction
268 (let ((start (point))
269 (end (progn
270 (condition-case ()
271 (search-forward "\n\n")
272 (error
273 (goto-char (point-max))
274 (insert "\n\n")))
275 (point)))
276 has-from has-date)
277 (narrow-to-region start end)
278 (let ((case-fold-search t))
279 (goto-char start)
280 (setq has-from (search-forward "\nFrom:" nil t))
281 (goto-char start)
282 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
283 (goto-char start))
284 (let ((case-fold-search nil))
285 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
286 (replace-match
287 (concat
288 "Mail-from: \\&"
289 ;; Keep and reformat the date if we don't
290 ;; have a Date: field.
291 (if has-date
292 ""
293 (concat
294 "Date: \\2, \\4 \\3 \\9 \\5 "
295
296 ;; The timezone could be matched by group 7 or group 10.
297 ;; If neither of them matched, assume EST, since only
298 ;; Easterners would be so sloppy.
299 ;; It's a shame the substitution can't use "\\10".
300 (cond
301 ((/= (match-beginning 7) (match-end 7)) "\\7")
302 ((/= (match-beginning 10) (match-end 10))
303 (buffer-substring (match-beginning 10)
304 (match-end 10)))
305 (t "EST"))
306 "\n"))
307 ;; Keep and reformat the sender if we don't
308 ;; have a From: field.
309 (if has-from
310 ""
311 "From: \\1\n"))
312 t)))))))
313\f
6aa7fc5b 314(autoload 'mail-mbox-from "mail-utils")
1640a85f 315
537ab246 316(defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
aab4c09b 317 "Convert the current buffer's text to mbox and output to FILE-NAME.
d4653d3c
GM
318Alters the current buffer's text, so it should be a temporary buffer.
319If a buffer is visiting FILE-NAME, adds the text to that buffer
320rather than saving the file directly. If the buffer is an Rmail buffer,
321updates it accordingly. If no buffer is visiting FILE-NAME, appends
322the text directly to FILE-NAME, and displays a \"Wrote file\" message
323unless NOMSG is a symbol (neither nil nor t).
537ab246
BG
324AS-SEEN is non-nil if we are copying the message \"as seen\"."
325 (let ((case-fold-search t)
050f62bf
GM
326 from date)
327 (goto-char (point-min))
537ab246
BG
328 ;; Preserve the Mail-From and MIME-Version fields
329 ;; even if they have been pruned.
330 (search-forward "\n\n" nil 'move)
331 (narrow-to-region (point-min) (point))
537ab246
BG
332 (rmail-delete-unwanted-fields
333 (if rmail-enable-mime "Mail-From"
334 "Mail-From\\|MIME-Version\\|Content-type"))
050f62bf 335 (goto-char (point-min))
1640a85f 336 (or (looking-at "From ")
6aa7fc5b 337 (insert (mail-mbox-from)))
537ab246 338 (widen)
537ab246
BG
339 ;; Make sure message ends with blank line.
340 (goto-char (point-max))
7184dd77 341 (rmail-ensure-blank-line)
537ab246 342 (goto-char (point-min))
537ab246
BG
343 (let ((buf (find-buffer-visiting file-name))
344 (tembuf (current-buffer)))
345 (if (null buf)
346 (let ((coding-system-for-write 'raw-text-unix))
7184dd77 347 ;; FIXME should ensure existing file ends with a blank line.
537ab246
BG
348 (write-region (point-min) (point-max) file-name t nomsg))
349 (if (eq buf (current-buffer))
350 (error "Can't output message to same file it's already in"))
351 ;; File has been visited, in buffer BUF.
352 (set-buffer buf)
353 (let ((inhibit-read-only t)
354 (msg (and (boundp 'rmail-current-message)
355 rmail-current-message)))
356 (and msg as-seen
357 (error "Can't output \"as seen\" to a visited Rmail file"))
358 (if msg
359 (rmail-output-to-rmail-buffer tembuf msg)
360 ;; Output file not in Rmail mode => just insert at the end.
361 (narrow-to-region (point-min) (1+ (buffer-size)))
362 (goto-char (point-max))
363 (insert-buffer-substring tembuf)))))))
364
537ab246 365(defun rmail-output-to-rmail-buffer (tembuf msg)
d4653d3c
GM
366 "Copy message in TEMBUF into the current Rmail buffer.
367Do what is necessary to make Rmail know about the new message. then
368display message number MSG."
537ab246
BG
369 (save-excursion
370 (rmail-swap-buffers-maybe)
9aadce25 371 (rmail-modify-format)
7184dd77
GM
372 ;; Turn on Auto Save mode, if it's off in this buffer but enabled
373 ;; by default.
537ab246
BG
374 (and (not buffer-auto-save-file-name)
375 auto-save-default
376 (auto-save-mode t))
377 (rmail-maybe-set-message-counters)
7184dd77
GM
378 ;; Insert the new message after the last old message.
379 (widen)
380 ;; Make sure the last old message ends with a blank line.
381 (goto-char (point-max))
382 (rmail-ensure-blank-line)
383 ;; Insert the new message at the end.
537ab246
BG
384 (narrow-to-region (point-max) (point-max))
385 (insert-buffer-substring tembuf)
386 (rmail-count-new-messages t)
7184dd77 387 ;; FIXME should re-use existing windows.
537ab246 388 (if (rmail-summary-exists)
d4653d3c 389 (rmail-select-summary (rmail-update-summary)))
a1a29341 390 (rmail-show-message-1 msg)))
537ab246
BG
391\f
392;;; There are functions elsewhere in Emacs that use this function;
393;;; look at them before you change the calling method.
394;;;###autoload
5b148883 395(defun rmail-output (file-name &optional count noattribute not-rmail)
537ab246 396 "Append this message to mail file FILE-NAME.
554fda1a
GM
397Writes mbox format, unless FILE-NAME exists and is Babyl format, in which
398case it writes Babyl.
d4653d3c
GM
399
400Interactively, the default file name comes from `rmail-default-file',
401which is updated to the name you use in this command. In all uses, if
402FILE-NAME is not absolute, it is expanded with the directory part of
403`rmail-default-file'.
537ab246 404
554fda1a
GM
405If a buffer is visiting FILE-NAME, adds the text to that buffer
406rather than saving the file directly. If the buffer is an Rmail
407buffer, updates it accordingly.
408
409This command always outputs the complete message header, even if
410the header display is currently pruned.
537ab246 411
554fda1a
GM
412Optional prefix argument COUNT (default 1) says to output that
413many consecutive messages, starting with the current one (ignoring
414deleted messages). If `rmail-delete-after-output' is non-nil, deletes
415messages after output.
537ab246 416
554fda1a
GM
417The optional third argument NOATTRIBUTE, if non-nil, says not to
418set the `filed' attribute, and not to display a \"Wrote file\"
419message (if writing a file directly).
537ab246 420
5b148883
GM
421Set the optional fourth argument NOT-RMAIL non-nil if you call this
422from a non-Rmail buffer. In this case, COUNT is ignored."
537ab246
BG
423 (interactive
424 (list (rmail-output-read-file-name)
425 (prefix-numeric-value current-prefix-arg)))
426 (or count (setq count 1))
427 (setq file-name
428 (expand-file-name file-name
429 (and rmail-default-file
430 (file-name-directory rmail-default-file))))
537ab246
BG
431 ;; Warn about creating new file.
432 (or (find-buffer-visiting file-name)
433 (file-exists-p file-name)
5b148883 434 (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
537ab246 435 (error "Output file does not exist"))
5b148883
GM
436 (if noattribute (setq noattribute 'nomsg))
437 (let ((babyl-format (and (file-readable-p file-name)
438 (mail-file-babyl-p file-name)))
de62d9e9
RS
439 (cur (current-buffer))
440 (buf (find-buffer-visiting file-name)))
441
442 ;; If a babyl file is visited in a buffer, is it visited as babyl
443 ;; or as mbox?
444 (and babyl-format buf
445 (with-current-buffer buf
446 (save-restriction
447 (widen)
448 (save-excursion
449 (goto-char (point-min))
450 (setq babyl-format
451 (looking-at "BABYL OPTIONS:"))))))
452
5b148883
GM
453 (if not-rmail ; eg via message-fcc-handler-function
454 (with-temp-buffer
5b148883
GM
455 (insert-buffer-substring cur)
456 ;; Output in the appropriate format.
457 (if babyl-format
1640a85f
GM
458 (progn
459 (goto-char (point-min))
460 ;; rmail-convert-to-babyl-format errors if no From line,
461 ;; whereas rmail-output-as-mbox inserts one.
462 (or (looking-at "From ")
6aa7fc5b 463 (insert (mail-mbox-from)))
1640a85f 464 (rmail-output-as-babyl file-name noattribute))
5b148883
GM
465 (rmail-output-as-mbox file-name noattribute)))
466 ;; Called from an Rmail buffer.
467 (if rmail-buffer
468 (set-buffer rmail-buffer)
469 (error "There is no Rmail buffer"))
470 (let ((orig-count count)
471 beg end)
537ab246 472 (while (> count 0)
5b148883
GM
473 (setq beg (rmail-msgbeg rmail-current-message)
474 end (rmail-msgend rmail-current-message))
475 ;; All access to the buffer's local variables is now finished...
476 (save-excursion
477 ;; ... so it is ok to go to a different buffer.
478 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
479 (setq cur (current-buffer))
480 (save-restriction
481 (widen)
482 (with-temp-buffer
483 (insert-buffer-substring cur beg end)
484 (if babyl-format
485 (rmail-output-as-babyl file-name noattribute)
486 (rmail-output-as-mbox file-name noattribute)))))
487 (or noattribute ; mark message as "filed"
488 (rmail-set-attribute rmail-filed-attr-index t))
537ab246 489 (setq count (1- count))
5b148883
GM
490 (let ((next-message-p
491 (if rmail-delete-after-output
492 (rmail-delete-forward)
493 (if (> count 0)
494 (rmail-next-undeleted-message 1))))
495 (num-appended (- orig-count count)))
496 (if (and (> count 0) (not next-message-p))
497 (error "Only %d message%s appended" num-appended
498 (if (= num-appended 1) "" "s")))))))))
499
500;; FIXME nothing outside uses this, so NOT-RMAIL could be dropped.
501;; FIXME this duplicates code from rmail-output.
33417df2 502;;;###autoload
5b148883 503(defun rmail-output-as-seen (file-name &optional count noattribute not-rmail)
aab4c09b 504 "Append this message to mbox file named FILE-NAME.
5b148883
GM
505The details are as for `rmail-output', except that:
506 i) the header is output as currently seen
507 ii) this function cannot write to Babyl files
508iii) an Rmail buffer cannot be visiting FILE-NAME
509
510Note that if NOT-RMAIL is non-nil, there is no difference between this
511function and `rmail-output'. This argument may be removed in future,
512so you should call `rmail-output' directly in that case."
537ab246
BG
513 (interactive
514 (list (rmail-output-read-file-name)
515 (prefix-numeric-value current-prefix-arg)))
5b148883
GM
516 (if not-rmail
517 (rmail-output file-name count noattribute not-rmail)
518 (or count (setq count 1))
519 (setq file-name
520 (expand-file-name file-name
521 (and rmail-default-file
522 (file-name-directory rmail-default-file))))
523 ;; Warn about creating new file.
524 (or (find-buffer-visiting file-name)
525 (file-exists-p file-name)
526 (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
527 (error "Output file does not exist"))
528 ;; FIXME why not?
537ab246
BG
529 (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
530 (error "Cannot output `as seen' to a Babyl file"))
5b148883
GM
531 (if noattribute (setq noattribute 'nomsg))
532 (if rmail-buffer
533 (set-buffer rmail-buffer)
534 (error "There is no Rmail buffer"))
535 (let ((orig-count count)
536 (cur (current-buffer)))
537 (while (> count 0)
538 (let (beg end)
539 ;; If operating from whole-mbox buffer, get message bounds.
540 (or (rmail-buffers-swapped-p)
541 (setq beg (rmail-msgbeg rmail-current-message)
542 end (rmail-msgend rmail-current-message)))
543 (save-restriction
544 (widen)
545 ;; If operating from the view buffer, get the bounds.
546 (or beg
547 (setq beg (point-min)
548 end (point-max)))
549 (with-temp-buffer
550 (insert-buffer-substring cur beg end)
551 (rmail-output-as-mbox file-name noattribute t))))
552 (or noattribute ; mark message as "filed"
537ab246 553 (rmail-set-attribute rmail-filed-attr-index t))
5b148883
GM
554 (setq count (1- count))
555 (let ((next-message-p
556 (if rmail-delete-after-output
557 (rmail-delete-forward)
558 (if (> count 0)
559 (rmail-next-undeleted-message 1))))
560 (num-appended (- orig-count count)))
561 (if (and (> count 0) (not next-message-p))
562 (error "Only %d message%s appended" num-appended
563 (if (= num-appended 1) "" "s"))))))))
537ab246
BG
564
565\f
566;;;###autoload
567(defun rmail-output-body-to-file (file-name)
568 "Write this message body to the file FILE-NAME.
d4653d3c
GM
569Interactively, the default file name comes from either the message
570\"Subject\" header, or from `rmail-default-body-file'. Updates the value
571of `rmail-default-body-file' accordingly. In all uses, if FILE-NAME
572is not absolute, it is expanded with the directory part of
573`rmail-default-body-file'.
574
575Note that this overwrites FILE-NAME (after confirmation), rather
576than appending to it. Deletes the message after writing if
577`rmail-delete-after-output' is non-nil."
537ab246
BG
578 (interactive
579 (let ((default-file
580 (or (mail-fetch-field "Subject")
581 rmail-default-body-file)))
fe6dd7e2
RS
582 (setq default-file
583 (replace-regexp-in-string ":" "-" default-file))
584 (setq default-file
585 (replace-regexp-in-string " " "-" default-file))
537ab246
BG
586 (list (setq rmail-default-body-file
587 (read-file-name
588 "Output message body to file: "
589 (and default-file (file-name-directory default-file))
590 default-file
591 nil default-file)))))
592 (setq file-name
593 (expand-file-name file-name
594 (and rmail-default-body-file
595 (file-name-directory rmail-default-body-file))))
596 (save-excursion
597 (goto-char (point-min))
598 (search-forward "\n\n")
599 (and (file-exists-p file-name)
600 (not (y-or-n-p (format "File %s exists; overwrite? " file-name)))
601 (error "Operation aborted"))
602 (write-region (point) (point-max) file-name))
603 (if rmail-delete-after-output
604 (rmail-delete-forward)))
605
537ab246 606;;; rmailout.el ends here