]> code.delx.au - gnu-emacs/blob - lisp/msb.el
Merge changes from emacs-23 branch
[gnu-emacs] / lisp / msb.el
1 ;;; msb.el --- customizable buffer-selection with multiple menus
2
3 ;; Copyright (C) 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Lindberg <lars.lindberg@home.se>
6 ;; Maintainer: FSF
7 ;; Created: 8 Oct 1993
8 ;; Lindberg's last update version: 3.34
9 ;; Keywords: mouse buffer menu
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Purpose of this package:
29 ;; 1. Offer a function for letting the user choose buffer,
30 ;; not necessarily for switching to it.
31 ;; 2. Make a better mouse-buffer-menu. This is done as a global
32 ;; minor mode, msb-mode.
33 ;;
34 ;; Customization:
35 ;; Look at the variable `msb-menu-cond' for deciding what menus you
36 ;; want. It's not that hard to customize, despite my not-so-good
37 ;; doc-string. Feel free to send me a better doc-string.
38 ;; There are some constants for you to try here:
39 ;; msb--few-menus
40 ;; msb--very-many-menus (default)
41 ;;
42 ;; Look at the variable `msb-item-handling-function' for customization
43 ;; of the appearance of every menu item. Try for instance setting
44 ;; it to `msb-alon-item-handler'.
45 ;;
46 ;; Look at the variable `msb-item-sort-function' for customization
47 ;; of sorting the menus. Set it to t for instance, which means no
48 ;; sorting - you will get latest used buffer first.
49 ;;
50 ;; Also check out the variable `msb-display-invisible-buffers-p'.
51
52 ;; Known bugs:
53 ;; - Files-by-directory
54 ;; + No possibility to show client/changed buffers separately.
55 ;; + All file buffers only appear in a file sub-menu, they will
56 ;; for instance not appear in the Mail sub-menu.
57
58 ;; Future enhancements:
59
60 ;;; Thanks goes to
61 ;; Mark Brader <msb@sq.com>
62 ;; Jim Berry <m1jhb00@FRB.GOV>
63 ;; Hans Chalupsky <hans@cs.Buffalo.EDU>
64 ;; Larry Rosenberg <ljr@ictv.com>
65 ;; Will Henney <will@astroscu.unam.mx>
66 ;; Jari Aalto <jaalto@tre.tele.nokia.fi>
67 ;; Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
68 ;; Gael Marziou <gael@gnlab030.grenoble.hp.com>
69 ;; Dave Gillespie <daveg@thymus.synaptics.com>
70 ;; Alon Albert <alon@milcse.rtsg.mot.com>
71 ;; Kevin Broadey, <KevinB@bartley.demon.co.uk>
72 ;; Ake Stenhof <ake@cadpoint.se>
73 ;; Richard Stallman <rms@gnu.org>
74 ;; Steve Fisk <fisk@medved.bowdoin.edu>
75
76 ;; This version turned into a global minor mode and subsequently
77 ;; hacked on by Dave Love.
78 ;;; Code:
79
80 (eval-when-compile (require 'cl))
81
82 ;;;
83 ;;; Some example constants to be used for `msb-menu-cond'. See that
84 ;;; variable for more information. Please note that if the condition
85 ;;; returns `multi', then the buffer can appear in several menus.
86 ;;;
87 (defconst msb--few-menus
88 '(((and (boundp 'server-buffer-clients)
89 server-buffer-clients
90 'multi)
91 3030
92 "Clients (%d)")
93 ((and msb-display-invisible-buffers-p
94 (msb-invisible-buffer-p)
95 'multi)
96 3090
97 "Invisible buffers (%d)")
98 ((eq major-mode 'dired-mode)
99 2010
100 "Dired (%d)"
101 msb-dired-item-handler
102 msb-sort-by-directory)
103 ((eq major-mode 'Man-mode)
104 4090
105 "Manuals (%d)")
106 ((eq major-mode 'w3-mode)
107 4020
108 "WWW (%d)")
109 ((or (memq major-mode
110 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
111 (memq major-mode '(mh-letter-mode mh-show-mode mh-folder-mode))
112 (memq major-mode
113 '(gnus-summary-mode message-mode gnus-group-mode
114 gnus-article-mode score-mode gnus-browse-killed-mode)))
115 4010
116 "Mail (%d)")
117 ((not buffer-file-name)
118 4099
119 "Buffers (%d)")
120 ('no-multi
121 1099
122 "Files (%d)")))
123
124 (defconst msb--very-many-menus
125 '(((and (boundp 'server-buffer-clients)
126 server-buffer-clients
127 'multi)
128 1010
129 "Clients (%d)")
130 ((and (boundp 'vc-mode) vc-mode 'multi)
131 1020
132 "Version Control (%d)")
133 ((and buffer-file-name
134 (buffer-modified-p)
135 'multi)
136 1030
137 "Changed files (%d)")
138 ((and (get-buffer-process (current-buffer))
139 'multi)
140 1040
141 "Processes (%d)")
142 ((and msb-display-invisible-buffers-p
143 (msb-invisible-buffer-p)
144 'multi)
145 1090
146 "Invisible buffers (%d)")
147 ((eq major-mode 'dired-mode)
148 2010
149 "Dired (%d)"
150 ;; Note this different menu-handler
151 msb-dired-item-handler
152 ;; Also note this item-sorter
153 msb-sort-by-directory)
154 ((eq major-mode 'Man-mode)
155 5030
156 "Manuals (%d)")
157 ((eq major-mode 'w3-mode)
158 5020
159 "WWW (%d)")
160 ((or (memq major-mode
161 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
162 (memq major-mode '(mh-letter-mode mh-show-mode mh-folder-mode))
163 (memq major-mode '(gnus-summary-mode message-mode gnus-group-mode
164 gnus-article-mode score-mode
165 gnus-browse-killed-mode)))
166 5010
167 "Mail (%d)")
168 ;; Catchup for all non-file buffers
169 ((and (not buffer-file-name)
170 'no-multi)
171 5099
172 "Other non-file buffers (%d)")
173 ((and (string-match "/\\.[^/]*$" buffer-file-name)
174 'multi)
175 3090
176 "Hidden Files (%d)")
177 ((memq major-mode '(c-mode c++-mode))
178 3010
179 "C/C++ Files (%d)")
180 ((eq major-mode 'emacs-lisp-mode)
181 3020
182 "Elisp Files (%d)")
183 ((eq major-mode 'latex-mode)
184 3030
185 "LaTeX Files (%d)")
186 ('no-multi
187 3099
188 "Other files (%d)")))
189
190 ;;;
191 ;;; Customizable variables
192 ;;;
193
194 (defgroup msb nil
195 "Customizable buffer-selection with multiple menus."
196 :prefix "msb-"
197 :group 'mouse)
198
199 (defun msb-custom-set (symbol value)
200 "Set the value of custom variables for msb."
201 (set symbol value)
202 (if (and (featurep 'msb) msb-mode)
203 ;; wait until package has been loaded before bothering to update
204 ;; the buffer lists.
205 (msb-menu-bar-update-buffers t)))
206
207 (defcustom msb-menu-cond msb--very-many-menus
208 "List of criteria for splitting the mouse buffer menu.
209 The elements in the list should be of this type:
210 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
211
212 When making the split, the buffers are tested one by one against the
213 CONDITION, just like a Lisp cond: When hitting a true condition, the
214 other criteria are *not* tested and the buffer name will appear in the
215 menu with the menu-title corresponding to the true condition.
216
217 If the condition returns the symbol `multi', then the buffer will be
218 added to this menu *and* tested for other menus too. If it returns
219 `no-multi', then the buffer will only be added if it hasn't been added
220 to any other menu.
221
222 During this test, the buffer in question is the current buffer, and
223 the test is surrounded by calls to `save-excursion' and
224 `save-match-data'.
225
226 The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
227 A value of nil means don't display this menu.
228
229 MENU-TITLE is really a format. If you add %d in it, the %d is
230 replaced with the number of items in that menu.
231
232 ITEM-HANDLING-FN is optional. If it is supplied and is a function,
233 then it is used for displaying the items in that particular buffer
234 menu, otherwise the function pointed out by
235 `msb-item-handling-function' is used.
236
237 ITEM-SORT-FN is also optional.
238 If it is not supplied, the function pointed out by
239 `msb-item-sort-function' is used.
240 If it is nil, then no sort takes place and the buffers are presented
241 in least-recently-used order.
242 If it is t, then no sort takes place and the buffers are presented in
243 most-recently-used order.
244 If it is supplied and non-nil and not t than it is used for sorting
245 the items in that particular buffer menu.
246
247 Note1: There should always be a `catch-all' as last element, in this
248 list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
249 Note2: A buffer menu appears only if it has at least one buffer in it.
250 Note3: If you have a CONDITION that can't be evaluated you will get an
251 error every time you do \\[msb]."
252 :type `(choice (const :tag "long" :value ,msb--very-many-menus)
253 (const :tag "short" :value ,msb--few-menus)
254 (sexp :tag "user"))
255 :set 'msb-custom-set
256 :group 'msb)
257
258 (defcustom msb-modes-key 4000
259 "The sort key for files sorted by mode."
260 :type 'integer
261 :set 'msb-custom-set
262 :group 'msb
263 :version "20.3")
264
265 (defcustom msb-separator-diff 100
266 "Non-nil means use separators.
267 The separators will appear between all menus that have a sorting key
268 that differs by this value or more."
269 :type '(choice integer (const nil))
270 :set 'msb-custom-set
271 :group 'msb)
272
273 (defvar msb-files-by-directory-sort-key 0
274 "*The sort key for files sorted by directory.")
275
276 (defcustom msb-max-menu-items 15
277 "The maximum number of items in a menu.
278 If this variable is set to 15 for instance, then the submenu will be
279 split up in minor parts, 15 items each. A value of nil means no limit."
280 :type '(choice integer (const nil))
281 :set 'msb-custom-set
282 :group 'msb)
283
284 (defcustom msb-max-file-menu-items 10
285 "The maximum number of items from different directories.
286
287 When the menu is of type `file by directory', this is the maximum
288 number of buffers that are clumped together from different
289 directories.
290
291 Set this to 1 if you want one menu per directory instead of clumping
292 them together.
293
294 If the value is not a number, then the value 10 is used."
295 :type 'integer
296 :set 'msb-custom-set
297 :group 'msb)
298
299 (defcustom msb-most-recently-used-sort-key -1010
300 "Where should the menu with the most recently used buffers be placed?"
301 :type 'integer
302 :set 'msb-custom-set
303 :group 'msb)
304
305 (defcustom msb-display-most-recently-used 15
306 "How many buffers should be in the most-recently-used menu.
307 No buffers at all if less than 1 or nil (or any non-number)."
308 :type 'integer
309 :set 'msb-custom-set
310 :group 'msb)
311
312 (defcustom msb-most-recently-used-title "Most recently used (%d)"
313 "The title for the most-recently-used menu."
314 :type 'string
315 :set 'msb-custom-set
316 :group 'msb)
317
318 (defvar msb-horizontal-shift-function '(lambda () 0)
319 "*Function that specifies how many pixels to shift the top menu leftwards.")
320
321 (defcustom msb-display-invisible-buffers-p nil
322 "Show invisible buffers or not.
323 Non-nil means that the buffer menu should include buffers that have
324 names that starts with a space character."
325 :type 'boolean
326 :set 'msb-custom-set
327 :group 'msb)
328
329 (defvar msb-item-handling-function 'msb-item-handler
330 "*The appearance of a buffer menu.
331
332 The default function to call for handling the appearance of a menu
333 item. It should take two arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
334 where the latter is the max length of all buffer names.
335
336 The function should return the string to use in the menu.
337
338 When the function is called, BUFFER is the current buffer. This
339 function is called for items in the variable `msb-menu-cond' that have
340 nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
341 information.")
342
343 (defcustom msb-item-sort-function 'msb-sort-by-name
344 "The order of items in a buffer menu.
345
346 The default function to call for handling the order of items in a menu
347 item. This function is called like a sort function. The items look
348 like (ITEM-NAME . BUFFER).
349
350 ITEM-NAME is the name of the item that will appear in the menu.
351 BUFFER is the buffer, this is not necessarily the current buffer.
352
353 Set this to nil or t if you don't want any sorting (faster)."
354 :type '(choice (const msb-sort-by-name)
355 (const :tag "Newest first" t)
356 (const :tag "Oldest first" nil))
357 :set 'msb-custom-set
358 :group 'msb)
359
360 (defcustom msb-files-by-directory nil
361 "Non-nil means that files should be sorted by directory.
362 This is instead of the groups in `msb-menu-cond'."
363 :type 'boolean
364 :set 'msb-custom-set
365 :group 'msb)
366
367 (define-obsolete-variable-alias 'msb-after-load-hooks
368 'msb-after-load-hook "24.1")
369
370 (defcustom msb-after-load-hook nil
371 "Hook run after the msb package has been loaded."
372 :type 'hook
373 :set 'msb-custom-set
374 :group 'msb)
375
376 ;;;
377 ;;; Internal variables
378 ;;;
379
380 ;; The last calculated menu.
381 (defvar msb--last-buffer-menu nil)
382
383 ;; If this is non-nil, then it is a string that describes the error.
384 (defvar msb--error nil)
385
386 ;;;
387 ;;; Some example function to be used for `msb-item-handling-function'.
388 ;;;
389 (defun msb-item-handler (buffer &optional maxbuf)
390 "Create one string item, concerning BUFFER, for the buffer menu.
391 The item looks like:
392 *% <buffer-name>
393 The `*' appears only if the buffer is marked as modified.
394 The `%' appears only if the buffer is read-only.
395 Optional second argument MAXBUF is completely ignored."
396 (let ((name (buffer-name))
397 (modified (if (buffer-modified-p) "*" " "))
398 (read-only (if buffer-read-only "%" " ")))
399 (format "%s%s %s" modified read-only name)))
400
401
402 (eval-when-compile (require 'dired))
403
404 ;; `dired' can be called with a list of the form (directory file1 file2 ...)
405 ;; which causes `dired-directory' to be in the same form.
406 (defun msb--dired-directory ()
407 (cond ((stringp dired-directory)
408 (abbreviate-file-name (expand-file-name dired-directory)))
409 ((consp dired-directory)
410 (abbreviate-file-name (expand-file-name (car dired-directory))))
411 (t
412 (error "Unknown type of `dired-directory' in buffer %s"
413 (buffer-name)))))
414
415 (defun msb-dired-item-handler (buffer &optional maxbuf)
416 "Create one string item, concerning a dired BUFFER, for the buffer menu.
417 The item looks like:
418 *% <buffer-name>
419 The `*' appears only if the buffer is marked as modified.
420 The `%' appears only if the buffer is read-only.
421 Optional second argument MAXBUF is completely ignored."
422 (let ((name (msb--dired-directory))
423 (modified (if (buffer-modified-p) "*" " "))
424 (read-only (if buffer-read-only "%" " ")))
425 (format "%s%s %s" modified read-only name)))
426
427 (defun msb-alon-item-handler (buffer maxbuf)
428 "Create one string item for the buffer menu.
429 The item looks like:
430 <buffer-name> *%# <file-name>
431 The `*' appears only if the buffer is marked as modified.
432 The `%' appears only if the buffer is read-only.
433 The `#' appears only version control file (SCCS/RCS)."
434 (format (format "%%%ds %%s%%s%%s %%s" maxbuf)
435 (buffer-name buffer)
436 (if (buffer-modified-p) "*" " ")
437 (if buffer-read-only "%" " ")
438 (if (and (boundp 'vc-mode) vc-mode) "#" " ")
439 (or buffer-file-name "")))
440
441 ;;;
442 ;;; Some example function to be used for `msb-item-sort-function'.
443 ;;;
444 (defun msb-sort-by-name (item1 item2)
445 "Sort the items ITEM1 and ITEM2 by their `buffer-name'.
446 An item looks like (NAME . BUFFER)."
447 (string-lessp (buffer-name (cdr item1))
448 (buffer-name (cdr item2))))
449
450
451 (defun msb-sort-by-directory (item1 item2)
452 "Sort the items ITEM1 and ITEM2 by directory name. Made for dired.
453 An item look like (NAME . BUFFER)."
454 (string-lessp (with-current-buffer (cdr item1)
455 (msb--dired-directory))
456 (with-current-buffer (cdr item2)
457 (msb--dired-directory))))
458
459 ;;;
460 ;;; msb
461 ;;;
462 ;;; This function can be used instead of (mouse-buffer-menu EVENT)
463 ;;; function in "mouse.el".
464 ;;;
465 (defun msb (event)
466 "Pop up several menus of buffers for selection with the mouse.
467 This command switches buffers in the window that you clicked on, and
468 selects that window.
469
470 See the function `mouse-select-buffer' and the variable
471 `msb-menu-cond' for more information about how the menus are split."
472 (interactive "e")
473 (let ((old-window (selected-window))
474 (window (posn-window (event-start event)))
475 early-release)
476 (unless (framep window) (select-window window))
477 ;; This `sit-for' magically makes the menu stay up if the mouse
478 ;; button is released within 0.1 second.
479 (setq early-release (not (sit-for 0.1 t)))
480 (let ((buffer (mouse-select-buffer event)))
481 (if buffer
482 (switch-to-buffer buffer)
483 (select-window old-window)))
484 ;; If the above `sit-for' was interrupted by a mouse-up, avoid
485 ;; generating a drag event.
486 (if (and early-release (memq 'down (event-modifiers last-input-event)))
487 (discard-input)))
488 nil)
489
490 ;;;
491 ;;; Some supportive functions
492 ;;;
493 (defun msb-invisible-buffer-p (&optional buffer)
494 "Return t if optional BUFFER is an \"invisible\" buffer.
495 If the argument is left out or nil, then the current buffer is considered."
496 (and (> (length (buffer-name buffer)) 0)
497 (eq ?\s (aref (buffer-name buffer) 0))))
498
499 (defun msb--strip-dir (dir)
500 "Strip one hierarchy level from the end of DIR."
501 (file-name-directory (directory-file-name dir)))
502
503 ;; Create an alist with all buffers from LIST that lies under the same
504 ;; directory will be in the same item as the directory name.
505 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K BUFFER-K+1...)) ...)
506 (defun msb--init-file-alist (list)
507 (let ((buffer-alist
508 ;; Make alist that looks like
509 ;; ((DIR-1 BUFFER-1) (DIR-2 BUFFER-2) ...)
510 ;; sorted on DIR-x
511 (sort
512 (apply #'nconc
513 (mapcar
514 (lambda (buffer)
515 (let ((file-name (expand-file-name
516 (buffer-file-name buffer))))
517 (when file-name
518 (list (cons (msb--strip-dir file-name) buffer)))))
519 list))
520 (lambda (item1 item2)
521 (string< (car item1) (car item2))))))
522 ;; Now clump buffers together that have the same directory name
523 ;; Make alist that looks like
524 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K)) ...)
525 (let ((dir nil)
526 (buffers nil))
527 (nconc
528 (apply
529 #'nconc
530 (mapcar (lambda (item)
531 (cond
532 ((equal dir (car item))
533 ;; The same dir as earlier:
534 ;; Add to current list of buffers.
535 (push (cdr item) buffers)
536 ;; This item should not be added to list
537 nil)
538 (t
539 ;; New dir
540 (let ((result (and dir (cons dir buffers))))
541 (setq dir (car item))
542 (setq buffers (list (cdr item)))
543 ;; Add the last result the list.
544 (and result (list result))))))
545 buffer-alist))
546 ;; Add the last result to the list
547 (list (cons dir buffers))))))
548
549 (defun msb--format-title (top-found-p dir number-of-items)
550 "Format a suitable title for the menu item."
551 (format (if top-found-p "%s... (%d)" "%s (%d)")
552 (abbreviate-file-name dir) number-of-items))
553
554 ;; Variables for debugging.
555 (defvar msb--choose-file-menu-list)
556 (defvar msb--choose-file-menu-arg-list)
557
558 (defun msb--choose-file-menu (list)
559 "Choose file-menu with respect to directory for every buffer in LIST."
560 (setq msb--choose-file-menu-arg-list list)
561 (let ((buffer-alist (msb--init-file-alist list))
562 (final-list nil)
563 (max-clumped-together (if (numberp msb-max-file-menu-items)
564 msb-max-file-menu-items
565 10))
566 (top-found-p nil)
567 (last-dir nil)
568 first rest dir buffers old-dir)
569 ;; Prepare for looping over all items in buffer-alist
570 (setq first (car buffer-alist)
571 rest (cdr buffer-alist)
572 dir (car first)
573 buffers (cdr first))
574 (setq msb--choose-file-menu-list (copy-sequence rest))
575 ;; This big loop tries to clump buffers together that have a
576 ;; similar name. Remember that buffer-alist is sorted based on the
577 ;; directory name of the buffers' visited files.
578 (while rest
579 (let ((found-p nil)
580 (tmp-rest rest)
581 item)
582 (setq item (car tmp-rest))
583 ;; Clump together the "rest"-buffers that have a dir that is
584 ;; a subdir of the current one.
585 (while (and tmp-rest
586 (<= (length buffers) max-clumped-together)
587 (>= (length (car item)) (length dir))
588 ;; `completion-ignore-case' seems to default to t
589 ;; on the systems with case-insensitive file names.
590 (eq t (compare-strings dir 0 nil
591 (car item) 0 (length dir)
592 completion-ignore-case)))
593 (setq found-p t)
594 (setq buffers (append buffers (cdr item))) ;nconc is faster than append
595 (setq tmp-rest (cdr tmp-rest)
596 item (car tmp-rest)))
597 (cond
598 ((> (length buffers) max-clumped-together)
599 ;; Oh, we failed. Too many buffers clumped together.
600 ;; Just use the original ones for the result.
601 (setq last-dir (car first))
602 (push (cons (msb--format-title top-found-p
603 (car first)
604 (length (cdr first)))
605 (cdr first))
606 final-list)
607 (setq top-found-p nil)
608 (setq first (car rest)
609 rest (cdr rest)
610 dir (car first)
611 buffers (cdr first)))
612 (t
613 ;; The first pass of clumping together worked out, go ahead
614 ;; with this result.
615 (when found-p
616 (setq top-found-p t)
617 (setq first (cons dir buffers)
618 rest tmp-rest))
619 ;; Now see if we can clump more buffers together if we go up
620 ;; one step in the file hierarchy.
621 ;; If dir isn't changed by msb--strip-dir, we are looking
622 ;; at the machine name component of an ange-ftp filename.
623 (setq old-dir dir)
624 (setq dir (msb--strip-dir dir)
625 buffers (cdr first))
626 (if (equal old-dir dir)
627 (setq last-dir dir))
628 (when (and last-dir
629 (or (and (>= (length dir) (length last-dir))
630 (eq t (compare-strings
631 last-dir 0 nil dir 0
632 (length last-dir)
633 completion-ignore-case)))
634 (and (< (length dir) (length last-dir))
635 (eq t (compare-strings
636 dir 0 nil last-dir 0 (length dir)
637 completion-ignore-case)))))
638 ;; We have reached the same place in the file hierarchy as
639 ;; the last result, so we should quit at this point and
640 ;; take what we have as result.
641 (push (cons (msb--format-title top-found-p
642 (car first)
643 (length (cdr first)))
644 (cdr first))
645 final-list)
646 (setq top-found-p nil)
647 (setq first (car rest)
648 rest (cdr rest)
649 dir (car first)
650 buffers (cdr first)))))))
651 ;; Now take care of the last item.
652 (when first
653 (push (cons (msb--format-title top-found-p
654 (car first)
655 (length (cdr first)))
656 (cdr first))
657 final-list))
658 (setq top-found-p nil)
659 (nreverse final-list)))
660
661 (defun msb--create-function-info (menu-cond-elt)
662 "Create a vector from an element MENU-COND-ELT of `msb-menu-cond'.
663 This takes the form:
664 \[BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER]
665 See `msb-menu-cond' for a description of its elements."
666 (let* ((list-symbol (make-symbol "-msb-buffer-list"))
667 (tmp-ih (and (> (length menu-cond-elt) 3)
668 (nth 3 menu-cond-elt)))
669 (item-handler (if (and tmp-ih (fboundp tmp-ih))
670 tmp-ih
671 msb-item-handling-function))
672 (tmp-s (if (> (length menu-cond-elt) 4)
673 (nth 4 menu-cond-elt)
674 msb-item-sort-function))
675 (sorter (if (or (fboundp tmp-s)
676 (null tmp-s)
677 (eq tmp-s t))
678 tmp-s
679 msb-item-sort-function)))
680 (when (< (length menu-cond-elt) 3)
681 (error "Wrong format of msb-menu-cond"))
682 (when (and (> (length menu-cond-elt) 3)
683 (not (fboundp tmp-ih)))
684 (signal 'invalid-function (list tmp-ih)))
685 (when (and (> (length menu-cond-elt) 4)
686 tmp-s
687 (not (fboundp tmp-s))
688 (not (eq tmp-s t)))
689 (signal 'invalid-function (list tmp-s)))
690 (set list-symbol ())
691 (vector list-symbol ;BUFFER-LIST-VARIABLE
692 (nth 0 menu-cond-elt) ;CONDITION
693 (nth 1 menu-cond-elt) ;SORT-KEY
694 (nth 2 menu-cond-elt) ;MENU-TITLE
695 item-handler ;ITEM-HANDLER
696 sorter) ;SORTER
697 ))
698
699 ;; This defsubst is only used in `msb--choose-menu' below. It was
700 ;; pulled out merely to make the code somewhat clearer. The indentation
701 ;; level was too big.
702 (defsubst msb--collect (function-info-vector)
703 (let ((result nil)
704 (multi-flag nil)
705 function-info-list)
706 (setq function-info-list
707 (loop for fi
708 across function-info-vector
709 if (and (setq result
710 (eval (aref fi 1))) ;Test CONDITION
711 (not (and (eq result 'no-multi)
712 multi-flag))
713 (progn (when (eq result 'multi)
714 (setq multi-flag t))
715 t))
716 collect fi
717 until (and result
718 (not (eq result 'multi)))))
719 (when (and (not function-info-list)
720 (not result))
721 (error "No catch-all in msb-menu-cond!"))
722 function-info-list))
723
724 (defun msb--add-to-menu (buffer function-info max-buffer-name-length)
725 "Add BUFFER to the menu depicted by FUNCTION-INFO.
726 All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
727 to the buffer-list variable in FUNCTION-INFO."
728 (let ((list-symbol (aref function-info 0))) ;BUFFER-LIST-VARIABLE
729 ;; Here comes the hairy side-effect!
730 (set list-symbol
731 (cons (cons (funcall (aref function-info 4) ;ITEM-HANDLER
732 buffer
733 max-buffer-name-length)
734 buffer)
735 (eval list-symbol)))))
736
737 (defsubst msb--choose-menu (buffer function-info-vector max-buffer-name-length)
738 "Select the appropriate menu for BUFFER."
739 ;; This is all side-effects, folks!
740 ;; This should be optimized.
741 (unless (and (not msb-display-invisible-buffers-p)
742 (msb-invisible-buffer-p buffer))
743 (condition-case nil
744 (with-current-buffer buffer
745 ;; Menu found. Add to this menu
746 (dolist (info (msb--collect function-info-vector))
747 (msb--add-to-menu buffer info max-buffer-name-length)))
748 (error (unless msb--error
749 (setq msb--error
750 (format
751 "In msb-menu-cond, error for buffer `%s'."
752 (buffer-name buffer)))
753 (error "%s" msb--error))))))
754
755 (defun msb--create-sort-item (function-info)
756 "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty."
757 (let ((buffer-list (eval (aref function-info 0))))
758 (when buffer-list
759 (let ((sorter (aref function-info 5)) ;SORTER
760 (sort-key (aref function-info 2))) ;MENU-SORT-KEY
761 (when sort-key
762 (cons sort-key
763 (cons (format (aref function-info 3) ;MENU-TITLE
764 (length buffer-list))
765 (cond
766 ((null sorter)
767 buffer-list)
768 ((eq sorter t)
769 (nreverse buffer-list))
770 (t
771 (sort buffer-list sorter))))))))))
772
773 (defun msb--aggregate-alist (alist same-predicate sort-predicate)
774 "Return ALIST as a sorted, aggregated alist.
775
776 In the result all items with the same car element (according to
777 SAME-PREDICATE) are aggregated together. The alist is first sorted by
778 SORT-PREDICATE.
779
780 Example:
781 \(msb--aggregate-alist
782 '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
783 (function string=)
784 (lambda (item1 item2)
785 (string< (symbol-name item1) (symbol-name item2))))
786 results in
787 \((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
788 (when (not (null alist))
789 (let (same
790 tmp-old-car
791 tmp-same
792 (first-time-p t)
793 old-car)
794 (nconc
795 (apply #'nconc
796 (mapcar
797 (lambda (item)
798 (cond
799 (first-time-p
800 (push (cdr item) same)
801 (setq first-time-p nil)
802 (setq old-car (car item))
803 nil)
804 ((funcall same-predicate (car item) old-car)
805 (push (cdr item) same)
806 nil)
807 (t
808 (setq tmp-same same
809 tmp-old-car old-car)
810 (setq same (list (cdr item))
811 old-car (car item))
812 (list (cons tmp-old-car (nreverse tmp-same))))))
813 (sort alist (lambda (item1 item2)
814 (funcall sort-predicate
815 (car item1) (car item2))))))
816 (list (cons old-car (nreverse same)))))))
817
818
819 (defun msb--mode-menu-cond ()
820 (let ((key msb-modes-key))
821 (mapcar (lambda (item)
822 (incf key)
823 (list `( eq major-mode (quote ,(car item)))
824 key
825 (concat (cdr item) " (%d)")))
826 (sort
827 (let ((mode-list nil))
828 (dolist (buffer (cdr (buffer-list)))
829 (with-current-buffer buffer
830 (when (and (not (msb-invisible-buffer-p))
831 (not (assq major-mode mode-list)))
832 (push (cons major-mode
833 (format-mode-line mode-name nil nil buffer))
834 mode-list))))
835 mode-list)
836 (lambda (item1 item2)
837 (string< (cdr item1) (cdr item2)))))))
838
839 (defun msb--most-recently-used-menu (max-buffer-name-length)
840 "Return a list for the most recently used buffers.
841 It takes the form ((TITLE . BUFFER-LIST)...)."
842 (when (and (numberp msb-display-most-recently-used)
843 (> msb-display-most-recently-used 0))
844 (let* ((buffers (cdr (buffer-list)))
845 (most-recently-used
846 (loop with n = 0
847 for buffer in buffers
848 if (with-current-buffer buffer
849 (and (not (msb-invisible-buffer-p))
850 (not (eq major-mode 'dired-mode))))
851 collect (with-current-buffer buffer
852 (cons (funcall msb-item-handling-function
853 buffer
854 max-buffer-name-length)
855 buffer))
856 and do (incf n)
857 until (>= n msb-display-most-recently-used))))
858 (cons (if (stringp msb-most-recently-used-title)
859 (format msb-most-recently-used-title
860 (length most-recently-used))
861 (signal 'wrong-type-argument (list msb-most-recently-used-title)))
862 most-recently-used))))
863
864 (defun msb--create-buffer-menu-2 ()
865 (let ((max-buffer-name-length 0)
866 file-buffers
867 function-info-vector)
868 ;; Calculate the longest buffer name.
869 (dolist (buffer (buffer-list))
870 (when (or msb-display-invisible-buffers-p
871 (not (msb-invisible-buffer-p)))
872 (setq max-buffer-name-length
873 (max max-buffer-name-length (length (buffer-name buffer))))))
874 ;; Make a list with elements of type
875 ;; (BUFFER-LIST-VARIABLE
876 ;; CONDITION
877 ;; MENU-SORT-KEY
878 ;; MENU-TITLE
879 ;; ITEM-HANDLER
880 ;; SORTER)
881 ;; Uses "function-global" variables:
882 ;; function-info-vector
883 (setq function-info-vector
884 (apply (function vector)
885 (mapcar (function msb--create-function-info)
886 (append msb-menu-cond (msb--mode-menu-cond)))))
887 ;; Split the buffer-list into several lists; one list for each
888 ;; criteria. This is the most critical part with respect to time.
889 (dolist (buffer (buffer-list))
890 (cond ((and msb-files-by-directory
891 (buffer-file-name buffer)
892 ;; exclude ange-ftp buffers
893 ;;(not (string-match "\\/[^/:]+:"
894 ;; (buffer-file-name buffer)))
895 )
896 (push buffer file-buffers))
897 (t
898 (msb--choose-menu buffer
899 function-info-vector
900 max-buffer-name-length))))
901 (when file-buffers
902 (setq file-buffers
903 (mapcar (lambda (buffer-list)
904 (list* msb-files-by-directory-sort-key
905 (car buffer-list)
906 (sort
907 (mapcar (lambda (buffer)
908 (cons (with-current-buffer buffer
909 (funcall
910 msb-item-handling-function
911 buffer
912 max-buffer-name-length))
913 buffer))
914 (cdr buffer-list))
915 (lambda (item1 item2)
916 (string< (car item1) (car item2))))))
917 (msb--choose-file-menu file-buffers))))
918 ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
919 (let* (menu
920 (most-recently-used
921 (msb--most-recently-used-menu max-buffer-name-length))
922 (others (nconc file-buffers
923 (loop for elt
924 across function-info-vector
925 for value = (msb--create-sort-item elt)
926 if value collect value))))
927 (setq menu
928 (mapcar 'cdr ;Remove the SORT-KEY
929 ;; Sort the menus - not the items.
930 (msb--add-separators
931 (sort
932 ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
933 ;; Also sorts the items within the menus.
934 (if (cdr most-recently-used)
935 (cons
936 ;; Add most recent used buffers
937 (cons msb-most-recently-used-sort-key
938 most-recently-used)
939 others)
940 others)
941 (lambda (elt1 elt2)
942 (< (car elt1) (car elt2)))))))
943 ;; Now make it a keymap menu
944 (append
945 '(keymap "Select Buffer")
946 (msb--make-keymap-menu menu)
947 (when msb-separator-diff
948 (list (list 'separator "--")))
949 (list (cons 'toggle
950 (cons
951 (if msb-files-by-directory
952 "*Files by type*"
953 "*Files by directory*")
954 'msb--toggle-menu-type)))))))
955
956 (defun msb--create-buffer-menu ()
957 (save-match-data
958 (save-excursion
959 (msb--create-buffer-menu-2))))
960
961 (defun msb--toggle-menu-type ()
962 "Multi-purpose function for selecting a buffer with the mouse."
963 (interactive)
964 (setq msb-files-by-directory (not msb-files-by-directory))
965 ;; This gets a warning, but it is correct,
966 ;; because this file redefines menu-bar-update-buffers.
967 (msb-menu-bar-update-buffers t))
968
969 (defun mouse-select-buffer (event)
970 "Pop up several menus of buffers, for selection with the mouse.
971 Returns the selected buffer or nil if no buffer is selected.
972
973 The way the buffers are split is conveniently handled with the
974 variable `msb-menu-cond'."
975 ;; Popup the menu and return the selected buffer.
976 (when (or msb--error
977 (not msb--last-buffer-menu)
978 (not (fboundp 'frame-or-buffer-changed-p))
979 (frame-or-buffer-changed-p))
980 (setq msb--error nil)
981 (setq msb--last-buffer-menu (msb--create-buffer-menu)))
982 (let ((position event)
983 choice)
984 (when (and (fboundp 'posn-x-y)
985 (fboundp 'posn-window))
986 (let ((posX (car (posn-x-y (event-start event))))
987 (posY (cdr (posn-x-y (event-start event))))
988 (posWind (posn-window (event-start event))))
989 ;; adjust position
990 (setq posX (- posX (funcall msb-horizontal-shift-function))
991 position (list (list posX posY) posWind))))
992 ;; Popup the menu
993 (setq choice (x-popup-menu position msb--last-buffer-menu))
994 (cond
995 ((eq (car choice) 'toggle)
996 ;; Bring up the menu again with type toggled.
997 (msb--toggle-menu-type)
998 (mouse-select-buffer event))
999 ((and (numberp (car choice))
1000 (null (cdr choice)))
1001 (let ((msb--last-buffer-menu (nthcdr 2 (assq (car choice)
1002 msb--last-buffer-menu))))
1003 (mouse-select-buffer event)))
1004 ((while (numberp (car choice))
1005 (setq choice (cdr choice))))
1006 ((and (stringp (car choice))
1007 (null (cdr choice)))
1008 (car choice))
1009 ((null choice)
1010 choice)
1011 (t
1012 (error "Unknown form for buffer: %s" choice)))))
1013
1014 ;; Add separators
1015 (defun msb--add-separators (sorted-list)
1016 (if (or (not msb-separator-diff)
1017 (not (numberp msb-separator-diff)))
1018 sorted-list
1019 (let ((last-key nil))
1020 (apply #'nconc
1021 (mapcar
1022 (lambda (item)
1023 (cond
1024 ((and msb-separator-diff
1025 last-key
1026 (> (- (car item) last-key)
1027 msb-separator-diff))
1028 (setq last-key (car item))
1029 (list (cons last-key 'separator)
1030 item))
1031 (t
1032 (setq last-key (car item))
1033 (list item))))
1034 sorted-list)))))
1035
1036 (defun msb--split-menus-2 (list mcount result)
1037 (cond
1038 ((> (length list) msb-max-menu-items)
1039 (let ((count 0)
1040 sub-name
1041 (tmp-list nil))
1042 (while (< count msb-max-menu-items)
1043 (push (pop list) tmp-list)
1044 (incf count))
1045 (setq tmp-list (nreverse tmp-list))
1046 (setq sub-name (concat (car (car tmp-list)) "..."))
1047 (push (nconc (list mcount sub-name
1048 'keymap sub-name)
1049 tmp-list)
1050 result))
1051 (msb--split-menus-2 list (1+ mcount) result))
1052 ((null result)
1053 list)
1054 (t
1055 (let (sub-name)
1056 (setq sub-name (concat (car (car list)) "..."))
1057 (push (nconc (list mcount sub-name 'keymap sub-name)
1058 list)
1059 result))
1060 (nreverse result))))
1061
1062 (defun msb--split-menus (list)
1063 (if (and (integerp msb-max-menu-items)
1064 (> msb-max-menu-items 0))
1065 (msb--split-menus-2 list 0 nil)
1066 list))
1067
1068 (defun msb--make-keymap-menu (raw-menu)
1069 (let ((end (cons '(nil) 'menu-bar-select-buffer))
1070 (mcount 0))
1071 (mapcar
1072 (lambda (sub-menu)
1073 (cond
1074 ((eq 'separator sub-menu)
1075 (list 'separator "--"))
1076 (t
1077 (let ((buffers (mapcar (lambda (item)
1078 (cons (buffer-name (cdr item))
1079 (cons (car item) end)))
1080 (cdr sub-menu))))
1081 (nconc (list (incf mcount) (car sub-menu)
1082 'keymap (car sub-menu))
1083 (msb--split-menus buffers))))))
1084 raw-menu)))
1085
1086 (defun msb-menu-bar-update-buffers (&optional arg)
1087 "A re-written version of `menu-bar-update-buffers'."
1088 ;; If user discards the Buffers item, play along.
1089 (when (and (lookup-key (current-global-map) [menu-bar buffer])
1090 (or (not (fboundp 'frame-or-buffer-changed-p))
1091 (frame-or-buffer-changed-p)
1092 arg))
1093 (let ((frames (frame-list))
1094 buffers-menu frames-menu)
1095 ;; Make the menu of buffers proper.
1096 (setq msb--last-buffer-menu (msb--create-buffer-menu))
1097 ;; Skip the `keymap' symbol.
1098 (setq buffers-menu (cdr msb--last-buffer-menu))
1099 ;; Make a Frames menu if we have more than one frame.
1100 (when (cdr frames)
1101 (let* ((frame-length (length frames))
1102 (f-title (format "Frames (%d)" frame-length)))
1103 ;; List only the N most recently selected frames
1104 (when (and (integerp msb-max-menu-items)
1105 (> msb-max-menu-items 1)
1106 (> frame-length msb-max-menu-items))
1107 (setcdr (nthcdr msb-max-menu-items frames) nil))
1108 (setq frames-menu
1109 (nconc
1110 (list 'frame f-title '(nil) 'keymap f-title)
1111 (mapcar
1112 (lambda (frame)
1113 (nconc
1114 (list (frame-parameter frame 'name)
1115 (frame-parameter frame 'name)
1116 (cons nil nil))
1117 `(lambda ()
1118 (interactive) (menu-bar-select-frame ,frame))))
1119 frames)))))
1120 (setcdr global-buffers-menu-map
1121 (if (and buffers-menu frames-menu)
1122 ;; Combine Frame and Buffers menus with separator between
1123 (nconc (list "Buffers and Frames" frames-menu
1124 (and msb-separator-diff '(separator "--")))
1125 (cdr buffers-menu))
1126 buffers-menu)))))
1127
1128 ;; Snarf current bindings of `mouse-buffer-menu' (normally
1129 ;; C-down-mouse-1).
1130 (defvar msb-mode-map
1131 (let ((map (make-sparse-keymap "Msb")))
1132 (define-key map [remap mouse-buffer-menu] 'msb)
1133 map))
1134
1135 ;;;###autoload
1136 (define-minor-mode msb-mode
1137 "Toggle Msb mode.
1138 With arg, turn Msb mode on if and only if arg is positive.
1139 This mode overrides the binding(s) of `mouse-buffer-menu' to provide a
1140 different buffer menu using the function `msb'."
1141 :global t :group 'msb
1142 (if msb-mode
1143 (progn
1144 (add-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers)
1145 (remove-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
1146 (msb-menu-bar-update-buffers t))
1147 (remove-hook 'menu-bar-update-hook 'msb-menu-bar-update-buffers)
1148 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
1149 (menu-bar-update-buffers t)))
1150
1151 (defun msb-unload-function ()
1152 "Unload the Msb library."
1153 (msb-mode -1)
1154 ;; continue standard unloading
1155 nil)
1156
1157 (provide 'msb)
1158 (run-hooks 'msb-after-load-hook)
1159
1160 ;;; msb.el ends here