Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / isearchb.el
bloba5e3743c5550af820bcd3a88330c234a2e6e027a
1 ;;; isearchb --- a marriage between iswitchb and isearch
3 ;; Copyright (C) 2004-2014 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
143 (event-basic-type last-command-event)))))
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
154 (iswitchb-completions iswitchb-text))
155 (if (= 1 (length iswitchb-matches))
156 (message "isearchb: %s (only match)" iswitchb-text)
157 (message "isearchb: %s" iswitchb-text))))))
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))))
166 (defun isearchb-follow-char ()
167 "Function added to `post-command-hook' to handle the isearchb \"mode\"."
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)
180 (setq last-command-event nil)
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)
186 (setq last-command-event nil)
187 (setq this-command 'isearchb))
188 ((equal keys "\C-r")
189 (iswitchb-prev-match)
190 (setq last-command-event nil)
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))))))
203 ;;;###autoload
204 (defun isearchb-activate ()
205 "Active isearchb mode for subsequent alphanumeric keystrokes.
206 Executing this command again will terminate the search; or, if
207 the search has not yet begun, will toggle to the last buffer
208 accessed 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)
216 (error "isearchb: There is no previous buffer to toggle to"))
217 (isearchb-stop nil t))
219 (message "isearchb: ")
220 (setq iswitchb-text nil
221 isearchb-start-buffer (current-buffer))
222 (add-hook 'pre-command-hook 'isearchb-follow-char))))
224 (provide 'isearchb)
226 ;;; isearchb.el ends here