]> code.delx.au - gnu-emacs/blob - lisp/mail/rmailout.el
(rmail-mode-map): Move new Re-send menu item to right place.
[gnu-emacs] / lisp / mail / rmailout.el
1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
2
3 ;; Copyright (C) 1985, 1987, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
23
24 ;;; Code:
25
26 (require 'rmail)
27
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.")
31
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.")
39
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."
45 (interactive "e")
46 (let ((file-name (rmail-secondary-file-menu event)))
47 (if file-name
48 (rmail-output-to-rmail-file
49 (setq rmail-default-rmail-file file-name)))))
50
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.
60
61 The default file name comes from `rmail-default-rmail-file',
62 which is updated to the name you use in this command.
63
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."
66 (interactive
67 (let ((default-file
68 (let (answer tail)
69 (setq tail rmail-output-file-alist)
70 ;; Suggest a file based on a pattern match.
71 (while (and tail (not answer))
72 (save-excursion
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
80 (let ((read-file
81 (read-file-name
82 (concat "Output message to Rmail file: (default "
83 (file-name-nondirectory default-file)
84 ") ")
85 (file-name-directory default-file)
86 default-file)))
87 (if (file-directory-p read-file)
88 (expand-file-name (file-name-nondirectory default-file)
89 read-file)
90 (expand-file-name
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))
95 (setq file-name
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)
104 (if (yes-or-no-p
105 (concat "\"" file-name "\" does not exist, create it? "))
106 (let ((file-buffer (create-file-buffer file-name)))
107 (save-excursion
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")))
114 (while (> count 0)
115 (let (redelete)
116 (unwind-protect
117 (progn
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)))
124 (save-restriction
125 (widen)
126 ;; Decide whether to append to a file or to an Emacs buffer.
127 (save-excursion
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))))
132 (if (not buf)
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.
137 (set-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.
142 (if msg
143 (progn
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)
147 auto-save-default
148 (auto-save-mode t))
149 (rmail-maybe-set-message-counters)
150 (widen)
151 (narrow-to-region (point-max) (point-max))
152 (insert-buffer-substring cur beg end)
153 (goto-char (point-min))
154 (widen)
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)
171 (if (> count 0)
172 (rmail-next-undeleted-message 1))))))
173
174 ;; Returns t if file FILE is an Rmail file.
175 ;;;###autoload
176 (defun rmail-file-p (file)
177 (let ((buf (generate-new-buffer " *rmail-file-p*")))
178 (unwind-protect
179 (save-excursion
180 (set-buffer buf)
181 (insert-file-contents file nil 0 100)
182 (looking-at "BABYL OPTIONS:"))
183 (kill-buffer buf))))
184
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.
192
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.
196
197 The default file name comes from `rmail-default-file',
198 which is updated to the name you use in this command.
199
200 The optional third argument NOATTRIBUTE, if non-nil, says not
201 to set the `filed' attribute, and not to display a message.
202
203 The optional fourth argument FROM-GNUS is set when called from GNUS."
204 (interactive
205 (let ((default-file
206 (let (answer tail)
207 (setq tail rmail-output-file-alist)
208 ;; Suggest a file based on a pattern match.
209 (while (and tail (not answer))
210 (save-excursion
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
218 (let ((read-file
219 (read-file-name
220 (concat "Output message to Unix mail file: (default "
221 (file-name-nondirectory default-file)
222 ") ")
223 (file-name-directory default-file)
224 default-file)))
225 (if (file-directory-p read-file)
226 (expand-file-name (file-name-nondirectory default-file)
227 read-file)
228 (expand-file-name
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))
233 (setq file-name
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))
241 (case-fold-search t)
242 (tembuf (get-buffer-create " rmail-output"))
243 (original-headers-p
244 (and (not from-gnus)
245 (save-excursion
246 (save-restriction
247 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
248 (goto-char (point-min))
249 (forward-line 1)
250 (= (following-char) ?0)))))
251 header-beginning
252 mail-from)
253 (while (> count 0)
254 (or from-gnus
255 (setq mail-from
256 (save-excursion
257 (save-restriction
258 (widen)
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")))))
264 (save-excursion
265 (set-buffer tembuf)
266 (erase-buffer)
267 (insert-buffer-substring rmailbuf)
268 (insert "\n")
269 (goto-char (point-min))
270 (if mail-from
271 (insert mail-from "\n")
272 (insert "From "
273 (mail-strip-quoted-names (or (mail-fetch-field "from")
274 (mail-fetch-field "really-from")
275 (mail-fetch-field "sender")
276 "unknown"))
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)
282 (forward-char -5)
283 (insert ?>))
284 (write-region (point-min) (point-max) file-name t
285 (if noattribute 'nomsg)))
286 (or noattribute
287 (if (equal major-mode 'rmail-mode)
288 (rmail-set-attribute "filed" t)))
289 (setq count (1- count))
290 (or from-gnus
291 (let ((next-message-p
292 (if rmail-delete-after-output
293 (rmail-delete-forward)
294 (if (> count 0)
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))
300 (progn
301 (error
302 (save-excursion
303 (set-buffer rmailbuf)
304 (format "Only %d message%s appended" num-appended
305 (if (= num-appended 1) "" "s"))))
306 (setq count 0))))))
307 (kill-buffer tembuf))))
308
309 ;;; rmailout.el ends here