comparison lisp/rmail/rmailout.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 0293115a14e9
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
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
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; 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
129 ;; the end.
130 (narrow-to-region (point-min) (1+ (buffer-size)))
131 (goto-char (point-max))
132 (insert-buffer-substring cur beg end)))))))
133 (rmail-set-attribute "filed" t))
134 (if redelete (rmail-set-attribute "deleted" t))))
135 (setq count (1- count))
136 (if rmail-delete-after-output
137 (rmail-delete-forward)
138 (if (> count 0)
139 (rmail-next-undeleted-message 1))))))
140
141 ;; Returns t if file FILE is an Rmail file.
142 ;;;###autoload
143 (defun rmail-file-p (file)
144 (let ((buf (generate-new-buffer " *rmail-file-p*")))
145 (unwind-protect
146 (save-excursion
147 (set-buffer buf)
148 (insert-file-contents file nil 0 100)
149 (looking-at "BABYL OPTIONS:"))
150 (kill-buffer buf))))
151
152 ;;; There are functions elsewhere in Emacs that use this function; check
153 ;;; them out before you change the calling method.
154 ;;; ####Boy, FROM-GNUS, what wonderful abstraction. You loser.
155 (defun rmail-output (file-name &optional count noattribute from-gnus)
156 "Append this message to Unix mail file named FILE-NAME.
157 A prefix argument N says to output N consecutive messages
158 starting with the current one. Deleted messages are skipped and don't count.
159 When called from lisp code, N may be omitted.
160
161 If the pruned message header is shown on the current message, then
162 messages will be appended with pruned headers; otherwise, messages
163 will be appended with their original headers.
164
165 The optional third argument NOATTRIBUTE, if non-nil, says not
166 to set the `filed' attribute, and not to display a message.
167
168 The optional fourth argument FROM-GNUS is set when called from GNUS."
169 (interactive
170 (list (setq rmail-last-file
171 (read-file-name
172 (concat "Output message to Unix mail file"
173 (if rmail-last-file
174 (concat " (default "
175 (file-name-nondirectory rmail-last-file)
176 "): " )
177 ": "))
178 (and rmail-last-file (file-name-directory rmail-last-file))
179 rmail-last-file))
180 (prefix-numeric-value current-prefix-arg)))
181 (or count (setq count 1))
182 (setq file-name
183 (expand-file-name file-name
184 (and rmail-last-file
185 (file-name-directory rmail-last-file))))
186 (if (and (file-readable-p file-name) (rmail-file-p file-name))
187 (rmail-output-to-rmail-file file-name count)
188 (let ((orig-count count)
189 (rmailbuf (current-buffer))
190 (case-fold-search t)
191 (tembuf (get-buffer-create " rmail-output"))
192 (original-headers-p
193 (and (not from-gnus)
194 (save-excursion
195 (save-restriction
196 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
197 (goto-char (point-min))
198 (forward-line 1)
199 (= (following-char) ?0)))))
200 header-beginning
201 mail-from)
202 (while (> count 0)
203 (or from-gnus
204 (setq mail-from
205 (save-excursion
206 (save-restriction
207 (widen)
208 (goto-char (rmail-msgbeg rmail-current-message))
209 (setq header-beginning (point))
210 (search-forward "\n*** EOOH ***\n")
211 (narrow-to-region header-beginning (point))
212 (mail-fetch-field "Mail-From")))))
213 (save-excursion
214 (set-buffer tembuf)
215 (erase-buffer)
216 (insert-buffer-substring rmailbuf)
217 (insert "\n")
218 (goto-char (point-min))
219 (if mail-from
220 (insert mail-from "\n")
221 (insert "From "
222 (mail-strip-quoted-names (or (mail-fetch-field "from")
223 (mail-fetch-field "really-from")
224 (mail-fetch-field "sender")
225 "unknown"))
226 " " (current-time-string) "\n"))
227 ;; ``Quote'' "\nFrom " as "\n>From "
228 ;; (note that this isn't really quoting, as there is no requirement
229 ;; that "\n[>]+From " be quoted in the same transparent way.)
230 (while (search-forward "\nFrom " nil t)
231 (forward-char -5)
232 (insert ?>))
233 (write-region (point-min) (point-max) file-name t
234 (if noattribute 'nomsg)))
235 (or noattribute
236 (if (equal major-mode 'rmail-mode)
237 (rmail-set-attribute "filed" t)))
238 (setq count (1- count))
239 (or from-gnus
240 (let ((next-message-p
241 (if rmail-delete-after-output
242 (rmail-delete-forward)
243 (if (> count 0)
244 (rmail-next-undeleted-message 1))))
245 (num-appended (- orig-count count)))
246 (if (and next-message-p original-headers-p)
247 (rmail-toggle-header))
248 (if (and (> count 0) (not next-message-p))
249 (progn
250 (error
251 (save-excursion
252 (set-buffer rmailbuf)
253 (format "Only %d message%s appended" num-appended
254 (if (= num-appended 1) "" "s"))))
255 (setq count 0))))))
256 (kill-buffer tembuf))))
257
258 ;;; rmailout.el ends here