Mercurial > emacs
annotate lisp/mail/mail-hist.el @ 10450:830e201e9603
(remote-shell-program): New var.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Wed, 18 Jan 1995 19:46:00 +0000 |
parents | 7d0509b6f039 |
children | 19442fbb6972 |
rev | line source |
---|---|
7267 | 1 ;;; mail-hist.el --- Headers and message body history for outgoing mail. |
2 ;; Copyright (C) 1994 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Karl Fogel <kfogel@cs.oberlin.edu> | |
5 ;; Created: March, 1994 | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
6 ;; Version: 1.3 |
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
7 ;; Keywords: mail, history |
7267 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;;; Commentary: | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
24 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | |
27 ;; Thanks to Jim Blandy for mentioning ring.el. It saved a lot of | |
28 ;; time. | |
29 ;; | |
30 ;; To use this package, put it in a directory in your load-path, and | |
31 ;; put this in your .emacs file: | |
32 ;; | |
33 ;; (load "mail-hist" nil t) | |
34 ;; | |
35 ;; Or you could do it with autoloads and hooks in your .emacs: | |
36 ;; | |
37 ;; (add-hook 'mail-mode-hook 'mail-hist-define-keys) | |
38 ;; (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history) | |
39 ;; (add-hook 'vm-mail-mode-hook 'mail-hist-define-keys) ;or rmail, etc | |
40 ;; (autoload 'mail-hist-define-keys "mail-hist") | |
41 ;; (autoload 'mail-hist-put-headers-into-history "mail-hist") | |
42 ;; | |
43 ;; Once it's installed, use M-p and M-n from mail headers to recover | |
44 ;; previous/next contents in the history for that header, or, in the | |
45 ;; body of the message, to recover previous/next text of the message. | |
46 ;; This only applies to outgoing mail -- mail-hist ignores received | |
47 ;; messages. | |
48 ;; | |
49 ;; Although repeated history requests do clear out the text from the | |
50 ;; previous request, an isolated request just inserts its text at | |
51 ;; point, so that you can mix the histories of different messages | |
52 ;; easily. This might be confusing at times, but there should be no | |
53 ;; problems that undo can't handle. | |
54 | |
55 ;;; Code: | |
56 (require 'ring) | |
57 | |
58 ;;;###autoload | |
59 (defun mail-hist-define-keys () | |
60 "Define keys for accessing mail header history. For use in hooks." | |
61 (local-set-key "\M-p" 'mail-hist-previous-input) | |
62 (local-set-key "\M-n" 'mail-hist-next-input)) | |
63 | |
64 ;;;###autoload | |
65 (add-hook 'mail-mode-hook 'mail-hist-define-keys) | |
66 | |
67 ;;;###autoload | |
68 (add-hook 'vm-mail-mode-hook 'mail-hist-define-keys) | |
69 | |
70 ;;;###autoload | |
71 (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history) | |
72 | |
73 (defvar mail-hist-header-ring-alist nil | |
74 "Alist of form (header-name . history-ring). | |
75 Used for knowing which history list to look in when the user asks for | |
76 previous/next input.") | |
77 | |
78 (defvar mail-hist-history-size (or kill-ring-max 1729) | |
79 "*The maximum number of elements in a mail field's history. | |
80 Oldest elements are dumped first.") | |
81 | |
82 ;;;###autoload | |
83 (defvar mail-hist-keep-history t | |
84 "*Non-nil means keep a history for headers and text of outgoing mail.") | |
85 | |
86 ;; For handling repeated history requests | |
87 (defvar mail-hist-access-count 0) | |
88 | |
89 (defvar mail-hist-last-bounds nil) | |
90 ;; (start . end) A pair indicating the buffer positions delimiting the | |
91 ;; last inserted history, so it can be replaced by a new input if the | |
92 ;; command is repeated. | |
93 | |
94 (defvar mail-hist-header-regexp "^[^:]*:" | |
95 "Regular expression for matching headers in a mail message.") | |
96 | |
97 (defsubst mail-hist-current-header-name () | |
98 "Get name of mail header point is currently in, without the colon. | |
99 Returns nil if not in a header, implying that point is in the body of | |
100 the message." | |
101 (if (save-excursion | |
9425
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
102 (re-search-backward (concat "^" (regexp-quote mail-header-separator)) |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
103 nil t)) |
7267 | 104 nil ; then we are in the body of the message |
105 (save-excursion | |
106 (let* ((body-start ; limit possibility of false headers | |
107 (save-excursion | |
9425
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
108 (re-search-forward |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
109 (concat "^" (regexp-quote mail-header-separator)) |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
110 nil t))) |
7267 | 111 (name-start |
112 (re-search-backward mail-hist-header-regexp nil t)) | |
113 (name-end | |
114 (prog2 (search-forward ":" body-start t) (1- (point))))) | |
115 (and | |
116 name-start | |
117 name-end | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
118 (downcase (buffer-substring name-start name-end))))))) |
7267 | 119 |
120 (defsubst mail-hist-forward-header (count) | |
121 "Move forward COUNT headers (backward if COUNT is negative). | |
122 If last/first header is encountered first, stop there and returns | |
123 nil. | |
124 | |
125 Places point on the first non-whitespace on the line following the | |
126 colon after the header name, or on the second space following that if | |
127 the header is empty." | |
128 (let ((boundary (save-excursion | |
9425
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
129 (re-search-forward |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
130 (concat "^" (regexp-quote mail-header-separator)) |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
131 nil t)))) |
7267 | 132 (and |
133 boundary | |
134 (let ((unstopped t)) | |
135 (setq boundary (save-excursion | |
136 (goto-char boundary) | |
137 (beginning-of-line) | |
138 (1- (point)))) | |
139 (if (> count 0) | |
140 (while (> count 0) | |
141 (setq | |
142 unstopped | |
143 (re-search-forward mail-hist-header-regexp boundary t)) | |
144 (setq count (1- count))) | |
145 ;; because the current header will match too. | |
146 (setq count (1- count)) | |
147 ;; count is negative | |
148 (while (< count 0) | |
149 (setq | |
150 unstopped | |
151 (re-search-backward mail-hist-header-regexp nil t)) | |
152 (setq count (1+ count))) | |
153 ;; we end up behind the header, so must move to the front | |
154 (re-search-forward mail-hist-header-regexp boundary t)) | |
155 ;; Now we are right after the colon | |
156 (and (looking-at "\\s-") (forward-char 1)) | |
157 ;; return nil if didn't go as far as asked, otherwise point | |
158 unstopped)))) | |
159 | |
160 (defsubst mail-hist-beginning-of-header () | |
161 "Move to the start of the current header. | |
162 The start of the current header is defined as one space after the | |
163 colon, or just after the colon if it is not followed by whitespace." | |
164 ;; this is slick as all heck: | |
165 (if (mail-hist-forward-header -1) | |
166 (mail-hist-forward-header 1) | |
167 (mail-hist-forward-header 1) | |
168 (mail-hist-forward-header -1))) | |
169 | |
170 (defsubst mail-hist-current-header-contents () | |
171 "Get the contents of the mail header in which point is located." | |
172 (save-excursion | |
173 (mail-hist-beginning-of-header) | |
174 (let ((start (point))) | |
175 (or (mail-hist-forward-header 1) | |
9425
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
176 (re-search-forward |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
177 (concat "^" (regexp-quote mail-header-separator)))) |
7267 | 178 (beginning-of-line) |
179 (buffer-substring start (1- (point)))))) | |
180 | |
181 (defsubst mail-hist-get-header-ring (header) | |
182 "Get HEADER's history ring, or nil if none. | |
183 HEADER is a string without the colon." | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
184 (setq header (downcase header)) |
7267 | 185 (cdr (assoc header mail-hist-header-ring-alist))) |
186 | |
187 (defsubst mail-hist-add-header-contents-to-ring (header &optional contents) | |
188 "Add the contents of HEADER to the header history ring. | |
189 Optional argument CONTENTS is a string which will be the contents | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
190 (instead of whatever's found in the header)." |
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
191 (setq header (downcase header)) |
7267 | 192 (let ((ring (cdr (assoc header mail-hist-header-ring-alist)))) |
193 (or ring | |
194 ;; If the ring doesn't exist, we'll have to make it and add it | |
195 ;; to the mail-header-ring-alist: | |
196 (prog1 | |
197 (setq ring (make-ring mail-hist-history-size)) | |
198 (setq mail-hist-header-ring-alist | |
199 (cons (cons header ring) mail-hist-header-ring-alist)))) | |
200 (ring-insert | |
201 ring | |
202 (or contents (mail-hist-current-header-contents))))) | |
203 | |
204 ;;;###autoload | |
205 (defun mail-hist-put-headers-into-history () | |
206 "Put headers and contents of this message into mail header history. | |
207 Each header has its own independent history, as does the body of the | |
208 message. | |
209 | |
210 This function normally would be called when the message is sent." | |
211 (and | |
212 mail-hist-keep-history | |
213 (progn | |
214 (goto-char (point-min)) | |
215 (while (mail-hist-forward-header 1) | |
216 (mail-hist-add-header-contents-to-ring | |
217 (mail-hist-current-header-name))) | |
218 (let ((body-contents | |
219 (save-excursion | |
220 (goto-char (point-min)) | |
9425
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
221 (re-search-forward |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
222 (concat "^" (regexp-quote mail-header-separator)) |
7d0509b6f039
(mail-hist-put-headers-into-history)
Richard M. Stallman <rms@gnu.org>
parents:
8277
diff
changeset
|
223 nil) |
7267 | 224 (forward-line 1) |
225 (buffer-substring (point) (point-max))))) | |
226 (mail-hist-add-header-contents-to-ring "body" body-contents))))) | |
227 | |
228 (defun mail-hist-previous-input (header) | |
229 "Insert the previous contents of this mail header or message body. | |
230 Moves back through the history of sent mail messages. Each header has | |
231 its own independent history, as does the body of the message. | |
232 | |
233 The history only contains the contents of outgoing messages, not | |
234 received mail." | |
235 (interactive (list (or (mail-hist-current-header-name) "body"))) | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
236 (setq header (downcase header)) |
7267 | 237 (let* ((ring (cdr (assoc header mail-hist-header-ring-alist))) |
238 (len (ring-length ring)) | |
239 (repeat (eq last-command 'mail-hist-input-access))) | |
240 (if repeat | |
241 (setq mail-hist-access-count | |
242 (ring-plus1 mail-hist-access-count len)) | |
243 (setq mail-hist-access-count 0)) | |
244 (if (null ring) | |
245 (progn | |
246 (ding) | |
247 (message "No history for \"%s\"." header)) | |
248 (if (ring-empty-p ring) | |
249 (error "\"%s\" ring is empty." header) | |
250 (and repeat | |
251 (delete-region (car mail-hist-last-bounds) | |
252 (cdr mail-hist-last-bounds))) | |
253 (let ((start (point))) | |
254 (insert (ring-ref ring mail-hist-access-count)) | |
255 (setq mail-hist-last-bounds (cons start (point))) | |
256 (setq this-command 'mail-hist-input-access)))))) | |
257 | |
258 (defun mail-hist-next-input (header) | |
259 "Insert next contents of this mail header or message body. | |
260 Moves back through the history of sent mail messages. Each header has | |
261 its own independent history, as does the body of the message. | |
262 | |
263 Although you can do so, it does not make much sense to call this | |
264 without having called `mail-hist-previous-header' first | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
265 (\\[mail-hist-previous-header]). |
7267 | 266 |
267 The history only contains the contents of outgoing messages, not | |
268 received mail." | |
269 (interactive (list (or (mail-hist-current-header-name) "body"))) | |
8277
824fb42f33ab
Make sure that headers are case-insensitive.
Richard M. Stallman <rms@gnu.org>
parents:
7639
diff
changeset
|
270 (setq header (downcase header)) |
7267 | 271 (let* ((ring (cdr (assoc header mail-hist-header-ring-alist))) |
272 (len (ring-length ring)) | |
273 (repeat (eq last-command 'mail-hist-input-access))) | |
274 (if repeat | |
275 (setq mail-hist-access-count | |
276 (ring-minus1 mail-hist-access-count len)) | |
277 (setq mail-hist-access-count 0)) | |
278 (if (null ring) | |
279 (progn | |
280 (ding) | |
281 (message "No history for \"%s\"." header)) | |
282 (if (ring-empty-p ring) | |
283 (error "\"%s\" ring is empty." header) | |
284 (and repeat | |
285 (delete-region (car mail-hist-last-bounds) | |
286 (cdr mail-hist-last-bounds))) | |
287 (let ((start (point))) | |
288 (insert (ring-ref ring mail-hist-access-count)) | |
289 (setq mail-hist-last-bounds (cons start (point))) | |
290 (setq this-command 'mail-hist-input-access)))))) | |
291 | |
292 (provide 'mail-hist) | |
293 | |
294 ;; mail-hist.el ends here |