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)
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.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to
30 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 ;; Thanks to Jim Blandy for mentioning ring.el. It saved a lot of
35 ;; To use this package, put it in a directory in your load-path, and
36 ;; put this in your .emacs file:
38 ;; (load "mail-hist" nil t)
40 ;; Or you could do it with autoloads and hooks in your .emacs:
42 ;; (add-hook 'mail-mode-hook 'mail-hist-define-keys)
43 ;; (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history)
44 ;; (add-hook 'vm-mail-mode-hook 'mail-hist-define-keys) ;or rmail, etc
45 ;; (autoload 'mail-hist-define-keys "mail-hist")
46 ;; (autoload 'mail-hist-put-headers-into-history "mail-hist")
48 ;; Once it's installed, use M-p and M-n from mail headers to recover
49 ;; previous/next contents in the history for that header, or, in the
50 ;; body of the message, to recover previous/next text of the message.
51 ;; This only applies to outgoing mail -- mail-hist ignores received
54 ;; Although repeated history requests do clear out the text from the
55 ;; previous request, an isolated request just inserts its text at
56 ;; point, so that you can mix the histories of different messages
57 ;; easily. This might be confusing at times, but there should be no
58 ;; problems that undo can't handle.
64 (defun mail-hist-define-keys ()
65 "Define keys for accessing mail header history. For use in hooks."
66 (local-set-key "\M-p" 'mail-hist-previous-input)
67 (local-set-key "\M-n" 'mail-hist-next-input))
70 (defun mail-hist-enable ()
71 (add-hook 'mail-mode-hook 'mail-hist-define-keys)
72 (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history))
74 (defvar mail-hist-header-ring-alist nil
75 "Alist of form (header-name . history-ring).
76 Used for knowing which history list to look in when the user asks for
77 previous/next input.")
79 (defvar mail-hist-history-size (or kill-ring-max 1729)
80 "*The maximum number of elements in a mail field's history.
81 Oldest elements are dumped first.")
84 (defvar mail-hist-keep-history t
85 "*Non-nil means keep a history for headers and text of outgoing mail.")
87 ;; For handling repeated history requests
88 (defvar mail-hist-access-count 0)
90 (defvar mail-hist-last-bounds nil)
91 ;; (start . end) A pair indicating the buffer positions delimiting the
92 ;; last inserted history, so it can be replaced by a new input if the
93 ;; command is repeated.
95 (defvar mail-hist-header-regexp "^[^:]*:"
96 "Regular expression for matching headers in a mail message.")
98 (defsubst mail-hist-current-header-name ()
99 "Get name of mail header point is currently in, without the colon.
100 Returns nil if not in a header, implying that point is in the body of
103 (re-search-backward (concat "^" (regexp-quote mail-header-separator)
106 nil ; then we are in the body of the message
108 (let* ((body-start ; limit possibility of false headers
111 (concat "^" (regexp-quote mail-header-separator) "$")
114 (re-search-backward mail-hist-header-regexp nil t))
116 (prog2 (search-forward ":" body-start t) (1- (point)))))
120 (downcase (buffer-substring-no-properties name-start name-end)))))))
122 (defsubst mail-hist-forward-header (count)
123 "Move forward COUNT headers (backward if COUNT is negative).
124 If last/first header is encountered first, stop there and returns
127 Places point on the first non-whitespace on the line following the
128 colon after the header name, or on the second space following that if
129 the header is empty."
130 (let ((boundary (save-excursion
132 (concat "^" (regexp-quote mail-header-separator) "$")
137 (setq boundary (save-excursion
145 (re-search-forward mail-hist-header-regexp boundary t))
146 (setq count (1- count)))
147 ;; because the current header will match too.
148 (setq count (1- count))
153 (re-search-backward mail-hist-header-regexp nil t))
154 (setq count (1+ count)))
155 ;; we end up behind the header, so must move to the front
156 (re-search-forward mail-hist-header-regexp boundary t))
157 ;; Now we are right after the colon
158 (and (looking-at "\\s-") (forward-char 1))
159 ;; return nil if didn't go as far as asked, otherwise point
162 (defsubst mail-hist-beginning-of-header ()
163 "Move to the start of the current header.
164 The start of the current header is defined as one space after the
165 colon, or just after the colon if it is not followed by whitespace."
166 ;; this is slick as all heck:
167 (if (mail-hist-forward-header -1)
168 (mail-hist-forward-header 1)
169 (mail-hist-forward-header 1)
170 (mail-hist-forward-header -1)))
172 (defsubst mail-hist-current-header-contents ()
173 "Get the contents of the mail header in which point is located."
175 (mail-hist-beginning-of-header)
176 (let ((start (point)))
177 (or (mail-hist-forward-header 1)
179 (concat "^" (regexp-quote mail-header-separator) "$")))
181 (buffer-substring start (1- (point))))))
183 (defsubst mail-hist-get-header-ring (header)
184 "Get HEADER's history ring, or nil if none.
185 HEADER is a string without the colon."
186 (setq header (downcase header))
187 (cdr (assoc header mail-hist-header-ring-alist)))
189 (defvar mail-hist-text-size-limit nil
190 "*Don't store any header or body with more than this many characters.
191 If the value is nil, that means no limit on text size.")
193 (defun mail-hist-text-too-long-p (text)
194 "Return t if TEXT does not exceed mail-hist's size limit.
195 The variable `mail-hist-text-size-limit' defines this limit."
196 (if mail-hist-text-size-limit
197 (> (length text) mail-hist-text-size-limit)))
199 (defsubst mail-hist-add-header-contents-to-ring (header &optional contents)
200 "Add the contents of HEADER to the header history ring.
201 Optional argument CONTENTS is a string which will be the contents
202 \(instead of whatever's found in the header)."
203 (setq header (downcase header))
204 (let ((ctnts (or contents (mail-hist-current-header-contents)))
205 (ring (cdr (assoc header mail-hist-header-ring-alist))))
206 (if (mail-hist-text-too-long-p ctnts) (setq ctnts ""))
208 ;; If the ring doesn't exist, we'll have to make it and add it
209 ;; to the mail-header-ring-alist:
211 (setq ring (make-ring mail-hist-history-size))
212 (setq mail-hist-header-ring-alist
213 (cons (cons header ring) mail-hist-header-ring-alist))))
214 (ring-insert ring ctnts)))
217 (defun mail-hist-put-headers-into-history ()
218 "Put headers and contents of this message into mail header history.
219 Each header has its own independent history, as does the body of the
222 This function normally would be called when the message is sent."
224 mail-hist-keep-history
226 (goto-char (point-min))
227 (while (mail-hist-forward-header 1)
228 (mail-hist-add-header-contents-to-ring
229 (mail-hist-current-header-name)))
232 (goto-char (point-min))
234 (concat "^" (regexp-quote mail-header-separator) "$")
237 (buffer-substring (point) (point-max)))))
238 (mail-hist-add-header-contents-to-ring "body" body-contents)))))
240 (defun mail-hist-previous-input (header)
241 "Insert the previous contents of this mail header or message body.
242 Moves back through the history of sent mail messages. Each header has
243 its own independent history, as does the body of the message.
245 The history only contains the contents of outgoing messages, not
247 (interactive (list (or (mail-hist-current-header-name) "body")))
248 (setq header (downcase header))
249 (let* ((ring (cdr (assoc header mail-hist-header-ring-alist)))
250 (len (ring-length ring))
251 (repeat (eq last-command 'mail-hist-input-access)))
253 (setq mail-hist-access-count
254 (ring-plus1 mail-hist-access-count len))
255 (setq mail-hist-access-count 0))
259 (message "No history for \"%s\"." header))
260 (if (ring-empty-p ring)
261 (error "\"%s\" ring is empty." header)
263 (delete-region (car mail-hist-last-bounds)
264 (cdr mail-hist-last-bounds)))
265 (let ((start (point)))
266 (insert (ring-ref ring mail-hist-access-count))
267 (setq mail-hist-last-bounds (cons start (point)))
268 (setq this-command 'mail-hist-input-access))))))
270 (defun mail-hist-next-input (header)
271 "Insert next contents of this mail header or message body.
272 Moves back through the history of sent mail messages. Each header has
273 its own independent history, as does the body of the message.
275 Although you can do so, it does not make much sense to call this
276 without having called `mail-hist-previous-header' first
277 (\\[mail-hist-previous-header]).
279 The history only contains the contents of outgoing messages, not
281 (interactive (list (or (mail-hist-current-header-name) "body")))
282 (setq header (downcase header))
283 (let* ((ring (cdr (assoc header mail-hist-header-ring-alist)))
284 (len (ring-length ring))
285 (repeat (eq last-command 'mail-hist-input-access)))
287 (setq mail-hist-access-count
288 (ring-minus1 mail-hist-access-count len))
289 (setq mail-hist-access-count 0))
293 (message "No history for \"%s\"." header))
294 (if (ring-empty-p ring)
295 (error "\"%s\" ring is empty." header)
297 (delete-region (car mail-hist-last-bounds)
298 (cdr mail-hist-last-bounds)))
299 (let ((start (point)))
300 (insert (ring-ref ring mail-hist-access-count))
301 (setq mail-hist-last-bounds (cons start (point)))
302 (setq this-command 'mail-hist-input-access))))))
306 ;; mail-hist.el ends here