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