Customized.
[emacs.git] / lisp / mail / mail-hist.el
blob86dcae229ee8154978f9d0098ddd3891a06a7106
1 ;;; mail-hist.el --- Headers and message body history for outgoing mail.
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
5 ;; Author: Karl Fogel <kfogel@cs.oberlin.edu>
6 ;; Created: March, 1994
7 ;; Keywords: mail, history
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; Thanks to Jim Blandy for mentioning ring.el. It saved a lot of
29 ;; time.
31 ;; To use this package, put it in a directory in your load-path, and
32 ;; put this in your .emacs file:
34 ;; (load "mail-hist" nil t)
36 ;; Or you could do it with autoloads and hooks in your .emacs:
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")
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.
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.
56 ;;; Code:
57 (require 'ring)
59 (defgroup mail-hist nil
60 "Headers and message body history for outgoing mail."
61 :prefix "mail-hist-"
62 :group 'mail)
64 ;;;###autoload
65 (defun mail-hist-define-keys ()
66 "Define keys for accessing mail header history. For use in hooks."
67 (local-set-key "\M-p" 'mail-hist-previous-input)
68 (local-set-key "\M-n" 'mail-hist-next-input))
70 ;;;###autoload
71 (defun mail-hist-enable ()
72 (add-hook 'mail-mode-hook 'mail-hist-define-keys)
73 (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history))
75 (defvar mail-hist-header-ring-alist nil
76 "Alist of form (header-name . history-ring).
77 Used for knowing which history list to look in when the user asks for
78 previous/next input.")
80 (defcustom mail-hist-history-size (or kill-ring-max 1729)
81 "*The maximum number of elements in a mail field's history.
82 Oldest elements are dumped first."
83 :type 'integer
84 :group 'mail-hist)
86 ;;;###autoload
87 (defcustom mail-hist-keep-history t
88 "*Non-nil means keep a history for headers and text of outgoing mail."
89 :type 'boolean
90 :group 'mail-hist)
92 ;; For handling repeated history requests
93 (defvar mail-hist-access-count 0)
95 (defvar mail-hist-last-bounds nil)
96 ;; (start . end) A pair indicating the buffer positions delimiting the
97 ;; last inserted history, so it can be replaced by a new input if the
98 ;; command is repeated.
100 (defvar mail-hist-header-regexp "^[^:]*:"
101 "Regular expression for matching headers in a mail message.")
103 (defsubst mail-hist-current-header-name ()
104 "Get name of mail header point is currently in, without the colon.
105 Returns nil if not in a header, implying that point is in the body of
106 the message."
107 (if (save-excursion
108 (re-search-backward (concat "^" (regexp-quote mail-header-separator)
109 "$")
110 nil t))
111 nil ; then we are in the body of the message
112 (save-excursion
113 (let* ((body-start ; limit possibility of false headers
114 (save-excursion
115 (re-search-forward
116 (concat "^" (regexp-quote mail-header-separator) "$")
117 nil t)))
118 (name-start
119 (re-search-backward mail-hist-header-regexp nil t))
120 (name-end
121 (prog2 (search-forward ":" body-start t) (1- (point)))))
122 (and
123 name-start
124 name-end
125 (downcase (buffer-substring-no-properties name-start name-end)))))))
127 (defsubst mail-hist-forward-header (count)
128 "Move forward COUNT headers (backward if COUNT is negative).
129 If last/first header is encountered first, stop there and returns
130 nil.
132 Places point on the first non-whitespace on the line following the
133 colon after the header name, or on the second space following that if
134 the header is empty."
135 (let ((boundary (save-excursion
136 (re-search-forward
137 (concat "^" (regexp-quote mail-header-separator) "$")
138 nil t))))
139 (and
140 boundary
141 (let ((unstopped t))
142 (setq boundary (save-excursion
143 (goto-char boundary)
144 (beginning-of-line)
145 (1- (point))))
146 (if (> count 0)
147 (while (> count 0)
148 (setq
149 unstopped
150 (re-search-forward mail-hist-header-regexp boundary t))
151 (setq count (1- count)))
152 ;; because the current header will match too.
153 (setq count (1- count))
154 ;; count is negative
155 (while (< count 0)
156 (setq
157 unstopped
158 (re-search-backward mail-hist-header-regexp nil t))
159 (setq count (1+ count)))
160 ;; we end up behind the header, so must move to the front
161 (re-search-forward mail-hist-header-regexp boundary t))
162 ;; Now we are right after the colon
163 (and (looking-at "\\s-") (forward-char 1))
164 ;; return nil if didn't go as far as asked, otherwise point
165 unstopped))))
167 (defsubst mail-hist-beginning-of-header ()
168 "Move to the start of the current header.
169 The start of the current header is defined as one space after the
170 colon, or just after the colon if it is not followed by whitespace."
171 ;; this is slick as all heck:
172 (if (mail-hist-forward-header -1)
173 (mail-hist-forward-header 1)
174 (mail-hist-forward-header 1)
175 (mail-hist-forward-header -1)))
177 (defsubst mail-hist-current-header-contents ()
178 "Get the contents of the mail header in which point is located."
179 (save-excursion
180 (mail-hist-beginning-of-header)
181 (let ((start (point)))
182 (or (mail-hist-forward-header 1)
183 (re-search-forward
184 (concat "^" (regexp-quote mail-header-separator) "$")))
185 (beginning-of-line)
186 (buffer-substring start (1- (point))))))
188 (defsubst mail-hist-get-header-ring (header)
189 "Get HEADER's history ring, or nil if none.
190 HEADER is a string without the colon."
191 (setq header (downcase header))
192 (cdr (assoc header mail-hist-header-ring-alist)))
194 (defcustom mail-hist-text-size-limit nil
195 "*Don't store any header or body with more than this many characters.
196 If the value is nil, that means no limit on text size."
197 :type '(choice (const nil) integer)
198 :group 'mail-hist)
200 (defun mail-hist-text-too-long-p (text)
201 "Return t if TEXT does not exceed mail-hist's size limit.
202 The variable `mail-hist-text-size-limit' defines this limit."
203 (if mail-hist-text-size-limit
204 (> (length text) mail-hist-text-size-limit)))
206 (defsubst mail-hist-add-header-contents-to-ring (header &optional contents)
207 "Add the contents of HEADER to the header history ring.
208 Optional argument CONTENTS is a string which will be the contents
209 \(instead of whatever's found in the header)."
210 (setq header (downcase header))
211 (let ((ctnts (or contents (mail-hist-current-header-contents)))
212 (ring (cdr (assoc header mail-hist-header-ring-alist))))
213 (if (mail-hist-text-too-long-p ctnts) (setq ctnts ""))
214 (or ring
215 ;; If the ring doesn't exist, we'll have to make it and add it
216 ;; to the mail-header-ring-alist:
217 (prog1
218 (setq ring (make-ring mail-hist-history-size))
219 (setq mail-hist-header-ring-alist
220 (cons (cons header ring) mail-hist-header-ring-alist))))
221 (ring-insert ring ctnts)))
223 ;;;###autoload
224 (defun mail-hist-put-headers-into-history ()
225 "Put headers and contents of this message into mail header history.
226 Each header has its own independent history, as does the body of the
227 message.
229 This function normally would be called when the message is sent."
230 (and
231 mail-hist-keep-history
232 (save-excursion
233 (goto-char (point-min))
234 (while (mail-hist-forward-header 1)
235 (mail-hist-add-header-contents-to-ring
236 (mail-hist-current-header-name)))
237 (let ((body-contents
238 (save-excursion
239 (goto-char (point-min))
240 (re-search-forward
241 (concat "^" (regexp-quote mail-header-separator) "$")
242 nil)
243 (forward-line 1)
244 (buffer-substring (point) (point-max)))))
245 (mail-hist-add-header-contents-to-ring "body" body-contents)))))
247 (defun mail-hist-previous-input (header)
248 "Insert the previous contents of this mail header or message body.
249 Moves back through the history of sent mail messages. Each header has
250 its own independent history, as does the body of the message.
252 The history only contains the contents of outgoing messages, not
253 received mail."
254 (interactive (list (or (mail-hist-current-header-name) "body")))
255 (setq header (downcase header))
256 (let* ((ring (cdr (assoc header mail-hist-header-ring-alist)))
257 (len (ring-length ring))
258 (repeat (eq last-command 'mail-hist-input-access)))
259 (if repeat
260 (setq mail-hist-access-count
261 (ring-plus1 mail-hist-access-count len))
262 (setq mail-hist-access-count 0))
263 (if (null ring)
264 (progn
265 (ding)
266 (message "No history for \"%s\"." header))
267 (if (ring-empty-p ring)
268 (error "\"%s\" ring is empty." header)
269 (and repeat
270 (delete-region (car mail-hist-last-bounds)
271 (cdr mail-hist-last-bounds)))
272 (let ((start (point)))
273 (insert (ring-ref ring mail-hist-access-count))
274 (setq mail-hist-last-bounds (cons start (point)))
275 (setq this-command 'mail-hist-input-access))))))
277 (defun mail-hist-next-input (header)
278 "Insert next contents of this mail header or message body.
279 Moves back through the history of sent mail messages. Each header has
280 its own independent history, as does the body of the message.
282 Although you can do so, it does not make much sense to call this
283 without having called `mail-hist-previous-header' first
284 (\\[mail-hist-previous-header]).
286 The history only contains the contents of outgoing messages, not
287 received mail."
288 (interactive (list (or (mail-hist-current-header-name) "body")))
289 (setq header (downcase header))
290 (let* ((ring (cdr (assoc header mail-hist-header-ring-alist)))
291 (len (ring-length ring))
292 (repeat (eq last-command 'mail-hist-input-access)))
293 (if repeat
294 (setq mail-hist-access-count
295 (ring-minus1 mail-hist-access-count len))
296 (setq mail-hist-access-count 0))
297 (if (null ring)
298 (progn
299 (ding)
300 (message "No history for \"%s\"." header))
301 (if (ring-empty-p ring)
302 (error "\"%s\" ring is empty." header)
303 (and repeat
304 (delete-region (car mail-hist-last-bounds)
305 (cdr mail-hist-last-bounds)))
306 (let ((start (point)))
307 (insert (ring-ref ring mail-hist-access-count))
308 (setq mail-hist-last-bounds (cons start (point)))
309 (setq this-command 'mail-hist-input-access))))))
311 (provide 'mail-hist)
313 ;; mail-hist.el ends here