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