Commit | Line | Data |
---|---|---|
597993cf MB |
1 | ;; erc-ring.el -- Command history handling for erc using ring.el |
2 | ||
3 | ;; Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Alex Schroeder <alex@gnu.org> | |
6 | ;; Keywords: comm | |
7 | ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcHistory | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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) | |
14 | ;; any later version. | |
15 | ||
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. | |
20 | ||
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. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; This file implements an input ring -- a history of the stuff you | |
29 | ;; wrote. To activate: | |
30 | ;; | |
31 | ;; (require 'erc-auto) or (require 'erc-ring) | |
32 | ;; (erc-ring-mode 1) | |
33 | ;; | |
34 | ;; Use M-n and M-p to navigate the ring | |
35 | ||
36 | ;;; Code: | |
37 | ||
38 | (require 'erc) | |
39 | (require 'comint) | |
40 | (require 'ring) | |
41 | ||
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))) | |
52 | ||
53 | (defvar erc-input-ring nil "Input ring for erc.") | |
54 | (make-variable-buffer-local 'erc-input-ring) | |
55 | ||
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 | |
61 | variable.") | |
62 | (make-variable-buffer-local 'erc-input-ring-index) | |
63 | ||
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)) | |
69 | ||
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)) | |
75 | ||
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." | |
80 | (interactive) | |
81 | (setq erc-input-ring (make-ring comint-input-ring-size) | |
82 | erc-input-ring-index nil) | |
83 | (garbage-collect) | |
84 | (message "ERC input ring cleared.")) | |
85 | ||
86 | (defun erc-previous-command () | |
87 | "Replace current command with the previous one from the history." | |
88 | (interactive) | |
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))) | |
94 | (progn | |
95 | (erc-replace-current-command "") | |
96 | (setq erc-input-ring-index nil)) | |
97 | ||
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 | |
101 | ;; front. | |
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 | |
105 | (point-max))) | |
106 | (setq erc-input-ring-index 0))) | |
107 | ||
108 | (setq erc-input-ring-index (if erc-input-ring-index | |
109 | (ring-plus1 erc-input-ring-index | |
110 | (ring-length erc-input-ring)) | |
111 | 0)) | |
112 | (erc-replace-current-command (ring-ref erc-input-ring | |
113 | erc-input-ring-index))))) | |
114 | ||
115 | (defun erc-next-command () | |
116 | "Replace current command with the next one from the history." | |
117 | (interactive) | |
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)) | |
123 | (progn | |
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))))) | |
130 | ||
131 | ||
132 | (defun erc-replace-current-command (s) | |
133 | "Replace current command with string S." | |
134 | ;; delete line | |
135 | (let ((inhibit-read-only t)) | |
136 | (delete-region | |
137 | (progn (goto-char erc-insert-marker) (erc-bol)) | |
138 | (goto-char (point-max))) | |
139 | (insert s))) | |
140 | ||
141 | (provide 'erc-ring) | |
142 | ||
143 | ;;; erc-ring.el ends here | |
144 | ;; Local Variables: | |
145 | ;; indent-tabs-mode: nil | |
146 | ;; End: | |
147 | ||
148 | ;; arch-tag: b77924a8-a80e-489d-84cd-b351761ea5c8 |