1 ;; erc-ring.el -- Command history handling for erc using ring.el
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcHistory
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This file implements an input ring -- a history of the stuff you
29 ;; wrote. To activate:
31 ;; (require 'erc-auto) or (require 'erc-ring)
34 ;; Use M-n and M-p to navigate the ring
42 ;;;###autoload (autoload 'erc-ring-mode "erc-ring" nil t)
43 (define-erc-module ring nil
44 "Stores input in a ring so that previous commands and messages can
45 be recalled using M-p and M-n."
46 ((add-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
47 (define-key erc-mode-map "\M-p" 'erc-previous-command)
48 (define-key erc-mode-map "\M-n" 'erc-next-command))
49 ((remove-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
50 (define-key erc-mode-map "\M-p" 'undefined)
51 (define-key erc-mode-map "\M-n" 'undefined)))
53 (defvar erc-input-ring nil "Input ring for erc.")
54 (make-variable-buffer-local 'erc-input-ring)
56 (defvar erc-input-ring-index nil
57 "Position in the input ring for erc.
58 If nil, the input line is blank and the user is conceptually 'after'
59 the most recently added item in the ring. If an integer, the input
60 line is non-blank and displays the item from the ring indexed by this
62 (make-variable-buffer-local 'erc-input-ring-index)
64 (defun erc-input-ring-setup ()
65 "Do the setup required so that we can use comint style input rings.
66 Call this function when setting up the mode."
67 (setq erc-input-ring (make-ring comint-input-ring-size))
68 (setq erc-input-ring-index nil))
70 (defun erc-add-to-input-ring (s)
71 "Add string S to the input ring and reset history position."
72 (unless erc-input-ring (erc-input-ring-setup))
73 (ring-insert erc-input-ring s)
74 (setq erc-input-ring-index nil))
76 (defun erc-clear-input-ring ()
77 "Remove all entries from the input ring, then call garbage-collect.
78 You might use this for security purposes if you have typed a command
79 containing a password."
81 (setq erc-input-ring (make-ring comint-input-ring-size)
82 erc-input-ring-index nil)
84 (message "ERC input ring cleared."))
86 (defun erc-previous-command ()
87 "Replace current command with the previous one from the history."
89 (unless erc-input-ring (erc-input-ring-setup))
90 ;; if the ring isn't empty
91 (when (> (ring-length erc-input-ring) 0)
92 (if (and erc-input-ring-index
93 (= (ring-length erc-input-ring) (1+ erc-input-ring-index)))
95 (erc-replace-current-command "")
96 (setq erc-input-ring-index nil))
98 ;; If we are not viewing old input and there's text in the input
99 ;; area, push it on the history ring before moving back through
100 ;; the input history, so it will be there when we return to the
102 (if (null erc-input-ring-index)
103 (when (> (point-max) erc-input-marker)
104 (erc-add-to-input-ring (buffer-substring erc-input-marker
106 (setq erc-input-ring-index 0)))
108 (setq erc-input-ring-index (if erc-input-ring-index
109 (ring-plus1 erc-input-ring-index
110 (ring-length erc-input-ring))
112 (erc-replace-current-command (ring-ref erc-input-ring
113 erc-input-ring-index)))))
115 (defun erc-next-command ()
116 "Replace current command with the next one from the history."
118 (unless erc-input-ring (erc-input-ring-setup))
119 ;; if the ring isn't empty
120 (when (> (ring-length erc-input-ring) 0)
121 (if (and erc-input-ring-index
122 (= 0 erc-input-ring-index))
124 (erc-replace-current-command "")
125 (setq erc-input-ring-index nil))
126 (setq erc-input-ring-index (ring-minus1 (or erc-input-ring-index 0)
127 (ring-length erc-input-ring)))
128 (erc-replace-current-command (ring-ref erc-input-ring
129 erc-input-ring-index)))))
132 (defun erc-replace-current-command (s)
133 "Replace current command with string S."
135 (let ((inhibit-read-only t))
137 (progn (goto-char erc-insert-marker) (erc-bol))
138 (goto-char (point-max)))
143 ;;; erc-ring.el ends here
145 ;; indent-tabs-mode: nil
148 ;; arch-tag: b77924a8-a80e-489d-84cd-b351761ea5c8