(show-paren-match-face): Use gray on all non-color screens.
[emacs.git] / lisp / iswitchb.el
blob4aa3803ea2aa2f27871c33401ee1397c21f28183
1 ;;; iswitchb.el --- switch between buffers using substrings
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Stephen Eglen <stephene@cogs.susx.ac.uk>
6 ;; Maintainer: Stephen Eglen <stephene@cogs.susx.ac.uk>
7 ;; Keywords: extensions
8 ;; location: http://www.cogs.susx.ac.uk/users/stephene/emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
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.
27 ;;; Installation:
29 ;; To get the functions in this package bound to keys, do
30 ;; (iswitchb-default-keybindings)
32 ;;; Commentary:
34 ;; As you type in a substring, the list of buffers currently matching
35 ;; the substring are displayed as you type. The list is ordered so
36 ;; that the most recent buffers visited come at the start of the list.
37 ;; The buffer at the start of the list will be the one visited when
38 ;; you press return. By typing more of the substring, the list is
39 ;; narrowed down so that gradually the buffer you want will be at the
40 ;; top of the list. Alternatively, you can use C-s an C-r to rotate
41 ;; buffer names in the list until the one you want is at the top of
42 ;; the list. Completion is also available so that you can see what is
43 ;; common to all of the matching buffers as you type.
45 ;; This code is similar to a couple of other packages. Michael R Cook
46 ;; <mcook@cognex.com wrote a similar buffer switching package, but
47 ;; does exact matching rather than substring matching on buffer names.
48 ;; I also modified a couple of functions from icomplete.el to provide
49 ;; the completion feedback in the minibuffer.
51 ;;; Example
53 ;;If I have two buffers called "123456" and "123", with "123456" the
54 ;;most recent, when I use iswitchb, I first of all get presented with
55 ;;the list of all the buffers
57 ;; iswitch {123456,123}
59 ;; If I then press 2:
60 ;; iswitch 2[3]{123456,123}
62 ;; The list in {} are the matching buffers, most recent first (buffers
63 ;; visible in the current frame are put at the end of the list by
64 ;; default). At any time I can select the item at the head of the
65 ;; list by pressing RET. I can also bring the put the first element
66 ;; at the end of the list by pressing C-s, or put the last element at
67 ;; the head of the list by pressing C-r. The item in [] indicates
68 ;; what can be added to my input by pressing TAB. In this case, I
69 ;; will get "3" added to my input. So, press TAB:
70 ;; iswitch 23{123456,123}
72 ;; At this point, I still have two matching buffers.
73 ;; If I want the first buffer in the list, I simply press RET. If I
74 ;; wanted the second in the list, I could press C-s to move it to the
75 ;; top of the list and then RET to select it.
77 ;;However, If I type 4, I only have one match left:
78 ;; iswitch 234[123456] [Matched]
80 ;;Since there is only one matching buffer left, it is given in [] and we
81 ;;see the text [Matched] afterwards. I can now press TAB or RET to go
82 ;;to that buffer.
84 ;; If however, I now type "a":
85 ;; iswitch 234a [No match]
86 ;; There are no matching buffers. If I press RET or TAB, I can be
87 ;; prompted to create a new buffer called "234a".
89 ;; Of course, where this function comes in really useful is when you
90 ;; can specify the buffer using only a few keystrokes. In the above
91 ;; example, the quickest way to get to the "123456" buffer would be
92 ;; just to type 4 and then RET (assuming there isnt any newer buffer
93 ;; with 4 in its name).
95 ;; To see a full list of all matching buffers in a separate buffer,
96 ;; hit ? or press TAB when there are no further completions to the
97 ;; substring.
99 ;; The buffer at the head of the list can be killed by pressing C-k.
100 ;; If the buffer needs saving, you will be queried before the buffer
101 ;; is killed.
103 ;; If you find that the file you are after is not in a buffer, you can
104 ;; press C-x C-f to immediately drop into find-file.
107 ;; See the doc string of iswitchb for full keybindings and features.
108 ;; (describe-function 'iswitchb)
110 ;;; Customisation
112 ;; See the User Variables section below for easy ways to change the
113 ;; functionality of the program. These are accessible using the
114 ;; custom package.
115 ;; To modify the keybindings, use the hook provided. For example:
116 ;;(add-hook 'iswitchb-define-mode-map-hook
117 ;; 'iswitchb-my-keys)
119 ;;(defun iswitchb-my-keys ()
120 ;; "Add my keybings for iswitchb."
121 ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match)
122 ;; )
124 ;; Seeing all the matching buffers.
126 ;; If you have many matching buffers, they may not all fit onto one
127 ;; line of the minibuffer. In this case, you should use rsz-mini
128 ;; (resize-minibuffer-mode). You can also limit iswitchb so that it
129 ;; only shows a certain number of lines -- see the documentation for
130 ;; `iswitchb-minibuffer-setup-hook'.
133 ;; Changing the list of buffers.
135 ;; By default, the list of current buffers is most recent first,
136 ;; oldest last, with the exception that the buffers visible in the
137 ;; current frame are put at the end of the list. A hook exists to
138 ;; allow other functions to order the list. For example, if you add:
140 ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
142 ;; then all buffers matching "Summary" are moved to the end of the
143 ;; list. (I find this handy for keeping the INBOX Summary and so on
144 ;; out of the way.) It also moves buffers matching "output\*$" to the
145 ;; end of the list (these are created by AUC TeX when compiling.)
146 ;; Other functions could be made available which alter the list of
147 ;; matching buffers (either deleting or rearranging elements.)
149 ;; Font-Lock
151 ;; If you have font-lock loaded, the first matching buffer is
152 ;; highlighted. To switch this off, set (setq iswitchb-use-fonts nil)
153 ;; I don't use font-lock that much, so I've hardcoded the faces. If
154 ;; this is too harsh, let me know. Colouring of the matching buffer
155 ;; name was suggested by Carsten Dominik (dominik@strw.leidenuniv.nl)
157 ;;; Comparison with iswitch-buffer
159 ;; This package is a rewrite of iswitch-buffer, using the minibuffer
160 ;; rather than the echo area. The advantages of using the minibuffer
161 ;; are several:
162 ;; o minibuffer has more powerful editing facilities
163 ;; o doesnt interfere with other packages that use the echo area
164 ;; o *Messages* buffer doesnt get filled up with all of the messages that
165 ;; go to the modeline
166 ;; o cursor is in the minibuffer, which somehow looks right.
167 ;; o minibuffer can be resized dynamically to show all the possible matching
168 ;; buffers rather than just the first line's worth (using rsz-mini).
170 ;; Disadvantages:
171 ;; o cant change the prompt to indicate status of searching (eg whether
172 ;; regexp searching is currently on).
175 ;;; TODO
176 ;; Could this selection also be used for other buffer selection
177 ;; routines, such as append-to-buffer and kill-buffer?
179 ;; Pressing Tab key twice (without completion) does not scroll the
180 ;; list of buffers.
182 ;;; Acknowledgements
184 ;; Thanks to Jari Aalto <jari.aalto@poboxes.com> for help with the
185 ;; first version of this package, iswitch-buffer. Thanks also to many
186 ;; others for testing earlier versions.
188 ;;; Code:
190 ;; Set up the custom library.
191 ;; taken from http://www.dina.kvl.dk/~abraham/custom/
192 (eval-and-compile
193 (condition-case ()
194 (require 'custom)
195 (error nil))
196 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
197 nil ;; We've got what we needed
198 ;; We have the old custom-library, hack around it!
199 (defmacro defgroup (&rest args)
200 nil)
201 (defmacro defcustom (var value doc &rest args)
202 (` (defvar (, var) (, value) (, doc))))))
204 ;;; User Variables
206 ;; These are some things you might want to change.
208 (defgroup iswitchb nil
209 "switch between buffers using substrings."
210 :group 'extensions
211 ;; These links are to be added in later versions of custom and
212 ;; so are currently commented out.
213 :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el")
214 :link '(emacs-library-link :tag "Lisp File" "iswitchb.el")
218 (defcustom iswitchb-case case-fold-search
219 "*Non-nil if searching of buffer names should ignore case."
220 :type 'boolean
221 :group 'iswitchb)
223 (defcustom iswitchb-buffer-ignore
224 '("^ ")
225 "*List of regexps or functions matching buffer names to ignore.
226 For example, traditional behavior is not to list buffers whose names begin
227 with a space, for which the regexp is `^ '. See the source file for
228 example functions that filter buffernames."
229 :type '(repeat regexp)
230 :group 'iswitchb)
233 ;;; Examples for setting the value of iswitchb-buffer-ignore
234 ;(defun -c-mode (name)
235 ; "Ignore all c mode buffers -- example function for iswitchb."
236 ; (save-excursion
237 ; (set-buffer name)
238 ; (string-match "^C$" mode-name)))
240 ;(setq iswitchb-buffer-ignore '("^ " ignore-c-mode))
241 ;(setq iswitchb-buffer-ignore '("^ " "\\.c$" "\\.h$"))
243 (defcustom iswitchb-default-method 'always-frame
244 "*How to switch to new buffer when using `iswitchb-buffer'.
245 Possible values:
246 `samewindow' Show new buffer in same window
247 `otherwindow' Show new buffer in another window (same frame)
248 `display' Display buffer in another window without switching to it
249 `otherframe' Show new buffer in another frame
250 `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
251 you want to see the buffer in the same window of the current
252 frame or in the other frame.
253 `always-frame' If a buffer is visible in another frame, raise that
254 frame. Otherwise, visit the buffer in the same window."
255 :type '(choice (const :tag "samewindow" samewindow)
256 (const :tag "otherwindow" otherwindow)
257 (const :tag "display" display)
258 (const :tag "otherframe" otherframe)
259 (const :tag "maybe-frame" maybe-frame)
260 (const :tag "always-frame" always-frame))
261 :group 'iswitchb)
264 (defcustom iswitchb-regexp nil
265 "*Non-nil means that `iswitchb' will do regexp matching.
266 Value can be toggled within `iswitchb'."
267 :type 'boolean
268 :group 'iswitchb)
271 (defcustom iswitchb-newbuffer t
272 "*Non-nil means create new buffer if no buffer matches substring.
273 See also `iswitchb-prompt-newbuffer'."
274 :type 'boolean
275 :group 'iswitchb)
278 (defcustom iswitchb-prompt-newbuffer t
279 "*Non-nil means prompt user to confirm before creating new buffer.
280 See also `iswitchb-newbuffer'."
281 :type 'boolean
282 :group 'iswitchb)
285 (defcustom iswitchb-define-mode-map-hook nil
286 "*Hook to define keys in `iswitchb-mode-map' for extra keybindings."
287 :type 'hook
288 :group 'iswitchb)
292 (defcustom iswitchb-use-fonts t
293 "*Non-nil means use fonts for showing first match."
294 :type 'boolean
295 :group 'iswitchb)
298 (defcustom iswitchb-make-buflist-hook nil
299 "*Hook to run when list of matching buffers is created."
300 :type 'hook
301 :group 'iswitchb)
305 (defvar iswitchb-method nil
306 "*Stores the method for viewing the selected buffer.
307 Its value is one of `samewindow', `otherwindow', `display', `otherframe',
308 `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
309 details of values.")
311 (defvar iswitchb-all-frames 'visible
312 "*Argument to pass to `walk-windows' when finding visible buffers.
313 See documentation of `walk-windows' for useful values.")
317 ;; Do we need the variable iswitchb-use-mycompletion?
320 ;;; Internal Variables
321 (defvar iswitchb-minibuffer-setup-hook nil
322 "Iswitchb-specific customization of minibuffer setup.
324 This hook is run during minibuffer setup iff `iswitchb' will be active.
325 It is intended for use in customizing iswitchb for interoperation
326 with other packages. For instance:
328 \(add-hook 'iswitchb-minibuffer-setup-hook
329 \(function
330 \(lambda ()
331 \(make-local-variable 'resize-minibuffer-window-max-height)
332 \(setq resize-minibuffer-window-max-height 3))))
334 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
335 iswitchb is running. Copied from `icomplete-minibuffer-setup-hook'.")
337 (defvar iswitchb-eoinput 1
338 "Point where minibuffer input ends and completion info begins.
339 Copied from `icomplete-eoinput'.")
340 (make-variable-buffer-local 'iswitchb-eoinput)
343 (defvar iswitchb-buflist nil
344 "Stores the current list of buffers that will be searched through.
345 The list is ordered, so that the most recent buffers come first,
346 although by default, the buffers visible in the current frame are put
347 at the end of the list. Created by `iswitchb-make-buflist'.")
349 ;; todo -- is this necessary?
351 (defvar iswitchb-use-mycompletion nil
352 "Non-nil means use `iswitchb-buffer' completion feedback.
353 Should only be set to t by iswitchb functions, so that it doesn't
354 interfere with other minibuffer usage.")
356 (defvar iswitchb-change-word-sub nil
357 "Private variable used by `iswitchb-word-matching-substring'.")
360 (defvar iswitchb-common-match-string nil
361 "Stores the string that is common to all matching buffers.")
364 (defvar iswitchb-rescan nil
365 "Non-nil means we need to regenerate the list of matching buffers.")
367 (defvar iswitchb-text nil
368 "Stores the users string as it is typed in.")
370 (defvar iswitchb-matches nil
371 "List of buffers currenly matching `iswitchb-text'.")
373 (defvar iswitchb-mode-map nil
374 "Keymap for `iswitchb-buffer'.")
376 (defvar iswitchb-history nil
377 "History of buffers selected using `iswitchb-buffer'.")
379 (defvar iswitchb-exit nil
380 "Flag to monitor how `iswitchb-buffer' exits.
381 If equal to `takeprompt', we use the prompt as the buffer name to be
382 selected.")
384 (defvar iswitchb-buffer-ignore-orig nil
385 "Stores original value of `iswitchb-buffer-ignore'.")
387 (defvar iswitchb-xemacs (string-match "XEmacs" (emacs-version))
388 "Non-nil if we are running XEmacs. Otherwise, assume we are running Emacs.")
391 ;;; FUNCTIONS
394 ;;; ISWITCHB KEYMAP
395 (defun iswitchb-define-mode-map ()
396 "Set up the keymap for `iswitchb-buffer'."
397 (interactive)
398 (let (map)
399 ;; generated every time so that it can inheret new functions.
400 ;;(or iswitchb-mode-map
402 (setq map (copy-keymap minibuffer-local-map))
403 (define-key map "?" 'iswitchb-completion-help)
404 (define-key map "\C-s" 'iswitchb-next-match)
405 (define-key map "\C-r" 'iswitchb-prev-match)
406 (define-key map "\t" 'iswitchb-complete)
407 (define-key map "\C-j" 'iswitchb-select-buffer-text)
408 (define-key map "\C-t" 'iswitchb-toggle-regexp)
409 (define-key map "\C-x\C-f" 'iswitchb-find-file)
410 ;;(define-key map "\C-a" 'iswitchb-toggle-ignore)
411 (define-key map "\C-c" 'iswitchb-toggle-case)
412 (define-key map "\C-k" 'iswitchb-kill-buffer)
413 (setq iswitchb-mode-map map)
414 (run-hooks 'iswitchb-define-mode-map-hook)
419 ;;; MAIN FUNCTION
420 (defun iswitchb ()
421 "Switch to buffer matching a substring.
422 As you type in a string, all of the buffers matching the string are
423 displayed. When you have found the buffer you want, it can then be
424 selected. As you type, most keys have their normal keybindings,
425 except for the following:
426 \\<iswitchb-mode-map>
428 RET Select the buffer at the front of the list of matches. If the
429 list is empty, possibly prompt to create new buffer.
431 \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
432 If no buffer is found, prompt for a new one.
434 \\[iswitchb-next-match] Put the first element at the end of the list.
435 \\[iswitchb-prev-match] Put the last element at the start of the list.
436 \\[iswitchb-complete] Complete a common suffix to the current string that
437 matches all buffers. If there is only one match, select that buffer.
438 If there is no common suffix, show a list of all matching buffers
439 in a separate window.
440 \\[iswitchb-toggle-regexp] Toggle rexep searching.
441 \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
442 \\[iswitchb-completion-help] Show list of matching buffers in separate window.
443 \\[iswitchb-find-file] Exit iswitchb and drop into find-file.
444 \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
445 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
446 ;;`iswitchb-buffer-ignore')
448 (let
450 prompt
451 buf-sel
452 iswitchb-final-text
453 (minibuffer-confirm-incomplete nil) ;XEmacs todo: prevent `;confirm'
454 (icomplete-mode nil) ;; prevent icomplete starting up
455 ;; can only use fonts if they have been bound.
456 (iswitchb-use-fonts (and iswitchb-use-fonts
457 (boundp 'font-lock-comment-face)
458 (boundp 'font-lock-function-name-face)))
461 (iswitchb-define-mode-map)
462 (setq iswitchb-exit nil)
463 (setq iswitchb-rescan t)
464 (setq iswitchb-text "")
465 (iswitchb-set-matches)
466 (setq prompt (format "iswitch "))
467 (iswitchb-make-buflist)
468 (let
469 ((minibuffer-local-completion-map iswitchb-mode-map))
470 ;; prompt the user for the buffer name
471 (setq iswitchb-final-text (completing-read prompt
472 ;;nil
473 '(("dummy".1))
474 ;;("2".2) ("3".3))
475 nil nil
476 nil;init string
477 'iswitchb-history)))
479 ;;(message "chosen text %s" iswitchb-final-text)
480 ;; Choose the buffer name: either the text typed in, or the head
481 ;; of the list of matches
483 (cond ( (eq iswitchb-exit 'findfile)
484 (call-interactively 'find-file))
487 (if (or
488 (eq iswitchb-exit 'takeprompt)
489 (null iswitchb-matches))
490 (setq buf-sel iswitchb-final-text)
491 ;; else take head of list
492 (setq buf-sel (car iswitchb-matches)))
494 ;; Or possibly choose the default buffer
495 (if (equal iswitchb-final-text "")
496 (setq buf-sel (car iswitchb-matches)))
498 ;; View the buffer
499 (message "go to buf %s" buf-sel)
500 ;; Check buf-sel is non-nil.
501 (if buf-sel
502 (if (get-buffer buf-sel)
503 ;; buffer exists, so view it and then exit
504 (iswitchb-visit-buffer buf-sel)
505 ;; else buffer doesnt exist
506 (iswitchb-possible-new-buffer buf-sel)))
512 ;;; COMPLETION CODE
514 (defun iswitchb-set-common-completion ()
515 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
516 The result is stored in `iswitchb-common-match-string'."
518 (let* (val)
519 (setq iswitchb-common-match-string nil)
520 (if (and iswitchb-matches
521 (stringp iswitchb-text)
522 (> (length iswitchb-text) 0))
523 (if (setq val (iswitchb-find-common-substring
524 iswitchb-matches iswitchb-text))
525 (setq iswitchb-common-match-string val)))
530 (defun iswitchb-complete ()
531 "Try and complete the current pattern amongst the buffer names."
532 (interactive)
533 (let (res)
534 (cond ((not iswitchb-matches)
535 (iswitchb-completion-help)
538 ((eq 1 (length iswitchb-matches))
539 ;; only one choice, so select it.
540 (exit-minibuffer))
543 ;; else there could be some completions
545 (setq res (iswitchb-find-common-substring
546 iswitchb-matches iswitchb-text))
547 (if (and (not (memq res '(t nil)))
548 (not (equal res iswitchb-text)))
549 ;; found something to complete, so put it in the minibuff.
550 (progn
551 (setq iswitchb-rescan nil)
552 (delete-region (point-min) (point))
553 (insert res))
554 ;; else nothing to complete
555 (iswitchb-completion-help)
562 ;;; TOGGLE FUNCTIONS
564 (defun iswitchb-toggle-case ()
565 "Toggle the value of `iswitchb-case'."
566 (interactive)
567 (setq iswitchb-case (not iswitchb-case))
568 ;; ask for list to be regenerated.
569 (setq iswitchb-rescan t)
572 (defun iswitchb-toggle-regexp ()
573 "Toggle the value of `iswitchb-regexp'."
574 (interactive)
575 (setq iswitchb-regexp (not iswitchb-regexp))
576 ;; ask for list to be regenerated.
577 (setq iswitchb-rescan t)
581 (defun iswitchb-toggle-ignore ()
582 "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
583 (interactive)
584 (if iswitchb-buffer-ignore
585 (progn
586 (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
587 (setq iswitchb-buffer-ignore nil)
589 ;; else
590 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig)
592 ;; ask for list to be regenerated.
593 (setq iswitchb-rescan t)
597 (defun iswitchb-select-buffer-text ()
598 "Select the buffer named by the prompt.
599 If no buffer exactly matching the prompt exists, maybe create a new one."
600 (interactive)
601 (setq iswitchb-exit 'takeprompt)
602 (exit-minibuffer))
606 (defun iswitchb-find-file ()
607 "Drop into find-file from buffer switching."
608 (interactive)
609 (setq iswitchb-exit 'findfile)
610 (exit-minibuffer))
612 (defun iswitchb-next-match ()
613 "Put first element of `iswitchb-matches' at the end of the list."
614 (interactive)
615 (let ((next (cadr iswitchb-matches)))
616 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next))
617 (setq iswitchb-rescan t)
620 (defun iswitchb-prev-match ()
621 "Put last element of `iswitchb-matches' at the front of the list."
622 (interactive)
623 (let ((prev (car (last iswitchb-matches))))
624 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev))
625 (setq iswitchb-rescan t)
631 (defun iswitchb-chop (list elem)
632 "Remove all elements before ELEM and put them at the end of LIST."
633 (let ((ret nil)
634 (next nil)
635 (sofar nil))
636 (while (not ret)
637 (setq next (car list))
638 (if (equal next elem)
639 (setq ret (append list (nreverse sofar)))
640 ;; else
641 (progn
642 (setq list (cdr list))
643 (setq sofar (cons next sofar)))))
644 ret))
649 ;;; CREATE LIST OF ALL CURRENT BUFFERS
652 (defun iswitchb-make-buflist ()
653 "Set `iswitchb-buflist' to the current list of buffers.
654 Currently visible buffers are put at the end of the list.
655 The hook `iswitchb-make-buflist-hook' is run after the list has been
656 created to allow the user to further modify the order of the buffer names
657 in this list."
658 (setq iswitchb-buflist
659 (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
660 (buflist
661 (delq nil
662 (mapcar
663 (lambda (x)
664 (let ((b-name (buffer-name x)))
665 (if (not
666 (or
667 (iswitchb-ignore-buffername-p b-name)
668 (memq b-name iswitchb-current-buffers)))
669 b-name)))
670 (buffer-list)))))
671 (nconc buflist iswitchb-current-buffers)
672 (run-hooks 'iswitchb-make-buflist-hook)
673 buflist)))
675 (defun iswitchb-to-end (lst)
676 "Move the elements from LST to the end of BUFLIST."
677 (mapcar
678 (lambda (elem)
679 (setq buflist (delq elem buflist)))
680 lst)
681 (nconc buflist lst))
685 (defun iswitchb-get-buffers-in-frames (&optional current)
686 "Return the list of buffers that are visible in the current frame.
687 If optional argument `current' is given, restrict searching to the
688 current frame, rather than all frames, regardless of value of
689 `iswitchb-all-frames'."
690 (let ((iswitchb-bufs-in-frame nil))
691 (walk-windows 'iswitchb-get-bufname nil
692 (if current
694 iswitchb-all-frames))
695 iswitchb-bufs-in-frame))
698 (defun iswitchb-get-bufname (win)
699 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
700 (setq iswitchb-bufs-in-frame
701 (cons (buffer-name (window-buffer win))
702 iswitchb-bufs-in-frame)))
705 ;;; FIND MATCHING BUFFERS
708 (defun iswitchb-set-matches ()
709 "Set `iswitchb-matches' to the list of buffers matching prompt."
710 (if iswitchb-rescan
711 (setq iswitchb-matches
712 (let* ((buflist iswitchb-buflist)
714 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
715 buflist)))))
717 (defun iswitchb-get-matched-buffers (regexp
718 &optional string-format buffer-list)
719 "Return buffers matching REGEXP.
720 If STRING-FORMAT is non-nil, consider REGEXP as string.
721 BUFFER-LIST can be list of buffers or list of strings."
722 (let* ((case-fold-search iswitchb-case)
723 ;; need reverse since we are building up list backwards
724 (list (reverse buffer-list))
725 (do-string (stringp (car list)))
726 name
729 (mapcar
730 (lambda (x)
732 (if do-string
733 (setq name x) ;We already have the name
734 (setq name (buffer-name x)))
736 (cond
737 ((and (or (and string-format (string-match regexp name))
738 (and (null string-format)
739 (string-match (regexp-quote regexp) name)))
741 ;; todo (not (iswitchb-ignore-buffername-p name))
743 (setq ret (cons name ret))
745 list)
752 (defun iswitchb-ignore-buffername-p (bufname)
753 "Return t if the buffer BUFNAME should be ignored."
754 (let ((data (match-data))
755 (re-list iswitchb-buffer-ignore)
756 ignorep
757 nextstr
759 (while re-list
760 (setq nextstr (car re-list))
761 (cond
762 ((stringp nextstr)
763 (if (string-match nextstr bufname)
764 (progn
765 (setq ignorep t)
766 (setq re-list nil))))
767 ((fboundp nextstr)
768 (if (funcall nextstr bufname)
769 (progn
770 (setq ignorep t)
771 (setq re-list nil))
774 (setq re-list (cdr re-list)))
775 (store-match-data data)
777 ;; return the result
778 ignorep)
783 (defun iswitchb-word-matching-substring (word)
784 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
785 If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
786 (let ((case-fold-search iswitchb-case))
787 (let ((m (string-match iswitchb-change-word-sub word)))
788 (if m
789 (substring word m)
790 ;; else no match
791 nil))))
798 (defun iswitchb-find-common-substring (lis subs)
799 "Return common string following SUBS in each element of LIS."
800 (let (res
801 alist
802 iswitchb-change-word-sub
804 (setq iswitchb-change-word-sub
805 (if iswitchb-regexp
806 subs
807 (regexp-quote subs)))
808 (setq res (mapcar 'iswitchb-word-matching-substring lis))
809 (setq res (delq nil res)) ;; remove any nil elements (shouldnt happen)
810 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
812 ;; try-completion returns t if there is an exact match.
813 (let ((completion-ignore-case iswitchb-case))
815 (try-completion subs alist)
819 (defun iswitchb-makealist (res)
820 "Return dotted pair (RES . 1)."
821 (cons res 1))
823 ;; from Wayne Mesard <wmesard@esd.sgi.com>
824 (defun iswitchb-rotate-list (lis)
825 "Destructively removes the last element from LIS.
826 Return the modified list with the last element prepended to it."
827 (if (<= (length lis) 1)
829 (let ((las lis)
830 (prev lis))
831 (while (consp (cdr las))
832 (setq prev las
833 las (cdr las)))
834 (setcdr prev nil)
835 (cons (car las) lis))
839 (defun iswitchb-completion-help ()
840 "Show possible completions in a *Buffer Completions* buffer."
841 ;; we could allow this buffer to be used to select match, but I think
842 ;; choose-completion-string will need redefining, so it just inserts
843 ;; choice with out any previous input.
844 (interactive)
845 (setq iswitchb-rescan nil)
846 (let ((completion-setup-hook nil) ;disable fancy highlight/selection.
848 (with-output-to-temp-buffer "*Buffer Completions*"
849 (if iswitchb-xemacs
851 ;; XEmacs extents are put on by default, doesn't seem to be
852 ;; any way of switching them off.
853 (display-completion-list (if iswitchb-matches
854 iswitchb-matches
855 iswitchb-buflist)
856 :help-string "iswitchb "
857 :activate-callback
858 '(lambda (x y z)
859 (message "doesnt work yet, sorry!")))
860 ;; else running Emacs
861 (display-completion-list (if iswitchb-matches
862 iswitchb-matches
863 iswitchb-buflist))
864 ))))
867 ;;; KILL CURRENT BUFFER
869 (defun iswitchb-kill-buffer ()
870 "Kill the buffer at the head of `iswtichb-matches'."
871 (interactive)
872 (let ( (enable-recursive-minibuffers t)
873 buf)
875 (setq buf (car iswitchb-matches))
876 ;; check to see if buf is non-nil.
877 (if buf
878 (progn
879 (kill-buffer buf)
881 ;; Check if buffer exists. XEmacs gnuserv.el makes alias
882 ;; for kill-buffer which does not return t if buffer is
883 ;; killed, so we can't rely on kill-buffer return value.
884 (if (get-buffer buf)
885 ;; buffer couldn't be killed.
886 (setq iswitchb-rescan t)
887 ;; else buffer was killed so remove name from list.
888 (setq iswitchb-buflist (delq buf iswitchb-buflist)))))))
891 ;;; VISIT CHOSEN BUFFER
892 (defun iswitchb-visit-buffer (buffer)
893 "Visit buffer named BUFFER according to `iswitchb-method'."
894 (let* (win newframe)
895 (cond
896 ((eq iswitchb-method 'samewindow)
897 (switch-to-buffer buffer))
899 ((memq iswitchb-method '(always-frame maybe-frame))
900 (cond
901 ((and (setq win (iswitchb-window-buffer-p buffer))
902 (or (eq iswitchb-method 'always-frame)
903 (y-or-n-p "Jump to frame? ")))
904 (setq newframe (window-frame win))
905 (raise-frame newframe)
906 (select-frame newframe)
907 (select-window win)
908 (if (not iswitchb-xemacs)
909 ;; reposition mouse to make frame active. not needed in XEmacs
910 ;; This line came from the other-frame defun in Emacs.
911 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
914 ;; No buffer in other frames...
915 (switch-to-buffer buffer)
920 ((eq iswitchb-method 'otherwindow)
921 (switch-to-buffer-other-window buffer))
923 ((eq iswitchb-method 'display)
924 (display-buffer buffer))
926 ((eq iswitchb-method 'otherframe)
927 (progn
928 (switch-to-buffer-other-frame buffer)
929 (if (not iswitchb-xemacs)
930 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
932 ) )))
934 (defun iswitchb-possible-new-buffer (buf)
935 "Possibly create and visit a new buffer called BUF."
937 (let ((newbufcreated))
938 (if (and iswitchb-newbuffer
940 (not iswitchb-prompt-newbuffer)
942 (and iswitchb-prompt-newbuffer
943 (y-or-n-p
944 (format
945 "No buffer matching `%s', create one? "
946 buf)))))
947 ;; then create a new buffer
948 (progn
949 (setq newbufcreated (get-buffer-create buf))
950 (if (fboundp 'set-buffer-major-mode)
951 (set-buffer-major-mode newbufcreated))
952 (iswitchb-visit-buffer newbufcreated))
953 ;; else wont create new buffer
954 (message (format "no buffer matching `%s'" buf))
957 (defun iswitchb-window-buffer-p (buffer)
958 "Return window pointer if BUFFER is visible in another frame.
959 If BUFFER is visible in the current frame, return nil."
960 (interactive)
961 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
962 ;;If the buffer is visible in current frame, return nil
963 (if (memq buffer blist)
965 ;; maybe in other frame...
966 (get-buffer-window buffer 'visible)
969 ;;;###autoload
970 (defun iswitchb-default-keybindings ()
971 "Set up default keybindings for `iswitchb-buffer'.
972 Call this function to override the normal bindings."
973 (interactive)
974 (global-set-key (read-kbd-macro "C-x b") 'iswitchb-buffer)
975 (global-set-key (read-kbd-macro "C-x 4 b") 'iswitchb-buffer-other-window)
976 (global-set-key (read-kbd-macro "C-x 4 C-o") 'iswitchb-display-buffer)
977 (global-set-key (read-kbd-macro "C-x 5 b") 'iswitchb-buffer-other-frame))
980 ;;;###autoload
981 (defun iswitchb-buffer ()
982 "Switch to another buffer.
984 The buffer name is selected interactively by typing a substring. The
985 buffer is displayed according to `iswitchb-default-method' -- the
986 default is to show it in the same window, unless it is already visible
987 in another frame.
988 For details of keybindings, do `\\[describe-function] iswitchb'."
989 (interactive)
990 (setq iswitchb-method iswitchb-default-method)
991 (iswitchb-entry))
994 ;;;###autoload
995 (defun iswitchb-buffer-other-window ()
996 "Switch to another buffer and show it in another window.
997 The buffer name is selected interactively by typing a substring.
998 For details of keybindings, do `\\[describe-function] iswitchb'."
999 (interactive)
1000 (setq iswitchb-method 'otherwindow)
1001 (iswitchb-entry))
1005 ;;;###autoload
1006 (defun iswitchb-display-buffer ()
1007 "Display a buffer in another window but don't select it.
1008 The buffer name is selected interactively by typing a substring.
1009 For details of keybindings, do `\\[describe-function] iswitchb'."
1010 (interactive)
1011 (setq iswitchb-method 'display)
1012 (iswitchb-entry))
1016 ;;;###autoload
1017 (defun iswitchb-buffer-other-frame ()
1018 "Switch to another buffer and show it in another frame.
1019 The buffer name is selected interactively by typing a substring.
1020 For details of keybindings, do `\\[describe-function] iswitchb'."
1021 (interactive)
1022 (setq iswitchb-method 'otherframe)
1023 (iswitchb-entry))
1027 (defun iswitchb-entry ()
1028 "Simply fall into `iswitchb' -- the main function."
1029 (iswitchb))
1035 ;;; XEmacs hack for showing default buffer
1037 ;; The first time we enter the minibuffer, Emacs puts up the default
1038 ;; buffer to switch to, but XEmacs doesnt -- presumably there is a
1039 ;; subtle difference in the two versions of post-command-hook. The
1040 ;; default is shown for both whenever we delete all of our text
1041 ;; though, indicating its just a problem the first time we enter the
1042 ;; function. To solve this, we use another entry hook for emacs to
1043 ;; show the default the first time we enter the minibuffer.
1045 (defun iswitchb-init-Xemacs-trick ()
1046 "Display default buffer when first entering minibuffer.
1047 This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
1048 (if (iswitchb-entryfn-p)
1049 (progn
1050 (iswitchb-exhibit)
1051 (goto-char (point-min)))))
1054 ;; add this hook for XEmacs only.
1055 (if iswitchb-xemacs
1056 (add-hook 'iswitchb-minibuffer-setup-hook
1057 'iswitchb-init-Xemacs-trick))
1060 ;;; XEmacs / backspace key
1061 ;; For some reason, if the backspace key is pressed in xemacs, the
1062 ;; line gets confused, so I've added a simple key definition to make
1063 ;; backspace act like the normal delete key.
1065 (defun iswitchb-xemacs-backspacekey ()
1066 "Bind backspace to `backward-delete-char'."
1067 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
1068 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word)
1072 (if iswitchb-xemacs
1073 (add-hook 'iswitchb-define-mode-map-hook
1074 'iswitchb-xemacs-backspacekey))
1078 ;;; ICOMPLETE TYPE CODE
1080 (defun iswitchb-exhibit ()
1081 "Find matching buffers and display a list in the minibuffer.
1082 Copied from `icomplete-exhibit' with two changes:
1083 1. It prints a default buffer name when there is no text yet entered.
1084 2. It calls my completion routine rather than the standard completion."
1086 (if iswitchb-use-mycompletion
1087 (let ((contents (buffer-substring (point-min)(point-max)))
1088 (buffer-undo-list t))
1089 (save-excursion
1090 (goto-char (point-max))
1091 ; Register the end of input, so we
1092 ; know where the extra stuff
1093 ; (match-status info) begins:
1094 (if (not (boundp 'iswitchb-eoinput))
1095 ;; In case it got wiped out by major mode business:
1096 (make-local-variable 'iswitchb-eoinput))
1097 (setq iswitchb-eoinput (point))
1098 ;; Update the list of matches
1099 (setq iswitchb-text contents)
1100 (iswitchb-set-matches)
1101 (setq iswitchb-rescan t)
1102 (iswitchb-set-common-completion)
1104 ;; Insert the match-status information:
1105 (insert-string
1106 (iswitchb-completions
1107 contents
1108 minibuffer-completion-table
1109 minibuffer-completion-predicate
1110 (not minibuffer-completion-confirm)))
1111 ))))
1115 (defun iswitchb-completions
1116 (name candidates predicate require-match)
1117 "Return the string that is displayed after the user's text.
1118 Modified from `icomplete-completions'."
1120 (let ((comps iswitchb-matches)
1121 ; "-determined" - only one candidate
1122 (open-bracket-determined (if require-match "(" "["))
1123 (close-bracket-determined (if require-match ")" "]"))
1124 ;"-prospects" - more than one candidate
1125 (open-bracket-prospects "{")
1126 (close-bracket-prospects "}")
1127 first
1130 (if (and iswitchb-use-fonts comps)
1131 (progn
1132 (setq first (car comps))
1133 (setq first (format "%s" first))
1134 (put-text-property 0 (length first) 'face
1135 (if (eq (length comps) 1)
1136 'font-lock-comment-face
1137 'font-lock-function-name-face)
1138 first)
1139 (setq comps (cons first (cdr comps)))
1142 (cond ((null comps) (format " %sNo match%s"
1143 open-bracket-determined
1144 close-bracket-determined))
1146 ((null (cdr comps)) ;one match
1147 (concat (if (and (> (length (car comps))
1148 (length name)))
1149 (concat open-bracket-determined
1150 ;; when there is one match, show the
1151 ;; matching buffer name in full
1152 (car comps)
1153 close-bracket-determined)
1155 (if (not iswitchb-use-fonts) " [Matched]")
1157 (t ;multiple matches
1158 (let* (
1159 ;;(most (try-completion name candidates predicate))
1160 (most nil)
1161 (most-len (length most))
1162 most-is-exact
1163 first
1164 (alternatives
1165 (apply
1166 (function concat)
1167 (cdr (apply
1168 (function nconc)
1169 (mapcar '(lambda (com)
1170 (if (= (length com) most-len)
1171 ;; Most is one exact match,
1172 ;; note that and leave out
1173 ;; for later indication:
1174 (progn
1175 (setq most-is-exact t)
1177 (list ","
1178 (substring com
1179 most-len))))
1180 comps))))))
1182 (concat
1184 ;; put in common completion item -- what you get by
1185 ;; pressing tab
1186 (if (> (length iswitchb-common-match-string) (length name))
1187 (concat open-bracket-determined
1188 (substring iswitchb-common-match-string
1189 (length name))
1190 close-bracket-determined)
1192 ;; end of partial matches...
1194 ;; think this bit can be ignored.
1195 (and (> most-len (length name))
1196 (concat open-bracket-determined
1197 (substring most (length name))
1198 close-bracket-determined))
1200 ;; list all alternatives
1201 open-bracket-prospects
1202 (if most-is-exact
1203 (concat "," alternatives)
1204 alternatives)
1205 close-bracket-prospects)))
1208 (defun iswitchb-minibuffer-setup ()
1209 "Set up minibuffer for `iswitchb-buffer'.
1210 Copied from `icomplete-minibuffer-setup-hook'."
1211 (if (iswitchb-entryfn-p)
1212 (progn
1214 (make-local-variable 'iswitchb-use-mycompletion)
1215 (setq iswitchb-use-mycompletion t)
1216 (make-local-hook 'pre-command-hook)
1217 (add-hook 'pre-command-hook
1218 'iswitchb-pre-command
1219 nil t)
1220 (make-local-hook 'post-command-hook)
1221 (add-hook 'post-command-hook
1222 'iswitchb-post-command
1223 nil t)
1225 (run-hooks 'iswitchb-minibuffer-setup-hook)
1230 (defun iswitchb-pre-command ()
1231 "Run before command in `iswitchb-buffer'."
1232 (iswitchb-tidy))
1235 (defun iswitchb-post-command ()
1236 "Run after command in `iswitchb-buffer'."
1237 (iswitchb-exhibit)
1242 (defun iswitchb-tidy ()
1243 "Remove completions display, if any, prior to new user input.
1244 Copied from `icomplete-tidy'."
1246 (if (and (boundp 'iswitchb-eoinput)
1247 iswitchb-eoinput)
1249 (if (> iswitchb-eoinput (point-max))
1250 ;; Oops, got rug pulled out from under us - reinit:
1251 (setq iswitchb-eoinput (point-max))
1252 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1253 (delete-region iswitchb-eoinput (point-max))))
1255 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1256 (make-local-variable 'iswitchb-eoinput)
1257 (setq iswitchb-eoinput 1)))
1260 (defun iswitchb-entryfn-p ()
1261 "Return non-nil if `this-command' shows we are using `iswitchb-buffer'."
1262 (and (symbolp this-command) ; ignore lambda functions
1263 (memq this-command
1264 '(iswitchb-buffer
1265 iswitchb-buffer-other-frame
1266 iswitchb-display-buffer
1267 iswitchb-buffer-other-window))))
1272 (defun iswitchb-summaries-to-end ()
1273 "Move the summaries to the end of the list.
1274 This is an example function which can be hooked on to
1275 `iswitchb-make-buflist-hook'. Any buffer matching the regexps
1276 `Summary' or `output\*$'are put to the end of the list."
1277 (let ((summaries (delq nil (mapcar
1278 (lambda (x)
1279 (if (or
1280 (string-match "Summary" x)
1281 (string-match "output\\*$" x))
1283 buflist)
1286 (iswitchb-to-end summaries)))
1290 ;;; HOOKS
1291 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1293 (provide 'iswitchb)
1295 ;;; iswitchb.el ends here