annotate lisp/mail/rmailout.el @ 145:1a0f15a66c06

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Mon, 24 Dec 1990 22:05:55 +0000
parents 714e88ef93c8
children ae5ace097df2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
1 ;; "RMAIL" mail reader for Emacs: output message to a file.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
2 ;; Copyright (C) 1985, 1987 Free Software Foundation, Inc.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
3
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
4 ;; This file is part of GNU Emacs.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
5
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
7 ;; it under the terms of the GNU General Public License as published by
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
8 ;; the Free Software Foundation; either version 1, or (at your option)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
9 ;; any later version.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
10
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
11 ;; GNU Emacs is distributed in the hope that it will be useful,
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
14 ;; GNU General Public License for more details.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
15
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
16 ;; You should have received a copy of the GNU General Public License
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
19
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
20
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
21 ;; Temporary until Emacs always has this variable.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
22 (defvar rmail-delete-after-output nil
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
23 "*Non-nil means automatically delete a message that is copied to a file.")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
24
145
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
25 (defvar rmail-output-file-alist nil
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
26 "*Alist matching regexps to suggested output Rmail files.
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
27 This is a list of elements of the form (REGEXP . FILENAME).")
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
28
63
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
29 (defun rmail-output-to-rmail-file (count file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
30 "Append the current message to an Rmail file named FILE-NAME.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
31 If the file does not exist, ask if it should be created.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
32 If file is being visited, the message is appended to the Emacs
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
33 buffer visiting that file.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
34 A prefix argument N says to output N consecutive messages
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
35 starting with the current one. Deleted messages are skipped and don't count."
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
36 (interactive (list (prefix-numeric-value current-prefix-arg)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
37 (read-file-name
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
38 (concat "Output message to Rmail file: (default "
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
39 (file-name-nondirectory rmail-last-rmail-file)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
40 ") ")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
41 (file-name-directory rmail-last-rmail-file)
145
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
42 (let (answer tail)
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
43 (setq tail rmail-output-file-alist)
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
44 ;; Suggest a file based on a pattern match.
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
45 (while (and tail (not answer))
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
46 (save-excursion
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
47 (goto-char (point-min))
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
48 (if (re-search-forward (car (car tail)) nil t)
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
49 (setq answer (cdr (car tail))))
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
50 (setq tail (cdr tail))))
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
51 ;; If not suggestions, use same file as last time.
1a0f15a66c06 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 63
diff changeset
52 (or answer rmail-last-rmail-file)))))
63
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
53 (setq file-name (expand-file-name file-name))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
54 (setq rmail-last-rmail-file file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
55 (rmail-maybe-set-message-counters)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
56 (or (get-file-buffer file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
57 (file-exists-p file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
58 (if (yes-or-no-p
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
59 (concat "\"" file-name "\" does not exist, create it? "))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
60 (let ((file-buffer (create-file-buffer file-name)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
61 (save-excursion
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
62 (set-buffer file-buffer)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
63 (rmail-insert-rmail-file-header)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
64 (let ((require-final-newline nil))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
65 (write-region (point-min) (point-max) file-name t 1)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
66 (kill-buffer file-buffer))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
67 (error "Output file does not exist")))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
68 (while (> count 0)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
69 (let (redelete)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
70 (unwind-protect
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
71 (progn
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
72 (save-restriction
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
73 (widen)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
74 (if (rmail-message-deleted-p rmail-current-message)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
75 (progn (setq redelete t)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
76 (rmail-set-attribute "deleted" nil)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
77 ;; Decide whether to append to a file or to an Emacs buffer.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
78 (save-excursion
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
79 (let ((buf (get-file-buffer file-name))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
80 (cur (current-buffer))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
81 (beg (1+ (rmail-msgbeg rmail-current-message)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
82 (end (1+ (rmail-msgend rmail-current-message))))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
83 (if (not buf)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
84 (append-to-file beg end file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
85 (if (eq buf (current-buffer))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
86 (error "Can't output message to same file it's already in"))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
87 ;; File has been visited, in buffer BUF.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
88 (set-buffer buf)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
89 (let ((buffer-read-only nil)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
90 (msg (and (boundp 'rmail-current-message)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
91 rmail-current-message)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
92 ;; If MSG is non-nil, buffer is in RMAIL mode.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
93 (if msg
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
94 (progn
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
95 (rmail-maybe-set-message-counters)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
96 (widen)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
97 (narrow-to-region (point-max) (point-max))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
98 (insert-buffer-substring cur beg end)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
99 (goto-char (point-min))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
100 (widen)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
101 (search-backward "\n\^_")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
102 (narrow-to-region (point) (point-max))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
103 (rmail-count-new-messages t)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
104 (rmail-show-message msg))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
105 ;; Output file not in rmail mode => just insert at the end.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
106 (narrow-to-region (point-min) (1+ (buffer-size)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
107 (goto-char (point-max))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
108 (insert-buffer-substring cur beg end)))))))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
109 (rmail-set-attribute "filed" t))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
110 (if redelete (rmail-set-attribute "deleted" t))))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
111 (setq count (1- count))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
112 (if rmail-delete-after-output
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
113 (rmail-delete-forward)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
114 (if (> count 0)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
115 (rmail-next-undeleted-message 1)))))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
116
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
117 (defun rmail-output (count file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
118 "Append this message to Unix mail file named FILE-NAME.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
119 A prefix argument N says to output N consecutive messages
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
120 starting with the current one. Deleted messages are skipped and don't count."
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
121 (interactive
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
122 (list (prefix-numeric-value current-prefix-arg)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
123 (read-file-name
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
124 (concat "Output message to Unix mail file"
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
125 (if rmail-last-file
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
126 (concat " (default "
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
127 (file-name-nondirectory rmail-last-file)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
128 "): " )
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
129 ": "))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
130 (and rmail-last-file (file-name-directory rmail-last-file))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
131 rmail-last-file)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
132 (setq file-name (expand-file-name file-name))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
133 (setq rmail-last-file file-name)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
134 (while (> count 0)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
135 (let ((rmailbuf (current-buffer))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
136 (tembuf (get-buffer-create " rmail-output"))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
137 (case-fold-search t))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
138 (save-excursion
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
139 (set-buffer tembuf)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
140 (erase-buffer)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
141 ;; If we can do it, read a little of the file
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
142 ;; to check whether it is an RMAIL file.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
143 ;; If it is, don't mess it up.
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
144 (if (fboundp 'insert-partial-file-contents)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
145 (progn
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
146 (insert-partial-file-contents file-name 0 20)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
147 (if (looking-at "BABYL OPTIONS:\n")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
148 (error (save-excursion
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
149 (set-buffer rmailbuf)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
150 (substitute-command-keys
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
151 "File %s is an RMAIL file; use the \\[rmail-output-to-rmail-file] command"))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
152 file-name))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
153 (erase-buffer)))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
154 (insert-buffer-substring rmailbuf)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
155 (insert "\n")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
156 (goto-char (point-min))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
157 (insert "From "
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
158 (mail-strip-quoted-names (or (mail-fetch-field "from")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
159 (mail-fetch-field "really-from")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
160 (mail-fetch-field "sender")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
161 "unknown"))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
162 " " (current-time-string) "\n")
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
163 ;; ``Quote'' "\nFrom " as "\n>From "
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
164 ;; (note that this isn't really quoting, as there is no requirement
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
165 ;; that "\n[>]+From " be quoted in the same transparent way.)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
166 (while (search-forward "\nFrom " nil t)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
167 (forward-char -5)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
168 (insert ?>))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
169 (append-to-file (point-min) (point-max) file-name))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
170 (kill-buffer tembuf))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
171 (if (equal major-mode 'rmail-mode)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
172 (rmail-set-attribute "filed" t))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
173 (setq count (1- count))
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
174 (if rmail-delete-after-output
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
175 (rmail-delete-forward)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
176 (if (> count 0)
714e88ef93c8 Initial revision
root <root>
parents:
diff changeset
177 (rmail-next-undeleted-message 1)))))