1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
3 ;; Copyright (C) 1985, 1987, 1993, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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.")
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.")
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.
50 The default file name comes from `rmail-default-rmail-file',
51 which is updated to the name you use in this command.
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."
58 (setq tail rmail-output-file-alist)
59 ;; Suggest a file based on a pattern match.
60 (while (and tail (not answer))
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
71 (concat "Output message to Rmail file: (default "
72 (file-name-nondirectory default-file)
74 (file-name-directory default-file)
76 (if (file-directory-p read-file)
77 (expand-file-name (file-name-nondirectory default-file)
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))
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)
94 (concat "\"" file-name "\" does not exist, create it? "))
95 (let ((file-buffer (create-file-buffer file-name)))
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")))
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)))
115 ;; Decide whether to append to a file or to an Emacs buffer.
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))))
122 (append-to-file beg end file-name)
123 (if (eq buf (current-buffer))
124 (error "Can't output message to same file it's already in"))
125 ;; File has been visited, in buffer BUF.
127 (let ((buffer-read-only nil)
128 (msg (and (boundp 'rmail-current-message)
129 rmail-current-message)))
130 ;; If MSG is non-nil, buffer is in RMAIL mode.
133 ;; Turn on auto save mode, if it's off in this
134 ;; buffer but enabled by default.
135 (and (not buffer-auto-save-file-name)
138 (rmail-maybe-set-message-counters)
140 (narrow-to-region (point-max) (point-max))
141 (insert-buffer-substring cur beg end)
142 (goto-char (point-min))
144 (search-backward "\n\^_")
145 (narrow-to-region (point) (point-max))
146 (rmail-count-new-messages t)
147 (if (rmail-summary-exists)
148 (rmail-select-summary
149 (rmail-update-summary)))
150 (rmail-show-message msg))
151 ;; Output file not in rmail mode => just insert at the end.
152 (narrow-to-region (point-min) (1+ (buffer-size)))
153 (goto-char (point-max))
154 (insert-buffer-substring cur beg end)))))))
155 (rmail-set-attribute "filed" t))
156 (if redelete (rmail-set-attribute "deleted" t))))
157 (setq count (1- count))
158 (if rmail-delete-after-output
159 (rmail-delete-forward)
161 (rmail-next-undeleted-message 1))))))
163 ;; Returns t if file FILE is an Rmail file.
165 (defun rmail-file-p (file)
166 (let ((buf (generate-new-buffer " *rmail-file-p*")))
170 (insert-file-contents file nil 0 100)
171 (looking-at "BABYL OPTIONS:"))
174 ;;; There are functions elsewhere in Emacs that use this function; check
175 ;;; them out before you change the calling method.
176 (defun rmail-output (file-name &optional count noattribute from-gnus)
177 "Append this message to system-inbox-format mail file named FILE-NAME.
178 A prefix argument N says to output N consecutive messages
179 starting with the current one. Deleted messages are skipped and don't count.
180 When called from lisp code, N may be omitted.
182 If the pruned message header is shown on the current message, then
183 messages will be appended with pruned headers; otherwise, messages
184 will be appended with their original headers.
186 The default file name comes from `rmail-default-file',
187 which is updated to the name you use in this command.
189 The optional third argument NOATTRIBUTE, if non-nil, says not
190 to set the `filed' attribute, and not to display a message.
192 The optional fourth argument FROM-GNUS is set when called from GNUS."
196 (setq tail rmail-output-file-alist)
197 ;; Suggest a file based on a pattern match.
198 (while (and tail (not answer))
200 (goto-char (point-min))
201 (if (re-search-forward (car (car tail)) nil t)
202 (setq answer (eval (cdr (car tail)))))
203 (setq tail (cdr tail))))
204 ;; If not suggestions, use same file as last time.
205 (or answer rmail-default-file))))
206 (list (setq rmail-default-file
209 (concat "Output message to Unix mail file: (default "
210 (file-name-nondirectory default-file)
212 (file-name-directory default-file)
214 (if (file-directory-p read-file)
215 (expand-file-name (file-name-nondirectory default-file)
218 (or read-file default-file)
219 (file-name-directory default-file)))))
220 (prefix-numeric-value current-prefix-arg))))
221 (or count (setq count 1))
223 (expand-file-name file-name
224 (and rmail-default-file
225 (file-name-directory rmail-default-file))))
226 (if (and (file-readable-p file-name) (rmail-file-p file-name))
227 (rmail-output-to-rmail-file file-name count)
228 (let ((orig-count count)
229 (rmailbuf (current-buffer))
231 (tembuf (get-buffer-create " rmail-output"))
236 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
237 (goto-char (point-min))
239 (= (following-char) ?0)))))
248 (goto-char (rmail-msgbeg rmail-current-message))
249 (setq header-beginning (point))
250 (search-forward "\n*** EOOH ***\n")
251 (narrow-to-region header-beginning (point))
252 (mail-fetch-field "Mail-From")))))
256 (insert-buffer-substring rmailbuf)
258 (goto-char (point-min))
260 (insert mail-from "\n")
262 (mail-strip-quoted-names (or (mail-fetch-field "from")
263 (mail-fetch-field "really-from")
264 (mail-fetch-field "sender")
266 " " (current-time-string) "\n"))
267 ;; ``Quote'' "\nFrom " as "\n>From "
268 ;; (note that this isn't really quoting, as there is no requirement
269 ;; that "\n[>]+From " be quoted in the same transparent way.)
270 (while (search-forward "\nFrom " nil t)
273 (write-region (point-min) (point-max) file-name t
274 (if noattribute 'nomsg)))
276 (if (equal major-mode 'rmail-mode)
277 (rmail-set-attribute "filed" t)))
278 (setq count (1- count))
280 (let ((next-message-p
281 (if rmail-delete-after-output
282 (rmail-delete-forward)
284 (rmail-next-undeleted-message 1))))
285 (num-appended (- orig-count count)))
286 (if (and next-message-p original-headers-p)
287 (rmail-toggle-header))
288 (if (and (> count 0) (not next-message-p))
292 (set-buffer rmailbuf)
293 (format "Only %d message%s appended" num-appended
294 (if (= num-appended 1) "" "s"))))
296 (kill-buffer tembuf))))
298 ;;; rmailout.el ends here