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