Corrected a bad ChangeLog comment by adding full information from the package
[emacs.git] / lisp / erc / erc-ring.el
blobae526c193c3171246b79898c9617964edc5fa6be
1 ;; erc-ring.el -- Command history handling for erc using ring.el
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2006,
4 ;; 2007 Free Software Foundation, Inc.
6 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; Keywords: comm
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcHistory
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This file implements an input ring -- a history of the stuff you
30 ;; wrote. To activate:
32 ;; (require 'erc-auto) or (require 'erc-ring)
33 ;; (erc-ring-mode 1)
35 ;; Use M-n and M-p to navigate the ring
37 ;;; Code:
39 (require 'erc)
40 (require 'comint)
41 (require 'ring)
43 ;;;###autoload (autoload 'erc-ring-mode "erc-ring" nil t)
44 (define-erc-module ring nil
45 "Stores input in a ring so that previous commands and messages can
46 be recalled using M-p and M-n."
47 ((add-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
48 (define-key erc-mode-map "\M-p" 'erc-previous-command)
49 (define-key erc-mode-map "\M-n" 'erc-next-command))
50 ((remove-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
51 (define-key erc-mode-map "\M-p" 'undefined)
52 (define-key erc-mode-map "\M-n" 'undefined)))
54 (defvar erc-input-ring nil "Input ring for erc.")
55 (make-variable-buffer-local 'erc-input-ring)
57 (defvar erc-input-ring-index nil
58 "Position in the input ring for erc.
59 If nil, the input line is blank and the user is conceptually 'after'
60 the most recently added item in the ring. If an integer, the input
61 line is non-blank and displays the item from the ring indexed by this
62 variable.")
63 (make-variable-buffer-local 'erc-input-ring-index)
65 (defun erc-input-ring-setup ()
66 "Do the setup required so that we can use comint style input rings.
67 Call this function when setting up the mode."
68 (setq erc-input-ring (make-ring comint-input-ring-size))
69 (setq erc-input-ring-index nil))
71 (defun erc-add-to-input-ring (s)
72 "Add string S to the input ring and reset history position."
73 (unless erc-input-ring (erc-input-ring-setup))
74 (ring-insert erc-input-ring s)
75 (setq erc-input-ring-index nil))
77 (defun erc-clear-input-ring ()
78 "Remove all entries from the input ring, then call garbage-collect.
79 You might use this for security purposes if you have typed a command
80 containing a password."
81 (interactive)
82 (setq erc-input-ring (make-ring comint-input-ring-size)
83 erc-input-ring-index nil)
84 (garbage-collect)
85 (message "ERC input ring cleared."))
87 (defun erc-previous-command ()
88 "Replace current command with the previous one from the history."
89 (interactive)
90 (unless erc-input-ring (erc-input-ring-setup))
91 ;; if the ring isn't empty
92 (when (> (ring-length erc-input-ring) 0)
93 (if (and erc-input-ring-index
94 (= (ring-length erc-input-ring) (1+ erc-input-ring-index)))
95 (progn
96 (erc-replace-current-command "")
97 (setq erc-input-ring-index nil))
99 ;; If we are not viewing old input and there's text in the input
100 ;; area, push it on the history ring before moving back through
101 ;; the input history, so it will be there when we return to the
102 ;; front.
103 (if (null erc-input-ring-index)
104 (when (> (point-max) erc-input-marker)
105 (erc-add-to-input-ring (buffer-substring erc-input-marker
106 (point-max)))
107 (setq erc-input-ring-index 0)))
109 (setq erc-input-ring-index (if erc-input-ring-index
110 (ring-plus1 erc-input-ring-index
111 (ring-length erc-input-ring))
113 (erc-replace-current-command (ring-ref erc-input-ring
114 erc-input-ring-index)))))
116 (defun erc-next-command ()
117 "Replace current command with the next one from the history."
118 (interactive)
119 (unless erc-input-ring (erc-input-ring-setup))
120 ;; if the ring isn't empty
121 (when (> (ring-length erc-input-ring) 0)
122 (if (and erc-input-ring-index
123 (= 0 erc-input-ring-index))
124 (progn
125 (erc-replace-current-command "")
126 (setq erc-input-ring-index nil))
127 (setq erc-input-ring-index (ring-minus1 (or erc-input-ring-index 0)
128 (ring-length erc-input-ring)))
129 (erc-replace-current-command (ring-ref erc-input-ring
130 erc-input-ring-index)))))
133 (defun erc-replace-current-command (s)
134 "Replace current command with string S."
135 ;; delete line
136 (let ((inhibit-read-only t))
137 (delete-region
138 (progn (goto-char erc-insert-marker) (erc-bol))
139 (goto-char (point-max)))
140 (insert s)))
142 (provide 'erc-ring)
144 ;;; erc-ring.el ends here
145 ;; Local Variables:
146 ;; indent-tabs-mode: nil
147 ;; End:
149 ;; arch-tag: b77924a8-a80e-489d-84cd-b351761ea5c8