(help-for-help): Update help text.
[bpt/emacs.git] / lisp / iswitchb.el
CommitLineData
6c56c80b 1;;; iswitchb.el --- switch between buffers using substrings
962a4216
RS
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>
962a4216
RS
7;; Keywords: extensions
8;; location: http://www.cogs.susx.ac.uk/users/stephene/emacs
9
6c56c80b
RS
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
962a4216
RS
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
6c56c80b 17;; GNU Emacs is distributed in the hope that it will be useful,
962a4216
RS
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
962a4216
RS
27;;; Installation:
28
962a4216
RS
29;; To get the functions in this package bound to keys, do
30;; (iswitchb-default-keybindings)
962a4216
RS
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 default buffer (xxx) to switch to:
56;;
57;; iswitch {default xxx}
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;;
100;; See the doc string of iswitchb for full keybindings and features.
101;; (describe-function 'iswitchb)
102
103;;; Customisation
104
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)
110;;
111;;(defun iswitchb-my-keys ()
112;; "Add my keybings for iswitchb."
113;; (define-key iswitchb-mode-map " " 'iswitchb-next-match)
114;; )
115;;
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'.
122
123
124;; Changing the list of buffers.
125
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:
130;;
131;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end)
132;;
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.)
139
140;; Font-Lock
141
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)
147
148;;; Comparison with iswitch-buffer
149
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).
160;;
161;; Disadvantages:
162;; o cant change the prompt to indicate status of searching (eg whether
163;; regexp searching is currently on).
164
165
166;;; Acknowledgements
167
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.
171
172;;; Code:
173
962a4216
RS
174;;; User Variables
175;;
176;; These are some things you might want to change.
177
178(defvar iswitchb-case case-fold-search
179 "*Non-nil if searching of buffer names should ignore case.")
180
181(defvar iswitchb-buffer-ignore
182 '("^ ")
6c56c80b
RS
183 "*List of regexps or functions matching buffer names to ignore.
184For example, traditional behavior is not to list buffers whose names begin
185with a space, for which the regexp is `^ '. See the source file for
962a4216
RS
186example functions that filter buffernames.")
187
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)))
194
195;(setq iswitchb-buffer-ignore '("^ " ignore-c-mode))
196;(setq iswitchb-buffer-ignore '("^ " "\\.c$" "\\.h$"))
197
198
199(defvar iswitchb-default-method 'always-frame
6c56c80b 200 "*How to switch to new buffer when using `iswitchb-buffer'.
962a4216
RS
201Possible values:
202`samewindow' Show new buffer in same window
203`otherwindow' Show new buffer in another window (same frame)
888472e0 204`display' Display buffer in another window without switching to it
962a4216
RS
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.")
211
212(defvar iswitchb-regexp nil
6c56c80b
RS
213 "*Non-nil means that `iswitchb' will do regexp matching.
214Value can be toggled within `iswitchb'.")
962a4216
RS
215
216(defvar iswitchb-newbuffer t
217 "*Non-nil means create new buffer if no buffer matches substring.
218See also `iswitchb-prompt-newbuffer'.")
219
220(defvar iswitchb-prompt-newbuffer t
221 "*Non-nil means prompt user to confirm before creating new buffer.
222See also `iswitchb-newbuffer'.")
223
224(defvar iswitchb-define-mode-map-hook nil
225 "*Hook to define keys in `iswitchb-mode-map' for extra keybindings.")
226
227
228(defvar iswitchb-use-fonts t
229 "*Non-nil means use fonts for showing first match.")
230
231(defvar iswitchb-make-buflist-hook nil
232 "*Hook to run when list of matching buffers is created.")
233
234
235(defvar iswitchb-method nil
6c56c80b 236 "*Stores the method for viewing the selected buffer.
888472e0 237Its value is one of `samewindow', `otherwindow', `display', `otherframe',
6c56c80b
RS
238`maybe-frame' or `always-frame'. See `iswitchb-default-method' for
239details of values.")
962a4216
RS
240
241(defvar iswitchb-all-frames 'visible
242 "*Argument to pass to `walk-windows' when finding visible buffers.
243See documentation of `walk-windows' for useful values.")
244
245;;; THINGS TO DO / BUGS
246
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.
253;
254; There is also a problem with the backspace key in XEmacs, so I have
255; bound it to the normal backward-delete-char.
256
257;; iswitch-buffer features Not yet implemented:
258; C-f Quit iswitch and drop into find-file
259
260
261;; Do we need the variable iswitchb-use-mycompletion?
262
263
264;;; Internal Variables
265(defvar iswitchb-minibuffer-setup-hook nil
266 "*Iswitchb-specific customization of minibuffer setup.
267
6c56c80b 268This hook is run during minibuffer setup iff `iswitchb' will be active.
962a4216
RS
269It is intended for use in customizing iswitchb for interoperation
270with other packages. For instance:
271
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))))
277
278will constrain rsz-mini to a maximum minibuffer height of 3 lines when
6c56c80b 279iswitchb is running. Copied from `icomplete-minibuffer-setup-hook'.")
962a4216
RS
280
281(defvar iswitchb-eoinput 1
282 "Point where minibuffer input ends and completion info begins.
6c56c80b 283Copied from `icomplete-eoinput'.")
962a4216
RS
284(make-variable-buffer-local 'iswitchb-eoinput)
285
286
287(defvar iswitchb-buflist nil
288 "Stores the current list of buffers that will be searched through.
289The list is ordered, so that the most recent buffers come first,
290although by default, the buffers visible in the current frame are put
291at the end of the list. Created by `iswitchb-make-buflist'.")
292
293;; todo -- is this necessary?
294
295(defvar iswitchb-use-mycompletion nil
6c56c80b
RS
296 "Non-nil means use `iswitchb-buffer' completion feedback.
297Should only be set to t by iswitchb functions, so that it doesn't
298interfere with other minibuffer usage.")
962a4216
RS
299
300(defvar iswitchb-change-word-sub nil
301 "Private variable used by `iswitchb-word-matching-substring'.")
302
303
304(defvar iswitchb-common-match-string nil
305 "Stores the string that is common to all matching buffers.")
306
307
308(defvar iswitchb-rescan nil
309 "Non-nil means we need to regenerate the list of matching buffers.")
310
311(defvar iswitchb-text nil
312 "Stores the users string as it is typed in.")
313
314(defvar iswitchb-matches nil
315 "List of buffers currenly matching `iswitchb-text'.")
316
317(defvar iswitchb-default-buffer nil
318 "Default buffer to switch to.")
319
320(defvar iswitchb-mode-map nil
6c56c80b 321 "Keymap for `iswitchb-buffer'.")
962a4216
RS
322
323(defvar iswitchb-history nil
6c56c80b 324 "History of buffers selected using `iswitchb-buffer'.")
962a4216
RS
325
326(defvar iswitchb-exit nil
6c56c80b
RS
327 "Flag to monitor how `iswitchb-buffer' exits.
328If equal to `takeprompt', we use the prompt as the buffer name to be
329selected.")
962a4216
RS
330
331(defvar iswitchb-buffer-ignore-orig nil
332 "Stores original value of `iswitchb-buffer-ignore'.")
333
334(defvar iswitchb-xemacs (string-match "XEmacs" (emacs-version))
335 "Non-nil if we are running XEmacs. Otherwise, assume we are running Emacs.")
336
337
338;;; FUNCTIONS
339
340
341;;; ISWITCHB KEYMAP
342(defun iswitchb-define-mode-map ()
6c56c80b 343 "Set up the keymap for `iswitchb-buffer'."
962a4216
RS
344 (interactive)
345 (let (map)
346 ;; generated every time so that it can inheret new functions.
347 ;;(or iswitchb-mode-map
348
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)
360 ))
361
362
363
364;;; MAIN FUNCTION
365(defun iswitchb ()
366 "Switch to buffer matching a substring.
367As you type in a string, all of the buffers matching the string are
368displayed. When you have found the buffer you want, it can then be
369selected. As you type, most keys have their normal keybindings,
370except for the following:
371\\<iswitchb-mode-map>
372
373RET Select the buffer at the front of the list of matches. If the
6c56c80b 374list is empty, possibly prompt to create new buffer.
962a4216
RS
375
376\\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
377If no buffer is found, prompt for a new one.
378
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
382matches all buffers. If there is only one match, select that buffer.
383If there is no common suffix, show a list of all matching buffers
384in a separate window.
385\\[iswitchb-toggle-regexp] Toggle rexep searching.
386\\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
6c56c80b 387\\[iswitchb-completion-help] Show list of matching buffers in separate window."
962a4216
RS
388 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
389 ;;`iswitchb-buffer-ignore')
390
391 (let
392 (
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)))
403 )
404
405 (iswitchb-define-mode-map)
406 (setq minibuffer-local-completion-map iswitchb-mode-map)
407
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)))
416
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))
423
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)))
441
442 ;; Or possibly choose the default buffer
443 (if (equal iswitchb-final-text "")
444 (setq buf-sel iswitchb-default-buffer))
445
446 ;; View the buffer
447 (message "go to buf %s" buf-sel)
448
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))
454
455 ))
456
457
458;;; COMPLETION CODE
459
460(defun iswitchb-set-common-completion ()
6c56c80b
RS
461 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
462The result is stored in `iswitchb-common-match-string'."
962a4216
RS
463
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)))
472 val
473 ))
474
475
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)
481
482 ;; todo
483 ;;(message "No buffer completions.")
484 ;;(sit-for 0.3)
485 (iswitchb-completion-help)
486 )
487
488 ((eq 1 (length iswitchb-matches))
489 ;; only one choice, so select it.
490 (exit-minibuffer))
491
492 (t
493 ;; else there could be some completions
494
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)
506 )
507 )
508 )))
509
510
511
512;;; TOGGLE FUNCTIONS
513
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)
520 )
521
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)
528 )
529
530
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)
538 )
539 ;; else
540 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig)
541 )
542 ;; ask for list to be regenerated.
543 (setq iswitchb-rescan t)
544 )
545
546
547(defun iswitchb-select-buffer-text ()
6c56c80b
RS
548 "Select the buffer named by the prompt.
549If no buffer exactly matching the prompt exists, maybe create a new one."
962a4216
RS
550 (interactive)
551 (setq iswitchb-exit 'takeprompt)
552 (exit-minibuffer))
553
554
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)
562 ))
563
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)
569 )
570
571
572
573
574;;; CREATE LIST OF ALL CURRENT BUFFERS
575
576(defun iswitchb-make-buflist ()
6c56c80b
RS
577 "Set `iswitchb-buflist' to the current list of buffers.
578Currently visible buffers are put at the end of the list."
962a4216
RS
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))
588 x))
589 buflist)))
590 (mapcar 'iswitchb-to-end iswitchb-current-buffers)
591
592 (run-hooks 'iswitchb-make-buflist-hook)
593 buflist)))
594
595
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))))
601
602
603
604
605(defun iswitchb-get-buffers-in-frames (&optional current)
962a4216
RS
606 "Return the list of buffers that are visible in the current frame.
607If optional argument `current' is given, restrict searching to the
608current frame, rather than all frames, regardless of value of
609`iswitchb-all-frames'."
962a4216 610 (let ((iswitchb-bufs-in-frame nil))
962a4216
RS
611 (walk-windows 'iswitchb-get-bufname nil
612 (if current
613 nil
614 iswitchb-all-frames))
615 iswitchb-bufs-in-frame))
616
617
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)))
623
624
625;;; FIND MATCHING BUFFERS
626
627(defun iswitchb-set-matches ()
628 "Set `iswitchb-matches' to the list of buffers matching prompt."
962a4216
RS
629 (if iswitchb-rescan
630 (setq iswitchb-matches
631 (let* ((buflist iswitchb-buflist)
632 )
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)))))
638
6c56c80b
RS
639(defun iswitchb-get-matched-buffers (regexp &optional string-format buffer-list)
640 "Return matched buffers.
888472e0 641If STRING-FORMAT is non-nil, consider REGEXP as string.
6c56c80b 642BUFFER-LIST can be list of buffers or list of strings."
962a4216
RS
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
648 ret
649 )
650 (mapcar
651 (function
652 (lambda (x)
653
654 (if do-string
655 (setq name x) ;We already have the name
656 (setq name (buffer-name x)))
657
658 (cond
659 ((and (or (and string-format (string-match regexp name))
660 (and (null string-format)
661 (string-match (regexp-quote regexp) name)))
662
663 ;; todo (not (iswitchb-ignore-buffername-p name))
664 )
665 (setq ret (cons name ret))
666 ))))
667 list)
668 ret
669 ))
670
671
672
673
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
680 )
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))
694 ))
695 )
696 (setq re-list (cdr re-list)))
697 (store-match-data data)
698
699 ;; return the result
700 ignorep)
701 )
702
703
704
705(defun iswitchb-word-matching-substring (word)
706 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
707If `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))))
714
715
716
717
718
719
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
725 )
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
733
734 ;; try-completion returns t if there is an exact match.
735 (let ((completion-ignore-case iswitchb-case))
736
737 (try-completion subs alist)
738 )))
739
740
741(defun iswitchb-makealist (res)
742 "Return dotted pair (RES . 1)."
743 (cons res 1))
744
745;; from Wayne Mesard <wmesard@esd.sgi.com>
746(defun iswitchb-rotate-list (lis)
747 "Destructively removes the last element from LIS.
748Return the modified list with the last element prepended to it."
749 (if (<= (length lis) 1)
750 lis
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))
758 ))
759
760
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.
768 )
769 (with-output-to-temp-buffer "*Buffer Completions*"
770 (if iswitchb-xemacs
771
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 ))))
786
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))
794
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))
808 )
809 (t
810 ;; No buffer in other frames...
811 (switch-to-buffer buffer)
812 )))
813
814
815
816 ((eq iswitchb-method 'otherwindow)
817 (switch-to-buffer-other-window buffer))
818
888472e0
RS
819 ((eq iswitchb-method 'display)
820 (display-buffer buffer))
821
962a4216
RS
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))
827 )
828 ) )))
829
830(defun iswitchb-possible-new-buffer (buf)
831 "Possibly create and visit a new buffer called BUF."
832
833 (let ((newbufcreated))
834 (if (and iswitchb-newbuffer
835 (or
836 (not iswitchb-prompt-newbuffer)
837
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))
851 )))
852
853(defun iswitchb-window-buffer-p (buffer)
6c56c80b
RS
854 "Return window pointer if BUFFER is visible in another frame.
855If BUFFER is visible in the current frame, return nil."
962a4216 856 (interactive)
962a4216
RS
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)
860 nil
861 ;; maybe in other frame...
862 (get-buffer-window buffer 'visible)
863 )))
864
6c56c80b 865;;;###autoload
962a4216 866(defun iswitchb-default-keybindings ()
6c56c80b 867 "Set up default keybindings for `iswitchb-buffer'.
962a4216
RS
868Call this function to override the normal bindings."
869 (interactive)
870 (global-set-key "\18b" 'iswitchb-buffer)
871 (global-set-key "\184b" 'iswitchb-buffer-other-window)
888472e0 872 (global-set-key "\184\ f" 'iswitchb-display-buffer)
962a4216
RS
873 (global-set-key "\185b" 'iswitchb-buffer-other-frame))
874
875
876
877;;;###autoload
878(defun iswitchb-buffer ()
879 "Switch to another buffer.
880
881The buffer name is selected interactively by typing a substring. The
882buffer is displayed according to `iswitchb-default-method' -- the
883default is to show it in the same window, unless it is already visible
6c56c80b
RS
884in another frame.
885For details of keybindings, do `\\[describe-function] iswitchb'."
962a4216
RS
886 (interactive)
887 (setq iswitchb-method iswitchb-default-method)
888 (iswitchb-entry))
889
890
891;;;###autoload
892(defun iswitchb-buffer-other-window ()
893 "Switch to another buffer and show it in another window.
894The buffer name is selected interactively by typing a substring.
6c56c80b 895For details of keybindings, do `\\[describe-function] iswitchb'."
962a4216
RS
896 (interactive)
897 (setq iswitchb-method 'otherwindow)
898 (iswitchb-entry))
899
900
901
888472e0
RS
902;;;###autoload
903(defun iswitchb-display-buffer ()
904 "Display a buffer in another window but don't select it.
905The buffer name is selected interactively by typing a substring.
906For details of keybindings, do `\\[describe-function] iswitchb'."
907 (interactive)
908 (setq iswitchb-method 'display)
909 (iswitchb-entry))
910
911
912
962a4216
RS
913;;;###autoload
914(defun iswitchb-buffer-other-frame ()
915 "Switch to another buffer and show it in another frame.
916The buffer name is selected interactively by typing a substring.
6c56c80b 917For details of keybindings, do `\\[describe-function] iswitchb'."
962a4216
RS
918 (interactive)
919 (setq iswitchb-method 'otherframe)
920 (iswitchb-entry))
921
922
923
924(defun iswitchb-entry ()
6c56c80b 925 "Simply fall into `iswitchb' -- the main function."
962a4216
RS
926 (iswitchb))
927
928
929
930
931
932;;; XEMACS HACK FOR SHOWING DEFAULT BUFFER
933
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.
941
942(defun iswitchb-init-Xemacs-trick ()
6c56c80b
RS
943 "Display default buffer when first entering minibuffer.
944This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
962a4216
RS
945 (if (iswitchb-entryfn-p)
946 (progn
947 (iswitchb-show-default-buffer)
948 (goto-char (point-min)))))
949
950;; add this hook for Xemacs only.
951(if iswitchb-xemacs
952 (add-hook 'iswitchb-minibuffer-setup-hook
953 'iswitchb-init-Xemacs-trick))
954
955
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.
960
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)
965 )
966
967
968(if iswitchb-xemacs
969 (add-hook 'iswitchb-define-mode-map-hook
970 'iswitchb-xemacs-backspacekey))
971
972
973
974;;; ICOMPLETE TYPE CODE
975
976(defun iswitchb-exhibit ()
6c56c80b 977 "Find matching buffers and display a list in the minibuffer.
962a4216
RS
978Copied from `icomplete-exhibit' with two changes:
9791. It prints a default buffer name when there is no text yet entered.
9802. It calls my completion routine rather than the standard completion."
981
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)
999
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 ))))
1011
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]")))
1016
1017(defun iswitchb-completions
1018 (name candidates predicate require-match)
1019 "Return the string that is displayed after the user's text.
1020Modified from `icomplete-completions'."
1021
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
1030 )
1031
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)))
1042 ))
1043
1044 (cond ((null comps) (format " %sNo match%s"
1045 open-bracket-determined
1046 close-bracket-determined))
1047
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)
1056 "")
1057 (if (not iswitchb-use-fonts) " [Matched]")
1058 ))
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)
1078 ())
1079 (list ","
1080 (substring com
1081 most-len))))
1082 comps))))))
1083
1084 (concat
1085
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)
1093 )
1094 ;; end of partial matches...
1095
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))
1101
1102 ;; list all alternatives
1103 open-bracket-prospects
1104 (if most-is-exact
1105 (concat "," alternatives)
1106 alternatives)
1107 close-bracket-prospects)))
1108 )))
1109
1110(defun iswitchb-minibuffer-setup ()
6c56c80b
RS
1111 "Set up minibuffer for `iswitchb-buffer'.
1112Copied from `icomplete-minibuffer-setup-hook'."
962a4216
RS
1113 (if (iswitchb-entryfn-p)
1114 (progn
1115
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)
1126
1127 (run-hooks 'iswitchb-minibuffer-setup-hook)
1128 )
1129 ))
1130
1131
1132(defun iswitchb-pre-command ()
6c56c80b 1133 "Run before command in `iswitchb-buffer'."
962a4216
RS
1134 (iswitchb-tidy))
1135
1136
1137(defun iswitchb-post-command ()
6c56c80b 1138 "Run after command in `iswitchb-buffer'."
962a4216
RS
1139 (iswitchb-exhibit)
1140 )
1141
1142
1143
1144(defun iswitchb-tidy ()
1145 "Remove completions display, if any, prior to new user input.
1146Copied from `icomplete-tidy'."
1147
1148 (if (and (boundp 'iswitchb-eoinput)
1149 iswitchb-eoinput)
1150
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))))
1156
1157 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1158 (make-local-variable 'iswitchb-eoinput)
1159 (setq iswitchb-eoinput 1)))
1160
1161
1162(defun iswitchb-entryfn-p ()
6c56c80b 1163 "Return non-nil if `this-command' shows we are using `iswitchb-buffer'."
962a4216 1164 (and (symbolp this-command) ; ignore lambda functions
662659f0
RS
1165 (memq this-command
1166 '(iswitchb-buffer
1167 iswitchb-buffer-other-frame
1168 iswitchb-display-buffer
1169 iswitchb-buffer-other-window))))
962a4216
RS
1170
1171(defun iswitchb-summaries-to-end ()
6c56c80b
RS
1172 "Move the summaries to the end of the list.
1173This 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."
962a4216
RS
1176 (let ((summaries (delq nil (mapcar
1177 '(lambda (x)
1178 (if (or
1179 (string-match "Summary" x)
1180 (string-match "output\\*$" x))
1181 x))
1182 buflist)
1183 )))
1184
1185 (mapcar 'iswitchb-to-end summaries)))
1186
1187
1188
1189;;; HOOKS
1190(add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1191
1192(provide 'iswitchb)
1193
6c56c80b 1194;;; iswitchb.el ends here