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