new version
[emacs.git] / lisp / iswitchb.el
blobddd14e0188d5d448a918e5557ae4078c810f60a0
1 ;;; iswitchb.el --- switch between buffers using substrings
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Stephen Eglen <stephene@cogs.susx.ac.uk>
6 ;; Maintainer: Stephen Eglen <stephene@cogs.susx.ac.uk>
7 ;; Keywords: extensions
8 ;; location: http://www.cogs.susx.ac.uk/users/stephene/emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Installation:
29 ;; To get the functions in this package bound to keys, do
30 ;; (iswitchb-default-keybindings)
32 ;;; Commentary:
34 ;; As you type in a substring, the list of buffers currently matching
35 ;; the substring are displayed as you type. The list is ordered so
36 ;; that the most recent buffers visited come at the start of the list.
37 ;; The buffer at the start of the list will be the one visited when
38 ;; you press return. By typing more of the substring, the list is
39 ;; narrowed down so that gradually the buffer you want will be at the
40 ;; top of the list. Alternatively, you can use C-s an C-r to rotate
41 ;; buffer names in the list until the one you want is at the top of
42 ;; the list. Completion is also available so that you can see what is
43 ;; common to all of the matching buffers as you type.
45 ;; This code is similar to a couple of other packages. Michael R Cook
46 ;; <mcook@cognex.com wrote a similar buffer switching package, but
47 ;; does exact matching rather than substring matching on buffer names.
48 ;; I also modified a couple of functions from icomplete.el to provide
49 ;; the completion feedback in the minibuffer.
51 ;;; Example
53 ;;If I have two buffers called "123456" and "123", with "123456" the
54 ;;most recent, when I use iswitchb, I first of all get presented with
55 ;;the default buffer (xxx) to switch to:
57 ;; iswitch {default xxx}
59 ;; If I then press 2:
60 ;; iswitch 2[3]{123456,123}
62 ;; The list in {} are the matching buffers, most recent first (buffers
63 ;; visible in the current frame are put at the end of the list by
64 ;; default). At any time I can select the item at the head of the
65 ;; list by pressing RET. I can also bring the put the first element
66 ;; at the end of the list by pressing C-s, or put the last element at
67 ;; the head of the list by pressing C-r. The item in [] indicates
68 ;; what can be added to my input by pressing TAB. In this case, I
69 ;; will get "3" added to my input. So, press TAB:
70 ;; iswitch 23{123456,123}
72 ;; At this point, I still have two matching buffers.
73 ;; If I want the first buffer in the list, I simply press RET. If I
74 ;; wanted the second in the list, I could press C-s to move it to the
75 ;; top of the list and then RET to select it.
77 ;;However, If I type 4, I only have one match left:
78 ;; iswitch 234[123456] [Matched]
80 ;;Since there is only one matching buffer left, it is given in [] and we
81 ;;see the text [Matched] afterwards. I can now press TAB or RET to go
82 ;;to that buffer.
84 ;; If however, I now type "a":
85 ;; iswitch 234a [No match]
86 ;; There are no matching buffers. If I press RET or TAB, I can be
87 ;; prompted to create a new buffer called "234a".
89 ;; Of course, where this function comes in really useful is when you
90 ;; can specify the buffer using only a few keystrokes. In the above
91 ;; example, the quickest way to get to the "123456" buffer would be
92 ;; just to type 4 and then RET (assuming there isnt any newer buffer
93 ;; with 4 in its name).
95 ;; To see a full list of all matching buffers in a separate buffer,
96 ;; hit ? or press TAB when there are no further completions to the
97 ;; substring.
100 ;; See the doc string of iswitchb for full keybindings and features.
101 ;; (describe-function 'iswitchb)
103 ;;; Customisation
105 ;; See the User Variables section below for easy ways to change the
106 ;; functionality of the program.
107 ;; To modify the keybindings, use the hook provided. For example:
108 ;;(add-hook 'iswitchb-define-mode-map-hook
109 ;; 'iswitchb-my-keys)
111 ;;(defun iswitchb-my-keys ()
112 ;; "Add my keybings for iswitchb."
113 ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match)
114 ;; )
116 ;; Seeing all the matching buffers.
117 ;; If you have many matching buffers, they may not all fit onto one
118 ;; line of the minibuffer. In this case, you should use rsz-mini
119 ;; (resize-minibuffer-mode). You can also limit iswitchb so that it
120 ;; only shows a certain number of lines -- see the documentation for
121 ;; `iswitchb-minibuffer-setup-hook'.
124 ;; Changing the list of buffers.
126 ;; By default, the list of current buffers is most recent first,
127 ;; oldest last, with the exception that the buffers visible in the
128 ;; current frame are put at the end of the list. A hook exists to
129 ;; allow other functions to order the list. For example, if you add:
131 ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
133 ;; then all buffers matching "Summary" are moved to the end of the
134 ;; list. (I find this handy for keeping the INBOX Summary and so on
135 ;; out of the way.) It also moves buffers matching "output\*$" to the
136 ;; end of the list (these are created by AUC TeX when compiling.)
137 ;; Other functions could be made available which alter the list of
138 ;; matching buffers (either deleting or rearranging elements.)
140 ;; Font-Lock
142 ;; If you have font-lock loaded, the first matching buffer is
143 ;; highlighted. To switch this off, set (setq iswitchb-use-fonts nil)
144 ;; I don't use font-lock that much, so I've hardcoded the faces. If
145 ;; this is too harsh, let me know. Colouring of the matching buffer
146 ;; name was suggested by Carsten Dominik (dominik@strw.leidenuniv.nl)
148 ;;; Comparison with iswitch-buffer
150 ;; This package is a rewrite of iswitch-buffer, using the minibuffer
151 ;; rather than the echo area. The advantages of using the minibuffer
152 ;; are several:
153 ;; o minibuffer has more powerful editing facilities
154 ;; o doesnt interfere with other packages that use the echo area
155 ;; o *Messages* buffer doesnt get filled up with all of the messages that
156 ;; go to the modeline
157 ;; o cursor is in the minibuffer, which somehow looks right.
158 ;; o minibuffer can be resized dynamically to show all the possible matching
159 ;; buffers rather than just the first line's worth (using rsz-mini).
161 ;; Disadvantages:
162 ;; o cant change the prompt to indicate status of searching (eg whether
163 ;; regexp searching is currently on).
166 ;;; Acknowledgements
168 ;; Thanks to Jari Aalto <jari.aalto@poboxes.com> for help with the
169 ;; first version of this package, iswitch-buffer. Thanks also to many
170 ;; others for testing earlier versions.
172 ;;; Code:
174 ;;; User Variables
176 ;; These are some things you might want to change.
178 (defvar iswitchb-case case-fold-search
179 "*Non-nil if searching of buffer names should ignore case.")
181 (defvar iswitchb-buffer-ignore
182 '("^ ")
183 "*List of regexps or functions matching buffer names to ignore.
184 For example, traditional behavior is not to list buffers whose names begin
185 with a space, for which the regexp is `^ '. See the source file for
186 example functions that filter buffernames.")
188 ;;; Examples for setting the value of iswitchb-buffer-ignore
189 ;(defun -c-mode (name)
190 ; "Ignore all c mode buffers -- example function for iswitchb."
191 ; (save-excursion
192 ; (set-buffer name)
193 ; (string-match "^C$" mode-name)))
195 ;(setq iswitchb-buffer-ignore '("^ " ignore-c-mode))
196 ;(setq iswitchb-buffer-ignore '("^ " "\\.c$" "\\.h$"))
199 (defvar iswitchb-default-method 'always-frame
200 "*How to switch to new buffer when using `iswitchb-buffer'.
201 Possible values:
202 `samewindow' Show new buffer in same window
203 `otherwindow' Show new buffer in another window (same frame)
204 `display' Display buffer in another window without switching to it
205 `otherframe' Show new buffer in another frame
206 `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
207 you want to see the buffer in the same window of the current
208 frame or in the other frame.
209 `always-frame' If a buffer is visible in another frame, raise that
210 frame. Otherwise, visit the buffer in the same window.")
212 (defvar iswitchb-regexp nil
213 "*Non-nil means that `iswitchb' will do regexp matching.
214 Value can be toggled within `iswitchb'.")
216 (defvar iswitchb-newbuffer t
217 "*Non-nil means create new buffer if no buffer matches substring.
218 See also `iswitchb-prompt-newbuffer'.")
220 (defvar iswitchb-prompt-newbuffer t
221 "*Non-nil means prompt user to confirm before creating new buffer.
222 See also `iswitchb-newbuffer'.")
224 (defvar iswitchb-define-mode-map-hook nil
225 "*Hook to define keys in `iswitchb-mode-map' for extra keybindings.")
228 (defvar iswitchb-use-fonts t
229 "*Non-nil means use fonts for showing first match.")
231 (defvar iswitchb-make-buflist-hook nil
232 "*Hook to run when list of matching buffers is created.")
235 (defvar iswitchb-method nil
236 "*Stores the method for viewing the selected buffer.
237 Its value is one of `samewindow', `otherwindow', `display', `otherframe',
238 `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
239 details of values.")
241 (defvar iswitchb-all-frames 'visible
242 "*Argument to pass to `walk-windows' when finding visible buffers.
243 See documentation of `walk-windows' for useful values.")
245 ;;; THINGS TO DO / BUGS
247 ;; In Xemacs, the default buffer is not shown the first time you enter
248 ; the minibuffer, but if you type a char and then delete a char, the
249 ; default appears. The first time we enter the minibuffer in XEmacs,
250 ; the default msg is not displayed, presumably because the hook is not
251 ; being called. I have put in a temporary hack therefore at the
252 ; bottom of this file.
254 ; There is also a problem with the backspace key in XEmacs, so I have
255 ; bound it to the normal backward-delete-char.
257 ;; iswitch-buffer features Not yet implemented:
258 ; C-f Quit iswitch and drop into find-file
261 ;; Do we need the variable iswitchb-use-mycompletion?
264 ;;; Internal Variables
265 (defvar iswitchb-minibuffer-setup-hook nil
266 "*Iswitchb-specific customization of minibuffer setup.
268 This hook is run during minibuffer setup iff `iswitchb' will be active.
269 It is intended for use in customizing iswitchb for interoperation
270 with other packages. For instance:
272 \(add-hook 'iswitchb-minibuffer-setup-hook
273 \(function
274 \(lambda ()
275 \(make-local-variable 'resize-minibuffer-window-max-height)
276 \(setq resize-minibuffer-window-max-height 3))))
278 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
279 iswitchb is running. Copied from `icomplete-minibuffer-setup-hook'.")
281 (defvar iswitchb-eoinput 1
282 "Point where minibuffer input ends and completion info begins.
283 Copied from `icomplete-eoinput'.")
284 (make-variable-buffer-local 'iswitchb-eoinput)
287 (defvar iswitchb-buflist nil
288 "Stores the current list of buffers that will be searched through.
289 The list is ordered, so that the most recent buffers come first,
290 although by default, the buffers visible in the current frame are put
291 at the end of the list. Created by `iswitchb-make-buflist'.")
293 ;; todo -- is this necessary?
295 (defvar iswitchb-use-mycompletion nil
296 "Non-nil means use `iswitchb-buffer' completion feedback.
297 Should only be set to t by iswitchb functions, so that it doesn't
298 interfere with other minibuffer usage.")
300 (defvar iswitchb-change-word-sub nil
301 "Private variable used by `iswitchb-word-matching-substring'.")
304 (defvar iswitchb-common-match-string nil
305 "Stores the string that is common to all matching buffers.")
308 (defvar iswitchb-rescan nil
309 "Non-nil means we need to regenerate the list of matching buffers.")
311 (defvar iswitchb-text nil
312 "Stores the users string as it is typed in.")
314 (defvar iswitchb-matches nil
315 "List of buffers currenly matching `iswitchb-text'.")
317 (defvar iswitchb-default-buffer nil
318 "Default buffer to switch to.")
320 (defvar iswitchb-mode-map nil
321 "Keymap for `iswitchb-buffer'.")
323 (defvar iswitchb-history nil
324 "History of buffers selected using `iswitchb-buffer'.")
326 (defvar iswitchb-exit nil
327 "Flag to monitor how `iswitchb-buffer' exits.
328 If equal to `takeprompt', we use the prompt as the buffer name to be
329 selected.")
331 (defvar iswitchb-buffer-ignore-orig nil
332 "Stores original value of `iswitchb-buffer-ignore'.")
334 (defvar iswitchb-xemacs (string-match "XEmacs" (emacs-version))
335 "Non-nil if we are running XEmacs. Otherwise, assume we are running Emacs.")
338 ;;; FUNCTIONS
341 ;;; ISWITCHB KEYMAP
342 (defun iswitchb-define-mode-map ()
343 "Set up the keymap for `iswitchb-buffer'."
344 (interactive)
345 (let (map)
346 ;; generated every time so that it can inheret new functions.
347 ;;(or iswitchb-mode-map
349 (setq map (copy-keymap minibuffer-local-map))
350 (define-key map "?" 'iswitchb-completion-help)
351 (define-key map "\C-s" 'iswitchb-next-match)
352 (define-key map "\C-r" 'iswitchb-prev-match)
353 (define-key map "\t" 'iswitchb-complete)
354 (define-key map "\C-j" 'iswitchb-select-buffer-text)
355 (define-key map "\C-t" 'iswitchb-toggle-regexp)
356 ;;(define-key map "\C-a" 'iswitchb-toggle-ignore)
357 (define-key map "\C-c" 'iswitchb-toggle-case)
358 (setq iswitchb-mode-map map)
359 (run-hooks 'iswitchb-define-mode-map-hook)
364 ;;; MAIN FUNCTION
365 (defun iswitchb ()
366 "Switch to buffer matching a substring.
367 As you type in a string, all of the buffers matching the string are
368 displayed. When you have found the buffer you want, it can then be
369 selected. As you type, most keys have their normal keybindings,
370 except for the following:
371 \\<iswitchb-mode-map>
373 RET Select the buffer at the front of the list of matches. If the
374 list is empty, possibly prompt to create new buffer.
376 \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
377 If no buffer is found, prompt for a new one.
379 \\[iswitchb-next-match] Put the first element at the end of the list.
380 \\[iswitchb-prev-match] Put the last element at the start of the list.
381 \\[iswitchb-complete] Complete a common suffix to the current string that
382 matches all buffers. If there is only one match, select that buffer.
383 If there is no common suffix, show a list of all matching buffers
384 in a separate window.
385 \\[iswitchb-toggle-regexp] Toggle rexep searching.
386 \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
387 \\[iswitchb-completion-help] Show list of matching buffers in separate window."
388 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
389 ;;`iswitchb-buffer-ignore')
391 (let
393 prompt
394 buf-sel
395 iswitchb-final-text
396 (minibuffer-confirm-incomplete nil) ;XEmacs todo: prevent `;confirm'
397 (icomplete-mode nil) ;; prevent icomplete starting up
398 (minibuffer-local-completion-map minibuffer-local-completion-map)
399 ;; can only use fonts if they have been bound.
400 (iswitchb-use-fonts (and iswitchb-use-fonts
401 (boundp 'font-lock-comment-face)
402 (boundp 'font-lock-function-name-face)))
405 (iswitchb-define-mode-map)
406 (setq minibuffer-local-completion-map iswitchb-mode-map)
408 (setq iswitchb-exit nil)
409 (setq iswitchb-rescan t)
410 (setq iswitchb-text "")
411 (setq iswitchb-matches nil)
412 ;;(setq iswitchb-default-buffer (buffer-name (other-buffer)))
413 (setq prompt (format "iswitch "))
414 (iswitchb-make-buflist)
415 (setq iswitchb-default-buffer (format "%s" (car iswitchb-buflist)))
417 ;; highlight the default.
418 (if iswitchb-use-fonts
419 (put-text-property 0 (length iswitchb-default-buffer)
420 'face
421 'font-lock-function-name-face
422 iswitchb-default-buffer))
424 ;; prompt the user for the buffer name
425 (setq iswitchb-final-text (completing-read prompt
426 ;;nil
427 '(("dummy".1))
428 ;;("2".2) ("3".3))
429 nil nil
430 nil;init string
431 'iswitchb-history))
432 ;;(message "chosen text %s" iswitchb-final-text)
433 ;; Choose the buffer name: either the text typed in, or the head
434 ;; of the list of matches
435 (if (or
436 (eq iswitchb-exit 'takeprompt)
437 (null iswitchb-matches))
438 (setq buf-sel iswitchb-final-text)
439 ;; else take head of list
440 (setq buf-sel (car iswitchb-matches)))
442 ;; Or possibly choose the default buffer
443 (if (equal iswitchb-final-text "")
444 (setq buf-sel iswitchb-default-buffer))
446 ;; View the buffer
447 (message "go to buf %s" buf-sel)
449 (if (get-buffer buf-sel)
450 ;; buffer exists, so view it and then exit
451 (iswitchb-visit-buffer buf-sel)
452 ;; else buffer doesnt exist
453 (iswitchb-possible-new-buffer buf-sel))
458 ;;; COMPLETION CODE
460 (defun iswitchb-set-common-completion ()
461 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
462 The result is stored in `iswitchb-common-match-string'."
464 (let* (val)
465 (setq iswitchb-common-match-string nil)
466 (if (and iswitchb-matches
467 (stringp iswitchb-text)
468 (> (length iswitchb-text) 0))
469 (if (setq val (iswitchb-find-common-substring
470 iswitchb-matches iswitchb-text))
471 (setq iswitchb-common-match-string val)))
476 (defun iswitchb-complete ()
477 "Try and complete the current pattern amongst the buffer names."
478 (interactive)
479 (let (res)
480 (cond ((not iswitchb-matches)
482 ;; todo
483 ;;(message "No buffer completions.")
484 ;;(sit-for 0.3)
485 (iswitchb-completion-help)
488 ((eq 1 (length iswitchb-matches))
489 ;; only one choice, so select it.
490 (exit-minibuffer))
493 ;; else there could be some completions
495 (setq res (iswitchb-find-common-substring
496 iswitchb-matches iswitchb-text))
497 (if (and (not (memq res '(t nil)))
498 (not (eq res iswitchb-text)))
499 ;; found something to complete, so put it in the minibuff.
500 (progn
501 (setq iswitchb-rescan nil)
502 (delete-region (point-min) (point))
503 (insert res))
504 ;; else nothing to complete
505 (iswitchb-completion-help)
512 ;;; TOGGLE FUNCTIONS
514 (defun iswitchb-toggle-case ()
515 "Toggle the value of `iswitchb-case'."
516 (interactive)
517 (setq iswitchb-case (not iswitchb-case))
518 ;; ask for list to be regenerated.
519 (setq iswitchb-rescan t)
522 (defun iswitchb-toggle-regexp ()
523 "Toggle the value of `iswitchb-regexp'."
524 (interactive)
525 (setq iswitchb-regexp (not iswitchb-regexp))
526 ;; ask for list to be regenerated.
527 (setq iswitchb-rescan t)
531 (defun iswitchb-toggle-ignore ()
532 "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
533 (interactive)
534 (if iswitchb-buffer-ignore
535 (progn
536 (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
537 (setq iswitchb-buffer-ignore nil)
539 ;; else
540 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig)
542 ;; ask for list to be regenerated.
543 (setq iswitchb-rescan t)
547 (defun iswitchb-select-buffer-text ()
548 "Select the buffer named by the prompt.
549 If no buffer exactly matching the prompt exists, maybe create a new one."
550 (interactive)
551 (setq iswitchb-exit 'takeprompt)
552 (exit-minibuffer))
555 (defun iswitchb-next-match ()
556 "Put first element of `iswitchb-matches' at the end of the list."
557 (interactive)
558 (let ((tmp (car iswitchb-matches)))
559 (setq iswitchb-matches (cdr iswitchb-matches))
560 (setq iswitchb-matches (append iswitchb-matches (list tmp)))
561 (setq iswitchb-rescan nil)
564 (defun iswitchb-prev-match ()
565 "Put last element of `iswitchb-matches' at the front of the list."
566 (interactive)
567 (setq iswitchb-matches (iswitchb-rotate-list iswitchb-matches))
568 (setq iswitchb-rescan nil)
574 ;;; CREATE LIST OF ALL CURRENT BUFFERS
576 (defun iswitchb-make-buflist ()
577 "Set `iswitchb-buflist' to the current list of buffers.
578 Currently visible buffers are put at the end of the list."
579 (setq iswitchb-buflist
580 (let (buflist
581 iswitchb-current-buffers)
582 (setq iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
583 (setq buflist (mapcar 'buffer-name (buffer-list)))
584 (setq buflist (delq nil
585 (mapcar
586 '(lambda (x)
587 (if (not (iswitchb-ignore-buffername-p x))
589 buflist)))
590 (mapcar 'iswitchb-to-end iswitchb-current-buffers)
592 (run-hooks 'iswitchb-make-buflist-hook)
593 buflist)))
596 (defun iswitchb-to-end (elem)
597 "Move ELEM to the end of BUFLIST."
598 (setq buflist (delq elem buflist))
599 ;;(message "removing %s" elem)
600 (setq buflist (append buflist (list elem))))
605 (defun iswitchb-get-buffers-in-frames (&optional current)
606 "Return the list of buffers that are visible in the current frame.
607 If optional argument `current' is given, restrict searching to the
608 current frame, rather than all frames, regardless of value of
609 `iswitchb-all-frames'."
610 (let ((iswitchb-bufs-in-frame nil))
611 (walk-windows 'iswitchb-get-bufname nil
612 (if current
614 iswitchb-all-frames))
615 iswitchb-bufs-in-frame))
618 (defun iswitchb-get-bufname (win)
619 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
620 (setq iswitchb-bufs-in-frame
621 (cons (buffer-name (window-buffer win))
622 iswitchb-bufs-in-frame)))
625 ;;; FIND MATCHING BUFFERS
627 (defun iswitchb-set-matches ()
628 "Set `iswitchb-matches' to the list of buffers matching prompt."
629 (if iswitchb-rescan
630 (setq iswitchb-matches
631 (let* ((buflist iswitchb-buflist)
633 (if (> (length iswitchb-text) 0)
634 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
635 buflist)
636 ;; else no text, no matches
637 nil)))))
639 (defun iswitchb-get-matched-buffers (regexp &optional string-format buffer-list)
640 "Return matched buffers.
641 If STRING-FORMAT is non-nil, consider REGEXP as string.
642 BUFFER-LIST can be list of buffers or list of strings."
643 (let* ((case-fold-search iswitchb-case)
644 ;; need reverse since we are building up list backwards
645 (list (reverse buffer-list))
646 (do-string (stringp (car list)))
647 name
650 (mapcar
651 (function
652 (lambda (x)
654 (if do-string
655 (setq name x) ;We already have the name
656 (setq name (buffer-name x)))
658 (cond
659 ((and (or (and string-format (string-match regexp name))
660 (and (null string-format)
661 (string-match (regexp-quote regexp) name)))
663 ;; todo (not (iswitchb-ignore-buffername-p name))
665 (setq ret (cons name ret))
666 ))))
667 list)
674 (defun iswitchb-ignore-buffername-p (bufname)
675 "Return t if the buffer BUFNAME should be ignored."
676 (let ((data (match-data))
677 (re-list iswitchb-buffer-ignore)
678 ignorep
679 nextstr
681 (while re-list
682 (setq nextstr (car re-list))
683 (cond
684 ((stringp nextstr)
685 (if (string-match nextstr bufname)
686 (progn
687 (setq ignorep t)
688 (setq re-list nil))))
689 ((fboundp nextstr)
690 (if (funcall nextstr bufname)
691 (progn
692 (setq ignorep t)
693 (setq re-list nil))
696 (setq re-list (cdr re-list)))
697 (store-match-data data)
699 ;; return the result
700 ignorep)
705 (defun iswitchb-word-matching-substring (word)
706 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
707 If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
708 (let ((case-fold-search iswitchb-case))
709 (let ((m (string-match iswitchb-change-word-sub word)))
710 (if m
711 (substring word m)
712 ;; else no match
713 nil))))
720 (defun iswitchb-find-common-substring (lis subs)
721 "Return common string following SUBS in each element of LIS."
722 (let (res
723 alist
724 iswitchb-change-word-sub
726 (setq iswitchb-change-word-sub
727 (if iswitchb-regexp
728 subs
729 (regexp-quote subs)))
730 (setq res (mapcar 'iswitchb-word-matching-substring lis))
731 (setq res (delq nil res)) ;; remove any nil elements (shouldnt happen)
732 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
734 ;; try-completion returns t if there is an exact match.
735 (let ((completion-ignore-case iswitchb-case))
737 (try-completion subs alist)
741 (defun iswitchb-makealist (res)
742 "Return dotted pair (RES . 1)."
743 (cons res 1))
745 ;; from Wayne Mesard <wmesard@esd.sgi.com>
746 (defun iswitchb-rotate-list (lis)
747 "Destructively removes the last element from LIS.
748 Return the modified list with the last element prepended to it."
749 (if (<= (length lis) 1)
751 (let ((las lis)
752 (prev lis))
753 (while (consp (cdr las))
754 (setq prev las
755 las (cdr las)))
756 (setcdr prev nil)
757 (cons (car las) lis))
761 (defun iswitchb-completion-help ()
762 "Show possible completions in a *Buffer Completions* buffer."
763 ;; we could allow this buffer to be used to select match, but I think
764 ;; choose-completion-string will need redifining, so it just inserts
765 ;; choice with out any previous input.
766 (interactive)
767 (let ((completion-setup-hook nil) ;disable fancy highlight/selection.
769 (with-output-to-temp-buffer "*Buffer Completions*"
770 (if iswitchb-xemacs
772 ;; XEmacs extents are put on by default, doesn't seem to be
773 ;; any way of switching them off.
774 (display-completion-list (if iswitchb-matches
775 iswitchb-matches
776 iswitchb-buflist)
777 :help-string "iswitchb "
778 :activate-callback
779 '(lambda (x y z)
780 (message "doesnt work yet, sorry!")))
781 ;; else running Emacs
782 (display-completion-list (if iswitchb-matches
783 iswitchb-matches
784 iswitchb-buflist))
785 ))))
787 ;;; VISIT CHOSEN BUFFER
788 (defun iswitchb-visit-buffer (buffer)
789 "Visit buffer named BUFFER according to `iswitchb-method'."
790 (let* (win newframe)
791 (cond
792 ((eq iswitchb-method 'samewindow)
793 (switch-to-buffer buffer))
795 ((memq iswitchb-method '(always-frame maybe-frame))
796 (cond
797 ((and (setq win (iswitchb-window-buffer-p buffer))
798 (or (eq iswitchb-method 'always-frame)
799 (y-or-n-p "Jump to frame? ")))
800 (setq newframe (window-frame win))
801 (raise-frame newframe)
802 (select-frame newframe)
803 (select-window win)
804 (if (not iswitchb-xemacs)
805 ;; reposition mouse to make frame active. not needed in XEmacs
806 ;; This line came from the other-frame defun in Emacs.
807 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
810 ;; No buffer in other frames...
811 (switch-to-buffer buffer)
816 ((eq iswitchb-method 'otherwindow)
817 (switch-to-buffer-other-window buffer))
819 ((eq iswitchb-method 'display)
820 (display-buffer buffer))
822 ((eq iswitchb-method 'otherframe)
823 (progn
824 (switch-to-buffer-other-frame buffer)
825 (if (not iswitchb-xemacs)
826 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
828 ) )))
830 (defun iswitchb-possible-new-buffer (buf)
831 "Possibly create and visit a new buffer called BUF."
833 (let ((newbufcreated))
834 (if (and iswitchb-newbuffer
836 (not iswitchb-prompt-newbuffer)
838 (and iswitchb-prompt-newbuffer
839 (y-or-n-p
840 (format
841 "No buffer matching `%s', create one? "
842 buf)))))
843 ;; then create a new buffer
844 (progn
845 (setq newbufcreated (get-buffer-create buf))
846 (if (fboundp 'set-buffer-major-mode)
847 (set-buffer-major-mode newbufcreated))
848 (iswitchb-visit-buffer newbufcreated))
849 ;; else wont create new buffer
850 (message (format "no buffer matching `%s'" buf))
853 (defun iswitchb-window-buffer-p (buffer)
854 "Return window pointer if BUFFER is visible in another frame.
855 If BUFFER is visible in the current frame, return nil."
856 (interactive)
857 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
858 ;;If the buffer is visible in current frame, return nil
859 (if (memq buffer blist)
861 ;; maybe in other frame...
862 (get-buffer-window buffer 'visible)
865 ;;;###autoload
866 (defun iswitchb-default-keybindings ()
867 "Set up default keybindings for `iswitchb-buffer'.
868 Call this function to override the normal bindings."
869 (interactive)
870 (global-set-key "\x18b" 'iswitchb-buffer)
871 (global-set-key "\x184b" 'iswitchb-buffer-other-window)
872 (global-set-key "\x184\x0f" 'iswitchb-display-buffer)
873 (global-set-key "\x185b" 'iswitchb-buffer-other-frame))
877 ;;;###autoload
878 (defun iswitchb-buffer ()
879 "Switch to another buffer.
881 The buffer name is selected interactively by typing a substring. The
882 buffer is displayed according to `iswitchb-default-method' -- the
883 default is to show it in the same window, unless it is already visible
884 in another frame.
885 For details of keybindings, do `\\[describe-function] iswitchb'."
886 (interactive)
887 (setq iswitchb-method iswitchb-default-method)
888 (iswitchb-entry))
891 ;;;###autoload
892 (defun iswitchb-buffer-other-window ()
893 "Switch to another buffer and show it in another window.
894 The buffer name is selected interactively by typing a substring.
895 For details of keybindings, do `\\[describe-function] iswitchb'."
896 (interactive)
897 (setq iswitchb-method 'otherwindow)
898 (iswitchb-entry))
902 ;;;###autoload
903 (defun iswitchb-display-buffer ()
904 "Display a buffer in another window but don't select it.
905 The buffer name is selected interactively by typing a substring.
906 For details of keybindings, do `\\[describe-function] iswitchb'."
907 (interactive)
908 (setq iswitchb-method 'display)
909 (iswitchb-entry))
913 ;;;###autoload
914 (defun iswitchb-buffer-other-frame ()
915 "Switch to another buffer and show it in another frame.
916 The buffer name is selected interactively by typing a substring.
917 For details of keybindings, do `\\[describe-function] iswitchb'."
918 (interactive)
919 (setq iswitchb-method 'otherframe)
920 (iswitchb-entry))
924 (defun iswitchb-entry ()
925 "Simply fall into `iswitchb' -- the main function."
926 (iswitchb))
932 ;;; XEMACS HACK FOR SHOWING DEFAULT BUFFER
934 ;; The first time we enter the minibuffer, Emacs puts up the default
935 ;; buffer to switch to, but XEmacs doesnt -- presumably there is a
936 ;; subtle difference in the two, either in icomplete or somewhere
937 ;; else. The default is shown for both whenever we delete all of our
938 ;; text though, indicating its just a problem the first time we enter
939 ;; the function. To solve this, we use another entry hook for emacs
940 ;; to show the default the first time we enter the minibuffer.
942 (defun iswitchb-init-Xemacs-trick ()
943 "Display default buffer when first entering minibuffer.
944 This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
945 (if (iswitchb-entryfn-p)
946 (progn
947 (iswitchb-show-default-buffer)
948 (goto-char (point-min)))))
950 ;; add this hook for Xemacs only.
951 (if iswitchb-xemacs
952 (add-hook 'iswitchb-minibuffer-setup-hook
953 'iswitchb-init-Xemacs-trick))
956 ;;; XEMACS / BACKSPACE key
957 ;; For some reason, if the backspace key is pressed in xemacs, the
958 ;; line gets confused, so I've added a simple key definition to make
959 ;; backspace act like the normal delete key.
961 (defun iswitchb-xemacs-backspacekey ()
962 "Bind backspace to `backward-delete-char'."
963 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
964 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word)
968 (if iswitchb-xemacs
969 (add-hook 'iswitchb-define-mode-map-hook
970 'iswitchb-xemacs-backspacekey))
974 ;;; ICOMPLETE TYPE CODE
976 (defun iswitchb-exhibit ()
977 "Find matching buffers and display a list in the minibuffer.
978 Copied from `icomplete-exhibit' with two changes:
979 1. It prints a default buffer name when there is no text yet entered.
980 2. It calls my completion routine rather than the standard completion."
982 (if iswitchb-use-mycompletion
983 (let ((contents (buffer-substring (point-min)(point-max)))
984 (buffer-undo-list t))
985 (save-excursion
986 (goto-char (point-max))
987 ; Register the end of input, so we
988 ; know where the extra stuff
989 ; (match-status info) begins:
990 (if (not (boundp 'iswitchb-eoinput))
991 ;; In case it got wiped out by major mode business:
992 (make-local-variable 'iswitchb-eoinput))
993 (setq iswitchb-eoinput (point))
994 ;; Update the list of matches
995 (setq iswitchb-text contents)
996 (iswitchb-set-matches)
997 (setq iswitchb-rescan t)
998 (iswitchb-set-common-completion)
1000 ;; Insert the match-status information:
1001 (if (> (point-max) 1)
1002 (insert-string
1003 (iswitchb-completions
1004 contents
1005 minibuffer-completion-table
1006 minibuffer-completion-predicate
1007 (not minibuffer-completion-confirm)))
1008 ;; else put in default
1009 (iswitchb-show-default-buffer))
1010 ))))
1012 (defun iswitchb-show-default-buffer ()
1013 "Insert the default buffer to switch to."
1014 ;; insert done this way to preserve any text-propertes.
1015 (insert (concat " {" iswitchb-default-buffer "} [Default]")))
1017 (defun iswitchb-completions
1018 (name candidates predicate require-match)
1019 "Return the string that is displayed after the user's text.
1020 Modified from `icomplete-completions'."
1022 (let ((comps iswitchb-matches)
1023 ; "-determined" - only one candidate
1024 (open-bracket-determined (if require-match "(" "["))
1025 (close-bracket-determined (if require-match ")" "]"))
1026 ;"-prospects" - more than one candidate
1027 (open-bracket-prospects "{")
1028 (close-bracket-prospects "}")
1029 first
1032 (if (and iswitchb-use-fonts comps)
1033 (progn
1034 (setq first (car comps))
1035 (setq first (format "%s" first))
1036 (put-text-property 0 (length first) 'face
1037 (if (eq (length comps) 1)
1038 'font-lock-comment-face
1039 'font-lock-function-name-face)
1040 first)
1041 (setq comps (cons first (cdr comps)))
1044 (cond ((null comps) (format " %sNo match%s"
1045 open-bracket-determined
1046 close-bracket-determined))
1048 ((null (cdr comps)) ;one match
1049 (concat (if (and (> (length (car comps))
1050 (length name)))
1051 (concat open-bracket-determined
1052 ;; when there is one match, show the
1053 ;; matching buffer name in full
1054 (car comps)
1055 close-bracket-determined)
1057 (if (not iswitchb-use-fonts) " [Matched]")
1059 (t ;multiple matches
1060 (let* (
1061 ;;(most (try-completion name candidates predicate))
1062 (most nil)
1063 (most-len (length most))
1064 most-is-exact
1065 first
1066 (alternatives
1067 (apply
1068 (function concat)
1069 (cdr (apply
1070 (function nconc)
1071 (mapcar '(lambda (com)
1072 (if (= (length com) most-len)
1073 ;; Most is one exact match,
1074 ;; note that and leave out
1075 ;; for later indication:
1076 (progn
1077 (setq most-is-exact t)
1079 (list ","
1080 (substring com
1081 most-len))))
1082 comps))))))
1084 (concat
1086 ;; put in common completion item -- what you get by
1087 ;; pressing tab
1088 (if (> (length iswitchb-common-match-string) (length name))
1089 (concat open-bracket-determined
1090 (substring iswitchb-common-match-string
1091 (length name))
1092 close-bracket-determined)
1094 ;; end of partial matches...
1096 ;; think this bit can be ignored.
1097 (and (> most-len (length name))
1098 (concat open-bracket-determined
1099 (substring most (length name))
1100 close-bracket-determined))
1102 ;; list all alternatives
1103 open-bracket-prospects
1104 (if most-is-exact
1105 (concat "," alternatives)
1106 alternatives)
1107 close-bracket-prospects)))
1110 (defun iswitchb-minibuffer-setup ()
1111 "Set up minibuffer for `iswitchb-buffer'.
1112 Copied from `icomplete-minibuffer-setup-hook'."
1113 (if (iswitchb-entryfn-p)
1114 (progn
1116 (make-local-variable 'iswitchb-use-mycompletion)
1117 (setq iswitchb-use-mycompletion t)
1118 (make-local-hook 'pre-command-hook)
1119 (add-hook 'pre-command-hook
1120 'iswitchb-pre-command
1121 nil t)
1122 (make-local-hook 'post-command-hook)
1123 (add-hook 'post-command-hook
1124 'iswitchb-post-command
1125 nil t)
1127 (run-hooks 'iswitchb-minibuffer-setup-hook)
1132 (defun iswitchb-pre-command ()
1133 "Run before command in `iswitchb-buffer'."
1134 (iswitchb-tidy))
1137 (defun iswitchb-post-command ()
1138 "Run after command in `iswitchb-buffer'."
1139 (iswitchb-exhibit)
1144 (defun iswitchb-tidy ()
1145 "Remove completions display, if any, prior to new user input.
1146 Copied from `icomplete-tidy'."
1148 (if (and (boundp 'iswitchb-eoinput)
1149 iswitchb-eoinput)
1151 (if (> iswitchb-eoinput (point-max))
1152 ;; Oops, got rug pulled out from under us - reinit:
1153 (setq iswitchb-eoinput (point-max))
1154 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1155 (delete-region iswitchb-eoinput (point-max))))
1157 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1158 (make-local-variable 'iswitchb-eoinput)
1159 (setq iswitchb-eoinput 1)))
1162 (defun iswitchb-entryfn-p ()
1163 "Return non-nil if `this-command' shows we are using `iswitchb-buffer'."
1164 (and (symbolp this-command) ; ignore lambda functions
1165 (memq this-command
1166 '(iswitchb-buffer
1167 iswitchb-buffer-other-frame
1168 iswitchb-display-buffer
1169 iswitchb-buffer-other-window))))
1171 (defun iswitchb-summaries-to-end ()
1172 "Move the summaries to the end of the list.
1173 This is an example function which can be hooked on to
1174 `iswitchb-make-buflist-hook'. Any buffer matching the regexps
1175 `Summary' or `output\*$'are put to the end of the list."
1176 (let ((summaries (delq nil (mapcar
1177 '(lambda (x)
1178 (if (or
1179 (string-match "Summary" x)
1180 (string-match "output\\*$" x))
1182 buflist)
1185 (mapcar 'iswitchb-to-end summaries)))
1189 ;;; HOOKS
1190 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1192 (provide 'iswitchb)
1194 ;;; iswitchb.el ends here