1 ;;; msb.el --- customizable buffer-selection with multiple menus
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Lars Lindberg <lars.lindberg@home.se>
9 ;; Lindberg's last update version: 3.34
10 ;; Keywords: mouse buffer menu
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; Purpose of this package:
32 ;; 1. Offer a function for letting the user choose buffer,
33 ;; not necessarily for switching to it.
34 ;; 2. Make a better mouse-buffer-menu. This is done as a global
35 ;; minor mode, msb-mode.
38 ;; Look at the variable `msb-menu-cond' for deciding what menus you
39 ;; want. It's not that hard to customize, despite my not-so-good
40 ;; doc-string. Feel free to send me a better doc-string.
41 ;; There are some constants for you to try here:
43 ;; msb--very-many-menus (default)
45 ;; Look at the variable `msb-item-handling-function' for customization
46 ;; of the appearance of every menu item. Try for instance setting
47 ;; it to `msb-alon-item-handler'.
49 ;; Look at the variable `msb-item-sort-function' for customization
50 ;; of sorting the menus. Set it to t for instance, which means no
51 ;; sorting - you will get latest used buffer first.
53 ;; Also check out the variable `msb-display-invisible-buffers-p'.
56 ;; - Files-by-directory
57 ;; + No possibility to show client/changed buffers separately.
58 ;; + All file buffers only appear in a file sub-menu, they will
59 ;; for instance not appear in the Mail sub-menu.
61 ;; Future enhancements:
64 ;; Mark Brader <msb@sq.com>
65 ;; Jim Berry <m1jhb00@FRB.GOV>
66 ;; Hans Chalupsky <hans@cs.Buffalo.EDU>
67 ;; Larry Rosenberg <ljr@ictv.com>
68 ;; Will Henney <will@astroscu.unam.mx>
69 ;; Jari Aalto <jaalto@tre.tele.nokia.fi>
70 ;; Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
71 ;; Gael Marziou <gael@gnlab030.grenoble.hp.com>
72 ;; Dave Gillespie <daveg@thymus.synaptics.com>
73 ;; Alon Albert <alon@milcse.rtsg.mot.com>
74 ;; Kevin Broadey, <KevinB@bartley.demon.co.uk>
75 ;; Ake Stenhof <ake@cadpoint.se>
76 ;; Richard Stallman <rms@gnu.org>
77 ;; Steve Fisk <fisk@medved.bowdoin.edu>
79 ;; This version turned into a global minor mode and subsequently
80 ;; hacked on by Dave Love.
83 (eval-when-compile (require 'cl
))
86 ;;; Some example constants to be used for `msb-menu-cond'. See that
87 ;;; variable for more information. Please note that if the condition
88 ;;; returns `multi', then the buffer can appear in several menus.
90 (defconst msb--few-menus
91 '(((and (boundp 'server-buffer-clients
)
96 ((and msb-display-invisible-buffers-p
97 (msb-invisible-buffer-p)
100 "Invisible buffers (%d)")
101 ((eq major-mode
'dired-mode
)
104 msb-dired-item-handler
105 msb-sort-by-directory
)
106 ((eq major-mode
'Man-mode
)
109 ((eq major-mode
'w3-mode
)
112 ((or (memq major-mode
113 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode
))
114 (memq major-mode
'(mh-letter-mode mh-show-mode mh-folder-mode
))
116 '(gnus-summary-mode message-mode gnus-group-mode
117 gnus-article-mode score-mode gnus-browse-killed-mode
)))
120 ((not buffer-file-name
)
127 (defconst msb--very-many-menus
128 '(((and (boundp 'server-buffer-clients
)
129 server-buffer-clients
133 ((and (boundp 'vc-mode
) vc-mode
'multi
)
135 "Version Control (%d)")
136 ((and buffer-file-name
140 "Changed files (%d)")
141 ((and (get-buffer-process (current-buffer))
145 ((and msb-display-invisible-buffers-p
146 (msb-invisible-buffer-p)
149 "Invisible buffers (%d)")
150 ((eq major-mode
'dired-mode
)
153 ;; Note this different menu-handler
154 msb-dired-item-handler
155 ;; Also note this item-sorter
156 msb-sort-by-directory
)
157 ((eq major-mode
'Man-mode
)
160 ((eq major-mode
'w3-mode
)
163 ((or (memq major-mode
164 '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode
))
165 (memq major-mode
'(mh-letter-mode mh-show-mode mh-folder-mode
))
166 (memq major-mode
'(gnus-summary-mode message-mode gnus-group-mode
167 gnus-article-mode score-mode
168 gnus-browse-killed-mode
)))
171 ;; Catchup for all non-file buffers
172 ((and (not buffer-file-name
)
175 "Other non-file buffers (%d)")
176 ((and (string-match "/\\.[^/]*$" buffer-file-name
)
180 ((memq major-mode
'(c-mode c
++-mode
))
183 ((eq major-mode
'emacs-lisp-mode
)
186 ((eq major-mode
'latex-mode
)
191 "Other files (%d)")))
193 ;; msb--many-menus is obsolete
194 (defvar msb--many-menus msb--very-many-menus
)
197 ;;; Customizable variables
201 "Customizable buffer-selection with multiple menus."
205 (defun msb-custom-set (symbol value
)
206 "Set the value of custom variables for msb."
208 (if (and (featurep 'msb
) msb-mode
)
209 ;; wait until package has been loaded before bothering to update
211 (msb-menu-bar-update-buffers t
)))
213 (defcustom msb-menu-cond msb--very-many-menus
214 "*List of criteria for splitting the mouse buffer menu.
215 The elements in the list should be of this type:
216 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
218 When making the split, the buffers are tested one by one against the
219 CONDITION, just like a Lisp cond: When hitting a true condition, the
220 other criteria are *not* tested and the buffer name will appear in the
221 menu with the menu-title corresponding to the true condition.
223 If the condition returns the symbol `multi', then the buffer will be
224 added to this menu *and* tested for other menus too. If it returns
225 `no-multi', then the buffer will only be added if it hasn't been added
228 During this test, the buffer in question is the current buffer, and
229 the test is surrounded by calls to `save-excursion' and
232 The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
233 A value of nil means don't display this menu.
235 MENU-TITLE is really a format. If you add %d in it, the %d is
236 replaced with the number of items in that menu.
238 ITEM-HANDLING-FN, is optional. If it is supplied and is a function,
239 than it is used for displaying the items in that particular buffer
240 menu, otherwise the function pointed out by
241 `msb-item-handling-function' is used.
243 ITEM-SORT-FN, is also optional.
244 If it is not supplied, the function pointed out by
245 `msb-item-sort-function' is used.
246 If it is nil, then no sort takes place and the buffers are presented
247 in least-recently-used order.
248 If it is t, then no sort takes place and the buffers are presented in
249 most-recently-used order.
250 If it is supplied and non-nil and not t than it is used for sorting
251 the items in that particular buffer menu.
253 Note1: There should always be a `catch-all' as last element, in this
254 list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
255 Note2: A buffer menu appears only if it has at least one buffer in it.
256 Note3: If you have a CONDITION that can't be evaluated you will get an
257 error every time you do \\[msb]."
258 :type
`(choice (const :tag
"long" :value
,msb--very-many-menus
)
259 (const :tag
"short" :value
,msb--few-menus
)
264 (defcustom msb-modes-key
4000
265 "The sort key for files sorted by mode."
271 (defcustom msb-separator-diff
100
272 "*Non-nil means use separators.
273 The separators will appear between all menus that have a sorting key
274 that differs by this value or more."
275 :type
'(choice integer
(const nil
))
279 (defvar msb-files-by-directory-sort-key
0
280 "*The sort key for files sorted by directory.")
282 (defcustom msb-max-menu-items
15
283 "*The maximum number of items in a menu.
284 If this variable is set to 15 for instance, then the submenu will be
285 split up in minor parts, 15 items each. nil means no limit."
286 :type
'(choice integer
(const nil
))
290 (defcustom msb-max-file-menu-items
10
291 "*The maximum number of items from different directories.
293 When the menu is of type `file by directory', this is the maximum
294 number of buffers that are clumped together from different
297 Set this to 1 if you want one menu per directory instead of clumping
300 If the value is not a number, then the value 10 is used."
305 (defcustom msb-most-recently-used-sort-key -
1010
306 "*Where should the menu with the most recently used buffers be placed?"
311 (defcustom msb-display-most-recently-used
15
312 "*How many buffers should be in the most-recently-used menu.
313 No buffers at all if less than 1 or nil (or any non-number)."
318 (defcustom msb-most-recently-used-title
"Most recently used (%d)"
319 "*The title for the most-recently-used menu."
324 (defvar msb-horizontal-shift-function
'(lambda () 0)
325 "*Function that specifies how many pixels to shift the top menu leftwards.")
327 (defcustom msb-display-invisible-buffers-p nil
328 "*Show invisible buffers or not.
329 Non-nil means that the buffer menu should include buffers that have
330 names that starts with a space character."
335 (defvar msb-item-handling-function
'msb-item-handler
336 "*The appearance of a buffer menu.
338 The default function to call for handling the appearance of a menu
339 item. It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
340 where the latter is the max length of all buffer names.
342 The function should return the string to use in the menu.
344 When the function is called, BUFFER is the current buffer. This
345 function is called for items in the variable `msb-menu-cond' that have
346 nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
349 (defcustom msb-item-sort-function
'msb-sort-by-name
350 "*The order of items in a buffer menu.
352 The default function to call for handling the order of items in a menu
353 item. This function is called like a sort function. The items look
354 like (ITEM-NAME . BUFFER).
356 ITEM-NAME is the name of the item that will appear in the menu.
357 BUFFER is the buffer, this is not necessarily the current buffer.
359 Set this to nil or t if you don't want any sorting (faster)."
360 :type
'(choice (const msb-sort-by-name
)
361 (const :tag
"Newest first" t
)
362 (const :tag
"Oldest first" nil
))
366 (defcustom msb-files-by-directory nil
367 "*Non-nil means that files should be sorted by directory.
368 This is instead of the groups in `msb-menu-cond'."
373 (defcustom msb-after-load-hook nil
374 "Hook run after the msb package has been loaded."
380 ;;; Internal variables
383 ;; The last calculated menu.
384 (defvar msb--last-buffer-menu nil
)
386 ;; If this is non-nil, then it is a string that describes the error.
387 (defvar msb--error nil
)
390 ;;; Some example function to be used for `msb-item-handling-function'.
392 (defun msb-item-handler (buffer &optional maxbuf
)
393 "Create one string item, concerning BUFFER, for the buffer menu.
396 The `*' appears only if the buffer is marked as modified.
397 The `%' appears only if the buffer is read-only.
398 Optional second argument MAXBUF is completely ignored."
399 (let ((name (buffer-name))
400 (modified (if (buffer-modified-p) "*" " "))
401 (read-only (if buffer-read-only
"%" " ")))
402 (format "%s%s %s" modified read-only name
)))
405 (eval-when-compile (require 'dired
))
407 ;; `dired' can be called with a list of the form (directory file1 file2 ...)
408 ;; which causes `dired-directory' to be in the same form.
409 (defun msb--dired-directory ()
410 (cond ((stringp dired-directory
)
411 (abbreviate-file-name (expand-file-name dired-directory
)))
412 ((consp dired-directory
)
413 (abbreviate-file-name (expand-file-name (car dired-directory
))))
415 (error "Unknown type of `dired-directory' in buffer %s"
418 (defun msb-dired-item-handler (buffer &optional maxbuf
)
419 "Create one string item, concerning a dired BUFFER, for the buffer menu.
422 The `*' appears only if the buffer is marked as modified.
423 The `%' appears only if the buffer is read-only.
424 Optional second argument MAXBUF is completely ignored."
425 (let ((name (msb--dired-directory))
426 (modified (if (buffer-modified-p) "*" " "))
427 (read-only (if buffer-read-only
"%" " ")))
428 (format "%s%s %s" modified read-only name
)))
430 (defun msb-alon-item-handler (buffer maxbuf
)
431 "Create one string item for the buffer menu.
433 <buffer-name> *%# <file-name>
434 The `*' appears only if the buffer is marked as modified.
435 The `%' appears only if the buffer is read-only.
436 The `#' appears only version control file (SCCS/RCS)."
437 (format (format "%%%ds %%s%%s%%s %%s" maxbuf
)
439 (if (buffer-modified-p) "*" " ")
440 (if buffer-read-only
"%" " ")
441 (if (and (boundp 'vc-mode
) vc-mode
) "#" " ")
442 (or buffer-file-name
"")))
445 ;;; Some example function to be used for `msb-item-sort-function'.
447 (defun msb-sort-by-name (item1 item2
)
448 "Sort the items ITEM1 and ITEM2 by their `buffer-name'.
449 An item looks like (NAME . BUFFER)."
450 (string-lessp (buffer-name (cdr item1
))
451 (buffer-name (cdr item2
))))
454 (defun msb-sort-by-directory (item1 item2
)
455 "Sort the items ITEM1 and ITEM2 by directory name. Made for dired.
456 An item look like (NAME . BUFFER)."
457 (string-lessp (with-current-buffer (cdr item1
)
458 (msb--dired-directory))
459 (with-current-buffer (cdr item2
)
460 (msb--dired-directory))))
465 ;;; This function can be used instead of (mouse-buffer-menu EVENT)
466 ;;; function in "mouse.el".
469 "Pop up several menus of buffers for selection with the mouse.
470 This command switches buffers in the window that you clicked on, and
473 See the function `mouse-select-buffer' and the variable
474 `msb-menu-cond' for more information about how the menus are split."
476 (let ((old-window (selected-window))
477 (window (posn-window (event-start event
)))
479 (unless (framep window
) (select-window window
))
480 ;; This `sit-for' magically makes the menu stay up if the mouse
481 ;; button is released within 0.1 second.
482 (setq early-release
(not (sit-for 0.1 t
)))
483 (let ((buffer (mouse-select-buffer event
)))
485 (switch-to-buffer buffer
)
486 (select-window old-window
)))
487 ;; If the above `sit-for' was interrupted by a mouse-up, avoid
488 ;; generating a drag event.
489 (if (and early-release
(memq 'down
(event-modifiers last-input-event
)))
494 ;;; Some supportive functions
496 (defun msb-invisible-buffer-p (&optional buffer
)
497 "Return t if optional BUFFER is an \"invisible\" buffer.
498 If the argument is left out or nil, then the current buffer is considered."
499 (and (> (length (buffer-name buffer
)) 0)
500 (eq ?\s
(aref (buffer-name buffer
) 0))))
502 (defun msb--strip-dir (dir)
503 "Strip one hierarchy level from the end of DIR."
504 (file-name-directory (directory-file-name dir
)))
506 ;; Create an alist with all buffers from LIST that lies under the same
507 ;; directory will be in the same item as the directory name.
508 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K BUFFER-K+1...)) ...)
509 (defun msb--init-file-alist (list)
511 ;; Make alist that looks like
512 ;; ((DIR-1 BUFFER-1) (DIR-2 BUFFER-2) ...)
518 (let ((file-name (expand-file-name
519 (buffer-file-name buffer
))))
521 (list (cons (msb--strip-dir file-name
) buffer
)))))
523 (lambda (item1 item2
)
524 (string< (car item1
) (car item2
))))))
525 ;; Now clump buffers together that have the same directory name
526 ;; Make alist that looks like
527 ;; ((DIR1 . (BUFFER-1 BUFFER-2 ...)) (DIR2 . (BUFFER-K)) ...)
533 (mapcar (lambda (item)
535 ((equal dir
(car item
))
536 ;; The same dir as earlier:
537 ;; Add to current list of buffers.
538 (push (cdr item
) buffers
)
539 ;; This item should not be added to list
543 (let ((result (and dir
(cons dir buffers
))))
544 (setq dir
(car item
))
545 (setq buffers
(list (cdr item
)))
546 ;; Add the last result the list.
547 (and result
(list result
))))))
549 ;; Add the last result to the list
550 (list (cons dir buffers
))))))
552 (defun msb--format-title (top-found-p dir number-of-items
)
553 "Format a suitable title for the menu item."
554 (format (if top-found-p
"%s... (%d)" "%s (%d)")
555 (abbreviate-file-name dir
) number-of-items
))
557 ;; Variables for debugging.
558 (defvar msb--choose-file-menu-list
)
559 (defvar msb--choose-file-menu-arg-list
)
561 (defun msb--choose-file-menu (list)
562 "Choose file-menu with respect to directory for every buffer in LIST."
563 (setq msb--choose-file-menu-arg-list list
)
564 (let ((buffer-alist (msb--init-file-alist list
))
566 (max-clumped-together (if (numberp msb-max-file-menu-items
)
567 msb-max-file-menu-items
571 first rest dir buffers old-dir
)
572 ;; Prepare for looping over all items in buffer-alist
573 (setq first
(car buffer-alist
)
574 rest
(cdr buffer-alist
)
577 (setq msb--choose-file-menu-list
(copy-sequence rest
))
578 ;; This big loop tries to clump buffers together that have a
579 ;; similar name. Remember that buffer-alist is sorted based on the
580 ;; directory name of the buffers' visited files.
585 (setq item
(car tmp-rest
))
586 ;; Clump together the "rest"-buffers that have a dir that is
587 ;; a subdir of the current one.
589 (<= (length buffers
) max-clumped-together
)
590 (>= (length (car item
)) (length dir
))
591 ;; `completion-ignore-case' seems to default to t
592 ;; on the systems with case-insensitive file names.
593 (eq t
(compare-strings dir
0 nil
594 (car item
) 0 (length dir
)
595 completion-ignore-case
)))
597 (setq buffers
(append buffers
(cdr item
))) ;nconc is faster than append
598 (setq tmp-rest
(cdr tmp-rest
)
599 item
(car tmp-rest
)))
601 ((> (length buffers
) max-clumped-together
)
602 ;; Oh, we failed. Too many buffers clumped together.
603 ;; Just use the original ones for the result.
604 (setq last-dir
(car first
))
605 (push (cons (msb--format-title top-found-p
607 (length (cdr first
)))
610 (setq top-found-p nil
)
611 (setq first
(car rest
)
614 buffers
(cdr first
)))
616 ;; The first pass of clumping together worked out, go ahead
620 (setq first
(cons dir buffers
)
622 ;; Now see if we can clump more buffers together if we go up
623 ;; one step in the file hierarchy.
624 ;; If dir isn't changed by msb--strip-dir, we are looking
625 ;; at the machine name component of an ange-ftp filename.
627 (setq dir
(msb--strip-dir dir
)
629 (if (equal old-dir dir
)
632 (or (and (>= (length dir
) (length last-dir
))
633 (eq t
(compare-strings
636 completion-ignore-case
)))
637 (and (< (length dir
) (length last-dir
))
638 (eq t
(compare-strings
639 dir
0 nil last-dir
0 (length dir
)
640 completion-ignore-case
)))))
641 ;; We have reached the same place in the file hierarchy as
642 ;; the last result, so we should quit at this point and
643 ;; take what we have as result.
644 (push (cons (msb--format-title top-found-p
646 (length (cdr first
)))
649 (setq top-found-p nil
)
650 (setq first
(car rest
)
653 buffers
(cdr first
)))))))
654 ;; Now take care of the last item.
656 (push (cons (msb--format-title top-found-p
658 (length (cdr first
)))
661 (setq top-found-p nil
)
662 (nreverse final-list
)))
664 (defun msb--create-function-info (menu-cond-elt)
665 "Create a vector from an element MENU-COND-ELT of `msb-menu-cond'.
667 \]BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER)
668 See `msb-menu-cond' for a description of its elements."
669 (let* ((list-symbol (make-symbol "-msb-buffer-list"))
670 (tmp-ih (and (> (length menu-cond-elt
) 3)
671 (nth 3 menu-cond-elt
)))
672 (item-handler (if (and tmp-ih
(fboundp tmp-ih
))
674 msb-item-handling-function
))
675 (tmp-s (if (> (length menu-cond-elt
) 4)
676 (nth 4 menu-cond-elt
)
677 msb-item-sort-function
))
678 (sorter (if (or (fboundp tmp-s
)
682 msb-item-sort-function
)))
683 (when (< (length menu-cond-elt
) 3)
684 (error "Wrong format of msb-menu-cond"))
685 (when (and (> (length menu-cond-elt
) 3)
686 (not (fboundp tmp-ih
)))
687 (signal 'invalid-function
(list tmp-ih
)))
688 (when (and (> (length menu-cond-elt
) 4)
690 (not (fboundp tmp-s
))
692 (signal 'invalid-function
(list tmp-s
)))
694 (vector list-symbol
;BUFFER-LIST-VARIABLE
695 (nth 0 menu-cond-elt
) ;CONDITION
696 (nth 1 menu-cond-elt
) ;SORT-KEY
697 (nth 2 menu-cond-elt
) ;MENU-TITLE
698 item-handler
;ITEM-HANDLER
702 ;; This defsubst is only used in `msb--choose-menu' below. It was
703 ;; pulled out merely to make the code somewhat clearer. The indentation
704 ;; level was too big.
705 (defsubst msb--collect
(function-info-vector)
709 (setq function-info-list
711 across function-info-vector
713 (eval (aref fi
1))) ;Test CONDITION
714 (not (and (eq result
'no-multi
)
716 (progn (when (eq result
'multi
)
721 (not (eq result
'multi
)))))
722 (when (and (not function-info-list
)
724 (error "No catch-all in msb-menu-cond!"))
727 (defun msb--add-to-menu (buffer function-info max-buffer-name-length
)
728 "Add BUFFER to the menu depicted by FUNCTION-INFO.
729 All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
730 to the buffer-list variable in function-info."
731 (let ((list-symbol (aref function-info
0))) ;BUFFER-LIST-VARIABLE
732 ;; Here comes the hairy side-effect!
734 (cons (cons (funcall (aref function-info
4) ;ITEM-HANDLER
736 max-buffer-name-length
)
738 (eval list-symbol
)))))
740 (defsubst msb--choose-menu
(buffer function-info-vector max-buffer-name-length
)
741 "Select the appropriate menu for BUFFER."
742 ;; This is all side-effects, folks!
743 ;; This should be optimized.
744 (unless (and (not msb-display-invisible-buffers-p
)
745 (msb-invisible-buffer-p buffer
))
747 (with-current-buffer buffer
748 ;; Menu found. Add to this menu
749 (dolist (info (msb--collect function-info-vector
))
750 (msb--add-to-menu buffer info max-buffer-name-length
)))
751 (error (unless msb--error
754 "In msb-menu-cond, error for buffer `%s'."
755 (buffer-name buffer
)))
756 (error "%s" msb--error
))))))
758 (defun msb--create-sort-item (function-info)
759 "Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the buffer-list is empty."
760 (let ((buffer-list (eval (aref function-info
0))))
762 (let ((sorter (aref function-info
5)) ;SORTER
763 (sort-key (aref function-info
2))) ;MENU-SORT-KEY
766 (cons (format (aref function-info
3) ;MENU-TITLE
767 (length buffer-list
))
772 (nreverse buffer-list
))
774 (sort buffer-list sorter
))))))))))
776 (defun msb--aggregate-alist (alist same-predicate sort-predicate
)
777 "Return ALIST as a sorted, aggregated alist.
779 In the result all items with the same car element (according to
780 SAME-PREDICATE) are aggregated together. The alist is first sorted by
784 \(msb--aggregate-alist
785 '((a . a1) (a . a2) (b . b1) (c . c3) (a . a4) (a . a3) (b . b3) (b . b2))
787 (lambda (item1 item2)
788 (string< (symbol-name item1) (symbol-name item2))))
790 \((a a1 a2 a4 a3) (b b1 b3 b2) (c c3))"
791 (when (not (null alist
))
803 (push (cdr item
) same
)
804 (setq first-time-p nil
)
805 (setq old-car
(car item
))
807 ((funcall same-predicate
(car item
) old-car
)
808 (push (cdr item
) same
)
813 (setq same
(list (cdr item
))
815 (list (cons tmp-old-car
(nreverse tmp-same
))))))
816 (sort alist
(lambda (item1 item2
)
817 (funcall sort-predicate
818 (car item1
) (car item2
))))))
819 (list (cons old-car
(nreverse same
)))))))
822 (defun msb--mode-menu-cond ()
823 (let ((key msb-modes-key
))
824 (mapcar (lambda (item)
826 (list `( eq major-mode
(quote ,(car item
)))
828 (concat (cdr item
) " (%d)")))
830 (let ((mode-list nil
))
831 (dolist (buffer (cdr (buffer-list)))
832 (with-current-buffer buffer
833 (when (and (not (msb-invisible-buffer-p))
834 (not (assq major-mode mode-list
)))
835 (push (cons major-mode mode-name
)
838 (lambda (item1 item2
)
839 (string< (cdr item1
) (cdr item2
)))))))
841 (defun msb--most-recently-used-menu (max-buffer-name-length)
842 "Return a list for the most recently used buffers.
843 It takes the form ((TITLE . BUFFER-LIST)...)."
844 (when (and (numberp msb-display-most-recently-used
)
845 (> msb-display-most-recently-used
0))
846 (let* ((buffers (cdr (buffer-list)))
849 for buffer in buffers
850 if
(with-current-buffer buffer
851 (and (not (msb-invisible-buffer-p))
852 (not (eq major-mode
'dired-mode
))))
853 collect
(with-current-buffer buffer
854 (cons (funcall msb-item-handling-function
856 max-buffer-name-length
)
859 until
(>= n msb-display-most-recently-used
))))
860 (cons (if (stringp msb-most-recently-used-title
)
861 (format msb-most-recently-used-title
862 (length most-recently-used
))
863 (signal 'wrong-type-argument
(list msb-most-recently-used-title
)))
864 most-recently-used
))))
866 (defun msb--create-buffer-menu-2 ()
867 (let ((max-buffer-name-length 0)
869 function-info-vector
)
870 ;; Calculate the longest buffer name.
871 (dolist (buffer (buffer-list))
872 (when (or msb-display-invisible-buffers-p
873 (not (msb-invisible-buffer-p)))
874 (setq max-buffer-name-length
875 (max max-buffer-name-length
(length (buffer-name buffer
))))))
876 ;; Make a list with elements of type
877 ;; (BUFFER-LIST-VARIABLE
883 ;; Uses "function-global" variables:
884 ;; function-info-vector
885 (setq function-info-vector
886 (apply (function vector
)
887 (mapcar (function msb--create-function-info
)
888 (append msb-menu-cond
(msb--mode-menu-cond)))))
889 ;; Split the buffer-list into several lists; one list for each
890 ;; criteria. This is the most critical part with respect to time.
891 (dolist (buffer (buffer-list))
892 (cond ((and msb-files-by-directory
893 (buffer-file-name buffer
)
894 ;; exclude ange-ftp buffers
895 ;;(not (string-match "\\/[^/:]+:"
896 ;; (buffer-file-name buffer)))
898 (push buffer file-buffers
))
900 (msb--choose-menu buffer
902 max-buffer-name-length
))))
905 (mapcar (lambda (buffer-list)
906 (list* msb-files-by-directory-sort-key
909 (mapcar (lambda (buffer)
910 (cons (with-current-buffer buffer
912 msb-item-handling-function
914 max-buffer-name-length
))
917 (lambda (item1 item2
)
918 (string< (car item1
) (car item2
))))))
919 (msb--choose-file-menu file-buffers
))))
920 ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
923 (msb--most-recently-used-menu max-buffer-name-length
))
924 (others (nconc file-buffers
926 across function-info-vector
927 for value
= (msb--create-sort-item elt
)
928 if value collect value
))))
930 (mapcar 'cdr
;Remove the SORT-KEY
931 ;; Sort the menus - not the items.
934 ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
935 ;; Also sorts the items within the menus.
936 (if (cdr most-recently-used
)
938 ;; Add most recent used buffers
939 (cons msb-most-recently-used-sort-key
944 (< (car elt1
) (car elt2
)))))))
945 ;; Now make it a keymap menu
947 '(keymap "Select Buffer")
948 (msb--make-keymap-menu menu
)
949 (when msb-separator-diff
950 (list (list 'separator
"--")))
953 (if msb-files-by-directory
955 "*Files by directory*")
956 'msb--toggle-menu-type
)))))))
958 (defun msb--create-buffer-menu ()
961 (msb--create-buffer-menu-2))))
963 (defun msb--toggle-menu-type ()
964 "Multi purpose function for selecting a buffer with the mouse."
966 (setq msb-files-by-directory
(not msb-files-by-directory
))
967 ;; This gets a warning, but it is correct,
968 ;; because this file redefines menu-bar-update-buffers.
969 (msb-menu-bar-update-buffers t
))
971 (defun mouse-select-buffer (event)
972 "Pop up several menus of buffers, for selection with the mouse.
973 Returns the selected buffer or nil if no buffer is selected.
975 The way the buffers are split is conveniently handled with the
976 variable `msb-menu-cond'."
977 ;; Popup the menu and return the selected buffer.
979 (not msb--last-buffer-menu
)
980 (not (fboundp 'frame-or-buffer-changed-p
))
981 (frame-or-buffer-changed-p))
982 (setq msb--error nil
)
983 (setq msb--last-buffer-menu
(msb--create-buffer-menu)))
984 (let ((position event
)
986 (when (and (fboundp 'posn-x-y
)
987 (fboundp 'posn-window
))
988 (let ((posX (car (posn-x-y (event-start event
))))
989 (posY (cdr (posn-x-y (event-start event
))))
990 (posWind (posn-window (event-start event
))))
992 (setq posX
(- posX
(funcall msb-horizontal-shift-function
))
993 position
(list (list posX posY
) posWind
))))
995 (setq choice
(x-popup-menu position msb--last-buffer-menu
))
997 ((eq (car choice
) 'toggle
)
998 ;; Bring up the menu again with type toggled.
999 (msb--toggle-menu-type)
1000 (mouse-select-buffer event
))
1001 ((and (numberp (car choice
))
1002 (null (cdr choice
)))
1003 (let ((msb--last-buffer-menu (nthcdr 2 (assq (car choice
)
1004 msb--last-buffer-menu
))))
1005 (mouse-select-buffer event
)))
1006 ((while (numberp (car choice
))
1007 (setq choice
(cdr choice
))))
1008 ((and (stringp (car choice
))
1009 (null (cdr choice
)))
1014 (error "Unknown form for buffer: %s" choice
)))))
1017 (defun msb--add-separators (sorted-list)
1018 (if (or (not msb-separator-diff
)
1019 (not (numberp msb-separator-diff
)))
1021 (let ((last-key nil
))
1026 ((and msb-separator-diff
1028 (> (- (car item
) last-key
)
1029 msb-separator-diff
))
1030 (setq last-key
(car item
))
1031 (list (cons last-key
'separator
)
1034 (setq last-key
(car item
))
1038 (defun msb--split-menus-2 (list mcount result
)
1040 ((> (length list
) msb-max-menu-items
)
1044 (while (< count msb-max-menu-items
)
1045 (push (pop list
) tmp-list
)
1047 (setq tmp-list
(nreverse tmp-list
))
1048 (setq sub-name
(concat (car (car tmp-list
)) "..."))
1049 (push (nconc (list mcount sub-name
1053 (msb--split-menus-2 list
(1+ mcount
) result
))
1058 (setq sub-name
(concat (car (car list
)) "..."))
1059 (push (nconc (list mcount sub-name
'keymap sub-name
)
1062 (nreverse result
))))
1064 (defun msb--split-menus (list)
1065 (if (and (integerp msb-max-menu-items
)
1066 (> msb-max-menu-items
0))
1067 (msb--split-menus-2 list
0 nil
)
1070 (defun msb--make-keymap-menu (raw-menu)
1071 (let ((end (cons '(nil) 'menu-bar-select-buffer
))
1076 ((eq 'separator sub-menu
)
1077 (list 'separator
"--"))
1079 (let ((buffers (mapcar (lambda (item)
1080 (cons (buffer-name (cdr item
))
1081 (cons (car item
) end
)))
1083 (nconc (list (incf mcount
) (car sub-menu
)
1084 'keymap
(car sub-menu
))
1085 (msb--split-menus buffers
))))))
1088 (defun msb-menu-bar-update-buffers (&optional arg
)
1089 "A re-written version of `menu-bar-update-buffers'."
1090 ;; If user discards the Buffers item, play along.
1091 (when (and (lookup-key (current-global-map) [menu-bar buffer
])
1092 (or (not (fboundp 'frame-or-buffer-changed-p
))
1093 (frame-or-buffer-changed-p)
1095 (let ((frames (frame-list))
1096 buffers-menu frames-menu
)
1097 ;; Make the menu of buffers proper.
1098 (setq msb--last-buffer-menu
(msb--create-buffer-menu))
1099 ;; Skip the `keymap' symbol.
1100 (setq buffers-menu
(cdr 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
)
1116 (list (frame-parameter frame
'name
)
1117 (frame-parameter frame
'name
)
1119 'menu-bar-select-frame
))
1121 (setcdr global-buffers-menu-map
1122 (if (and buffers-menu frames-menu
)
1123 ;; Combine Frame and Buffers menus with separator between
1124 (nconc (list "Buffers and Frames" frames-menu
1125 (and msb-separator-diff
'(separator "--")))
1129 ;; Snarf current bindings of `mouse-buffer-menu' (normally
1131 (defvar msb-mode-map
1132 (let ((map (make-sparse-keymap "Msb")))
1133 (define-key map
[remap mouse-buffer-menu
] 'msb
)
1137 (define-minor-mode msb-mode
1139 With arg, turn Msb mode on if and only if arg is positive.
1140 This mode overrides the binding(s) of `mouse-buffer-menu' to provide a
1141 different buffer menu using the function `msb'."
1142 :global t
:group
'msb
1145 (add-hook 'menu-bar-update-hook
'msb-menu-bar-update-buffers
)
1146 (remove-hook 'menu-bar-update-hook
'menu-bar-update-buffers
)
1147 (msb-menu-bar-update-buffers t
))
1148 (remove-hook 'menu-bar-update-hook
'msb-menu-bar-update-buffers
)
1149 (add-hook 'menu-bar-update-hook
'menu-bar-update-buffers
)
1150 (menu-bar-update-buffers t
)))
1152 (defun msb-unload-hook ()
1154 (add-hook 'msb-unload-hook
'msb-unload-hook
)
1157 (eval-after-load "msb" '(run-hooks 'msb-after-load-hook
'msb-after-load-hooks
))
1159 ;; arch-tag: 403f9e82-b92e-4e7a-a797-5d6d9b76da36
1160 ;;; msb.el ends here