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