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