Many doc fixes.
[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)
204`otherframe' Show new buffer in another frame
205`maybe-frame' If a buffer is visible in another frame, prompt to ask if you
206 you want to see the buffer in the same window of the current
207 frame or in the other frame.
208`always-frame' If a buffer is visible in another frame, raise that
209 frame. Otherwise, visit the buffer in the same window.")
210
211(defvar iswitchb-regexp nil
6c56c80b
RS
212 "*Non-nil means that `iswitchb' will do regexp matching.
213Value can be toggled within `iswitchb'.")
962a4216
RS
214
215(defvar iswitchb-newbuffer t
216 "*Non-nil means create new buffer if no buffer matches substring.
217See also `iswitchb-prompt-newbuffer'.")
218
219(defvar iswitchb-prompt-newbuffer t
220 "*Non-nil means prompt user to confirm before creating new buffer.
221See also `iswitchb-newbuffer'.")
222
223(defvar iswitchb-define-mode-map-hook nil
224 "*Hook to define keys in `iswitchb-mode-map' for extra keybindings.")
225
226
227(defvar iswitchb-use-fonts t
228 "*Non-nil means use fonts for showing first match.")
229
230(defvar iswitchb-make-buflist-hook nil
231 "*Hook to run when list of matching buffers is created.")
232
233
234(defvar iswitchb-method nil
6c56c80b
RS
235 "*Stores the method for viewing the selected buffer.
236Its value is one of `samewindow', `otherwindow', `otherframe',
237`maybe-frame' or `always-frame'. See `iswitchb-default-method' for
238details of values.")
962a4216
RS
239
240(defvar iswitchb-all-frames 'visible
241 "*Argument to pass to `walk-windows' when finding visible buffers.
242See documentation of `walk-windows' for useful values.")
243
244;;; THINGS TO DO / BUGS
245
246;; In Xemacs, the default buffer is not shown the first time you enter
247; the minibuffer, but if you type a char and then delete a char, the
248; default appears. The first time we enter the minibuffer in XEmacs,
249; the default msg is not displayed, presumably because the hook is not
250; being called. I have put in a temporary hack therefore at the
251; bottom of this file.
252;
253; There is also a problem with the backspace key in XEmacs, so I have
254; bound it to the normal backward-delete-char.
255
256;; iswitch-buffer features Not yet implemented:
257; C-f Quit iswitch and drop into find-file
258
259
260;; Do we need the variable iswitchb-use-mycompletion?
261
262
263;;; Internal Variables
264(defvar iswitchb-minibuffer-setup-hook nil
265 "*Iswitchb-specific customization of minibuffer setup.
266
6c56c80b 267This hook is run during minibuffer setup iff `iswitchb' will be active.
962a4216
RS
268It is intended for use in customizing iswitchb for interoperation
269with other packages. For instance:
270
271 \(add-hook 'iswitchb-minibuffer-setup-hook
272 \(function
273 \(lambda ()
274 \(make-local-variable 'resize-minibuffer-window-max-height)
275 \(setq resize-minibuffer-window-max-height 3))))
276
277will constrain rsz-mini to a maximum minibuffer height of 3 lines when
6c56c80b 278iswitchb is running. Copied from `icomplete-minibuffer-setup-hook'.")
962a4216
RS
279
280(defvar iswitchb-eoinput 1
281 "Point where minibuffer input ends and completion info begins.
6c56c80b 282Copied from `icomplete-eoinput'.")
962a4216
RS
283(make-variable-buffer-local 'iswitchb-eoinput)
284
285
286(defvar iswitchb-buflist nil
287 "Stores the current list of buffers that will be searched through.
288The list is ordered, so that the most recent buffers come first,
289although by default, the buffers visible in the current frame are put
290at the end of the list. Created by `iswitchb-make-buflist'.")
291
292;; todo -- is this necessary?
293
294(defvar iswitchb-use-mycompletion nil
6c56c80b
RS
295 "Non-nil means use `iswitchb-buffer' completion feedback.
296Should only be set to t by iswitchb functions, so that it doesn't
297interfere with other minibuffer usage.")
962a4216
RS
298
299(defvar iswitchb-change-word-sub nil
300 "Private variable used by `iswitchb-word-matching-substring'.")
301
302
303(defvar iswitchb-common-match-string nil
304 "Stores the string that is common to all matching buffers.")
305
306
307(defvar iswitchb-rescan nil
308 "Non-nil means we need to regenerate the list of matching buffers.")
309
310(defvar iswitchb-text nil
311 "Stores the users string as it is typed in.")
312
313(defvar iswitchb-matches nil
314 "List of buffers currenly matching `iswitchb-text'.")
315
316(defvar iswitchb-default-buffer nil
317 "Default buffer to switch to.")
318
319(defvar iswitchb-mode-map nil
6c56c80b 320 "Keymap for `iswitchb-buffer'.")
962a4216
RS
321
322(defvar iswitchb-history nil
6c56c80b 323 "History of buffers selected using `iswitchb-buffer'.")
962a4216
RS
324
325(defvar iswitchb-exit nil
6c56c80b
RS
326 "Flag to monitor how `iswitchb-buffer' exits.
327If equal to `takeprompt', we use the prompt as the buffer name to be
328selected.")
962a4216
RS
329
330(defvar iswitchb-buffer-ignore-orig nil
331 "Stores original value of `iswitchb-buffer-ignore'.")
332
333(defvar iswitchb-xemacs (string-match "XEmacs" (emacs-version))
334 "Non-nil if we are running XEmacs. Otherwise, assume we are running Emacs.")
335
336
337;;; FUNCTIONS
338
339
340;;; ISWITCHB KEYMAP
341(defun iswitchb-define-mode-map ()
6c56c80b 342 "Set up the keymap for `iswitchb-buffer'."
962a4216
RS
343 (interactive)
344 (let (map)
345 ;; generated every time so that it can inheret new functions.
346 ;;(or iswitchb-mode-map
347
348 (setq map (copy-keymap minibuffer-local-map))
349 (define-key map "?" 'iswitchb-completion-help)
350 (define-key map "\C-s" 'iswitchb-next-match)
351 (define-key map "\C-r" 'iswitchb-prev-match)
352 (define-key map "\t" 'iswitchb-complete)
353 (define-key map "\C-j" 'iswitchb-select-buffer-text)
354 (define-key map "\C-t" 'iswitchb-toggle-regexp)
355 ;;(define-key map "\C-a" 'iswitchb-toggle-ignore)
356 (define-key map "\C-c" 'iswitchb-toggle-case)
357 (setq iswitchb-mode-map map)
358 (run-hooks 'iswitchb-define-mode-map-hook)
359 ))
360
361
362
363;;; MAIN FUNCTION
364(defun iswitchb ()
365 "Switch to buffer matching a substring.
366As you type in a string, all of the buffers matching the string are
367displayed. When you have found the buffer you want, it can then be
368selected. As you type, most keys have their normal keybindings,
369except for the following:
370\\<iswitchb-mode-map>
371
372RET Select the buffer at the front of the list of matches. If the
6c56c80b 373list is empty, possibly prompt to create new buffer.
962a4216
RS
374
375\\[iswitchb-select-buffer-text] Select the current prompt as the buffer.
376If no buffer is found, prompt for a new one.
377
378\\[iswitchb-next-match] Put the first element at the end of the list.
379\\[iswitchb-prev-match] Put the last element at the start of the list.
380\\[iswitchb-complete] Complete a common suffix to the current string that
381matches all buffers. If there is only one match, select that buffer.
382If there is no common suffix, show a list of all matching buffers
383in a separate window.
384\\[iswitchb-toggle-regexp] Toggle rexep searching.
385\\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names.
6c56c80b 386\\[iswitchb-completion-help] Show list of matching buffers in separate window."
962a4216
RS
387 ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
388 ;;`iswitchb-buffer-ignore')
389
390 (let
391 (
392 prompt
393 buf-sel
394 iswitchb-final-text
395 (minibuffer-confirm-incomplete nil) ;XEmacs todo: prevent `;confirm'
396 (icomplete-mode nil) ;; prevent icomplete starting up
397 (minibuffer-local-completion-map minibuffer-local-completion-map)
398 ;; can only use fonts if they have been bound.
399 (iswitchb-use-fonts (and iswitchb-use-fonts
400 (boundp 'font-lock-comment-face)
401 (boundp 'font-lock-function-name-face)))
402 )
403
404 (iswitchb-define-mode-map)
405 (setq minibuffer-local-completion-map iswitchb-mode-map)
406
407 (setq iswitchb-exit nil)
408 (setq iswitchb-rescan t)
409 (setq iswitchb-text "")
410 (setq iswitchb-matches nil)
411 ;;(setq iswitchb-default-buffer (buffer-name (other-buffer)))
412 (setq prompt (format "iswitch "))
413 (iswitchb-make-buflist)
414 (setq iswitchb-default-buffer (format "%s" (car iswitchb-buflist)))
415
416 ;; highlight the default.
417 (if iswitchb-use-fonts
418 (put-text-property 0 (length iswitchb-default-buffer)
419 'face
420 'font-lock-function-name-face
421 iswitchb-default-buffer))
422
423 ;; prompt the user for the buffer name
424 (setq iswitchb-final-text (completing-read prompt
425 ;;nil
426 '(("dummy".1))
427 ;;("2".2) ("3".3))
428 nil nil
429 nil;init string
430 'iswitchb-history))
431 ;;(message "chosen text %s" iswitchb-final-text)
432 ;; Choose the buffer name: either the text typed in, or the head
433 ;; of the list of matches
434 (if (or
435 (eq iswitchb-exit 'takeprompt)
436 (null iswitchb-matches))
437 (setq buf-sel iswitchb-final-text)
438 ;; else take head of list
439 (setq buf-sel (car iswitchb-matches)))
440
441 ;; Or possibly choose the default buffer
442 (if (equal iswitchb-final-text "")
443 (setq buf-sel iswitchb-default-buffer))
444
445 ;; View the buffer
446 (message "go to buf %s" buf-sel)
447
448 (if (get-buffer buf-sel)
449 ;; buffer exists, so view it and then exit
450 (iswitchb-visit-buffer buf-sel)
451 ;; else buffer doesnt exist
452 (iswitchb-possible-new-buffer buf-sel))
453
454 ))
455
456
457;;; COMPLETION CODE
458
459(defun iswitchb-set-common-completion ()
6c56c80b
RS
460 "Find common completion of `iswitchb-text' in `iswitchb-matches'.
461The result is stored in `iswitchb-common-match-string'."
962a4216
RS
462
463 (let* (val)
464 (setq iswitchb-common-match-string nil)
465 (if (and iswitchb-matches
466 (stringp iswitchb-text)
467 (> (length iswitchb-text) 0))
468 (if (setq val (iswitchb-find-common-substring
469 iswitchb-matches iswitchb-text))
470 (setq iswitchb-common-match-string val)))
471 val
472 ))
473
474
475(defun iswitchb-complete ()
476 "Try and complete the current pattern amongst the buffer names."
477 (interactive)
478 (let (res)
479 (cond ((not iswitchb-matches)
480
481 ;; todo
482 ;;(message "No buffer completions.")
483 ;;(sit-for 0.3)
484 (iswitchb-completion-help)
485 )
486
487 ((eq 1 (length iswitchb-matches))
488 ;; only one choice, so select it.
489 (exit-minibuffer))
490
491 (t
492 ;; else there could be some completions
493
494 (setq res (iswitchb-find-common-substring
495 iswitchb-matches iswitchb-text))
496 (if (and (not (memq res '(t nil)))
497 (not (eq res iswitchb-text)))
498 ;; found something to complete, so put it in the minibuff.
499 (progn
500 (setq iswitchb-rescan nil)
501 (delete-region (point-min) (point))
502 (insert res))
503 ;; else nothing to complete
504 (iswitchb-completion-help)
505 )
506 )
507 )))
508
509
510
511;;; TOGGLE FUNCTIONS
512
513(defun iswitchb-toggle-case ()
514 "Toggle the value of `iswitchb-case'."
515 (interactive)
516 (setq iswitchb-case (not iswitchb-case))
517 ;; ask for list to be regenerated.
518 (setq iswitchb-rescan t)
519 )
520
521(defun iswitchb-toggle-regexp ()
522 "Toggle the value of `iswitchb-regexp'."
523 (interactive)
524 (setq iswitchb-regexp (not iswitchb-regexp))
525 ;; ask for list to be regenerated.
526 (setq iswitchb-rescan t)
527 )
528
529
530(defun iswitchb-toggle-ignore ()
531 "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'."
532 (interactive)
533 (if iswitchb-buffer-ignore
534 (progn
535 (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore)
536 (setq iswitchb-buffer-ignore nil)
537 )
538 ;; else
539 (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig)
540 )
541 ;; ask for list to be regenerated.
542 (setq iswitchb-rescan t)
543 )
544
545
546(defun iswitchb-select-buffer-text ()
6c56c80b
RS
547 "Select the buffer named by the prompt.
548If no buffer exactly matching the prompt exists, maybe create a new one."
962a4216
RS
549 (interactive)
550 (setq iswitchb-exit 'takeprompt)
551 (exit-minibuffer))
552
553
554(defun iswitchb-next-match ()
555 "Put first element of `iswitchb-matches' at the end of the list."
556 (interactive)
557 (let ((tmp (car iswitchb-matches)))
558 (setq iswitchb-matches (cdr iswitchb-matches))
559 (setq iswitchb-matches (append iswitchb-matches (list tmp)))
560 (setq iswitchb-rescan nil)
561 ))
562
563(defun iswitchb-prev-match ()
564 "Put last element of `iswitchb-matches' at the front of the list."
565 (interactive)
566 (setq iswitchb-matches (iswitchb-rotate-list iswitchb-matches))
567 (setq iswitchb-rescan nil)
568 )
569
570
571
572
573;;; CREATE LIST OF ALL CURRENT BUFFERS
574
575(defun iswitchb-make-buflist ()
6c56c80b
RS
576 "Set `iswitchb-buflist' to the current list of buffers.
577Currently visible buffers are put at the end of the list."
962a4216
RS
578 (setq iswitchb-buflist
579 (let (buflist
580 iswitchb-current-buffers)
581 (setq iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
582 (setq buflist (mapcar 'buffer-name (buffer-list)))
583 (setq buflist (delq nil
584 (mapcar
585 '(lambda (x)
586 (if (not (iswitchb-ignore-buffername-p x))
587 x))
588 buflist)))
589 (mapcar 'iswitchb-to-end iswitchb-current-buffers)
590
591 (run-hooks 'iswitchb-make-buflist-hook)
592 buflist)))
593
594
595(defun iswitchb-to-end (elem)
596 "Move ELEM to the end of BUFLIST."
597 (setq buflist (delq elem buflist))
598 ;;(message "removing %s" elem)
599 (setq buflist (append buflist (list elem))))
600
601
602
603
604(defun iswitchb-get-buffers-in-frames (&optional current)
962a4216
RS
605 "Return the list of buffers that are visible in the current frame.
606If optional argument `current' is given, restrict searching to the
607current frame, rather than all frames, regardless of value of
608`iswitchb-all-frames'."
962a4216 609 (let ((iswitchb-bufs-in-frame nil))
962a4216
RS
610 (walk-windows 'iswitchb-get-bufname nil
611 (if current
612 nil
613 iswitchb-all-frames))
614 iswitchb-bufs-in-frame))
615
616
617(defun iswitchb-get-bufname (win)
618 "Used by `iswitchb-get-buffers-in-frames' to walk through all windows."
619 (setq iswitchb-bufs-in-frame
620 (cons (buffer-name (window-buffer win))
621 iswitchb-bufs-in-frame)))
622
623
624;;; FIND MATCHING BUFFERS
625
626(defun iswitchb-set-matches ()
627 "Set `iswitchb-matches' to the list of buffers matching prompt."
962a4216
RS
628 (if iswitchb-rescan
629 (setq iswitchb-matches
630 (let* ((buflist iswitchb-buflist)
631 )
632 (if (> (length iswitchb-text) 0)
633 (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp
634 buflist)
635 ;; else no text, no matches
636 nil)))))
637
6c56c80b
RS
638(defun iswitchb-get-matched-buffers (regexp &optional string-format buffer-list)
639 "Return matched buffers.
640If STRING-FORMAT is non-nil, consider EGEXP as string.
641BUFFER-LIST can be list of buffers or list of strings."
962a4216
RS
642 (let* ((case-fold-search iswitchb-case)
643 ;; need reverse since we are building up list backwards
644 (list (reverse buffer-list))
645 (do-string (stringp (car list)))
646 name
647 ret
648 )
649 (mapcar
650 (function
651 (lambda (x)
652
653 (if do-string
654 (setq name x) ;We already have the name
655 (setq name (buffer-name x)))
656
657 (cond
658 ((and (or (and string-format (string-match regexp name))
659 (and (null string-format)
660 (string-match (regexp-quote regexp) name)))
661
662 ;; todo (not (iswitchb-ignore-buffername-p name))
663 )
664 (setq ret (cons name ret))
665 ))))
666 list)
667 ret
668 ))
669
670
671
672
673(defun iswitchb-ignore-buffername-p (bufname)
674 "Return t if the buffer BUFNAME should be ignored."
675 (let ((data (match-data))
676 (re-list iswitchb-buffer-ignore)
677 ignorep
678 nextstr
679 )
680 (while re-list
681 (setq nextstr (car re-list))
682 (cond
683 ((stringp nextstr)
684 (if (string-match nextstr bufname)
685 (progn
686 (setq ignorep t)
687 (setq re-list nil))))
688 ((fboundp nextstr)
689 (if (funcall nextstr bufname)
690 (progn
691 (setq ignorep t)
692 (setq re-list nil))
693 ))
694 )
695 (setq re-list (cdr re-list)))
696 (store-match-data data)
697
698 ;; return the result
699 ignorep)
700 )
701
702
703
704(defun iswitchb-word-matching-substring (word)
705 "Return part of WORD before 1st match to `iswitchb-change-word-sub'.
706If `iswitchb-change-word-sub' cannot be found in WORD, return nil."
707 (let ((case-fold-search iswitchb-case))
708 (let ((m (string-match iswitchb-change-word-sub word)))
709 (if m
710 (substring word m)
711 ;; else no match
712 nil))))
713
714
715
716
717
718
719(defun iswitchb-find-common-substring (lis subs)
720 "Return common string following SUBS in each element of LIS."
721 (let (res
722 alist
723 iswitchb-change-word-sub
724 )
725 (setq iswitchb-change-word-sub
726 (if iswitchb-regexp
727 subs
728 (regexp-quote subs)))
729 (setq res (mapcar 'iswitchb-word-matching-substring lis))
730 (setq res (delq nil res)) ;; remove any nil elements (shouldnt happen)
731 (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY
732
733 ;; try-completion returns t if there is an exact match.
734 (let ((completion-ignore-case iswitchb-case))
735
736 (try-completion subs alist)
737 )))
738
739
740(defun iswitchb-makealist (res)
741 "Return dotted pair (RES . 1)."
742 (cons res 1))
743
744;; from Wayne Mesard <wmesard@esd.sgi.com>
745(defun iswitchb-rotate-list (lis)
746 "Destructively removes the last element from LIS.
747Return the modified list with the last element prepended to it."
748 (if (<= (length lis) 1)
749 lis
750 (let ((las lis)
751 (prev lis))
752 (while (consp (cdr las))
753 (setq prev las
754 las (cdr las)))
755 (setcdr prev nil)
756 (cons (car las) lis))
757 ))
758
759
760(defun iswitchb-completion-help ()
761 "Show possible completions in a *Buffer Completions* buffer."
762 ;; we could allow this buffer to be used to select match, but I think
763 ;; choose-completion-string will need redifining, so it just inserts
764 ;; choice with out any previous input.
765 (interactive)
766 (let ((completion-setup-hook nil) ;disable fancy highlight/selection.
767 )
768 (with-output-to-temp-buffer "*Buffer Completions*"
769 (if iswitchb-xemacs
770
771 ;; XEmacs extents are put on by default, doesn't seem to be
772 ;; any way of switching them off.
773 (display-completion-list (if iswitchb-matches
774 iswitchb-matches
775 iswitchb-buflist)
776 :help-string "iswitchb "
777 :activate-callback
778 '(lambda (x y z)
779 (message "doesnt work yet, sorry!")))
780 ;; else running Emacs
781 (display-completion-list (if iswitchb-matches
782 iswitchb-matches
783 iswitchb-buflist))
784 ))))
785
786;;; VISIT CHOSEN BUFFER
787(defun iswitchb-visit-buffer (buffer)
788 "Visit buffer named BUFFER according to `iswitchb-method'."
789 (let* (win newframe)
790 (cond
791 ((eq iswitchb-method 'samewindow)
792 (switch-to-buffer buffer))
793
794 ((memq iswitchb-method '(always-frame maybe-frame))
795 (cond
796 ((and (setq win (iswitchb-window-buffer-p buffer))
797 (or (eq iswitchb-method 'always-frame)
798 (y-or-n-p "Jump to frame? ")))
799 (setq newframe (window-frame win))
800 (raise-frame newframe)
801 (select-frame newframe)
802 (select-window win)
803 (if (not iswitchb-xemacs)
804 ;; reposition mouse to make frame active. not needed in XEmacs
805 ;; This line came from the other-frame defun in Emacs.
806 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
807 )
808 (t
809 ;; No buffer in other frames...
810 (switch-to-buffer buffer)
811 )))
812
813
814
815 ((eq iswitchb-method 'otherwindow)
816 (switch-to-buffer-other-window buffer))
817
818 ((eq iswitchb-method 'otherframe)
819 (progn
820 (switch-to-buffer-other-frame buffer)
821 (if (not iswitchb-xemacs)
822 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
823 )
824 ) )))
825
826(defun iswitchb-possible-new-buffer (buf)
827 "Possibly create and visit a new buffer called BUF."
828
829 (let ((newbufcreated))
830 (if (and iswitchb-newbuffer
831 (or
832 (not iswitchb-prompt-newbuffer)
833
834 (and iswitchb-prompt-newbuffer
835 (y-or-n-p
836 (format
837 "No buffer matching `%s', create one? "
838 buf)))))
839 ;; then create a new buffer
840 (progn
841 (setq newbufcreated (get-buffer-create buf))
842 (if (fboundp 'set-buffer-major-mode)
843 (set-buffer-major-mode newbufcreated))
844 (iswitchb-visit-buffer newbufcreated))
845 ;; else wont create new buffer
846 (message (format "no buffer matching `%s'" buf))
847 )))
848
849(defun iswitchb-window-buffer-p (buffer)
6c56c80b
RS
850 "Return window pointer if BUFFER is visible in another frame.
851If BUFFER is visible in the current frame, return nil."
962a4216 852 (interactive)
962a4216
RS
853 (let ((blist (iswitchb-get-buffers-in-frames 'current)))
854 ;;If the buffer is visible in current frame, return nil
855 (if (memq buffer blist)
856 nil
857 ;; maybe in other frame...
858 (get-buffer-window buffer 'visible)
859 )))
860
6c56c80b 861;;;###autoload
962a4216 862(defun iswitchb-default-keybindings ()
6c56c80b 863 "Set up default keybindings for `iswitchb-buffer'.
962a4216
RS
864Call this function to override the normal bindings."
865 (interactive)
866 (global-set-key "\18b" 'iswitchb-buffer)
867 (global-set-key "\184b" 'iswitchb-buffer-other-window)
868 (global-set-key "\185b" 'iswitchb-buffer-other-frame))
869
870
871
872;;;###autoload
873(defun iswitchb-buffer ()
874 "Switch to another buffer.
875
876The buffer name is selected interactively by typing a substring. The
877buffer is displayed according to `iswitchb-default-method' -- the
878default is to show it in the same window, unless it is already visible
6c56c80b
RS
879in another frame.
880For details of keybindings, do `\\[describe-function] iswitchb'."
962a4216
RS
881 (interactive)
882 (setq iswitchb-method iswitchb-default-method)
883 (iswitchb-entry))
884
885
886;;;###autoload
887(defun iswitchb-buffer-other-window ()
888 "Switch to another buffer and show it in another window.
889The buffer name is selected interactively by typing a substring.
6c56c80b 890For details of keybindings, do `\\[describe-function] iswitchb'."
962a4216
RS
891 (interactive)
892 (setq iswitchb-method 'otherwindow)
893 (iswitchb-entry))
894
895
896
897;;;###autoload
898(defun iswitchb-buffer-other-frame ()
899 "Switch to another buffer and show it in another frame.
900The buffer name is selected interactively by typing a substring.
6c56c80b 901For details of keybindings, do `\\[describe-function] iswitchb'."
962a4216
RS
902 (interactive)
903 (setq iswitchb-method 'otherframe)
904 (iswitchb-entry))
905
906
907
908(defun iswitchb-entry ()
6c56c80b 909 "Simply fall into `iswitchb' -- the main function."
962a4216
RS
910 (iswitchb))
911
912
913
914
915
916;;; XEMACS HACK FOR SHOWING DEFAULT BUFFER
917
918;; The first time we enter the minibuffer, Emacs puts up the default
919;; buffer to switch to, but XEmacs doesnt -- presumably there is a
920;; subtle difference in the two, either in icomplete or somewhere
921;; else. The default is shown for both whenever we delete all of our
922;; text though, indicating its just a problem the first time we enter
923;; the function. To solve this, we use another entry hook for emacs
924;; to show the default the first time we enter the minibuffer.
925
926(defun iswitchb-init-Xemacs-trick ()
6c56c80b
RS
927 "Display default buffer when first entering minibuffer.
928This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'."
962a4216
RS
929 (if (iswitchb-entryfn-p)
930 (progn
931 (iswitchb-show-default-buffer)
932 (goto-char (point-min)))))
933
934;; add this hook for Xemacs only.
935(if iswitchb-xemacs
936 (add-hook 'iswitchb-minibuffer-setup-hook
937 'iswitchb-init-Xemacs-trick))
938
939
940;;; XEMACS / BACKSPACE key
941;; For some reason, if the backspace key is pressed in xemacs, the
942;; line gets confused, so I've added a simple key definition to make
943;; backspace act like the normal delete key.
944
945(defun iswitchb-xemacs-backspacekey ()
946 "Bind backspace to `backward-delete-char'."
947 (define-key iswitchb-mode-map '[backspace] 'backward-delete-char)
948 (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word)
949 )
950
951
952(if iswitchb-xemacs
953 (add-hook 'iswitchb-define-mode-map-hook
954 'iswitchb-xemacs-backspacekey))
955
956
957
958;;; ICOMPLETE TYPE CODE
959
960(defun iswitchb-exhibit ()
6c56c80b 961 "Find matching buffers and display a list in the minibuffer.
962a4216
RS
962Copied from `icomplete-exhibit' with two changes:
9631. It prints a default buffer name when there is no text yet entered.
9642. It calls my completion routine rather than the standard completion."
965
966 (if iswitchb-use-mycompletion
967 (let ((contents (buffer-substring (point-min)(point-max)))
968 (buffer-undo-list t))
969 (save-excursion
970 (goto-char (point-max))
971 ; Register the end of input, so we
972 ; know where the extra stuff
973 ; (match-status info) begins:
974 (if (not (boundp 'iswitchb-eoinput))
975 ;; In case it got wiped out by major mode business:
976 (make-local-variable 'iswitchb-eoinput))
977 (setq iswitchb-eoinput (point))
978 ;; Update the list of matches
979 (setq iswitchb-text contents)
980 (iswitchb-set-matches)
981 (setq iswitchb-rescan t)
982 (iswitchb-set-common-completion)
983
984 ;; Insert the match-status information:
985 (if (> (point-max) 1)
986 (insert-string
987 (iswitchb-completions
988 contents
989 minibuffer-completion-table
990 minibuffer-completion-predicate
991 (not minibuffer-completion-confirm)))
992 ;; else put in default
993 (iswitchb-show-default-buffer))
994 ))))
995
996(defun iswitchb-show-default-buffer ()
997 "Insert the default buffer to switch to."
998 ;; insert done this way to preserve any text-propertes.
999 (insert (concat " {" iswitchb-default-buffer "} [Default]")))
1000
1001(defun iswitchb-completions
1002 (name candidates predicate require-match)
1003 "Return the string that is displayed after the user's text.
1004Modified from `icomplete-completions'."
1005
1006 (let ((comps iswitchb-matches)
1007 ; "-determined" - only one candidate
1008 (open-bracket-determined (if require-match "(" "["))
1009 (close-bracket-determined (if require-match ")" "]"))
1010 ;"-prospects" - more than one candidate
1011 (open-bracket-prospects "{")
1012 (close-bracket-prospects "}")
1013 first
1014 )
1015
1016 (if (and iswitchb-use-fonts comps)
1017 (progn
1018 (setq first (car comps))
1019 (setq first (format "%s" first))
1020 (put-text-property 0 (length first) 'face
1021 (if (eq (length comps) 1)
1022 'font-lock-comment-face
1023 'font-lock-function-name-face)
1024 first)
1025 (setq comps (cons first (cdr comps)))
1026 ))
1027
1028 (cond ((null comps) (format " %sNo match%s"
1029 open-bracket-determined
1030 close-bracket-determined))
1031
1032 ((null (cdr comps)) ;one match
1033 (concat (if (and (> (length (car comps))
1034 (length name)))
1035 (concat open-bracket-determined
1036 ;; when there is one match, show the
1037 ;; matching buffer name in full
1038 (car comps)
1039 close-bracket-determined)
1040 "")
1041 (if (not iswitchb-use-fonts) " [Matched]")
1042 ))
1043 (t ;multiple matches
1044 (let* (
1045 ;;(most (try-completion name candidates predicate))
1046 (most nil)
1047 (most-len (length most))
1048 most-is-exact
1049 first
1050 (alternatives
1051 (apply
1052 (function concat)
1053 (cdr (apply
1054 (function nconc)
1055 (mapcar '(lambda (com)
1056 (if (= (length com) most-len)
1057 ;; Most is one exact match,
1058 ;; note that and leave out
1059 ;; for later indication:
1060 (progn
1061 (setq most-is-exact t)
1062 ())
1063 (list ","
1064 (substring com
1065 most-len))))
1066 comps))))))
1067
1068 (concat
1069
1070 ;; put in common completion item -- what you get by
1071 ;; pressing tab
1072 (if (> (length iswitchb-common-match-string) (length name))
1073 (concat open-bracket-determined
1074 (substring iswitchb-common-match-string
1075 (length name))
1076 close-bracket-determined)
1077 )
1078 ;; end of partial matches...
1079
1080 ;; think this bit can be ignored.
1081 (and (> most-len (length name))
1082 (concat open-bracket-determined
1083 (substring most (length name))
1084 close-bracket-determined))
1085
1086 ;; list all alternatives
1087 open-bracket-prospects
1088 (if most-is-exact
1089 (concat "," alternatives)
1090 alternatives)
1091 close-bracket-prospects)))
1092 )))
1093
1094(defun iswitchb-minibuffer-setup ()
6c56c80b
RS
1095 "Set up minibuffer for `iswitchb-buffer'.
1096Copied from `icomplete-minibuffer-setup-hook'."
962a4216
RS
1097 (if (iswitchb-entryfn-p)
1098 (progn
1099
1100 (make-local-variable 'iswitchb-use-mycompletion)
1101 (setq iswitchb-use-mycompletion t)
1102 (make-local-hook 'pre-command-hook)
1103 (add-hook 'pre-command-hook
1104 'iswitchb-pre-command
1105 nil t)
1106 (make-local-hook 'post-command-hook)
1107 (add-hook 'post-command-hook
1108 'iswitchb-post-command
1109 nil t)
1110
1111 (run-hooks 'iswitchb-minibuffer-setup-hook)
1112 )
1113 ))
1114
1115
1116(defun iswitchb-pre-command ()
6c56c80b 1117 "Run before command in `iswitchb-buffer'."
962a4216
RS
1118 (iswitchb-tidy))
1119
1120
1121(defun iswitchb-post-command ()
6c56c80b 1122 "Run after command in `iswitchb-buffer'."
962a4216
RS
1123 (iswitchb-exhibit)
1124 )
1125
1126
1127
1128(defun iswitchb-tidy ()
1129 "Remove completions display, if any, prior to new user input.
1130Copied from `icomplete-tidy'."
1131
1132 (if (and (boundp 'iswitchb-eoinput)
1133 iswitchb-eoinput)
1134
1135 (if (> iswitchb-eoinput (point-max))
1136 ;; Oops, got rug pulled out from under us - reinit:
1137 (setq iswitchb-eoinput (point-max))
1138 (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
1139 (delete-region iswitchb-eoinput (point-max))))
1140
1141 ;; Reestablish the local variable 'cause minibuffer-setup is weird:
1142 (make-local-variable 'iswitchb-eoinput)
1143 (setq iswitchb-eoinput 1)))
1144
1145
1146(defun iswitchb-entryfn-p ()
6c56c80b 1147 "Return non-nil if `this-command' shows we are using `iswitchb-buffer'."
962a4216
RS
1148 (and (symbolp this-command) ; ignore lambda functions
1149 (member (symbol-name this-command)
1150 '("iswitchb-buffer"
1151 "iswitchb-buffer-other-frame"
1152 "iswitchb-buffer-other-window"))))
1153
1154(defun iswitchb-summaries-to-end ()
6c56c80b
RS
1155 "Move the summaries to the end of the list.
1156This is an example function which can be hooked on to
1157`iswitchb-make-buflist-hook'. Any buffer matching the regexps
1158`Summary' or `output\*$'are put to the end of the list."
962a4216
RS
1159 (let ((summaries (delq nil (mapcar
1160 '(lambda (x)
1161 (if (or
1162 (string-match "Summary" x)
1163 (string-match "output\\*$" x))
1164 x))
1165 buflist)
1166 )))
1167
1168 (mapcar 'iswitchb-to-end summaries)))
1169
1170
1171
1172;;; HOOKS
1173(add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
1174
1175(provide 'iswitchb)
1176
6c56c80b 1177;;; iswitchb.el ends here