Add (require 'rmail) so the macro will be byte-compiled properly.
[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 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 (require 'rmail)
27
28 ;; Temporary until Emacs always has this variable.
29 (defvar rmail-delete-after-output nil
30 "*Non-nil means automatically delete a message that is copied to a file.")
31
32 (defvar rmail-output-file-alist nil
33 "*Alist matching regexps to suggested output Rmail files.
34 This is a list of elements of the form (REGEXP . NAME-EXP).
35 The suggestion is taken if REGEXP matches anywhere in the message buffer.
36 NAME-EXP may be a string constant giving the file name to use,
37 or more generally it may be any kind of expression that returns
38 a file name as a string.")
39
40 ;;; There are functions elsewhere in Emacs that use this function; check
41 ;;; them out before you change the calling method.
42 (defun rmail-output-to-rmail-file (file-name &optional count)
43 "Append the current message to an Rmail file named FILE-NAME.
44 If the file does not exist, ask if it should be created.
45 If file is being visited, the message is appended to the Emacs
46 buffer visiting that file.
47 If the file exists and is not an Rmail file,
48 the message is appended in inbox format.
49
50 The default file name comes from `rmail-last-rmail-file',
51 which is updated to the name you use in this command.
52
53 A prefix argument N says to output N consecutive messages
54 starting with the current one. Deleted messages are skipped and don't count."
55 (interactive
56 (let ((default-file
57 (let (answer tail)
58 (setq tail rmail-output-file-alist)
59 ;; Suggest a file based on a pattern match.
60 (while (and tail (not answer))
61 (save-excursion
62 (goto-char (point-min))
63 (if (re-search-forward (car (car tail)) nil t)
64 (setq answer (eval (cdr (car tail)))))
65 (setq tail (cdr tail))))
66 ;; If not suggestions, use same file as last time.
67 (or answer rmail-default-rmail-file))))
68 (list (setq rmail-default-rmail-file
69 (let ((read-file
70 (read-file-name
71 (concat "Output message to Rmail file: (default "
72 (file-name-nondirectory default-file)
73 ") ")
74 (file-name-directory default-file)
75 default-file)))
76 (if (file-directory-p read-file)
77 (expand-file-name (file-name-nondirectory default-file)
78 read-file)
79 (expand-file-name
80 (or read-file default-file)
81 (file-name-directory default-file)))))
82 (prefix-numeric-value current-prefix-arg))))
83 (or count (setq count 1))
84 (setq file-name
85 (expand-file-name file-name
86 (file-name-directory rmail-default-rmail-file)))
87 (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
88 (rmail-output file-name count)
89 (rmail-maybe-set-message-counters)
90 (setq file-name (abbreviate-file-name file-name))
91 (or (get-file-buffer file-name)
92 (file-exists-p file-name)
93 (if (yes-or-no-p
94 (concat "\"" file-name "\" does not exist, create it? "))
95 (let ((file-buffer (create-file-buffer file-name)))
96 (save-excursion
97 (set-buffer file-buffer)
98 (rmail-insert-rmail-file-header)
99 (let ((require-final-newline nil))
100 (write-region (point-min) (point-max) file-name t 1)))
101 (kill-buffer file-buffer))
102 (error "Output file does not exist")))
103 (while (> count 0)
104 (let (redelete)
105 (unwind-protect
106 (progn
107 ;; Temporarily turn off Deleted attribute.
108 ;; Do this outside the save-restriction, since it would
109 ;; shift the place in the buffer where the visible text starts.
110 (if (rmail-message-deleted-p rmail-current-message)
111 (progn (setq redelete t)
112 (rmail-set-attribute "deleted" nil)))
113 (save-restriction
114 (widen)
115 ;; Decide whether to append to a file or to an Emacs buffer.
116 (save-excursion
117 (let ((buf (get-file-buffer file-name))
118 (cur (current-buffer))
119 (beg (1+ (rmail-msgbeg rmail-current-message)))
120 (end (1+ (rmail-msgend rmail-current-message))))
121 (if (not buf)
122 (append-to-file beg end file-name)
123 (if (eq buf (current-buffer))
124 (error "Can't output message to same file it's already in"))
125 ;; File has been visited, in buffer BUF.
126 (set-buffer buf)
127 (let ((buffer-read-only nil)
128 (msg (and (boundp 'rmail-current-message)
129 rmail-current-message)))
130 ;; If MSG is non-nil, buffer is in RMAIL mode.
131 (if msg
132 (progn
133 ;; Turn on auto save mode, if it's off in this
134 ;; buffer but enabled by default.
135 (and (not buffer-auto-save-file-name)
136 auto-save-default
137 (auto-save-mode t))
138 (rmail-maybe-set-message-counters)
139 (widen)
140 (narrow-to-region (point-max) (point-max))
141 (insert-buffer-substring cur beg end)
142 (goto-char (point-min))
143 (widen)
144 (search-backward "\n\^_")
145 (narrow-to-region (point) (point-max))
146 (rmail-count-new-messages t)
147 (if (rmail-summary-exists)
148 (rmail-select-summary
149 (rmail-update-summary)))
150 (rmail-show-message msg))
151 ;; Output file not in rmail mode => just insert at the end.
152 (narrow-to-region (point-min) (1+ (buffer-size)))
153 (goto-char (point-max))
154 (insert-buffer-substring cur beg end)))))))
155 (rmail-set-attribute "filed" t))
156 (if redelete (rmail-set-attribute "deleted" t))))
157 (setq count (1- count))
158 (if rmail-delete-after-output
159 (rmail-delete-forward)
160 (if (> count 0)
161 (rmail-next-undeleted-message 1))))))
162
163 ;; Returns t if file FILE is an Rmail file.
164 ;;;###autoload
165 (defun rmail-file-p (file)
166 (let ((buf (generate-new-buffer " *rmail-file-p*")))
167 (unwind-protect
168 (save-excursion
169 (set-buffer buf)
170 (insert-file-contents file nil 0 100)
171 (looking-at "BABYL OPTIONS:"))
172 (kill-buffer buf))))
173
174 ;;; There are functions elsewhere in Emacs that use this function; check
175 ;;; them out before you change the calling method.
176 (defun rmail-output (file-name &optional count noattribute from-gnus)
177 "Append this message to Unix mail file named FILE-NAME.
178 A prefix argument N says to output N consecutive messages
179 starting with the current one. Deleted messages are skipped and don't count.
180 When called from lisp code, N may be omitted.
181
182 If the pruned message header is shown on the current message, then
183 messages will be appended with pruned headers; otherwise, messages
184 will be appended with their original headers.
185
186 The default file name comes from `rmail-last-file',
187 which is updated to the name you use in this command.
188
189 The optional third argument NOATTRIBUTE, if non-nil, says not
190 to set the `filed' attribute, and not to display a message.
191
192 The optional fourth argument FROM-GNUS is set when called from GNUS."
193 (interactive
194 (let ((default-file
195 (let (answer tail)
196 (setq tail rmail-output-file-alist)
197 ;; Suggest a file based on a pattern match.
198 (while (and tail (not answer))
199 (save-excursion
200 (goto-char (point-min))
201 (if (re-search-forward (car (car tail)) nil t)
202 (setq answer (eval (cdr (car tail)))))
203 (setq tail (cdr tail))))
204 ;; If not suggestions, use same file as last time.
205 (or answer rmail-default-file))))
206 (list (setq rmail-default-file
207 (let ((read-file
208 (read-file-name
209 (concat "Output message to Unix mail file: (default "
210 (file-name-nondirectory default-file)
211 ") ")
212 (file-name-directory default-file)
213 default-file)))
214 (if (file-directory-p read-file)
215 (expand-file-name (file-name-nondirectory default-file)
216 read-file)
217 (expand-file-name
218 (or read-file default-file)
219 (file-name-directory default-file)))))
220 (prefix-numeric-value current-prefix-arg))))
221 (or count (setq count 1))
222 (setq file-name
223 (expand-file-name file-name
224 (and rmail-default-file
225 (file-name-directory rmail-default-file))))
226 (if (and (file-readable-p file-name) (rmail-file-p file-name))
227 (rmail-output-to-rmail-file file-name count)
228 (let ((orig-count count)
229 (rmailbuf (current-buffer))
230 (case-fold-search t)
231 (tembuf (get-buffer-create " rmail-output"))
232 (original-headers-p
233 (and (not from-gnus)
234 (save-excursion
235 (save-restriction
236 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
237 (goto-char (point-min))
238 (forward-line 1)
239 (= (following-char) ?0)))))
240 header-beginning
241 mail-from)
242 (while (> count 0)
243 (or from-gnus
244 (setq mail-from
245 (save-excursion
246 (save-restriction
247 (widen)
248 (goto-char (rmail-msgbeg rmail-current-message))
249 (setq header-beginning (point))
250 (search-forward "\n*** EOOH ***\n")
251 (narrow-to-region header-beginning (point))
252 (mail-fetch-field "Mail-From")))))
253 (save-excursion
254 (set-buffer tembuf)
255 (erase-buffer)
256 (insert-buffer-substring rmailbuf)
257 (insert "\n")
258 (goto-char (point-min))
259 (if mail-from
260 (insert mail-from "\n")
261 (insert "From "
262 (mail-strip-quoted-names (or (mail-fetch-field "from")
263 (mail-fetch-field "really-from")
264 (mail-fetch-field "sender")
265 "unknown"))
266 " " (current-time-string) "\n"))
267 ;; ``Quote'' "\nFrom " as "\n>From "
268 ;; (note that this isn't really quoting, as there is no requirement
269 ;; that "\n[>]+From " be quoted in the same transparent way.)
270 (while (search-forward "\nFrom " nil t)
271 (forward-char -5)
272 (insert ?>))
273 (write-region (point-min) (point-max) file-name t
274 (if noattribute 'nomsg)))
275 (or noattribute
276 (if (equal major-mode 'rmail-mode)
277 (rmail-set-attribute "filed" t)))
278 (setq count (1- count))
279 (or from-gnus
280 (let ((next-message-p
281 (if rmail-delete-after-output
282 (rmail-delete-forward)
283 (if (> count 0)
284 (rmail-next-undeleted-message 1))))
285 (num-appended (- orig-count count)))
286 (if (and next-message-p original-headers-p)
287 (rmail-toggle-header))
288 (if (and (> count 0) (not next-message-p))
289 (progn
290 (error
291 (save-excursion
292 (set-buffer rmailbuf)
293 (format "Only %d message%s appended" num-appended
294 (if (= num-appended 1) "" "s"))))
295 (setq count 0))))))
296 (kill-buffer tembuf))))
297
298 ;;; rmailout.el ends here