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