]> code.delx.au - gnu-emacs/blob - lisp/mail/rmailout.el
3d81d6a25dd531051229ef663acf5d95e9601a49
[gnu-emacs] / 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
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-default-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 ;; Output to a file.
123 (if rmail-fields-not-to-output
124 ;; Delete some fields while we output.
125 (let ((obuf (current-buffer)))
126 (set-buffer (get-buffer-create " rmail-out-temp"))
127 (insert-buffer-substring obuf beg end)
128 (rmail-delete-unwanted-fields)
129 (append-to-file (point-min) (point-max) file-name)
130 (set-buffer obuf)
131 (kill-buffer (get-buffer " rmail-out-temp")))
132 (append-to-file beg end file-name))
133 (if (eq buf (current-buffer))
134 (error "Can't output message to same file it's already in"))
135 ;; File has been visited, in buffer BUF.
136 (set-buffer buf)
137 (let ((buffer-read-only nil)
138 (msg (and (boundp 'rmail-current-message)
139 rmail-current-message)))
140 ;; If MSG is non-nil, buffer is in RMAIL mode.
141 (if msg
142 (progn
143 ;; Turn on auto save mode, if it's off in this
144 ;; buffer but enabled by default.
145 (and (not buffer-auto-save-file-name)
146 auto-save-default
147 (auto-save-mode t))
148 (rmail-maybe-set-message-counters)
149 (widen)
150 (narrow-to-region (point-max) (point-max))
151 (insert-buffer-substring cur beg end)
152 (goto-char (point-min))
153 (widen)
154 (search-backward "\n\^_")
155 (narrow-to-region (point) (point-max))
156 (rmail-delete-unwanted-fields)
157 (rmail-count-new-messages t)
158 (if (rmail-summary-exists)
159 (rmail-select-summary
160 (rmail-update-summary)))
161 (rmail-show-message msg))
162 ;; Output file not in rmail mode => just insert at the end.
163 (narrow-to-region (point-min) (1+ (buffer-size)))
164 (goto-char (point-max))
165 (insert-buffer-substring cur beg end)
166 (rmail-delete-unwanted-fields)))))))
167 (rmail-set-attribute "filed" t))
168 (if redelete (rmail-set-attribute "deleted" t))))
169 (setq count (1- count))
170 (if rmail-delete-after-output
171 (rmail-delete-forward)
172 (if (> count 0)
173 (rmail-next-undeleted-message 1))))))
174
175 (defvar rmail-fields-not-to-output nil
176 "*Regexp describing fields to exclude when outputting a message to a file.")
177
178 ;; Delete from the buffer header fields we don't want output.
179 ;; NOT-RMAIL if t means this buffer does not have the full header
180 ;; and *** EOOH *** that a message in an Rmail file has.
181 (defun rmail-delete-unwanted-fields (&optional not-rmail)
182 (if rmail-fields-not-to-output
183 (save-excursion
184 (goto-char (point-min))
185 ;; Find the end of the header.
186 (if (and (or not-rmail (search-forward "\n*** EOOH ***\n" nil t))
187 (search-forward "\n\n" nil t))
188 (let ((end (point-marker)))
189 (goto-char (point-min))
190 (while (re-search-forward rmail-fields-not-to-output end t)
191 (beginning-of-line)
192 (delete-region (point)
193 (progn (forward-line 1) (point)))))))))
194
195 ;; Returns t if file FILE is an Rmail file.
196 ;;;###autoload
197 (defun rmail-file-p (file)
198 (let ((buf (generate-new-buffer " *rmail-file-p*")))
199 (unwind-protect
200 (save-excursion
201 (set-buffer buf)
202 (insert-file-contents file nil 0 100)
203 (looking-at "BABYL OPTIONS:"))
204 (kill-buffer buf))))
205
206 ;;; There are functions elsewhere in Emacs that use this function; check
207 ;;; them out before you change the calling method.
208 (defun rmail-output (file-name &optional count noattribute from-gnus)
209 "Append this message to system-inbox-format mail file named FILE-NAME.
210 A prefix argument N says to output N consecutive messages
211 starting with the current one. Deleted messages are skipped and don't count.
212 When called from lisp code, N may be omitted.
213
214 If the pruned message header is shown on the current message, then
215 messages will be appended with pruned headers; otherwise, messages
216 will be appended with their original headers.
217
218 The default file name comes from `rmail-default-file',
219 which is updated to the name you use in this command.
220
221 The optional third argument NOATTRIBUTE, if non-nil, says not
222 to set the `filed' attribute, and not to display a message.
223
224 The optional fourth argument FROM-GNUS is set when called from GNUS."
225 (interactive
226 (let ((default-file
227 (let (answer tail)
228 (setq tail rmail-output-file-alist)
229 ;; Suggest a file based on a pattern match.
230 (while (and tail (not answer))
231 (save-excursion
232 (goto-char (point-min))
233 (if (re-search-forward (car (car tail)) nil t)
234 (setq answer (eval (cdr (car tail)))))
235 (setq tail (cdr tail))))
236 ;; If not suggestions, use same file as last time.
237 (or answer rmail-default-file))))
238 (list (setq rmail-default-file
239 (let ((read-file
240 (read-file-name
241 (concat "Output message to Unix mail file: (default "
242 (file-name-nondirectory default-file)
243 ") ")
244 (file-name-directory default-file)
245 default-file)))
246 (if (file-directory-p read-file)
247 (expand-file-name (file-name-nondirectory default-file)
248 read-file)
249 (expand-file-name
250 (or read-file default-file)
251 (file-name-directory default-file)))))
252 (prefix-numeric-value current-prefix-arg))))
253 (or count (setq count 1))
254 (setq file-name
255 (expand-file-name file-name
256 (and rmail-default-file
257 (file-name-directory rmail-default-file))))
258 (if (and (file-readable-p file-name) (rmail-file-p file-name))
259 (rmail-output-to-rmail-file file-name count)
260 (let ((orig-count count)
261 (rmailbuf (current-buffer))
262 (case-fold-search t)
263 (tembuf (get-buffer-create " rmail-output"))
264 (original-headers-p
265 (and (not from-gnus)
266 (save-excursion
267 (save-restriction
268 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
269 (goto-char (point-min))
270 (forward-line 1)
271 (= (following-char) ?0)))))
272 header-beginning
273 mail-from)
274 (while (> count 0)
275 (or from-gnus
276 (setq mail-from
277 (save-excursion
278 (save-restriction
279 (widen)
280 (goto-char (rmail-msgbeg rmail-current-message))
281 (setq header-beginning (point))
282 (search-forward "\n*** EOOH ***\n")
283 (narrow-to-region header-beginning (point))
284 (mail-fetch-field "Mail-From")))))
285 (save-excursion
286 (set-buffer tembuf)
287 (erase-buffer)
288 (insert-buffer-substring rmailbuf)
289 (rmail-delete-unwanted-fields t)
290 (insert "\n")
291 (goto-char (point-min))
292 (if mail-from
293 (insert mail-from "\n")
294 (insert "From "
295 (mail-strip-quoted-names (or (mail-fetch-field "from")
296 (mail-fetch-field "really-from")
297 (mail-fetch-field "sender")
298 "unknown"))
299 " " (current-time-string) "\n"))
300 ;; ``Quote'' "\nFrom " as "\n>From "
301 ;; (note that this isn't really quoting, as there is no requirement
302 ;; that "\n[>]+From " be quoted in the same transparent way.)
303 (while (search-forward "\nFrom " nil t)
304 (forward-char -5)
305 (insert ?>))
306 (write-region (point-min) (point-max) file-name t
307 (if noattribute 'nomsg)))
308 (or noattribute
309 (if (equal major-mode 'rmail-mode)
310 (rmail-set-attribute "filed" t)))
311 (setq count (1- count))
312 (or from-gnus
313 (let ((next-message-p
314 (if rmail-delete-after-output
315 (rmail-delete-forward)
316 (if (> count 0)
317 (rmail-next-undeleted-message 1))))
318 (num-appended (- orig-count count)))
319 (if (and next-message-p original-headers-p)
320 (rmail-toggle-header))
321 (if (and (> count 0) (not next-message-p))
322 (progn
323 (error
324 (save-excursion
325 (set-buffer rmailbuf)
326 (format "Only %d message%s appended" num-appended
327 (if (= num-appended 1) "" "s"))))
328 (setq count 0))))))
329 (kill-buffer tembuf))))
330
331 ;;; rmailout.el ends here