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