*** empty log message ***
[emacs.git] / lisp / iswitchb.el
blob233997285c974d5b20f04a8c43dde83e77a89bc5
1 ;;; iswitchb.el --- switch between buffers using substrings
3 ;; Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Stephen Eglen <stephen@gnu.org>
7 ;; Maintainer: Stephen Eglen <stephen@gnu.org>
8 ;; Keywords: completion convenience
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 3, 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Installation:
30 ;; To get the functions in this package bound to keys, use
31 ;; M-x iswitchb-mode or customize the option `iswitchb-mode'.
32 ;; Alternatively, add the following line to your .emacs:
33 ;; (iswitchb-mode 1)
35 ;; As you type in a substring, the list of buffers currently matching
36 ;; the substring is displayed as you type. The list is ordered so
37 ;; that the most recent buffers visited come at the start of the list.
38 ;; The buffer at the start of the list will be the one visited when
39 ;; you press return. By typing more of the substring, the list is
40 ;; narrowed down so that gradually the buffer you want will be at the
41 ;; top of the list. Alternatively, you can use C-s and C-r to rotate
42 ;; buffer names in the list until the one you want is at the top of
43 ;; the list. Completion is also available so that you can see what is
44 ;; common to all of the matching buffers as you type.
46 ;; This code is similar to a couple of other packages. Michael R Cook
47 ;; <cook@sightpath.com> wrote a similar buffer switching package, but
48 ;; does exact matching rather than substring matching on buffer names.
49 ;; I also modified a couple of functions from icomplete.el to provide
50 ;; the completion feedback in the minibuffer.
52 ;;; Example
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
58 ;; iswitch {123456,123}
60 ;; If I then press 2:
61 ;; iswitch 2[3]{123456,123}
63 ;; The list in {} are the matching buffers, most recent first (buffers
64 ;; visible in the current frame are put at the end of the list by
65 ;; default). At any time I can select the item at the head of the
66 ;; list by pressing RET. I can also put the first element at the end
67 ;; of the list by pressing C-s, or put the last element at the head of
68 ;; the list by pressing C-r. The item in [] indicates what can be
69 ;; added to my input by pressing TAB. In this case, I will get "3"
70 ;; added to my input. So, press TAB:
71 ;; iswitch 23{123456,123}
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.
78 ;; However, if I type 4, I only have one match left:
79 ;; iswitch 234[123456] [Matched]
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.
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".
90 ;; Of course, where this function comes in really useful is when you
91 ;; can specify the buffer using only a few keystrokes. In the above
92 ;; example, the quickest way to get to the "123456" buffer would be
93 ;; just to type 4 and then RET (assuming there isn't any newer buffer
94 ;; with 4 in its name).
96 ;; To see a full list of all matching buffers in a separate buffer,
97 ;; hit ? or press TAB when there are no further completions to the
98 ;; substring. Repeated TAB presses will scroll you through this
99 ;; separate buffer.
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.
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.
108 ;; See the doc string of iswitchb for full keybindings and features.
109 ;; (describe-function 'iswitchb)
111 ;; Case matching: The case of strings when matching can be ignored or
112 ;; used depending on the value of iswitchb-case (default is the same
113 ;; as case-fold-search, normally t). Imagine you have the following
114 ;; buffers:
116 ;; INBOX *info* *scratch*
118 ;; Then these will be the matching buffers, depending on how you type
119 ;; the two letters `in' and the value of iswitchb-case:
121 ;; iswitchb-case user input | matching buffers
122 ;; ----------------------------------------------
123 ;; nil in | *info*
124 ;; t in | INBOX, *info*
125 ;; t IN | INBOX
126 ;; t In | [No match]
128 ;;; Customisation
130 ;; See the User Variables section below for easy ways to change the
131 ;; functionality of the program. These are accessible using the
132 ;; custom package.
133 ;; To modify the keybindings, use something like:
135 ;;(add-hook 'iswitchb-mode-hook 'iswitchb-my-keys)
136 ;;(defun iswitchb-my-keys ()
137 ;; "Add my keybindings for iswitchb."
138 ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match))
140 ;; Seeing all the matching buffers
142 ;; If you have many matching buffers, they may not all fit onto one
143 ;; line of the minibuffer. In Emacs 21, the variable
144 ;; `resize-mini-windows' controls how many lines of the minibuffer can
145 ;; be seen. For older versions of emacs, you can use
146 ;; `resize-minibuffer-mode'. You can also limit iswitchb so that it
147 ;; only shows a certain number of lines -- see the documentation for
148 ;; `iswitchb-minibuffer-setup-hook'.
150 ;; Changing the list of buffers
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:
157 ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
159 ;; then all buffers matching "Summary" are moved to the end of the
160 ;; list. (I find this handy for keeping the INBOX Summary and so on
161 ;; out of the way.) It also moves buffers matching "output\*$" to the
162 ;; end of the list (these are created by AUCTeX when compiling.)
163 ;; Other functions could be made available which alter the list of
164 ;; matching buffers (either deleting or rearranging elements.)
166 ;; Font-Lock
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)
173 ;; Replacement for read-buffer
175 ;; iswitchb-read-buffer has been written to be a drop in replacement
176 ;; for the normal buffer selection routine `read-buffer'. To use
177 ;; iswitch for all buffer selections in Emacs, add:
178 ;; (setq read-buffer-function 'iswitchb-read-buffer)
179 ;; (This variable was introduced in Emacs 20.3.)
180 ;; XEmacs users can get the same behaviour by doing:
181 ;; (defalias 'read-buffer 'iswitchb-read-buffer)
182 ;; since `read-buffer' is defined in lisp.
184 ;; Using iswitchb for other completion tasks.
186 ;; Kin Cho (kin@neoscale.com) sent the following suggestion to use
187 ;; iswitchb for other completion tasks.
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)))
198 ;; example:
199 ;; (my-icompleting-read "Which fruit? " '
200 ;; ("apple" "pineapple" "pear" "bananas" "oranges") )
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))
212 ;; (add-hook 'iswitchb-define-mode-map-hook
213 ;; '(lambda () (define-key
214 ;; iswitchb-mode-map "\C-o"
215 ;; 'iswitchb-exclude-nonmatching)))
217 ;; Other lisp packages extend iswitchb behaviour to other tasks. See
218 ;; ido.el (by Kim Storm) and mcomplete.el (Yuji Minejima).
220 ;; Window managers: Switching frames/focus follows mouse; Sawfish.
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.
227 ;; iswitch functionality has also been implemented for switching
228 ;; between windows in the Sawfish window manager.
230 ;; Regexp matching
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.
237 ;;; TODO
239 ;;; Acknowledgements
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.
245 ;;; Code:
247 ;; CL needed for cadr and last
248 (if (not (and (fboundp 'cadr)
249 (fboundp 'last)))
250 (require 'cl))
252 (require 'font-lock)
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)
265 (defmacro defcustom (var value doc &rest args)
266 `(defvar ,var ,value ,doc))))
268 ;;; User Variables
270 ;; These are some things you might want to change.
272 (defgroup iswitchb nil
273 "Switch between buffers using substrings."
274 :group 'convenience
275 :group 'completion
276 :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el")
277 :link '(url-link "http://www.anc.ed.ac.uk/~stephen/emacs/")
278 :link '(emacs-library-link :tag "Lisp File" "iswitchb.el"))
280 (defcustom iswitchb-case case-fold-search
281 "*Non-nil if searching of buffer names should ignore case.
282 If this is non-nil but the user input has any upper case letters, matching
283 is temporarily case sensitive."
284 :type 'boolean
285 :group 'iswitchb)
287 (defcustom iswitchb-buffer-ignore
288 '("^ ")
289 "*List of regexps or functions matching buffer names to ignore.
290 For example, traditional behavior is not to list buffers whose names begin
291 with a space, for which the regexp is `^ '. See the source file for
292 example functions that filter buffernames."
293 :type '(repeat (choice regexp function))
294 :group 'iswitchb)
295 (put 'iswitchb-buffer-ignore 'risky-local-variable t)
297 (defcustom iswitchb-max-to-show nil
298 "*If non-nil, limit the number of names shown in the minibuffer.
299 If this value is N, and N is greater than the number of matching
300 buffers, the first N/2 and the last N/2 matching buffers are
301 shown. This can greatly speed up iswitchb if you have a
302 multitude of buffers open."
303 :type '(choice (const :tag "Show all" nil) integer)
304 :group 'iswitchb)
306 (defcustom iswitchb-use-virtual-buffers nil
307 "*If non-nil, refer to past buffers when none match.
308 This feature relies upon the `recentf' package, which will be
309 enabled if this variable is configured to a non-nil value."
310 :type 'boolean
311 :require 'recentf
312 :set (function
313 (lambda (sym value)
314 (if value (recentf-mode 1))
315 (set sym value)))
316 :group 'iswitchb)
318 (defvar iswitchb-virtual-buffers nil)
320 (defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help
321 "*Hook run when `iswitchb-complete' can't complete any more.
322 The most useful values are `iswitchb-completion-help', which pops up a
323 window with completion alternatives, or `iswitchb-next-match' or
324 `iswitchb-prev-match', which cycle the buffer list."
325 :type 'hook
326 :group 'iswitchb)
328 ;;; Examples for setting the value of iswitchb-buffer-ignore
329 ;(defun iswitchb-ignore-c-mode (name)
330 ; "Ignore all c mode buffers -- example function for iswitchb."
331 ; (save-excursion
332 ; (set-buffer name)
333 ; (string-match "^C$" mode-name)))
335 ;(setq iswitchb-buffer-ignore '("^ " iswitchb-ignore-c-mode))
336 ;(setq iswitchb-buffer-ignore '("^ " "\\.c$" "\\.h$"))
338 (defcustom iswitchb-default-method 'always-frame
339 "*How to switch to new buffer when using `iswitchb-buffer'.
340 Possible values:
341 `samewindow' Show new buffer in same window
342 `otherwindow' Show new buffer in another window (same frame)
343 `display' Display buffer in another window without switching to it
344 `otherframe' Show new buffer in another frame
345 `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
346 you want to see the buffer in the same window of the current
347 frame or in the other frame.
348 `always-frame' If a buffer is visible in another frame, raise that
349 frame. Otherwise, visit the buffer in the same window."
350 :type '(choice (const samewindow)
351 (const otherwindow)
352 (const display)
353 (const otherframe)
354 (const maybe-frame)
355 (const always-frame))
356 :group 'iswitchb)
358 (defcustom iswitchb-regexp nil
359 "*Non-nil means that `iswitchb' will do regexp matching.
360 Value can be toggled within `iswitchb' using `iswitchb-toggle-regexp'."
361 :type 'boolean
362 :group 'iswitchb)
364 (defcustom iswitchb-newbuffer t
365 "*Non-nil means create new buffer if no buffer matches substring.
366 See also `iswitchb-prompt-newbuffer'."
367 :type 'boolean
368 :group 'iswitchb)
370 (defcustom iswitchb-prompt-newbuffer t
371 "*Non-nil means prompt user to confirm before creating new buffer.
372 See also `iswitchb-newbuffer'."
373 :type 'boolean
374 :group 'iswitchb)
376 (defcustom iswitchb-use-faces t
377 "*Non-nil means use font-lock fonts for showing first match."
378 :type 'boolean
379 :group 'iswitchb)
380 (define-obsolete-variable-alias 'iswitchb-use-fonts 'iswitchb-use-faces "22.1")
382 (defcustom iswitchb-use-frame-buffer-list nil
383 "*Non-nil means use the currently selected frame's buffer list."
384 :type 'boolean
385 :group 'iswitchb)
387 (defcustom iswitchb-make-buflist-hook nil
388 "Hook to run when list of matching buffers is created."
389 :type 'hook
390 :group 'iswitchb)
392 (defvar iswitchb-all-frames 'visible
393 "*Argument to pass to `walk-windows' when finding visible buffers.
394 See documentation of `walk-windows' for useful values.")
396 (defcustom iswitchb-minibuffer-setup-hook nil
397 "Iswitchb-specific customization of minibuffer setup.
399 This hook is run during minibuffer setup if `iswitchb' is active.
400 For instance:
401 \(add-hook 'iswitchb-minibuffer-setup-hook
402 '\(lambda () (set (make-local-variable 'max-mini-window-height) 3)))
403 will constrain the minibuffer to a maximum height of 3 lines when
404 iswitchb is running."
405 :type 'hook
406 :group 'iswitchb)
408 (defface iswitchb-single-match
409 '((t
410 (:inherit font-lock-comment-face)))
411 "Iswitchb face for single matching buffer name."
412 :version "22.1"
413 :group 'iswitchb)
415 (defface iswitchb-current-match
416 '((t
417 (:inherit font-lock-function-name-face)))
418 "Iswitchb face for current matching buffer name."
419 :version "22.1"
420 :group 'iswitchb)
422 (defface iswitchb-virtual-matches
423 '((t
424 (:inherit font-lock-builtin-face)))
425 "Iswitchb face for matching virtual buffer names.
426 See also `iswitchb-use-virtual-buffers'."
427 :version "22.1"
428 :group 'iswitchb)
430 (defface iswitchb-invalid-regexp
431 '((t
432 (:inherit font-lock-warning-face)))
433 "Iswitchb face for indicating invalid regexp. "
434 :version "22.1"
435 :group 'iswitchb)
437 ;; Do we need the variable iswitchb-use-mycompletion?
439 ;;; Internal Variables
441 (defvar iswitchb-method nil
442 "Stores the method for viewing the selected buffer.
443 Its value is one of `samewindow', `otherwindow', `display', `otherframe',
444 `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
445 details of values.")
447 (defvar iswitchb-eoinput 1
448 "Point where minibuffer input ends and completion info begins.
449 Copied from `icomplete-eoinput'.")
450 (make-variable-buffer-local 'iswitchb-eoinput)
452 (defvar iswitchb-buflist nil
453 "Stores the current list of buffers that will be searched through.
454 The list is ordered, so that the most recent buffers come first,
455 although by default, the buffers visible in the current frame are put
456 at the end of the list. Created by `iswitchb-make-buflist'.")
458 ;; todo -- is this necessary?
460 (defvar iswitchb-use-mycompletion nil
461 "Non-nil means use `iswitchb-buffer' completion feedback.
462 Should only be set to t by iswitchb functions, so that it doesn't
463 interfere with other minibuffer usage.")
465 (defvar iswitchb-change-word-sub nil
466 "Private variable used by `iswitchb-word-matching-substring'.")
468 (defvar iswitchb-common-match-string nil
469 "Stores the string that is common to all matching buffers.")
471 (defvar iswitchb-rescan nil
472 "Non-nil means we need to regenerate the list of matching buffers.")
474 (defvar iswitchb-text nil
475 "Stores the users string as it is typed in.")
477 (defvar iswitchb-matches nil
478 "List of buffers currently matching `iswitchb-text'.")
480 (defvar iswitchb-mode-map
481 (let ((map (make-sparse-keymap)))
482 (set-keymap-parent map minibuffer-local-map)
483 (define-key map "?" 'iswitchb-completion-help)
484 (define-key map "\C-s" 'iswitchb-next-match)
485 (define-key map "\C-r" 'iswitchb-prev-match)
486 (define-key map "\t" 'iswitchb-complete)
487 (define-key map "\C-j" 'iswitchb-select-buffer-text)
488 (define-key map "\C-t" 'iswitchb-toggle-regexp)
489 (define-key map "\C-x\C-f" 'iswitchb-find-file)
490 (define-key map "\C-c" 'iswitchb-toggle-case)
491 (define-key map "\C-k" 'iswitchb-kill-buffer)
492 (define-key map "\C-m" 'iswitchb-exit-minibuffer)
493 map)
494 "Minibuffer keymap for `iswitchb-buffer'.")
496 (defvar iswitchb-global-map
497 (let ((map (make-sparse-keymap)))
498 (dolist (b '((switch-to-buffer . iswitchb-buffer)
499 (switch-to-buffer-other-window . iswitchb-buffer-other-window)
500 (switch-to-buffer-other-frame . iswitchb-buffer-other-frame)
501 (display-buffer . iswitchb-display-buffer)))
502 (if (fboundp 'command-remapping)
503 (define-key map (vector 'remap (car b)) (cdr b))
504 (substitute-key-definition (car b) (cdr b) map global-map)))
505 map)
506 "Global keymap for `iswitchb-mode'.")
508 (defvar iswitchb-history nil
509 "History of buffers selected using `iswitchb-buffer'.")
511 (defvar iswitchb-exit nil
512 "Flag to monitor how `iswitchb-buffer' exits.
513 If equal to `takeprompt', we use the prompt as the buffer name to be
514 selected.")
516 (defvar iswitchb-buffer-ignore-orig nil
517 "Stores original value of `iswitchb-buffer-ignore'.")
519 (defvar iswitchb-default nil
520 "Default buffer for iswitchb.")
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.")
526 (defvar iswitchb-temp-buflist nil
527 "Stores a temporary version of the buffer list being created.")
529 (defvar iswitchb-bufs-in-frame nil
530 "List of the buffers visible in the current frame.")
532 (defvar iswitchb-minibuf-depth nil
533 "Value we expect to be returned by `minibuffer-depth' in the minibuffer.")
535 (defvar iswitchb-common-match-inserted nil
536 "Non-nil if we have just inserted a common match in the minibuffer.")
538 (defvar iswitchb-invalid-regexp)
540 ;;; FUNCTIONS
542 ;;; ISWITCHB KEYMAP
543 (defun iswitchb-define-mode-map ()
544 "Set up the keymap for `iswitchb-buffer'.
545 This is obsolete. Use \\[iswitchb-mode] or customize the
546 variable `iswitchb-mode'."
547 (interactive)
548 (let (map)
549 ;; generated every time so that it can inherit new functions.
550 ;;(or iswitchb-mode-map
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)
559 (define-key map "\C-x\C-f" 'iswitchb-find-file)
560 (define-key map "\C-n" 'iswitchb-toggle-ignore)
561 (define-key map "\C-c" 'iswitchb-toggle-case)
562 (define-key map "\C-k" 'iswitchb-kill-buffer)
563 (define-key map "\C-m" 'iswitchb-exit-minibuffer)
564 (setq iswitchb-mode-map map)
565 (run-hooks 'iswitchb-define-mode-map-hook)))
567 ;;; MAIN FUNCTION
568 (defun iswitchb ()
569 "Switch to buffer matching a substring.
570 As you type in a string, all of the buffers matching the string are
571 displayed. When you have found the buffer you want, it can then be
572 selected. As you type, most keys have their normal keybindings,
573 except for the following:
574 \\<iswitchb-mode-map>
576 RET Select the buffer at the front of the list of matches. If the
577 list is empty, possibly prompt to create new buffer.
579 \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
580 If no buffer is found, prompt for a new one.
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.
584 \\[iswitchb-complete] Complete a common suffix to the current string that
585 matches all buffers. If there is only one match, select that buffer.
586 If there is no common suffix, show a list of all matching buffers
587 in a separate window.
588 \\[iswitchb-toggle-regexp] Toggle regexp searching.
589 \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
590 \\[iswitchb-completion-help] Show list of matching buffers in separate window.
591 \\[iswitchb-find-file] Exit iswitchb and drop into `find-file'.
592 \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
593 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
594 ;;`iswitchb-buffer-ignore')
596 (let* ((prompt "iswitch ")
597 iswitchb-invalid-regexp
598 (buf (iswitchb-read-buffer prompt)))
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
604 (cond ( (eq iswitchb-exit 'findfile)
605 (call-interactively 'find-file))
606 (iswitchb-invalid-regexp
607 (message "Won't make invalid regexp named buffer"))
609 ;; View the buffer
610 ;;(message "go to buf %s" buf)
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)))
618 ))))
620 (defun iswitchb-read-buffer (prompt &optional default require-match
621 start matches-set)
622 "Replacement for the built-in `read-buffer'.
623 Return the name of a buffer selected.
624 PROMPT is the prompt to give to the user.
625 DEFAULT if given is the default buffer to be selected, which will
626 go to the front of the list.
627 If REQUIRE-MATCH is non-nil, an existing-buffer must be selected.
628 If START is a string, the selection process is started with that
629 string.
630 If MATCHES-SET is non-nil, the buflist is not updated before
631 the selection process begins. Used by isearchb.el."
632 (let
634 buf-sel
635 iswitchb-final-text
636 (icomplete-mode nil) ;; prevent icomplete starting up
639 (iswitchb-define-mode-map)
640 (setq iswitchb-exit nil)
641 (setq iswitchb-default
642 (if (bufferp default)
643 (buffer-name default)
644 default))
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))
650 (let
651 ((minibuffer-local-completion-map iswitchb-mode-map)
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)))
655 (iswitchb-require-match require-match))
656 ;; prompt the user for the buffer name
657 (setq iswitchb-final-text (completing-read
658 prompt ;the prompt
659 '(("dummy" . 1)) ;table
660 nil ;predicate
661 nil ;require-match [handled elsewhere]
662 start ;initial-contents
663 'iswitchb-history)))
664 (if (and (not (eq iswitchb-exit 'usefirst))
665 (get-buffer iswitchb-final-text))
666 ;; This happens for example if the buffer was chosen with the mouse.
667 (setq iswitchb-matches (list iswitchb-final-text)
668 iswitchb-virtual-buffers nil))
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)))
679 ;; Handling the require-match must be done in a better way.
680 (if (and require-match
681 (not (iswitchb-existing-buffer-p)))
682 (error "Must specify valid buffer"))
684 (if (or (eq iswitchb-exit 'takeprompt)
685 (null iswitchb-matches))
686 (setq buf-sel iswitchb-final-text)
687 ;; else take head of list
688 (setq buf-sel (car iswitchb-matches)))
690 ;; Or possibly choose the default buffer
691 (if (equal iswitchb-final-text "")
692 (setq buf-sel (car iswitchb-matches)))
694 buf-sel))
696 (defun iswitchb-existing-buffer-p ()
697 "Return non-nil if there is a matching buffer."
698 (not (null iswitchb-matches)))
700 ;;; COMPLETION CODE
702 (defun iswitchb-set-common-completion ()
703 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
704 The result is stored in `iswitchb-common-match-string'."
706 (let* (val)
707 (setq iswitchb-common-match-string nil)
708 (if (and iswitchb-matches
709 (not iswitchb-regexp) ;; testing
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)))
715 val))
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)
722 (run-hooks 'iswitchb-cannot-complete-hook))
723 (iswitchb-invalid-regexp
724 ;; Do nothing
726 ((= 1 (length iswitchb-matches))
727 ;; only one choice, so select it.
728 (exit-minibuffer))
731 ;; else there could be some completions
732 (setq res iswitchb-common-match-string)
733 (if (and (not (memq res '(t nil)))
734 (not (equal res iswitchb-text)))
735 ;; found something to complete, so put it in the minibuffer.
736 (progn
737 (setq iswitchb-rescan nil
738 iswitchb-common-match-inserted t)
739 (delete-region (minibuffer-prompt-end) (point))
740 (insert res))
741 ;; else nothing to complete
742 (run-hooks 'iswitchb-cannot-complete-hook)
743 )))))
745 ;;; TOGGLE FUNCTIONS
747 (defun iswitchb-toggle-case ()
748 "Toggle the value of variable `iswitchb-case'."
749 (interactive)
750 (setq iswitchb-case (not iswitchb-case))
751 ;; ask for list to be regenerated.
752 (setq iswitchb-rescan t))
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.
759 (setq iswitchb-rescan t))
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)
767 (setq iswitchb-buffer-ignore nil))
768 ;; else
769 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig))
770 (iswitchb-make-buflist iswitchb-default)
771 ;; ask for list to be regenerated.
772 (setq iswitchb-rescan t))
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))
779 (progn
780 (setq iswitchb-exit 'usefirst)
781 (throw 'exit nil))))
783 (defun iswitchb-select-buffer-text ()
784 "Select the buffer named by the prompt.
785 If no buffer exactly matching the prompt exists, maybe create a new one."
786 (interactive)
787 (setq iswitchb-exit 'takeprompt)
788 (exit-minibuffer))
790 (defun iswitchb-find-file ()
791 "Drop into `find-file' from buffer switching."
792 (interactive)
793 (setq iswitchb-exit 'findfile)
794 (exit-minibuffer))
796 (eval-when-compile
797 (defvar recentf-list))
799 (defun iswitchb-next-match ()
800 "Put first element of `iswitchb-matches' at the end of the list."
801 (interactive)
802 (let ((next (cadr iswitchb-matches)))
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)))
808 (setq iswitchb-rescan t)))
810 (defun iswitchb-prev-match ()
811 "Put last element of `iswitchb-matches' at the front of the list."
812 (interactive)
813 (let ((prev (car (last iswitchb-matches))))
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)))
819 (setq iswitchb-rescan t)))
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))
836 ;;; CREATE LIST OF ALL CURRENT BUFFERS
838 (defun iswitchb-make-buflist (default)
839 "Set `iswitchb-buflist' to the current list of buffers.
840 Currently visible buffers are put at the end of the list.
841 The hook `iswitchb-make-buflist-hook' is run after the list has been
842 created to allow the user to further modify the order of the buffer names
843 in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
844 it is put to the start of the list."
845 (setq iswitchb-buflist
846 (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
847 (iswitchb-temp-buflist
848 (delq nil
849 (mapcar
850 (lambda (x)
851 (let ((b-name (buffer-name x)))
852 (if (not
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))
861 (run-hooks 'iswitchb-make-buflist-hook)
862 ;; Should this be after the hooks, or should the hooks be the
863 ;; final thing to be run?
864 (if default
865 (progn
866 (setq iswitchb-temp-buflist
867 (delete default iswitchb-temp-buflist))
868 (setq iswitchb-temp-buflist
869 (cons default iswitchb-temp-buflist))))
870 iswitchb-temp-buflist)))
872 (defun iswitchb-to-end (lst)
873 "Move the elements from LST to the end of `iswitchb-temp-buflist'."
874 (dolist (elem lst)
875 (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist)))
876 (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst)))
878 (defun iswitchb-get-buffers-in-frames (&optional current)
879 "Return the list of buffers that are visible in the current frame.
880 If optional argument CURRENT is given, restrict searching to the
881 current frame, rather than all frames, regardless of value of
882 `iswitchb-all-frames'."
883 (let ((iswitchb-bufs-in-frame nil))
884 (walk-windows 'iswitchb-get-bufname nil
885 (if current
887 iswitchb-all-frames))
888 iswitchb-bufs-in-frame))
890 (defun iswitchb-get-bufname (win)
891 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
892 (let ((buf (buffer-name (window-buffer win))))
893 (if (not (member buf iswitchb-bufs-in-frame))
894 ;; Only add buf if it is not already in list.
895 ;; This prevents same buf in two different windows being
896 ;; put into the list twice.
897 (setq iswitchb-bufs-in-frame
898 (cons buf iswitchb-bufs-in-frame)))))
900 ;;; FIND MATCHING BUFFERS
902 (defun iswitchb-set-matches ()
903 "Set `iswitchb-matches' to the list of buffers matching prompt."
904 (if iswitchb-rescan
905 (setq iswitchb-matches
906 (let* ((buflist iswitchb-buflist))
907 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
908 buflist))
909 iswitchb-virtual-buffers nil)))
911 (defun iswitchb-get-matched-buffers (regexp
912 &optional string-format buffer-list)
913 "Return buffers matching REGEXP.
914 If STRING-FORMAT is nil, consider REGEXP as just a string.
915 BUFFER-LIST can be list of buffers or list of strings."
916 (let* ((case-fold-search (iswitchb-case))
917 name ret)
918 (if (null string-format) (setq regexp (regexp-quote regexp)))
919 (setq iswitchb-invalid-regexp nil)
920 (condition-case error
921 (dolist (x buffer-list (nreverse ret))
922 (setq name (if (stringp x) x (buffer-name x)))
923 (when (and (string-match regexp name)
924 (not (iswitchb-ignore-buffername-p name)))
925 (push name ret)))
926 (invalid-regexp
927 (setq iswitchb-invalid-regexp t)
928 (cdr error)))))
930 (defun iswitchb-ignore-buffername-p (bufname)
931 "Return t if the buffer BUFNAME should be ignored."
932 (let ((data (match-data))
933 (re-list iswitchb-buffer-ignore)
934 ignorep
935 nextstr)
936 (while re-list
937 (setq nextstr (car re-list))
938 (cond
939 ((stringp nextstr)
940 (if (string-match nextstr bufname)
941 (progn
942 (setq ignorep t)
943 (setq re-list nil))))
944 ((functionp nextstr)
945 (if (funcall nextstr bufname)
946 (progn
947 (setq ignorep t)
948 (setq re-list nil)))))
949 (setq re-list (cdr re-list)))
950 (set-match-data data)
952 ;; return the result
953 ignorep))
955 (defun iswitchb-word-matching-substring (word)
956 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
957 If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
958 (let ((case-fold-search (iswitchb-case)))
959 (let ((m (string-match iswitchb-change-word-sub word)))
960 (if m
961 (substring word m)
962 ;; else no match
963 nil))))
965 (defun iswitchb-find-common-substring (lis subs)
966 "Return common string following SUBS in each element of LIS."
967 (let (res
968 alist
969 iswitchb-change-word-sub)
970 (setq iswitchb-change-word-sub
971 (if iswitchb-regexp
972 subs
973 (regexp-quote subs)))
974 (setq res (mapcar 'iswitchb-word-matching-substring lis))
975 (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
976 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
978 ;; try-completion returns t if there is an exact match.
979 (let ((completion-ignore-case (iswitchb-case)))
981 (try-completion subs alist))))
983 (defun iswitchb-makealist (res)
984 "Return dotted pair (RES . 1)."
985 (cons res 1))
987 ;; from Wayne Mesard <wmesard@esd.sgi.com>
988 (defun iswitchb-rotate-list (lis)
989 "Destructively remove the last element from LIS.
990 Return the modified list with the last element prepended to it."
991 (if (<= (length lis) 1)
993 (let ((las lis)
994 (prev lis))
995 (while (consp (cdr las))
996 (setq prev las
997 las (cdr las)))
998 (setcdr prev nil)
999 (cons (car las) lis))))
1001 (defun iswitchb-completion-help ()
1002 "Show possible completions in a *Completions* buffer."
1003 ;; we could allow this buffer to be used to select match, but I think
1004 ;; choose-completion-string will need redefining, so it just inserts
1005 ;; choice with out any previous input.
1006 (interactive)
1007 (setq iswitchb-rescan nil)
1008 (let ((buf (current-buffer))
1009 (temp-buf "*Completions*")
1010 (win))
1012 (if (and (eq last-command this-command)
1013 (not iswitchb-common-match-inserted))
1014 ;; scroll buffer
1015 (progn
1016 (set-buffer temp-buf)
1017 (setq win (get-buffer-window temp-buf))
1018 (if (pos-visible-in-window-p (point-max) win)
1019 (set-window-start win (point-min))
1020 (scroll-other-window))
1021 (set-buffer buf))
1023 (with-output-to-temp-buffer temp-buf
1024 (if (featurep 'xemacs)
1026 ;; XEmacs extents are put on by default, doesn't seem to be
1027 ;; any way of switching them off.
1028 (display-completion-list (if iswitchb-matches
1029 iswitchb-matches
1030 iswitchb-buflist)
1031 :help-string "iswitchb "
1032 :activate-callback
1033 (lambda (x y z)
1034 (message "doesn't work yet, sorry!")))
1035 ;; else running Emacs
1036 (with-current-buffer standard-output
1037 (fundamental-mode))
1038 (display-completion-list (if iswitchb-matches
1039 iswitchb-matches
1040 iswitchb-buflist))))
1041 (setq iswitchb-common-match-inserted nil))))
1043 ;;; KILL CURRENT BUFFER
1045 (defun iswitchb-kill-buffer ()
1046 "Kill the buffer at the head of `iswitchb-matches'."
1047 (interactive)
1048 (let ( (enable-recursive-minibuffers t)
1049 buf)
1051 (setq buf (car iswitchb-matches))
1052 ;; check to see if buf is non-nil.
1053 (if buf
1054 (progn
1055 (kill-buffer buf)
1057 ;; Check if buffer exists. XEmacs gnuserv.el makes alias
1058 ;; for kill-buffer which does not return t if buffer is
1059 ;; killed, so we can't rely on kill-buffer return value.
1060 (if (get-buffer buf)
1061 ;; buffer couldn't be killed.
1062 (setq iswitchb-rescan t)
1063 ;; else buffer was killed so remove name from list.
1064 (setq iswitchb-buflist (delq buf iswitchb-buflist)))))))
1066 ;;; VISIT CHOSEN BUFFER
1067 (defun iswitchb-visit-buffer (buffer)
1068 "Visit buffer named BUFFER according to `iswitchb-method'."
1069 (let* (win newframe)
1070 (cond
1071 ((eq iswitchb-method 'samewindow)
1072 (switch-to-buffer buffer))
1074 ((memq iswitchb-method '(always-frame maybe-frame))
1075 (cond
1076 ((and (setq win (iswitchb-window-buffer-p buffer))
1077 (or (eq iswitchb-method 'always-frame)
1078 (y-or-n-p "Jump to frame? ")))
1079 (setq newframe (window-frame win))
1080 (if (fboundp 'select-frame-set-input-focus)
1081 (select-frame-set-input-focus newframe)
1082 (raise-frame newframe)
1083 (select-frame newframe)
1085 (select-window win))
1087 ;; No buffer in other frames...
1088 (switch-to-buffer buffer)
1091 ((eq iswitchb-method 'otherwindow)
1092 (switch-to-buffer-other-window buffer))
1094 ((eq iswitchb-method 'display)
1095 (display-buffer buffer))
1097 ((eq iswitchb-method 'otherframe)
1098 (progn
1099 (switch-to-buffer-other-frame buffer)
1100 (if (fboundp 'select-frame-set-input-focus)
1101 (select-frame-set-input-focus (selected-frame)))
1102 )))))
1104 (defun iswitchb-possible-new-buffer (buf)
1105 "Possibly create and visit a new buffer called BUF."
1107 (let ((newbufcreated))
1108 (if (and iswitchb-newbuffer
1110 (not iswitchb-prompt-newbuffer)
1112 (and iswitchb-prompt-newbuffer
1113 (y-or-n-p
1114 (format
1115 "No buffer matching `%s', create one? "
1116 buf)))))
1117 ;; then create a new buffer
1118 (progn
1119 (setq newbufcreated (get-buffer-create buf))
1120 (if (fboundp 'set-buffer-major-mode)
1121 (set-buffer-major-mode newbufcreated))
1122 (iswitchb-visit-buffer newbufcreated))
1123 ;; else wont create new buffer
1124 (message "no buffer matching `%s'" buf))))
1126 (defun iswitchb-window-buffer-p (buffer)
1127 "Return window pointer if BUFFER is visible in another frame.
1128 If BUFFER is visible in the current frame, return nil."
1129 (interactive)
1130 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
1131 ;;If the buffer is visible in current frame, return nil
1132 (if (memq buffer blist)
1134 ;; maybe in other frame or icon
1135 (get-buffer-window buffer 0) ; better than 'visible
1138 (defun iswitchb-default-keybindings ()
1139 "Set up default keybindings for `iswitchb-buffer'.
1140 Call this function to override the normal bindings. This function also
1141 adds a hook to the minibuffer.
1143 Obsolescent. Use `iswitchb-mode'."
1144 (interactive)
1145 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1146 (global-set-key "\C-xb" 'iswitchb-buffer)
1147 (global-set-key "\C-x4b" 'iswitchb-buffer-other-window)
1148 (global-set-key "\C-x4\C-o" 'iswitchb-display-buffer)
1149 (global-set-key "\C-x5b" 'iswitchb-buffer-other-frame))
1151 (defun iswitchb-buffer ()
1152 "Switch to another buffer.
1154 The buffer name is selected interactively by typing a substring. The
1155 buffer is displayed according to `iswitchb-default-method' -- the
1156 default is to show it in the same window, unless it is already visible
1157 in another frame.
1158 For details of keybindings, do `\\[describe-function] iswitchb'."
1159 (interactive)
1160 (setq iswitchb-method iswitchb-default-method)
1161 (iswitchb))
1163 (defun iswitchb-buffer-other-window ()
1164 "Switch to another buffer and show it in another window.
1165 The buffer name is selected interactively by typing a substring.
1166 For details of keybindings, do `\\[describe-function] iswitchb'."
1167 (interactive)
1168 (setq iswitchb-method 'otherwindow)
1169 (iswitchb))
1171 (defun iswitchb-display-buffer ()
1172 "Display a buffer in another window but don't select it.
1173 The buffer name is selected interactively by typing a substring.
1174 For details of keybindings, do `\\[describe-function] iswitchb'."
1175 (interactive)
1176 (setq iswitchb-method 'display)
1177 (iswitchb))
1179 (defun iswitchb-buffer-other-frame ()
1180 "Switch to another buffer and show it in another frame.
1181 The buffer name is selected interactively by typing a substring.
1182 For details of keybindings, do `\\[describe-function] iswitchb'."
1183 (interactive)
1184 (setq iswitchb-method 'otherframe)
1185 (iswitchb))
1187 ;;; XEmacs hack for showing default buffer
1189 ;; The first time we enter the minibuffer, Emacs puts up the default
1190 ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
1191 ;; subtle difference in the two versions of post-command-hook. The
1192 ;; default is shown for both whenever we delete all of our text
1193 ;; though, indicating its just a problem the first time we enter the
1194 ;; function. To solve this, we use another entry hook for emacs to
1195 ;; show the default the first time we enter the minibuffer.
1197 (defun iswitchb-init-XEmacs-trick ()
1198 "Display default buffer when first entering minibuffer.
1199 This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
1200 (if (iswitchb-entryfn-p)
1201 (progn
1202 (iswitchb-exhibit)
1203 (goto-char (point-min)))))
1205 ;; add this hook for XEmacs only.
1206 (if (featurep 'xemacs)
1207 (add-hook 'iswitchb-minibuffer-setup-hook
1208 'iswitchb-init-XEmacs-trick))
1210 ;;; XEmacs / backspace key
1211 ;; For some reason, if the backspace key is pressed in XEmacs, the
1212 ;; line gets confused, so I've added a simple key definition to make
1213 ;; backspace act like the normal delete key.
1215 (defun iswitchb-xemacs-backspacekey ()
1216 "Bind backspace to `backward-delete-char'."
1217 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
1218 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word))
1220 (if (featurep 'xemacs)
1221 (add-hook 'iswitchb-define-mode-map-hook
1222 'iswitchb-xemacs-backspacekey))
1224 ;;; ICOMPLETE TYPE CODE
1226 (defun iswitchb-exhibit ()
1227 "Find matching buffers and display a list in the minibuffer.
1228 Copied from `icomplete-exhibit' with two changes:
1229 1. It prints a default buffer name when there is no text yet entered.
1230 2. It calls my completion routine rather than the standard completion."
1231 (if iswitchb-use-mycompletion
1232 (let ((contents (buffer-substring (minibuffer-prompt-end) (point-max)))
1233 (buffer-undo-list t))
1234 (save-excursion
1235 (goto-char (point-max))
1236 ; Register the end of input, so we
1237 ; know where the extra stuff
1238 ; (match-status info) begins:
1239 (if (not (boundp 'iswitchb-eoinput))
1240 ;; In case it got wiped out by major mode business:
1241 (make-local-variable 'iswitchb-eoinput))
1242 (setq iswitchb-eoinput (point))
1243 ;; Update the list of matches
1244 (setq iswitchb-text contents)
1245 (iswitchb-set-matches)
1246 (setq iswitchb-rescan t)
1247 (iswitchb-set-common-completion)
1249 ;; Insert the match-status information:
1250 (insert (iswitchb-completions
1251 contents))))))
1253 (eval-when-compile
1254 (defvar most-len)
1255 (defvar most-is-exact))
1257 (defun iswitchb-output-completion (com)
1258 (if (= (length com) most-len)
1259 ;; Most is one exact match,
1260 ;; note that and leave out
1261 ;; for later indication:
1262 (ignore
1263 (setq most-is-exact t))
1264 (substring com most-len)))
1266 (defun iswitchb-completions (name)
1267 "Return the string that is displayed after the user's text.
1268 Modified from `icomplete-completions'."
1270 (let ((comps iswitchb-matches)
1271 ; "-determined" - only one candidate
1272 (open-bracket-determined "[")
1273 (close-bracket-determined "]")
1274 ;"-prospects" - more than one candidate
1275 (open-bracket-prospects "{")
1276 (close-bracket-prospects "}")
1277 first)
1279 (if (and iswitchb-use-faces comps)
1280 (progn
1281 (setq first (car comps))
1282 (setq first (format "%s" first))
1283 (put-text-property 0 (length first) 'face
1284 (if (= (length comps) 1)
1285 (if iswitchb-invalid-regexp
1286 'iswitchb-invalid-regexp
1287 'iswitchb-single-match)
1288 'iswitchb-current-match)
1289 first)
1290 (setq comps (cons first (cdr comps)))))
1292 ;; If no buffers matched, and virtual buffers are being used, then
1293 ;; consult the list of past visited files, to see if we can find
1294 ;; the file which the user might thought was still open.
1295 (when (and iswitchb-use-virtual-buffers (null comps)
1296 recentf-list)
1297 (setq iswitchb-virtual-buffers nil)
1298 (let ((head recentf-list) name)
1299 (while head
1300 (if (and (setq name (file-name-nondirectory (car head)))
1301 (string-match (if iswitchb-regexp
1302 iswitchb-text
1303 (regexp-quote iswitchb-text)) name)
1304 (null (get-file-buffer (car head)))
1305 (not (assoc name iswitchb-virtual-buffers))
1306 (not (iswitchb-ignore-buffername-p name))
1307 (file-exists-p (car head)))
1308 (setq iswitchb-virtual-buffers
1309 (cons (cons name (car head))
1310 iswitchb-virtual-buffers)))
1311 (setq head (cdr head)))
1312 (setq iswitchb-virtual-buffers (nreverse iswitchb-virtual-buffers)
1313 comps (mapcar 'car iswitchb-virtual-buffers))
1314 (let ((comp comps))
1315 (while comp
1316 (put-text-property 0 (length (car comp))
1317 'face 'iswitchb-virtual-matches
1318 (car comp))
1319 (setq comp (cdr comp))))))
1321 (cond ((null comps) (format " %sNo match%s"
1322 open-bracket-determined
1323 close-bracket-determined))
1325 (iswitchb-invalid-regexp
1326 (concat " " (car comps)))
1327 ((null (cdr comps)) ;one match
1328 (concat
1329 (if (if (not iswitchb-regexp)
1330 (= (length name)
1331 (length (car comps)))
1332 (string-match name (car comps))
1333 (string-equal (match-string 0 (car comps))
1334 (car comps)))
1336 (concat open-bracket-determined
1337 ;; when there is one match, show the
1338 ;; matching buffer name in full
1339 (car comps)
1340 close-bracket-determined))
1341 (if (not iswitchb-use-faces) " [Matched]")))
1342 (t ;multiple matches
1343 (if (and iswitchb-max-to-show
1344 (> (length comps) iswitchb-max-to-show))
1345 (setq comps
1346 (append
1347 (let ((res nil)
1348 (comp comps)
1349 (end (/ iswitchb-max-to-show 2)))
1350 (while (>= (setq end (1- end)) 0)
1351 (setq res (cons (car comp) res)
1352 comp (cdr comp)))
1353 (nreverse res))
1354 (list "...")
1355 (nthcdr (- (length comps)
1356 (/ iswitchb-max-to-show 2)) comps))))
1357 (let* (
1358 ;;(most (try-completion name candidates predicate))
1359 (most nil)
1360 (most-len (length most))
1361 most-is-exact
1362 (alternatives
1363 (mapconcat (if most 'iswitchb-output-completion
1364 'identity) comps ",")))
1366 (concat
1368 ;; put in common completion item -- what you get by
1369 ;; pressing tab
1370 (if (and (stringp iswitchb-common-match-string)
1371 (> (length iswitchb-common-match-string) (length name)))
1372 (concat open-bracket-determined
1373 (substring iswitchb-common-match-string
1374 (length name))
1375 close-bracket-determined))
1376 ;; end of partial matches...
1378 ;; think this bit can be ignored.
1379 (and (> most-len (length name))
1380 (concat open-bracket-determined
1381 (substring most (length name))
1382 close-bracket-determined))
1384 ;; list all alternatives
1385 open-bracket-prospects
1386 (if most-is-exact
1387 (concat "," alternatives)
1388 alternatives)
1389 close-bracket-prospects))))))
1391 (defun iswitchb-minibuffer-setup ()
1392 "Set up minibuffer for `iswitchb-buffer'.
1393 Copied from `icomplete-minibuffer-setup-hook'."
1394 (when (iswitchb-entryfn-p)
1395 (set (make-local-variable 'iswitchb-use-mycompletion) t)
1396 (add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
1397 (add-hook 'post-command-hook 'iswitchb-post-command nil t)
1398 (run-hooks 'iswitchb-minibuffer-setup-hook)))
1400 (defun iswitchb-pre-command ()
1401 "Run before command in `iswitchb-buffer'."
1402 (iswitchb-tidy))
1404 (defun iswitchb-post-command ()
1405 "Run after command in `iswitchb-buffer'."
1406 (iswitchb-exhibit))
1408 (defun iswitchb-tidy ()
1409 "Remove completions display, if any, prior to new user input.
1410 Copied from `icomplete-tidy'."
1412 (if (and (boundp 'iswitchb-eoinput)
1413 iswitchb-eoinput)
1415 (if (> iswitchb-eoinput (point-max))
1416 ;; Oops, got rug pulled out from under us - reinit:
1417 (setq iswitchb-eoinput (point-max))
1418 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1419 (delete-region iswitchb-eoinput (point-max))))
1421 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1422 (make-local-variable 'iswitchb-eoinput)
1423 (setq iswitchb-eoinput 1)))
1425 (defun iswitchb-entryfn-p ()
1426 "Return non-nil if we are using `iswitchb-buffer'."
1427 (eq iswitchb-minibuf-depth (minibuffer-depth)))
1429 (defun iswitchb-summaries-to-end ()
1430 "Move the summaries to the end of the list.
1431 This is an example function which can be hooked on to
1432 `iswitchb-make-buflist-hook'. Any buffer matching the regexps
1433 `Summary' or `output\*$'are put to the end of the list."
1434 (let ((summaries (delq nil
1435 (mapcar
1436 (lambda (x)
1437 (if (string-match "Summary\\|output\\*$" x)
1439 iswitchb-temp-buflist))))
1440 (iswitchb-to-end summaries)))
1442 (defun iswitchb-case ()
1443 "Return non-nil if we should ignore case when matching.
1444 See the variable `iswitchb-case' for details."
1445 (if iswitchb-case
1446 (if (featurep 'xemacs)
1447 (isearch-no-upper-case-p iswitchb-text)
1448 (isearch-no-upper-case-p iswitchb-text t))))
1450 ;;;###autoload
1451 (define-minor-mode iswitchb-mode
1452 "Toggle Iswitchb global minor mode.
1453 With arg, turn Iswitchb mode on if ARG is positive, otherwise turn it off.
1454 This mode enables switching between buffers using substrings. See
1455 `iswitchb' for details."
1456 nil nil iswitchb-global-map :global t :group 'iswitchb
1457 (if iswitchb-mode
1458 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1459 (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))
1461 (provide 'iswitchb)
1463 ;; arch-tag: d74198ae-753f-44f2-b34f-0c515398d90a
1464 ;;; iswitchb.el ends here