(calc-full-help): Use emacs-copyright.
[bpt/emacs.git] / lisp / erc / erc-ring.el
CommitLineData
597993cf
MB
1;; erc-ring.el -- Command history handling for erc using ring.el
2
ff59d266
MB
3;; Copyright (C) 2001, 2002, 2003, 2004, 2006,
4;; 2007 Free Software Foundation, Inc.
597993cf
MB
5
6;; Author: Alex Schroeder <alex@gnu.org>
7;; Keywords: comm
8;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcHistory
9
10;; This file is part of GNU Emacs.
11
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
e0085d62 14;; the Free Software Foundation; either version 3, or (at your option)
597993cf
MB
15;; any later version.
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
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.
26
27;;; Commentary:
28
29;; This file implements an input ring -- a history of the stuff you
30;; wrote. To activate:
31;;
32;; (require 'erc-auto) or (require 'erc-ring)
33;; (erc-ring-mode 1)
34;;
35;; Use M-n and M-p to navigate the ring
36
37;;; Code:
38
39(require 'erc)
40(require 'comint)
41(require 'ring)
42
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
46be 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)))
53
54(defvar erc-input-ring nil "Input ring for erc.")
55(make-variable-buffer-local 'erc-input-ring)
56
57(defvar erc-input-ring-index nil
58 "Position in the input ring for erc.
59If nil, the input line is blank and the user is conceptually 'after'
60the most recently added item in the ring. If an integer, the input
61line is non-blank and displays the item from the ring indexed by this
62variable.")
63(make-variable-buffer-local 'erc-input-ring-index)
64
65(defun erc-input-ring-setup ()
66 "Do the setup required so that we can use comint style input rings.
67Call 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))
70
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))
76
77(defun erc-clear-input-ring ()
78 "Remove all entries from the input ring, then call garbage-collect.
79You might use this for security purposes if you have typed a command
80containing 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."))
86
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))
98
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)))
108
109 (setq erc-input-ring-index (if erc-input-ring-index
110 (ring-plus1 erc-input-ring-index
111 (ring-length erc-input-ring))
112 0))
113 (erc-replace-current-command (ring-ref erc-input-ring
114 erc-input-ring-index)))))
115
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)))))
131
132
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)))
141
142(provide 'erc-ring)
143
144;;; erc-ring.el ends here
145;; Local Variables:
146;; indent-tabs-mode: nil
147;; End:
148
149;; arch-tag: b77924a8-a80e-489d-84cd-b351761ea5c8