Set maintainer of most lisp/erc/*.el files to FSF
[bpt/emacs.git] / lisp / erc / erc-ring.el
CommitLineData
597993cf
MB
1;; erc-ring.el -- Command history handling for erc using ring.el
2
acaf905b 3;; Copyright (C) 2001-2004, 2006-2012 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Alex Schroeder <alex@gnu.org>
df5d5f59 6;; Maintainer: FSF
597993cf
MB
7;; Keywords: comm
8;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcHistory
9
10;; This file is part of GNU Emacs.
11
4ee57b2a 12;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 13;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
597993cf
MB
16
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.
21
22;; You should have received a copy of the GNU General Public License
4ee57b2a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
24
25;;; Commentary:
26
27;; This file implements an input ring -- a history of the stuff you
28;; wrote. To activate:
29;;
30;; (require 'erc-auto) or (require 'erc-ring)
31;; (erc-ring-mode 1)
32;;
33;; Use M-n and M-p to navigate the ring
34
35;;; Code:
36
37(require 'erc)
38(require 'comint)
39(require 'ring)
40
41;;;###autoload (autoload 'erc-ring-mode "erc-ring" nil t)
42(define-erc-module ring nil
43 "Stores input in a ring so that previous commands and messages can
44be recalled using M-p and M-n."
45 ((add-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
46 (define-key erc-mode-map "\M-p" 'erc-previous-command)
47 (define-key erc-mode-map "\M-n" 'erc-next-command))
48 ((remove-hook 'erc-send-pre-hook 'erc-add-to-input-ring)
49 (define-key erc-mode-map "\M-p" 'undefined)
50 (define-key erc-mode-map "\M-n" 'undefined)))
51
52(defvar erc-input-ring nil "Input ring for erc.")
53(make-variable-buffer-local 'erc-input-ring)
54
55(defvar erc-input-ring-index nil
56 "Position in the input ring for erc.
57If nil, the input line is blank and the user is conceptually 'after'
58the most recently added item in the ring. If an integer, the input
59line is non-blank and displays the item from the ring indexed by this
60variable.")
61(make-variable-buffer-local 'erc-input-ring-index)
62
63(defun erc-input-ring-setup ()
64 "Do the setup required so that we can use comint style input rings.
65Call this function when setting up the mode."
66 (setq erc-input-ring (make-ring comint-input-ring-size))
67 (setq erc-input-ring-index nil))
68
69(defun erc-add-to-input-ring (s)
70 "Add string S to the input ring and reset history position."
71 (unless erc-input-ring (erc-input-ring-setup))
72 (ring-insert erc-input-ring s)
73 (setq erc-input-ring-index nil))
74
75(defun erc-clear-input-ring ()
76 "Remove all entries from the input ring, then call garbage-collect.
77You might use this for security purposes if you have typed a command
78containing a password."
79 (interactive)
80 (setq erc-input-ring (make-ring comint-input-ring-size)
81 erc-input-ring-index nil)
82 (garbage-collect)
83 (message "ERC input ring cleared."))
84
85(defun erc-previous-command ()
86 "Replace current command with the previous one from the history."
87 (interactive)
88 (unless erc-input-ring (erc-input-ring-setup))
89 ;; if the ring isn't empty
90 (when (> (ring-length erc-input-ring) 0)
91 (if (and erc-input-ring-index
92 (= (ring-length erc-input-ring) (1+ erc-input-ring-index)))
93 (progn
94 (erc-replace-current-command "")
95 (setq erc-input-ring-index nil))
96
97 ;; If we are not viewing old input and there's text in the input
98 ;; area, push it on the history ring before moving back through
99 ;; the input history, so it will be there when we return to the
100 ;; front.
101 (if (null erc-input-ring-index)
102 (when (> (point-max) erc-input-marker)
103 (erc-add-to-input-ring (buffer-substring erc-input-marker
104 (point-max)))
105 (setq erc-input-ring-index 0)))
106
107 (setq erc-input-ring-index (if erc-input-ring-index
108 (ring-plus1 erc-input-ring-index
109 (ring-length erc-input-ring))
110 0))
111 (erc-replace-current-command (ring-ref erc-input-ring
112 erc-input-ring-index)))))
113
114(defun erc-next-command ()
115 "Replace current command with the next one from the history."
116 (interactive)
117 (unless erc-input-ring (erc-input-ring-setup))
118 ;; if the ring isn't empty
119 (when (> (ring-length erc-input-ring) 0)
120 (if (and erc-input-ring-index
121 (= 0 erc-input-ring-index))
122 (progn
123 (erc-replace-current-command "")
124 (setq erc-input-ring-index nil))
125 (setq erc-input-ring-index (ring-minus1 (or erc-input-ring-index 0)
126 (ring-length erc-input-ring)))
127 (erc-replace-current-command (ring-ref erc-input-ring
128 erc-input-ring-index)))))
129
130
131(defun erc-replace-current-command (s)
132 "Replace current command with string S."
133 ;; delete line
134 (let ((inhibit-read-only t))
135 (delete-region
136 (progn (goto-char erc-insert-marker) (erc-bol))
137 (goto-char (point-max)))
138 (insert s)))
139
140(provide 'erc-ring)
141
142;;; erc-ring.el ends here
143;; Local Variables:
144;; indent-tabs-mode: nil
145;; End:
146