]> code.delx.au - gnu-emacs/blob - lisp/mail/rmailout.el
(rmail-file-p): Add autoload cookie.
[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 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 ;; 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.")
29
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.")
37
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.
47
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."
50 (interactive
51 (let ((default-file
52 (let (answer tail)
53 (setq tail rmail-output-file-alist)
54 ;; Suggest a file based on a pattern match.
55 (while (and tail (not answer))
56 (save-excursion
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
64 (read-file-name
65 (concat "Output message to Rmail file: (default "
66 (file-name-nondirectory default-file)
67 ") ")
68 (file-name-directory default-file)
69 default-file))
70 (prefix-numeric-value current-prefix-arg))))
71 (or count (setq count 1))
72 (setq file-name
73 (expand-file-name file-name
74 (file-name-directory rmail-last-rmail-file)))
75 (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
76 (rmail-output file-name count)
77 (rmail-maybe-set-message-counters)
78 (setq file-name (abbreviate-file-name file-name))
79 (or (get-file-buffer file-name)
80 (file-exists-p file-name)
81 (if (yes-or-no-p
82 (concat "\"" file-name "\" does not exist, create it? "))
83 (let ((file-buffer (create-file-buffer file-name)))
84 (save-excursion
85 (set-buffer file-buffer)
86 (rmail-insert-rmail-file-header)
87 (let ((require-final-newline nil))
88 (write-region (point-min) (point-max) file-name t 1)))
89 (kill-buffer file-buffer))
90 (error "Output file does not exist")))
91 (while (> count 0)
92 (let (redelete)
93 (unwind-protect
94 (progn
95 (save-restriction
96 (widen)
97 (if (rmail-message-deleted-p rmail-current-message)
98 (progn (setq redelete t)
99 (rmail-set-attribute "deleted" nil)))
100 ;; Decide whether to append to a file or to an Emacs buffer.
101 (save-excursion
102 (let ((buf (get-file-buffer file-name))
103 (cur (current-buffer))
104 (beg (1+ (rmail-msgbeg rmail-current-message)))
105 (end (1+ (rmail-msgend rmail-current-message))))
106 (if (not buf)
107 (append-to-file beg end file-name)
108 (if (eq buf (current-buffer))
109 (error "Can't output message to same file it's already in"))
110 ;; File has been visited, in buffer BUF.
111 (set-buffer buf)
112 (let ((buffer-read-only nil)
113 (msg (and (boundp 'rmail-current-message)
114 rmail-current-message)))
115 ;; If MSG is non-nil, buffer is in RMAIL mode.
116 (if msg
117 (progn
118 (rmail-maybe-set-message-counters)
119 (widen)
120 (narrow-to-region (point-max) (point-max))
121 (insert-buffer-substring cur beg end)
122 (goto-char (point-min))
123 (widen)
124 (search-backward "\n\^_")
125 (narrow-to-region (point) (point-max))
126 (rmail-count-new-messages t)
127 (rmail-show-message msg))
128 ;; Output file not in rmail mode => just insert at the end.
129 (narrow-to-region (point-min) (1+ (buffer-size)))
130 (goto-char (point-max))
131 (insert-buffer-substring cur beg end)))))))
132 (rmail-set-attribute "filed" t))
133 (if redelete (rmail-set-attribute "deleted" t))))
134 (setq count (1- count))
135 (if rmail-delete-after-output
136 (rmail-delete-forward)
137 (if (> count 0)
138 (rmail-next-undeleted-message 1))))))
139
140 ;; Returns t if file FILE is an Rmail file.
141 ;;;###autoload
142 (defun rmail-file-p (file)
143 (let ((buf (generate-new-buffer " *rmail-file-p*")))
144 (unwind-protect
145 (save-excursion
146 (set-buffer buf)
147 (insert-file-contents file nil 0 100)
148 (looking-at "BABYL OPTIONS:"))
149 (kill-buffer buf))))
150
151 ;;; There are functions elsewhere in Emacs that use this function; check
152 ;;; them out before you change the calling method.
153 (defun rmail-output (file-name &optional count noattribute from-gnus)
154 "Append this message to Unix mail file named FILE-NAME.
155 A prefix argument N says to output N consecutive messages
156 starting with the current one. Deleted messages are skipped and don't count.
157 When called from lisp code, N may be omitted.
158
159 If the pruned message header is shown on the current message, then
160 messages will be appended with pruned headers; otherwise, messages
161 will be appended with their original headers.
162
163 The optional third argument NOATTRIBUTE, if non-nil, says not
164 to set the `filed' attribute, and not to display a message.
165
166 The optional fourth argument FROM-GNUS is set when called from GNUS."
167 (interactive
168 (list (setq rmail-last-file
169 (read-file-name
170 (concat "Output message to Unix mail file"
171 (if rmail-last-file
172 (concat " (default "
173 (file-name-nondirectory rmail-last-file)
174 "): " )
175 ": "))
176 (and rmail-last-file (file-name-directory rmail-last-file))
177 rmail-last-file))
178 (prefix-numeric-value current-prefix-arg)))
179 (or count (setq count 1))
180 (setq file-name
181 (expand-file-name file-name
182 (and rmail-last-file
183 (file-name-directory rmail-last-file))))
184 (if (and (file-readable-p file-name) (rmail-file-p file-name))
185 (rmail-output-to-rmail-file file-name count)
186 (let ((orig-count count)
187 (rmailbuf (current-buffer))
188 (case-fold-search t)
189 (tembuf (get-buffer-create " rmail-output"))
190 (original-headers-p
191 (and (not from-gnus)
192 (save-excursion
193 (save-restriction
194 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
195 (goto-char (point-min))
196 (forward-line 1)
197 (= (following-char) ?0)))))
198 header-beginning
199 mail-from)
200 (while (> count 0)
201 (or from-gnus
202 (setq mail-from
203 (save-excursion
204 (save-restriction
205 (widen)
206 (goto-char (rmail-msgbeg rmail-current-message))
207 (setq header-beginning (point))
208 (search-forward "\n*** EOOH ***\n")
209 (narrow-to-region header-beginning (point))
210 (mail-fetch-field "Mail-From")))))
211 (save-excursion
212 (set-buffer tembuf)
213 (erase-buffer)
214 (insert-buffer-substring rmailbuf)
215 (insert "\n")
216 (goto-char (point-min))
217 (if mail-from
218 (insert mail-from "\n")
219 (insert "From "
220 (mail-strip-quoted-names (or (mail-fetch-field "from")
221 (mail-fetch-field "really-from")
222 (mail-fetch-field "sender")
223 "unknown"))
224 " " (current-time-string) "\n"))
225 ;; ``Quote'' "\nFrom " as "\n>From "
226 ;; (note that this isn't really quoting, as there is no requirement
227 ;; that "\n[>]+From " be quoted in the same transparent way.)
228 (while (search-forward "\nFrom " nil t)
229 (forward-char -5)
230 (insert ?>))
231 (write-region (point-min) (point-max) file-name t
232 (if noattribute 'nomsg)))
233 (or noattribute
234 (if (equal major-mode 'rmail-mode)
235 (rmail-set-attribute "filed" t)))
236 (setq count (1- count))
237 (or from-gnus
238 (let ((next-message-p
239 (if rmail-delete-after-output
240 (rmail-delete-forward)
241 (if (> count 0)
242 (rmail-next-undeleted-message 1))))
243 (num-appended (- orig-count count)))
244 (if (and next-message-p original-headers-p)
245 (rmail-toggle-header))
246 (if (and (> count 0) (not next-message-p))
247 (progn
248 (error
249 (save-excursion
250 (set-buffer rmailbuf)
251 (format "Only %d message%s appended" num-appended
252 (if (= num-appended 1) "" "s"))))
253 (setq count 0))))))
254 (kill-buffer tembuf))))
255
256 ;;; rmailout.el ends here