Add separator.pbm tool-bar image
[emacs.git] / lisp / isearchb.el
blob5e7771cea524ad7eb1684c5a054fd4330b6972f5
1 ;;; isearchb --- a marriage between iswitchb and isearch
3 ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 16 Apr 2004
8 ;; Version: 1.5
9 ;; Keywords: lisp
10 ;; X-URL: http://www.newartisans.com/johnw/emacs.html
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
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.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
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.
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:
39 ;; (isearchb-set-keybindings 'super) ; s-x s-y s-z now finds "xyz"
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:
45 ;; (define-key global-map [(control ?z)] 'isearchb-activate)
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.
53 ;; C-z after C-z toggles between the previously selected buffer and
54 ;; the current one.
56 ;; C-g aborts the search and returns you to your original buffer.
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.
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.
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.
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
78 ;;; Code:
80 (require 'iswitchb) ;FIXME: Don't rely on iswitchb!
82 (defgroup isearchb nil
83 "Switch between buffers using a mechanism like isearch."
84 :group 'iswitchb)
86 (defcustom isearchb-idle-timeout nil
87 "Number of idle seconds before isearchb turns itself off.
88 If nil, don't use a timeout."
89 :type '(choice (integer :tag "Seconds")
90 (const :tag "Disable" nil))
91 :group 'isearchb)
93 (defcustom isearchb-show-completions t
94 "If non-nil, show possible completions in the minibuffer."
95 :type 'boolean
96 :group 'isearchb)
98 (defvar isearchb-start-buffer nil)
99 (defvar isearchb-last-buffer nil)
100 (defvar isearchb-idle-timer nil)
102 (defun isearchb-stop (&optional return-to-buffer ignore-command)
103 "Called by isearchb to terminate a search in progress."
104 (remove-hook 'pre-command-hook 'isearchb-follow-char)
105 (if return-to-buffer
106 (switch-to-buffer isearchb-start-buffer)
107 (setq isearchb-last-buffer isearchb-start-buffer))
108 (when isearchb-idle-timer
109 (cancel-timer isearchb-idle-timer)
110 (setq isearchb-idle-timer nil))
111 (if ignore-command
112 (setq this-command 'ignore
113 last-command 'ignore))
114 (message nil))
116 (defun isearchb-iswitchb ()
117 "isearchb's custom version of the `iswitchb' command.
118 Its purpose is to pass different call arguments to
119 `iswitchb-read-buffer'."
120 (interactive)
121 (let* ((prompt "iswitch ")
122 (iswitchb-method 'samewindow)
123 (buf (iswitchb-read-buffer prompt nil nil nil iswitchb-text t)))
124 (if (eq iswitchb-exit 'findfile)
125 (call-interactively 'find-file)
126 (when buf
127 (if (get-buffer buf)
128 ;; buffer exists, so view it and then exit
129 (iswitchb-visit-buffer buf)
130 ;; else buffer doesn't exist
131 (iswitchb-possible-new-buffer buf))))))
133 (defun isearchb ()
134 "Switch to buffer matching a substring, based on chars typed."
135 (interactive)
136 (unless (eq last-command 'isearchb)
137 (setq iswitchb-text nil))
138 (unless iswitchb-text
139 (setq iswitchb-text "")
140 (iswitchb-make-buflist nil))
141 (if last-command-event
142 (setq iswitchb-rescan t
143 iswitchb-text (concat iswitchb-text
144 (char-to-string
145 (event-basic-type last-command-event)))))
146 (iswitchb-set-matches)
147 (let* ((match (car iswitchb-matches))
148 (buf (and match (get-buffer match))))
149 (if (null buf)
150 (progn
151 (isearchb-stop t)
152 (isearchb-iswitchb))
153 (switch-to-buffer buf)
154 (if isearchb-show-completions
155 (message "isearchb: %s%s" iswitchb-text
156 (iswitchb-completions iswitchb-text))
157 (if (= 1 (length iswitchb-matches))
158 (message "isearchb: %s (only match)" iswitchb-text)
159 (message "isearchb: %s" iswitchb-text))))))
161 (defun isearchb-set-keybindings (modifier)
162 "Setup isearchb on the given MODIFIER."
163 (dotimes (i 128)
164 (if (eq 'self-insert-command
165 (lookup-key global-map (vector i)))
166 (define-key global-map (vector (list modifier i)) 'isearchb))))
168 (defun isearchb-follow-char ()
169 "Function added to `post-command-hook' to handle the isearchb \"mode\"."
170 (let (keys)
171 (if (not (and (memq last-command '(isearchb isearchb-activate))
172 (setq keys (this-command-keys))
173 (= 1 (length keys))))
174 (isearchb-stop)
175 (cond
176 ((or (equal keys "\C-h") (equal keys "\C-?")
177 (equal keys [backspace]) (equal keys [delete]))
178 (setq iswitchb-text
179 (substring iswitchb-text 0 (1- (length iswitchb-text))))
180 (if (= 0 (length iswitchb-text))
181 (isearchb-stop t t)
182 (setq last-command-event nil)
183 (setq this-command 'isearchb)))
184 ((or (equal keys "\C-i") (equal keys [tab]))
185 (setq this-command 'isearchb-iswitchb))
186 ((equal keys "\C-s")
187 (iswitchb-next-match)
188 (setq last-command-event nil)
189 (setq this-command 'isearchb))
190 ((equal keys "\C-r")
191 (iswitchb-prev-match)
192 (setq last-command-event nil)
193 (setq this-command 'isearchb))
194 ((equal keys "\C-g")
195 (ding)
196 (isearchb-stop t t))
197 ((eq (lookup-key global-map keys) 'self-insert-command)
198 (setq this-command 'isearchb)))
199 (if (and isearchb-idle-timeout
200 (null isearchb-idle-timer))
201 (setq isearchb-idle-timer
202 (run-with-idle-timer isearchb-idle-timeout nil
203 'isearchb-stop))))))
205 ;;;###autoload
206 (defun isearchb-activate ()
207 "Active isearchb mode for subsequent alphanumeric keystrokes.
208 Executing this command again will terminate the search; or, if
209 the search has not yet begun, will toggle to the last buffer
210 accessed via isearchb."
211 (interactive)
212 (cond
213 ((eq last-command 'isearchb)
214 (isearchb-stop nil t))
215 ((eq last-command 'isearchb-activate)
216 (if isearchb-last-buffer
217 (switch-to-buffer isearchb-last-buffer)
218 (error "isearchb: There is no previous buffer to toggle to"))
219 (isearchb-stop nil t))
221 (message "isearchb: ")
222 (setq iswitchb-text nil
223 isearchb-start-buffer (current-buffer))
224 (add-hook 'pre-command-hook 'isearchb-follow-char))))
226 (provide 'isearchb)
228 ;;; isearchb.el ends here