* lisp/files.el (locate-dominating-file): Doc fix.
[emacs.git] / lisp / isearchb.el
blob030e4925619497c952ff015ed072d5d06135c0d9
1 ;;; isearchb --- a marriage between iswitchb and isearch
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: FSF
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 (require 'iswitchb)
80 (defgroup isearchb nil
81 "Switch between buffers using a mechanism like isearch."
82 :group 'iswitchb)
84 (defcustom isearchb-idle-timeout nil
85 "Number of idle seconds before isearchb turns itself off.
86 If nil, don't use a timeout."
87 :type '(choice (integer :tag "Seconds")
88 (const :tag "Disable" nil))
89 :group 'isearchb)
91 (defcustom isearchb-show-completions t
92 "If non-nil, show possible completions in the minibuffer."
93 :type 'boolean
94 :group 'isearchb)
96 (defvar isearchb-start-buffer nil)
97 (defvar isearchb-last-buffer nil)
98 (defvar isearchb-idle-timer nil)
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))
114 (defun isearchb-iswitchb ()
115 "isearchb's custom version of the `iswitchb' command.
116 Its purpose is to pass different call arguments to
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))))))
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))
139 (if last-command-event
140 (setq iswitchb-rescan t
141 iswitchb-text (concat iswitchb-text
142 (char-to-string last-command-event))))
143 (iswitchb-set-matches)
144 (let* ((match (car iswitchb-matches))
145 (buf (and match (get-buffer match))))
146 (if (null buf)
147 (progn
148 (isearchb-stop t)
149 (isearchb-iswitchb))
150 (switch-to-buffer buf)
151 (if isearchb-show-completions
152 (message "isearchb: %s%s" iswitchb-text
153 (iswitchb-completions iswitchb-text))
154 (if (= 1 (length iswitchb-matches))
155 (message "isearchb: %s (only match)" iswitchb-text)
156 (message "isearchb: %s" iswitchb-text))))))
158 (defun isearchb-set-keybindings (modifier)
159 "Setup isearchb on the given MODIFIER."
160 (dotimes (i 128)
161 (if (eq 'self-insert-command
162 (lookup-key global-map (vector i)))
163 (define-key global-map (vector (list modifier i)) 'isearchb))))
165 (defun isearchb-follow-char ()
166 "Function added to `post-command-hook' to handle the isearchb \"mode\"."
167 (let (keys)
168 (if (not (and (memq last-command '(isearchb isearchb-activate))
169 (setq keys (this-command-keys))
170 (= 1 (length keys))))
171 (isearchb-stop)
172 (cond
173 ((or (equal keys "\C-h") (equal keys "\C-?")
174 (equal keys [backspace]) (equal keys [delete]))
175 (setq iswitchb-text
176 (substring iswitchb-text 0 (1- (length iswitchb-text))))
177 (if (= 0 (length iswitchb-text))
178 (isearchb-stop t t)
179 (setq last-command-event nil)
180 (setq this-command 'isearchb)))
181 ((or (equal keys "\C-i") (equal keys [tab]))
182 (setq this-command 'isearchb-iswitchb))
183 ((equal keys "\C-s")
184 (iswitchb-next-match)
185 (setq last-command-event nil)
186 (setq this-command 'isearchb))
187 ((equal keys "\C-r")
188 (iswitchb-prev-match)
189 (setq last-command-event nil)
190 (setq this-command 'isearchb))
191 ((equal keys "\C-g")
192 (ding)
193 (isearchb-stop t t))
194 ((eq (lookup-key global-map keys) 'self-insert-command)
195 (setq this-command 'isearchb)))
196 (if (and isearchb-idle-timeout
197 (null isearchb-idle-timer))
198 (setq isearchb-idle-timer
199 (run-with-idle-timer isearchb-idle-timeout nil
200 'isearchb-stop))))))
202 ;;;###autoload
203 (defun isearchb-activate ()
204 "Active isearchb mode for subsequent alphanumeric keystrokes.
205 Executing this command again will terminate the search; or, if
206 the search has not yet begun, will toggle to the last buffer
207 accessed via isearchb."
208 (interactive)
209 (cond
210 ((eq last-command 'isearchb)
211 (isearchb-stop nil t))
212 ((eq last-command 'isearchb-activate)
213 (if isearchb-last-buffer
214 (switch-to-buffer isearchb-last-buffer)
215 (error "isearchb: There is no previous buffer to toggle to"))
216 (isearchb-stop nil t))
218 (message "isearchb: ")
219 (setq iswitchb-text nil
220 isearchb-start-buffer (current-buffer))
221 (add-hook 'pre-command-hook 'isearchb-follow-char))))
223 (provide 'isearchb)
225 ;;; isearchb.el ends here