1 ;;; msb.el --- Customizable buffer-selection with multiple menus.
3 ;; Copyright (C) 1993, 94, 95, 97, 98, 99, 2000 Free Software Foundation, Inc.
5 ;; Author: Lars Lindberg <Lars.G.Lindberg@capgemini.se>
8 ;; Lindberg's last update version: 3.34
9 ;; Keywords: mouse buffer menu
11 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Purpose of this package:
31 ;; 1. Offer a function for letting the user choose buffer,
32 ;; not necessarily for switching to it.
33 ;; 2. Make a better mouse-buffer-menu. This is done as a global
34 ;; minor mode, msb-mode.
37 ;; Look at the variable `msb-menu-cond' for deciding what menus you
38 ;; want. It's not that hard to customize, despite my not-so-good
39 ;; doc-string. Feel free to send me a better doc-string.
40 ;; There are some constants for you to try here:
42 ;; msb--very-many-menus (default)
44 ;; Look at the variable `msb-item-handling-function' for customization
45 ;; of the appearance of every menu item. Try for instance setting
46 ;; it to `msb-alon-item-handler'.
48 ;; Look at the variable `msb-item-sort-function' for customization
49 ;; of sorting the menus. Set it to t for instance, which means no
50 ;; sorting - you will get latest used buffer first.
52 ;; Also check out the variable `msb-display-invisible-buffers-p'.
55 ;; - Files-by-directory
56 ;; + No possibility to show client/changed buffers separately.
57 ;; + All file buffers only appear in a file sub-menu, they will
58 ;; for instance not appear in the Mail sub-menu.
60 ;; Future enhancements:
63 ;; Mark Brader <msb@sq.com>
64 ;; Jim Berry <m1jhb00@FRB.GOV>
65 ;; Hans Chalupsky <hans@cs.Buffalo.EDU>
66 ;; Larry Rosenberg <ljr@ictv.com>
67 ;; Will Henney <will@astroscu.unam.mx>
68 ;; Jari Aalto <jaalto@tre.tele.nokia.fi>
69 ;; Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
70 ;; Gael Marziou <gael@gnlab030.grenoble.hp.com>
71 ;; Dave Gillespie <daveg@thymus.synaptics.com>
72 ;; Alon Albert <alon@milcse.rtsg.mot.com>
73 ;; Kevin Broadey, <KevinB@bartley.demon.co.uk>
74 ;; Ake Stenhof <ake@cadpoint.se>
75 ;; Richard Stallman <rms@gnu.org>
76 ;; Steve Fisk <fisk@medved.bowdoin.edu>
78 ;; This version turned into a global minor mode and subsequently
79 ;; hacked on by Dave Love.
82 (eval-when-compile (require 'cl
))
85 ;;; Some example constants to be used for `msb-menu-cond'. See that
86 ;;; variable for more information. Please note that if the condition
87 ;;; returns `multi', then the buffer can appear in several menus.
89 (defconst msb--few-menus
90 '(((and (boundp 'server-buffer-clients
)
95 ((and msb-display-invisible-buffers-p
96 (msb-invisible-buffer-p)
99 "Invisible buffers (%d)")
100 ((eq major-mode
'dired-mode
)
103 msb-dired-item-handler
104 msb-sort-by-directory
)
105 ((eq major-mode
'Man-mode
)
108 ((eq major-mode
'w3-mode
)
111 ((or (memq major-mode
112 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode
))
113 (memq major-mode
'(mh-letter-mode mh-show-mode mh-folder-mode
))
115 '(gnus-summary-mode message-mode gnus-group-mode
116 gnus-article-mode score-mode gnus-browse-killed-mode
)))
119 ((not buffer-file-name
)
126 (defconst msb--very-many-menus
127 '(((and (boundp 'server-buffer-clients
)
128 server-buffer-clients
132 ((and (boundp 'vc-mode
) vc-mode
'multi
)
134 "Version Control (%d)")
135 ((and buffer-file-name
139 "Changed files (%d)")
140 ((and (get-buffer-process (current-buffer))
144 ((and msb-display-invisible-buffers-p
145 (msb-invisible-buffer-p)
148 "Invisible buffers (%d)")
149 ((eq major-mode
'dired-mode
)
152 ;; Note this different menu-handler
153 msb-dired-item-handler
154 ;; Also note this item-sorter
155 msb-sort-by-directory
)
156 ((eq major-mode
'Man-mode
)
159 ((eq major-mode
'w3-mode
)
162 ((or (memq major-mode
163 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode
))
164 (memq major-mode
'(mh-letter-mode mh-show-mode mh-folder-mode
))
165 (memq major-mode
'(gnus-summary-mode message-mode gnus-group-mode
166 gnus-article-mode score-mode
167 gnus-browse-killed-mode
)))
170 ;; Catchup for all non-file buffers
171 ((and (not buffer-file-name
)
174 "Other non-file buffers (%d)")
175 ((and (string-match "/\\.[^/]*$" buffer-file-name
)
179 ((memq major-mode
'(c-mode c
++-mode
))
182 ((eq major-mode
'emacs-lisp-mode
)
185 ((eq major-mode
'latex-mode
)
190 "Other files (%d)")))
192 ;; msb--many-menus is obsolete
193 (defvar msb--many-menus msb--very-many-menus
)
196 ;;; Customizable variables
200 "Customizable buffer-selection with multiple menus."
204 (defun msb-custom-set (symbol value
)
205 "Set the value of custom variables for msb."
207 (if (and (featurep 'msb
) msb-mode
)
208 ;; wait until package has been loaded before bothering to update
210 (msb-menu-bar-update-buffers t
)))
212 (defcustom msb-menu-cond msb--very-many-menus
213 "*List of criteria for splitting the mouse buffer menu.
214 The elements in the list should be of this type:
215 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
217 When making the split, the buffers are tested one by one against the
218 CONDITION, just like a Lisp cond: When hitting a true condition, the
219 other criteria are *not* tested and the buffer name will appear in the
220 menu with the menu-title corresponding to the true condition.
222 If the condition returns the symbol `multi', then the buffer will be
223 added to this menu *and* tested for other menus too. If it returns
224 `no-multi', then the buffer will only be added if it hasn't been added
227 During this test, the buffer in question is the current buffer, and
228 the test is surrounded by calls to `save-excursion' and
231 The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
232 nil means don't display this menu.
234 MENU-TITLE is really a format. If you add %d in it, the %d is
235 replaced with the number of items in that menu.
237 ITEM-HANDLING-FN, is optional. If it is supplied and is a function,
238 than it is used for displaying the items in that particular buffer
239 menu, otherwise the function pointed out by
240 `msb-item-handling-function' is used.
242 ITEM-SORT-FN, is also optional.
243 If it is not supplied, the function pointed out by
244 `msb-item-sort-function' is used.
245 If it is nil, then no sort takes place and the buffers are presented
246 in least-recently-used order.
247 If it is t, then no sort takes place and the buffers are presented in
248 most-recently-used order.
249 If it is supplied and non-nil and not t than it is used for sorting
250 the items in that particular buffer menu.
252 Note1: There should always be a `catch-all' as last element, in this
253 list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
254 Note2: A buffer menu appears only if it has at least one buffer in it.
255 Note3: If you have a CONDITION that can't be evaluated you will get an
256 error every time you do \\[msb]."
257 :type
`(choice (const :tag
"long" :value
,msb--very-many-menus
)
258 (const :tag
"short" :value
,msb--few-menus
)
263 (defcustom msb-modes-key
4000
264 "The sort key for files sorted by mode."
270 (defcustom msb-separator-diff
100
271 "*Non-nil means use separators.
272 The separators will appear between all menus that have a sorting key
273 that differs by this value or more."
274 :type
'(choice integer
(const nil
))
278 (defvar msb-files-by-directory-sort-key
0
279 "*The sort key for files sorted by directory.")
281 (defcustom msb-max-menu-items
15
282 "*The maximum number of items in a menu.
283 If this variable is set to 15 for instance, then the submenu will be
284 split up in minor parts, 15 items each. Nil means no limit."
285 :type
'(choice integer
(const nil
))
289 (defcustom msb-max-file-menu-items
10
290 "*The maximum number of items from different directories.
292 When the menu is of type `file by directory', this is the maximum
293 number of buffers that are clumped together from different
296 Set this to 1 if you want one menu per directory instead of clumping
299 If the value is not a number, then the value 10 is used."
304 (defcustom msb-most-recently-used-sort-key -
1010
305 "*Where should the menu with the most recently used buffers be placed?"
310 (defcustom msb-display-most-recently-used
15
311 "*How many buffers should be in the most-recently-used menu.
312 No buffers at all if less than 1 or nil (or any non-number)."
317 (defcustom msb-most-recently-used-title
"Most recently used (%d)"
318 "*The title for the most-recently-used menu."
323 (defvar msb-horizontal-shift-function
'(lambda () 0)
324 "*Function that specifies how many pixels to shift the top menu leftwards.")
326 (defcustom msb-display-invisible-buffers-p nil
327 "*Show invisible buffers or not.
328 Non-nil means that the buffer menu should include buffers that have
329 names that starts with a space character."
334 (defvar msb-item-handling-function
'msb-item-handler
335 "*The appearance of a buffer menu.
337 The default function to call for handling the appearance of a menu
338 item. It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
339 where the latter is the max length of all buffer names.
341 The function should return the string to use in the menu.
343 When the function is called, BUFFER is the current buffer. This
344 function is called for items in the variable `msb-menu-cond' that have
345 nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
348 (defcustom msb-item-sort-function
'msb-sort-by-name
349 "*The order of items in a buffer menu.
351 The default function to call for handling the order of items in a menu
352 item. This function is called like a sort function. The items look
353 like (ITEM-NAME . BUFFER).
355 ITEM-NAME is the name of the item that will appear in the menu.
356 BUFFER is the buffer, this is not necessarily the current buffer.
358 Set this to nil or t if you don't want any sorting (faster)."
359 :type
'(choice (const msb-sort-by-name
)
360 (const :tag
"Newest first" t
)
361 (const :tag
"Oldest first" nil
))
365 (defcustom msb-files-by-directory nil
366 "*Non-nil means that files should be sorted by directory.
367 This is instead of the groups in `msb-menu-cond'."
372 (defcustom msb-after-load-hook nil
373 "Hook run after the msb package has been loaded."
379 ;;; Internal variables
382 ;; The last calculated menu.
383 (defvar msb--last-buffer-menu nil
)
385 ;; If this is non-nil, then it is a string that describes the error.
386 (defvar msb--error nil
)
389 ;;; Some example function to be used for `msb-item-handling-function'.
391 (defun msb-item-handler (buffer &optional maxbuf
)
392 "Create one string item, concerning BUFFER, for the buffer menu.
395 The `*' appears only if the buffer is marked as modified.
396 The `%' appears only if the buffer is read-only.
397 Optional second argument MAXBUF is completely ignored."
398 (let ((name (buffer-name))
399 (modified (if (buffer-modified-p) "*" " "))
400 (read-only (if buffer-read-only
"%" " ")))
401 (format "%s%s %s" modified read-only name
)))
404 (eval-when-compile (require 'dired
))
406 ;; `dired' can be called with a list of the form (directory file1 file2 ...)
407 ;; which causes `dired-directory' to be in the same form.
408 (defun msb--dired-directory ()
409 (cond ((stringp dired-directory
)
410 (abbreviate-file-name (expand-file-name dired-directory
)))
411 ((consp dired-directory
)
412 (abbreviate-file-name (expand-file-name (car dired-directory
))))
414 (error "Unknown type of `dired-directory' in buffer %s"
417 (defun msb-dired-item-handler (buffer &optional maxbuf
)
418 "Create one string item, concerning a dired BUFFER, for the buffer menu.
421 The `*' appears only if the buffer is marked as modified.
422 The `%' appears only if the buffer is read-only.
423 Optional second argument MAXBUF is completely ignored."
424 (let ((name (msb--dired-directory))
425 (modified (if (buffer-modified-p) "*" " "))
426 (read-only (if buffer-read-only
"%" " ")))
427 (format "%s%s %s" modified read-only name
)))
429 (defun msb-alon-item-handler (buffer maxbuf
)
430 "Create one string item for the buffer menu.
432 <buffer-name> *%# <file-name>
433 The `*' appears only if the buffer is marked as modified.
434 The `%' appears only if the buffer is read-only.
435 The `#' appears only version control file (SCCS/RCS)."
436 (format (format "%%%ds %%s%%s%%s %%s" maxbuf
)
438 (if (buffer-modified-p) "*" " ")
439 (if buffer-read-only
"%" " ")
440 (if (and (boundp 'vc-mode
) vc-mode
) "#" " ")
441 (or buffer-file-name
"")))
444 ;;; Some example function to be used for `msb-item-sort-function'.
446 (defun msb-sort-by-name (item1 item2
)
447 "Sort the items ITEM1 and ITEM2 by their `buffer-name'.
448 An item looks like (NAME . BUFFER)."
449 (string-lessp (buffer-name (cdr item1
))
450 (buffer-name (cdr item2
))))
453 (defun msb-sort-by-directory (item1 item2
)
454 "Sort the items ITEM1 and ITEM2 by directory name. Made for dired.
455 An item look like (NAME . BUFFER)."
456 (string-lessp (save-excursion (set-buffer (cdr item1
))
457 (msb--dired-directory))
458 (save-excursion (set-buffer (cdr item2
))
459 (msb--dired-directory))))
464 ;;; This function can be used instead of (mouse-buffer-menu EVENT)
465 ;;; function in "mouse.el".
468 "Pop up several menus of buffers for selection with the mouse.
469 This command switches buffers in the window that you clicked on, and
472 See the function `mouse-select-buffer' and the variable
473 `msb-menu-cond' for more information about how the menus are split."
475 (let ((old-window (selected-window))
476 (window (posn-window (event-start event
))))
477 (unless (framep window
) (select-window window
))
478 (let ((buffer (mouse-select-buffer event
)))
480 (switch-to-buffer buffer
)
481 (select-window old-window
))))
485 ;;; Some supportive functions
487 (defun msb-invisible-buffer-p (&optional buffer
)
488 "Return t if optional BUFFER is an \"invisible\" buffer.
489 If the argument is left out or nil, then the current buffer is considered."
490 (and (> (length (buffer-name buffer
)) 0)
491 (eq ?\
(aref (buffer-name buffer
) 0))))
493 (defun msb--strip-dir (dir)
494 "Strip one hierarchy level from the end of DIR."
495 (file-name-directory (directory-file-name dir
)))
497 ;; Create an alist with all buffers from LIST that lies under the same
498 ;; directory will be in the same item as the directory string.
499 ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...)
500 (defun msb--init-file-alist (list)
502 ;; Make alist that looks like
503 ;; ((PATH-1 BUFFER-1) (PATH-2 BUFFER-2) ...)
509 (let ((file-name (expand-file-name
510 (buffer-file-name buffer
))))
512 (list (cons (msb--strip-dir file-name
) buffer
)))))
514 (lambda (item1 item2
)
515 (string< (car item1
) (car item2
))))))
516 ;; Now clump buffers together that have the same path
517 ;; Make alist that looks like
518 ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...)
524 (mapcar (lambda (item)
526 ((equal path
(car item
))
527 ;; The same path as earlier: Add to current list of
529 (push (cdr item
) buffers
)
530 ;; This item should not be added to list
534 (let ((result (and path
(cons path buffers
))))
535 (setq path
(car item
))
536 (setq buffers
(list (cdr item
)))
537 ;; Add the last result the list.
538 (and result
(list result
))))))
540 ;; Add the last result to the list
541 (list (cons path buffers
))))))
543 (defun msb--format-title (top-found-p path number-of-items
)
544 "Format a suitable title for the menu item."
545 (format (if top-found-p
"%s... (%d)" "%s (%d)")
546 (abbreviate-file-name path
) number-of-items
))
548 ;; Variables for debugging.
549 (defvar msb--choose-file-menu-list
)
550 (defvar msb--choose-file-menu-arg-list
)
552 (defun msb--choose-file-menu (list)
553 "Choose file-menu with respect to directory for every buffer in LIST."
554 (setq msb--choose-file-menu-arg-list list
)
555 (let ((buffer-alist (msb--init-file-alist list
))
557 (max-clumped-together (if (numberp msb-max-file-menu-items
)
558 msb-max-file-menu-items
562 first rest path buffers old-path
)
563 ;; Prepare for looping over all items in buffer-alist
564 (setq first
(car buffer-alist
)
565 rest
(cdr buffer-alist
)
568 (setq msb--choose-file-menu-list
(copy-sequence rest
))
569 ;; This big loop tries to clump buffers together that have a
570 ;; similar name. Remember that buffer-alist is sorted based on the
571 ;; path for the buffers.
577 (setq item
(car tmp-rest
))
578 ;; Clump together the "rest"-buffers that have a path that is
579 ;; a subpath of the current one.
581 (<= (length buffers
) max-clumped-together
)
582 (>= (length (car item
)) (length path
))
583 ;; `completion-ignore-case' seems to default to t
584 ;; on the systems with case-insensitive file names.
585 (eq t
(compare-strings path
0 nil
586 (car item
) 0 (length path
)
587 completion-ignore-case
)))
589 (setq buffers
(append buffers
(cdr item
))) ;nconc is faster than append
590 (setq tmp-rest
(cdr tmp-rest
)
591 item
(car tmp-rest
)))
593 ((> (length buffers
) max-clumped-together
)
594 ;; Oh, we failed. Too many buffers clumped together.
595 ;; Just use the original ones for the result.
596 (setq last-path
(car first
))
597 (push (cons (msb--format-title top-found-p
599 (length (cdr first
)))
602 (setq top-found-p nil
)
603 (setq first
(car rest
)
606 buffers
(cdr first
)))
608 ;; The first pass of clumping together worked out, go ahead
612 (setq first
(cons path buffers
)
614 ;; Now see if we can clump more buffers together if we go up
615 ;; one step in the file hierarchy.
616 ;; If path isn't changed by msb--strip-dir, we are looking
617 ;; at the machine name component of an ange-ftp filename.
619 (setq path
(msb--strip-dir path
)
621 (if (equal old-path path
)
622 (setq last-path path
))
624 (or (and (>= (length path
) (length last-path
))
625 (eq t
(compare-strings
626 last-path
0 nil path
0
628 completion-ignore-case
)))
629 (and (< (length path
) (length last-path
))
630 (eq t
(compare-strings
631 path
0 nil last-path
0 (length path
)
632 completion-ignore-case
)))))
633 ;; We have reached the same place in the file hierarchy as
634 ;; the last result, so we should quit at this point and
635 ;; take what we have as result.
636 (push (cons (msb--format-title top-found-p
638 (length (cdr first
)))
641 (setq top-found-p nil
)
642 (setq first
(car rest
)
645 buffers
(cdr first
)))))))
646 ;; Now take care of the last item.
648 (push (cons (msb--format-title top-found-p
650 (length (cdr first
)))
653 (setq top-found-p nil
)
654 (nreverse final-list
)))
656 (defun msb--create-function-info (menu-cond-elt)
657 "Create a vector from an element MENU-COND-ELT of `msb-menu-cond'.
659 \]BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER)
660 See `msb-menu-cond' for a description of its elements."
661 (let* ((list-symbol (make-symbol "-msb-buffer-list"))
662 (tmp-ih (and (> (length menu-cond-elt
) 3)
663 (nth 3 menu-cond-elt
)))
664 (item-handler (if (and tmp-ih
(fboundp tmp-ih
))
666 msb-item-handling-function
))
667 (tmp-s (if (> (length menu-cond-elt
) 4)
668 (nth 4 menu-cond-elt
)
669 msb-item-sort-function
))
670 (sorter (if (or (fboundp tmp-s
)
674 msb-item-sort-function
)))
675 (when (< (length menu-cond-elt
) 3)
676 (error "Wrong format of msb-menu-cond"))
677 (when (and (> (length menu-cond-elt
) 3)
678 (not (fboundp tmp-ih
)))
679 (signal 'invalid-function
(list tmp-ih
)))
680 (when (and (> (length menu-cond-elt
) 4)
682 (not (fboundp tmp-s
))
684 (signal 'invalid-function
(list tmp-s
)))
686 (vector list-symbol
;BUFFER-LIST-VARIABLE
687 (nth 0 menu-cond-elt
) ;CONDITION
688 (nth 1 menu-cond-elt
) ;SORT-KEY
689 (nth 2 menu-cond-elt
) ;MENU-TITLE
690 item-handler
;ITEM-HANDLER
694 ;; This defsubst is only used in `msb--choose-menu' below. It was
695 ;; pulled out merely to make the code somewhat clearer. The indentation
696 ;; level was too big.
697 (defsubst msb--collect
(function-info-vector)
701 (setq function-info-list
703 across function-info-vector
705 (eval (aref fi
1))) ;Test CONDITION
706 (not (and (eq result
'no-multi
)
708 (progn (when (eq result
'multi
)
713 (not (eq result
'multi
)))))
714 (when (and (not function-info-list
)
716 (error "No catch-all in msb-menu-cond!"))
719 (defun msb--add-to-menu (buffer function-info max-buffer-name-length
)
720 "Add BUFFER to the menu depicted by FUNCTION-INFO.
721 All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
722 to the buffer-list variable in function-info."
723 (let ((list-symbol (aref function-info
0))) ;BUFFER-LIST-VARIABLE
724 ;; Here comes the hairy side-effect!
726 (cons (cons (funcall (aref function-info
4) ;ITEM-HANDLER
728 max-buffer-name-length
)
730 (eval list-symbol
)))))
732 (defsubst msb--choose-menu
(buffer function-info-vector max-buffer-name-length
)
733 "Select the appropriate menu for BUFFER."
734 ;; This is all side-effects, folks!
735 ;; This should be optimized.
736 (unless (and (not msb-display-invisible-buffers-p
)
737 (msb-invisible-buffer-p buffer
))
741 ;; Menu found. Add to this menu
742 (dolist (info (msb--collect function-info-vector
))
743 (msb--add-to-menu buffer info max-buffer-name-length
)))
744 (error (unless msb--error
747 "In msb-menu-cond, error for buffer `%s'."
748 (buffer-name buffer
)))
749 (error "%s" msb--error
))))))
751 (defun msb--create-sort-item (function-info)
752 "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty."
753 (let ((buffer-list (eval (aref function-info
0))))
755 (let ((sorter (aref function-info
5)) ;SORTER
756 (sort-key (aref function-info
2))) ;MENU-SORT-KEY
759 (cons (format (aref function-info
3) ;MENU-TITLE
760 (length buffer-list
))
765 (nreverse buffer-list
))
767 (sort buffer-list sorter
))))))))))
769 (defun msb--aggregate-alist (alist same-predicate sort-predicate
)
770 "Return ALIST as a sorted, aggregated alist.
772 In the result all items with the same car element (according to
773 SAME-PREDICATE) are aggregated together. The alist is first sorted by
777 \(msb--aggregate-alist
778 '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
780 (lambda (item1 item2)
781 (string< (symbol-name item1) (symbol-name item2))))
783 \((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
784 (when (not (null alist
))
797 (push (cdr item
) same
)
798 (setq first-time-p nil
)
799 (setq old-car
(car item
))
801 ((funcall same-predicate
(car item
) old-car
)
802 (push (cdr item
) same
)
807 (setq same
(list (cdr item
))
809 (list (cons tmp-old-car
(nreverse tmp-same
))))))
810 (sort alist
(lambda (item1 item2
)
811 (funcall sort-predicate
(car item1
) (car item2
))))))
812 (list (cons old-car
(nreverse same
)))))))
815 (defun msb--mode-menu-cond ()
816 (let ((key msb-modes-key
))
817 (mapcar (lambda (item)
819 (list `( eq major-mode
(quote ,(car item
)))
821 (concat (cdr item
) " (%d)")))
823 (let ((mode-list nil
))
824 (dolist (buffer (cdr (buffer-list)))
827 (when (and (not (msb-invisible-buffer-p))
828 (not (assq major-mode mode-list
)))
829 (push (cons major-mode mode-name
)
832 (lambda (item1 item2
)
833 (string< (cdr item1
) (cdr item2
)))))))
835 (defun msb--most-recently-used-menu (max-buffer-name-length)
836 "Return a list for the most recently used buffers.
837 It takes the form ((TITLE . BUFFER-LIST)...)."
838 (when (and (numberp msb-display-most-recently-used
)
839 (> msb-display-most-recently-used
0))
840 (let* ((buffers (cdr (buffer-list)))
843 for buffer in buffers
846 (and (not (msb-invisible-buffer-p))
847 (not (eq major-mode
'dired-mode
))))
848 collect
(save-excursion
850 (cons (funcall msb-item-handling-function
852 max-buffer-name-length
)
855 until
(>= n msb-display-most-recently-used
))))
856 (cons (if (stringp msb-most-recently-used-title
)
857 (format msb-most-recently-used-title
858 (length most-recently-used
))
859 (signal 'wrong-type-argument
(list msb-most-recently-used-title
)))
860 most-recently-used
))))
862 (defun msb--create-buffer-menu-2 ()
863 (let ((max-buffer-name-length 0)
865 function-info-vector
)
866 ;; Calculate the longest buffer name.
867 (dolist (buffer (buffer-list))
868 (when (or msb-display-invisible-buffers-p
869 (not (msb-invisible-buffer-p)))
870 (setq max-buffer-name-length
871 (max max-buffer-name-length
(length (buffer-name buffer
))))))
872 ;; Make a list with elements of type
873 ;; (BUFFER-LIST-VARIABLE
879 ;; Uses "function-global" variables:
880 ;; function-info-vector
881 (setq function-info-vector
882 (apply (function vector
)
883 (mapcar (function msb--create-function-info
)
884 (append msb-menu-cond
(msb--mode-menu-cond)))))
885 ;; Split the buffer-list into several lists; one list for each
886 ;; criteria. This is the most critical part with respect to time.
887 (dolist (buffer (buffer-list))
888 (cond ((and msb-files-by-directory
889 (buffer-file-name buffer
)
890 ;; exclude ange-ftp buffers
891 ;;(not (string-match "\\/[^/:]+:"
892 ;; (buffer-file-name buffer)))
894 (push buffer file-buffers
))
896 (msb--choose-menu buffer
898 max-buffer-name-length
))))
901 (mapcar (lambda (buffer-list)
902 (cons msb-files-by-directory-sort-key
903 (cons (car buffer-list
)
907 (cons (save-excursion
909 (funcall msb-item-handling-function
911 max-buffer-name-length
))
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)
921 (msb--most-recently-used-menu max-buffer-name-length
))
922 (others (nconc file-buffers
924 across function-info-vector
925 for value
= (msb--create-sort-item elt
)
926 if value collect value
))))
928 (mapcar 'cdr
;Remove the SORT-KEY
929 ;; Sort the menus - not the items.
932 ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
933 ;; Also sorts the items within the menus.
934 (if (cdr most-recently-used
)
936 ;; Add most recent used buffers
937 (cons msb-most-recently-used-sort-key
942 (< (car elt1
) (car elt2
)))))))
943 ;; Now make it a keymap menu
945 '(keymap "Select Buffer")
946 (msb--make-keymap-menu menu
)
947 (when msb-separator-diff
948 (list (list 'separator
"--")))
951 (if msb-files-by-directory
953 "*Files by directory*")
954 'msb--toggle-menu-type
)))))))
956 (defun msb--create-buffer-menu ()
959 (msb--create-buffer-menu-2))))
961 (defun msb--toggle-menu-type ()
962 "Multi purpose function for selecting a buffer with the mouse."
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
))
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.
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.
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
)
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
))))
990 (setq posX
(- posX
(funcall msb-horizontal-shift-function
))
991 position
(list (list posX posY
) posWind
))))
992 ;; This `sit-for' magically makes the menu stay up if the mouse
993 ;; button is released within 0.1 second.
996 (setq choice
(x-popup-menu position msb--last-buffer-menu
))
998 ((eq (car choice
) 'toggle
)
999 ;; Bring up the menu again with type toggled.
1000 (msb--toggle-menu-type)
1001 (mouse-select-buffer event
))
1002 ((and (numberp (car choice
))
1003 (null (cdr choice
)))
1004 (let ((msb--last-buffer-menu (nthcdr 3 (assq (car choice
)
1005 msb--last-buffer-menu
))))
1006 (mouse-select-buffer event
)))
1007 ((while (numberp (car choice
))
1008 (setq choice
(cdr choice
))))
1009 ((and (stringp (car choice
))
1010 (null (cdr choice
)))
1015 (error "Unknown form for buffer: %s" choice
)))))
1018 (defun msb--add-separators (sorted-list)
1019 (if (or (not msb-separator-diff
)
1020 (not (numberp msb-separator-diff
)))
1022 (let ((last-key nil
))
1027 ((and msb-separator-diff
1029 (> (- (car item
) last-key
)
1030 msb-separator-diff
))
1031 (setq last-key
(car item
))
1032 (list (cons last-key
'separator
)
1035 (setq last-key
(car item
))
1039 (defun msb--split-menus-2 (list mcount result
)
1041 ((> (length list
) msb-max-menu-items
)
1045 (while (< count msb-max-menu-items
)
1046 (push (pop list
) tmp-list
)
1048 (setq tmp-list
(nreverse tmp-list
))
1049 (setq sub-name
(concat (car (car tmp-list
)) "..."))
1050 (push (nconc (list mcount sub-name
1054 (msb--split-menus-2 list
(1+ mcount
) result
))
1059 (setq sub-name
(concat (car (car list
)) "..."))
1060 (push (nconc (list mcount sub-name
'keymap sub-name
)
1063 (nreverse result
))))
1065 (defun msb--split-menus (list)
1066 (if (and (integerp msb-max-menu-items
)
1067 (> msb-max-menu-items
0))
1068 (msb--split-menus-2 list
0 nil
)
1071 (defun msb--make-keymap-menu (raw-menu)
1072 (let ((end (cons '(nil) 'menu-bar-select-buffer
))
1077 ((eq 'separator sub-menu
)
1078 (list 'separator
"--"))
1080 (let ((buffers (mapcar (lambda (item)
1081 (cons (buffer-name (cdr item
))
1082 (cons (car item
) end
)))
1084 (nconc (list (incf mcount
) (car sub-menu
)
1085 'keymap
(car sub-menu
))
1086 (msb--split-menus buffers
))))))
1089 (defun msb-menu-bar-update-buffers (&optional arg
)
1090 "A re-written version of `menu-bar-update-buffers'."
1091 ;; If user discards the Buffers item, play along.
1092 (when (and (lookup-key (current-global-map) [menu-bar buffer
])
1093 (or (not (fboundp 'frame-or-buffer-changed-p
))
1094 (frame-or-buffer-changed-p)
1096 (let ((frames (frame-list))
1097 buffers-menu frames-menu
)
1098 ;; Make the menu of buffers proper.
1099 (setq msb--last-buffer-menu
(msb--create-buffer-menu))
1100 (setq buffers-menu msb--last-buffer-menu
)
1101 ;; Make a Frames menu if we have more than one frame.
1103 (let* ((frame-length (length frames
))
1104 (f-title (format "Frames (%d)" frame-length
)))
1105 ;; List only the N most recently selected frames
1106 (when (and (integerp msb-max-menu-items
)
1107 (> msb-max-menu-items
1)
1108 (> frame-length msb-max-menu-items
))
1109 (setcdr (nthcdr msb-max-menu-items frames
) nil
))
1112 (list 'frame f-title
'(nil) 'keymap f-title
)
1118 (frame-parameters frame
)))
1120 'menu-bar-select-frame
))
1122 (define-key (current-global-map) [menu-bar buffer
]
1124 (if (and buffers-menu frames-menu
)
1125 ;; Combine Frame and Buffers menus with separator between
1126 (nconc (list 'keymap
"Buffers and Frames" frames-menu
1127 (and msb-separator-diff
'(separator "--")))
1128 (cddr buffers-menu
))
1129 (or buffers-menu
'undefined
)))))))
1131 ;; Snarf current bindings of `mouse-buffer-menu' (normally
1133 (defvar msb-mode-map
1134 (let ((map (make-sparse-keymap "Msb")))
1135 (substitute-key-definition 'mouse-buffer-menu
'msb map global-map
)
1139 (define-minor-mode msb-mode
1141 With arg, turn Msb mode on if and only if arg is positive.
1142 This mode overrides the binding(s) of `mouse-buffer-menu' to provide a
1143 different buffer menu using the function `msb'."
1147 (add-hook 'menu-bar-update-hook
'msb-menu-bar-update-buffers
)
1148 (remove-hook 'menu-bar-update-hook
'menu-bar-update-buffers
))
1149 (remove-hook 'menu-bar-update-hook
'msb-menu-bar-update-buffers
)
1150 (add-hook 'menu-bar-update-hook
'menu-bar-update-buffers
))
1151 (run-hooks 'menu-bar-update-hook
))
1153 (defun msb-unload-hook ()
1157 (eval-after-load 'msb
(run-hooks 'msb-after-load-hook
'msb-after-load-hooks
))
1159 ;;; msb.el ends here