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 The default file name comes from `rmail-last-rmail-file',
49 which is updated to the name you use in this command.
51 A prefix argument N says to output N consecutive messages
52 starting with the current one. Deleted messages are skipped and don't count."
56 (setq tail rmail-output-file-alist
)
57 ;; Suggest a file based on a pattern match.
58 (while (and tail
(not answer
))
60 (goto-char (point-min))
61 (if (re-search-forward (car (car tail
)) nil t
)
62 (setq answer
(eval (cdr (car tail
)))))
63 (setq tail
(cdr tail
))))
64 ;; If not suggestions, use same file as last time.
65 (or answer rmail-default-rmail-file
))))
66 (list (setq rmail-default-rmail-file
69 (concat "Output message to Rmail file: (default "
70 (file-name-nondirectory default-file
)
72 (file-name-directory default-file
)
74 (if (file-directory-p read-file
)
75 (expand-file-name (file-name-nondirectory default-file
)
78 (or read-file default-file
)
79 (file-name-directory default-file
)))))
80 (prefix-numeric-value current-prefix-arg
))))
81 (or count
(setq count
1))
83 (expand-file-name file-name
84 (file-name-directory rmail-default-rmail-file
)))
85 (if (and (file-readable-p file-name
) (not (rmail-file-p file-name
)))
86 (rmail-output file-name count
)
87 (rmail-maybe-set-message-counters)
88 (setq file-name
(abbreviate-file-name file-name
))
89 (or (get-file-buffer file-name
)
90 (file-exists-p file-name
)
92 (concat "\"" file-name
"\" does not exist, create it? "))
93 (let ((file-buffer (create-file-buffer file-name
)))
95 (set-buffer file-buffer
)
96 (rmail-insert-rmail-file-header)
97 (let ((require-final-newline nil
))
98 (write-region (point-min) (point-max) file-name t
1)))
99 (kill-buffer file-buffer
))
100 (error "Output file does not exist")))
105 ;; Temporarily turn off Deleted attribute.
106 ;; Do this outside the save-restriction, since it would
107 ;; shift the place in the buffer where the visible text starts.
108 (if (rmail-message-deleted-p rmail-current-message
)
109 (progn (setq redelete t
)
110 (rmail-set-attribute "deleted" nil
)))
113 ;; Decide whether to append to a file or to an Emacs buffer.
115 (let ((buf (get-file-buffer file-name
))
116 (cur (current-buffer))
117 (beg (1+ (rmail-msgbeg rmail-current-message
)))
118 (end (1+ (rmail-msgend rmail-current-message
))))
120 (append-to-file beg end file-name
)
121 (if (eq buf
(current-buffer))
122 (error "Can't output message to same file it's already in"))
123 ;; File has been visited, in buffer BUF.
125 (let ((buffer-read-only nil
)
126 (msg (and (boundp 'rmail-current-message
)
127 rmail-current-message
)))
128 ;; If MSG is non-nil, buffer is in RMAIL mode.
131 ;; Turn on auto save mode, if it's off in this
132 ;; buffer but enabled by default.
133 (and (not buffer-auto-save-file-name
)
136 (rmail-maybe-set-message-counters)
138 (narrow-to-region (point-max) (point-max))
139 (insert-buffer-substring cur beg end
)
140 (goto-char (point-min))
142 (search-backward "\n\^_")
143 (narrow-to-region (point) (point-max))
144 (rmail-count-new-messages t
)
145 (if (rmail-summary-exists)
146 (rmail-select-summary
147 (rmail-update-summary)))
148 (rmail-show-message msg
))
149 ;; Output file not in rmail mode => just insert at the end.
150 (narrow-to-region (point-min) (1+ (buffer-size)))
151 (goto-char (point-max))
152 (insert-buffer-substring cur beg end
)))))))
153 (rmail-set-attribute "filed" t
))
154 (if redelete
(rmail-set-attribute "deleted" t
))))
155 (setq count
(1- count
))
156 (if rmail-delete-after-output
157 (rmail-delete-forward)
159 (rmail-next-undeleted-message 1))))))
161 ;; Returns t if file FILE is an Rmail file.
163 (defun rmail-file-p (file)
164 (let ((buf (generate-new-buffer " *rmail-file-p*")))
168 (insert-file-contents file nil
0 100)
169 (looking-at "BABYL OPTIONS:"))
172 ;;; There are functions elsewhere in Emacs that use this function; check
173 ;;; them out before you change the calling method.
174 (defun rmail-output (file-name &optional count noattribute from-gnus
)
175 "Append this message to Unix mail file named FILE-NAME.
176 A prefix argument N says to output N consecutive messages
177 starting with the current one. Deleted messages are skipped and don't count.
178 When called from lisp code, N may be omitted.
180 If the pruned message header is shown on the current message, then
181 messages will be appended with pruned headers; otherwise, messages
182 will be appended with their original headers.
184 The default file name comes from `rmail-last-file',
185 which is updated to the name you use in this command.
187 The optional third argument NOATTRIBUTE, if non-nil, says not
188 to set the `filed' attribute, and not to display a message.
190 The optional fourth argument FROM-GNUS is set when called from GNUS."
192 (list (setq rmail-default-file
194 (concat "Output message to Unix mail file"
195 (if rmail-default-file
197 (file-name-nondirectory rmail-default-file
)
200 (and rmail-default-file
(file-name-directory rmail-default-file
))
202 (prefix-numeric-value current-prefix-arg
)))
203 (or count
(setq count
1))
205 (expand-file-name file-name
206 (and rmail-default-file
207 (file-name-directory rmail-default-file
))))
208 (if (and (file-readable-p file-name
) (rmail-file-p file-name
))
209 (rmail-output-to-rmail-file file-name count
)
210 (let ((orig-count count
)
211 (rmailbuf (current-buffer))
213 (tembuf (get-buffer-create " rmail-output"))
218 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
219 (goto-char (point-min))
221 (= (following-char) ?
0)))))
230 (goto-char (rmail-msgbeg rmail-current-message
))
231 (setq header-beginning
(point))
232 (search-forward "\n*** EOOH ***\n")
233 (narrow-to-region header-beginning
(point))
234 (mail-fetch-field "Mail-From")))))
238 (insert-buffer-substring rmailbuf
)
240 (goto-char (point-min))
242 (insert mail-from
"\n")
244 (mail-strip-quoted-names (or (mail-fetch-field "from")
245 (mail-fetch-field "really-from")
246 (mail-fetch-field "sender")
248 " " (current-time-string) "\n"))
249 ;; ``Quote'' "\nFrom " as "\n>From "
250 ;; (note that this isn't really quoting, as there is no requirement
251 ;; that "\n[>]+From " be quoted in the same transparent way.)
252 (while (search-forward "\nFrom " nil t
)
255 (write-region (point-min) (point-max) file-name t
256 (if noattribute
'nomsg
)))
258 (if (equal major-mode
'rmail-mode
)
259 (rmail-set-attribute "filed" t
)))
260 (setq count
(1- count
))
262 (let ((next-message-p
263 (if rmail-delete-after-output
264 (rmail-delete-forward)
266 (rmail-next-undeleted-message 1))))
267 (num-appended (- orig-count count
)))
268 (if (and next-message-p original-headers-p
)
269 (rmail-toggle-header))
270 (if (and (> count
0) (not next-message-p
))
274 (set-buffer rmailbuf
)
275 (format "Only %d message%s appended" num-appended
276 (if (= num-appended
1) "" "s"))))
278 (kill-buffer tembuf
))))
280 ;;; rmailout.el ends here