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 (defun rmail-output-menu (event)
41 "Output current message to another Rmail file, chosen with a menu.
42 Also set the default for subsequent \\[rmail-output-to-rmail-file] commands.
43 The variables `rmail-secondary-file-directory' and
44 `rmail-secondary-file-regexp' control which files are offered in the menu."
46 (let ((file-name (rmail-secondary-file-menu event)))
48 (rmail-output-to-rmail-file
49 (setq rmail-default-rmail-file file-name)))))
51 ;;; There are functions elsewhere in Emacs that use this function; check
52 ;;; them out before you change the calling method.
53 (defun rmail-output-to-rmail-file (file-name &optional count)
54 "Append the current message to an Rmail file named FILE-NAME.
55 If the file does not exist, ask if it should be created.
56 If file is being visited, the message is appended to the Emacs
57 buffer visiting that file.
58 If the file exists and is not an Rmail file,
59 the message is appended in inbox format.
61 The default file name comes from `rmail-default-rmail-file',
62 which is updated to the name you use in this command.
64 A prefix argument N says to output N consecutive messages
65 starting with the current one. Deleted messages are skipped and don't count."
69 (setq tail rmail-output-file-alist)
70 ;; Suggest a file based on a pattern match.
71 (while (and tail (not answer))
73 (goto-char (point-min))
74 (if (re-search-forward (car (car tail)) nil t)
75 (setq answer (eval (cdr (car tail)))))
76 (setq tail (cdr tail))))
77 ;; If not suggestions, use same file as last time.
78 (or answer rmail-default-rmail-file))))
79 (list (setq rmail-default-rmail-file
82 (concat "Output message to Rmail file: (default "
83 (file-name-nondirectory default-file)
85 (file-name-directory default-file)
87 (if (file-directory-p read-file)
88 (expand-file-name (file-name-nondirectory default-file)
91 (or read-file default-file)
92 (file-name-directory default-file)))))
93 (prefix-numeric-value current-prefix-arg))))
94 (or count (setq count 1))
96 (expand-file-name file-name
97 (file-name-directory rmail-default-rmail-file)))
98 (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
99 (rmail-output file-name count)
100 (rmail-maybe-set-message-counters)
101 (setq file-name (abbreviate-file-name file-name))
102 (or (get-file-buffer file-name)
103 (file-exists-p file-name)
105 (concat "\"" file-name "\" does not exist, create it? "))
106 (let ((file-buffer (create-file-buffer file-name)))
108 (set-buffer file-buffer)
109 (rmail-insert-rmail-file-header)
110 (let ((require-final-newline nil))
111 (write-region (point-min) (point-max) file-name t 1)))
112 (kill-buffer file-buffer))
113 (error "Output file does not exist")))
118 ;; Temporarily turn off Deleted attribute.
119 ;; Do this outside the save-restriction, since it would
120 ;; shift the place in the buffer where the visible text starts.
121 (if (rmail-message-deleted-p rmail-current-message)
122 (progn (setq redelete t)
123 (rmail-set-attribute "deleted" nil)))
126 ;; Decide whether to append to a file or to an Emacs buffer.
128 (let ((buf (get-file-buffer file-name))
129 (cur (current-buffer))
130 (beg (1+ (rmail-msgbeg rmail-current-message)))
131 (end (1+ (rmail-msgend rmail-current-message))))
133 (append-to-file beg end file-name)
134 (if (eq buf (current-buffer))
135 (error "Can't output message to same file it's already in"))
136 ;; File has been visited, in buffer BUF.
138 (let ((buffer-read-only nil)
139 (msg (and (boundp 'rmail-current-message)
140 rmail-current-message)))
141 ;; If MSG is non-nil, buffer is in RMAIL mode.
144 ;; Turn on auto save mode, if it's off in this
145 ;; buffer but enabled by default.
146 (and (not buffer-auto-save-file-name)
149 (rmail-maybe-set-message-counters)
151 (narrow-to-region (point-max) (point-max))
152 (insert-buffer-substring cur beg end)
153 (goto-char (point-min))
155 (search-backward "\n\^_")
156 (narrow-to-region (point) (point-max))
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-set-attribute "filed" t))
167 (if redelete (rmail-set-attribute "deleted" t))))
168 (setq count (1- count))
169 (if rmail-delete-after-output
170 (rmail-delete-forward)
172 (rmail-next-undeleted-message 1))))))
174 ;; Returns t if file FILE is an Rmail file.
176 (defun rmail-file-p (file)
177 (let ((buf (generate-new-buffer " *rmail-file-p*")))
181 (insert-file-contents file nil 0 100)
182 (looking-at "BABYL OPTIONS:"))
185 ;;; There are functions elsewhere in Emacs that use this function; check
186 ;;; them out before you change the calling method.
187 (defun rmail-output (file-name &optional count noattribute from-gnus)
188 "Append this message to system-inbox-format mail file named FILE-NAME.
189 A prefix argument N says to output N consecutive messages
190 starting with the current one. Deleted messages are skipped and don't count.
191 When called from lisp code, N may be omitted.
193 If the pruned message header is shown on the current message, then
194 messages will be appended with pruned headers; otherwise, messages
195 will be appended with their original headers.
197 The default file name comes from `rmail-default-file',
198 which is updated to the name you use in this command.
200 The optional third argument NOATTRIBUTE, if non-nil, says not
201 to set the `filed' attribute, and not to display a message.
203 The optional fourth argument FROM-GNUS is set when called from GNUS."
207 (setq tail rmail-output-file-alist)
208 ;; Suggest a file based on a pattern match.
209 (while (and tail (not answer))
211 (goto-char (point-min))
212 (if (re-search-forward (car (car tail)) nil t)
213 (setq answer (eval (cdr (car tail)))))
214 (setq tail (cdr tail))))
215 ;; If not suggestions, use same file as last time.
216 (or answer rmail-default-file))))
217 (list (setq rmail-default-file
220 (concat "Output message to Unix mail file: (default "
221 (file-name-nondirectory default-file)
223 (file-name-directory default-file)
225 (if (file-directory-p read-file)
226 (expand-file-name (file-name-nondirectory default-file)
229 (or read-file default-file)
230 (file-name-directory default-file)))))
231 (prefix-numeric-value current-prefix-arg))))
232 (or count (setq count 1))
234 (expand-file-name file-name
235 (and rmail-default-file
236 (file-name-directory rmail-default-file))))
237 (if (and (file-readable-p file-name) (rmail-file-p file-name))
238 (rmail-output-to-rmail-file file-name count)
239 (let ((orig-count count)
240 (rmailbuf (current-buffer))
242 (tembuf (get-buffer-create " rmail-output"))
247 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
248 (goto-char (point-min))
250 (= (following-char) ?0)))))
259 (goto-char (rmail-msgbeg rmail-current-message))
260 (setq header-beginning (point))
261 (search-forward "\n*** EOOH ***\n")
262 (narrow-to-region header-beginning (point))
263 (mail-fetch-field "Mail-From")))))
267 (insert-buffer-substring rmailbuf)
269 (goto-char (point-min))
271 (insert mail-from "\n")
273 (mail-strip-quoted-names (or (mail-fetch-field "from")
274 (mail-fetch-field "really-from")
275 (mail-fetch-field "sender")
277 " " (current-time-string) "\n"))
278 ;; ``Quote'' "\nFrom " as "\n>From "
279 ;; (note that this isn't really quoting, as there is no requirement
280 ;; that "\n[>]+From " be quoted in the same transparent way.)
281 (while (search-forward "\nFrom " nil t)
284 (write-region (point-min) (point-max) file-name t
285 (if noattribute 'nomsg)))
287 (if (equal major-mode 'rmail-mode)
288 (rmail-set-attribute "filed" t)))
289 (setq count (1- count))
291 (let ((next-message-p
292 (if rmail-delete-after-output
293 (rmail-delete-forward)
295 (rmail-next-undeleted-message 1))))
296 (num-appended (- orig-count count)))
297 (if (and next-message-p original-headers-p)
298 (rmail-toggle-header))
299 (if (and (> count 0) (not next-message-p))
303 (set-buffer rmailbuf)
304 (format "Only %d message%s appended" num-appended
305 (if (= num-appended 1) "" "s"))))
307 (kill-buffer tembuf))))
309 ;;; rmailout.el ends here