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