Update copyright year to 2015
[emacs.git] / lisp / obsolete / iswitchb.el
blob6b1e5347e2196a242161bcccea5012f3e67ea26c
1 ;;; iswitchb.el --- switch between buffers using substrings
3 ;; Copyright (C) 1996-1997, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Stephen Eglen <stephen@gnu.org>
6 ;; Maintainer: Stephen Eglen <stephen@gnu.org>
7 ;; Keywords: completion convenience
8 ;; Obsolete-since: 24.4
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 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file is obsolete - use icomplete-mode or ido-mode instead.
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 ;;; Customization
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). Coloring 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 behavior 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 behavior 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 ;;; Acknowledgments
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 (require 'font-lock)
249 ;;; User Variables
251 ;; These are some things you might want to change.
253 (defgroup iswitchb nil
254 "Switch between buffers using substrings."
255 :group 'convenience
256 :group 'completion
257 :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el")
258 :link '(url-link "http://www.anc.ed.ac.uk/~stephen/emacs/")
259 :link '(emacs-library-link :tag "Lisp File" "iswitchb.el"))
261 (defcustom iswitchb-case case-fold-search
262 "Non-nil if searching of buffer names should ignore case.
263 If this is non-nil but the user input has any upper case letters, matching
264 is temporarily case sensitive."
265 :type 'boolean
266 :group 'iswitchb)
268 (defcustom iswitchb-buffer-ignore
269 '("^ ")
270 "List of regexps or functions matching buffer names to ignore.
271 For example, traditional behavior is not to list buffers whose names begin
272 with a space, for which the regexp is `^ '. See the source file for
273 example functions that filter buffer names."
274 :type '(repeat (choice regexp function))
275 :group 'iswitchb)
276 (put 'iswitchb-buffer-ignore 'risky-local-variable t)
278 (defcustom iswitchb-max-to-show nil
279 "If non-nil, limit the number of names shown in the minibuffer.
280 If this value is N, and N is greater than the number of matching
281 buffers, the first N/2 and the last N/2 matching buffers are
282 shown. This can greatly speed up iswitchb if you have a
283 multitude of buffers open."
284 :type '(choice (const :tag "Show all" nil) integer)
285 :group 'iswitchb)
287 (defcustom iswitchb-use-virtual-buffers nil
288 "If non-nil, refer to past buffers when none match.
289 This feature relies upon the `recentf' package, which will be
290 enabled if this variable is configured to a non-nil value."
291 :type 'boolean
292 :require 'recentf
293 :set (function
294 (lambda (sym value)
295 (if value (recentf-mode 1))
296 (set sym value)))
297 :group 'iswitchb)
299 (defvar iswitchb-virtual-buffers nil)
301 (defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help
302 "Hook run when `iswitchb-complete' can't complete any more.
303 The most useful values are `iswitchb-completion-help', which pops up a
304 window with completion alternatives, or `iswitchb-next-match' or
305 `iswitchb-prev-match', which cycle the buffer list."
306 :type 'hook
307 :group 'iswitchb)
309 ;; Examples for setting the value of iswitchb-buffer-ignore
310 ;;(defun iswitchb-ignore-c-mode (name)
311 ;; "Ignore all c mode buffers -- example function for iswitchb."
312 ;; (with-current-buffer name
313 ;; (derived-mode-p 'c-mode)))
315 ;;(setq iswitchb-buffer-ignore '("^ " iswitchb-ignore-c-mode))
316 ;;(setq iswitchb-buffer-ignore '("^ " "\\.c\\'" "\\.h\\'"))
318 (defcustom iswitchb-default-method 'always-frame
319 "How to switch to new buffer when using `iswitchb-buffer'.
320 Possible values:
321 `samewindow' Show new buffer in same window
322 `otherwindow' Show new buffer in another window (same frame)
323 `display' Display buffer in another window without switching to it
324 `otherframe' Show new buffer in another frame
325 `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
326 you want to see the buffer in the same window of the current
327 frame or in the other frame.
328 `always-frame' If a buffer is visible in another frame, raise that
329 frame. Otherwise, visit the buffer in the same window."
330 :type '(choice (const samewindow)
331 (const otherwindow)
332 (const display)
333 (const otherframe)
334 (const maybe-frame)
335 (const always-frame))
336 :group 'iswitchb)
338 (defcustom iswitchb-regexp nil
339 "Non-nil means that `iswitchb' will do regexp matching.
340 Value can be toggled within `iswitchb' using `iswitchb-toggle-regexp'."
341 :type 'boolean
342 :group 'iswitchb)
344 (defcustom iswitchb-newbuffer t
345 "Non-nil means create new buffer if no buffer matches substring.
346 See also `iswitchb-prompt-newbuffer'."
347 :type 'boolean
348 :group 'iswitchb)
350 (defcustom iswitchb-prompt-newbuffer t
351 "Non-nil means prompt user to confirm before creating new buffer.
352 See also `iswitchb-newbuffer'."
353 :type 'boolean
354 :group 'iswitchb)
356 (define-obsolete-variable-alias 'iswitchb-use-fonts 'iswitchb-use-faces "22.1")
358 (defcustom iswitchb-use-faces t
359 "Non-nil means use font-lock faces for showing first match."
360 :type 'boolean
361 :group 'iswitchb)
363 (defcustom iswitchb-use-frame-buffer-list nil
364 "Non-nil means use the currently selected frame's buffer list."
365 :type 'boolean
366 :group 'iswitchb)
368 (defcustom iswitchb-make-buflist-hook nil
369 "Hook to run when list of matching buffers is created."
370 :type 'hook
371 :group 'iswitchb)
373 (defcustom iswitchb-delim ","
374 "Delimiter to put between buffer names when displaying results."
375 :type 'string
376 :group 'iswitchb)
378 (defcustom iswitchb-all-frames 'visible
379 "Argument to pass to `walk-windows' when iswitchb is finding buffers.
380 See documentation of `walk-windows' for useful values."
381 :type '(choice (const :tag "Selected frame only" nil)
382 (const :tag "All existing frames" t)
383 (const :tag "All visible frames" visible)
384 (const :tag "All frames on this terminal" 0))
385 :group 'iswitchb)
387 (defcustom iswitchb-minibuffer-setup-hook nil
388 "Iswitchb-specific customization of minibuffer setup.
390 This hook is run during minibuffer setup if `iswitchb' is active.
391 For instance:
392 \(add-hook 'iswitchb-minibuffer-setup-hook
393 '\(lambda () (set (make-local-variable 'max-mini-window-height) 3)))
394 will constrain the minibuffer to a maximum height of 3 lines when
395 iswitchb is running."
396 :type 'hook
397 :group 'iswitchb)
399 (defface iswitchb-single-match
400 '((t
401 (:inherit font-lock-comment-face)))
402 "Iswitchb face for single matching buffer name."
403 :version "22.1"
404 :group 'iswitchb)
406 (defface iswitchb-current-match
407 '((t
408 (:inherit font-lock-function-name-face)))
409 "Iswitchb face for current matching buffer name."
410 :version "22.1"
411 :group 'iswitchb)
413 (defface iswitchb-virtual-matches
414 '((t
415 (:inherit font-lock-builtin-face)))
416 "Iswitchb face for matching virtual buffer names.
417 See also `iswitchb-use-virtual-buffers'."
418 :version "22.1"
419 :group 'iswitchb)
421 (defface iswitchb-invalid-regexp
422 '((t
423 (:inherit font-lock-warning-face)))
424 "Iswitchb face for indicating invalid regexp. "
425 :version "22.1"
426 :group 'iswitchb)
428 ;; Do we need the variable iswitchb-use-mycompletion?
430 ;;; Internal Variables
432 (defvar iswitchb-method nil
433 "Stores the method for viewing the selected buffer.
434 Its value is one of `samewindow', `otherwindow', `display', `otherframe',
435 `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
436 details of values.")
438 (defvar iswitchb-eoinput 1
439 "Point where minibuffer input ends and completion info begins.
440 Copied from `icomplete-eoinput'.")
441 (make-variable-buffer-local 'iswitchb-eoinput)
443 (defvar iswitchb-buflist nil
444 "Stores the current list of buffers that will be searched through.
445 The list is ordered, so that the most recent buffers come first,
446 although by default, the buffers visible in the current frame are put
447 at the end of the list. Created by `iswitchb-make-buflist'.")
449 ;; todo -- is this necessary?
451 (defvar iswitchb-use-mycompletion nil
452 "Non-nil means use `iswitchb-buffer' completion feedback.
453 Should only be set to t by iswitchb functions, so that it doesn't
454 interfere with other minibuffer usage.")
456 (defvar iswitchb-change-word-sub nil
457 "Private variable used by `iswitchb-word-matching-substring'.")
459 (defvar iswitchb-common-match-string nil
460 "Stores the string that is common to all matching buffers.")
462 (defvar iswitchb-rescan nil
463 "Non-nil means we need to regenerate the list of matching buffers.")
465 (defvar iswitchb-text nil
466 "Stores the users string as it is typed in.")
468 (defvar iswitchb-matches nil
469 "List of buffers currently matching `iswitchb-text'.")
471 (defvar iswitchb-mode-map
472 (let ((map (make-sparse-keymap)))
473 (set-keymap-parent map minibuffer-local-map)
474 (define-key map "?" 'iswitchb-completion-help)
475 (define-key map "\C-s" 'iswitchb-next-match)
476 (define-key map "\C-r" 'iswitchb-prev-match)
477 (define-key map [?\C-.] 'iswitchb-next-match)
478 (define-key map [?\C-,] 'iswitchb-prev-match)
479 (define-key map "\t" 'iswitchb-complete)
480 (define-key map "\C-j" 'iswitchb-select-buffer-text)
481 (define-key map "\C-t" 'iswitchb-toggle-regexp)
482 (define-key map "\C-x\C-f" 'iswitchb-find-file)
483 (define-key map "\C-c" 'iswitchb-toggle-case)
484 (define-key map "\C-k" 'iswitchb-kill-buffer)
485 (define-key map "\C-m" 'iswitchb-exit-minibuffer)
486 map)
487 "Minibuffer keymap for `iswitchb-buffer'.")
489 (defvar iswitchb-global-map
490 (let ((map (make-sparse-keymap)))
491 (dolist (b '((switch-to-buffer . iswitchb-buffer)
492 (switch-to-buffer-other-window . iswitchb-buffer-other-window)
493 (switch-to-buffer-other-frame . iswitchb-buffer-other-frame)
494 (display-buffer . iswitchb-display-buffer)))
495 (if (fboundp 'command-remapping)
496 (define-key map (vector 'remap (car b)) (cdr b))
497 (substitute-key-definition (car b) (cdr b) map global-map)))
498 map)
499 "Global keymap for `iswitchb-mode'.")
501 (defvar iswitchb-history nil
502 "History of buffers selected using `iswitchb-buffer'.")
504 (defvar iswitchb-exit nil
505 "Flag to monitor how `iswitchb-buffer' exits.
506 If equal to `takeprompt', we use the prompt as the buffer name to be
507 selected.")
509 (defvar iswitchb-buffer-ignore-orig nil
510 "Stores original value of `iswitchb-buffer-ignore'.")
512 (defvar iswitchb-default nil
513 "Default buffer for iswitchb.")
515 ;; The following variables are needed to keep the byte compiler quiet.
516 (defvar iswitchb-require-match nil
517 "Non-nil if matching buffer must be selected.")
519 (defvar iswitchb-temp-buflist nil
520 "Stores a temporary version of the buffer list being created.")
522 (defvar iswitchb-bufs-in-frame nil
523 "List of the buffers visible in the current frame.")
525 (defvar iswitchb-minibuf-depth nil
526 "Value we expect to be returned by `minibuffer-depth' in the minibuffer.")
528 (defvar iswitchb-common-match-inserted nil
529 "Non-nil if we have just inserted a common match in the minibuffer.")
531 (defvar iswitchb-invalid-regexp)
533 ;;; FUNCTIONS
535 ;;; MAIN FUNCTION
536 (defun iswitchb ()
537 "Switch to buffer matching a substring.
538 As you type in a string, all of the buffers matching the string are
539 displayed. When you have found the buffer you want, it can then be
540 selected. As you type, most keys have their normal keybindings,
541 except for the following:
542 \\<iswitchb-mode-map>
544 RET Select the buffer at the front of the list of matches. If the
545 list is empty, possibly prompt to create new buffer.
547 \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
548 If no buffer is found, prompt for a new one.
550 \\[iswitchb-next-match] Put the first element at the end of the list.
551 \\[iswitchb-prev-match] Put the last element at the start of the list.
552 \\[iswitchb-complete] Complete a common suffix to the current string that
553 matches all buffers. If there is only one match, select that buffer.
554 If there is no common suffix, show a list of all matching buffers
555 in a separate window.
556 \\[iswitchb-toggle-regexp] Toggle regexp searching.
557 \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
558 \\[iswitchb-completion-help] Show list of matching buffers in separate window.
559 \\[iswitchb-find-file] Exit iswitchb and drop into `find-file'.
560 \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
561 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
562 ;;`iswitchb-buffer-ignore')
564 (let* ((prompt "iswitch ")
565 iswitchb-invalid-regexp
566 (buf (iswitchb-read-buffer prompt)))
568 ;;(message "chosen text %s" iswitchb-final-text)
569 ;; Choose the buffer name: either the text typed in, or the head
570 ;; of the list of matches
572 (cond ( (eq iswitchb-exit 'findfile)
573 (call-interactively 'find-file))
574 (iswitchb-invalid-regexp
575 (message "Won't make invalid regexp named buffer"))
577 ;; View the buffer
578 ;;(message "go to buf %s" buf)
579 ;; Check buf is non-nil.
580 (if buf
581 (if (get-buffer buf)
582 ;; buffer exists, so view it and then exit
583 (iswitchb-visit-buffer buf)
584 ;; else buffer doesn't exist
585 (iswitchb-possible-new-buffer buf)))
586 ))))
588 (defun iswitchb-read-buffer (prompt &optional default require-match
589 start matches-set)
590 "Replacement for the built-in `read-buffer'.
591 Return the name of a buffer selected.
592 PROMPT is the prompt to give to the user.
593 DEFAULT if given is the default buffer to be selected, which will
594 go to the front of the list.
595 If REQUIRE-MATCH is non-nil, an existing buffer must be selected.
596 If START is a string, the selection process is started with that
597 string.
598 If MATCHES-SET is non-nil, the buflist is not updated before
599 the selection process begins. Used by isearchb.el."
600 ;; The map is generated every time so that it can inherit new
601 ;; functions.
602 (let ((map (copy-keymap minibuffer-local-map))
603 buf-sel iswitchb-final-text
604 icomplete-mode) ; prevent icomplete starting up
605 (define-key map "?" 'iswitchb-completion-help)
606 (define-key map "\C-s" 'iswitchb-next-match)
607 (define-key map "\C-r" 'iswitchb-prev-match)
608 (define-key map "\t" 'iswitchb-complete)
609 (define-key map "\C-j" 'iswitchb-select-buffer-text)
610 (define-key map "\C-t" 'iswitchb-toggle-regexp)
611 (define-key map "\C-x\C-f" 'iswitchb-find-file)
612 (define-key map "\C-n" 'iswitchb-toggle-ignore)
613 (define-key map "\C-c" 'iswitchb-toggle-case)
614 (define-key map "\C-k" 'iswitchb-kill-buffer)
615 (define-key map "\C-m" 'iswitchb-exit-minibuffer)
616 (setq iswitchb-mode-map map)
617 (run-hooks 'iswitchb-define-mode-map-hook)
619 (setq iswitchb-exit nil)
620 (setq iswitchb-default
621 (if (bufferp default)
622 (buffer-name default)
623 default))
624 (setq iswitchb-text (or start ""))
625 (unless matches-set
626 (setq iswitchb-rescan t)
627 (iswitchb-make-buflist iswitchb-default)
628 (iswitchb-set-matches))
629 (let
630 ((minibuffer-local-completion-map iswitchb-mode-map)
631 ;; Record the minibuffer depth that we expect to find once
632 ;; the minibuffer is set up and iswitchb-entryfn-p is called.
633 (iswitchb-minibuf-depth (1+ (minibuffer-depth)))
634 (iswitchb-require-match require-match))
635 ;; prompt the user for the buffer name
636 (setq iswitchb-final-text (completing-read
637 prompt ;the prompt
638 '(("dummy" . 1)) ;table
639 nil ;predicate
640 nil ;require-match [handled elsewhere]
641 start ;initial-contents
642 'iswitchb-history)))
643 (if (and (not (eq iswitchb-exit 'usefirst))
644 (get-buffer iswitchb-final-text))
645 ;; This happens for example if the buffer was chosen with the mouse.
646 (setq iswitchb-matches (list iswitchb-final-text)
647 iswitchb-virtual-buffers nil))
649 ;; If no buffer matched, but a virtual buffer was selected, visit
650 ;; that file now and act as though that buffer had been selected.
651 (if (and iswitchb-virtual-buffers
652 (not (iswitchb-existing-buffer-p)))
653 (let ((virt (car iswitchb-virtual-buffers))
654 (new-buf))
655 ;; Keep the name of the buffer returned by find-file-noselect, as
656 ;; the buffer 'virt' could be a symlink to a file of a different name.
657 (setq new-buf (buffer-name (find-file-noselect (cdr virt))))
658 (setq iswitchb-matches (list new-buf)
659 iswitchb-virtual-buffers nil)))
661 ;; Handling the require-match must be done in a better way.
662 (if (and require-match
663 (not (iswitchb-existing-buffer-p)))
664 (error "Must specify valid buffer"))
666 (if (or (eq iswitchb-exit 'takeprompt)
667 (null iswitchb-matches))
668 (setq buf-sel iswitchb-final-text)
669 ;; else take head of list
670 (setq buf-sel (car iswitchb-matches)))
672 ;; Or possibly choose the default buffer
673 (if (equal iswitchb-final-text "")
674 (setq buf-sel (car iswitchb-matches)))
676 buf-sel))
678 (defun iswitchb-existing-buffer-p ()
679 "Return non-nil if there is a matching buffer."
680 (not (null iswitchb-matches)))
682 ;;; COMPLETION CODE
684 (defun iswitchb-set-common-completion ()
685 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
686 The result is stored in `iswitchb-common-match-string'."
688 (let (val)
689 (setq iswitchb-common-match-string nil)
690 (if (and iswitchb-matches
691 (not iswitchb-regexp) ;; testing
692 (stringp iswitchb-text)
693 (> (length iswitchb-text) 0))
694 (if (setq val (iswitchb-find-common-substring
695 iswitchb-matches iswitchb-text))
696 (setq iswitchb-common-match-string val)))
697 val))
699 (defun iswitchb-complete ()
700 "Try and complete the current pattern amongst the buffer names."
701 (interactive)
702 (let (res)
703 (cond ((not iswitchb-matches)
704 (run-hooks 'iswitchb-cannot-complete-hook))
705 (iswitchb-invalid-regexp
706 ;; Do nothing
708 ((= 1 (length iswitchb-matches))
709 ;; only one choice, so select it.
710 (exit-minibuffer))
713 ;; else there could be some completions
714 (setq res iswitchb-common-match-string)
715 (if (and (not (memq res '(t nil)))
716 (not (equal res iswitchb-text)))
717 ;; found something to complete, so put it in the minibuffer.
718 (progn
719 (setq iswitchb-rescan nil
720 iswitchb-common-match-inserted t)
721 (delete-region (minibuffer-prompt-end) (point))
722 (insert res))
723 ;; else nothing to complete
724 (run-hooks 'iswitchb-cannot-complete-hook)
725 )))))
727 ;;; TOGGLE FUNCTIONS
729 (defun iswitchb-toggle-case ()
730 "Toggle the value of variable `iswitchb-case'."
731 (interactive)
732 (setq iswitchb-case (not iswitchb-case))
733 ;; ask for list to be regenerated.
734 (setq iswitchb-rescan t))
736 (defun iswitchb-toggle-regexp ()
737 "Toggle the value of `iswitchb-regexp'."
738 (interactive)
739 (setq iswitchb-regexp (not iswitchb-regexp))
740 ;; ask for list to be regenerated.
741 (setq iswitchb-rescan t))
743 (defun iswitchb-toggle-ignore ()
744 "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
745 (interactive)
746 (if iswitchb-buffer-ignore
747 (progn
748 (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
749 (setq iswitchb-buffer-ignore nil))
750 ;; else
751 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig))
752 (iswitchb-make-buflist iswitchb-default)
753 ;; ask for list to be regenerated.
754 (setq iswitchb-rescan t))
756 (defun iswitchb-exit-minibuffer ()
757 "Exit minibuffer, but make sure we have a match if one is needed."
758 (interactive)
759 (if (or (not iswitchb-require-match)
760 (iswitchb-existing-buffer-p))
761 (progn
762 (setq iswitchb-exit 'usefirst)
763 (throw 'exit nil))))
765 (defun iswitchb-select-buffer-text ()
766 "Select the buffer named by the prompt.
767 If no buffer exactly matching the prompt exists, maybe create a new one."
768 (interactive)
769 (setq iswitchb-exit 'takeprompt)
770 (exit-minibuffer))
772 (defun iswitchb-find-file ()
773 "Drop into `find-file' from buffer switching."
774 (interactive)
775 (setq iswitchb-exit 'findfile)
776 (exit-minibuffer))
778 (defvar recentf-list)
780 (defun iswitchb-next-match ()
781 "Put first element of `iswitchb-matches' at the end of the list."
782 (interactive)
783 (let ((next (cadr iswitchb-matches)))
784 (if (and (null next) iswitchb-virtual-buffers)
785 (setq recentf-list
786 (iswitchb-chop recentf-list
787 (cdr (cadr iswitchb-virtual-buffers))))
788 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next)))
789 (setq iswitchb-rescan t)))
791 (defun iswitchb-prev-match ()
792 "Put last element of `iswitchb-matches' at the front of the list."
793 (interactive)
794 (let ((prev (car (last iswitchb-matches))))
795 (if (and (null prev) iswitchb-virtual-buffers)
796 (setq recentf-list
797 (iswitchb-chop recentf-list
798 (cdr (car (last iswitchb-virtual-buffers)))))
799 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev)))
800 (setq iswitchb-rescan t)))
802 (defun iswitchb-chop (list elem)
803 "Remove all elements before ELEM and put them at the end of LIST."
804 (let ((ret nil)
805 (next nil)
806 (sofar nil))
807 (while (not ret)
808 (setq next (car list))
809 (if (equal next elem)
810 (setq ret (append list (nreverse sofar)))
811 ;; else
812 (progn
813 (setq list (cdr list))
814 (setq sofar (cons next sofar)))))
815 ret))
817 ;;; CREATE LIST OF ALL CURRENT BUFFERS
819 (defun iswitchb-make-buflist (default)
820 "Set `iswitchb-buflist' to the current list of buffers.
821 Currently visible buffers are put at the end of the list.
822 The hook `iswitchb-make-buflist-hook' is run after the list has been
823 created to allow the user to further modify the order of the buffer names
824 in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
825 it is put to the start of the list."
826 (setq iswitchb-buflist
827 (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
828 (iswitchb-temp-buflist
829 (delq nil
830 (mapcar
831 (lambda (x)
832 (let ((b-name (buffer-name x)))
833 (if (not
835 (iswitchb-ignore-buffername-p b-name)
836 (memq b-name iswitchb-current-buffers)))
837 b-name)))
838 (buffer-list (and iswitchb-use-frame-buffer-list
839 (selected-frame)))))))
840 (setq iswitchb-temp-buflist
841 (nconc iswitchb-temp-buflist iswitchb-current-buffers))
842 (run-hooks 'iswitchb-make-buflist-hook)
843 ;; Should this be after the hooks, or should the hooks be the
844 ;; final thing to be run?
845 (if default
846 (progn
847 (setq iswitchb-temp-buflist
848 (delete default iswitchb-temp-buflist))
849 (setq iswitchb-temp-buflist
850 (cons default iswitchb-temp-buflist))))
851 iswitchb-temp-buflist)))
853 (defun iswitchb-to-end (lst)
854 "Move the elements from LST to the end of `iswitchb-temp-buflist'."
855 (dolist (elem lst)
856 (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist)))
857 (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst)))
859 (defun iswitchb-get-buffers-in-frames (&optional current)
860 "Return the list of buffers that are visible in the current frame.
861 If optional argument CURRENT is given, restrict searching to the
862 current frame, rather than all frames, regardless of value of
863 `iswitchb-all-frames'."
864 (let ((iswitchb-bufs-in-frame nil))
865 (walk-windows 'iswitchb-get-bufname nil
866 (if current
868 iswitchb-all-frames))
869 iswitchb-bufs-in-frame))
871 (defun iswitchb-get-bufname (win)
872 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
873 (let ((buf (buffer-name (window-buffer win))))
874 (if (not (member buf iswitchb-bufs-in-frame))
875 ;; Only add buf if it is not already in list.
876 ;; This prevents same buf in two different windows being
877 ;; put into the list twice.
878 (setq iswitchb-bufs-in-frame
879 (cons buf iswitchb-bufs-in-frame)))))
881 ;;; FIND MATCHING BUFFERS
883 (defun iswitchb-set-matches ()
884 "Set `iswitchb-matches' to the list of buffers matching prompt."
885 (if iswitchb-rescan
886 (setq iswitchb-matches
887 (let ((buflist iswitchb-buflist))
888 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
889 buflist))
890 iswitchb-virtual-buffers nil)))
892 (defun iswitchb-get-matched-buffers (regexp
893 &optional string-format buffer-list)
894 "Return buffers matching REGEXP.
895 If STRING-FORMAT is nil, consider REGEXP as just a string.
896 BUFFER-LIST can be list of buffers or list of strings."
897 (let ((case-fold-search (iswitchb-case))
898 name ret)
899 (if (null string-format) (setq regexp (regexp-quote regexp)))
900 (setq iswitchb-invalid-regexp nil)
901 (condition-case error
902 (dolist (x buffer-list (nreverse ret))
903 (setq name (if (stringp x) x (buffer-name x)))
904 (when (and (string-match regexp name)
905 (not (iswitchb-ignore-buffername-p name)))
906 (push name ret)))
907 (invalid-regexp
908 (setq iswitchb-invalid-regexp t)
909 (cdr error)))))
911 (defun iswitchb-ignore-buffername-p (bufname)
912 "Return t if the buffer BUFNAME should be ignored."
913 (let ((data (match-data))
914 (re-list iswitchb-buffer-ignore)
915 ignorep
916 nextstr)
917 (while re-list
918 (setq nextstr (car re-list))
919 (cond
920 ((stringp nextstr)
921 (if (string-match nextstr bufname)
922 (progn
923 (setq ignorep t)
924 (setq re-list nil))))
925 ((functionp nextstr)
926 (if (funcall nextstr bufname)
927 (progn
928 (setq ignorep t)
929 (setq re-list nil)))))
930 (setq re-list (cdr re-list)))
931 (set-match-data data)
933 ;; return the result
934 ignorep))
936 (defun iswitchb-word-matching-substring (word)
937 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
938 If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
939 (let ((case-fold-search (iswitchb-case)))
940 (let ((m (string-match iswitchb-change-word-sub word)))
941 (if m
942 (substring word m)
943 ;; else no match
944 nil))))
946 (defun iswitchb-find-common-substring (lis subs)
947 "Return common string following SUBS in each element of LIS."
948 (let (res
949 alist
950 iswitchb-change-word-sub)
951 (setq iswitchb-change-word-sub
952 (if iswitchb-regexp
953 subs
954 (regexp-quote subs)))
955 (setq res (mapcar 'iswitchb-word-matching-substring lis))
956 (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
957 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
959 ;; try-completion returns t if there is an exact match.
960 (let ((completion-ignore-case (iswitchb-case)))
962 (try-completion subs alist))))
964 (defun iswitchb-makealist (res)
965 "Return dotted pair (RES . 1)."
966 (cons res 1))
968 ;; from Wayne Mesard <wmesard@esd.sgi.com>
969 (defun iswitchb-rotate-list (lis)
970 "Destructively remove the last element from LIS.
971 Return the modified list with the last element prepended to it."
972 (if (<= (length lis) 1)
974 (let ((las lis)
975 (prev lis))
976 (while (consp (cdr las))
977 (setq prev las
978 las (cdr las)))
979 (setcdr prev nil)
980 (cons (car las) lis))))
982 (defun iswitchb-completion-help ()
983 "Show possible completions in a *Completions* buffer."
984 ;; we could allow this buffer to be used to select match, but I think
985 ;; choose-completion-string will need redefining, so it just inserts
986 ;; choice with out any previous input.
987 (interactive)
988 (setq iswitchb-rescan nil)
989 (let ((buf (current-buffer))
990 (temp-buf "*Completions*")
991 (win))
993 (if (and (eq last-command this-command)
994 (not iswitchb-common-match-inserted))
995 ;; scroll buffer
996 (progn
997 (set-buffer temp-buf)
998 (setq win (get-buffer-window temp-buf))
999 (if (pos-visible-in-window-p (point-max) win)
1000 (set-window-start win (point-min))
1001 (scroll-other-window))
1002 (set-buffer buf))
1004 (with-output-to-temp-buffer temp-buf
1005 (if (featurep 'xemacs)
1007 ;; XEmacs extents are put on by default, doesn't seem to be
1008 ;; any way of switching them off.
1009 (display-completion-list (or iswitchb-matches iswitchb-buflist)
1010 :help-string "iswitchb "
1011 :activate-callback
1012 (lambda (_x _y _z)
1013 (message "doesn't work yet, sorry!")))
1014 ;; else running Emacs
1015 (display-completion-list (or iswitchb-matches iswitchb-buflist))))
1016 (setq iswitchb-common-match-inserted nil))))
1018 ;;; KILL CURRENT BUFFER
1020 (defun iswitchb-kill-buffer ()
1021 "Kill the buffer at the head of `iswitchb-matches'."
1022 (interactive)
1023 (let ((enable-recursive-minibuffers t)
1024 buf)
1026 (setq buf (car iswitchb-matches))
1027 ;; check to see if buf is non-nil.
1028 (if buf
1029 (let ((bufobjs (mapcar (lambda (name)
1030 (or (get-buffer name) name))
1031 iswitchb-buflist)))
1032 (kill-buffer buf)
1034 ;; Check if buffer exists. XEmacs gnuserv.el makes alias
1035 ;; for kill-buffer which does not return t if buffer is
1036 ;; killed, so we can't rely on kill-buffer return value.
1037 (if (get-buffer buf)
1038 ;; buffer couldn't be killed.
1039 (setq iswitchb-rescan t)
1040 ;; Else `kill-buffer' succeeds so re-make the buffer list
1041 ;; taking into account packages like uniquify may rename
1042 ;; buffers, and try to preserve the ordering of buffers.
1043 (setq iswitchb-buflist
1044 (delq nil (mapcar (lambda (b)
1045 (if (bufferp b)
1046 (buffer-name b)
1048 bufobjs))))))))
1050 ;;; VISIT CHOSEN BUFFER
1051 (defun iswitchb-visit-buffer (buffer)
1052 "Visit buffer named BUFFER according to `iswitchb-method'."
1053 (let (win newframe)
1054 (cond
1055 ((eq iswitchb-method 'samewindow)
1056 (switch-to-buffer buffer))
1058 ((memq iswitchb-method '(always-frame maybe-frame))
1059 (cond
1060 ((and (setq win (iswitchb-window-buffer-p buffer))
1061 (or (eq iswitchb-method 'always-frame)
1062 (y-or-n-p "Jump to frame? ")))
1063 (setq newframe (window-frame win))
1064 (if (fboundp 'select-frame-set-input-focus)
1065 (select-frame-set-input-focus newframe)
1066 (raise-frame newframe)
1067 (select-frame newframe)
1069 (select-window win))
1071 ;; No buffer in other frames...
1072 (switch-to-buffer buffer)
1075 ((eq iswitchb-method 'otherwindow)
1076 (switch-to-buffer-other-window buffer))
1078 ((eq iswitchb-method 'display)
1079 (display-buffer buffer))
1081 ((eq iswitchb-method 'otherframe)
1082 (progn
1083 (switch-to-buffer-other-frame buffer)
1084 (if (fboundp 'select-frame-set-input-focus)
1085 (select-frame-set-input-focus (selected-frame)))
1086 )))))
1088 (defun iswitchb-possible-new-buffer (buf)
1089 "Possibly create and visit a new buffer called BUF."
1091 (let ((newbufcreated))
1092 (if (and iswitchb-newbuffer
1094 (not iswitchb-prompt-newbuffer)
1096 (and iswitchb-prompt-newbuffer
1097 (y-or-n-p
1098 (format
1099 "No buffer matching `%s', create one? "
1100 buf)))))
1101 ;; then create a new buffer
1102 (progn
1103 (setq newbufcreated (get-buffer-create buf))
1104 (if (fboundp 'set-buffer-major-mode)
1105 (set-buffer-major-mode newbufcreated))
1106 (iswitchb-visit-buffer newbufcreated))
1107 ;; else won't create new buffer
1108 (message "no buffer matching `%s'" buf))))
1110 (defun iswitchb-window-buffer-p (buffer)
1111 "Return window pointer if BUFFER is visible in another frame.
1112 If BUFFER is visible in the current frame, return nil."
1113 (interactive)
1114 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
1115 ;; If the buffer is visible in current frame, return nil
1116 (unless (member buffer blist)
1117 ;; maybe in other frame or icon
1118 (get-buffer-window buffer 0) ; better than 'visible
1121 (defun iswitchb-buffer ()
1122 "Switch to another buffer.
1124 The buffer name is selected interactively by typing a substring. The
1125 buffer is displayed according to `iswitchb-default-method' -- the
1126 default is to show it in the same window, unless it is already visible
1127 in another frame.
1128 For details of keybindings, do `\\[describe-function] iswitchb'."
1129 (interactive)
1130 (setq iswitchb-method iswitchb-default-method)
1131 (iswitchb))
1133 (defun iswitchb-buffer-other-window ()
1134 "Switch to another buffer and show it in another window.
1135 The buffer name is selected interactively by typing a substring.
1136 For details of keybindings, do `\\[describe-function] iswitchb'."
1137 (interactive)
1138 (setq iswitchb-method 'otherwindow)
1139 (iswitchb))
1141 (defun iswitchb-display-buffer ()
1142 "Display a buffer in another window but don't select it.
1143 The buffer name is selected interactively by typing a substring.
1144 For details of keybindings, do `\\[describe-function] iswitchb'."
1145 (interactive)
1146 (setq iswitchb-method 'display)
1147 (iswitchb))
1149 (defun iswitchb-buffer-other-frame ()
1150 "Switch to another buffer and show it in another frame.
1151 The buffer name is selected interactively by typing a substring.
1152 For details of keybindings, do `\\[describe-function] iswitchb'."
1153 (interactive)
1154 (setq iswitchb-method 'otherframe)
1155 (iswitchb))
1157 ;;; XEmacs hack for showing default buffer
1159 ;; The first time we enter the minibuffer, Emacs puts up the default
1160 ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
1161 ;; subtle difference in the two versions of post-command-hook. The
1162 ;; default is shown for both whenever we delete all of our text
1163 ;; though, indicating its just a problem the first time we enter the
1164 ;; function. To solve this, we use another entry hook for emacs to
1165 ;; show the default the first time we enter the minibuffer.
1167 (defun iswitchb-init-XEmacs-trick ()
1168 "Display default buffer when first entering minibuffer.
1169 This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
1170 (if (iswitchb-entryfn-p)
1171 (progn
1172 (iswitchb-exhibit)
1173 (goto-char (point-min)))))
1175 ;; add this hook for XEmacs only.
1176 (if (featurep 'xemacs)
1177 (add-hook 'iswitchb-minibuffer-setup-hook
1178 'iswitchb-init-XEmacs-trick))
1180 ;;; XEmacs / backspace key
1181 ;; For some reason, if the backspace key is pressed in XEmacs, the
1182 ;; line gets confused, so I've added a simple key definition to make
1183 ;; backspace act like the normal delete key.
1185 (defun iswitchb-xemacs-backspacekey ()
1186 "Bind backspace to `backward-delete-char'."
1187 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
1188 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word))
1190 (if (featurep 'xemacs)
1191 (add-hook 'iswitchb-define-mode-map-hook
1192 'iswitchb-xemacs-backspacekey))
1194 ;;; ICOMPLETE TYPE CODE
1196 (defun iswitchb-exhibit ()
1197 "Find matching buffers and display a list in the minibuffer.
1198 Copied from `icomplete-exhibit' with two changes:
1199 1. It prints a default buffer name when there is no text yet entered.
1200 2. It calls my completion routine rather than the standard completion."
1201 (if iswitchb-use-mycompletion
1202 (let ((contents (buffer-substring (minibuffer-prompt-end) (point-max)))
1203 (buffer-undo-list t))
1204 (save-excursion
1205 (goto-char (point-max))
1206 ; Register the end of input, so we
1207 ; know where the extra stuff
1208 ; (match-status info) begins:
1209 (if (not (boundp 'iswitchb-eoinput))
1210 ;; In case it got wiped out by major mode business:
1211 (make-local-variable 'iswitchb-eoinput))
1212 (setq iswitchb-eoinput (point))
1213 ;; Update the list of matches
1214 (setq iswitchb-text contents)
1215 (iswitchb-set-matches)
1216 (setq iswitchb-rescan t)
1217 (iswitchb-set-common-completion)
1219 ;; Insert the match-status information:
1220 (insert (iswitchb-completions
1221 contents))))))
1223 (defvar most-len)
1224 (defvar most-is-exact)
1226 (defun iswitchb-output-completion (com)
1227 (if (= (length com) most-len)
1228 ;; Most is one exact match,
1229 ;; note that and leave out
1230 ;; for later indication:
1231 (ignore
1232 (setq most-is-exact t))
1233 (substring com most-len)))
1235 (defun iswitchb-completions (name)
1236 "Return the string that is displayed after the user's text.
1237 Modified from `icomplete-completions'."
1239 (let ((comps iswitchb-matches)
1240 ; "-determined" - only one candidate
1241 (open-bracket-determined "[")
1242 (close-bracket-determined "]")
1243 ;"-prospects" - more than one candidate
1244 (open-bracket-prospects "{")
1245 (close-bracket-prospects "}")
1246 first)
1248 (if (and iswitchb-use-faces comps)
1249 (progn
1250 (setq first (car comps))
1251 (setq first (format "%s" first))
1252 (put-text-property 0 (length first) 'face
1253 (if (= (length comps) 1)
1254 (if iswitchb-invalid-regexp
1255 'iswitchb-invalid-regexp
1256 'iswitchb-single-match)
1257 'iswitchb-current-match)
1258 first)
1259 (setq comps (cons first (cdr comps)))))
1261 ;; If no buffers matched, and virtual buffers are being used, then
1262 ;; consult the list of past visited files, to see if we can find
1263 ;; the file which the user might thought was still open.
1264 (when (and iswitchb-use-virtual-buffers (null comps)
1265 recentf-list)
1266 (setq iswitchb-virtual-buffers nil)
1267 (let ((head recentf-list) name)
1268 (while head
1269 (if (and (setq name (file-name-nondirectory (car head)))
1270 (string-match (if iswitchb-regexp
1271 iswitchb-text
1272 (regexp-quote iswitchb-text)) name)
1273 (null (get-file-buffer (car head)))
1274 (not (assoc name iswitchb-virtual-buffers))
1275 (not (iswitchb-ignore-buffername-p name))
1276 (file-exists-p (car head)))
1277 (setq iswitchb-virtual-buffers
1278 (cons (cons name (car head))
1279 iswitchb-virtual-buffers)))
1280 (setq head (cdr head)))
1281 (setq iswitchb-virtual-buffers (nreverse iswitchb-virtual-buffers)
1282 comps (mapcar 'car iswitchb-virtual-buffers))
1283 (let ((comp comps))
1284 (while comp
1285 (put-text-property 0 (length (car comp))
1286 'face 'iswitchb-virtual-matches
1287 (car comp))
1288 (setq comp (cdr comp))))))
1290 (cond ((null comps) (format " %sNo match%s"
1291 open-bracket-determined
1292 close-bracket-determined))
1294 (iswitchb-invalid-regexp
1295 (concat " " (car comps)))
1296 ((null (cdr comps)) ;one match
1297 (concat
1298 (if (if (not iswitchb-regexp)
1299 (= (length name)
1300 (length (car comps)))
1301 (string-match name (car comps))
1302 (string-equal (match-string 0 (car comps))
1303 (car comps)))
1305 (concat open-bracket-determined
1306 ;; when there is one match, show the
1307 ;; matching buffer name in full
1308 (car comps)
1309 close-bracket-determined))
1310 (if (not iswitchb-use-faces) " [Matched]")))
1311 (t ;multiple matches
1312 (if (and iswitchb-max-to-show
1313 (> (length comps) iswitchb-max-to-show))
1314 (setq comps
1315 (append
1316 (let ((res nil)
1317 (comp comps)
1318 (end (/ iswitchb-max-to-show 2)))
1319 (while (>= (setq end (1- end)) 0)
1320 (setq res (cons (car comp) res)
1321 comp (cdr comp)))
1322 (nreverse res))
1323 (list "...")
1324 (nthcdr (- (length comps)
1325 (/ iswitchb-max-to-show 2)) comps))))
1326 (let* (
1327 ;;(most (try-completion name candidates predicate))
1328 (most nil)
1329 (most-len (length most))
1330 most-is-exact
1331 (alternatives
1332 (mapconcat (if most 'iswitchb-output-completion
1333 'identity) comps iswitchb-delim)))
1335 (concat
1337 ;; put in common completion item -- what you get by
1338 ;; pressing tab
1339 (if (and (stringp iswitchb-common-match-string)
1340 (> (length iswitchb-common-match-string) (length name)))
1341 (concat open-bracket-determined
1342 (substring iswitchb-common-match-string
1343 (length name))
1344 close-bracket-determined))
1345 ;; end of partial matches...
1347 ;; think this bit can be ignored.
1348 (and (> most-len (length name))
1349 (concat open-bracket-determined
1350 (substring most (length name))
1351 close-bracket-determined))
1353 ;; list all alternatives
1354 open-bracket-prospects
1355 (if most-is-exact
1356 (concat iswitchb-delim alternatives)
1357 alternatives)
1358 close-bracket-prospects))))))
1360 (defun iswitchb-minibuffer-setup ()
1361 "Set up minibuffer for `iswitchb-buffer'.
1362 Copied from `icomplete-minibuffer-setup-hook'."
1363 (when (iswitchb-entryfn-p)
1364 (set (make-local-variable 'iswitchb-use-mycompletion) t)
1365 (add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
1366 (add-hook 'post-command-hook 'iswitchb-post-command nil t)
1367 (run-hooks 'iswitchb-minibuffer-setup-hook)))
1369 (defun iswitchb-pre-command ()
1370 "Run before command in `iswitchb-buffer'."
1371 (iswitchb-tidy))
1373 (defun iswitchb-post-command ()
1374 "Run after command in `iswitchb-buffer'."
1375 (iswitchb-exhibit))
1377 (defun iswitchb-tidy ()
1378 "Remove completions display, if any, prior to new user input.
1379 Copied from `icomplete-tidy'."
1381 (if (and (boundp 'iswitchb-eoinput)
1382 iswitchb-eoinput)
1384 (if (> iswitchb-eoinput (point-max))
1385 ;; Oops, got rug pulled out from under us - reinit:
1386 (setq iswitchb-eoinput (point-max))
1387 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1388 (delete-region iswitchb-eoinput (point-max))))
1390 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1391 (make-local-variable 'iswitchb-eoinput)
1392 (setq iswitchb-eoinput 1)))
1394 (defun iswitchb-entryfn-p ()
1395 "Return non-nil if we are using `iswitchb-buffer'."
1396 (eq iswitchb-minibuf-depth (minibuffer-depth)))
1398 (defun iswitchb-summaries-to-end ()
1399 "Move the summaries to the end of the list.
1400 This is an example function which can be hooked on to
1401 `iswitchb-make-buflist-hook'. Any buffer matching the regexps
1402 `Summary' or `output\*$'are put to the end of the list."
1403 (let ((summaries (delq nil
1404 (mapcar
1405 (lambda (x)
1406 (if (string-match "Summary\\|output\\*$" x)
1408 iswitchb-temp-buflist))))
1409 (iswitchb-to-end summaries)))
1411 (defun iswitchb-case ()
1412 "Return non-nil if we should ignore case when matching.
1413 See the variable `iswitchb-case' for details."
1414 (if iswitchb-case
1415 (if (featurep 'xemacs)
1416 (isearch-no-upper-case-p iswitchb-text)
1417 (isearch-no-upper-case-p iswitchb-text t))))
1419 ;;;###autoload
1420 (define-minor-mode iswitchb-mode
1421 "Toggle Iswitchb mode.
1422 With a prefix argument ARG, enable Iswitchb mode if ARG is
1423 positive, and disable it otherwise. If called from Lisp, enable
1424 the mode if ARG is omitted or nil.
1426 Iswitchb mode is a global minor mode that enables switching
1427 between buffers using substrings. See `iswitchb' for details."
1428 nil nil iswitchb-global-map :global t :group 'iswitchb
1429 (if iswitchb-mode
1430 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1431 (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))
1433 (provide 'iswitchb)
1435 ;;; iswitchb.el ends here