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