]> code.delx.au - gnu-emacs/blob - lisp/iswitchb.el
(mouse-throw-magnifier-with-scroll-bar)
[gnu-emacs] / lisp / iswitchb.el
1 ;;; iswitchb.el --- switch between buffers using substrings
2
3 ;; Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Stephen Eglen <stephen@gnu.org>
7 ;; Maintainer: Stephen Eglen <stephen@gnu.org>
8 ;; Keywords: completion convenience
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; Installation:
30 ;; To get the functions in this package bound to keys, use
31 ;; M-x iswitchb-mode or customize the option `iswitchb-mode'.
32 ;; Alternatively, add the following line to your .emacs:
33 ;; (iswitchb-mode 1)
34
35 ;; As you type in a substring, the list of buffers currently matching
36 ;; the substring is displayed as you type. The list is ordered so
37 ;; that the most recent buffers visited come at the start of the list.
38 ;; The buffer at the start of the list will be the one visited when
39 ;; you press return. By typing more of the substring, the list is
40 ;; narrowed down so that gradually the buffer you want will be at the
41 ;; top of the list. Alternatively, you can use C-s and C-r to rotate
42 ;; buffer names in the list until the one you want is at the top of
43 ;; the list. Completion is also available so that you can see what is
44 ;; common to all of the matching buffers as you type.
45
46 ;; This code is similar to a couple of other packages. Michael R Cook
47 ;; <cook@sightpath.com> wrote a similar buffer switching package, but
48 ;; does exact matching rather than substring matching on buffer names.
49 ;; I also modified a couple of functions from icomplete.el to provide
50 ;; the completion feedback in the minibuffer.
51
52 ;;; Example
53
54 ;; If I have two buffers called "123456" and "123", with "123456" the
55 ;; most recent, when I use iswitchb, I first of all get presented with
56 ;; the list of all the buffers
57 ;;
58 ;; iswitch {123456,123}
59 ;;
60 ;; If I then press 2:
61 ;; iswitch 2[3]{123456,123}
62 ;;
63 ;; The list in {} are the matching buffers, most recent first (buffers
64 ;; visible in the current frame are put at the end of the list by
65 ;; default). At any time I can select the item at the head of the
66 ;; list by pressing RET. I can also put the first element at the end
67 ;; of the list by pressing C-s, or put the last element at the head of
68 ;; the list by pressing C-r. The item in [] indicates what can be
69 ;; added to my input by pressing TAB. In this case, I will get "3"
70 ;; added to my input. So, press TAB:
71 ;; iswitch 23{123456,123}
72 ;;
73 ;; At this point, I still have two matching buffers.
74 ;; If I want the first buffer in the list, I simply press RET. If I
75 ;; wanted the second in the list, I could press C-s to move it to the
76 ;; top of the list and then RET to select it.
77 ;;
78 ;; However, if I type 4, I only have one match left:
79 ;; iswitch 234[123456] [Matched]
80 ;;
81 ;; Since there is only one matching buffer left, it is given in [] and we
82 ;; see the text [Matched] afterwards. I can now press TAB or RET to go
83 ;; to that buffer.
84 ;;
85 ;; If however, I now type "a":
86 ;; iswitch 234a [No match]
87 ;; There are no matching buffers. If I press RET or TAB, I can be
88 ;; prompted to create a new buffer called "234a".
89 ;;
90 ;; Of course, where this function comes in really useful is when you
91 ;; can specify the buffer using only a few keystrokes. In the above
92 ;; example, the quickest way to get to the "123456" buffer would be
93 ;; just to type 4 and then RET (assuming there isn't any newer buffer
94 ;; with 4 in its name).
95
96 ;; To see a full list of all matching buffers in a separate buffer,
97 ;; hit ? or press TAB when there are no further completions to the
98 ;; substring. Repeated TAB presses will scroll you through this
99 ;; separate buffer.
100
101 ;; The buffer at the head of the list can be killed by pressing C-k.
102 ;; If the buffer needs saving, you will be queried before the buffer
103 ;; is killed.
104
105 ;; If you find that the file you are after is not in a buffer, you can
106 ;; press C-x C-f to immediately drop into find-file.
107
108 ;; See the doc string of iswitchb for full keybindings and features.
109 ;; (describe-function 'iswitchb)
110
111 ;; Case matching: The case of strings when matching can be ignored or
112 ;; used depending on the value of iswitchb-case (default is the same
113 ;; as case-fold-search, normally t). Imagine you have the following
114 ;; buffers:
115 ;;
116 ;; INBOX *info* *scratch*
117 ;;
118 ;; Then these will be the matching buffers, depending on how you type
119 ;; the two letters `in' and the value of iswitchb-case:
120 ;;
121 ;; iswitchb-case user input | matching buffers
122 ;; ----------------------------------------------
123 ;; nil in | *info*
124 ;; t in | INBOX, *info*
125 ;; t IN | INBOX
126 ;; t In | [No match]
127
128 ;;; Customisation
129
130 ;; See the User Variables section below for easy ways to change the
131 ;; functionality of the program. These are accessible using the
132 ;; custom package.
133 ;; To modify the keybindings, use something like:
134 ;;
135 ;;(add-hook 'iswitchb-mode-hook 'iswitchb-my-keys)
136 ;;(defun iswitchb-my-keys ()
137 ;; "Add my keybindings for iswitchb."
138 ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match))
139 ;;
140 ;; Seeing all the matching buffers
141 ;;
142 ;; If you have many matching buffers, they may not all fit onto one
143 ;; line of the minibuffer. In Emacs 21, the variable
144 ;; `resize-mini-windows' controls how many lines of the minibuffer can
145 ;; be seen. For older versions of emacs, you can use
146 ;; `resize-minibuffer-mode'. You can also limit iswitchb so that it
147 ;; only shows a certain number of lines -- see the documentation for
148 ;; `iswitchb-minibuffer-setup-hook'.
149
150 ;; Changing the list of buffers
151
152 ;; By default, the list of current buffers is most recent first,
153 ;; oldest last, with the exception that the buffers visible in the
154 ;; current frame are put at the end of the list. A hook exists to
155 ;; allow other functions to order the list. For example, if you add:
156 ;;
157 ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
158 ;;
159 ;; then all buffers matching "Summary" are moved to the end of the
160 ;; list. (I find this handy for keeping the INBOX Summary and so on
161 ;; out of the way.) It also moves buffers matching "output\*$" to the
162 ;; end of the list (these are created by AUCTeX when compiling.)
163 ;; Other functions could be made available which alter the list of
164 ;; matching buffers (either deleting or rearranging elements.)
165
166 ;; Font-Lock
167
168 ;; font-lock is used to highlight the first matching buffer. To
169 ;; switch this off, set (setq iswitchb-use-faces nil). Colouring of
170 ;; the matching buffer name was suggested by Carsten Dominik
171 ;; (dominik@strw.leidenuniv.nl)
172
173 ;; Replacement for read-buffer
174
175 ;; iswitchb-read-buffer has been written to be a drop in replacement
176 ;; for the normal buffer selection routine `read-buffer'. To use
177 ;; iswitch for all buffer selections in Emacs, add:
178 ;; (setq read-buffer-function 'iswitchb-read-buffer)
179 ;; (This variable was introduced in Emacs 20.3.)
180 ;; XEmacs users can get the same behaviour by doing:
181 ;; (defalias 'read-buffer 'iswitchb-read-buffer)
182 ;; since `read-buffer' is defined in lisp.
183
184 ;; Using iswitchb for other completion tasks.
185
186 ;; Kin Cho (kin@neoscale.com) sent the following suggestion to use
187 ;; iswitchb for other completion tasks.
188 ;;
189 ;; (defun my-icompleting-read (prompt choices)
190 ;; "Use iswitch as a completing-read replacement to choose from
191 ;; choices. PROMPT is a string to prompt with. CHOICES is a list of
192 ;; strings to choose from."
193 ;; (let ((iswitchb-make-buflist-hook
194 ;; (lambda ()
195 ;; (setq iswitchb-temp-buflist choices))))
196 ;; (iswitchb-read-buffer prompt)))
197 ;;
198 ;; example:
199 ;; (my-icompleting-read "Which fruit? " '
200 ;; ("apple" "pineapple" "pear" "bananas" "oranges") )
201
202 ;; Kin Cho also suggested the following defun. Once you have a subset of
203 ;; matching buffers matching your current prompt, you can then press
204 ;; e.g. C-o to restrict matching to those buffers and clearing the prompt:
205 ;; (defun iswitchb-exclude-nonmatching()
206 ;; "Make iswitchb work on only the currently matching names."
207 ;; (interactive)
208 ;; (setq iswitchb-buflist iswitchb-matches)
209 ;; (setq iswitchb-rescan t)
210 ;; (delete-minibuffer-contents))
211 ;;
212 ;; (add-hook 'iswitchb-define-mode-map-hook
213 ;; '(lambda () (define-key
214 ;; iswitchb-mode-map "\C-o"
215 ;; 'iswitchb-exclude-nonmatching)))
216
217 ;; Other lisp packages extend iswitchb behaviour to other tasks. See
218 ;; ido.el (by Kim Storm) and mcomplete.el (Yuji Minejima).
219
220 ;; Window managers: Switching frames/focus follows mouse; Sawfish.
221
222 ;; If you switch to a buffer that is visible in another frame,
223 ;; iswitchb can switch focus to that frame. If your window manager
224 ;; uses "click to focus" policy for window selection, you should also
225 ;; set focus-follows-mouse to nil.
226
227 ;; iswitch functionality has also been implemented for switching
228 ;; between windows in the Sawfish window manager.
229
230 ;; Regexp matching
231
232 ;; There is provision for regexp matching within iswitchb, enabled
233 ;; through `iswitchb-regexp'. This allows you to type `c$' for
234 ;; example and see all buffer names ending in `c'. No completion
235 ;; mechanism is currently offered when regexp searching.
236
237 ;;; TODO
238
239 ;;; Acknowledgements
240
241 ;; Thanks to Jari Aalto <jari.aalto@poboxes.com> for help with the
242 ;; first version of this package, iswitch-buffer. Thanks also to many
243 ;; others for testing earlier versions.
244
245 ;;; Code:
246
247 (require 'font-lock)
248
249 ;;; User Variables
250 ;;
251 ;; These are some things you might want to change.
252
253 (defgroup iswitchb nil
254 "Switch between buffers using substrings."
255 :group 'convenience
256 :group 'completion
257 :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el")
258 :link '(url-link "http://www.anc.ed.ac.uk/~stephen/emacs/")
259 :link '(emacs-library-link :tag "Lisp File" "iswitchb.el"))
260
261 (defcustom iswitchb-case case-fold-search
262 "*Non-nil if searching of buffer names should ignore case.
263 If this is non-nil but the user input has any upper case letters, matching
264 is temporarily case sensitive."
265 :type 'boolean
266 :group 'iswitchb)
267
268 (defcustom iswitchb-buffer-ignore
269 '("^ ")
270 "*List of regexps or functions matching buffer names to ignore.
271 For example, traditional behavior is not to list buffers whose names begin
272 with a space, for which the regexp is `^ '. See the source file for
273 example functions that filter buffer names."
274 :type '(repeat (choice regexp function))
275 :group 'iswitchb)
276 (put 'iswitchb-buffer-ignore 'risky-local-variable t)
277
278 (defcustom iswitchb-max-to-show nil
279 "*If non-nil, limit the number of names shown in the minibuffer.
280 If this value is N, and N is greater than the number of matching
281 buffers, the first N/2 and the last N/2 matching buffers are
282 shown. This can greatly speed up iswitchb if you have a
283 multitude of buffers open."
284 :type '(choice (const :tag "Show all" nil) integer)
285 :group 'iswitchb)
286
287 (defcustom iswitchb-use-virtual-buffers nil
288 "*If non-nil, refer to past buffers when none match.
289 This feature relies upon the `recentf' package, which will be
290 enabled if this variable is configured to a non-nil value."
291 :type 'boolean
292 :require 'recentf
293 :set (function
294 (lambda (sym value)
295 (if value (recentf-mode 1))
296 (set sym value)))
297 :group 'iswitchb)
298
299 (defvar iswitchb-virtual-buffers nil)
300
301 (defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help
302 "*Hook run when `iswitchb-complete' can't complete any more.
303 The most useful values are `iswitchb-completion-help', which pops up a
304 window with completion alternatives, or `iswitchb-next-match' or
305 `iswitchb-prev-match', which cycle the buffer list."
306 :type 'hook
307 :group 'iswitchb)
308
309 ;; Examples for setting the value of iswitchb-buffer-ignore
310 ;;(defun iswitchb-ignore-c-mode (name)
311 ;; "Ignore all c mode buffers -- example function for iswitchb."
312 ;; (with-current-buffer name
313 ;; (derived-mode-p 'c-mode)))
314
315 ;;(setq iswitchb-buffer-ignore '("^ " iswitchb-ignore-c-mode))
316 ;;(setq iswitchb-buffer-ignore '("^ " "\\.c\\'" "\\.h\\'"))
317
318 (defcustom iswitchb-default-method 'always-frame
319 "*How to switch to new buffer when using `iswitchb-buffer'.
320 Possible values:
321 `samewindow' Show new buffer in same window
322 `otherwindow' Show new buffer in another window (same frame)
323 `display' Display buffer in another window without switching to it
324 `otherframe' Show new buffer in another frame
325 `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
326 you want to see the buffer in the same window of the current
327 frame or in the other frame.
328 `always-frame' If a buffer is visible in another frame, raise that
329 frame. Otherwise, visit the buffer in the same window."
330 :type '(choice (const samewindow)
331 (const otherwindow)
332 (const display)
333 (const otherframe)
334 (const maybe-frame)
335 (const always-frame))
336 :group 'iswitchb)
337
338 (defcustom iswitchb-regexp nil
339 "*Non-nil means that `iswitchb' will do regexp matching.
340 Value can be toggled within `iswitchb' using `iswitchb-toggle-regexp'."
341 :type 'boolean
342 :group 'iswitchb)
343
344 (defcustom iswitchb-newbuffer t
345 "*Non-nil means create new buffer if no buffer matches substring.
346 See also `iswitchb-prompt-newbuffer'."
347 :type 'boolean
348 :group 'iswitchb)
349
350 (defcustom iswitchb-prompt-newbuffer t
351 "*Non-nil means prompt user to confirm before creating new buffer.
352 See also `iswitchb-newbuffer'."
353 :type 'boolean
354 :group 'iswitchb)
355
356 (define-obsolete-variable-alias 'iswitchb-use-fonts 'iswitchb-use-faces "22.1")
357
358 (defcustom iswitchb-use-faces t
359 "*Non-nil means use font-lock faces for showing first match."
360 :type 'boolean
361 :group 'iswitchb)
362
363 (defcustom iswitchb-use-frame-buffer-list nil
364 "*Non-nil means use the currently selected frame's buffer list."
365 :type 'boolean
366 :group 'iswitchb)
367
368 (defcustom iswitchb-make-buflist-hook nil
369 "Hook to run when list of matching buffers is created."
370 :type 'hook
371 :group 'iswitchb)
372
373 (defvar iswitchb-all-frames 'visible
374 "*Argument to pass to `walk-windows' when finding visible buffers.
375 See documentation of `walk-windows' for useful values.")
376
377 (defcustom iswitchb-minibuffer-setup-hook nil
378 "Iswitchb-specific customization of minibuffer setup.
379
380 This hook is run during minibuffer setup if `iswitchb' is active.
381 For instance:
382 \(add-hook 'iswitchb-minibuffer-setup-hook
383 '\(lambda () (set (make-local-variable 'max-mini-window-height) 3)))
384 will constrain the minibuffer to a maximum height of 3 lines when
385 iswitchb is running."
386 :type 'hook
387 :group 'iswitchb)
388
389 (defface iswitchb-single-match
390 '((t
391 (:inherit font-lock-comment-face)))
392 "Iswitchb face for single matching buffer name."
393 :version "22.1"
394 :group 'iswitchb)
395
396 (defface iswitchb-current-match
397 '((t
398 (:inherit font-lock-function-name-face)))
399 "Iswitchb face for current matching buffer name."
400 :version "22.1"
401 :group 'iswitchb)
402
403 (defface iswitchb-virtual-matches
404 '((t
405 (:inherit font-lock-builtin-face)))
406 "Iswitchb face for matching virtual buffer names.
407 See also `iswitchb-use-virtual-buffers'."
408 :version "22.1"
409 :group 'iswitchb)
410
411 (defface iswitchb-invalid-regexp
412 '((t
413 (:inherit font-lock-warning-face)))
414 "Iswitchb face for indicating invalid regexp. "
415 :version "22.1"
416 :group 'iswitchb)
417
418 ;; Do we need the variable iswitchb-use-mycompletion?
419
420 ;;; Internal Variables
421
422 (defvar iswitchb-method nil
423 "Stores the method for viewing the selected buffer.
424 Its value is one of `samewindow', `otherwindow', `display', `otherframe',
425 `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
426 details of values.")
427
428 (defvar iswitchb-eoinput 1
429 "Point where minibuffer input ends and completion info begins.
430 Copied from `icomplete-eoinput'.")
431 (make-variable-buffer-local 'iswitchb-eoinput)
432
433 (defvar iswitchb-buflist nil
434 "Stores the current list of buffers that will be searched through.
435 The list is ordered, so that the most recent buffers come first,
436 although by default, the buffers visible in the current frame are put
437 at the end of the list. Created by `iswitchb-make-buflist'.")
438
439 ;; todo -- is this necessary?
440
441 (defvar iswitchb-use-mycompletion nil
442 "Non-nil means use `iswitchb-buffer' completion feedback.
443 Should only be set to t by iswitchb functions, so that it doesn't
444 interfere with other minibuffer usage.")
445
446 (defvar iswitchb-change-word-sub nil
447 "Private variable used by `iswitchb-word-matching-substring'.")
448
449 (defvar iswitchb-common-match-string nil
450 "Stores the string that is common to all matching buffers.")
451
452 (defvar iswitchb-rescan nil
453 "Non-nil means we need to regenerate the list of matching buffers.")
454
455 (defvar iswitchb-text nil
456 "Stores the users string as it is typed in.")
457
458 (defvar iswitchb-matches nil
459 "List of buffers currently matching `iswitchb-text'.")
460
461 (defvar iswitchb-mode-map
462 (let ((map (make-sparse-keymap)))
463 (set-keymap-parent map minibuffer-local-map)
464 (define-key map "?" 'iswitchb-completion-help)
465 (define-key map "\C-s" 'iswitchb-next-match)
466 (define-key map "\C-r" 'iswitchb-prev-match)
467 (define-key map "\t" 'iswitchb-complete)
468 (define-key map "\C-j" 'iswitchb-select-buffer-text)
469 (define-key map "\C-t" 'iswitchb-toggle-regexp)
470 (define-key map "\C-x\C-f" 'iswitchb-find-file)
471 (define-key map "\C-c" 'iswitchb-toggle-case)
472 (define-key map "\C-k" 'iswitchb-kill-buffer)
473 (define-key map "\C-m" 'iswitchb-exit-minibuffer)
474 map)
475 "Minibuffer keymap for `iswitchb-buffer'.")
476
477 (defvar iswitchb-global-map
478 (let ((map (make-sparse-keymap)))
479 (dolist (b '((switch-to-buffer . iswitchb-buffer)
480 (switch-to-buffer-other-window . iswitchb-buffer-other-window)
481 (switch-to-buffer-other-frame . iswitchb-buffer-other-frame)
482 (display-buffer . iswitchb-display-buffer)))
483 (if (fboundp 'command-remapping)
484 (define-key map (vector 'remap (car b)) (cdr b))
485 (substitute-key-definition (car b) (cdr b) map global-map)))
486 map)
487 "Global keymap for `iswitchb-mode'.")
488
489 (defvar iswitchb-history nil
490 "History of buffers selected using `iswitchb-buffer'.")
491
492 (defvar iswitchb-exit nil
493 "Flag to monitor how `iswitchb-buffer' exits.
494 If equal to `takeprompt', we use the prompt as the buffer name to be
495 selected.")
496
497 (defvar iswitchb-buffer-ignore-orig nil
498 "Stores original value of `iswitchb-buffer-ignore'.")
499
500 (defvar iswitchb-default nil
501 "Default buffer for iswitchb.")
502
503 ;; The following variables are needed to keep the byte compiler quiet.
504 (defvar iswitchb-require-match nil
505 "Non-nil if matching buffer must be selected.")
506
507 (defvar iswitchb-temp-buflist nil
508 "Stores a temporary version of the buffer list being created.")
509
510 (defvar iswitchb-bufs-in-frame nil
511 "List of the buffers visible in the current frame.")
512
513 (defvar iswitchb-minibuf-depth nil
514 "Value we expect to be returned by `minibuffer-depth' in the minibuffer.")
515
516 (defvar iswitchb-common-match-inserted nil
517 "Non-nil if we have just inserted a common match in the minibuffer.")
518
519 (defvar iswitchb-invalid-regexp)
520
521 ;;; FUNCTIONS
522
523 ;;; ISWITCHB KEYMAP
524 (defun iswitchb-define-mode-map ()
525 "Set up the keymap for `iswitchb-buffer'."
526 (interactive)
527 (let (map)
528 ;; generated every time so that it can inherit new functions.
529 ;;(or iswitchb-mode-map
530
531 (setq map (copy-keymap minibuffer-local-map))
532 (define-key map "?" 'iswitchb-completion-help)
533 (define-key map "\C-s" 'iswitchb-next-match)
534 (define-key map "\C-r" 'iswitchb-prev-match)
535 (define-key map "\t" 'iswitchb-complete)
536 (define-key map "\C-j" 'iswitchb-select-buffer-text)
537 (define-key map "\C-t" 'iswitchb-toggle-regexp)
538 (define-key map "\C-x\C-f" 'iswitchb-find-file)
539 (define-key map "\C-n" 'iswitchb-toggle-ignore)
540 (define-key map "\C-c" 'iswitchb-toggle-case)
541 (define-key map "\C-k" 'iswitchb-kill-buffer)
542 (define-key map "\C-m" 'iswitchb-exit-minibuffer)
543 (setq iswitchb-mode-map map)
544 (run-hooks 'iswitchb-define-mode-map-hook)))
545
546 (make-obsolete 'iswitchb-define-mode-map
547 "use M-x iswitchb-mode or customize the variable `iswitchb-mode'."
548 "21.1")
549
550 ;;; MAIN FUNCTION
551 (defun iswitchb ()
552 "Switch to buffer matching a substring.
553 As you type in a string, all of the buffers matching the string are
554 displayed. When you have found the buffer you want, it can then be
555 selected. As you type, most keys have their normal keybindings,
556 except for the following:
557 \\<iswitchb-mode-map>
558
559 RET Select the buffer at the front of the list of matches. If the
560 list is empty, possibly prompt to create new buffer.
561
562 \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
563 If no buffer is found, prompt for a new one.
564
565 \\[iswitchb-next-match] Put the first element at the end of the list.
566 \\[iswitchb-prev-match] Put the last element at the start of the list.
567 \\[iswitchb-complete] Complete a common suffix to the current string that
568 matches all buffers. If there is only one match, select that buffer.
569 If there is no common suffix, show a list of all matching buffers
570 in a separate window.
571 \\[iswitchb-toggle-regexp] Toggle regexp searching.
572 \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
573 \\[iswitchb-completion-help] Show list of matching buffers in separate window.
574 \\[iswitchb-find-file] Exit iswitchb and drop into `find-file'.
575 \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
576 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
577 ;;`iswitchb-buffer-ignore')
578
579 (let* ((prompt "iswitch ")
580 iswitchb-invalid-regexp
581 (buf (iswitchb-read-buffer prompt)))
582
583 ;;(message "chosen text %s" iswitchb-final-text)
584 ;; Choose the buffer name: either the text typed in, or the head
585 ;; of the list of matches
586
587 (cond ( (eq iswitchb-exit 'findfile)
588 (call-interactively 'find-file))
589 (iswitchb-invalid-regexp
590 (message "Won't make invalid regexp named buffer"))
591 (t
592 ;; View the buffer
593 ;;(message "go to buf %s" buf)
594 ;; Check buf is non-nil.
595 (if buf
596 (if (get-buffer buf)
597 ;; buffer exists, so view it and then exit
598 (iswitchb-visit-buffer buf)
599 ;; else buffer doesn't exist
600 (iswitchb-possible-new-buffer buf)))
601 ))))
602
603 (defun iswitchb-read-buffer (prompt &optional default require-match
604 start matches-set)
605 "Replacement for the built-in `read-buffer'.
606 Return the name of a buffer selected.
607 PROMPT is the prompt to give to the user.
608 DEFAULT if given is the default buffer to be selected, which will
609 go to the front of the list.
610 If REQUIRE-MATCH is non-nil, an existing buffer must be selected.
611 If START is a string, the selection process is started with that
612 string.
613 If MATCHES-SET is non-nil, the buflist is not updated before
614 the selection process begins. Used by isearchb.el."
615 (let
616 (
617 buf-sel
618 iswitchb-final-text
619 (icomplete-mode nil) ;; prevent icomplete starting up
620 )
621
622 (iswitchb-define-mode-map)
623 (setq iswitchb-exit nil)
624 (setq iswitchb-default
625 (if (bufferp default)
626 (buffer-name default)
627 default))
628 (setq iswitchb-text (or start ""))
629 (unless matches-set
630 (setq iswitchb-rescan t)
631 (iswitchb-make-buflist iswitchb-default)
632 (iswitchb-set-matches))
633 (let
634 ((minibuffer-local-completion-map iswitchb-mode-map)
635 ;; Record the minibuffer depth that we expect to find once
636 ;; the minibuffer is set up and iswitchb-entryfn-p is called.
637 (iswitchb-minibuf-depth (1+ (minibuffer-depth)))
638 (iswitchb-require-match require-match))
639 ;; prompt the user for the buffer name
640 (setq iswitchb-final-text (completing-read
641 prompt ;the prompt
642 '(("dummy" . 1)) ;table
643 nil ;predicate
644 nil ;require-match [handled elsewhere]
645 start ;initial-contents
646 'iswitchb-history)))
647 (if (and (not (eq iswitchb-exit 'usefirst))
648 (get-buffer iswitchb-final-text))
649 ;; This happens for example if the buffer was chosen with the mouse.
650 (setq iswitchb-matches (list iswitchb-final-text)
651 iswitchb-virtual-buffers nil))
652
653 ;; If no buffer matched, but a virtual buffer was selected, visit
654 ;; that file now and act as though that buffer had been selected.
655 (if (and iswitchb-virtual-buffers
656 (not (iswitchb-existing-buffer-p)))
657 (let ((virt (car iswitchb-virtual-buffers)))
658 (find-file-noselect (cdr virt))
659 (setq iswitchb-matches (list (car virt))
660 iswitchb-virtual-buffers nil)))
661
662 ;; Handling the require-match must be done in a better way.
663 (if (and require-match
664 (not (iswitchb-existing-buffer-p)))
665 (error "Must specify valid buffer"))
666
667 (if (or (eq iswitchb-exit 'takeprompt)
668 (null iswitchb-matches))
669 (setq buf-sel iswitchb-final-text)
670 ;; else take head of list
671 (setq buf-sel (car iswitchb-matches)))
672
673 ;; Or possibly choose the default buffer
674 (if (equal iswitchb-final-text "")
675 (setq buf-sel (car iswitchb-matches)))
676
677 buf-sel))
678
679 (defun iswitchb-existing-buffer-p ()
680 "Return non-nil if there is a matching buffer."
681 (not (null iswitchb-matches)))
682
683 ;;; COMPLETION CODE
684
685 (defun iswitchb-set-common-completion ()
686 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
687 The result is stored in `iswitchb-common-match-string'."
688
689 (let (val)
690 (setq iswitchb-common-match-string nil)
691 (if (and iswitchb-matches
692 (not iswitchb-regexp) ;; testing
693 (stringp iswitchb-text)
694 (> (length iswitchb-text) 0))
695 (if (setq val (iswitchb-find-common-substring
696 iswitchb-matches iswitchb-text))
697 (setq iswitchb-common-match-string val)))
698 val))
699
700 (defun iswitchb-complete ()
701 "Try and complete the current pattern amongst the buffer names."
702 (interactive)
703 (let (res)
704 (cond ((not iswitchb-matches)
705 (run-hooks 'iswitchb-cannot-complete-hook))
706 (iswitchb-invalid-regexp
707 ;; Do nothing
708 )
709 ((= 1 (length iswitchb-matches))
710 ;; only one choice, so select it.
711 (exit-minibuffer))
712
713 (t
714 ;; else there could be some completions
715 (setq res iswitchb-common-match-string)
716 (if (and (not (memq res '(t nil)))
717 (not (equal res iswitchb-text)))
718 ;; found something to complete, so put it in the minibuffer.
719 (progn
720 (setq iswitchb-rescan nil
721 iswitchb-common-match-inserted t)
722 (delete-region (minibuffer-prompt-end) (point))
723 (insert res))
724 ;; else nothing to complete
725 (run-hooks 'iswitchb-cannot-complete-hook)
726 )))))
727
728 ;;; TOGGLE FUNCTIONS
729
730 (defun iswitchb-toggle-case ()
731 "Toggle the value of variable `iswitchb-case'."
732 (interactive)
733 (setq iswitchb-case (not iswitchb-case))
734 ;; ask for list to be regenerated.
735 (setq iswitchb-rescan t))
736
737 (defun iswitchb-toggle-regexp ()
738 "Toggle the value of `iswitchb-regexp'."
739 (interactive)
740 (setq iswitchb-regexp (not iswitchb-regexp))
741 ;; ask for list to be regenerated.
742 (setq iswitchb-rescan t))
743
744 (defun iswitchb-toggle-ignore ()
745 "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
746 (interactive)
747 (if iswitchb-buffer-ignore
748 (progn
749 (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
750 (setq iswitchb-buffer-ignore nil))
751 ;; else
752 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig))
753 (iswitchb-make-buflist iswitchb-default)
754 ;; ask for list to be regenerated.
755 (setq iswitchb-rescan t))
756
757 (defun iswitchb-exit-minibuffer ()
758 "Exit minibuffer, but make sure we have a match if one is needed."
759 (interactive)
760 (if (or (not iswitchb-require-match)
761 (iswitchb-existing-buffer-p))
762 (progn
763 (setq iswitchb-exit 'usefirst)
764 (throw 'exit nil))))
765
766 (defun iswitchb-select-buffer-text ()
767 "Select the buffer named by the prompt.
768 If no buffer exactly matching the prompt exists, maybe create a new one."
769 (interactive)
770 (setq iswitchb-exit 'takeprompt)
771 (exit-minibuffer))
772
773 (defun iswitchb-find-file ()
774 "Drop into `find-file' from buffer switching."
775 (interactive)
776 (setq iswitchb-exit 'findfile)
777 (exit-minibuffer))
778
779 (defvar recentf-list)
780
781 (defun iswitchb-next-match ()
782 "Put first element of `iswitchb-matches' at the end of the list."
783 (interactive)
784 (let ((next (cadr iswitchb-matches)))
785 (if (and (null next) iswitchb-virtual-buffers)
786 (setq recentf-list
787 (iswitchb-chop recentf-list
788 (cdr (cadr iswitchb-virtual-buffers))))
789 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next)))
790 (setq iswitchb-rescan t)))
791
792 (defun iswitchb-prev-match ()
793 "Put last element of `iswitchb-matches' at the front of the list."
794 (interactive)
795 (let ((prev (car (last iswitchb-matches))))
796 (if (and (null prev) iswitchb-virtual-buffers)
797 (setq recentf-list
798 (iswitchb-chop recentf-list
799 (cdr (car (last iswitchb-virtual-buffers)))))
800 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev)))
801 (setq iswitchb-rescan t)))
802
803 (defun iswitchb-chop (list elem)
804 "Remove all elements before ELEM and put them at the end of LIST."
805 (let ((ret nil)
806 (next nil)
807 (sofar nil))
808 (while (not ret)
809 (setq next (car list))
810 (if (equal next elem)
811 (setq ret (append list (nreverse sofar)))
812 ;; else
813 (progn
814 (setq list (cdr list))
815 (setq sofar (cons next sofar)))))
816 ret))
817
818 ;;; CREATE LIST OF ALL CURRENT BUFFERS
819
820 (defun iswitchb-make-buflist (default)
821 "Set `iswitchb-buflist' to the current list of buffers.
822 Currently visible buffers are put at the end of the list.
823 The hook `iswitchb-make-buflist-hook' is run after the list has been
824 created to allow the user to further modify the order of the buffer names
825 in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
826 it is put to the start of the list."
827 (setq iswitchb-buflist
828 (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
829 (iswitchb-temp-buflist
830 (delq nil
831 (mapcar
832 (lambda (x)
833 (let ((b-name (buffer-name x)))
834 (if (not
835 (or
836 (iswitchb-ignore-buffername-p b-name)
837 (memq b-name iswitchb-current-buffers)))
838 b-name)))
839 (buffer-list (and iswitchb-use-frame-buffer-list
840 (selected-frame)))))))
841 (setq iswitchb-temp-buflist
842 (nconc iswitchb-temp-buflist iswitchb-current-buffers))
843 (run-hooks 'iswitchb-make-buflist-hook)
844 ;; Should this be after the hooks, or should the hooks be the
845 ;; final thing to be run?
846 (if default
847 (progn
848 (setq iswitchb-temp-buflist
849 (delete default iswitchb-temp-buflist))
850 (setq iswitchb-temp-buflist
851 (cons default iswitchb-temp-buflist))))
852 iswitchb-temp-buflist)))
853
854 (defun iswitchb-to-end (lst)
855 "Move the elements from LST to the end of `iswitchb-temp-buflist'."
856 (dolist (elem lst)
857 (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist)))
858 (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst)))
859
860 (defun iswitchb-get-buffers-in-frames (&optional current)
861 "Return the list of buffers that are visible in the current frame.
862 If optional argument CURRENT is given, restrict searching to the
863 current frame, rather than all frames, regardless of value of
864 `iswitchb-all-frames'."
865 (let ((iswitchb-bufs-in-frame nil))
866 (walk-windows 'iswitchb-get-bufname nil
867 (if current
868 nil
869 iswitchb-all-frames))
870 iswitchb-bufs-in-frame))
871
872 (defun iswitchb-get-bufname (win)
873 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
874 (let ((buf (buffer-name (window-buffer win))))
875 (if (not (member buf iswitchb-bufs-in-frame))
876 ;; Only add buf if it is not already in list.
877 ;; This prevents same buf in two different windows being
878 ;; put into the list twice.
879 (setq iswitchb-bufs-in-frame
880 (cons buf iswitchb-bufs-in-frame)))))
881
882 ;;; FIND MATCHING BUFFERS
883
884 (defun iswitchb-set-matches ()
885 "Set `iswitchb-matches' to the list of buffers matching prompt."
886 (if iswitchb-rescan
887 (setq iswitchb-matches
888 (let ((buflist iswitchb-buflist))
889 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
890 buflist))
891 iswitchb-virtual-buffers nil)))
892
893 (defun iswitchb-get-matched-buffers (regexp
894 &optional string-format buffer-list)
895 "Return buffers matching REGEXP.
896 If STRING-FORMAT is nil, consider REGEXP as just a string.
897 BUFFER-LIST can be list of buffers or list of strings."
898 (let ((case-fold-search (iswitchb-case))
899 name ret)
900 (if (null string-format) (setq regexp (regexp-quote regexp)))
901 (setq iswitchb-invalid-regexp nil)
902 (condition-case error
903 (dolist (x buffer-list (nreverse ret))
904 (setq name (if (stringp x) x (buffer-name x)))
905 (when (and (string-match regexp name)
906 (not (iswitchb-ignore-buffername-p name)))
907 (push name ret)))
908 (invalid-regexp
909 (setq iswitchb-invalid-regexp t)
910 (cdr error)))))
911
912 (defun iswitchb-ignore-buffername-p (bufname)
913 "Return t if the buffer BUFNAME should be ignored."
914 (let ((data (match-data))
915 (re-list iswitchb-buffer-ignore)
916 ignorep
917 nextstr)
918 (while re-list
919 (setq nextstr (car re-list))
920 (cond
921 ((stringp nextstr)
922 (if (string-match nextstr bufname)
923 (progn
924 (setq ignorep t)
925 (setq re-list nil))))
926 ((functionp nextstr)
927 (if (funcall nextstr bufname)
928 (progn
929 (setq ignorep t)
930 (setq re-list nil)))))
931 (setq re-list (cdr re-list)))
932 (set-match-data data)
933
934 ;; return the result
935 ignorep))
936
937 (defun iswitchb-word-matching-substring (word)
938 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
939 If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
940 (let ((case-fold-search (iswitchb-case)))
941 (let ((m (string-match iswitchb-change-word-sub word)))
942 (if m
943 (substring word m)
944 ;; else no match
945 nil))))
946
947 (defun iswitchb-find-common-substring (lis subs)
948 "Return common string following SUBS in each element of LIS."
949 (let (res
950 alist
951 iswitchb-change-word-sub)
952 (setq iswitchb-change-word-sub
953 (if iswitchb-regexp
954 subs
955 (regexp-quote subs)))
956 (setq res (mapcar 'iswitchb-word-matching-substring lis))
957 (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
958 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
959
960 ;; try-completion returns t if there is an exact match.
961 (let ((completion-ignore-case (iswitchb-case)))
962
963 (try-completion subs alist))))
964
965 (defun iswitchb-makealist (res)
966 "Return dotted pair (RES . 1)."
967 (cons res 1))
968
969 ;; from Wayne Mesard <wmesard@esd.sgi.com>
970 (defun iswitchb-rotate-list (lis)
971 "Destructively remove the last element from LIS.
972 Return the modified list with the last element prepended to it."
973 (if (<= (length lis) 1)
974 lis
975 (let ((las lis)
976 (prev lis))
977 (while (consp (cdr las))
978 (setq prev las
979 las (cdr las)))
980 (setcdr prev nil)
981 (cons (car las) lis))))
982
983 (defun iswitchb-completion-help ()
984 "Show possible completions in a *Completions* buffer."
985 ;; we could allow this buffer to be used to select match, but I think
986 ;; choose-completion-string will need redefining, so it just inserts
987 ;; choice with out any previous input.
988 (interactive)
989 (setq iswitchb-rescan nil)
990 (let ((buf (current-buffer))
991 (temp-buf "*Completions*")
992 (win))
993
994 (if (and (eq last-command this-command)
995 (not iswitchb-common-match-inserted))
996 ;; scroll buffer
997 (progn
998 (set-buffer temp-buf)
999 (setq win (get-buffer-window temp-buf))
1000 (if (pos-visible-in-window-p (point-max) win)
1001 (set-window-start win (point-min))
1002 (scroll-other-window))
1003 (set-buffer buf))
1004
1005 (with-output-to-temp-buffer temp-buf
1006 (if (featurep 'xemacs)
1007
1008 ;; XEmacs extents are put on by default, doesn't seem to be
1009 ;; any way of switching them off.
1010 (display-completion-list (if iswitchb-matches
1011 iswitchb-matches
1012 iswitchb-buflist)
1013 :help-string "iswitchb "
1014 :activate-callback
1015 (lambda (x y z)
1016 (message "doesn't work yet, sorry!")))
1017 ;; else running Emacs
1018 (with-current-buffer standard-output
1019 (fundamental-mode))
1020 (display-completion-list (if iswitchb-matches
1021 iswitchb-matches
1022 iswitchb-buflist))))
1023 (setq iswitchb-common-match-inserted nil))))
1024
1025 ;;; KILL CURRENT BUFFER
1026
1027 (defun iswitchb-kill-buffer ()
1028 "Kill the buffer at the head of `iswitchb-matches'."
1029 (interactive)
1030 (let ( (enable-recursive-minibuffers t)
1031 buf)
1032
1033 (setq buf (car iswitchb-matches))
1034 ;; check to see if buf is non-nil.
1035 (if buf
1036 (progn
1037 (kill-buffer buf)
1038
1039 ;; Check if buffer exists. XEmacs gnuserv.el makes alias
1040 ;; for kill-buffer which does not return t if buffer is
1041 ;; killed, so we can't rely on kill-buffer return value.
1042 (if (get-buffer buf)
1043 ;; buffer couldn't be killed.
1044 (setq iswitchb-rescan t)
1045 ;; else buffer was killed so remove name from list.
1046 (setq iswitchb-buflist (delq buf iswitchb-buflist)))))))
1047
1048 ;;; VISIT CHOSEN BUFFER
1049 (defun iswitchb-visit-buffer (buffer)
1050 "Visit buffer named BUFFER according to `iswitchb-method'."
1051 (let (win newframe)
1052 (cond
1053 ((eq iswitchb-method 'samewindow)
1054 (switch-to-buffer buffer))
1055
1056 ((memq iswitchb-method '(always-frame maybe-frame))
1057 (cond
1058 ((and (setq win (iswitchb-window-buffer-p buffer))
1059 (or (eq iswitchb-method 'always-frame)
1060 (y-or-n-p "Jump to frame? ")))
1061 (setq newframe (window-frame win))
1062 (if (fboundp 'select-frame-set-input-focus)
1063 (select-frame-set-input-focus newframe)
1064 (raise-frame newframe)
1065 (select-frame newframe)
1066 )
1067 (select-window win))
1068 (t
1069 ;; No buffer in other frames...
1070 (switch-to-buffer buffer)
1071 )))
1072
1073 ((eq iswitchb-method 'otherwindow)
1074 (switch-to-buffer-other-window buffer))
1075
1076 ((eq iswitchb-method 'display)
1077 (display-buffer buffer))
1078
1079 ((eq iswitchb-method 'otherframe)
1080 (progn
1081 (switch-to-buffer-other-frame buffer)
1082 (if (fboundp 'select-frame-set-input-focus)
1083 (select-frame-set-input-focus (selected-frame)))
1084 )))))
1085
1086 (defun iswitchb-possible-new-buffer (buf)
1087 "Possibly create and visit a new buffer called BUF."
1088
1089 (let ((newbufcreated))
1090 (if (and iswitchb-newbuffer
1091 (or
1092 (not iswitchb-prompt-newbuffer)
1093
1094 (and iswitchb-prompt-newbuffer
1095 (y-or-n-p
1096 (format
1097 "No buffer matching `%s', create one? "
1098 buf)))))
1099 ;; then create a new buffer
1100 (progn
1101 (setq newbufcreated (get-buffer-create buf))
1102 (if (fboundp 'set-buffer-major-mode)
1103 (set-buffer-major-mode newbufcreated))
1104 (iswitchb-visit-buffer newbufcreated))
1105 ;; else wont create new buffer
1106 (message "no buffer matching `%s'" buf))))
1107
1108 (defun iswitchb-window-buffer-p (buffer)
1109 "Return window pointer if BUFFER is visible in another frame.
1110 If BUFFER is visible in the current frame, return nil."
1111 (interactive)
1112 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
1113 ;;If the buffer is visible in current frame, return nil
1114 (if (memq buffer blist)
1115 nil
1116 ;; maybe in other frame or icon
1117 (get-buffer-window buffer 0) ; better than 'visible
1118 )))
1119
1120 (defun iswitchb-default-keybindings ()
1121 "Set up default keybindings for `iswitchb-buffer'.
1122 Call this function to override the normal bindings. This function also
1123 adds a hook to the minibuffer."
1124 (interactive)
1125 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1126 (global-set-key "\C-xb" 'iswitchb-buffer)
1127 (global-set-key "\C-x4b" 'iswitchb-buffer-other-window)
1128 (global-set-key "\C-x4\C-o" 'iswitchb-display-buffer)
1129 (global-set-key "\C-x5b" 'iswitchb-buffer-other-frame))
1130
1131 (make-obsolete 'iswitchb-default-keybindings 'iswitchb-mode "21.1")
1132
1133 (defun iswitchb-buffer ()
1134 "Switch to another buffer.
1135
1136 The buffer name is selected interactively by typing a substring. The
1137 buffer is displayed according to `iswitchb-default-method' -- the
1138 default is to show it in the same window, unless it is already visible
1139 in another frame.
1140 For details of keybindings, do `\\[describe-function] iswitchb'."
1141 (interactive)
1142 (setq iswitchb-method iswitchb-default-method)
1143 (iswitchb))
1144
1145 (defun iswitchb-buffer-other-window ()
1146 "Switch to another buffer and show it in another window.
1147 The buffer name is selected interactively by typing a substring.
1148 For details of keybindings, do `\\[describe-function] iswitchb'."
1149 (interactive)
1150 (setq iswitchb-method 'otherwindow)
1151 (iswitchb))
1152
1153 (defun iswitchb-display-buffer ()
1154 "Display a buffer in another window but don't select it.
1155 The buffer name is selected interactively by typing a substring.
1156 For details of keybindings, do `\\[describe-function] iswitchb'."
1157 (interactive)
1158 (setq iswitchb-method 'display)
1159 (iswitchb))
1160
1161 (defun iswitchb-buffer-other-frame ()
1162 "Switch to another buffer and show it in another frame.
1163 The buffer name is selected interactively by typing a substring.
1164 For details of keybindings, do `\\[describe-function] iswitchb'."
1165 (interactive)
1166 (setq iswitchb-method 'otherframe)
1167 (iswitchb))
1168
1169 ;;; XEmacs hack for showing default buffer
1170
1171 ;; The first time we enter the minibuffer, Emacs puts up the default
1172 ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
1173 ;; subtle difference in the two versions of post-command-hook. The
1174 ;; default is shown for both whenever we delete all of our text
1175 ;; though, indicating its just a problem the first time we enter the
1176 ;; function. To solve this, we use another entry hook for emacs to
1177 ;; show the default the first time we enter the minibuffer.
1178
1179 (defun iswitchb-init-XEmacs-trick ()
1180 "Display default buffer when first entering minibuffer.
1181 This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
1182 (if (iswitchb-entryfn-p)
1183 (progn
1184 (iswitchb-exhibit)
1185 (goto-char (point-min)))))
1186
1187 ;; add this hook for XEmacs only.
1188 (if (featurep 'xemacs)
1189 (add-hook 'iswitchb-minibuffer-setup-hook
1190 'iswitchb-init-XEmacs-trick))
1191
1192 ;;; XEmacs / backspace key
1193 ;; For some reason, if the backspace key is pressed in XEmacs, the
1194 ;; line gets confused, so I've added a simple key definition to make
1195 ;; backspace act like the normal delete key.
1196
1197 (defun iswitchb-xemacs-backspacekey ()
1198 "Bind backspace to `backward-delete-char'."
1199 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
1200 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word))
1201
1202 (if (featurep 'xemacs)
1203 (add-hook 'iswitchb-define-mode-map-hook
1204 'iswitchb-xemacs-backspacekey))
1205
1206 ;;; ICOMPLETE TYPE CODE
1207
1208 (defun iswitchb-exhibit ()
1209 "Find matching buffers and display a list in the minibuffer.
1210 Copied from `icomplete-exhibit' with two changes:
1211 1. It prints a default buffer name when there is no text yet entered.
1212 2. It calls my completion routine rather than the standard completion."
1213 (if iswitchb-use-mycompletion
1214 (let ((contents (buffer-substring (minibuffer-prompt-end) (point-max)))
1215 (buffer-undo-list t))
1216 (save-excursion
1217 (goto-char (point-max))
1218 ; Register the end of input, so we
1219 ; know where the extra stuff
1220 ; (match-status info) begins:
1221 (if (not (boundp 'iswitchb-eoinput))
1222 ;; In case it got wiped out by major mode business:
1223 (make-local-variable 'iswitchb-eoinput))
1224 (setq iswitchb-eoinput (point))
1225 ;; Update the list of matches
1226 (setq iswitchb-text contents)
1227 (iswitchb-set-matches)
1228 (setq iswitchb-rescan t)
1229 (iswitchb-set-common-completion)
1230
1231 ;; Insert the match-status information:
1232 (insert (iswitchb-completions
1233 contents))))))
1234
1235 (defvar most-len)
1236 (defvar most-is-exact)
1237
1238 (defun iswitchb-output-completion (com)
1239 (if (= (length com) most-len)
1240 ;; Most is one exact match,
1241 ;; note that and leave out
1242 ;; for later indication:
1243 (ignore
1244 (setq most-is-exact t))
1245 (substring com most-len)))
1246
1247 (defun iswitchb-completions (name)
1248 "Return the string that is displayed after the user's text.
1249 Modified from `icomplete-completions'."
1250
1251 (let ((comps iswitchb-matches)
1252 ; "-determined" - only one candidate
1253 (open-bracket-determined "[")
1254 (close-bracket-determined "]")
1255 ;"-prospects" - more than one candidate
1256 (open-bracket-prospects "{")
1257 (close-bracket-prospects "}")
1258 first)
1259
1260 (if (and iswitchb-use-faces comps)
1261 (progn
1262 (setq first (car comps))
1263 (setq first (format "%s" first))
1264 (put-text-property 0 (length first) 'face
1265 (if (= (length comps) 1)
1266 (if iswitchb-invalid-regexp
1267 'iswitchb-invalid-regexp
1268 'iswitchb-single-match)
1269 'iswitchb-current-match)
1270 first)
1271 (setq comps (cons first (cdr comps)))))
1272
1273 ;; If no buffers matched, and virtual buffers are being used, then
1274 ;; consult the list of past visited files, to see if we can find
1275 ;; the file which the user might thought was still open.
1276 (when (and iswitchb-use-virtual-buffers (null comps)
1277 recentf-list)
1278 (setq iswitchb-virtual-buffers nil)
1279 (let ((head recentf-list) name)
1280 (while head
1281 (if (and (setq name (file-name-nondirectory (car head)))
1282 (string-match (if iswitchb-regexp
1283 iswitchb-text
1284 (regexp-quote iswitchb-text)) name)
1285 (null (get-file-buffer (car head)))
1286 (not (assoc name iswitchb-virtual-buffers))
1287 (not (iswitchb-ignore-buffername-p name))
1288 (file-exists-p (car head)))
1289 (setq iswitchb-virtual-buffers
1290 (cons (cons name (car head))
1291 iswitchb-virtual-buffers)))
1292 (setq head (cdr head)))
1293 (setq iswitchb-virtual-buffers (nreverse iswitchb-virtual-buffers)
1294 comps (mapcar 'car iswitchb-virtual-buffers))
1295 (let ((comp comps))
1296 (while comp
1297 (put-text-property 0 (length (car comp))
1298 'face 'iswitchb-virtual-matches
1299 (car comp))
1300 (setq comp (cdr comp))))))
1301
1302 (cond ((null comps) (format " %sNo match%s"
1303 open-bracket-determined
1304 close-bracket-determined))
1305
1306 (iswitchb-invalid-regexp
1307 (concat " " (car comps)))
1308 ((null (cdr comps)) ;one match
1309 (concat
1310 (if (if (not iswitchb-regexp)
1311 (= (length name)
1312 (length (car comps)))
1313 (string-match name (car comps))
1314 (string-equal (match-string 0 (car comps))
1315 (car comps)))
1316 ""
1317 (concat open-bracket-determined
1318 ;; when there is one match, show the
1319 ;; matching buffer name in full
1320 (car comps)
1321 close-bracket-determined))
1322 (if (not iswitchb-use-faces) " [Matched]")))
1323 (t ;multiple matches
1324 (if (and iswitchb-max-to-show
1325 (> (length comps) iswitchb-max-to-show))
1326 (setq comps
1327 (append
1328 (let ((res nil)
1329 (comp comps)
1330 (end (/ iswitchb-max-to-show 2)))
1331 (while (>= (setq end (1- end)) 0)
1332 (setq res (cons (car comp) res)
1333 comp (cdr comp)))
1334 (nreverse res))
1335 (list "...")
1336 (nthcdr (- (length comps)
1337 (/ iswitchb-max-to-show 2)) comps))))
1338 (let* (
1339 ;;(most (try-completion name candidates predicate))
1340 (most nil)
1341 (most-len (length most))
1342 most-is-exact
1343 (alternatives
1344 (mapconcat (if most 'iswitchb-output-completion
1345 'identity) comps ",")))
1346
1347 (concat
1348
1349 ;; put in common completion item -- what you get by
1350 ;; pressing tab
1351 (if (and (stringp iswitchb-common-match-string)
1352 (> (length iswitchb-common-match-string) (length name)))
1353 (concat open-bracket-determined
1354 (substring iswitchb-common-match-string
1355 (length name))
1356 close-bracket-determined))
1357 ;; end of partial matches...
1358
1359 ;; think this bit can be ignored.
1360 (and (> most-len (length name))
1361 (concat open-bracket-determined
1362 (substring most (length name))
1363 close-bracket-determined))
1364
1365 ;; list all alternatives
1366 open-bracket-prospects
1367 (if most-is-exact
1368 (concat "," alternatives)
1369 alternatives)
1370 close-bracket-prospects))))))
1371
1372 (defun iswitchb-minibuffer-setup ()
1373 "Set up minibuffer for `iswitchb-buffer'.
1374 Copied from `icomplete-minibuffer-setup-hook'."
1375 (when (iswitchb-entryfn-p)
1376 (set (make-local-variable 'iswitchb-use-mycompletion) t)
1377 (add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
1378 (add-hook 'post-command-hook 'iswitchb-post-command nil t)
1379 (run-hooks 'iswitchb-minibuffer-setup-hook)))
1380
1381 (defun iswitchb-pre-command ()
1382 "Run before command in `iswitchb-buffer'."
1383 (iswitchb-tidy))
1384
1385 (defun iswitchb-post-command ()
1386 "Run after command in `iswitchb-buffer'."
1387 (iswitchb-exhibit))
1388
1389 (defun iswitchb-tidy ()
1390 "Remove completions display, if any, prior to new user input.
1391 Copied from `icomplete-tidy'."
1392
1393 (if (and (boundp 'iswitchb-eoinput)
1394 iswitchb-eoinput)
1395
1396 (if (> iswitchb-eoinput (point-max))
1397 ;; Oops, got rug pulled out from under us - reinit:
1398 (setq iswitchb-eoinput (point-max))
1399 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1400 (delete-region iswitchb-eoinput (point-max))))
1401
1402 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1403 (make-local-variable 'iswitchb-eoinput)
1404 (setq iswitchb-eoinput 1)))
1405
1406 (defun iswitchb-entryfn-p ()
1407 "Return non-nil if we are using `iswitchb-buffer'."
1408 (eq iswitchb-minibuf-depth (minibuffer-depth)))
1409
1410 (defun iswitchb-summaries-to-end ()
1411 "Move the summaries to the end of the list.
1412 This is an example function which can be hooked on to
1413 `iswitchb-make-buflist-hook'. Any buffer matching the regexps
1414 `Summary' or `output\*$'are put to the end of the list."
1415 (let ((summaries (delq nil
1416 (mapcar
1417 (lambda (x)
1418 (if (string-match "Summary\\|output\\*$" x)
1419 x))
1420 iswitchb-temp-buflist))))
1421 (iswitchb-to-end summaries)))
1422
1423 (defun iswitchb-case ()
1424 "Return non-nil if we should ignore case when matching.
1425 See the variable `iswitchb-case' for details."
1426 (if iswitchb-case
1427 (if (featurep 'xemacs)
1428 (isearch-no-upper-case-p iswitchb-text)
1429 (isearch-no-upper-case-p iswitchb-text t))))
1430
1431 ;;;###autoload
1432 (define-minor-mode iswitchb-mode
1433 "Toggle Iswitchb global minor mode.
1434 With arg, turn Iswitchb mode on if ARG is positive, otherwise turn it off.
1435 This mode enables switching between buffers using substrings. See
1436 `iswitchb' for details."
1437 nil nil iswitchb-global-map :global t :group 'iswitchb
1438 (if iswitchb-mode
1439 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1440 (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))
1441
1442 (provide 'iswitchb)
1443
1444 ;; arch-tag: d74198ae-753f-44f2-b34f-0c515398d90a
1445 ;;; iswitchb.el ends here