(rmail-output): Insert newline only if necessary.
[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;
44 ;;; look at them 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 (expand-file-name (or answer rmail-default-rmail-file)))))
72 (let ((read-file
73 (expand-file-name
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 (file-name-directory default-file))))
81 ;; If the user enters just a directory,
82 ;; use the name within that directory chosen by the default.
83 (setq rmail-default-rmail-file
84 (if (file-directory-p read-file)
85 (expand-file-name (file-name-nondirectory default-file)
86 read-file)
87 read-file)))
88 (list rmail-default-rmail-file
89 (prefix-numeric-value current-prefix-arg))))
90 (or count (setq count 1))
91 (setq file-name
92 (expand-file-name file-name
93 (file-name-directory rmail-default-rmail-file)))
94 (if (and (file-readable-p file-name) (not (mail-file-babyl-p file-name)))
95 (rmail-output file-name count)
96 (rmail-maybe-set-message-counters)
97 (setq file-name (abbreviate-file-name file-name))
98 (or (find-buffer-visiting file-name)
99 (file-exists-p file-name)
100 (if (yes-or-no-p
101 (concat "\"" file-name "\" does not exist, create it? "))
102 (let ((file-buffer (create-file-buffer file-name)))
103 (save-excursion
104 (set-buffer file-buffer)
105 (rmail-insert-rmail-file-header)
106 (let ((require-final-newline nil))
107 (write-region (point-min) (point-max) file-name t 1)))
108 (kill-buffer file-buffer))
109 (error "Output file does not exist")))
110 (while (> count 0)
111 (let (redelete)
112 (unwind-protect
113 (progn
114 ;; Temporarily turn off Deleted attribute.
115 ;; Do this outside the save-restriction, since it would
116 ;; shift the place in the buffer where the visible text starts.
117 (if (rmail-message-deleted-p rmail-current-message)
118 (progn (setq redelete t)
119 (rmail-set-attribute "deleted" nil)))
120 (save-restriction
121 (widen)
122 ;; Decide whether to append to a file or to an Emacs buffer.
123 (save-excursion
124 (let ((buf (find-buffer-visiting file-name))
125 (cur (current-buffer))
126 (beg (1+ (rmail-msgbeg rmail-current-message)))
127 (end (1+ (rmail-msgend rmail-current-message)))
128 (coding-system-for-write
129 (or rmail-file-coding-system
130 'emacs-mule-unix)))
131 (if (not buf)
132 ;; Output to a file.
133 (if rmail-fields-not-to-output
134 ;; Delete some fields while we output.
135 (let ((obuf (current-buffer)))
136 (set-buffer (get-buffer-create " rmail-out-temp"))
137 (insert-buffer-substring obuf beg end)
138 (rmail-delete-unwanted-fields)
139 (append-to-file (point-min) (point-max) file-name)
140 (set-buffer obuf)
141 (kill-buffer (get-buffer " rmail-out-temp")))
142 (append-to-file beg end file-name))
143 (if (eq buf (current-buffer))
144 (error "Can't output message to same file it's already in"))
145 ;; File has been visited, in buffer BUF.
146 (set-buffer buf)
147 (let ((buffer-read-only nil)
148 (msg (and (boundp 'rmail-current-message)
149 rmail-current-message)))
150 ;; If MSG is non-nil, buffer is in RMAIL mode.
151 (if msg
152 (progn
153 ;; Turn on auto save mode, if it's off in this
154 ;; buffer but enabled by default.
155 (and (not buffer-auto-save-file-name)
156 auto-save-default
157 (auto-save-mode t))
158 (rmail-maybe-set-message-counters)
159 (widen)
160 (narrow-to-region (point-max) (point-max))
161 (insert-buffer-substring cur beg end)
162 (goto-char (point-min))
163 (widen)
164 (search-backward "\n\^_")
165 (narrow-to-region (point) (point-max))
166 (rmail-delete-unwanted-fields)
167 (rmail-count-new-messages t)
168 (if (rmail-summary-exists)
169 (rmail-select-summary
170 (rmail-update-summary)))
171 (rmail-show-message msg))
172 ;; Output file not in rmail mode => just insert at the end.
173 (narrow-to-region (point-min) (1+ (buffer-size)))
174 (goto-char (point-max))
175 (insert-buffer-substring cur beg end)
176 (rmail-delete-unwanted-fields)))))))
177 (rmail-set-attribute "filed" t))
178 (if redelete (rmail-set-attribute "deleted" t))))
179 (setq count (1- count))
180 (if rmail-delete-after-output
181 (rmail-delete-forward)
182 (if (> count 0)
183 (rmail-next-undeleted-message 1))))))
184
185 ;;;###autoload
186 (defcustom rmail-fields-not-to-output nil
187 "*Regexp describing fields to exclude when outputting a message to a file."
188 :type '(choice (const :tag "None" nil)
189 regexp)
190 :group 'rmail-output)
191
192 ;; Delete from the buffer header fields we don't want output.
193 ;; NOT-RMAIL if t means this buffer does not have the full header
194 ;; and *** EOOH *** that a message in an Rmail file has.
195 (defun rmail-delete-unwanted-fields (&optional not-rmail)
196 (if rmail-fields-not-to-output
197 (save-excursion
198 (goto-char (point-min))
199 ;; Find the end of the header.
200 (if (and (or not-rmail (search-forward "\n*** EOOH ***\n" nil t))
201 (search-forward "\n\n" nil t))
202 (let ((end (point-marker)))
203 (goto-char (point-min))
204 (while (re-search-forward rmail-fields-not-to-output end t)
205 (beginning-of-line)
206 (delete-region (point)
207 (progn (forward-line 1) (point)))))))))
208
209 ;;; There are functions elsewhere in Emacs that use this function;
210 ;;; look at them before you change the calling method.
211 ;;;###autoload
212 (defun rmail-output (file-name &optional count noattribute from-gnus)
213 "Append this message to system-inbox-format mail file named FILE-NAME.
214 A prefix argument N says to output N consecutive messages
215 starting with the current one. Deleted messages are skipped and don't count.
216 When called from lisp code, N may be omitted.
217
218 If the pruned message header is shown on the current message, then
219 messages will be appended with pruned headers; otherwise, messages
220 will be appended with their original headers.
221
222 The default file name comes from `rmail-default-file',
223 which is updated to the name you use in this command.
224
225 The optional third argument NOATTRIBUTE, if non-nil, says not
226 to set the `filed' attribute, and not to display a message.
227
228 The optional fourth argument FROM-GNUS is set when called from GNUS."
229 (interactive
230 (let ((default-file
231 (let (answer tail)
232 (setq tail rmail-output-file-alist)
233 ;; Suggest a file based on a pattern match.
234 (while (and tail (not answer))
235 (save-excursion
236 (goto-char (point-min))
237 (if (re-search-forward (car (car tail)) nil t)
238 (setq answer (eval (cdr (car tail)))))
239 (setq tail (cdr tail))))
240 ;; If no suggestion, use same file as last time.
241 (or answer rmail-default-file))))
242 (list (setq rmail-default-file
243 (let ((read-file
244 (read-file-name
245 (concat "Output message to Unix mail file: (default "
246 (file-name-nondirectory default-file)
247 ") ")
248 (file-name-directory (expand-file-name default-file))
249 (expand-file-name default-file))))
250 (if (file-directory-p read-file)
251 (expand-file-name (file-name-nondirectory default-file)
252 read-file)
253 (expand-file-name
254 (or read-file (file-name-nondirectory default-file))
255 (file-name-directory default-file)))))
256 (prefix-numeric-value current-prefix-arg))))
257 (or count (setq count 1))
258 (setq file-name
259 (expand-file-name file-name
260 (and rmail-default-file
261 (file-name-directory rmail-default-file))))
262 (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
263 (rmail-output-to-rmail-file file-name count)
264 (let ((orig-count count)
265 (rmailbuf (current-buffer))
266 (case-fold-search t)
267 (tembuf (get-buffer-create " rmail-output"))
268 (original-headers-p
269 (and (not from-gnus)
270 (save-excursion
271 (save-restriction
272 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
273 (goto-char (point-min))
274 (forward-line 1)
275 (= (following-char) ?0)))))
276 header-beginning
277 mail-from mime-version)
278 (while (> count 0)
279 ;; Preserve the Mail-From and MIME-Version fields
280 ;; even if they have been pruned.
281 (or from-gnus
282 (save-excursion
283 (save-restriction
284 (widen)
285 (goto-char (rmail-msgbeg rmail-current-message))
286 (setq header-beginning (point))
287 (search-forward "\n*** EOOH ***\n")
288 (narrow-to-region header-beginning (point))
289 (setq mail-from
290 (mail-fetch-field "Mail-From")
291 mime-version
292 (mail-fetch-field "MIME-Version")))))
293 (save-excursion
294 (set-buffer tembuf)
295 (erase-buffer)
296 (insert-buffer-substring rmailbuf)
297 (rmail-delete-unwanted-fields t)
298 (or (bolp) (insert "\n"))
299 (goto-char (point-min))
300 (if mail-from
301 (insert mail-from "\n")
302 (insert "From "
303 (mail-strip-quoted-names (or (mail-fetch-field "from")
304 (mail-fetch-field "really-from")
305 (mail-fetch-field "sender")
306 "unknown"))
307 " " (current-time-string) "\n"))
308 (if mime-version
309 (insert "MIME-Version: " mime-version "\n"))
310 ;; ``Quote'' "\nFrom " as "\n>From "
311 ;; (note that this isn't really quoting, as there is no requirement
312 ;; that "\n[>]+From " be quoted in the same transparent way.)
313 (let ((case-fold-search nil))
314 (while (search-forward "\nFrom " nil t)
315 (forward-char -5)
316 (insert ?>)))
317 (write-region (point-min) (point-max) file-name t
318 (if noattribute 'nomsg)))
319 (or noattribute
320 (if (equal major-mode 'rmail-mode)
321 (rmail-set-attribute "filed" t)))
322 (setq count (1- count))
323 (or from-gnus
324 (let ((next-message-p
325 (if rmail-delete-after-output
326 (rmail-delete-forward)
327 (if (> count 0)
328 (rmail-next-undeleted-message 1))))
329 (num-appended (- orig-count count)))
330 (if (and next-message-p original-headers-p)
331 (rmail-toggle-header))
332 (if (and (> count 0) (not next-message-p))
333 (progn
334 (error
335 (save-excursion
336 (set-buffer rmailbuf)
337 (format "Only %d message%s appended" num-appended
338 (if (= num-appended 1) "" "s"))))
339 (setq count 0))))))
340 (kill-buffer tembuf))))
341
342 ;;;###autoload
343 (defun rmail-output-body-to-file (file-name)
344 "Write this message body to the file FILE-NAME.
345 FILE-NAME defaults, interactively, from the Subject field of the message."
346 (interactive
347 (let ((default-file
348 (or (mail-fetch-field "Subject")
349 rmail-default-body-file)))
350 (list (setq rmail-default-body-file
351 (read-file-name
352 "Output message body to file: "
353 (and default-file (file-name-directory default-file))
354 default-file
355 nil default-file)))))
356 (setq file-name
357 (expand-file-name file-name
358 (and rmail-default-body-file
359 (file-name-directory rmail-default-body-file))))
360 (save-excursion
361 (goto-char (point-min))
362 (search-forward "\n\n")
363 (and (file-exists-p file-name)
364 (not (y-or-n-p (message "File %s exists; overwrite? " file-name)))
365 (error "Operation aborted"))
366 (write-region (point) (point-max) file-name)
367 (if (equal major-mode 'rmail-mode)
368 (rmail-set-attribute "stored" t)))
369 (if rmail-delete-after-output
370 (rmail-delete-forward)))
371
372 ;;; rmailout.el ends here