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