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