1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
3 ;; Copyright (C) 1985, 1987, 1993 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.
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.")
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.")
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.
48 A prefix argument N says to output N consecutive messages
49 starting with the current one. Deleted messages are skipped and don't count."
53 (setq tail rmail-output-file-alist)
54 ;; Suggest a file based on a pattern match.
55 (while (and tail (not answer))
57 (goto-char (point-min))
58 (if (re-search-forward (car (car tail)) nil t)
59 (setq answer (eval (cdr (car tail)))))
60 (setq tail (cdr tail))))
61 ;; If not suggestions, use same file as last time.
62 (or answer rmail-last-rmail-file))))
63 (list (setq rmail-last-rmail-file
67 (concat "Output message to Rmail file: (default "
68 (file-name-nondirectory default-file)
70 (file-name-directory default-file)
73 (file-name-directory default-file)))
74 (prefix-numeric-value current-prefix-arg))))
75 (or count (setq count 1))
77 (expand-file-name file-name
78 (file-name-directory rmail-last-rmail-file)))
79 (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
80 (rmail-output file-name count)
81 (rmail-maybe-set-message-counters)
82 (setq file-name (abbreviate-file-name file-name))
83 (or (get-file-buffer file-name)
84 (file-exists-p file-name)
86 (concat "\"" file-name "\" does not exist, create it? "))
87 (let ((file-buffer (create-file-buffer file-name)))
89 (set-buffer file-buffer)
90 (rmail-insert-rmail-file-header)
91 (let ((require-final-newline nil))
92 (write-region (point-min) (point-max) file-name t 1)))
93 (kill-buffer file-buffer))
94 (error "Output file does not exist")))
99 ;; Temporarily turn off Deleted attribute.
100 ;; Do this outside the save-restriction, since it would
101 ;; shift the place in the buffer where the visible text starts.
102 (if (rmail-message-deleted-p rmail-current-message)
103 (progn (setq redelete t)
104 (rmail-set-attribute "deleted" nil)))
107 ;; Decide whether to append to a file or to an Emacs buffer.
109 (let ((buf (get-file-buffer file-name))
110 (cur (current-buffer))
111 (beg (1+ (rmail-msgbeg rmail-current-message)))
112 (end (1+ (rmail-msgend rmail-current-message))))
114 (append-to-file beg end file-name)
115 (if (eq buf (current-buffer))
116 (error "Can't output message to same file it's already in"))
117 ;; File has been visited, in buffer BUF.
119 (let ((buffer-read-only nil)
120 (msg (and (boundp 'rmail-current-message)
121 rmail-current-message)))
122 ;; If MSG is non-nil, buffer is in RMAIL mode.
125 ;; Turn on auto save mode, if it's off in this
126 ;; buffer but enabled by default.
127 (and (not buffer-auto-save-file-name)
130 (rmail-maybe-set-message-counters)
132 (narrow-to-region (point-max) (point-max))
133 (insert-buffer-substring cur beg end)
134 (goto-char (point-min))
136 (search-backward "\n\^_")
137 (narrow-to-region (point) (point-max))
138 (rmail-count-new-messages t)
139 (if (rmail-summary-exists)
140 (rmail-select-summary
141 (rmail-update-summary)))
142 (rmail-show-message msg))
143 ;; Output file not in rmail mode => just insert at the end.
144 (narrow-to-region (point-min) (1+ (buffer-size)))
145 (goto-char (point-max))
146 (insert-buffer-substring cur beg end)))))))
147 (rmail-set-attribute "filed" t))
148 (if redelete (rmail-set-attribute "deleted" t))))
149 (setq count (1- count))
150 (if rmail-delete-after-output
151 (rmail-delete-forward)
153 (rmail-next-undeleted-message 1))))))
155 ;; Returns t if file FILE is an Rmail file.
157 (defun rmail-file-p (file)
158 (let ((buf (generate-new-buffer " *rmail-file-p*")))
162 (insert-file-contents file nil 0 100)
163 (looking-at "BABYL OPTIONS:"))
166 ;;; There are functions elsewhere in Emacs that use this function; check
167 ;;; them out before you change the calling method.
168 (defun rmail-output (file-name &optional count noattribute from-gnus)
169 "Append this message to Unix mail file named FILE-NAME.
170 A prefix argument N says to output N consecutive messages
171 starting with the current one. Deleted messages are skipped and don't count.
172 When called from lisp code, N may be omitted.
174 If the pruned message header is shown on the current message, then
175 messages will be appended with pruned headers; otherwise, messages
176 will be appended with their original headers.
178 The optional third argument NOATTRIBUTE, if non-nil, says not
179 to set the `filed' attribute, and not to display a message.
181 The optional fourth argument FROM-GNUS is set when called from GNUS."
183 (list (setq rmail-last-file
185 (concat "Output message to Unix mail file"
188 (file-name-nondirectory rmail-last-file)
191 (and rmail-last-file (file-name-directory rmail-last-file))
193 (prefix-numeric-value current-prefix-arg)))
194 (or count (setq count 1))
196 (expand-file-name file-name
198 (file-name-directory rmail-last-file))))
199 (if (and (file-readable-p file-name) (rmail-file-p file-name))
200 (rmail-output-to-rmail-file file-name count)
201 (let ((orig-count count)
202 (rmailbuf (current-buffer))
204 (tembuf (get-buffer-create " rmail-output"))
209 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
210 (goto-char (point-min))
212 (= (following-char) ?0)))))
221 (goto-char (rmail-msgbeg rmail-current-message))
222 (setq header-beginning (point))
223 (search-forward "\n*** EOOH ***\n")
224 (narrow-to-region header-beginning (point))
225 (mail-fetch-field "Mail-From")))))
229 (insert-buffer-substring rmailbuf)
231 (goto-char (point-min))
233 (insert mail-from "\n")
235 (mail-strip-quoted-names (or (mail-fetch-field "from")
236 (mail-fetch-field "really-from")
237 (mail-fetch-field "sender")
239 " " (current-time-string) "\n"))
240 ;; ``Quote'' "\nFrom " as "\n>From "
241 ;; (note that this isn't really quoting, as there is no requirement
242 ;; that "\n[>]+From " be quoted in the same transparent way.)
243 (while (search-forward "\nFrom " nil t)
246 (write-region (point-min) (point-max) file-name t
247 (if noattribute 'nomsg)))
249 (if (equal major-mode 'rmail-mode)
250 (rmail-set-attribute "filed" t)))
251 (setq count (1- count))
253 (let ((next-message-p
254 (if rmail-delete-after-output
255 (rmail-delete-forward)
257 (rmail-next-undeleted-message 1))))
258 (num-appended (- orig-count count)))
259 (if (and next-message-p original-headers-p)
260 (rmail-toggle-header))
261 (if (and (> count 0) (not next-message-p))
265 (set-buffer rmailbuf)
266 (format "Only %d message%s appended" num-appended
267 (if (= num-appended 1) "" "s"))))
269 (kill-buffer tembuf))))
271 ;;; rmailout.el ends here