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