Commit | Line | Data |
---|---|---|
6c56c80b | 1 | ;;; iswitchb.el --- switch between buffers using substrings |
962a4216 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1996-1997, 2000-2014 Free Software Foundation, Inc. |
962a4216 | 4 | |
b2537dfa RS |
5 | ;; Author: Stephen Eglen <stephen@gnu.org> |
6 | ;; Maintainer: Stephen Eglen <stephen@gnu.org> | |
19db4308 | 7 | ;; Keywords: completion convenience |
2b568bc9 | 8 | ;; Obsolete-since: 24.4 |
962a4216 | 9 | |
6c56c80b RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
962a4216 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
962a4216 | 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 | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
962a4216 | 24 | |
c6e5b93f | 25 | ;;; Commentary: |
962a4216 | 26 | |
2b568bc9 GM |
27 | ;; This file is obsolete - use icomplete-mode or ido-mode instead. |
28 | ||
c6e5b93f | 29 | ;; Installation: |
19db4308 DL |
30 | ;; To get the functions in this package bound to keys, use |
31 | ;; M-x iswitchb-mode or customize the option `iswitchb-mode'. | |
79f0a5ed SE |
32 | ;; Alternatively, add the following line to your .emacs: |
33 | ;; (iswitchb-mode 1) | |
962a4216 | 34 | |
962a4216 | 35 | ;; As you type in a substring, the list of buffers currently matching |
19db4308 | 36 | ;; the substring is displayed as you type. The list is ordered so |
962a4216 RS |
37 | ;; that the most recent buffers visited come at the start of the list. |
38 | ;; The buffer at the start of the list will be the one visited when | |
39 | ;; you press return. By typing more of the substring, the list is | |
40 | ;; narrowed down so that gradually the buffer you want will be at the | |
19db4308 | 41 | ;; top of the list. Alternatively, you can use C-s and C-r to rotate |
962a4216 RS |
42 | ;; buffer names in the list until the one you want is at the top of |
43 | ;; the list. Completion is also available so that you can see what is | |
44 | ;; common to all of the matching buffers as you type. | |
45 | ||
46 | ;; This code is similar to a couple of other packages. Michael R Cook | |
8097dd15 | 47 | ;; <cook@sightpath.com> wrote a similar buffer switching package, but |
962a4216 RS |
48 | ;; does exact matching rather than substring matching on buffer names. |
49 | ;; I also modified a couple of functions from icomplete.el to provide | |
50 | ;; the completion feedback in the minibuffer. | |
51 | ||
19db4308 | 52 | ;;; Example |
962a4216 | 53 | |
4d8ae757 RS |
54 | ;; If I have two buffers called "123456" and "123", with "123456" the |
55 | ;; most recent, when I use iswitchb, I first of all get presented with | |
56 | ;; the list of all the buffers | |
962a4216 | 57 | ;; |
19db4308 | 58 | ;; iswitch {123456,123} |
962a4216 RS |
59 | ;; |
60 | ;; If I then press 2: | |
61 | ;; iswitch 2[3]{123456,123} | |
62 | ;; | |
63 | ;; The list in {} are the matching buffers, most recent first (buffers | |
64 | ;; visible in the current frame are put at the end of the list by | |
65 | ;; default). At any time I can select the item at the head of the | |
2d839509 SE |
66 | ;; list by pressing RET. I can also put the first element at the end |
67 | ;; of the list by pressing C-s, or put the last element at the head of | |
68 | ;; the list by pressing C-r. The item in [] indicates what can be | |
69 | ;; added to my input by pressing TAB. In this case, I will get "3" | |
70 | ;; added to my input. So, press TAB: | |
962a4216 RS |
71 | ;; iswitch 23{123456,123} |
72 | ;; | |
73 | ;; At this point, I still have two matching buffers. | |
74 | ;; If I want the first buffer in the list, I simply press RET. If I | |
75 | ;; wanted the second in the list, I could press C-s to move it to the | |
76 | ;; top of the list and then RET to select it. | |
77 | ;; | |
4d8ae757 | 78 | ;; However, if I type 4, I only have one match left: |
962a4216 RS |
79 | ;; iswitch 234[123456] [Matched] |
80 | ;; | |
4d8ae757 RS |
81 | ;; Since there is only one matching buffer left, it is given in [] and we |
82 | ;; see the text [Matched] afterwards. I can now press TAB or RET to go | |
83 | ;; to that buffer. | |
962a4216 RS |
84 | ;; |
85 | ;; If however, I now type "a": | |
86 | ;; iswitch 234a [No match] | |
87 | ;; There are no matching buffers. If I press RET or TAB, I can be | |
88 | ;; prompted to create a new buffer called "234a". | |
89 | ;; | |
90 | ;; Of course, where this function comes in really useful is when you | |
91 | ;; can specify the buffer using only a few keystrokes. In the above | |
92 | ;; example, the quickest way to get to the "123456" buffer would be | |
ccd2f997 | 93 | ;; just to type 4 and then RET (assuming there isn't any newer buffer |
962a4216 RS |
94 | ;; with 4 in its name). |
95 | ||
96 | ;; To see a full list of all matching buffers in a separate buffer, | |
97 | ;; hit ? or press TAB when there are no further completions to the | |
ccd2f997 KH |
98 | ;; substring. Repeated TAB presses will scroll you through this |
99 | ;; separate buffer. | |
962a4216 | 100 | |
36a3b01c RS |
101 | ;; The buffer at the head of the list can be killed by pressing C-k. |
102 | ;; If the buffer needs saving, you will be queried before the buffer | |
103 | ;; is killed. | |
104 | ||
105 | ;; If you find that the file you are after is not in a buffer, you can | |
106 | ;; press C-x C-f to immediately drop into find-file. | |
107 | ||
4d8ae757 RS |
108 | ;; See the doc string of iswitchb for full keybindings and features. |
109 | ;; (describe-function 'iswitchb) | |
962a4216 | 110 | |
8097dd15 GM |
111 | ;; Case matching: The case of strings when matching can be ignored or |
112 | ;; used depending on the value of iswitchb-case (default is the same | |
113 | ;; as case-fold-search, normally t). Imagine you have the following | |
19db4308 | 114 | ;; buffers: |
8097dd15 GM |
115 | ;; |
116 | ;; INBOX *info* *scratch* | |
117 | ;; | |
118 | ;; Then these will be the matching buffers, depending on how you type | |
119 | ;; the two letters `in' and the value of iswitchb-case: | |
120 | ;; | |
121 | ;; iswitchb-case user input | matching buffers | |
122 | ;; ---------------------------------------------- | |
19db4308 DL |
123 | ;; nil in | *info* |
124 | ;; t in | INBOX, *info* | |
125 | ;; t IN | INBOX | |
126 | ;; t In | [No match] | |
8097dd15 | 127 | |
e1dbe924 | 128 | ;;; Customization |
962a4216 RS |
129 | |
130 | ;; See the User Variables section below for easy ways to change the | |
36a3b01c RS |
131 | ;; functionality of the program. These are accessible using the |
132 | ;; custom package. | |
8ae3ef6e | 133 | ;; To modify the keybindings, use something like: |
962a4216 | 134 | ;; |
8ae3ef6e | 135 | ;;(add-hook 'iswitchb-mode-hook 'iswitchb-my-keys) |
962a4216 | 136 | ;;(defun iswitchb-my-keys () |
ccd2f997 | 137 | ;; "Add my keybindings for iswitchb." |
8ae3ef6e | 138 | ;; (define-key iswitchb-mode-map " " 'iswitchb-next-match)) |
962a4216 | 139 | ;; |
6b0cc1b5 | 140 | ;; Seeing all the matching buffers |
36a3b01c | 141 | ;; |
962a4216 | 142 | ;; If you have many matching buffers, they may not all fit onto one |
f05b502e GM |
143 | ;; line of the minibuffer. In Emacs 21, the variable |
144 | ;; `resize-mini-windows' controls how many lines of the minibuffer can | |
145 | ;; be seen. For older versions of emacs, you can use | |
146 | ;; `resize-minibuffer-mode'. You can also limit iswitchb so that it | |
962a4216 RS |
147 | ;; only shows a certain number of lines -- see the documentation for |
148 | ;; `iswitchb-minibuffer-setup-hook'. | |
149 | ||
6b0cc1b5 | 150 | ;; Changing the list of buffers |
962a4216 RS |
151 | |
152 | ;; By default, the list of current buffers is most recent first, | |
153 | ;; oldest last, with the exception that the buffers visible in the | |
154 | ;; current frame are put at the end of the list. A hook exists to | |
155 | ;; allow other functions to order the list. For example, if you add: | |
156 | ;; | |
157 | ;; (add-hook 'iswitchb-make-buflist-hook 'iswitchb-summaries-to-end) | |
158 | ;; | |
159 | ;; then all buffers matching "Summary" are moved to the end of the | |
160 | ;; list. (I find this handy for keeping the INBOX Summary and so on | |
161 | ;; out of the way.) It also moves buffers matching "output\*$" to the | |
90a02640 | 162 | ;; end of the list (these are created by AUCTeX when compiling.) |
962a4216 RS |
163 | ;; Other functions could be made available which alter the list of |
164 | ;; matching buffers (either deleting or rearranging elements.) | |
165 | ||
166 | ;; Font-Lock | |
167 | ||
b21688f0 | 168 | ;; font-lock is used to highlight the first matching buffer. To |
da6062e6 | 169 | ;; switch this off, set (setq iswitchb-use-faces nil). Coloring of |
b21688f0 EZ |
170 | ;; the matching buffer name was suggested by Carsten Dominik |
171 | ;; (dominik@strw.leidenuniv.nl) | |
962a4216 | 172 | |
6b0cc1b5 | 173 | ;; Replacement for read-buffer |
962a4216 | 174 | |
ccd2f997 KH |
175 | ;; iswitchb-read-buffer has been written to be a drop in replacement |
176 | ;; for the normal buffer selection routine `read-buffer'. To use | |
c6e5b93f | 177 | ;; iswitch for all buffer selections in Emacs, add: |
ccd2f997 | 178 | ;; (setq read-buffer-function 'iswitchb-read-buffer) |
4d8ae757 | 179 | ;; (This variable was introduced in Emacs 20.3.) |
fffa137c | 180 | ;; XEmacs users can get the same behavior by doing: |
19db4308 | 181 | ;; (defalias 'read-buffer 'iswitchb-read-buffer) |
c6e5b93f | 182 | ;; since `read-buffer' is defined in lisp. |
36a3b01c | 183 | |
4d8ae757 RS |
184 | ;; Using iswitchb for other completion tasks. |
185 | ||
a2c060f6 | 186 | ;; Kin Cho (kin@neoscale.com) sent the following suggestion to use |
f1180544 | 187 | ;; iswitchb for other completion tasks. |
4d8ae757 RS |
188 | ;; |
189 | ;; (defun my-icompleting-read (prompt choices) | |
190 | ;; "Use iswitch as a completing-read replacement to choose from | |
191 | ;; choices. PROMPT is a string to prompt with. CHOICES is a list of | |
192 | ;; strings to choose from." | |
193 | ;; (let ((iswitchb-make-buflist-hook | |
194 | ;; (lambda () | |
195 | ;; (setq iswitchb-temp-buflist choices)))) | |
196 | ;; (iswitchb-read-buffer prompt))) | |
197 | ;; | |
198 | ;; example: | |
199 | ;; (my-icompleting-read "Which fruit? " ' | |
200 | ;; ("apple" "pineapple" "pear" "bananas" "oranges") ) | |
201 | ||
a2c060f6 PJ |
202 | ;; Kin Cho also suggested the following defun. Once you have a subset of |
203 | ;; matching buffers matching your current prompt, you can then press | |
204 | ;; e.g. C-o to restrict matching to those buffers and clearing the prompt: | |
205 | ;; (defun iswitchb-exclude-nonmatching() | |
206 | ;; "Make iswitchb work on only the currently matching names." | |
207 | ;; (interactive) | |
208 | ;; (setq iswitchb-buflist iswitchb-matches) | |
209 | ;; (setq iswitchb-rescan t) | |
210 | ;; (delete-minibuffer-contents)) | |
211 | ;; | |
212 | ;; (add-hook 'iswitchb-define-mode-map-hook | |
4f91a816 | 213 | ;; (lambda () (define-key |
f1180544 | 214 | ;; iswitchb-mode-map "\C-o" |
a2c060f6 PJ |
215 | ;; 'iswitchb-exclude-nonmatching))) |
216 | ||
fffa137c | 217 | ;; Other lisp packages extend iswitchb behavior to other tasks. See |
4d8ae757 RS |
218 | ;; ido.el (by Kim Storm) and mcomplete.el (Yuji Minejima). |
219 | ||
220 | ;; Window managers: Switching frames/focus follows mouse; Sawfish. | |
221 | ||
222 | ;; If you switch to a buffer that is visible in another frame, | |
223 | ;; iswitchb can switch focus to that frame. If your window manager | |
224 | ;; uses "click to focus" policy for window selection, you should also | |
225 | ;; set focus-follows-mouse to nil. | |
226 | ||
227 | ;; iswitch functionality has also been implemented for switching | |
228 | ;; between windows in the Sawfish window manager. | |
229 | ||
6b0cc1b5 SE |
230 | ;; Regexp matching |
231 | ||
b21688f0 EZ |
232 | ;; There is provision for regexp matching within iswitchb, enabled |
233 | ;; through `iswitchb-regexp'. This allows you to type `c$' for | |
234 | ;; example and see all buffer names ending in `c'. No completion | |
235 | ;; mechanism is currently offered when regexp searching. | |
6b0cc1b5 | 236 | |
ccd2f997 | 237 | ;;; TODO |
36a3b01c | 238 | |
09ae5da1 | 239 | ;;; Acknowledgments |
962a4216 RS |
240 | |
241 | ;; Thanks to Jari Aalto <jari.aalto@poboxes.com> for help with the | |
242 | ;; first version of this package, iswitch-buffer. Thanks also to many | |
243 | ;; others for testing earlier versions. | |
244 | ||
245 | ;;; Code: | |
246 | ||
b21688f0 EZ |
247 | (require 'font-lock) |
248 | ||
962a4216 RS |
249 | ;;; User Variables |
250 | ;; | |
251 | ;; These are some things you might want to change. | |
252 | ||
36a3b01c | 253 | (defgroup iswitchb nil |
c6e5b93f | 254 | "Switch between buffers using substrings." |
f5f727f8 | 255 | :group 'convenience |
19db4308 | 256 | :group 'completion |
36a3b01c | 257 | :link '(emacs-commentary-link :tag "Commentary" "iswitchb.el") |
19db4308 | 258 | :link '(url-link "http://www.anc.ed.ac.uk/~stephen/emacs/") |
181688f2 | 259 | :link '(emacs-library-link :tag "Lisp File" "iswitchb.el")) |
36a3b01c RS |
260 | |
261 | (defcustom iswitchb-case case-fold-search | |
9201cc28 | 262 | "Non-nil if searching of buffer names should ignore case. |
8097dd15 GM |
263 | If this is non-nil but the user input has any upper case letters, matching |
264 | is temporarily case sensitive." | |
36a3b01c RS |
265 | :type 'boolean |
266 | :group 'iswitchb) | |
962a4216 | 267 | |
36a3b01c | 268 | (defcustom iswitchb-buffer-ignore |
962a4216 | 269 | '("^ ") |
9201cc28 | 270 | "List of regexps or functions matching buffer names to ignore. |
6c56c80b RS |
271 | For example, traditional behavior is not to list buffers whose names begin |
272 | with a space, for which the regexp is `^ '. See the source file for | |
252ca122 | 273 | example functions that filter buffer names." |
e47525c4 | 274 | :type '(repeat (choice regexp function)) |
36a3b01c | 275 | :group 'iswitchb) |
1a1dd431 | 276 | (put 'iswitchb-buffer-ignore 'risky-local-variable t) |
36a3b01c | 277 | |
c80d9534 | 278 | (defcustom iswitchb-max-to-show nil |
9201cc28 | 279 | "If non-nil, limit the number of names shown in the minibuffer. |
f329780b SE |
280 | If this value is N, and N is greater than the number of matching |
281 | buffers, the first N/2 and the last N/2 matching buffers are | |
282 | shown. This can greatly speed up iswitchb if you have a | |
283 | multitude of buffers open." | |
79e1c9d4 | 284 | :type '(choice (const :tag "Show all" nil) integer) |
c80d9534 JW |
285 | :group 'iswitchb) |
286 | ||
c4ae2d51 | 287 | (defcustom iswitchb-use-virtual-buffers nil |
9201cc28 | 288 | "If non-nil, refer to past buffers when none match. |
c4ae2d51 JW |
289 | This feature relies upon the `recentf' package, which will be |
290 | enabled if this variable is configured to a non-nil value." | |
291 | :type 'boolean | |
292 | :require 'recentf | |
293 | :set (function | |
294 | (lambda (sym value) | |
533b22e2 | 295 | (if value (recentf-mode 1)) |
c4ae2d51 JW |
296 | (set sym value))) |
297 | :group 'iswitchb) | |
298 | ||
299 | (defvar iswitchb-virtual-buffers nil) | |
300 | ||
c9403808 | 301 | (defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help |
9201cc28 | 302 | "Hook run when `iswitchb-complete' can't complete any more. |
c9403808 RS |
303 | The most useful values are `iswitchb-completion-help', which pops up a |
304 | window with completion alternatives, or `iswitchb-next-match' or | |
305 | `iswitchb-prev-match', which cycle the buffer list." | |
306 | :type 'hook | |
307 | :group 'iswitchb) | |
308 | ||
3da360a7 SM |
309 | ;; Examples for setting the value of iswitchb-buffer-ignore |
310 | ;;(defun iswitchb-ignore-c-mode (name) | |
311 | ;; "Ignore all c mode buffers -- example function for iswitchb." | |
312 | ;; (with-current-buffer name | |
313 | ;; (derived-mode-p 'c-mode))) | |
962a4216 | 314 | |
3da360a7 SM |
315 | ;;(setq iswitchb-buffer-ignore '("^ " iswitchb-ignore-c-mode)) |
316 | ;;(setq iswitchb-buffer-ignore '("^ " "\\.c\\'" "\\.h\\'")) | |
962a4216 | 317 | |
36a3b01c | 318 | (defcustom iswitchb-default-method 'always-frame |
9201cc28 | 319 | "How to switch to new buffer when using `iswitchb-buffer'. |
962a4216 RS |
320 | Possible values: |
321 | `samewindow' Show new buffer in same window | |
322 | `otherwindow' Show new buffer in another window (same frame) | |
888472e0 | 323 | `display' Display buffer in another window without switching to it |
962a4216 RS |
324 | `otherframe' Show new buffer in another frame |
325 | `maybe-frame' If a buffer is visible in another frame, prompt to ask if you | |
326 | you want to see the buffer in the same window of the current | |
327 | frame or in the other frame. | |
c6e5b93f | 328 | `always-frame' If a buffer is visible in another frame, raise that |
36a3b01c | 329 | frame. Otherwise, visit the buffer in the same window." |
19db4308 | 330 | :type '(choice (const samewindow) |
c6e5b93f SE |
331 | (const otherwindow) |
332 | (const display) | |
19db4308 | 333 | (const otherframe) |
c6e5b93f SE |
334 | (const maybe-frame) |
335 | (const always-frame)) | |
36a3b01c | 336 | :group 'iswitchb) |
962a4216 | 337 | |
36a3b01c | 338 | (defcustom iswitchb-regexp nil |
9201cc28 | 339 | "Non-nil means that `iswitchb' will do regexp matching. |
6b0cc1b5 | 340 | Value can be toggled within `iswitchb' using `iswitchb-toggle-regexp'." |
36a3b01c RS |
341 | :type 'boolean |
342 | :group 'iswitchb) | |
343 | ||
36a3b01c | 344 | (defcustom iswitchb-newbuffer t |
9201cc28 | 345 | "Non-nil means create new buffer if no buffer matches substring. |
36a3b01c RS |
346 | See also `iswitchb-prompt-newbuffer'." |
347 | :type 'boolean | |
348 | :group 'iswitchb) | |
962a4216 | 349 | |
36a3b01c | 350 | (defcustom iswitchb-prompt-newbuffer t |
9201cc28 | 351 | "Non-nil means prompt user to confirm before creating new buffer. |
36a3b01c RS |
352 | See also `iswitchb-newbuffer'." |
353 | :type 'boolean | |
354 | :group 'iswitchb) | |
355 | ||
cd6ef82d GM |
356 | (define-obsolete-variable-alias 'iswitchb-use-fonts 'iswitchb-use-faces "22.1") |
357 | ||
b21688f0 | 358 | (defcustom iswitchb-use-faces t |
9201cc28 | 359 | "Non-nil means use font-lock faces for showing first match." |
36a3b01c RS |
360 | :type 'boolean |
361 | :group 'iswitchb) | |
962a4216 | 362 | |
f5bd1691 | 363 | (defcustom iswitchb-use-frame-buffer-list nil |
9201cc28 | 364 | "Non-nil means use the currently selected frame's buffer list." |
f5bd1691 GM |
365 | :type 'boolean |
366 | :group 'iswitchb) | |
367 | ||
36a3b01c | 368 | (defcustom iswitchb-make-buflist-hook nil |
19db4308 | 369 | "Hook to run when list of matching buffers is created." |
36a3b01c RS |
370 | :type 'hook |
371 | :group 'iswitchb) | |
372 | ||
6a0e4c60 SE |
373 | (defcustom iswitchb-delim "," |
374 | "Delimiter to put between buffer names when displaying results." | |
375 | :type 'string | |
376 | :group 'iswitchb) | |
377 | ||
85e8b518 | 378 | (defcustom iswitchb-all-frames 'visible |
78f3273a CY |
379 | "Argument to pass to `walk-windows' when iswitchb is finding buffers. |
380 | See documentation of `walk-windows' for useful values." | |
381 | :type '(choice (const :tag "Selected frame only" nil) | |
382 | (const :tag "All existing frames" t) | |
383 | (const :tag "All visible frames" visible) | |
384 | (const :tag "All frames on this terminal" 0)) | |
385 | :group 'iswitchb) | |
962a4216 | 386 | |
6b0cc1b5 | 387 | (defcustom iswitchb-minibuffer-setup-hook nil |
19db4308 | 388 | "Iswitchb-specific customization of minibuffer setup. |
962a4216 | 389 | |
4837b516 | 390 | This hook is run during minibuffer setup if `iswitchb' is active. |
33a316ba SE |
391 | For instance: |
392 | \(add-hook 'iswitchb-minibuffer-setup-hook | |
393 | '\(lambda () (set (make-local-variable 'max-mini-window-height) 3))) | |
394 | will constrain the minibuffer to a maximum height of 3 lines when | |
395 | iswitchb is running." | |
6b0cc1b5 SE |
396 | :type 'hook |
397 | :group 'iswitchb) | |
398 | ||
54907cdc | 399 | (defface iswitchb-single-match |
b21688f0 EZ |
400 | '((t |
401 | (:inherit font-lock-comment-face))) | |
402 | "Iswitchb face for single matching buffer name." | |
403 | :version "22.1" | |
404 | :group 'iswitchb) | |
405 | ||
54907cdc | 406 | (defface iswitchb-current-match |
b21688f0 EZ |
407 | '((t |
408 | (:inherit font-lock-function-name-face))) | |
409 | "Iswitchb face for current matching buffer name." | |
410 | :version "22.1" | |
411 | :group 'iswitchb) | |
412 | ||
54907cdc | 413 | (defface iswitchb-virtual-matches |
b21688f0 EZ |
414 | '((t |
415 | (:inherit font-lock-builtin-face))) | |
416 | "Iswitchb face for matching virtual buffer names. | |
417 | See also `iswitchb-use-virtual-buffers'." | |
418 | :version "22.1" | |
419 | :group 'iswitchb) | |
420 | ||
54907cdc | 421 | (defface iswitchb-invalid-regexp |
b21688f0 EZ |
422 | '((t |
423 | (:inherit font-lock-warning-face))) | |
424 | "Iswitchb face for indicating invalid regexp. " | |
425 | :version "22.1" | |
426 | :group 'iswitchb) | |
427 | ||
6b0cc1b5 SE |
428 | ;; Do we need the variable iswitchb-use-mycompletion? |
429 | ||
6b0cc1b5 SE |
430 | ;;; Internal Variables |
431 | ||
432 | (defvar iswitchb-method nil | |
19db4308 | 433 | "Stores the method for viewing the selected buffer. |
6b0cc1b5 SE |
434 | Its value is one of `samewindow', `otherwindow', `display', `otherframe', |
435 | `maybe-frame' or `always-frame'. See `iswitchb-default-method' for | |
436 | details of values.") | |
962a4216 RS |
437 | |
438 | (defvar iswitchb-eoinput 1 | |
439 | "Point where minibuffer input ends and completion info begins. | |
6c56c80b | 440 | Copied from `icomplete-eoinput'.") |
962a4216 RS |
441 | (make-variable-buffer-local 'iswitchb-eoinput) |
442 | ||
962a4216 RS |
443 | (defvar iswitchb-buflist nil |
444 | "Stores the current list of buffers that will be searched through. | |
445 | The list is ordered, so that the most recent buffers come first, | |
446 | although by default, the buffers visible in the current frame are put | |
447 | at the end of the list. Created by `iswitchb-make-buflist'.") | |
448 | ||
449 | ;; todo -- is this necessary? | |
450 | ||
451 | (defvar iswitchb-use-mycompletion nil | |
19db4308 | 452 | "Non-nil means use `iswitchb-buffer' completion feedback. |
6c56c80b RS |
453 | Should only be set to t by iswitchb functions, so that it doesn't |
454 | interfere with other minibuffer usage.") | |
962a4216 | 455 | |
19db4308 | 456 | (defvar iswitchb-change-word-sub nil |
962a4216 RS |
457 | "Private variable used by `iswitchb-word-matching-substring'.") |
458 | ||
962a4216 RS |
459 | (defvar iswitchb-common-match-string nil |
460 | "Stores the string that is common to all matching buffers.") | |
461 | ||
962a4216 RS |
462 | (defvar iswitchb-rescan nil |
463 | "Non-nil means we need to regenerate the list of matching buffers.") | |
464 | ||
465 | (defvar iswitchb-text nil | |
466 | "Stores the users string as it is typed in.") | |
467 | ||
468 | (defvar iswitchb-matches nil | |
ccd2f997 | 469 | "List of buffers currently matching `iswitchb-text'.") |
962a4216 | 470 | |
19db4308 DL |
471 | (defvar iswitchb-mode-map |
472 | (let ((map (make-sparse-keymap))) | |
473 | (set-keymap-parent map minibuffer-local-map) | |
474 | (define-key map "?" 'iswitchb-completion-help) | |
475 | (define-key map "\C-s" 'iswitchb-next-match) | |
476 | (define-key map "\C-r" 'iswitchb-prev-match) | |
d7e76a89 J |
477 | (define-key map [?\C-.] 'iswitchb-next-match) |
478 | (define-key map [?\C-,] 'iswitchb-prev-match) | |
19db4308 DL |
479 | (define-key map "\t" 'iswitchb-complete) |
480 | (define-key map "\C-j" 'iswitchb-select-buffer-text) | |
481 | (define-key map "\C-t" 'iswitchb-toggle-regexp) | |
482 | (define-key map "\C-x\C-f" 'iswitchb-find-file) | |
483 | (define-key map "\C-c" 'iswitchb-toggle-case) | |
484 | (define-key map "\C-k" 'iswitchb-kill-buffer) | |
485 | (define-key map "\C-m" 'iswitchb-exit-minibuffer) | |
486 | map) | |
487 | "Minibuffer keymap for `iswitchb-buffer'.") | |
488 | ||
489 | (defvar iswitchb-global-map | |
490 | (let ((map (make-sparse-keymap))) | |
51443e4f SM |
491 | (dolist (b '((switch-to-buffer . iswitchb-buffer) |
492 | (switch-to-buffer-other-window . iswitchb-buffer-other-window) | |
493 | (switch-to-buffer-other-frame . iswitchb-buffer-other-frame) | |
494 | (display-buffer . iswitchb-display-buffer))) | |
495 | (if (fboundp 'command-remapping) | |
496 | (define-key map (vector 'remap (car b)) (cdr b)) | |
497 | (substitute-key-definition (car b) (cdr b) map global-map))) | |
19db4308 | 498 | map) |
971cb834 | 499 | "Global keymap for `iswitchb-mode'.") |
19db4308 DL |
500 | |
501 | (defvar iswitchb-history nil | |
6c56c80b | 502 | "History of buffers selected using `iswitchb-buffer'.") |
962a4216 | 503 | |
19db4308 DL |
504 | (defvar iswitchb-exit nil |
505 | "Flag to monitor how `iswitchb-buffer' exits. | |
6c56c80b RS |
506 | If equal to `takeprompt', we use the prompt as the buffer name to be |
507 | selected.") | |
962a4216 RS |
508 | |
509 | (defvar iswitchb-buffer-ignore-orig nil | |
510 | "Stores original value of `iswitchb-buffer-ignore'.") | |
511 | ||
6b0cc1b5 SE |
512 | (defvar iswitchb-default nil |
513 | "Default buffer for iswitchb.") | |
962a4216 | 514 | |
02e526ad SE |
515 | ;; The following variables are needed to keep the byte compiler quiet. |
516 | (defvar iswitchb-require-match nil | |
517 | "Non-nil if matching buffer must be selected.") | |
518 | ||
519 | (defvar iswitchb-temp-buflist nil | |
520 | "Stores a temporary version of the buffer list being created.") | |
521 | ||
522 | (defvar iswitchb-bufs-in-frame nil | |
523 | "List of the buffers visible in the current frame.") | |
524 | ||
c7b89861 EZ |
525 | (defvar iswitchb-minibuf-depth nil |
526 | "Value we expect to be returned by `minibuffer-depth' in the minibuffer.") | |
527 | ||
b21688f0 EZ |
528 | (defvar iswitchb-common-match-inserted nil |
529 | "Non-nil if we have just inserted a common match in the minibuffer.") | |
530 | ||
531 | (defvar iswitchb-invalid-regexp) | |
532 | ||
962a4216 RS |
533 | ;;; FUNCTIONS |
534 | ||
962a4216 RS |
535 | ;;; MAIN FUNCTION |
536 | (defun iswitchb () | |
537 | "Switch to buffer matching a substring. | |
538 | As you type in a string, all of the buffers matching the string are | |
539 | displayed. When you have found the buffer you want, it can then be | |
540 | selected. As you type, most keys have their normal keybindings, | |
541 | except for the following: | |
542 | \\<iswitchb-mode-map> | |
543 | ||
544 | RET Select the buffer at the front of the list of matches. If the | |
6c56c80b | 545 | list is empty, possibly prompt to create new buffer. |
962a4216 RS |
546 | |
547 | \\[iswitchb-select-buffer-text] Select the current prompt as the buffer. | |
548 | If no buffer is found, prompt for a new one. | |
549 | ||
550 | \\[iswitchb-next-match] Put the first element at the end of the list. | |
551 | \\[iswitchb-prev-match] Put the last element at the start of the list. | |
19db4308 | 552 | \\[iswitchb-complete] Complete a common suffix to the current string that |
962a4216 RS |
553 | matches all buffers. If there is only one match, select that buffer. |
554 | If there is no common suffix, show a list of all matching buffers | |
555 | in a separate window. | |
ccd2f997 | 556 | \\[iswitchb-toggle-regexp] Toggle regexp searching. |
962a4216 | 557 | \\[iswitchb-toggle-case] Toggle case-sensitive searching of buffer names. |
36a3b01c | 558 | \\[iswitchb-completion-help] Show list of matching buffers in separate window. |
19db4308 | 559 | \\[iswitchb-find-file] Exit iswitchb and drop into `find-file'. |
36a3b01c | 560 | \\[iswitchb-kill-buffer] Kill buffer at head of buffer list." |
962a4216 RS |
561 | ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \ |
562 | ;;`iswitchb-buffer-ignore') | |
f1180544 | 563 | |
8ae3ef6e | 564 | (let* ((prompt "iswitch ") |
b21688f0 | 565 | iswitchb-invalid-regexp |
8ae3ef6e | 566 | (buf (iswitchb-read-buffer prompt))) |
ccd2f997 | 567 | |
ccd2f997 KH |
568 | ;;(message "chosen text %s" iswitchb-final-text) |
569 | ;; Choose the buffer name: either the text typed in, or the head | |
570 | ;; of the list of matches | |
571 | ||
572 | (cond ( (eq iswitchb-exit 'findfile) | |
573 | (call-interactively 'find-file)) | |
b21688f0 EZ |
574 | (iswitchb-invalid-regexp |
575 | (message "Won't make invalid regexp named buffer")) | |
ccd2f997 KH |
576 | (t |
577 | ;; View the buffer | |
c6e5b93f | 578 | ;;(message "go to buf %s" buf) |
ccd2f997 KH |
579 | ;; Check buf is non-nil. |
580 | (if buf | |
581 | (if (get-buffer buf) | |
582 | ;; buffer exists, so view it and then exit | |
583 | (iswitchb-visit-buffer buf) | |
584 | ;; else buffer doesn't exist | |
585 | (iswitchb-possible-new-buffer buf))) | |
181688f2 | 586 | )))) |
ccd2f997 | 587 | |
c4ae2d51 JW |
588 | (defun iswitchb-read-buffer (prompt &optional default require-match |
589 | start matches-set) | |
ccd2f997 | 590 | "Replacement for the built-in `read-buffer'. |
19db4308 | 591 | Return the name of a buffer selected. |
c4ae2d51 JW |
592 | PROMPT is the prompt to give to the user. |
593 | DEFAULT if given is the default buffer to be selected, which will | |
594 | go to the front of the list. | |
252ca122 | 595 | If REQUIRE-MATCH is non-nil, an existing buffer must be selected. |
c4ae2d51 JW |
596 | If START is a string, the selection process is started with that |
597 | string. | |
598 | If MATCHES-SET is non-nil, the buflist is not updated before | |
599 | the selection process begins. Used by isearchb.el." | |
59f7af81 CY |
600 | ;; The map is generated every time so that it can inherit new |
601 | ;; functions. | |
602 | (let ((map (copy-keymap minibuffer-local-map)) | |
89468837 | 603 | buf-sel iswitchb-final-text |
59f7af81 CY |
604 | icomplete-mode) ; prevent icomplete starting up |
605 | (define-key map "?" 'iswitchb-completion-help) | |
606 | (define-key map "\C-s" 'iswitchb-next-match) | |
607 | (define-key map "\C-r" 'iswitchb-prev-match) | |
608 | (define-key map "\t" 'iswitchb-complete) | |
609 | (define-key map "\C-j" 'iswitchb-select-buffer-text) | |
610 | (define-key map "\C-t" 'iswitchb-toggle-regexp) | |
611 | (define-key map "\C-x\C-f" 'iswitchb-find-file) | |
612 | (define-key map "\C-n" 'iswitchb-toggle-ignore) | |
613 | (define-key map "\C-c" 'iswitchb-toggle-case) | |
614 | (define-key map "\C-k" 'iswitchb-kill-buffer) | |
615 | (define-key map "\C-m" 'iswitchb-exit-minibuffer) | |
616 | (setq iswitchb-mode-map map) | |
617 | (run-hooks 'iswitchb-define-mode-map-hook) | |
618 | ||
962a4216 | 619 | (setq iswitchb-exit nil) |
6b0cc1b5 SE |
620 | (setq iswitchb-default |
621 | (if (bufferp default) | |
622 | (buffer-name default) | |
623 | default)) | |
c4ae2d51 JW |
624 | (setq iswitchb-text (or start "")) |
625 | (unless matches-set | |
626 | (setq iswitchb-rescan t) | |
627 | (iswitchb-make-buflist iswitchb-default) | |
628 | (iswitchb-set-matches)) | |
19db4308 | 629 | (let |
ccd2f997 | 630 | ((minibuffer-local-completion-map iswitchb-mode-map) |
c7b89861 EZ |
631 | ;; Record the minibuffer depth that we expect to find once |
632 | ;; the minibuffer is set up and iswitchb-entryfn-p is called. | |
633 | (iswitchb-minibuf-depth (1+ (minibuffer-depth))) | |
181688f2 | 634 | (iswitchb-require-match require-match)) |
36a3b01c | 635 | ;; prompt the user for the buffer name |
19db4308 | 636 | (setq iswitchb-final-text (completing-read |
c4ae2d51 | 637 | prompt ;the prompt |
660e161f | 638 | '(("dummy" . 1)) ;table |
c4ae2d51 JW |
639 | nil ;predicate |
640 | nil ;require-match [handled elsewhere] | |
641 | start ;initial-contents | |
ccd2f997 | 642 | 'iswitchb-history))) |
4dddd07f SE |
643 | (if (and (not (eq iswitchb-exit 'usefirst)) |
644 | (get-buffer iswitchb-final-text)) | |
8ae3ef6e | 645 | ;; This happens for example if the buffer was chosen with the mouse. |
c4ae2d51 JW |
646 | (setq iswitchb-matches (list iswitchb-final-text) |
647 | iswitchb-virtual-buffers nil)) | |
648 | ||
649 | ;; If no buffer matched, but a virtual buffer was selected, visit | |
650 | ;; that file now and act as though that buffer had been selected. | |
651 | (if (and iswitchb-virtual-buffers | |
652 | (not (iswitchb-existing-buffer-p))) | |
4078fd57 SE |
653 | (let ((virt (car iswitchb-virtual-buffers)) |
654 | (new-buf)) | |
06b60517 | 655 | ;; Keep the name of the buffer returned by find-file-noselect, as |
4078fd57 SE |
656 | ;; the buffer 'virt' could be a symlink to a file of a different name. |
657 | (setq new-buf (buffer-name (find-file-noselect (cdr virt)))) | |
658 | (setq iswitchb-matches (list new-buf) | |
c4ae2d51 | 659 | iswitchb-virtual-buffers nil))) |
8ae3ef6e | 660 | |
ccd2f997 | 661 | ;; Handling the require-match must be done in a better way. |
c4ae2d51 JW |
662 | (if (and require-match |
663 | (not (iswitchb-existing-buffer-p))) | |
19db4308 | 664 | (error "Must specify valid buffer")) |
ccd2f997 | 665 | |
c4ae2d51 JW |
666 | (if (or (eq iswitchb-exit 'takeprompt) |
667 | (null iswitchb-matches)) | |
ccd2f997 KH |
668 | (setq buf-sel iswitchb-final-text) |
669 | ;; else take head of list | |
670 | (setq buf-sel (car iswitchb-matches))) | |
f1180544 | 671 | |
ccd2f997 | 672 | ;; Or possibly choose the default buffer |
19db4308 | 673 | (if (equal iswitchb-final-text "") |
c4ae2d51 | 674 | (setq buf-sel (car iswitchb-matches))) |
36a3b01c | 675 | |
ccd2f997 | 676 | buf-sel)) |
36a3b01c | 677 | |
ccd2f997 KH |
678 | (defun iswitchb-existing-buffer-p () |
679 | "Return non-nil if there is a matching buffer." | |
680 | (not (null iswitchb-matches))) | |
962a4216 RS |
681 | |
682 | ;;; COMPLETION CODE | |
683 | ||
684 | (defun iswitchb-set-common-completion () | |
6c56c80b RS |
685 | "Find common completion of `iswitchb-text' in `iswitchb-matches'. |
686 | The result is stored in `iswitchb-common-match-string'." | |
962a4216 | 687 | |
d7396512 | 688 | (let (val) |
962a4216 RS |
689 | (setq iswitchb-common-match-string nil) |
690 | (if (and iswitchb-matches | |
6b0cc1b5 | 691 | (not iswitchb-regexp) ;; testing |
962a4216 RS |
692 | (stringp iswitchb-text) |
693 | (> (length iswitchb-text) 0)) | |
694 | (if (setq val (iswitchb-find-common-substring | |
695 | iswitchb-matches iswitchb-text)) | |
696 | (setq iswitchb-common-match-string val))) | |
181688f2 | 697 | val)) |
962a4216 RS |
698 | |
699 | (defun iswitchb-complete () | |
700 | "Try and complete the current pattern amongst the buffer names." | |
701 | (interactive) | |
702 | (let (res) | |
703 | (cond ((not iswitchb-matches) | |
c9403808 | 704 | (run-hooks 'iswitchb-cannot-complete-hook)) |
b21688f0 EZ |
705 | (iswitchb-invalid-regexp |
706 | ;; Do nothing | |
707 | ) | |
ccd2f997 | 708 | ((= 1 (length iswitchb-matches)) |
962a4216 RS |
709 | ;; only one choice, so select it. |
710 | (exit-minibuffer)) | |
f1180544 | 711 | |
962a4216 RS |
712 | (t |
713 | ;; else there could be some completions | |
6b0cc1b5 | 714 | (setq res iswitchb-common-match-string) |
962a4216 | 715 | (if (and (not (memq res '(t nil))) |
36a3b01c | 716 | (not (equal res iswitchb-text))) |
ccd2f997 | 717 | ;; found something to complete, so put it in the minibuffer. |
962a4216 | 718 | (progn |
b21688f0 EZ |
719 | (setq iswitchb-rescan nil |
720 | iswitchb-common-match-inserted t) | |
53811b42 | 721 | (delete-region (minibuffer-prompt-end) (point)) |
962a4216 RS |
722 | (insert res)) |
723 | ;; else nothing to complete | |
c9403808 | 724 | (run-hooks 'iswitchb-cannot-complete-hook) |
181688f2 | 725 | ))))) |
962a4216 RS |
726 | |
727 | ;;; TOGGLE FUNCTIONS | |
728 | ||
729 | (defun iswitchb-toggle-case () | |
19db4308 | 730 | "Toggle the value of variable `iswitchb-case'." |
962a4216 RS |
731 | (interactive) |
732 | (setq iswitchb-case (not iswitchb-case)) | |
733 | ;; ask for list to be regenerated. | |
181688f2 | 734 | (setq iswitchb-rescan t)) |
962a4216 RS |
735 | |
736 | (defun iswitchb-toggle-regexp () | |
737 | "Toggle the value of `iswitchb-regexp'." | |
738 | (interactive) | |
739 | (setq iswitchb-regexp (not iswitchb-regexp)) | |
740 | ;; ask for list to be regenerated. | |
181688f2 | 741 | (setq iswitchb-rescan t)) |
962a4216 RS |
742 | |
743 | (defun iswitchb-toggle-ignore () | |
744 | "Toggle ignoring buffers specified with `iswitchb-buffer-ignore'." | |
745 | (interactive) | |
746 | (if iswitchb-buffer-ignore | |
747 | (progn | |
748 | (setq iswitchb-buffer-ignore-orig iswitchb-buffer-ignore) | |
181688f2 | 749 | (setq iswitchb-buffer-ignore nil)) |
962a4216 | 750 | ;; else |
181688f2 | 751 | (setq iswitchb-buffer-ignore iswitchb-buffer-ignore-orig)) |
6b0cc1b5 | 752 | (iswitchb-make-buflist iswitchb-default) |
962a4216 | 753 | ;; ask for list to be regenerated. |
181688f2 | 754 | (setq iswitchb-rescan t)) |
962a4216 | 755 | |
ccd2f997 KH |
756 | (defun iswitchb-exit-minibuffer () |
757 | "Exit minibuffer, but make sure we have a match if one is needed." | |
758 | (interactive) | |
759 | (if (or (not iswitchb-require-match) | |
760 | (iswitchb-existing-buffer-p)) | |
4dddd07f SE |
761 | (progn |
762 | (setq iswitchb-exit 'usefirst) | |
763 | (throw 'exit nil)))) | |
962a4216 RS |
764 | |
765 | (defun iswitchb-select-buffer-text () | |
6c56c80b RS |
766 | "Select the buffer named by the prompt. |
767 | If no buffer exactly matching the prompt exists, maybe create a new one." | |
962a4216 RS |
768 | (interactive) |
769 | (setq iswitchb-exit 'takeprompt) | |
770 | (exit-minibuffer)) | |
771 | ||
36a3b01c | 772 | (defun iswitchb-find-file () |
19db4308 | 773 | "Drop into `find-file' from buffer switching." |
36a3b01c RS |
774 | (interactive) |
775 | (setq iswitchb-exit 'findfile) | |
776 | (exit-minibuffer)) | |
777 | ||
d7396512 | 778 | (defvar recentf-list) |
c4ae2d51 | 779 | |
19db4308 | 780 | (defun iswitchb-next-match () |
962a4216 RS |
781 | "Put first element of `iswitchb-matches' at the end of the list." |
782 | (interactive) | |
36a3b01c | 783 | (let ((next (cadr iswitchb-matches))) |
c4ae2d51 JW |
784 | (if (and (null next) iswitchb-virtual-buffers) |
785 | (setq recentf-list | |
786 | (iswitchb-chop recentf-list | |
787 | (cdr (cadr iswitchb-virtual-buffers)))) | |
788 | (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist next))) | |
181688f2 | 789 | (setq iswitchb-rescan t))) |
962a4216 | 790 | |
19db4308 | 791 | (defun iswitchb-prev-match () |
962a4216 RS |
792 | "Put last element of `iswitchb-matches' at the front of the list." |
793 | (interactive) | |
36a3b01c | 794 | (let ((prev (car (last iswitchb-matches)))) |
c4ae2d51 JW |
795 | (if (and (null prev) iswitchb-virtual-buffers) |
796 | (setq recentf-list | |
797 | (iswitchb-chop recentf-list | |
798 | (cdr (car (last iswitchb-virtual-buffers))))) | |
799 | (setq iswitchb-buflist (iswitchb-chop iswitchb-buflist prev))) | |
181688f2 | 800 | (setq iswitchb-rescan t))) |
36a3b01c RS |
801 | |
802 | (defun iswitchb-chop (list elem) | |
803 | "Remove all elements before ELEM and put them at the end of LIST." | |
804 | (let ((ret nil) | |
805 | (next nil) | |
806 | (sofar nil)) | |
807 | (while (not ret) | |
808 | (setq next (car list)) | |
809 | (if (equal next elem) | |
810 | (setq ret (append list (nreverse sofar))) | |
811 | ;; else | |
812 | (progn | |
813 | (setq list (cdr list)) | |
814 | (setq sofar (cons next sofar))))) | |
815 | ret)) | |
962a4216 | 816 | |
962a4216 RS |
817 | ;;; CREATE LIST OF ALL CURRENT BUFFERS |
818 | ||
ccd2f997 | 819 | (defun iswitchb-make-buflist (default) |
6c56c80b | 820 | "Set `iswitchb-buflist' to the current list of buffers. |
36a3b01c | 821 | Currently visible buffers are put at the end of the list. |
19db4308 | 822 | The hook `iswitchb-make-buflist-hook' is run after the list has been |
36a3b01c | 823 | created to allow the user to further modify the order of the buffer names |
ccd2f997 KH |
824 | in this list. If DEFAULT is non-nil, and corresponds to an existing buffer, |
825 | it is put to the start of the list." | |
19db4308 | 826 | (setq iswitchb-buflist |
36a3b01c | 827 | (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames)) |
ceba0dac GM |
828 | (iswitchb-temp-buflist |
829 | (delq nil | |
830 | (mapcar | |
831 | (lambda (x) | |
832 | (let ((b-name (buffer-name x))) | |
833 | (if (not | |
834 | (or | |
835 | (iswitchb-ignore-buffername-p b-name) | |
836 | (memq b-name iswitchb-current-buffers))) | |
837 | b-name))) | |
838 | (buffer-list (and iswitchb-use-frame-buffer-list | |
839 | (selected-frame))))))) | |
840 | (setq iswitchb-temp-buflist | |
841 | (nconc iswitchb-temp-buflist iswitchb-current-buffers)) | |
962a4216 | 842 | (run-hooks 'iswitchb-make-buflist-hook) |
ceba0dac | 843 | ;; Should this be after the hooks, or should the hooks be the |
ccd2f997 KH |
844 | ;; final thing to be run? |
845 | (if default | |
846 | (progn | |
19db4308 | 847 | (setq iswitchb-temp-buflist |
02e526ad | 848 | (delete default iswitchb-temp-buflist)) |
19db4308 | 849 | (setq iswitchb-temp-buflist |
02e526ad SE |
850 | (cons default iswitchb-temp-buflist)))) |
851 | iswitchb-temp-buflist))) | |
962a4216 | 852 | |
36a3b01c | 853 | (defun iswitchb-to-end (lst) |
02e526ad | 854 | "Move the elements from LST to the end of `iswitchb-temp-buflist'." |
561ec225 SM |
855 | (dolist (elem lst) |
856 | (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist))) | |
ceba0dac | 857 | (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst))) |
962a4216 | 858 | |
962a4216 | 859 | (defun iswitchb-get-buffers-in-frames (&optional current) |
962a4216 | 860 | "Return the list of buffers that are visible in the current frame. |
19db4308 | 861 | If optional argument CURRENT is given, restrict searching to the |
962a4216 RS |
862 | current frame, rather than all frames, regardless of value of |
863 | `iswitchb-all-frames'." | |
962a4216 | 864 | (let ((iswitchb-bufs-in-frame nil)) |
962a4216 | 865 | (walk-windows 'iswitchb-get-bufname nil |
19db4308 | 866 | (if current |
962a4216 RS |
867 | nil |
868 | iswitchb-all-frames)) | |
869 | iswitchb-bufs-in-frame)) | |
870 | ||
962a4216 RS |
871 | (defun iswitchb-get-bufname (win) |
872 | "Used by `iswitchb-get-buffers-in-frames' to walk through all windows." | |
ae5ac7d0 RS |
873 | (let ((buf (buffer-name (window-buffer win)))) |
874 | (if (not (member buf iswitchb-bufs-in-frame)) | |
875 | ;; Only add buf if it is not already in list. | |
876 | ;; This prevents same buf in two different windows being | |
877 | ;; put into the list twice. | |
878 | (setq iswitchb-bufs-in-frame | |
879 | (cons buf iswitchb-bufs-in-frame))))) | |
962a4216 | 880 | |
962a4216 RS |
881 | ;;; FIND MATCHING BUFFERS |
882 | ||
883 | (defun iswitchb-set-matches () | |
884 | "Set `iswitchb-matches' to the list of buffers matching prompt." | |
962a4216 RS |
885 | (if iswitchb-rescan |
886 | (setq iswitchb-matches | |
d7396512 | 887 | (let ((buflist iswitchb-buflist)) |
36a3b01c | 888 | (iswitchb-get-matched-buffers iswitchb-text iswitchb-regexp |
c4ae2d51 JW |
889 | buflist)) |
890 | iswitchb-virtual-buffers nil))) | |
36a3b01c RS |
891 | |
892 | (defun iswitchb-get-matched-buffers (regexp | |
893 | &optional string-format buffer-list) | |
6b0cc1b5 SE |
894 | "Return buffers matching REGEXP. |
895 | If STRING-FORMAT is nil, consider REGEXP as just a string. | |
6c56c80b | 896 | BUFFER-LIST can be list of buffers or list of strings." |
d7396512 | 897 | (let ((case-fold-search (iswitchb-case)) |
561ec225 SM |
898 | name ret) |
899 | (if (null string-format) (setq regexp (regexp-quote regexp))) | |
b21688f0 | 900 | (setq iswitchb-invalid-regexp nil) |
561ec225 SM |
901 | (condition-case error |
902 | (dolist (x buffer-list (nreverse ret)) | |
903 | (setq name (if (stringp x) x (buffer-name x))) | |
904 | (when (and (string-match regexp name) | |
905 | (not (iswitchb-ignore-buffername-p name))) | |
906 | (push name ret))) | |
907 | (invalid-regexp | |
908 | (setq iswitchb-invalid-regexp t) | |
909 | (cdr error))))) | |
962a4216 RS |
910 | |
911 | (defun iswitchb-ignore-buffername-p (bufname) | |
912 | "Return t if the buffer BUFNAME should be ignored." | |
913 | (let ((data (match-data)) | |
914 | (re-list iswitchb-buffer-ignore) | |
915 | ignorep | |
181688f2 | 916 | nextstr) |
962a4216 RS |
917 | (while re-list |
918 | (setq nextstr (car re-list)) | |
919 | (cond | |
920 | ((stringp nextstr) | |
921 | (if (string-match nextstr bufname) | |
922 | (progn | |
923 | (setq ignorep t) | |
924 | (setq re-list nil)))) | |
1a1dd431 | 925 | ((functionp nextstr) |
962a4216 RS |
926 | (if (funcall nextstr bufname) |
927 | (progn | |
928 | (setq ignorep t) | |
181688f2 | 929 | (setq re-list nil))))) |
962a4216 | 930 | (setq re-list (cdr re-list))) |
25c80f5c | 931 | (set-match-data data) |
962a4216 RS |
932 | |
933 | ;; return the result | |
181688f2 | 934 | ignorep)) |
962a4216 | 935 | |
962a4216 RS |
936 | (defun iswitchb-word-matching-substring (word) |
937 | "Return part of WORD before 1st match to `iswitchb-change-word-sub'. | |
938 | If `iswitchb-change-word-sub' cannot be found in WORD, return nil." | |
8097dd15 | 939 | (let ((case-fold-search (iswitchb-case))) |
962a4216 RS |
940 | (let ((m (string-match iswitchb-change-word-sub word))) |
941 | (if m | |
942 | (substring word m) | |
943 | ;; else no match | |
944 | nil)))) | |
945 | ||
962a4216 RS |
946 | (defun iswitchb-find-common-substring (lis subs) |
947 | "Return common string following SUBS in each element of LIS." | |
948 | (let (res | |
949 | alist | |
181688f2 | 950 | iswitchb-change-word-sub) |
962a4216 RS |
951 | (setq iswitchb-change-word-sub |
952 | (if iswitchb-regexp | |
953 | subs | |
954 | (regexp-quote subs))) | |
955 | (setq res (mapcar 'iswitchb-word-matching-substring lis)) | |
ccd2f997 | 956 | (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen) |
962a4216 RS |
957 | (setq alist (mapcar 'iswitchb-makealist res)) ;; could use an OBARRAY |
958 | ||
959 | ;; try-completion returns t if there is an exact match. | |
8097dd15 | 960 | (let ((completion-ignore-case (iswitchb-case))) |
962a4216 | 961 | |
181688f2 | 962 | (try-completion subs alist)))) |
962a4216 RS |
963 | |
964 | (defun iswitchb-makealist (res) | |
965 | "Return dotted pair (RES . 1)." | |
966 | (cons res 1)) | |
967 | ||
968 | ;; from Wayne Mesard <wmesard@esd.sgi.com> | |
969 | (defun iswitchb-rotate-list (lis) | |
19db4308 | 970 | "Destructively remove the last element from LIS. |
962a4216 RS |
971 | Return the modified list with the last element prepended to it." |
972 | (if (<= (length lis) 1) | |
973 | lis | |
974 | (let ((las lis) | |
975 | (prev lis)) | |
976 | (while (consp (cdr las)) | |
977 | (setq prev las | |
978 | las (cdr las))) | |
979 | (setcdr prev nil) | |
181688f2 | 980 | (cons (car las) lis)))) |
962a4216 RS |
981 | |
982 | (defun iswitchb-completion-help () | |
8ae3ef6e | 983 | "Show possible completions in a *Completions* buffer." |
962a4216 | 984 | ;; we could allow this buffer to be used to select match, but I think |
36a3b01c | 985 | ;; choose-completion-string will need redefining, so it just inserts |
19db4308 | 986 | ;; choice with out any previous input. |
962a4216 | 987 | (interactive) |
36a3b01c | 988 | (setq iswitchb-rescan nil) |
8ae3ef6e SM |
989 | (let ((buf (current-buffer)) |
990 | (temp-buf "*Completions*") | |
991 | (win)) | |
ccd2f997 | 992 | |
b21688f0 EZ |
993 | (if (and (eq last-command this-command) |
994 | (not iswitchb-common-match-inserted)) | |
ccd2f997 KH |
995 | ;; scroll buffer |
996 | (progn | |
997 | (set-buffer temp-buf) | |
998 | (setq win (get-buffer-window temp-buf)) | |
999 | (if (pos-visible-in-window-p (point-max) win) | |
1000 | (set-window-start win (point-min)) | |
1001 | (scroll-other-window)) | |
181688f2 | 1002 | (set-buffer buf)) |
f1180544 | 1003 | |
ccd2f997 | 1004 | (with-output-to-temp-buffer temp-buf |
2d839509 | 1005 | (if (featurep 'xemacs) |
f1180544 | 1006 | |
ccd2f997 KH |
1007 | ;; XEmacs extents are put on by default, doesn't seem to be |
1008 | ;; any way of switching them off. | |
d5e63715 | 1009 | (display-completion-list (or iswitchb-matches iswitchb-buflist) |
ccd2f997 | 1010 | :help-string "iswitchb " |
19db4308 | 1011 | :activate-callback |
06b60517 | 1012 | (lambda (_x _y _z) |
19db4308 | 1013 | (message "doesn't work yet, sorry!"))) |
ccd2f997 | 1014 | ;; else running Emacs |
d5e63715 | 1015 | (display-completion-list (or iswitchb-matches iswitchb-buflist)))) |
b21688f0 | 1016 | (setq iswitchb-common-match-inserted nil)))) |
ccd2f997 | 1017 | |
36a3b01c RS |
1018 | ;;; KILL CURRENT BUFFER |
1019 | ||
1020 | (defun iswitchb-kill-buffer () | |
ccd2f997 | 1021 | "Kill the buffer at the head of `iswitchb-matches'." |
36a3b01c | 1022 | (interactive) |
e0143335 LL |
1023 | (let ((enable-recursive-minibuffers t) |
1024 | buf) | |
36a3b01c RS |
1025 | |
1026 | (setq buf (car iswitchb-matches)) | |
1027 | ;; check to see if buf is non-nil. | |
1028 | (if buf | |
19677c71 LL |
1029 | (let ((bufobjs (mapcar (lambda (name) |
1030 | (or (get-buffer name) name)) | |
1031 | iswitchb-buflist))) | |
36a3b01c RS |
1032 | (kill-buffer buf) |
1033 | ||
1034 | ;; Check if buffer exists. XEmacs gnuserv.el makes alias | |
1035 | ;; for kill-buffer which does not return t if buffer is | |
1036 | ;; killed, so we can't rely on kill-buffer return value. | |
1037 | (if (get-buffer buf) | |
1038 | ;; buffer couldn't be killed. | |
19db4308 | 1039 | (setq iswitchb-rescan t) |
e0143335 LL |
1040 | ;; Else `kill-buffer' succeeds so re-make the buffer list |
1041 | ;; taking into account packages like uniquify may rename | |
19677c71 LL |
1042 | ;; buffers, and try to preserve the ordering of buffers. |
1043 | (setq iswitchb-buflist | |
1044 | (delq nil (mapcar (lambda (b) | |
1045 | (if (bufferp b) | |
1046 | (buffer-name b) | |
1047 | b)) | |
1048 | bufobjs)))))))) | |
36a3b01c | 1049 | |
962a4216 RS |
1050 | ;;; VISIT CHOSEN BUFFER |
1051 | (defun iswitchb-visit-buffer (buffer) | |
1052 | "Visit buffer named BUFFER according to `iswitchb-method'." | |
d7396512 | 1053 | (let (win newframe) |
962a4216 RS |
1054 | (cond |
1055 | ((eq iswitchb-method 'samewindow) | |
1056 | (switch-to-buffer buffer)) | |
1057 | ||
1058 | ((memq iswitchb-method '(always-frame maybe-frame)) | |
1059 | (cond | |
1060 | ((and (setq win (iswitchb-window-buffer-p buffer)) | |
1061 | (or (eq iswitchb-method 'always-frame) | |
1062 | (y-or-n-p "Jump to frame? "))) | |
1063 | (setq newframe (window-frame win)) | |
2d839509 | 1064 | (if (fboundp 'select-frame-set-input-focus) |
4d8ae757 RS |
1065 | (select-frame-set-input-focus newframe) |
1066 | (raise-frame newframe) | |
1067 | (select-frame newframe) | |
1068 | ) | |
1069 | (select-window win)) | |
962a4216 RS |
1070 | (t |
1071 | ;; No buffer in other frames... | |
1072 | (switch-to-buffer buffer) | |
1073 | ))) | |
1074 | ||
962a4216 RS |
1075 | ((eq iswitchb-method 'otherwindow) |
1076 | (switch-to-buffer-other-window buffer)) | |
1077 | ||
888472e0 RS |
1078 | ((eq iswitchb-method 'display) |
1079 | (display-buffer buffer)) | |
1080 | ||
962a4216 RS |
1081 | ((eq iswitchb-method 'otherframe) |
1082 | (progn | |
1083 | (switch-to-buffer-other-frame buffer) | |
2d839509 | 1084 | (if (fboundp 'select-frame-set-input-focus) |
4d8ae757 | 1085 | (select-frame-set-input-focus (selected-frame))) |
181688f2 | 1086 | ))))) |
962a4216 RS |
1087 | |
1088 | (defun iswitchb-possible-new-buffer (buf) | |
1089 | "Possibly create and visit a new buffer called BUF." | |
1090 | ||
1091 | (let ((newbufcreated)) | |
1092 | (if (and iswitchb-newbuffer | |
1093 | (or | |
1094 | (not iswitchb-prompt-newbuffer) | |
f1180544 | 1095 | |
962a4216 RS |
1096 | (and iswitchb-prompt-newbuffer |
1097 | (y-or-n-p | |
1098 | (format | |
1099 | "No buffer matching `%s', create one? " | |
1100 | buf))))) | |
1101 | ;; then create a new buffer | |
1102 | (progn | |
1103 | (setq newbufcreated (get-buffer-create buf)) | |
1104 | (if (fboundp 'set-buffer-major-mode) | |
1105 | (set-buffer-major-mode newbufcreated)) | |
1106 | (iswitchb-visit-buffer newbufcreated)) | |
c7015153 | 1107 | ;; else won't create new buffer |
8a26c165 | 1108 | (message "no buffer matching `%s'" buf)))) |
962a4216 RS |
1109 | |
1110 | (defun iswitchb-window-buffer-p (buffer) | |
6c56c80b RS |
1111 | "Return window pointer if BUFFER is visible in another frame. |
1112 | If BUFFER is visible in the current frame, return nil." | |
962a4216 | 1113 | (interactive) |
962a4216 | 1114 | (let ((blist (iswitchb-get-buffers-in-frames 'current))) |
6c4cab03 | 1115 | ;; If the buffer is visible in current frame, return nil |
ade1b01c | 1116 | (unless (member buffer blist) |
6c4cab03 | 1117 | ;; maybe in other frame or icon |
ccd2f997 | 1118 | (get-buffer-window buffer 0) ; better than 'visible |
962a4216 RS |
1119 | ))) |
1120 | ||
962a4216 RS |
1121 | (defun iswitchb-buffer () |
1122 | "Switch to another buffer. | |
1123 | ||
1124 | The buffer name is selected interactively by typing a substring. The | |
1125 | buffer is displayed according to `iswitchb-default-method' -- the | |
1126 | default is to show it in the same window, unless it is already visible | |
6c56c80b RS |
1127 | in another frame. |
1128 | For details of keybindings, do `\\[describe-function] iswitchb'." | |
962a4216 RS |
1129 | (interactive) |
1130 | (setq iswitchb-method iswitchb-default-method) | |
ccd2f997 | 1131 | (iswitchb)) |
962a4216 | 1132 | |
962a4216 RS |
1133 | (defun iswitchb-buffer-other-window () |
1134 | "Switch to another buffer and show it in another window. | |
1135 | The buffer name is selected interactively by typing a substring. | |
6c56c80b | 1136 | For details of keybindings, do `\\[describe-function] iswitchb'." |
962a4216 RS |
1137 | (interactive) |
1138 | (setq iswitchb-method 'otherwindow) | |
ccd2f997 | 1139 | (iswitchb)) |
962a4216 | 1140 | |
888472e0 RS |
1141 | (defun iswitchb-display-buffer () |
1142 | "Display a buffer in another window but don't select it. | |
1143 | The buffer name is selected interactively by typing a substring. | |
1144 | For details of keybindings, do `\\[describe-function] iswitchb'." | |
1145 | (interactive) | |
1146 | (setq iswitchb-method 'display) | |
ccd2f997 | 1147 | (iswitchb)) |
888472e0 | 1148 | |
962a4216 RS |
1149 | (defun iswitchb-buffer-other-frame () |
1150 | "Switch to another buffer and show it in another frame. | |
1151 | The buffer name is selected interactively by typing a substring. | |
6c56c80b | 1152 | For details of keybindings, do `\\[describe-function] iswitchb'." |
962a4216 RS |
1153 | (interactive) |
1154 | (setq iswitchb-method 'otherframe) | |
962a4216 RS |
1155 | (iswitchb)) |
1156 | ||
36a3b01c | 1157 | ;;; XEmacs hack for showing default buffer |
962a4216 RS |
1158 | |
1159 | ;; The first time we enter the minibuffer, Emacs puts up the default | |
ccd2f997 | 1160 | ;; buffer to switch to, but XEmacs doesn't -- presumably there is a |
36a3b01c RS |
1161 | ;; subtle difference in the two versions of post-command-hook. The |
1162 | ;; default is shown for both whenever we delete all of our text | |
1163 | ;; though, indicating its just a problem the first time we enter the | |
1164 | ;; function. To solve this, we use another entry hook for emacs to | |
1165 | ;; show the default the first time we enter the minibuffer. | |
962a4216 | 1166 | |
6b0cc1b5 | 1167 | (defun iswitchb-init-XEmacs-trick () |
6c56c80b RS |
1168 | "Display default buffer when first entering minibuffer. |
1169 | This is a hack for XEmacs, and should really be handled by `iswitchb-exhibit'." | |
962a4216 RS |
1170 | (if (iswitchb-entryfn-p) |
1171 | (progn | |
36a3b01c | 1172 | (iswitchb-exhibit) |
962a4216 RS |
1173 | (goto-char (point-min))))) |
1174 | ||
36a3b01c | 1175 | ;; add this hook for XEmacs only. |
2d839509 | 1176 | (if (featurep 'xemacs) |
19db4308 | 1177 | (add-hook 'iswitchb-minibuffer-setup-hook |
6b0cc1b5 | 1178 | 'iswitchb-init-XEmacs-trick)) |
962a4216 | 1179 | |
36a3b01c | 1180 | ;;; XEmacs / backspace key |
ccd2f997 | 1181 | ;; For some reason, if the backspace key is pressed in XEmacs, the |
962a4216 | 1182 | ;; line gets confused, so I've added a simple key definition to make |
19db4308 | 1183 | ;; backspace act like the normal delete key. |
962a4216 RS |
1184 | |
1185 | (defun iswitchb-xemacs-backspacekey () | |
1186 | "Bind backspace to `backward-delete-char'." | |
1187 | (define-key iswitchb-mode-map '[backspace] 'backward-delete-char) | |
181688f2 | 1188 | (define-key iswitchb-mode-map '[(meta backspace)] 'backward-kill-word)) |
962a4216 | 1189 | |
2d839509 | 1190 | (if (featurep 'xemacs) |
19db4308 | 1191 | (add-hook 'iswitchb-define-mode-map-hook |
962a4216 RS |
1192 | 'iswitchb-xemacs-backspacekey)) |
1193 | ||
962a4216 RS |
1194 | ;;; ICOMPLETE TYPE CODE |
1195 | ||
1196 | (defun iswitchb-exhibit () | |
6c56c80b | 1197 | "Find matching buffers and display a list in the minibuffer. |
962a4216 RS |
1198 | Copied from `icomplete-exhibit' with two changes: |
1199 | 1. It prints a default buffer name when there is no text yet entered. | |
1200 | 2. It calls my completion routine rather than the standard completion." | |
962a4216 | 1201 | (if iswitchb-use-mycompletion |
600f9d03 | 1202 | (let ((contents (buffer-substring (minibuffer-prompt-end) (point-max))) |
962a4216 RS |
1203 | (buffer-undo-list t)) |
1204 | (save-excursion | |
1205 | (goto-char (point-max)) | |
1206 | ; Register the end of input, so we | |
1207 | ; know where the extra stuff | |
1208 | ; (match-status info) begins: | |
1209 | (if (not (boundp 'iswitchb-eoinput)) | |
1210 | ;; In case it got wiped out by major mode business: | |
1211 | (make-local-variable 'iswitchb-eoinput)) | |
1212 | (setq iswitchb-eoinput (point)) | |
1213 | ;; Update the list of matches | |
1214 | (setq iswitchb-text contents) | |
1215 | (iswitchb-set-matches) | |
1216 | (setq iswitchb-rescan t) | |
1217 | (iswitchb-set-common-completion) | |
1218 | ||
1219 | ;; Insert the match-status information: | |
f1180544 | 1220 | (insert (iswitchb-completions |
b21688f0 | 1221 | contents)))))) |
26119624 | 1222 | |
d7396512 JB |
1223 | (defvar most-len) |
1224 | (defvar most-is-exact) | |
c4ae2d51 | 1225 | |
c80d9534 JW |
1226 | (defun iswitchb-output-completion (com) |
1227 | (if (= (length com) most-len) | |
1228 | ;; Most is one exact match, | |
1229 | ;; note that and leave out | |
1230 | ;; for later indication: | |
1231 | (ignore | |
1232 | (setq most-is-exact t)) | |
1233 | (substring com most-len))) | |
1234 | ||
b21688f0 | 1235 | (defun iswitchb-completions (name) |
962a4216 RS |
1236 | "Return the string that is displayed after the user's text. |
1237 | Modified from `icomplete-completions'." | |
f1180544 | 1238 | |
962a4216 RS |
1239 | (let ((comps iswitchb-matches) |
1240 | ; "-determined" - only one candidate | |
b21688f0 EZ |
1241 | (open-bracket-determined "[") |
1242 | (close-bracket-determined "]") | |
962a4216 RS |
1243 | ;"-prospects" - more than one candidate |
1244 | (open-bracket-prospects "{") | |
1245 | (close-bracket-prospects "}") | |
181688f2 | 1246 | first) |
962a4216 | 1247 | |
b21688f0 | 1248 | (if (and iswitchb-use-faces comps) |
962a4216 RS |
1249 | (progn |
1250 | (setq first (car comps)) | |
1251 | (setq first (format "%s" first)) | |
1252 | (put-text-property 0 (length first) 'face | |
19db4308 | 1253 | (if (= (length comps) 1) |
b21688f0 | 1254 | (if iswitchb-invalid-regexp |
54907cdc KS |
1255 | 'iswitchb-invalid-regexp |
1256 | 'iswitchb-single-match) | |
1257 | 'iswitchb-current-match) | |
19db4308 | 1258 | first) |
181688f2 | 1259 | (setq comps (cons first (cdr comps))))) |
962a4216 | 1260 | |
c4ae2d51 JW |
1261 | ;; If no buffers matched, and virtual buffers are being used, then |
1262 | ;; consult the list of past visited files, to see if we can find | |
1263 | ;; the file which the user might thought was still open. | |
1264 | (when (and iswitchb-use-virtual-buffers (null comps) | |
e6d23bb5 | 1265 | recentf-list) |
c4ae2d51 JW |
1266 | (setq iswitchb-virtual-buffers nil) |
1267 | (let ((head recentf-list) name) | |
1268 | (while head | |
1269 | (if (and (setq name (file-name-nondirectory (car head))) | |
1270 | (string-match (if iswitchb-regexp | |
1271 | iswitchb-text | |
1272 | (regexp-quote iswitchb-text)) name) | |
1273 | (null (get-file-buffer (car head))) | |
1274 | (not (assoc name iswitchb-virtual-buffers)) | |
1275 | (not (iswitchb-ignore-buffername-p name)) | |
1276 | (file-exists-p (car head))) | |
1277 | (setq iswitchb-virtual-buffers | |
1278 | (cons (cons name (car head)) | |
1279 | iswitchb-virtual-buffers))) | |
e6d23bb5 CY |
1280 | (setq head (cdr head))) |
1281 | (setq iswitchb-virtual-buffers (nreverse iswitchb-virtual-buffers) | |
1282 | comps (mapcar 'car iswitchb-virtual-buffers)) | |
c4ae2d51 JW |
1283 | (let ((comp comps)) |
1284 | (while comp | |
1285 | (put-text-property 0 (length (car comp)) | |
54907cdc | 1286 | 'face 'iswitchb-virtual-matches |
c4ae2d51 | 1287 | (car comp)) |
e6d23bb5 | 1288 | (setq comp (cdr comp)))))) |
c4ae2d51 | 1289 | |
962a4216 RS |
1290 | (cond ((null comps) (format " %sNo match%s" |
1291 | open-bracket-determined | |
1292 | close-bracket-determined)) | |
1293 | ||
b21688f0 EZ |
1294 | (iswitchb-invalid-regexp |
1295 | (concat " " (car comps))) | |
1296 | ((null (cdr comps)) ;one match | |
1297 | (concat | |
1298 | (if (if (not iswitchb-regexp) | |
1299 | (= (length name) | |
1300 | (length (car comps))) | |
1301 | (string-match name (car comps)) | |
1302 | (string-equal (match-string 0 (car comps)) | |
1303 | (car comps))) | |
1304 | "" | |
1305 | (concat open-bracket-determined | |
19db4308 | 1306 | ;; when there is one match, show the |
962a4216 RS |
1307 | ;; matching buffer name in full |
1308 | (car comps) | |
b21688f0 EZ |
1309 | close-bracket-determined)) |
1310 | (if (not iswitchb-use-faces) " [Matched]"))) | |
962a4216 | 1311 | (t ;multiple matches |
c80d9534 JW |
1312 | (if (and iswitchb-max-to-show |
1313 | (> (length comps) iswitchb-max-to-show)) | |
1314 | (setq comps | |
1315 | (append | |
5f5d410a JW |
1316 | (let ((res nil) |
1317 | (comp comps) | |
1318 | (end (/ iswitchb-max-to-show 2))) | |
1319 | (while (>= (setq end (1- end)) 0) | |
1320 | (setq res (cons (car comp) res) | |
1321 | comp (cdr comp))) | |
1322 | (nreverse res)) | |
c80d9534 | 1323 | (list "...") |
5f5d410a JW |
1324 | (nthcdr (- (length comps) |
1325 | (/ iswitchb-max-to-show 2)) comps)))) | |
962a4216 RS |
1326 | (let* ( |
1327 | ;;(most (try-completion name candidates predicate)) | |
1328 | (most nil) | |
1329 | (most-len (length most)) | |
1330 | most-is-exact | |
c4ae2d51 JW |
1331 | (alternatives |
1332 | (mapconcat (if most 'iswitchb-output-completion | |
6a0e4c60 | 1333 | 'identity) comps iswitchb-delim))) |
962a4216 RS |
1334 | |
1335 | (concat | |
1336 | ||
1337 | ;; put in common completion item -- what you get by | |
1338 | ;; pressing tab | |
4e141ed7 SE |
1339 | (if (and (stringp iswitchb-common-match-string) |
1340 | (> (length iswitchb-common-match-string) (length name))) | |
962a4216 | 1341 | (concat open-bracket-determined |
19db4308 | 1342 | (substring iswitchb-common-match-string |
962a4216 | 1343 | (length name)) |
181688f2 | 1344 | close-bracket-determined)) |
962a4216 RS |
1345 | ;; end of partial matches... |
1346 | ||
1347 | ;; think this bit can be ignored. | |
1348 | (and (> most-len (length name)) | |
1349 | (concat open-bracket-determined | |
1350 | (substring most (length name)) | |
1351 | close-bracket-determined)) | |
f1180544 | 1352 | |
962a4216 RS |
1353 | ;; list all alternatives |
1354 | open-bracket-prospects | |
1355 | (if most-is-exact | |
6a0e4c60 | 1356 | (concat iswitchb-delim alternatives) |
962a4216 | 1357 | alternatives) |
181688f2 | 1358 | close-bracket-prospects)))))) |
962a4216 RS |
1359 | |
1360 | (defun iswitchb-minibuffer-setup () | |
6c56c80b RS |
1361 | "Set up minibuffer for `iswitchb-buffer'. |
1362 | Copied from `icomplete-minibuffer-setup-hook'." | |
8ae3ef6e SM |
1363 | (when (iswitchb-entryfn-p) |
1364 | (set (make-local-variable 'iswitchb-use-mycompletion) t) | |
1365 | (add-hook 'pre-command-hook 'iswitchb-pre-command nil t) | |
1366 | (add-hook 'post-command-hook 'iswitchb-post-command nil t) | |
1367 | (run-hooks 'iswitchb-minibuffer-setup-hook))) | |
962a4216 RS |
1368 | |
1369 | (defun iswitchb-pre-command () | |
6c56c80b | 1370 | "Run before command in `iswitchb-buffer'." |
962a4216 RS |
1371 | (iswitchb-tidy)) |
1372 | ||
962a4216 | 1373 | (defun iswitchb-post-command () |
6c56c80b | 1374 | "Run after command in `iswitchb-buffer'." |
181688f2 | 1375 | (iswitchb-exhibit)) |
962a4216 RS |
1376 | |
1377 | (defun iswitchb-tidy () | |
1378 | "Remove completions display, if any, prior to new user input. | |
1379 | Copied from `icomplete-tidy'." | |
1380 | ||
1381 | (if (and (boundp 'iswitchb-eoinput) | |
1382 | iswitchb-eoinput) | |
f1180544 | 1383 | |
962a4216 RS |
1384 | (if (> iswitchb-eoinput (point-max)) |
1385 | ;; Oops, got rug pulled out from under us - reinit: | |
1386 | (setq iswitchb-eoinput (point-max)) | |
1387 | (let ((buffer-undo-list buffer-undo-list )) ; prevent entry | |
1388 | (delete-region iswitchb-eoinput (point-max)))) | |
f1180544 | 1389 | |
962a4216 RS |
1390 | ;; Reestablish the local variable 'cause minibuffer-setup is weird: |
1391 | (make-local-variable 'iswitchb-eoinput) | |
1392 | (setq iswitchb-eoinput 1))) | |
1393 | ||
962a4216 | 1394 | (defun iswitchb-entryfn-p () |
26119624 | 1395 | "Return non-nil if we are using `iswitchb-buffer'." |
c7b89861 | 1396 | (eq iswitchb-minibuf-depth (minibuffer-depth))) |
ccd2f997 | 1397 | |
962a4216 | 1398 | (defun iswitchb-summaries-to-end () |
6c56c80b RS |
1399 | "Move the summaries to the end of the list. |
1400 | This is an example function which can be hooked on to | |
1401 | `iswitchb-make-buflist-hook'. Any buffer matching the regexps | |
1402 | `Summary' or `output\*$'are put to the end of the list." | |
19db4308 DL |
1403 | (let ((summaries (delq nil |
1404 | (mapcar | |
1405 | (lambda (x) | |
1406 | (if (string-match "Summary\\|output\\*$" x) | |
1407 | x)) | |
1408 | iswitchb-temp-buflist)))) | |
36a3b01c | 1409 | (iswitchb-to-end summaries))) |
962a4216 | 1410 | |
8097dd15 | 1411 | (defun iswitchb-case () |
4837b516 | 1412 | "Return non-nil if we should ignore case when matching. |
8097dd15 GM |
1413 | See the variable `iswitchb-case' for details." |
1414 | (if iswitchb-case | |
2d839509 | 1415 | (if (featurep 'xemacs) |
8097dd15 GM |
1416 | (isearch-no-upper-case-p iswitchb-text) |
1417 | (isearch-no-upper-case-p iswitchb-text t)))) | |
1418 | ||
6730259f | 1419 | ;;;###obsolete-autoload |
8ae3ef6e | 1420 | (define-minor-mode iswitchb-mode |
06e21633 CY |
1421 | "Toggle Iswitchb mode. |
1422 | With a prefix argument ARG, enable Iswitchb mode if ARG is | |
1423 | positive, and disable it otherwise. If called from Lisp, enable | |
1424 | the mode if ARG is omitted or nil. | |
1425 | ||
1426 | Iswitchb mode is a global minor mode that enables switching | |
1427 | between buffers using substrings. See `iswitchb' for details." | |
8ae3ef6e | 1428 | nil nil iswitchb-global-map :global t :group 'iswitchb |
19db4308 DL |
1429 | (if iswitchb-mode |
1430 | (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup) | |
8ae3ef6e | 1431 | (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup))) |
19db4308 | 1432 | |
6730259f | 1433 | ;;;###obsolete-autoload |
cb3a1380 | 1434 | (make-obsolete 'iswitchb-mode |
2b568bc9 | 1435 | "use `icomplete-mode' or `ido-mode' instead." "24.4") |
122a7e46 | 1436 | |
962a4216 RS |
1437 | (provide 'iswitchb) |
1438 | ||
6c56c80b | 1439 | ;;; iswitchb.el ends here |