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