1 ;;; msb.el --- Customizable buffer-selection with multiple menus.
3 ;; Copyright (C) 1993, 94, 95, 97, 98, 99 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
'(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode
))
112 (memq major-mode
'(mh-letter-mode
115 (memq major-mode
'(gnus-summary-mode
120 gnus-browse-killed-mode
)))
123 ((not buffer-file-name
)
130 (defconst msb--very-many-menus
131 '(((and (boundp 'server-buffer-clients
)
132 server-buffer-clients
136 ((and (boundp 'vc-mode
) vc-mode
'multi
)
138 "Version Control (%d)")
139 ((and buffer-file-name
143 "Changed files (%d)")
144 ((and (get-buffer-process (current-buffer))
148 ((and msb-display-invisible-buffers-p
149 (msb-invisible-buffer-p)
152 "Invisible buffers (%d)")
153 ((eq major-mode
'dired-mode
)
156 ;; Note this different menu-handler
157 msb-dired-item-handler
158 ;; Also note this item-sorter
159 msb-sort-by-directory
)
160 ((eq major-mode
'Man-mode
)
163 ((eq major-mode
'w3-mode
)
166 ((or (memq major-mode
'(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode
))
167 (memq major-mode
'(mh-letter-mode
170 (memq major-mode
'(gnus-summary-mode
175 gnus-browse-killed-mode
)))
178 ;; Catchup for all non-file buffers
179 ((and (not buffer-file-name
)
182 "Other non-file buffers (%d)")
183 ((and (string-match "/\\.[^/]*$" buffer-file-name
)
187 ((memq major-mode
'(c-mode c
++-mode
))
190 ((eq major-mode
'emacs-lisp-mode
)
193 ((eq major-mode
'latex-mode
)
198 "Other files (%d)")))
200 ;; msb--many-menus is obsolete
201 (defvar msb--many-menus msb--very-many-menus
)
204 ;;; Customizable variables
208 "Customizable buffer-selection with multiple menus."
213 (defcustom msb-mode nil
215 Setting this variable directly does not take effect;
216 use either \\[customize] or the function `msb-mode'."
217 :set
(lambda (symbol value
)
218 (msb-mode (or value
0)))
219 :initialize
'custom-initialize-default
225 (defun msb-custom-set (symbol value
)
226 "Set the value of custom variables for msb."
228 (if (and (featurep 'msb
) msb-mode
)
229 ;; wait until package has been loaded before bothering to update
231 (msb-menu-bar-update-buffers t
)))
233 (defcustom msb-menu-cond msb--very-many-menus
234 "*List of criteria for splitting the mouse buffer menu.
235 The elements in the list should be of this type:
236 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
238 When making the split, the buffers are tested one by one against the
239 CONDITION, just like a Lisp cond: When hitting a true condition, the
240 other criteria are *not* tested and the buffer name will appear in the
241 menu with the menu-title corresponding to the true condition.
243 If the condition returns the symbol `multi', then the buffer will be
244 added to this menu *and* tested for other menus too. If it returns
245 `no-multi', then the buffer will only be added if it hasn't been added
248 During this test, the buffer in question is the current buffer, and
249 the test is surrounded by calls to `save-excursion' and
252 The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
253 nil means don't display this menu.
255 MENU-TITLE is really a format. If you add %d in it, the %d is
256 replaced with the number of items in that menu.
258 ITEM-HANDLING-FN, is optional. If it is supplied and is a function,
259 than it is used for displaying the items in that particular buffer
260 menu, otherwise the function pointed out by
261 `msb-item-handling-function' is used.
263 ITEM-SORT-FN, is also optional.
264 If it is not supplied, the function pointed out by
265 `msb-item-sort-function' is used.
266 If it is nil, then no sort takes place and the buffers are presented
267 in least-recently-used order.
268 If it is t, then no sort takes place and the buffers are presented in
269 most-recently-used order.
270 If it is supplied and non-nil and not t than it is used for sorting
271 the items in that particular buffer menu.
273 Note1: There should always be a `catch-all' as last element, in this
274 list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
275 Note2: A buffer menu appears only if it has at least one buffer in it.
276 Note3: If you have a CONDITION that can't be evaluated you will get an
277 error every time you do \\[msb]."
278 :type
`(choice (const :tag
"long" :value
,msb--very-many-menus
)
279 (const :tag
"short" :value
,msb--few-menus
)
284 (defcustom msb-modes-key
4000
285 "The sort key for files sorted by mode."
291 (defcustom msb-separator-diff
100
292 "*Non-nil means use separators.
293 The separators will appear between all menus that have a sorting key
294 that differs by this value or more."
295 :type
'(choice integer
(const nil
))
299 (defvar msb-files-by-directory-sort-key
0
300 "*The sort key for files sorted by directory.")
302 (defcustom msb-max-menu-items
15
303 "*The maximum number of items in a menu.
304 If this variable is set to 15 for instance, then the submenu will be
305 split up in minor parts, 15 items each. Nil means no limit."
306 :type
'(choice integer
(const nil
))
310 (defcustom msb-max-file-menu-items
10
311 "*The maximum number of items from different directories.
313 When the menu is of type `file by directory', this is the maximum
314 number of buffers that are clumped together from different
317 Set this to 1 if you want one menu per directory instead of clumping
320 If the value is not a number, then the value 10 is used."
325 (defcustom msb-most-recently-used-sort-key -
1010
326 "*Where should the menu with the most recently used buffers be placed?"
331 (defcustom msb-display-most-recently-used
15
332 "*How many buffers should be in the most-recently-used menu.
333 No buffers at all if less than 1 or nil (or any non-number)."
338 (defcustom msb-most-recently-used-title
"Most recently used (%d)"
339 "*The title for the most-recently-used menu."
344 (defvar msb-horizontal-shift-function
'(lambda () 0)
345 "*Function that specifies how many pixels to shift the top menu leftwards.")
347 (defcustom msb-display-invisible-buffers-p nil
348 "*Show invisible buffers or not.
349 Non-nil means that the buffer menu should include buffers that have
350 names that starts with a space character."
355 (defvar msb-item-handling-function
'msb-item-handler
356 "*The appearance of a buffer menu.
358 The default function to call for handling the appearance of a menu
359 item. It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
360 where the latter is the max length of all buffer names.
362 The function should return the string to use in the menu.
364 When the function is called, BUFFER is the current buffer. This
365 function is called for items in the variable `msb-menu-cond' that have
366 nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
369 (defcustom msb-item-sort-function
'msb-sort-by-name
370 "*The order of items in a buffer menu.
372 The default function to call for handling the order of items in a menu
373 item. This function is called like a sort function. The items look
374 like (ITEM-NAME . BUFFER).
376 ITEM-NAME is the name of the item that will appear in the menu.
377 BUFFER is the buffer, this is not necessarily the current buffer.
379 Set this to nil or t if you don't want any sorting (faster)."
380 :type
'(choice (const msb-sort-by-name
)
381 (const :tag
"Newest first" t
)
382 (const :tag
"Oldest first" nil
))
387 (defcustom msb-files-by-directory nil
388 "*Non-nil means that files should be sorted by directory.
389 This is instead of the groups in `msb-menu-cond'."
394 (defcustom msb-after-load-hooks nil
395 "Hooks to be run after the msb package has been loaded."
401 ;;; Internal variables
404 ;; The last calculated menu.
405 (defvar msb--last-buffer-menu nil
)
407 ;; If this is non-nil, then it is a string that describes the error.
408 (defvar msb--error nil
)
411 ;;; Some example function to be used for `msb-item-handling-function'.
413 (defun msb-item-handler (buffer &optional maxbuf
)
414 "Create one string item, concerning BUFFER, for the buffer menu.
417 The `*' appears only if the buffer is marked as modified.
418 The `%' appears only if the buffer is read-only.
419 Optional second argument MAXBUF is completely ignored."
420 (let ((name (buffer-name))
421 (modified (if (buffer-modified-p) "*" " "))
422 (read-only (if buffer-read-only
"%" " ")))
423 (format "%s%s %s" modified read-only name
)))
426 (eval-when-compile (require 'dired
))
428 ;; `dired' can be called with a list of the form (directory file1 file2 ...)
429 ;; which causes `dired-directory' to be in the same form.
430 (defun msb--dired-directory ()
431 (cond ((stringp dired-directory
)
432 (abbreviate-file-name (expand-file-name dired-directory
)))
433 ((consp dired-directory
)
434 (abbreviate-file-name (expand-file-name (car dired-directory
))))
436 (error "Unknown type of `dired-directory' in buffer %s"
439 (defun msb-dired-item-handler (buffer &optional maxbuf
)
440 "Create one string item, concerning a dired BUFFER, for the buffer menu.
443 The `*' appears only if the buffer is marked as modified.
444 The `%' appears only if the buffer is read-only.
445 Optional second argument MAXBUF is completely ignored."
446 (let ((name (msb--dired-directory))
447 (modified (if (buffer-modified-p) "*" " "))
448 (read-only (if buffer-read-only
"%" " ")))
449 (format "%s%s %s" modified read-only name
)))
451 (defun msb-alon-item-handler (buffer maxbuf
)
452 "Create one string item for the buffer menu.
454 <buffer-name> *%# <file-name>
455 The `*' appears only if the buffer is marked as modified.
456 The `%' appears only if the buffer is read-only.
457 The `#' appears only version control file (SCCS/RCS)."
458 (format (format "%%%ds %%s%%s%%s %%s" maxbuf
)
460 (if (buffer-modified-p) "*" " ")
461 (if buffer-read-only
"%" " ")
462 (if (and (boundp 'vc-mode
) vc-mode
) "#" " ")
463 (or buffer-file-name
"")))
466 ;;; Some example function to be used for `msb-item-sort-function'.
468 (defun msb-sort-by-name (item1 item2
)
469 "Sort the items ITEM1 and ITEM2 by their `buffer-name'.
470 An item looks like (NAME . BUFFER)."
471 (string-lessp (buffer-name (cdr item1
))
472 (buffer-name (cdr item2
))))
475 (defun msb-sort-by-directory (item1 item2
)
476 "Sort the items ITEM1 and ITEM2 by directory name. Made for dired.
477 An item look like (NAME . BUFFER)."
478 (string-lessp (save-excursion (set-buffer (cdr item1
))
479 (msb--dired-directory))
480 (save-excursion (set-buffer (cdr item2
))
481 (msb--dired-directory))))
486 ;;; This function can be used instead of (mouse-buffer-menu EVENT)
487 ;;; function in "mouse.el".
490 "Pop up several menus of buffers for selection with the mouse.
491 This command switches buffers in the window that you clicked on, and
494 See the function `mouse-select-buffer' and the variable
495 `msb-menu-cond' for more information about how the menus are split."
497 (let ((old-window (selected-window))
498 (window (posn-window (event-start event
))))
499 (unless (framep window
) (select-window window
))
500 (let ((buffer (mouse-select-buffer event
)))
502 (switch-to-buffer buffer
)
503 (select-window old-window
))))
507 ;;; Some supportive functions
509 (defun msb-invisible-buffer-p (&optional buffer
)
510 "Return t if optional BUFFER is an \"invisible\" buffer.
511 If the argument is left out or nil, then the current buffer is considered."
512 (and (> (length (buffer-name buffer
)) 0)
513 (eq ?\
(aref (buffer-name buffer
) 0))))
515 (defun msb--strip-dir (dir)
516 "Strip one hierarchy level from the end of DIR."
517 (file-name-directory (directory-file-name dir
)))
519 ;; Create an alist with all buffers from LIST that lies under the same
520 ;; directory will be in the same item as the directory string.
521 ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...)
522 (defun msb--init-file-alist (list)
524 ;; Make alist that looks like
525 ;; ((PATH-1 BUFFER-1) (PATH-2 BUFFER-2) ...)
529 (let ((file-name (expand-file-name (buffer-file-name buffer
))))
531 (list (cons (msb--strip-dir file-name
) buffer
)))))
533 (lambda (item1 item2
)
534 (string< (car item1
) (car item2
))))))
535 ;; Now clump buffers together that have the same path
536 ;; Make alist that looks like
537 ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...)
541 (mapcar (lambda (item)
544 (string= path
(car item
)))
545 ;; The same path as earlier: Add to current list of
547 (push (cdr item
) buffers
)
548 ;; This item should not be added to list
552 (let ((result (and path
(cons path buffers
))))
553 (setq path
(car item
))
554 (setq buffers
(list (cdr item
)))
555 ;; Add the last result the list.
556 (and result
(list result
))))))
558 ;; Add the last result to the list
559 (list (cons path buffers
))))))
561 (defun msb--format-title (top-found-p path number-of-items
)
562 "Format a suitable title for the menu item."
563 (format (if top-found-p
"%s... (%d)" "%s (%d)")
564 (abbreviate-file-name path
) number-of-items
))
566 ;; Variables for debugging.
567 (defvar msb--choose-file-menu-list
)
568 (defvar msb--choose-file-menu-arg-list
)
570 (defun msb--choose-file-menu (list)
571 "Choose file-menu with respect to directory for every buffer in LIST."
572 (setq msb--choose-file-menu-arg-list list
)
573 (let ((buffer-alist (msb--init-file-alist list
))
575 (max-clumped-together (if (numberp msb-max-file-menu-items
)
576 msb-max-file-menu-items
580 first rest path buffers old-path
)
581 ;; Prepare for looping over all items in buffer-alist
582 (setq first
(car buffer-alist
)
583 rest
(cdr buffer-alist
)
586 (setq msb--choose-file-menu-list
(apply #'list rest
))
587 ;; This big loop tries to clump buffers together that have a
588 ;; similar name. Remember that buffer-alist is sorted based on the
589 ;; path for the buffers.
595 (setq item
(car tmp-rest
))
596 ;; Clump together the "rest"-buffers that have a path that is
597 ;; a subpath of the current one.
599 (<= (length buffers
) max-clumped-together
)
600 (>= (length (car item
)) (length path
))
601 ;; `completion-ignore-case' seems to default to t
602 ;; on the systems with case-insensitive file names.
603 (eq t
(compare-strings path
0 nil
604 (car item
) 0 (length path
)
605 completion-ignore-case
)))
607 (setq buffers
(append buffers
(cdr item
))) ;nconc is faster than append
608 (setq tmp-rest
(cdr tmp-rest
)
609 item
(car tmp-rest
)))
611 ((> (length buffers
) max-clumped-together
)
612 ;; Oh, we failed. Too many buffers clumped together.
613 ;; Just use the original ones for the result.
614 (setq last-path
(car first
))
615 (push (cons (msb--format-title top-found-p
617 (length (cdr first
)))
620 (setq top-found-p nil
)
621 (setq first
(car rest
)
624 buffers
(cdr first
)))
626 ;; The first pass of clumping together worked out, go ahead
630 (setq first
(cons path buffers
)
632 ;; Now see if we can clump more buffers together if we go up
633 ;; one step in the file hierarchy.
634 ;; If path isn't changed by msb--strip-dir, we are looking
635 ;; at the machine name component of an ange-ftp filename.
637 (setq path
(msb--strip-dir path
)
639 (if (equal old-path path
)
640 (setq last-path path
))
642 (or (and (>= (length path
) (length last-path
))
643 (eq t
(compare-strings
644 last-path
0 nil path
0
646 completion-ignore-case
)))
647 (and (< (length path
) (length last-path
))
648 (eq t
(compare-strings
649 path
0 nil last-path
0 (length path
)
650 completion-ignore-case
)))))
651 ;; We have reached the same place in the file hierarchy as
652 ;; the last result, so we should quit at this point and
653 ;; take what we have as result.
654 (push (cons (msb--format-title top-found-p
656 (length (cdr first
)))
659 (setq top-found-p nil
)
660 (setq first
(car rest
)
663 buffers
(cdr first
)))))))
664 ;; Now take care of the last item.
666 (push (cons (msb--format-title top-found-p
668 (length (cdr first
)))
671 (setq top-found-p nil
)
672 (nreverse final-list
)))
674 (defun msb--create-function-info (menu-cond-elt)
675 "Create a vector from an element MENU-COND-ELT of `msb-menu-cond'.
677 \]BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER)
678 See `msb-menu-cond' for a description of its elements."
679 (let* ((list-symbol (make-symbol "-msb-buffer-list"))
680 (tmp-ih (and (> (length menu-cond-elt
) 3)
681 (nth 3 menu-cond-elt
)))
682 (item-handler (if (and tmp-ih
(fboundp tmp-ih
))
684 msb-item-handling-function
))
685 (tmp-s (if (> (length menu-cond-elt
) 4)
686 (nth 4 menu-cond-elt
)
687 msb-item-sort-function
))
688 (sorter (if (or (fboundp tmp-s
)
692 msb-item-sort-function
)))
693 (when (< (length menu-cond-elt
) 3)
694 (error "Wrong format of msb-menu-cond"))
695 (when (and (> (length menu-cond-elt
) 3)
696 (not (fboundp tmp-ih
)))
697 (signal 'invalid-function
(list tmp-ih
)))
698 (when (and (> (length menu-cond-elt
) 4)
700 (not (fboundp tmp-s
))
702 (signal 'invalid-function
(list tmp-s
)))
704 (vector list-symbol
;BUFFER-LIST-VARIABLE
705 (nth 0 menu-cond-elt
) ;CONDITION
706 (nth 1 menu-cond-elt
) ;SORT-KEY
707 (nth 2 menu-cond-elt
) ;MENU-TITLE
708 item-handler
;ITEM-HANDLER
712 ;; This defsubst is only used in `msb--choose-menu' below. It was
713 ;; pulled out merely to make the code somewhat clearer. The indentation
714 ;; level was too big.
715 (defsubst msb--collect
(function-info-vector)
719 (setq function-info-list
721 across function-info-vector
723 (eval (aref fi
1))) ;Test CONDITION
724 (not (and (eq result
'no-multi
)
726 (progn (when (eq result
'multi
)
731 (not (eq result
'multi
)))))
732 (when (and (not function-info-list
)
734 (error "No catch-all in msb-menu-cond!"))
737 (defun msb--add-to-menu (buffer function-info max-buffer-name-length
)
738 "Add BUFFER to the menu depicted by FUNCTION-INFO.
739 All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
740 to the buffer-list variable in function-info."
741 (let ((list-symbol (aref function-info
0))) ;BUFFER-LIST-VARIABLE
742 ;; Here comes the hairy side-effect!
744 (cons (cons (funcall (aref function-info
4) ;ITEM-HANDLER
746 max-buffer-name-length
)
748 (eval list-symbol
)))))
750 (defsubst msb--choose-menu
(buffer function-info-vector max-buffer-name-length
)
751 "Select the appropriate menu for BUFFER."
752 ;; This is all side-effects, folks!
753 ;; This should be optimized.
754 (unless (and (not msb-display-invisible-buffers-p
)
755 (msb-invisible-buffer-p buffer
))
759 ;; Menu found. Add to this menu
760 (dolist (info (msb--collect function-info-vector
))
761 (msb--add-to-menu buffer info max-buffer-name-length
)))
762 (error (unless msb--error
765 "In msb-menu-cond, error for buffer `%s'."
766 (buffer-name buffer
)))
767 (error "%s" msb--error
))))))
769 (defun msb--create-sort-item (function-info)
770 "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty."
771 (let ((buffer-list (eval (aref function-info
0))))
773 (let ((sorter (aref function-info
5)) ;SORTER
774 (sort-key (aref function-info
2))) ;MENU-SORT-KEY
777 (cons (format (aref function-info
3) ;MENU-TITLE
778 (length buffer-list
))
783 (nreverse buffer-list
))
785 (sort buffer-list sorter
))))))))))
787 (defun msb--aggregate-alist (alist same-predicate sort-predicate
)
788 "Return ALIST as a sorted, aggregated alist.
790 In the result all items with the same car element (according to
791 SAME-PREDICATE) are aggregated together. The alist is first sorted by
795 \(msb--aggregate-alist
796 '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
798 (lambda (item1 item2)
799 (string< (symbol-name item1) (symbol-name item2))))
801 \((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
802 (when (not (null alist
))
810 (mapcar (lambda (item)
813 (push (cdr item
) same
)
814 (setq first-time-p nil
)
815 (setq old-car
(car item
))
817 ((funcall same-predicate
(car item
) old-car
)
818 (push (cdr item
) same
)
823 (setq same
(list (cdr item
))
825 (list (cons tmp-old-car
(nreverse tmp-same
))))))
826 (sort alist
(lambda (item1 item2
)
827 (funcall sort-predicate
(car item1
) (car item2
)))))
828 (list (cons old-car
(nreverse same
)))))))
831 (defun msb--mode-menu-cond ()
832 (let ((key msb-modes-key
))
833 (mapcar (lambda (item)
835 (list `( eq major-mode
(quote ,(car item
)))
837 (concat (cdr item
) " (%d)")))
839 (let ((mode-list nil
))
840 (dolist (buffer (cdr (buffer-list)))
843 (when (and (not (msb-invisible-buffer-p))
844 (not (assq major-mode mode-list
)))
845 (push (cons major-mode mode-name
)
848 (lambda (item1 item2
)
849 (string< (cdr item1
) (cdr item2
)))))))
851 (defun msb--most-recently-used-menu (max-buffer-name-length)
852 "Return a list for the most recently used buffers.
853 It takes the form ((TITLE . BUFFER-LIST)...)."
854 (when (and (numberp msb-display-most-recently-used
)
855 (> msb-display-most-recently-used
0))
856 (let* ((buffers (cdr (buffer-list)))
859 for buffer in buffers
862 (and (not (msb-invisible-buffer-p))
863 (not (eq major-mode
'dired-mode
))))
864 collect
(save-excursion
866 (cons (funcall msb-item-handling-function
868 max-buffer-name-length
)
871 until
(>= n msb-display-most-recently-used
))))
872 (cons (if (stringp msb-most-recently-used-title
)
873 (format msb-most-recently-used-title
874 (length most-recently-used
))
875 (signal 'wrong-type-argument
(list msb-most-recently-used-title
)))
876 most-recently-used
))))
878 (defun msb--create-buffer-menu-2 ()
879 (let ((max-buffer-name-length 0)
881 function-info-vector
)
882 ;; Calculate the longest buffer name.
883 (dolist (buffer (buffer-list))
884 (when (or msb-display-invisible-buffers-p
885 (not (msb-invisible-buffer-p)))
886 (setq max-buffer-name-length
887 (max max-buffer-name-length
(length (buffer-name buffer
))))))
888 ;; Make a list with elements of type
889 ;; (BUFFER-LIST-VARIABLE
895 ;; Uses "function-global" variables:
896 ;; function-info-vector
897 (setq function-info-vector
898 (apply (function vector
)
899 (mapcar (function msb--create-function-info
)
900 (append msb-menu-cond
(msb--mode-menu-cond)))))
901 ;; Split the buffer-list into several lists; one list for each
902 ;; criteria. This is the most critical part with respect to time.
903 (dolist (buffer (buffer-list))
904 (cond ((and msb-files-by-directory
905 (buffer-file-name buffer
)
906 ;; exclude ange-ftp buffers
907 ;;(not (string-match "\\/[^/:]+:"
908 ;; (buffer-file-name buffer)))
910 (push buffer file-buffers
))
912 (msb--choose-menu buffer
914 max-buffer-name-length
))))
917 (mapcar (lambda (buffer-list)
918 (cons msb-files-by-directory-sort-key
919 (cons (car buffer-list
)
923 (cons (save-excursion
925 (funcall msb-item-handling-function
927 max-buffer-name-length
))
931 (lambda (item1 item2
)
932 (string< (car item1
) (car item2
))))))))
933 (msb--choose-file-menu file-buffers
))))
934 ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
937 (msb--most-recently-used-menu max-buffer-name-length
))
938 (others (nconc file-buffers
940 across function-info-vector
941 for value
= (msb--create-sort-item elt
)
942 if value collect value
))))
944 (mapcar 'cdr
;Remove the SORT-KEY
945 ;; Sort the menus - not the items.
948 ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
949 ;; Also sorts the items within the menus.
950 (if (cdr most-recently-used
)
952 ;; Add most recent used buffers
953 (cons msb-most-recently-used-sort-key
958 (< (car elt1
) (car elt2
)))))))
959 ;; Now make it a keymap menu
961 '(keymap "Select Buffer")
962 (msb--make-keymap-menu menu
)
963 (when msb-separator-diff
964 (list (list 'separator
"--")))
967 (if msb-files-by-directory
969 "*Files by directory*")
970 'msb--toggle-menu-type
)))))))
972 (defun msb--create-buffer-menu ()
975 (msb--create-buffer-menu-2))))
977 (defun msb--toggle-menu-type ()
978 "Multi purpose function for selecting a buffer with the mouse."
980 (setq msb-files-by-directory
(not msb-files-by-directory
))
981 ;; This gets a warning, but it is correct,
982 ;; because this file redefines menu-bar-update-buffers.
983 (msb-menu-bar-update-buffers t
))
985 (defun mouse-select-buffer (event)
986 "Pop up several menus of buffers, for selection with the mouse.
987 Returns the selected buffer or nil if no buffer is selected.
989 The way the buffers are split is conveniently handled with the
990 variable `msb-menu-cond'."
991 ;; Popup the menu and return the selected buffer.
993 (not msb--last-buffer-menu
)
994 (not (fboundp 'frame-or-buffer-changed-p
))
995 (frame-or-buffer-changed-p))
996 (setq msb--error nil
)
997 (setq msb--last-buffer-menu
(msb--create-buffer-menu)))
998 (let ((position event
)
1000 (when (and (fboundp 'posn-x-y
)
1001 (fboundp 'posn-window
))
1002 (let ((posX (car (posn-x-y (event-start event
))))
1003 (posY (cdr (posn-x-y (event-start event
))))
1004 (posWind (posn-window (event-start event
))))
1006 (setq posX
(- posX
(funcall msb-horizontal-shift-function
))
1007 position
(list (list posX posY
) posWind
))))
1008 ;; This `sit-for' magically makes the menu stay up if the mouse
1009 ;; button is released within 0.1 second.
1012 (setq choice
(x-popup-menu position msb--last-buffer-menu
))
1014 ((eq (car choice
) 'toggle
)
1015 ;; Bring up the menu again with type toggled.
1016 (msb--toggle-menu-type)
1017 (mouse-select-buffer event
))
1018 ((and (numberp (car choice
))
1019 (null (cdr choice
)))
1020 (let ((msb--last-buffer-menu (nthcdr 3 (assq (car choice
) msb--last-buffer-menu
))))
1021 (mouse-select-buffer event
)))
1022 ((while (numberp (car choice
))
1023 (setq choice
(cdr choice
))))
1024 ((and (stringp (car choice
))
1025 (null (cdr choice
)))
1030 (error "Unknown form for buffer: %s" choice
)))))
1033 (defun msb--add-separators (sorted-list)
1035 ((or (not msb-separator-diff
)
1036 (not (numberp msb-separator-diff
)))
1039 (let ((last-key nil
))
1043 ((and msb-separator-diff
1045 (> (- (car item
) last-key
)
1046 msb-separator-diff
))
1047 (setq last-key
(car item
))
1048 (list (cons last-key
'separator
)
1051 (setq last-key
(car item
))
1055 (defun msb--split-menus-2 (list mcount result
)
1057 ((> (length list
) msb-max-menu-items
)
1061 (while (< count msb-max-menu-items
)
1062 (push (pop list
) tmp-list
)
1064 (setq tmp-list
(nreverse tmp-list
))
1065 (setq sub-name
(concat (car (car tmp-list
)) "..."))
1066 (push (nconc (list mcount sub-name
1070 (msb--split-menus-2 list
(1+ mcount
) result
))
1075 (setq sub-name
(concat (car (car list
)) "..."))
1076 (push (nconc (list mcount sub-name
1080 (nreverse result
))))
1082 (defun msb--split-menus (list)
1083 (if (and (integerp msb-max-menu-items
)
1084 (> msb-max-menu-items
0))
1085 (msb--split-menus-2 list
0 nil
)
1088 (defun msb--make-keymap-menu (raw-menu)
1089 (let ((end (cons '(nil) 'menu-bar-select-buffer
))
1094 ((eq 'separator sub-menu
)
1095 (list 'separator
"--"))
1097 (let ((buffers (mapcar (function
1099 (let ((string (car item
))
1100 (buffer (cdr item
)))
1101 (cons (buffer-name buffer
)
1102 (cons string end
)))))
1104 (nconc (list (incf mcount
) (car sub-menu
)
1105 'keymap
(car sub-menu
))
1106 (msb--split-menus buffers
))))))
1109 (defun msb-menu-bar-update-buffers (&optional arg
)
1110 "A re-written version of `menu-bar-update-buffers'."
1111 ;; If user discards the Buffers item, play along.
1112 (when (and (lookup-key (current-global-map) [menu-bar buffer
])
1113 (or (not (fboundp 'frame-or-buffer-changed-p
))
1114 (frame-or-buffer-changed-p)
1116 (let ((frames (frame-list))
1117 buffers-menu frames-menu
)
1118 ;; Make the menu of buffers proper.
1119 (setq msb--last-buffer-menu
(msb--create-buffer-menu))
1120 (setq buffers-menu msb--last-buffer-menu
)
1121 ;; Make a Frames menu if we have more than one frame.
1123 (let* ((frame-length (length frames
))
1124 (f-title (format "Frames (%d)" frame-length
)))
1125 ;; List only the N most recently selected frames
1126 (when (and (integerp msb-max-menu-items
)
1127 (> msb-max-menu-items
1)
1128 (> frame-length msb-max-menu-items
))
1129 (setcdr (nthcdr msb-max-menu-items frames
) nil
))
1132 (list 'frame f-title
'(nil) 'keymap f-title
)
1138 (frame-parameters frame
)))
1140 'menu-bar-select-frame
))
1142 (define-key (current-global-map) [menu-bar buffer
]
1144 (if (and buffers-menu frames-menu
)
1145 ;; Combine Frame and Buffers menus with separator between
1146 (nconc (list 'keymap
"Buffers and Frames" frames-menu
1147 (and msb-separator-diff
'(separator "--")))
1148 (cddr buffers-menu
))
1149 (or buffers-menu
'undefined
)))))))
1151 ;; Snarf current bindings of `mouse-buffer-menu' (normally
1153 (defvar msb-mode-map
1154 (let ((map (make-sparse-keymap)))
1155 (mapcar (lambda (key)
1156 (define-key map key
#'msb
))
1157 (where-is-internal 'mouse-buffer-menu
(make-sparse-keymap)))
1161 (defun msb-mode (&optional arg
)
1163 With arg, turn Msb mode on if and only if arg is positive.
1164 This mode overrides the binding(s) of `mouse-buffer-menu' to provide a
1165 different buffer menu using the function `msb'."
1167 (setq msb-mode
(if arg
1168 (> (prefix-numeric-value arg
) 0)
1172 (add-hook 'menu-bar-update-hook
'msb-menu-bar-update-buffers
)
1173 (remove-hook 'menu-bar-update-hook
'menu-bar-update-buffers
))
1174 (remove-hook 'menu-bar-update-hook
'msb-menu-bar-update-buffers
)
1175 (add-hook 'menu-bar-update-hook
'menu-bar-update-buffers
))
1176 (run-hooks 'menu-bar-update-hook
))
1178 (add-to-list 'minor-mode-map-alist
(cons 'msb-mode msb-mode-map
))
1181 (eval-after-load 'msb
(run-hooks 'msb-after-load-hooks
))
1183 ;;; msb.el ends here