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