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