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