mail-hist.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. ;;; mail-hist.el --- headers and message body history for outgoing mail
  2. ;; Copyright (C) 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Karl Fogel <kfogel@red-bean.com>
  4. ;; Created: March, 1994
  5. ;; Keywords: mail, history
  6. ;; Package: mail-utils
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Thanks to Jim Blandy for mentioning ring.el. It saved a lot of
  20. ;; time.
  21. ;;
  22. ;; To use this package, put it in a directory in your load-path, and
  23. ;; put this in your .emacs file:
  24. ;;
  25. ;; (load "mail-hist" nil t)
  26. ;;
  27. ;; Or you could do it with autoloads and hooks in your .emacs:
  28. ;;
  29. ;; (add-hook 'mail-mode-hook 'mail-hist-define-keys)
  30. ;; (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history)
  31. ;; (add-hook 'vm-mail-mode-hook 'mail-hist-define-keys) ;or rmail, etc
  32. ;; (autoload 'mail-hist-define-keys "mail-hist")
  33. ;; (autoload 'mail-hist-put-headers-into-history "mail-hist")
  34. ;;
  35. ;; Once it's installed, use M-p and M-n from mail headers to recover
  36. ;; previous/next contents in the history for that header, or, in the
  37. ;; body of the message, to recover previous/next text of the message.
  38. ;; This only applies to outgoing mail -- mail-hist ignores received
  39. ;; messages.
  40. ;;
  41. ;; Although repeated history requests do clear out the text from the
  42. ;; previous request, an isolated request just inserts its text at
  43. ;; point, so that you can mix the histories of different messages
  44. ;; easily. This might be confusing at times, but there should be no
  45. ;; problems that undo can't handle.
  46. ;;; Code:
  47. (require 'ring)
  48. (require 'sendmail)
  49. (defgroup mail-hist nil
  50. "Headers and message body history for outgoing mail."
  51. :prefix "mail-hist-"
  52. :group 'mail)
  53. ;;;###autoload
  54. (defun mail-hist-define-keys ()
  55. "Define keys for accessing mail header history. For use in hooks."
  56. (local-set-key "\M-p" 'mail-hist-previous-input)
  57. (local-set-key "\M-n" 'mail-hist-next-input))
  58. ;;;###autoload
  59. (defun mail-hist-enable ()
  60. (add-hook 'mail-mode-hook 'mail-hist-define-keys)
  61. (add-hook 'mail-send-hook 'mail-hist-put-headers-into-history))
  62. (defvar mail-hist-header-ring-alist nil
  63. "Alist of form (header-name . history-ring).
  64. Used for knowing which history list to look in when the user asks for
  65. previous/next input.")
  66. (defcustom mail-hist-history-size (or kill-ring-max 1729)
  67. "*The maximum number of elements in a mail field's history.
  68. Oldest elements are dumped first."
  69. :type 'integer
  70. :group 'mail-hist)
  71. ;;;###autoload
  72. (defcustom mail-hist-keep-history t
  73. "*Non-nil means keep a history for headers and text of outgoing mail."
  74. :type 'boolean
  75. :group 'mail-hist)
  76. ;; For handling repeated history requests
  77. (defvar mail-hist-access-count 0)
  78. (defvar mail-hist-last-bounds nil)
  79. ;; (start . end) A pair indicating the buffer positions delimiting the
  80. ;; last inserted history, so it can be replaced by a new input if the
  81. ;; command is repeated.
  82. (defvar mail-hist-header-regexp "^[^:]*:"
  83. "Regular expression for matching headers in a mail message.")
  84. (defsubst mail-hist-current-header-name ()
  85. "Get name of mail header point is currently in, without the colon.
  86. Returns nil if not in a header, implying that point is in the body of
  87. the message."
  88. (if (>= (point) (mail-text-start))
  89. nil ; then we are in the body of the message
  90. (save-excursion
  91. (let* ((body-start
  92. (mail-text-start))
  93. (name-start
  94. (re-search-backward mail-hist-header-regexp nil t))
  95. (name-end
  96. (prog2 (search-forward ":" body-start t) (1- (point)))))
  97. (and
  98. name-start
  99. name-end
  100. (downcase (buffer-substring-no-properties name-start name-end)))))))
  101. (defsubst mail-hist-forward-header (count)
  102. "Move forward COUNT headers (backward if COUNT is negative).
  103. If last/first header is encountered first, stop there and returns
  104. nil.
  105. Places point on the first non-whitespace on the line following the
  106. colon after the header name, or on the second space following that if
  107. the header is empty."
  108. (let ((boundary (mail-header-end)))
  109. (and
  110. (> boundary 0)
  111. (let ((unstopped t))
  112. (setq boundary (save-excursion
  113. (goto-char boundary)
  114. (beginning-of-line)
  115. (1- (point))))
  116. (if (> count 0)
  117. (while (> count 0)
  118. (setq
  119. unstopped
  120. (re-search-forward mail-hist-header-regexp boundary t))
  121. (setq count (1- count)))
  122. ;; because the current header will match too.
  123. (setq count (1- count))
  124. ;; count is negative
  125. (while (< count 0)
  126. (setq
  127. unstopped
  128. (re-search-backward mail-hist-header-regexp nil t))
  129. (setq count (1+ count)))
  130. ;; we end up behind the header, so must move to the front
  131. (re-search-forward mail-hist-header-regexp boundary t))
  132. ;; Now we are right after the colon
  133. (and (looking-at "\\s-") (forward-char 1))
  134. ;; return nil if didn't go as far as asked, otherwise point
  135. unstopped))))
  136. (defsubst mail-hist-beginning-of-header ()
  137. "Move to the start of the current header.
  138. The start of the current header is defined as one space after the
  139. colon, or just after the colon if it is not followed by whitespace."
  140. ;; this is slick as all heck:
  141. (if (mail-hist-forward-header -1)
  142. (mail-hist-forward-header 1)
  143. (mail-hist-forward-header 1)
  144. (mail-hist-forward-header -1)))
  145. (defsubst mail-hist-current-header-contents ()
  146. "Get the contents of the mail header in which point is located."
  147. (save-excursion
  148. (mail-hist-beginning-of-header)
  149. (let ((start (point)))
  150. (or (mail-hist-forward-header 1)
  151. (goto-char (mail-header-end)))
  152. (beginning-of-line)
  153. (buffer-substring start (1- (point))))))
  154. (defsubst mail-hist-get-header-ring (header)
  155. "Get HEADER's history ring, or nil if none.
  156. HEADER is a string without the colon."
  157. (setq header (downcase header))
  158. (cdr (assoc header mail-hist-header-ring-alist)))
  159. (defcustom mail-hist-text-size-limit nil
  160. "*Don't store any header or body with more than this many characters.
  161. If the value is nil, that means no limit on text size."
  162. :type '(choice (const nil) integer)
  163. :group 'mail-hist)
  164. (defun mail-hist-text-too-long-p (text)
  165. "Return non-nil if TEXT's length exceeds `mail-hist-text-size-limit'."
  166. (if mail-hist-text-size-limit
  167. (> (length text) mail-hist-text-size-limit)))
  168. (defsubst mail-hist-add-header-contents-to-ring (header &optional contents)
  169. "Add the contents of HEADER to the header history ring.
  170. Optional argument CONTENTS is a string which will be the contents
  171. \(instead of whatever's found in the header)."
  172. (setq header (downcase header))
  173. (let ((ctnts (or contents (mail-hist-current-header-contents)))
  174. (ring (cdr (assoc header mail-hist-header-ring-alist))))
  175. (if (mail-hist-text-too-long-p ctnts) (setq ctnts ""))
  176. (or ring
  177. ;; If the ring doesn't exist, we'll have to make it and add it
  178. ;; to the mail-header-ring-alist:
  179. (prog1
  180. (setq ring (make-ring mail-hist-history-size))
  181. (setq mail-hist-header-ring-alist
  182. (cons (cons header ring) mail-hist-header-ring-alist))))
  183. (ring-insert ring ctnts)))
  184. ;;;###autoload
  185. (defun mail-hist-put-headers-into-history ()
  186. "Put headers and contents of this message into mail header history.
  187. Each header has its own independent history, as does the body of the
  188. message.
  189. This function normally would be called when the message is sent."
  190. (and
  191. mail-hist-keep-history
  192. (save-excursion
  193. (goto-char (point-min))
  194. (while (mail-hist-forward-header 1)
  195. (mail-hist-add-header-contents-to-ring
  196. (mail-hist-current-header-name)))
  197. (let ((body-contents
  198. (buffer-substring (mail-text-start) (point-max))))
  199. (mail-hist-add-header-contents-to-ring "body" body-contents)))))
  200. (defun mail-hist-retrieve-and-insert (header access-func)
  201. "Helper for `mail-hist-previous-input' and `mail-hist-next-input'."
  202. (setq header (downcase header))
  203. (let* ((ring (cdr (assoc header mail-hist-header-ring-alist)))
  204. (len (ring-length ring))
  205. (repeat (eq last-command 'mail-hist-input-access)))
  206. (if repeat
  207. (setq mail-hist-access-count
  208. (funcall access-func mail-hist-access-count len))
  209. (setq mail-hist-access-count 0))
  210. (if (null ring)
  211. (progn
  212. (ding)
  213. (message "No history for \"%s\"." header))
  214. (if (ring-empty-p ring)
  215. (error "\"%s\" ring is empty" header)
  216. (and repeat
  217. (delete-region (car mail-hist-last-bounds)
  218. (cdr mail-hist-last-bounds)))
  219. (let ((start (point)))
  220. (insert (ring-ref ring mail-hist-access-count))
  221. (setq mail-hist-last-bounds (cons start (point)))
  222. (setq this-command 'mail-hist-input-access)
  223. ;; Special case: when flipping through message bodies, it's
  224. ;; usually most useful for point to stay at the top. This
  225. ;; is because the unique part of a message in a thread is
  226. ;; more likely to be at the top than the bottom, as the
  227. ;; bottom is often just the same quoted history for every
  228. ;; message in the thread, differing only in indentation
  229. ;; level.
  230. (if (string-equal header "body")
  231. (goto-char start)))
  232. ))))
  233. (defun mail-hist-previous-input (header)
  234. "Insert the previous contents of this mail header or message body.
  235. Moves back through the history of sent mail messages. Each header has
  236. its own independent history, as does the body of the message.
  237. The history only contains the contents of outgoing messages, not
  238. received mail."
  239. (interactive (list (or (mail-hist-current-header-name) "body")))
  240. (mail-hist-retrieve-and-insert header 'ring-plus1))
  241. (defun mail-hist-next-input (header)
  242. "Insert next contents of this mail header or message body.
  243. Moves back through the history of sent mail messages. Each header has
  244. its own independent history, as does the body of the message.
  245. Although you can do so, it does not make much sense to call this
  246. without having called `mail-hist-previous-header' first
  247. \(\\[mail-hist-previous-header]).
  248. The history only contains the contents of outgoing messages, not
  249. received mail."
  250. (interactive (list (or (mail-hist-current-header-name) "body")))
  251. (mail-hist-retrieve-and-insert header 'ring-minus1))
  252. (provide 'mail-hist)
  253. ;;; mail-hist.el ends here