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