avoid recursive `require' when loading semantic
[bpt/emacs.git] / lisp / isearchb.el
CommitLineData
5d28fa01
JW
1;;; isearchb --- a marriage between iswitchb and isearch
2
ba318903 3;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5d28fa01
JW
4
5;; Author: John Wiegley <johnw@gnu.org>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
5d28fa01
JW
7;; Created: 16 Apr 2004
8;; Version: 1.5
9;; Keywords: lisp
10;; X-URL: http://www.newartisans.com/johnw/emacs.html
11
12;; This file is part of GNU Emacs.
13
eb3fa2cf 14;; GNU Emacs is free software: you can redistribute it and/or modify
5d28fa01 15;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
5d28fa01
JW
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
eb3fa2cf 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5d28fa01
JW
26
27;;; Commentary:
28
29;; This module allows you to switch to buffers even faster than with
30;; iswitchb! It is not intended to replace it, however, as it works
31;; well only with buffers whose names don't typically overlap. You'll
32;; have to try it first, and see how your mileage varies.
33;;
34;; The first way to use isearchb is by holding down a modifier key, in
35;; which case every letter you type while holding it searches for any
36;; buffer matching what you're typing (using the same ordering scheme
37;; employed by iswitchb). To use it this way, add to your .emacs:
38;;
39;; (isearchb-set-keybindings 'super) ; s-x s-y s-z now finds "xyz"
40;;
41;; The other way is by using a command that puts you into "search"
42;; mode, just like with isearch. I use C-z for this. The binding in
43;; my .emacs looks like:
44;;
45;; (define-key global-map [(control ?z)] 'isearchb-activate)
46;;
47;; Now, after pressing C-z (for example), each self-inserting
48;; character thereafter will search for a buffer containing those
49;; characters. For instance, typing "C-z xyz" will switch to the
50;; first buffer containing "xyz". Once you press a non-self-inserting
51;; character (such as any control key sequence), the search will end.
52;;
53;; C-z after C-z toggles between the previously selected buffer and
54;; the current one.
55;;
56;; C-g aborts the search and returns you to your original buffer.
57;;
58;; TAB, after typing in a few characters (after C-z), will jump into
59;; iswitchb, using the prefix you've typed so far. This is handy when
60;; you realize that isearchb is not powerful enough to find the buffer
61;; you're looking for.
62;;
63;; C-s and C-r move forward and backward in the buffer list. If
64;; `isearchb-show-completions' is non-nil (the default), the list of
65;; possible completions is shown in the minibuffer.
66;;
67;; If `isearchb-idle-timeout' is set to a number, isearchb will quit
68;; after that many seconds of idle time. I recommend trying it set to
69;; one or two seconds. Then, if you switch to a buffer and wait for
70;; that amount of time, you can start typing without manually exiting
71;; isearchb.
72
73;; TODO:
74;; C-z C-z is broken
75;; killing iswitchb.el and then trying to switch back is broken
76;; make sure TAB isn't broken
77
78(require 'iswitchb)
79
80(defgroup isearchb nil
81 "Switch between buffers using a mechanism like isearch."
82 :group 'iswitchb)
83
84(defcustom isearchb-idle-timeout nil
9201cc28 85 "Number of idle seconds before isearchb turns itself off.
5d28fa01
JW
86If nil, don't use a timeout."
87 :type '(choice (integer :tag "Seconds")
88 (const :tag "Disable" nil))
89 :group 'isearchb)
90
91(defcustom isearchb-show-completions t
9201cc28 92 "If non-nil, show possible completions in the minibuffer."
5d28fa01
JW
93 :type 'boolean
94 :group 'isearchb)
95
96(defvar isearchb-start-buffer nil)
97(defvar isearchb-last-buffer nil)
98(defvar isearchb-idle-timer nil)
99
100(defun isearchb-stop (&optional return-to-buffer ignore-command)
101 "Called by isearchb to terminate a search in progress."
102 (remove-hook 'pre-command-hook 'isearchb-follow-char)
103 (if return-to-buffer
104 (switch-to-buffer isearchb-start-buffer)
105 (setq isearchb-last-buffer isearchb-start-buffer))
106 (when isearchb-idle-timer
107 (cancel-timer isearchb-idle-timer)
108 (setq isearchb-idle-timer nil))
109 (if ignore-command
110 (setq this-command 'ignore
111 last-command 'ignore))
112 (message nil))
113
114(defun isearchb-iswitchb ()
115 "isearchb's custom version of the `iswitchb' command.
228b1afd 116Its purpose is to pass different call arguments to
5d28fa01
JW
117`iswitchb-read-buffer'."
118 (interactive)
119 (let* ((prompt "iswitch ")
120 (iswitchb-method 'samewindow)
121 (buf (iswitchb-read-buffer prompt nil nil iswitchb-text t)))
122 (if (eq iswitchb-exit 'findfile)
123 (call-interactively 'find-file)
124 (when buf
125 (if (get-buffer buf)
126 ;; buffer exists, so view it and then exit
127 (iswitchb-visit-buffer buf)
128 ;; else buffer doesn't exist
129 (iswitchb-possible-new-buffer buf))))))
130
131(defun isearchb ()
132 "Switch to buffer matching a substring, based on chars typed."
133 (interactive)
134 (unless (eq last-command 'isearchb)
135 (setq iswitchb-text nil))
136 (unless iswitchb-text
137 (setq iswitchb-text "")
138 (iswitchb-make-buflist nil))
8989a920 139 (if last-command-event
5d28fa01
JW
140 (setq iswitchb-rescan t
141 iswitchb-text (concat iswitchb-text
50ab1da6
JL
142 (char-to-string
143 (event-basic-type last-command-event)))))
5d28fa01
JW
144 (iswitchb-set-matches)
145 (let* ((match (car iswitchb-matches))
146 (buf (and match (get-buffer match))))
147 (if (null buf)
148 (progn
149 (isearchb-stop t)
150 (isearchb-iswitchb))
151 (switch-to-buffer buf)
152 (if isearchb-show-completions
153 (message "isearchb: %s%s" iswitchb-text
f7f17975 154 (iswitchb-completions iswitchb-text))
5d28fa01
JW
155 (if (= 1 (length iswitchb-matches))
156 (message "isearchb: %s (only match)" iswitchb-text)
157 (message "isearchb: %s" iswitchb-text))))))
158
159(defun isearchb-set-keybindings (modifier)
160 "Setup isearchb on the given MODIFIER."
161 (dotimes (i 128)
162 (if (eq 'self-insert-command
163 (lookup-key global-map (vector i)))
164 (define-key global-map (vector (list modifier i)) 'isearchb))))
165
166(defun isearchb-follow-char ()
99181af3 167 "Function added to `post-command-hook' to handle the isearchb \"mode\"."
5d28fa01
JW
168 (let (keys)
169 (if (not (and (memq last-command '(isearchb isearchb-activate))
170 (setq keys (this-command-keys))
171 (= 1 (length keys))))
172 (isearchb-stop)
173 (cond
174 ((or (equal keys "\C-h") (equal keys "\C-?")
175 (equal keys [backspace]) (equal keys [delete]))
176 (setq iswitchb-text
177 (substring iswitchb-text 0 (1- (length iswitchb-text))))
178 (if (= 0 (length iswitchb-text))
179 (isearchb-stop t t)
8989a920 180 (setq last-command-event nil)
5d28fa01
JW
181 (setq this-command 'isearchb)))
182 ((or (equal keys "\C-i") (equal keys [tab]))
183 (setq this-command 'isearchb-iswitchb))
184 ((equal keys "\C-s")
185 (iswitchb-next-match)
8989a920 186 (setq last-command-event nil)
5d28fa01
JW
187 (setq this-command 'isearchb))
188 ((equal keys "\C-r")
189 (iswitchb-prev-match)
8989a920 190 (setq last-command-event nil)
5d28fa01
JW
191 (setq this-command 'isearchb))
192 ((equal keys "\C-g")
193 (ding)
194 (isearchb-stop t t))
195 ((eq (lookup-key global-map keys) 'self-insert-command)
196 (setq this-command 'isearchb)))
197 (if (and isearchb-idle-timeout
198 (null isearchb-idle-timer))
199 (setq isearchb-idle-timer
200 (run-with-idle-timer isearchb-idle-timeout nil
201 'isearchb-stop))))))
202
203;;;###autoload
204(defun isearchb-activate ()
205 "Active isearchb mode for subsequent alphanumeric keystrokes.
206Executing this command again will terminate the search; or, if
207the search has not yet begun, will toggle to the last buffer
208accessed via isearchb."
209 (interactive)
210 (cond
211 ((eq last-command 'isearchb)
212 (isearchb-stop nil t))
213 ((eq last-command 'isearchb-activate)
214 (if isearchb-last-buffer
215 (switch-to-buffer isearchb-last-buffer)
90d7908e 216 (error "isearchb: There is no previous buffer to toggle to"))
5d28fa01
JW
217 (isearchb-stop nil t))
218 (t
219 (message "isearchb: ")
220 (setq iswitchb-text nil
221 isearchb-start-buffer (current-buffer))
222 (add-hook 'pre-command-hook 'isearchb-follow-char))))
223
224(provide 'isearchb)
225
226;;; isearchb.el ends here