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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 ;; Temporary until Emacs always has this variable.
30 (defvar rmail-delete-after-output nil
31 "*Non-nil means automatically delete a message that is copied to a file.")
33 (defvar rmail-output-file-alist nil
34 "*Alist matching regexps to suggested output Rmail files.
35 This is a list of elements of the form (REGEXP . NAME-EXP).
36 The suggestion is taken if REGEXP matches anywhere in the message buffer.
37 NAME-EXP may be a string constant giving the file name to use,
38 or more generally it may be any kind of expression that returns
39 a file name as a string.")
41 ;;; There are functions elsewhere in Emacs that use this function; check
42 ;;; them out before you change the calling method.
43 (defun rmail-output-to-rmail-file (file-name &optional count
)
44 "Append the current message to an Rmail file named FILE-NAME.
45 If the file does not exist, ask if it should be created.
46 If file is being visited, the message is appended to the Emacs
47 buffer visiting that file.
48 If the file exists and is not an Rmail file,
49 the message is appended in inbox format.
51 The default file name comes from `rmail-default-rmail-file',
52 which is updated to the name you use in this command.
54 A prefix argument N says to output N consecutive messages
55 starting with the current one. Deleted messages are skipped and don't count."
59 (setq tail rmail-output-file-alist
)
60 ;; Suggest a file based on a pattern match.
61 (while (and tail
(not answer
))
63 (goto-char (point-min))
64 (if (re-search-forward (car (car tail
)) nil t
)
65 (setq answer
(eval (cdr (car tail
)))))
66 (setq tail
(cdr tail
))))
67 ;; If not suggestions, use same file as last time.
68 (or answer rmail-default-rmail-file
))))
69 (list (setq rmail-default-rmail-file
72 (concat "Output message to Rmail file: (default "
73 (file-name-nondirectory default-file
)
75 (file-name-directory default-file
)
77 (if (file-directory-p read-file
)
78 (expand-file-name (file-name-nondirectory default-file
)
81 (or read-file default-file
)
82 (file-name-directory default-file
)))))
83 (prefix-numeric-value current-prefix-arg
))))
84 (or count
(setq count
1))
86 (expand-file-name file-name
87 (file-name-directory rmail-default-rmail-file
)))
88 (if (and (file-readable-p file-name
) (not (mail-file-babyl-p file-name
)))
89 (rmail-output file-name count
)
90 (rmail-maybe-set-message-counters)
91 (setq file-name
(abbreviate-file-name file-name
))
92 (or (get-file-buffer file-name
)
93 (file-exists-p file-name
)
95 (concat "\"" file-name
"\" does not exist, create it? "))
96 (let ((file-buffer (create-file-buffer file-name
)))
98 (set-buffer file-buffer
)
99 (rmail-insert-rmail-file-header)
100 (let ((require-final-newline nil
))
101 (write-region (point-min) (point-max) file-name t
1)))
102 (kill-buffer file-buffer
))
103 (error "Output file does not exist")))
108 ;; Temporarily turn off Deleted attribute.
109 ;; Do this outside the save-restriction, since it would
110 ;; shift the place in the buffer where the visible text starts.
111 (if (rmail-message-deleted-p rmail-current-message
)
112 (progn (setq redelete t
)
113 (rmail-set-attribute "deleted" nil
)))
116 ;; Decide whether to append to a file or to an Emacs buffer.
118 (let ((buf (get-file-buffer file-name
))
119 (cur (current-buffer))
120 (beg (1+ (rmail-msgbeg rmail-current-message
)))
121 (end (1+ (rmail-msgend rmail-current-message
))))
124 (if rmail-fields-not-to-output
125 ;; Delete some fields while we output.
126 (let ((obuf (current-buffer)))
127 (set-buffer (get-buffer-create " rmail-out-temp"))
128 (insert-buffer-substring obuf beg end
)
129 (rmail-delete-unwanted-fields)
130 (append-to-file (point-min) (point-max) file-name
)
132 (kill-buffer (get-buffer " rmail-out-temp")))
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-delete-unwanted-fields)
158 (rmail-count-new-messages t
)
159 (if (rmail-summary-exists)
160 (rmail-select-summary
161 (rmail-update-summary)))
162 (rmail-show-message msg
))
163 ;; Output file not in rmail mode => just insert at the end.
164 (narrow-to-region (point-min) (1+ (buffer-size)))
165 (goto-char (point-max))
166 (insert-buffer-substring cur beg end
)
167 (rmail-delete-unwanted-fields)))))))
168 (rmail-set-attribute "filed" t
))
169 (if redelete
(rmail-set-attribute "deleted" t
))))
170 (setq count
(1- count
))
171 (if rmail-delete-after-output
172 (rmail-delete-forward)
174 (rmail-next-undeleted-message 1))))))
176 (defvar rmail-fields-not-to-output nil
177 "*Regexp describing fields to exclude when outputting a message to a file.")
179 ;; Delete from the buffer header fields we don't want output.
180 ;; NOT-RMAIL if t means this buffer does not have the full header
181 ;; and *** EOOH *** that a message in an Rmail file has.
182 (defun rmail-delete-unwanted-fields (&optional not-rmail
)
183 (if rmail-fields-not-to-output
185 (goto-char (point-min))
186 ;; Find the end of the header.
187 (if (and (or not-rmail
(search-forward "\n*** EOOH ***\n" nil t
))
188 (search-forward "\n\n" nil t
))
189 (let ((end (point-marker)))
190 (goto-char (point-min))
191 (while (re-search-forward rmail-fields-not-to-output end t
)
193 (delete-region (point)
194 (progn (forward-line 1) (point)))))))))
196 ;;; There are functions elsewhere in Emacs that use this function; check
197 ;;; them out before you change the calling method.
198 (defun rmail-output (file-name &optional count noattribute from-gnus
)
199 "Append this message to system-inbox-format mail file named FILE-NAME.
200 A prefix argument N says to output N consecutive messages
201 starting with the current one. Deleted messages are skipped and don't count.
202 When called from lisp code, N may be omitted.
204 If the pruned message header is shown on the current message, then
205 messages will be appended with pruned headers; otherwise, messages
206 will be appended with their original headers.
208 The default file name comes from `rmail-default-file',
209 which is updated to the name you use in this command.
211 The optional third argument NOATTRIBUTE, if non-nil, says not
212 to set the `filed' attribute, and not to display a message.
214 The optional fourth argument FROM-GNUS is set when called from GNUS."
218 (setq tail rmail-output-file-alist
)
219 ;; Suggest a file based on a pattern match.
220 (while (and tail
(not answer
))
222 (goto-char (point-min))
223 (if (re-search-forward (car (car tail
)) nil t
)
224 (setq answer
(eval (cdr (car tail
)))))
225 (setq tail
(cdr tail
))))
226 ;; If not suggestions, use same file as last time.
227 (or answer rmail-default-file
))))
228 (list (setq rmail-default-file
231 (concat "Output message to Unix mail file: (default "
232 (file-name-nondirectory default-file
)
234 (file-name-directory default-file
)
236 (if (file-directory-p read-file
)
237 (expand-file-name (file-name-nondirectory default-file
)
240 (or read-file default-file
)
241 (file-name-directory default-file
)))))
242 (prefix-numeric-value current-prefix-arg
))))
243 (or count
(setq count
1))
245 (expand-file-name file-name
246 (and rmail-default-file
247 (file-name-directory rmail-default-file
))))
248 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
249 (rmail-output-to-rmail-file file-name count
)
250 (let ((orig-count count
)
251 (rmailbuf (current-buffer))
253 (tembuf (get-buffer-create " rmail-output"))
258 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
259 (goto-char (point-min))
261 (= (following-char) ?
0)))))
270 (goto-char (rmail-msgbeg rmail-current-message
))
271 (setq header-beginning
(point))
272 (search-forward "\n*** EOOH ***\n")
273 (narrow-to-region header-beginning
(point))
274 (mail-fetch-field "Mail-From")))))
278 (insert-buffer-substring rmailbuf
)
279 (rmail-delete-unwanted-fields t
)
281 (goto-char (point-min))
283 (insert mail-from
"\n")
285 (mail-strip-quoted-names (or (mail-fetch-field "from")
286 (mail-fetch-field "really-from")
287 (mail-fetch-field "sender")
289 " " (current-time-string) "\n"))
290 ;; ``Quote'' "\nFrom " as "\n>From "
291 ;; (note that this isn't really quoting, as there is no requirement
292 ;; that "\n[>]+From " be quoted in the same transparent way.)
293 (let ((case-fold-search nil
))
294 (while (search-forward "\nFrom " nil t
)
297 (write-region (point-min) (point-max) file-name t
298 (if noattribute
'nomsg
)))
300 (if (equal major-mode
'rmail-mode
)
301 (rmail-set-attribute "filed" t
)))
302 (setq count
(1- count
))
304 (let ((next-message-p
305 (if rmail-delete-after-output
306 (rmail-delete-forward)
308 (rmail-next-undeleted-message 1))))
309 (num-appended (- orig-count count
)))
310 (if (and next-message-p original-headers-p
)
311 (rmail-toggle-header))
312 (if (and (> count
0) (not next-message-p
))
316 (set-buffer rmailbuf
)
317 (format "Only %d message%s appended" num-appended
318 (if (= num-appended
1) "" "s"))))
320 (kill-buffer tembuf
))))
322 ;;; rmailout.el ends here