]> code.delx.au - gnu-emacs/blob - lisp/iswitchb.el
(iswitchb-get-bufname): Only add buffer of current
[gnu-emacs] / lisp / iswitchb.el
1 ;;; iswitchb.el --- switch between buffers using substrings
2
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4
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
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Installation:
28
29 ;; To get the functions in this package bound to keys, do
30 ;; (iswitchb-default-keybindings)
31
32 ;;; Commentary:
33
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.
44
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.
50
51 ;;; Example
52
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 list of all the buffers
56 ;;
57 ;; iswitch {123456,123}
58 ;;
59 ;; If I then press 2:
60 ;; iswitch 2[3]{123456,123}
61 ;;
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}
71 ;;
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.
76 ;;
77 ;;However, If I type 4, I only have one match left:
78 ;; iswitch 234[123456] [Matched]
79 ;;
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.
83 ;;
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".
88 ;;
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).
94
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.
98
99 ;; The buffer at the head of the list can be killed by pressing C-k.
100 ;; If the buffer needs saving, you will be queried before the buffer
101 ;; is killed.
102
103 ;; If you find that the file you are after is not in a buffer, you can
104 ;; press C-x C-f to immediately drop into find-file.
105
106 ;;
107 ;; See the doc string of iswitchb for full keybindings and features.
108 ;; (describe-function 'iswitchb)
109
110 ;;; Customisation
111
112 ;; See the User Variables section below for easy ways to change the
113 ;; functionality of the program. These are accessible using the
114 ;; custom package.
115 ;; To modify the keybindings, use the hook provided. For example:
116 ;;(add-hook 'iswitchb-define-mode-map-hook
117 ;; 'iswitchb-my-keys)
118 ;;
119 ;;(defun iswitchb-my-keys ()
120 ;; "Add my keybings for iswitchb."
121 ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match)
122 ;; )
123 ;;
124 ;; Seeing all the matching buffers.
125 ;;
126 ;; If you have many matching buffers, they may not all fit onto one
127 ;; line of the minibuffer. In this case, you should use rsz-mini
128 ;; (resize-minibuffer-mode). You can also limit iswitchb so that it
129 ;; only shows a certain number of lines -- see the documentation for
130 ;; `iswitchb-minibuffer-setup-hook'.
131
132
133 ;; Changing the list of buffers.
134
135 ;; By default, the list of current buffers is most recent first,
136 ;; oldest last, with the exception that the buffers visible in the
137 ;; current frame are put at the end of the list. A hook exists to
138 ;; allow other functions to order the list. For example, if you add:
139 ;;
140 ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
141 ;;
142 ;; then all buffers matching "Summary" are moved to the end of the
143 ;; list. (I find this handy for keeping the INBOX Summary and so on
144 ;; out of the way.) It also moves buffers matching "output\*$" to the
145 ;; end of the list (these are created by AUC TeX when compiling.)
146 ;; Other functions could be made available which alter the list of
147 ;; matching buffers (either deleting or rearranging elements.)
148
149 ;; Font-Lock
150
151 ;; If you have font-lock loaded, the first matching buffer is
152 ;; highlighted. To switch this off, set (setq iswitchb-use-fonts nil)
153 ;; I don't use font-lock that much, so I've hardcoded the faces. If
154 ;; this is too harsh, let me know. Colouring of the matching buffer
155 ;; name was suggested by Carsten Dominik (dominik@strw.leidenuniv.nl)
156
157 ;;; Comparison with iswitch-buffer
158
159 ;; This package is a rewrite of iswitch-buffer, using the minibuffer
160 ;; rather than the echo area. The advantages of using the minibuffer
161 ;; are several:
162 ;; o minibuffer has more powerful editing facilities
163 ;; o doesnt interfere with other packages that use the echo area
164 ;; o *Messages* buffer doesnt get filled up with all of the messages that
165 ;; go to the modeline
166 ;; o cursor is in the minibuffer, which somehow looks right.
167 ;; o minibuffer can be resized dynamically to show all the possible matching
168 ;; buffers rather than just the first line's worth (using rsz-mini).
169 ;;
170 ;; Disadvantages:
171 ;; o cant change the prompt to indicate status of searching (eg whether
172 ;; regexp searching is currently on).
173
174
175 ;;; TODO
176 ;; Could this selection also be used for other buffer selection
177 ;; routines, such as append-to-buffer and kill-buffer?
178
179 ;; Pressing Tab key twice (without completion) does not scroll the
180 ;; list of buffers.
181
182 ;;; Acknowledgements
183
184 ;; Thanks to Jari Aalto <jari.aalto@poboxes.com> for help with the
185 ;; first version of this package, iswitch-buffer. Thanks also to many
186 ;; others for testing earlier versions.
187
188 ;;; Code:
189
190 ;; Set up the custom library.
191 ;; taken from http://www.dina.kvl.dk/~abraham/custom/
192 (eval-and-compile
193 (condition-case ()
194 (require 'custom)
195 (error nil))
196 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
197 nil ;; We've got what we needed
198 ;; We have the old custom-library, hack around it!
199 (defmacro defgroup (&rest args)
200 nil)
201 (defmacro defcustom (var value doc &rest args)
202 (` (defvar (, var) (, value) (, doc))))))
203
204 ;;; User Variables
205 ;;
206 ;; These are some things you might want to change.
207
208 (defgroup iswitchb nil
209 "switch between buffers using substrings."
210 :group 'extensions
211 ;; These links are to be added in later versions of custom and
212 ;; so are currently commented out.
213 :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el")
214 :link '(emacs-library-link :tag "Lisp File" "iswitchb.el")
215 )
216
217
218 (defcustom iswitchb-case case-fold-search
219 "*Non-nil if searching of buffer names should ignore case."
220 :type 'boolean
221 :group 'iswitchb)
222
223 (defcustom iswitchb-buffer-ignore
224 '("^ ")
225 "*List of regexps or functions matching buffer names to ignore.
226 For example, traditional behavior is not to list buffers whose names begin
227 with a space, for which the regexp is `^ '. See the source file for
228 example functions that filter buffernames."
229 :type '(repeat regexp)
230 :group 'iswitchb)
231
232
233 ;;; Examples for setting the value of iswitchb-buffer-ignore
234 ;(defun -c-mode (name)
235 ; "Ignore all c mode buffers -- example function for iswitchb."
236 ; (save-excursion
237 ; (set-buffer name)
238 ; (string-match "^C$" mode-name)))
239
240 ;(setq iswitchb-buffer-ignore '("^ " ignore-c-mode))
241 ;(setq iswitchb-buffer-ignore '("^ " "\\.c$" "\\.h$"))
242
243 (defcustom iswitchb-default-method 'always-frame
244 "*How to switch to new buffer when using `iswitchb-buffer'.
245 Possible values:
246 `samewindow' Show new buffer in same window
247 `otherwindow' Show new buffer in another window (same frame)
248 `display' Display buffer in another window without switching to it
249 `otherframe' Show new buffer in another frame
250 `maybe-frame' If a buffer is visible in another frame, prompt to ask if you
251 you want to see the buffer in the same window of the current
252 frame or in the other frame.
253 `always-frame' If a buffer is visible in another frame, raise that
254 frame. Otherwise, visit the buffer in the same window."
255 :type '(choice (const :tag "samewindow" samewindow)
256 (const :tag "otherwindow" otherwindow)
257 (const :tag "display" display)
258 (const :tag "otherframe" otherframe)
259 (const :tag "maybe-frame" maybe-frame)
260 (const :tag "always-frame" always-frame))
261 :group 'iswitchb)
262
263
264 (defcustom iswitchb-regexp nil
265 "*Non-nil means that `iswitchb' will do regexp matching.
266 Value can be toggled within `iswitchb'."
267 :type 'boolean
268 :group 'iswitchb)
269
270
271 (defcustom iswitchb-newbuffer t
272 "*Non-nil means create new buffer if no buffer matches substring.
273 See also `iswitchb-prompt-newbuffer'."
274 :type 'boolean
275 :group 'iswitchb)
276
277
278 (defcustom iswitchb-prompt-newbuffer t
279 "*Non-nil means prompt user to confirm before creating new buffer.
280 See also `iswitchb-newbuffer'."
281 :type 'boolean
282 :group 'iswitchb)
283
284
285 (defcustom iswitchb-define-mode-map-hook nil
286 "*Hook to define keys in `iswitchb-mode-map' for extra keybindings."
287 :type 'hook
288 :group 'iswitchb)
289
290
291
292 (defcustom iswitchb-use-fonts t
293 "*Non-nil means use fonts for showing first match."
294 :type 'boolean
295 :group 'iswitchb)
296
297
298 (defcustom iswitchb-make-buflist-hook nil
299 "*Hook to run when list of matching buffers is created."
300 :type 'hook
301 :group 'iswitchb)
302
303
304
305 (defvar iswitchb-method nil
306 "*Stores the method for viewing the selected buffer.
307 Its value is one of `samewindow', `otherwindow', `display', `otherframe',
308 `maybe-frame' or `always-frame'. See `iswitchb-default-method' for
309 details of values.")
310
311 (defvar iswitchb-all-frames 'visible
312 "*Argument to pass to `walk-windows' when finding visible buffers.
313 See documentation of `walk-windows' for useful values.")
314
315
316
317 ;; Do we need the variable iswitchb-use-mycompletion?
318
319
320 ;;; Internal Variables
321 (defvar iswitchb-minibuffer-setup-hook nil
322 "Iswitchb-specific customization of minibuffer setup.
323
324 This hook is run during minibuffer setup iff `iswitchb' will be active.
325 It is intended for use in customizing iswitchb for interoperation
326 with other packages. For instance:
327
328 \(add-hook 'iswitchb-minibuffer-setup-hook
329 \(function
330 \(lambda ()
331 \(make-local-variable 'resize-minibuffer-window-max-height)
332 \(setq resize-minibuffer-window-max-height 3))))
333
334 will constrain rsz-mini to a maximum minibuffer height of 3 lines when
335 iswitchb is running. Copied from `icomplete-minibuffer-setup-hook'.")
336
337 (defvar iswitchb-eoinput 1
338 "Point where minibuffer input ends and completion info begins.
339 Copied from `icomplete-eoinput'.")
340 (make-variable-buffer-local 'iswitchb-eoinput)
341
342
343 (defvar iswitchb-buflist nil
344 "Stores the current list of buffers that will be searched through.
345 The list is ordered, so that the most recent buffers come first,
346 although by default, the buffers visible in the current frame are put
347 at the end of the list. Created by `iswitchb-make-buflist'.")
348
349 ;; todo -- is this necessary?
350
351 (defvar iswitchb-use-mycompletion nil
352 "Non-nil means use `iswitchb-buffer' completion feedback.
353 Should only be set to t by iswitchb functions, so that it doesn't
354 interfere with other minibuffer usage.")
355
356 (defvar iswitchb-change-word-sub nil
357 "Private variable used by `iswitchb-word-matching-substring'.")
358
359
360 (defvar iswitchb-common-match-string nil
361 "Stores the string that is common to all matching buffers.")
362
363
364 (defvar iswitchb-rescan nil
365 "Non-nil means we need to regenerate the list of matching buffers.")
366
367 (defvar iswitchb-text nil
368 "Stores the users string as it is typed in.")
369
370 (defvar iswitchb-matches nil
371 "List of buffers currenly matching `iswitchb-text'.")
372
373 (defvar iswitchb-mode-map nil
374 "Keymap for `iswitchb-buffer'.")
375
376 (defvar iswitchb-history nil
377 "History of buffers selected using `iswitchb-buffer'.")
378
379 (defvar iswitchb-exit nil
380 "Flag to monitor how `iswitchb-buffer' exits.
381 If equal to `takeprompt', we use the prompt as the buffer name to be
382 selected.")
383
384 (defvar iswitchb-buffer-ignore-orig nil
385 "Stores original value of `iswitchb-buffer-ignore'.")
386
387 (defvar iswitchb-xemacs (string-match "XEmacs" (emacs-version))
388 "Non-nil if we are running XEmacs. Otherwise, assume we are running Emacs.")
389
390
391 ;;; FUNCTIONS
392
393
394 ;;; ISWITCHB KEYMAP
395 (defun iswitchb-define-mode-map ()
396 "Set up the keymap for `iswitchb-buffer'."
397 (interactive)
398 (let (map)
399 ;; generated every time so that it can inheret new functions.
400 ;;(or iswitchb-mode-map
401
402 (setq map (copy-keymap minibuffer-local-map))
403 (define-key map "?" 'iswitchb-completion-help)
404 (define-key map "\C-s" 'iswitchb-next-match)
405 (define-key map "\C-r" 'iswitchb-prev-match)
406 (define-key map "\t" 'iswitchb-complete)
407 (define-key map "\C-j" 'iswitchb-select-buffer-text)
408 (define-key map "\C-t" 'iswitchb-toggle-regexp)
409 (define-key map "\C-x\C-f" 'iswitchb-find-file)
410 ;;(define-key map "\C-a" 'iswitchb-toggle-ignore)
411 (define-key map "\C-c" 'iswitchb-toggle-case)
412 (define-key map "\C-k" 'iswitchb-kill-buffer)
413 (setq iswitchb-mode-map map)
414 (run-hooks 'iswitchb-define-mode-map-hook)
415 ))
416
417
418
419 ;;; MAIN FUNCTION
420 (defun iswitchb ()
421 "Switch to buffer matching a substring.
422 As you type in a string, all of the buffers matching the string are
423 displayed. When you have found the buffer you want, it can then be
424 selected. As you type, most keys have their normal keybindings,
425 except for the following:
426 \\<iswitchb-mode-map>
427
428 RET Select the buffer at the front of the list of matches. If the
429 list is empty, possibly prompt to create new buffer.
430
431 \\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
432 If no buffer is found, prompt for a new one.
433
434 \\[iswitchb-next-match] Put the first element at the end of the list.
435 \\[iswitchb-prev-match] Put the last element at the start of the list.
436 \\[iswitchb-complete] Complete a common suffix to the current string that
437 matches all buffers. If there is only one match, select that buffer.
438 If there is no common suffix, show a list of all matching buffers
439 in a separate window.
440 \\[iswitchb-toggle-regexp] Toggle rexep searching.
441 \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
442 \\[iswitchb-completion-help] Show list of matching buffers in separate window.
443 \\[iswitchb-find-file] Exit iswitchb and drop into find-file.
444 \\[iswitchb-kill-buffer] Kill buffer at head of buffer list."
445 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
446 ;;`iswitchb-buffer-ignore')
447
448 (let
449 (
450 prompt
451 buf-sel
452 iswitchb-final-text
453 (minibuffer-confirm-incomplete nil) ;XEmacs todo: prevent `;confirm'
454 (icomplete-mode nil) ;; prevent icomplete starting up
455 ;; can only use fonts if they have been bound.
456 (iswitchb-use-fonts (and iswitchb-use-fonts
457 (boundp 'font-lock-comment-face)
458 (boundp 'font-lock-function-name-face)))
459 )
460
461 (iswitchb-define-mode-map)
462 (setq iswitchb-exit nil)
463 (setq iswitchb-rescan t)
464 (setq iswitchb-text "")
465 (iswitchb-set-matches)
466 (setq prompt (format "iswitch "))
467 (iswitchb-make-buflist)
468 (let
469 ((minibuffer-local-completion-map iswitchb-mode-map))
470 ;; prompt the user for the buffer name
471 (setq iswitchb-final-text (completing-read prompt
472 ;;nil
473 '(("dummy".1))
474 ;;("2".2) ("3".3))
475 nil nil
476 nil;init string
477 'iswitchb-history)))
478
479 ;;(message "chosen text %s" iswitchb-final-text)
480 ;; Choose the buffer name: either the text typed in, or the head
481 ;; of the list of matches
482
483 (cond ( (eq iswitchb-exit 'findfile)
484 (call-interactively 'find-file))
485
486 (t
487 (if (or
488 (eq iswitchb-exit 'takeprompt)
489 (null iswitchb-matches))
490 (setq buf-sel iswitchb-final-text)
491 ;; else take head of list
492 (setq buf-sel (car iswitchb-matches)))
493
494 ;; Or possibly choose the default buffer
495 (if (equal iswitchb-final-text "")
496 (setq buf-sel (car iswitchb-matches)))
497
498 ;; View the buffer
499 (message "go to buf %s" buf-sel)
500 ;; Check buf-sel is non-nil.
501 (if buf-sel
502 (if (get-buffer buf-sel)
503 ;; buffer exists, so view it and then exit
504 (iswitchb-visit-buffer buf-sel)
505 ;; else buffer doesnt exist
506 (iswitchb-possible-new-buffer buf-sel)))
507 ))
508
509 ))
510
511
512 ;;; COMPLETION CODE
513
514 (defun iswitchb-set-common-completion ()
515 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
516 The result is stored in `iswitchb-common-match-string'."
517
518 (let* (val)
519 (setq iswitchb-common-match-string nil)
520 (if (and iswitchb-matches
521 (stringp iswitchb-text)
522 (> (length iswitchb-text) 0))
523 (if (setq val (iswitchb-find-common-substring
524 iswitchb-matches iswitchb-text))
525 (setq iswitchb-common-match-string val)))
526 val
527 ))
528
529
530 (defun iswitchb-complete ()
531 "Try and complete the current pattern amongst the buffer names."
532 (interactive)
533 (let (res)
534 (cond ((not iswitchb-matches)
535 (iswitchb-completion-help)
536 )
537
538 ((eq 1 (length iswitchb-matches))
539 ;; only one choice, so select it.
540 (exit-minibuffer))
541
542 (t
543 ;; else there could be some completions
544
545 (setq res (iswitchb-find-common-substring
546 iswitchb-matches iswitchb-text))
547 (if (and (not (memq res '(t nil)))
548 (not (equal res iswitchb-text)))
549 ;; found something to complete, so put it in the minibuff.
550 (progn
551 (setq iswitchb-rescan nil)
552 (delete-region (point-min) (point))
553 (insert res))
554 ;; else nothing to complete
555 (iswitchb-completion-help)
556 )
557 )
558 )))
559
560
561
562 ;;; TOGGLE FUNCTIONS
563
564 (defun iswitchb-toggle-case ()
565 "Toggle the value of `iswitchb-case'."
566 (interactive)
567 (setq iswitchb-case (not iswitchb-case))
568 ;; ask for list to be regenerated.
569 (setq iswitchb-rescan t)
570 )
571
572 (defun iswitchb-toggle-regexp ()
573 "Toggle the value of `iswitchb-regexp'."
574 (interactive)
575 (setq iswitchb-regexp (not iswitchb-regexp))
576 ;; ask for list to be regenerated.
577 (setq iswitchb-rescan t)
578 )
579
580
581 (defun iswitchb-toggle-ignore ()
582 "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
583 (interactive)
584 (if iswitchb-buffer-ignore
585 (progn
586 (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
587 (setq iswitchb-buffer-ignore nil)
588 )
589 ;; else
590 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig)
591 )
592 ;; ask for list to be regenerated.
593 (setq iswitchb-rescan t)
594 )
595
596
597 (defun iswitchb-select-buffer-text ()
598 "Select the buffer named by the prompt.
599 If no buffer exactly matching the prompt exists, maybe create a new one."
600 (interactive)
601 (setq iswitchb-exit 'takeprompt)
602 (exit-minibuffer))
603
604
605
606 (defun iswitchb-find-file ()
607 "Drop into find-file from buffer switching."
608 (interactive)
609 (setq iswitchb-exit 'findfile)
610 (exit-minibuffer))
611
612 (defun iswitchb-next-match ()
613 "Put first element of `iswitchb-matches' at the end of the list."
614 (interactive)
615 (let ((next (cadr iswitchb-matches)))
616 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next))
617 (setq iswitchb-rescan t)
618 ))
619
620 (defun iswitchb-prev-match ()
621 "Put last element of `iswitchb-matches' at the front of the list."
622 (interactive)
623 (let ((prev (car (last iswitchb-matches))))
624 (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev))
625 (setq iswitchb-rescan t)
626 ))
627
628
629
630
631 (defun iswitchb-chop (list elem)
632 "Remove all elements before ELEM and put them at the end of LIST."
633 (let ((ret nil)
634 (next nil)
635 (sofar nil))
636 (while (not ret)
637 (setq next (car list))
638 (if (equal next elem)
639 (setq ret (append list (nreverse sofar)))
640 ;; else
641 (progn
642 (setq list (cdr list))
643 (setq sofar (cons next sofar)))))
644 ret))
645
646
647
648
649 ;;; CREATE LIST OF ALL CURRENT BUFFERS
650
651
652 (defun iswitchb-make-buflist ()
653 "Set `iswitchb-buflist' to the current list of buffers.
654 Currently visible buffers are put at the end of the list.
655 The hook `iswitchb-make-buflist-hook' is run after the list has been
656 created to allow the user to further modify the order of the buffer names
657 in this list."
658 (setq iswitchb-buflist
659 (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
660 (buflist
661 (delq nil
662 (mapcar
663 (lambda (x)
664 (let ((b-name (buffer-name x)))
665 (if (not
666 (or
667 (iswitchb-ignore-buffername-p b-name)
668 (memq b-name iswitchb-current-buffers)))
669 b-name)))
670 (buffer-list)))))
671 (nconc buflist iswitchb-current-buffers)
672 (run-hooks 'iswitchb-make-buflist-hook)
673 buflist)))
674
675 (defun iswitchb-to-end (lst)
676 "Move the elements from LST to the end of BUFLIST."
677 (mapcar
678 (lambda (elem)
679 (setq buflist (delq elem buflist)))
680 lst)
681 (nconc buflist lst))
682
683
684
685 (defun iswitchb-get-buffers-in-frames (&optional current)
686 "Return the list of buffers that are visible in the current frame.
687 If optional argument `current' is given, restrict searching to the
688 current frame, rather than all frames, regardless of value of
689 `iswitchb-all-frames'."
690 (let ((iswitchb-bufs-in-frame nil))
691 (walk-windows 'iswitchb-get-bufname nil
692 (if current
693 nil
694 iswitchb-all-frames))
695 iswitchb-bufs-in-frame))
696
697
698 (defun iswitchb-get-bufname (win)
699 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
700 (let ((buf (buffer-name (window-buffer win))))
701 (if (not (member buf iswitchb-bufs-in-frame))
702 ;; Only add buf if it is not already in list.
703 ;; This prevents same buf in two different windows being
704 ;; put into the list twice.
705 (setq iswitchb-bufs-in-frame
706 (cons buf iswitchb-bufs-in-frame)))))
707
708
709 ;;; FIND MATCHING BUFFERS
710
711
712 (defun iswitchb-set-matches ()
713 "Set `iswitchb-matches' to the list of buffers matching prompt."
714 (if iswitchb-rescan
715 (setq iswitchb-matches
716 (let* ((buflist iswitchb-buflist)
717 )
718 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
719 buflist)))))
720
721 (defun iswitchb-get-matched-buffers (regexp
722 &optional string-format buffer-list)
723 "Return buffers matching REGEXP.
724 If STRING-FORMAT is non-nil, consider REGEXP as string.
725 BUFFER-LIST can be list of buffers or list of strings."
726 (let* ((case-fold-search iswitchb-case)
727 ;; need reverse since we are building up list backwards
728 (list (reverse buffer-list))
729 (do-string (stringp (car list)))
730 name
731 ret
732 )
733 (mapcar
734 (lambda (x)
735
736 (if do-string
737 (setq name x) ;We already have the name
738 (setq name (buffer-name x)))
739
740 (cond
741 ((and (or (and string-format (string-match regexp name))
742 (and (null string-format)
743 (string-match (regexp-quote regexp) name)))
744
745 ;; todo (not (iswitchb-ignore-buffername-p name))
746 )
747 (setq ret (cons name ret))
748 )))
749 list)
750 ret
751 ))
752
753
754
755
756 (defun iswitchb-ignore-buffername-p (bufname)
757 "Return t if the buffer BUFNAME should be ignored."
758 (let ((data (match-data))
759 (re-list iswitchb-buffer-ignore)
760 ignorep
761 nextstr
762 )
763 (while re-list
764 (setq nextstr (car re-list))
765 (cond
766 ((stringp nextstr)
767 (if (string-match nextstr bufname)
768 (progn
769 (setq ignorep t)
770 (setq re-list nil))))
771 ((fboundp nextstr)
772 (if (funcall nextstr bufname)
773 (progn
774 (setq ignorep t)
775 (setq re-list nil))
776 ))
777 )
778 (setq re-list (cdr re-list)))
779 (store-match-data data)
780
781 ;; return the result
782 ignorep)
783 )
784
785
786
787 (defun iswitchb-word-matching-substring (word)
788 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
789 If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
790 (let ((case-fold-search iswitchb-case))
791 (let ((m (string-match iswitchb-change-word-sub word)))
792 (if m
793 (substring word m)
794 ;; else no match
795 nil))))
796
797
798
799
800
801
802 (defun iswitchb-find-common-substring (lis subs)
803 "Return common string following SUBS in each element of LIS."
804 (let (res
805 alist
806 iswitchb-change-word-sub
807 )
808 (setq iswitchb-change-word-sub
809 (if iswitchb-regexp
810 subs
811 (regexp-quote subs)))
812 (setq res (mapcar 'iswitchb-word-matching-substring lis))
813 (setq res (delq nil res)) ;; remove any nil elements (shouldnt happen)
814 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
815
816 ;; try-completion returns t if there is an exact match.
817 (let ((completion-ignore-case iswitchb-case))
818
819 (try-completion subs alist)
820 )))
821
822
823 (defun iswitchb-makealist (res)
824 "Return dotted pair (RES . 1)."
825 (cons res 1))
826
827 ;; from Wayne Mesard <wmesard@esd.sgi.com>
828 (defun iswitchb-rotate-list (lis)
829 "Destructively removes the last element from LIS.
830 Return the modified list with the last element prepended to it."
831 (if (<= (length lis) 1)
832 lis
833 (let ((las lis)
834 (prev lis))
835 (while (consp (cdr las))
836 (setq prev las
837 las (cdr las)))
838 (setcdr prev nil)
839 (cons (car las) lis))
840 ))
841
842
843 (defun iswitchb-completion-help ()
844 "Show possible completions in a *Buffer Completions* buffer."
845 ;; we could allow this buffer to be used to select match, but I think
846 ;; choose-completion-string will need redefining, so it just inserts
847 ;; choice with out any previous input.
848 (interactive)
849 (setq iswitchb-rescan nil)
850 (let ((completion-setup-hook nil) ;disable fancy highlight/selection.
851 )
852 (with-output-to-temp-buffer "*Buffer Completions*"
853 (if iswitchb-xemacs
854
855 ;; XEmacs extents are put on by default, doesn't seem to be
856 ;; any way of switching them off.
857 (display-completion-list (if iswitchb-matches
858 iswitchb-matches
859 iswitchb-buflist)
860 :help-string "iswitchb "
861 :activate-callback
862 '(lambda (x y z)
863 (message "doesnt work yet, sorry!")))
864 ;; else running Emacs
865 (display-completion-list (if iswitchb-matches
866 iswitchb-matches
867 iswitchb-buflist))
868 ))))
869
870
871 ;;; KILL CURRENT BUFFER
872
873 (defun iswitchb-kill-buffer ()
874 "Kill the buffer at the head of `iswtichb-matches'."
875 (interactive)
876 (let ( (enable-recursive-minibuffers t)
877 buf)
878
879 (setq buf (car iswitchb-matches))
880 ;; check to see if buf is non-nil.
881 (if buf
882 (progn
883 (kill-buffer buf)
884
885 ;; Check if buffer exists. XEmacs gnuserv.el makes alias
886 ;; for kill-buffer which does not return t if buffer is
887 ;; killed, so we can't rely on kill-buffer return value.
888 (if (get-buffer buf)
889 ;; buffer couldn't be killed.
890 (setq iswitchb-rescan t)
891 ;; else buffer was killed so remove name from list.
892 (setq iswitchb-buflist (delq buf iswitchb-buflist)))))))
893
894
895 ;;; VISIT CHOSEN BUFFER
896 (defun iswitchb-visit-buffer (buffer)
897 "Visit buffer named BUFFER according to `iswitchb-method'."
898 (let* (win newframe)
899 (cond
900 ((eq iswitchb-method 'samewindow)
901 (switch-to-buffer buffer))
902
903 ((memq iswitchb-method '(always-frame maybe-frame))
904 (cond
905 ((and (setq win (iswitchb-window-buffer-p buffer))
906 (or (eq iswitchb-method 'always-frame)
907 (y-or-n-p "Jump to frame? ")))
908 (setq newframe (window-frame win))
909 (raise-frame newframe)
910 (select-frame newframe)
911 (select-window win)
912 (if (not iswitchb-xemacs)
913 ;; reposition mouse to make frame active. not needed in XEmacs
914 ;; This line came from the other-frame defun in Emacs.
915 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
916 )
917 (t
918 ;; No buffer in other frames...
919 (switch-to-buffer buffer)
920 )))
921
922
923
924 ((eq iswitchb-method 'otherwindow)
925 (switch-to-buffer-other-window buffer))
926
927 ((eq iswitchb-method 'display)
928 (display-buffer buffer))
929
930 ((eq iswitchb-method 'otherframe)
931 (progn
932 (switch-to-buffer-other-frame buffer)
933 (if (not iswitchb-xemacs)
934 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
935 )
936 ) )))
937
938 (defun iswitchb-possible-new-buffer (buf)
939 "Possibly create and visit a new buffer called BUF."
940
941 (let ((newbufcreated))
942 (if (and iswitchb-newbuffer
943 (or
944 (not iswitchb-prompt-newbuffer)
945
946 (and iswitchb-prompt-newbuffer
947 (y-or-n-p
948 (format
949 "No buffer matching `%s', create one? "
950 buf)))))
951 ;; then create a new buffer
952 (progn
953 (setq newbufcreated (get-buffer-create buf))
954 (if (fboundp 'set-buffer-major-mode)
955 (set-buffer-major-mode newbufcreated))
956 (iswitchb-visit-buffer newbufcreated))
957 ;; else wont create new buffer
958 (message (format "no buffer matching `%s'" buf))
959 )))
960
961 (defun iswitchb-window-buffer-p (buffer)
962 "Return window pointer if BUFFER is visible in another frame.
963 If BUFFER is visible in the current frame, return nil."
964 (interactive)
965 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
966 ;;If the buffer is visible in current frame, return nil
967 (if (memq buffer blist)
968 nil
969 ;; maybe in other frame...
970 (get-buffer-window buffer 'visible)
971 )))
972
973 ;;;###autoload
974 (defun iswitchb-default-keybindings ()
975 "Set up default keybindings for `iswitchb-buffer'.
976 Call this function to override the normal bindings."
977 (interactive)
978 (global-set-key (read-kbd-macro "C-x b") 'iswitchb-buffer)
979 (global-set-key (read-kbd-macro "C-x 4 b") 'iswitchb-buffer-other-window)
980 (global-set-key (read-kbd-macro "C-x 4 C-o") 'iswitchb-display-buffer)
981 (global-set-key (read-kbd-macro "C-x 5 b") 'iswitchb-buffer-other-frame))
982
983
984 ;;;###autoload
985 (defun iswitchb-buffer ()
986 "Switch to another buffer.
987
988 The buffer name is selected interactively by typing a substring. The
989 buffer is displayed according to `iswitchb-default-method' -- the
990 default is to show it in the same window, unless it is already visible
991 in another frame.
992 For details of keybindings, do `\\[describe-function] iswitchb'."
993 (interactive)
994 (setq iswitchb-method iswitchb-default-method)
995 (iswitchb-entry))
996
997
998 ;;;###autoload
999 (defun iswitchb-buffer-other-window ()
1000 "Switch to another buffer and show it in another window.
1001 The buffer name is selected interactively by typing a substring.
1002 For details of keybindings, do `\\[describe-function] iswitchb'."
1003 (interactive)
1004 (setq iswitchb-method 'otherwindow)
1005 (iswitchb-entry))
1006
1007
1008
1009 ;;;###autoload
1010 (defun iswitchb-display-buffer ()
1011 "Display a buffer in another window but don't select it.
1012 The buffer name is selected interactively by typing a substring.
1013 For details of keybindings, do `\\[describe-function] iswitchb'."
1014 (interactive)
1015 (setq iswitchb-method 'display)
1016 (iswitchb-entry))
1017
1018
1019
1020 ;;;###autoload
1021 (defun iswitchb-buffer-other-frame ()
1022 "Switch to another buffer and show it in another frame.
1023 The buffer name is selected interactively by typing a substring.
1024 For details of keybindings, do `\\[describe-function] iswitchb'."
1025 (interactive)
1026 (setq iswitchb-method 'otherframe)
1027 (iswitchb-entry))
1028
1029
1030
1031 (defun iswitchb-entry ()
1032 "Simply fall into `iswitchb' -- the main function."
1033 (iswitchb))
1034
1035
1036
1037
1038
1039 ;;; XEmacs hack for showing default buffer
1040
1041 ;; The first time we enter the minibuffer, Emacs puts up the default
1042 ;; buffer to switch to, but XEmacs doesnt -- presumably there is a
1043 ;; subtle difference in the two versions of post-command-hook. The
1044 ;; default is shown for both whenever we delete all of our text
1045 ;; though, indicating its just a problem the first time we enter the
1046 ;; function. To solve this, we use another entry hook for emacs to
1047 ;; show the default the first time we enter the minibuffer.
1048
1049 (defun iswitchb-init-Xemacs-trick ()
1050 "Display default buffer when first entering minibuffer.
1051 This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
1052 (if (iswitchb-entryfn-p)
1053 (progn
1054 (iswitchb-exhibit)
1055 (goto-char (point-min)))))
1056
1057
1058 ;; add this hook for XEmacs only.
1059 (if iswitchb-xemacs
1060 (add-hook 'iswitchb-minibuffer-setup-hook
1061 'iswitchb-init-Xemacs-trick))
1062
1063
1064 ;;; XEmacs / backspace key
1065 ;; For some reason, if the backspace key is pressed in xemacs, the
1066 ;; line gets confused, so I've added a simple key definition to make
1067 ;; backspace act like the normal delete key.
1068
1069 (defun iswitchb-xemacs-backspacekey ()
1070 "Bind backspace to `backward-delete-char'."
1071 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
1072 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word)
1073 )
1074
1075
1076 (if iswitchb-xemacs
1077 (add-hook 'iswitchb-define-mode-map-hook
1078 'iswitchb-xemacs-backspacekey))
1079
1080
1081
1082 ;;; ICOMPLETE TYPE CODE
1083
1084 (defun iswitchb-exhibit ()
1085 "Find matching buffers and display a list in the minibuffer.
1086 Copied from `icomplete-exhibit' with two changes:
1087 1. It prints a default buffer name when there is no text yet entered.
1088 2. It calls my completion routine rather than the standard completion."
1089
1090 (if iswitchb-use-mycompletion
1091 (let ((contents (buffer-substring (point-min)(point-max)))
1092 (buffer-undo-list t))
1093 (save-excursion
1094 (goto-char (point-max))
1095 ; Register the end of input, so we
1096 ; know where the extra stuff
1097 ; (match-status info) begins:
1098 (if (not (boundp 'iswitchb-eoinput))
1099 ;; In case it got wiped out by major mode business:
1100 (make-local-variable 'iswitchb-eoinput))
1101 (setq iswitchb-eoinput (point))
1102 ;; Update the list of matches
1103 (setq iswitchb-text contents)
1104 (iswitchb-set-matches)
1105 (setq iswitchb-rescan t)
1106 (iswitchb-set-common-completion)
1107
1108 ;; Insert the match-status information:
1109 (insert-string
1110 (iswitchb-completions
1111 contents
1112 minibuffer-completion-table
1113 minibuffer-completion-predicate
1114 (not minibuffer-completion-confirm)))
1115 ))))
1116
1117
1118
1119 (defun iswitchb-completions
1120 (name candidates predicate require-match)
1121 "Return the string that is displayed after the user's text.
1122 Modified from `icomplete-completions'."
1123
1124 (let ((comps iswitchb-matches)
1125 ; "-determined" - only one candidate
1126 (open-bracket-determined (if require-match "(" "["))
1127 (close-bracket-determined (if require-match ")" "]"))
1128 ;"-prospects" - more than one candidate
1129 (open-bracket-prospects "{")
1130 (close-bracket-prospects "}")
1131 first
1132 )
1133
1134 (if (and iswitchb-use-fonts comps)
1135 (progn
1136 (setq first (car comps))
1137 (setq first (format "%s" first))
1138 (put-text-property 0 (length first) 'face
1139 (if (eq (length comps) 1)
1140 'font-lock-comment-face
1141 'font-lock-function-name-face)
1142 first)
1143 (setq comps (cons first (cdr comps)))
1144 ))
1145
1146 (cond ((null comps) (format " %sNo match%s"
1147 open-bracket-determined
1148 close-bracket-determined))
1149
1150 ((null (cdr comps)) ;one match
1151 (concat (if (and (> (length (car comps))
1152 (length name)))
1153 (concat open-bracket-determined
1154 ;; when there is one match, show the
1155 ;; matching buffer name in full
1156 (car comps)
1157 close-bracket-determined)
1158 "")
1159 (if (not iswitchb-use-fonts) " [Matched]")
1160 ))
1161 (t ;multiple matches
1162 (let* (
1163 ;;(most (try-completion name candidates predicate))
1164 (most nil)
1165 (most-len (length most))
1166 most-is-exact
1167 first
1168 (alternatives
1169 (apply
1170 (function concat)
1171 (cdr (apply
1172 (function nconc)
1173 (mapcar '(lambda (com)
1174 (if (= (length com) most-len)
1175 ;; Most is one exact match,
1176 ;; note that and leave out
1177 ;; for later indication:
1178 (progn
1179 (setq most-is-exact t)
1180 ())
1181 (list ","
1182 (substring com
1183 most-len))))
1184 comps))))))
1185
1186 (concat
1187
1188 ;; put in common completion item -- what you get by
1189 ;; pressing tab
1190 (if (> (length iswitchb-common-match-string) (length name))
1191 (concat open-bracket-determined
1192 (substring iswitchb-common-match-string
1193 (length name))
1194 close-bracket-determined)
1195 )
1196 ;; end of partial matches...
1197
1198 ;; think this bit can be ignored.
1199 (and (> most-len (length name))
1200 (concat open-bracket-determined
1201 (substring most (length name))
1202 close-bracket-determined))
1203
1204 ;; list all alternatives
1205 open-bracket-prospects
1206 (if most-is-exact
1207 (concat "," alternatives)
1208 alternatives)
1209 close-bracket-prospects)))
1210 )))
1211
1212 (defun iswitchb-minibuffer-setup ()
1213 "Set up minibuffer for `iswitchb-buffer'.
1214 Copied from `icomplete-minibuffer-setup-hook'."
1215 (if (iswitchb-entryfn-p)
1216 (progn
1217
1218 (make-local-variable 'iswitchb-use-mycompletion)
1219 (setq iswitchb-use-mycompletion t)
1220 (make-local-hook 'pre-command-hook)
1221 (add-hook 'pre-command-hook
1222 'iswitchb-pre-command
1223 nil t)
1224 (make-local-hook 'post-command-hook)
1225 (add-hook 'post-command-hook
1226 'iswitchb-post-command
1227 nil t)
1228
1229 (run-hooks 'iswitchb-minibuffer-setup-hook)
1230 )
1231 ))
1232
1233
1234 (defun iswitchb-pre-command ()
1235 "Run before command in `iswitchb-buffer'."
1236 (iswitchb-tidy))
1237
1238
1239 (defun iswitchb-post-command ()
1240 "Run after command in `iswitchb-buffer'."
1241 (iswitchb-exhibit)
1242 )
1243
1244
1245
1246 (defun iswitchb-tidy ()
1247 "Remove completions display, if any, prior to new user input.
1248 Copied from `icomplete-tidy'."
1249
1250 (if (and (boundp 'iswitchb-eoinput)
1251 iswitchb-eoinput)
1252
1253 (if (> iswitchb-eoinput (point-max))
1254 ;; Oops, got rug pulled out from under us - reinit:
1255 (setq iswitchb-eoinput (point-max))
1256 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1257 (delete-region iswitchb-eoinput (point-max))))
1258
1259 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1260 (make-local-variable 'iswitchb-eoinput)
1261 (setq iswitchb-eoinput 1)))
1262
1263
1264 (defun iswitchb-entryfn-p ()
1265 "Return non-nil if `this-command' shows we are using `iswitchb-buffer'."
1266 (and (symbolp this-command) ; ignore lambda functions
1267 (memq this-command
1268 '(iswitchb-buffer
1269 iswitchb-buffer-other-frame
1270 iswitchb-display-buffer
1271 iswitchb-buffer-other-window))))
1272
1273
1274
1275
1276 (defun iswitchb-summaries-to-end ()
1277 "Move the summaries to the end of the list.
1278 This is an example function which can be hooked on to
1279 `iswitchb-make-buflist-hook'. Any buffer matching the regexps
1280 `Summary' or `output\*$'are put to the end of the list."
1281 (let ((summaries (delq nil (mapcar
1282 (lambda (x)
1283 (if (or
1284 (string-match "Summary" x)
1285 (string-match "output\\*$" x))
1286 x))
1287 buflist)
1288 )))
1289
1290 (iswitchb-to-end summaries)))
1291
1292
1293
1294 ;;; HOOKS
1295 (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1296
1297 (provide 'iswitchb)
1298
1299 ;;; iswitchb.el ends here