0
|
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 XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
16
|
21 ;; along with XEmacs; 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.
|
0
|
24
|
|
25 ;;; Code:
|
|
26
|
|
27 ;; Temporary until Emacs always has this variable.
|
|
28 (defvar rmail-delete-after-output nil
|
|
29 "*Non-nil means automatically delete a message that is copied to a file.")
|
|
30
|
|
31 (defvar rmail-output-file-alist nil
|
|
32 "*Alist matching regexps to suggested output Rmail files.
|
|
33 This is a list of elements of the form (REGEXP . NAME-EXP).
|
|
34 The suggestion is taken if REGEXP matches anywhere in the message buffer.
|
|
35 NAME-EXP may be a string constant giving the file name to use,
|
|
36 or more generally it may be any kind of expression that returns
|
|
37 a file name as a string.")
|
|
38
|
|
39 ;;; There are functions elsewhere in Emacs that use this function; check
|
|
40 ;;; them out before you change the calling method.
|
|
41 (defun rmail-output-to-rmail-file (file-name &optional count)
|
|
42 "Append the current message to an Rmail file named FILE-NAME.
|
|
43 If the file does not exist, ask if it should be created.
|
|
44 If file is being visited, the message is appended to the Emacs
|
|
45 buffer visiting that file.
|
|
46 If the file exists and is not an Rmail file,
|
|
47 the message is appended in inbox format.
|
|
48
|
|
49 A prefix argument N says to output N consecutive messages
|
|
50 starting with the current one. Deleted messages are skipped and don't count."
|
|
51 (interactive
|
|
52 (let ((default-file
|
|
53 (let (answer tail)
|
|
54 (setq tail rmail-output-file-alist)
|
|
55 ;; Suggest a file based on a pattern match.
|
|
56 (while (and tail (not answer))
|
|
57 (save-excursion
|
|
58 (goto-char (point-min))
|
|
59 (if (re-search-forward (car (car tail)) nil t)
|
|
60 (setq answer (eval (cdr (car tail)))))
|
|
61 (setq tail (cdr tail))))
|
|
62 ;; If not suggestions, use same file as last time.
|
|
63 (or answer rmail-last-rmail-file))))
|
|
64 (list (setq rmail-last-rmail-file
|
70
|
65 (read-file-name
|
|
66 (concat "Output message to Rmail file: (default "
|
|
67 (file-name-nondirectory default-file)
|
|
68 ") ")
|
|
69 (file-name-directory default-file)
|
|
70 default-file))
|
0
|
71 (prefix-numeric-value current-prefix-arg))))
|
|
72 (or count (setq count 1))
|
|
73 (setq file-name
|
|
74 (expand-file-name file-name
|
|
75 (file-name-directory rmail-last-rmail-file)))
|
|
76 (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
|
|
77 (rmail-output file-name count)
|
|
78 (rmail-maybe-set-message-counters)
|
|
79 (setq file-name (abbreviate-file-name file-name))
|
|
80 (or (get-file-buffer file-name)
|
|
81 (file-exists-p file-name)
|
|
82 (if (yes-or-no-p
|
|
83 (concat "\"" file-name "\" does not exist, create it? "))
|
|
84 (let ((file-buffer (create-file-buffer file-name)))
|
|
85 (save-excursion
|
|
86 (set-buffer file-buffer)
|
|
87 (rmail-insert-rmail-file-header)
|
|
88 (let ((require-final-newline nil))
|
|
89 (write-region (point-min) (point-max) file-name t 1)))
|
|
90 (kill-buffer file-buffer))
|
|
91 (error "Output file does not exist")))
|
|
92 (while (> count 0)
|
|
93 (let (redelete)
|
|
94 (unwind-protect
|
|
95 (progn
|
|
96 (save-restriction
|
|
97 (widen)
|
|
98 (if (rmail-message-deleted-p rmail-current-message)
|
|
99 (progn (setq redelete t)
|
|
100 (rmail-set-attribute "deleted" nil)))
|
|
101 ;; Decide whether to append to a file or to an Emacs buffer.
|
|
102 (save-excursion
|
|
103 (let ((buf (get-file-buffer file-name))
|
|
104 (cur (current-buffer))
|
|
105 (beg (1+ (rmail-msgbeg rmail-current-message)))
|
|
106 (end (1+ (rmail-msgend rmail-current-message))))
|
|
107 (if (not buf)
|
|
108 (append-to-file beg end file-name)
|
|
109 (if (eq buf (current-buffer))
|
|
110 (error "Can't output message to same file it's already in"))
|
|
111 ;; File has been visited, in buffer BUF.
|
|
112 (set-buffer buf)
|
|
113 (let ((buffer-read-only nil)
|
|
114 (msg (and (boundp 'rmail-current-message)
|
|
115 rmail-current-message)))
|
|
116 ;; If MSG is non-nil, buffer is in RMAIL mode.
|
|
117 (if msg
|
|
118 (progn
|
|
119 (rmail-maybe-set-message-counters)
|
|
120 (widen)
|
|
121 (narrow-to-region (point-max) (point-max))
|
|
122 (insert-buffer-substring cur beg end)
|
|
123 (goto-char (point-min))
|
|
124 (widen)
|
|
125 (search-backward "\n\^_")
|
|
126 (narrow-to-region (point) (point-max))
|
|
127 (rmail-count-new-messages t)
|
|
128 (rmail-show-message msg))
|
|
129 ;; Output file not in rmail mode => just insert at
|
|
130 ;; the end.
|
|
131 (narrow-to-region (point-min) (1+ (buffer-size)))
|
|
132 (goto-char (point-max))
|
|
133 (insert-buffer-substring cur beg end)))))))
|
|
134 (rmail-set-attribute "filed" t))
|
|
135 (if redelete (rmail-set-attribute "deleted" t))))
|
|
136 (setq count (1- count))
|
|
137 (if rmail-delete-after-output
|
|
138 (rmail-delete-forward)
|
|
139 (if (> count 0)
|
|
140 (rmail-next-undeleted-message 1))))))
|
|
141
|
|
142 ;; Returns t if file FILE is an Rmail file.
|
|
143 ;;;###autoload
|
|
144 (defun rmail-file-p (file)
|
|
145 (let ((buf (generate-new-buffer " *rmail-file-p*")))
|
|
146 (unwind-protect
|
|
147 (save-excursion
|
|
148 (set-buffer buf)
|
|
149 (insert-file-contents file nil 0 100)
|
|
150 (looking-at "BABYL OPTIONS:"))
|
|
151 (kill-buffer buf))))
|
|
152
|
|
153 ;;; There are functions elsewhere in Emacs that use this function; check
|
|
154 ;;; them out before you change the calling method.
|
|
155 ;;; ####Boy, FROM-GNUS, what wonderful abstraction. You loser.
|
|
156 (defun rmail-output (file-name &optional count noattribute from-gnus)
|
|
157 "Append this message to Unix mail file named FILE-NAME.
|
|
158 A prefix argument N says to output N consecutive messages
|
|
159 starting with the current one. Deleted messages are skipped and don't count.
|
|
160 When called from lisp code, N may be omitted.
|
|
161
|
|
162 If the pruned message header is shown on the current message, then
|
|
163 messages will be appended with pruned headers; otherwise, messages
|
|
164 will be appended with their original headers.
|
|
165
|
|
166 The optional third argument NOATTRIBUTE, if non-nil, says not
|
|
167 to set the `filed' attribute, and not to display a message.
|
|
168
|
|
169 The optional fourth argument FROM-GNUS is set when called from GNUS."
|
|
170 (interactive
|
|
171 (list (setq rmail-last-file
|
|
172 (read-file-name
|
|
173 (concat "Output message to Unix mail file"
|
|
174 (if rmail-last-file
|
|
175 (concat " (default "
|
|
176 (file-name-nondirectory rmail-last-file)
|
|
177 "): " )
|
|
178 ": "))
|
|
179 (and rmail-last-file (file-name-directory rmail-last-file))
|
|
180 rmail-last-file))
|
|
181 (prefix-numeric-value current-prefix-arg)))
|
|
182 (or count (setq count 1))
|
|
183 (setq file-name
|
|
184 (expand-file-name file-name
|
|
185 (and rmail-last-file
|
|
186 (file-name-directory rmail-last-file))))
|
|
187 (if (and (file-readable-p file-name) (rmail-file-p file-name))
|
|
188 (rmail-output-to-rmail-file file-name count)
|
|
189 (let ((orig-count count)
|
|
190 (rmailbuf (current-buffer))
|
|
191 (case-fold-search t)
|
|
192 (tembuf (get-buffer-create " rmail-output"))
|
|
193 (original-headers-p
|
|
194 (and (not from-gnus)
|
|
195 (save-excursion
|
|
196 (save-restriction
|
|
197 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
|
|
198 (goto-char (point-min))
|
|
199 (forward-line 1)
|
|
200 (= (following-char) ?0)))))
|
|
201 header-beginning
|
|
202 mail-from)
|
|
203 (while (> count 0)
|
|
204 (or from-gnus
|
|
205 (setq mail-from
|
|
206 (save-excursion
|
|
207 (save-restriction
|
|
208 (widen)
|
|
209 (goto-char (rmail-msgbeg rmail-current-message))
|
|
210 (setq header-beginning (point))
|
|
211 (search-forward "\n*** EOOH ***\n")
|
|
212 (narrow-to-region header-beginning (point))
|
|
213 (mail-fetch-field "Mail-From")))))
|
|
214 (save-excursion
|
|
215 (set-buffer tembuf)
|
|
216 (erase-buffer)
|
|
217 (insert-buffer-substring rmailbuf)
|
|
218 (insert "\n")
|
|
219 (goto-char (point-min))
|
|
220 (if mail-from
|
|
221 (insert mail-from "\n")
|
|
222 (insert "From "
|
|
223 (mail-strip-quoted-names (or (mail-fetch-field "from")
|
|
224 (mail-fetch-field "really-from")
|
|
225 (mail-fetch-field "sender")
|
|
226 "unknown"))
|
|
227 " " (current-time-string) "\n"))
|
|
228 ;; ``Quote'' "\nFrom " as "\n>From "
|
|
229 ;; (note that this isn't really quoting, as there is no requirement
|
|
230 ;; that "\n[>]+From " be quoted in the same transparent way.)
|
|
231 (while (search-forward "\nFrom " nil t)
|
|
232 (forward-char -5)
|
|
233 (insert ?>))
|
|
234 (write-region (point-min) (point-max) file-name t
|
|
235 (if noattribute 'nomsg)))
|
|
236 (or noattribute
|
|
237 (if (equal major-mode 'rmail-mode)
|
|
238 (rmail-set-attribute "filed" t)))
|
|
239 (setq count (1- count))
|
|
240 (or from-gnus
|
|
241 (let ((next-message-p
|
|
242 (if rmail-delete-after-output
|
|
243 (rmail-delete-forward)
|
|
244 (if (> count 0)
|
|
245 (rmail-next-undeleted-message 1))))
|
|
246 (num-appended (- orig-count count)))
|
|
247 (if (and next-message-p original-headers-p)
|
|
248 (rmail-toggle-header))
|
|
249 (if (and (> count 0) (not next-message-p))
|
|
250 (progn
|
|
251 (error
|
|
252 (save-excursion
|
|
253 (set-buffer rmailbuf)
|
|
254 (format "Only %d message%s appended" num-appended
|
|
255 (if (= num-appended 1) "" "s"))))
|
|
256 (setq count 0))))))
|
|
257 (kill-buffer tembuf))))
|
|
258
|
|
259 ;;; rmailout.el ends here
|