(rmail-output-to-rmail-file): Specify the coding system for writing.
[bpt/emacs.git] / lisp / mail / rmailout.el
1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
2
3 ;; Copyright (C) 1985, 1987, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'rmail)
28
29 ;;;###autoload
30 (defcustom rmail-output-file-alist nil
31 "*Alist matching regexps to suggested output Rmail files.
32 This is a list of elements of the form (REGEXP . NAME-EXP).
33 The suggestion is taken if REGEXP matches anywhere in the message buffer.
34 NAME-EXP may be a string constant giving the file name to use,
35 or more generally it may be any kind of expression that returns
36 a file name as a string."
37 :type '(repeat (cons regexp
38 (choice :value ""
39 (string :tag "File Name")
40 sexp)))
41 :group 'rmail-output)
42
43 ;;; There are functions elsewhere in Emacs that use this function; check
44 ;;; them out before you change the calling method.
45 ;;;###autoload
46 (defun rmail-output-to-rmail-file (file-name &optional count)
47 "Append the current message to an Rmail file named FILE-NAME.
48 If the file does not exist, ask if it should be created.
49 If file is being visited, the message is appended to the Emacs
50 buffer visiting that file.
51 If the file exists and is not an Rmail file, the message is
52 appended in inbox format, the same way `rmail-output' does it.
53
54 The default file name comes from `rmail-default-rmail-file',
55 which is updated to the name you use in this command.
56
57 A prefix argument N says to output N consecutive messages
58 starting with the current one. Deleted messages are skipped and don't count."
59 (interactive
60 (let ((default-file
61 (let (answer tail)
62 (setq tail rmail-output-file-alist)
63 ;; Suggest a file based on a pattern match.
64 (while (and tail (not answer))
65 (save-excursion
66 (goto-char (point-min))
67 (if (re-search-forward (car (car tail)) nil t)
68 (setq answer (eval (cdr (car tail)))))
69 (setq tail (cdr tail))))
70 ;; If not suggestions, use same file as last time.
71 (or answer rmail-default-rmail-file))))
72 (list (setq rmail-default-rmail-file
73 (let ((read-file
74 (read-file-name
75 (concat "Output message to Rmail file: (default "
76 (file-name-nondirectory default-file)
77 ") ")
78 (file-name-directory default-file)
79 default-file)))
80 (if (file-directory-p read-file)
81 (expand-file-name (file-name-nondirectory default-file)
82 read-file)
83 (expand-file-name
84 (or read-file default-file)
85 (file-name-directory default-file)))))
86 (prefix-numeric-value current-prefix-arg))))
87 (or count (setq count 1))
88 (setq file-name
89 (expand-file-name file-name
90 (file-name-directory rmail-default-rmail-file)))
91 (if (and (file-readable-p file-name) (not (mail-file-babyl-p file-name)))
92 (rmail-output file-name count)
93 (rmail-maybe-set-message-counters)
94 (setq file-name (abbreviate-file-name file-name))
95 (or (find-buffer-visiting file-name)
96 (file-exists-p file-name)
97 (if (yes-or-no-p
98 (concat "\"" file-name "\" does not exist, create it? "))
99 (let ((file-buffer (create-file-buffer file-name)))
100 (save-excursion
101 (set-buffer file-buffer)
102 (rmail-insert-rmail-file-header)
103 (let ((require-final-newline nil))
104 (write-region (point-min) (point-max) file-name t 1)))
105 (kill-buffer file-buffer))
106 (error "Output file does not exist")))
107 (while (> count 0)
108 (let (redelete)
109 (unwind-protect
110 (progn
111 ;; Temporarily turn off Deleted attribute.
112 ;; Do this outside the save-restriction, since it would
113 ;; shift the place in the buffer where the visible text starts.
114 (if (rmail-message-deleted-p rmail-current-message)
115 (progn (setq redelete t)
116 (rmail-set-attribute "deleted" nil)))
117 (save-restriction
118 (widen)
119 ;; Decide whether to append to a file or to an Emacs buffer.
120 (save-excursion
121 (let ((buf (find-buffer-visiting file-name))
122 (cur (current-buffer))
123 (beg (1+ (rmail-msgbeg rmail-current-message)))
124 (end (1+ (rmail-msgend rmail-current-message)))
125 (coding-system-for-write
126 (or rmail-file-coding-system
127 'emacs-mule-unix)))
128 (if (not buf)
129 ;; Output to a file.
130 (if rmail-fields-not-to-output
131 ;; Delete some fields while we output.
132 (let ((obuf (current-buffer)))
133 (set-buffer (get-buffer-create " rmail-out-temp"))
134 (insert-buffer-substring obuf beg end)
135 (rmail-delete-unwanted-fields)
136 (append-to-file (point-min) (point-max) file-name)
137 (set-buffer obuf)
138 (kill-buffer (get-buffer " rmail-out-temp")))
139 (append-to-file beg end file-name))
140 (if (eq buf (current-buffer))
141 (error "Can't output message to same file it's already in"))
142 ;; File has been visited, in buffer BUF.
143 (set-buffer buf)
144 (let ((buffer-read-only nil)
145 (msg (and (boundp 'rmail-current-message)
146 rmail-current-message)))
147 ;; If MSG is non-nil, buffer is in RMAIL mode.
148 (if msg
149 (progn
150 ;; Turn on auto save mode, if it's off in this
151 ;; buffer but enabled by default.
152 (and (not buffer-auto-save-file-name)
153 auto-save-default
154 (auto-save-mode t))
155 (rmail-maybe-set-message-counters)
156 (widen)
157 (narrow-to-region (point-max) (point-max))
158 (insert-buffer-substring cur beg end)
159 (goto-char (point-min))
160 (widen)
161 (search-backward "\n\^_")
162 (narrow-to-region (point) (point-max))
163 (rmail-delete-unwanted-fields)
164 (rmail-count-new-messages t)
165 (if (rmail-summary-exists)
166 (rmail-select-summary
167 (rmail-update-summary)))
168 (rmail-show-message msg))
169 ;; Output file not in rmail mode => just insert at the end.
170 (narrow-to-region (point-min) (1+ (buffer-size)))
171 (goto-char (point-max))
172 (insert-buffer-substring cur beg end)
173 (rmail-delete-unwanted-fields)))))))
174 (rmail-set-attribute "filed" t))
175 (if redelete (rmail-set-attribute "deleted" t))))
176 (setq count (1- count))
177 (if rmail-delete-after-output
178 (rmail-delete-forward)
179 (if (> count 0)
180 (rmail-next-undeleted-message 1))))))
181
182 ;;;###autoload
183 (defcustom rmail-fields-not-to-output nil
184 "*Regexp describing fields to exclude when outputting a message to a file."
185 :type '(choice (const :tag "None" nil)
186 regexp)
187 :group 'rmail-output)
188
189 ;; Delete from the buffer header fields we don't want output.
190 ;; NOT-RMAIL if t means this buffer does not have the full header
191 ;; and *** EOOH *** that a message in an Rmail file has.
192 (defun rmail-delete-unwanted-fields (&optional not-rmail)
193 (if rmail-fields-not-to-output
194 (save-excursion
195 (goto-char (point-min))
196 ;; Find the end of the header.
197 (if (and (or not-rmail (search-forward "\n*** EOOH ***\n" nil t))
198 (search-forward "\n\n" nil t))
199 (let ((end (point-marker)))
200 (goto-char (point-min))
201 (while (re-search-forward rmail-fields-not-to-output end t)
202 (beginning-of-line)
203 (delete-region (point)
204 (progn (forward-line 1) (point)))))))))
205
206 ;;; There are functions elsewhere in Emacs that use this function; check
207 ;;; them out before you change the calling method.
208 ;;;###autoload
209 (defun rmail-output (file-name &optional count noattribute from-gnus)
210 "Append this message to system-inbox-format mail file named FILE-NAME.
211 A prefix argument N says to output N consecutive messages
212 starting with the current one. Deleted messages are skipped and don't count.
213 When called from lisp code, N may be omitted.
214
215 If the pruned message header is shown on the current message, then
216 messages will be appended with pruned headers; otherwise, messages
217 will be appended with their original headers.
218
219 The default file name comes from `rmail-default-file',
220 which is updated to the name you use in this command.
221
222 The optional third argument NOATTRIBUTE, if non-nil, says not
223 to set the `filed' attribute, and not to display a message.
224
225 The optional fourth argument FROM-GNUS is set when called from GNUS."
226 (interactive
227 (let ((default-file
228 (let (answer tail)
229 (setq tail rmail-output-file-alist)
230 ;; Suggest a file based on a pattern match.
231 (while (and tail (not answer))
232 (save-excursion
233 (goto-char (point-min))
234 (if (re-search-forward (car (car tail)) nil t)
235 (setq answer (eval (cdr (car tail)))))
236 (setq tail (cdr tail))))
237 ;; If not suggestions, use same file as last time.
238 (or answer rmail-default-file))))
239 (list (setq rmail-default-file
240 (let ((read-file
241 (read-file-name
242 (concat "Output message to Unix mail file: (default "
243 (file-name-nondirectory default-file)
244 ") ")
245 (file-name-directory default-file)
246 default-file)))
247 (if (file-directory-p read-file)
248 (expand-file-name (file-name-nondirectory default-file)
249 read-file)
250 (expand-file-name
251 (or read-file default-file)
252 (file-name-directory default-file)))))
253 (prefix-numeric-value current-prefix-arg))))
254 (or count (setq count 1))
255 (setq file-name
256 (expand-file-name file-name
257 (and rmail-default-file
258 (file-name-directory rmail-default-file))))
259 (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
260 (rmail-output-to-rmail-file file-name count)
261 (let ((orig-count count)
262 (rmailbuf (current-buffer))
263 (case-fold-search t)
264 (tembuf (get-buffer-create " rmail-output"))
265 (original-headers-p
266 (and (not from-gnus)
267 (save-excursion
268 (save-restriction
269 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
270 (goto-char (point-min))
271 (forward-line 1)
272 (= (following-char) ?0)))))
273 header-beginning
274 mail-from)
275 (while (> count 0)
276 (or from-gnus
277 (setq mail-from
278 (save-excursion
279 (save-restriction
280 (widen)
281 (goto-char (rmail-msgbeg rmail-current-message))
282 (setq header-beginning (point))
283 (search-forward "\n*** EOOH ***\n")
284 (narrow-to-region header-beginning (point))
285 (mail-fetch-field "Mail-From")))))
286 (save-excursion
287 (set-buffer tembuf)
288 (erase-buffer)
289 (insert-buffer-substring rmailbuf)
290 (rmail-delete-unwanted-fields t)
291 (insert "\n")
292 (goto-char (point-min))
293 (if mail-from
294 (insert mail-from "\n")
295 (insert "From "
296 (mail-strip-quoted-names (or (mail-fetch-field "from")
297 (mail-fetch-field "really-from")
298 (mail-fetch-field "sender")
299 "unknown"))
300 " " (current-time-string) "\n"))
301 ;; ``Quote'' "\nFrom " as "\n>From "
302 ;; (note that this isn't really quoting, as there is no requirement
303 ;; that "\n[>]+From " be quoted in the same transparent way.)
304 (let ((case-fold-search nil))
305 (while (search-forward "\nFrom " nil t)
306 (forward-char -5)
307 (insert ?>)))
308 (write-region (point-min) (point-max) file-name t
309 (if noattribute 'nomsg)))
310 (or noattribute
311 (if (equal major-mode 'rmail-mode)
312 (rmail-set-attribute "filed" t)))
313 (setq count (1- count))
314 (or from-gnus
315 (let ((next-message-p
316 (if rmail-delete-after-output
317 (rmail-delete-forward)
318 (if (> count 0)
319 (rmail-next-undeleted-message 1))))
320 (num-appended (- orig-count count)))
321 (if (and next-message-p original-headers-p)
322 (rmail-toggle-header))
323 (if (and (> count 0) (not next-message-p))
324 (progn
325 (error
326 (save-excursion
327 (set-buffer rmailbuf)
328 (format "Only %d message%s appended" num-appended
329 (if (= num-appended 1) "" "s"))))
330 (setq count 0))))))
331 (kill-buffer tembuf))))
332
333 ;;;###autoload
334 (defun rmail-output-body-to-file (file-name)
335 "Write this message body to the file FILE-NAME.
336 FILE-NAME defaults, interactively, from the Subject field of the message."
337 (interactive
338 (let ((default-file
339 (or (mail-fetch-field "Subject")
340 rmail-default-body-file)))
341 (list (setq rmail-default-body-file
342 (read-file-name
343 "Output message body to file: "
344 (and default-file (file-name-directory default-file))
345 default-file
346 nil default-file)))))
347 (setq file-name
348 (expand-file-name file-name
349 (and rmail-default-body-file
350 (file-name-directory rmail-default-body-file))))
351 (save-excursion
352 (goto-char (point-min))
353 (search-forward "\n\n")
354 (and (file-exists-p file-name)
355 (not (y-or-n-p (message "File %s exists; overwrite? " file-name)))
356 (error "Operation aborted"))
357 (write-region (point) (point-max) file-name)
358 (if (equal major-mode 'rmail-mode)
359 (rmail-set-attribute "stored" t)))
360 (if rmail-delete-after-output
361 (rmail-delete-forward)))
362
363 ;;; rmailout.el ends here