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