1 ;;; msb.el --- Customizable buffer-selection with multiple menus.
3 ;; Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
5 ;; Author: Lars Lindberg <Lars.G.Lindberg@capgemini.se>
7 ;; Lindberg's last update version: 3.33
8 ;; Keywords: mouse buffer menu
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Purpose of this package:
30 ;; 1. Offer a function for letting the user choose buffer,
31 ;; not necessarily for switching to it.
32 ;; 2. Make a better mouse-buffer-menu.
36 ;; 1. Byte compile msb first. It uses things in the cl package that
37 ;; are slow if not compiled, but blazingly fast when compiled. I
38 ;; have also had one report that said that msb malfunctioned when
41 ;; Note! You now use msb instead of mouse-buffer-menu.
42 ;; 3. Now try the menu bar Buffers menu.
45 ;; Look at the variable `msb-menu-cond' for deciding what menus you
46 ;; want. It's not that hard to customize, despite my not-so-good
47 ;; doc-string. Feel free to send me a better doc-string.
48 ;; There are some constants for you to try here:
50 ;; msb--very-many-menus (default)
52 ;; Look at the variable `msb-item-handling-function' for customization
53 ;; of the appearance of every menu item. Try for instance setting
54 ;; it to `msb-alon-item-handler'.
56 ;; Look at the variable `msb-item-sort-function' for customization
57 ;; of sorting the menus. Set it to t for instance, which means no
58 ;; sorting - you will get latest used buffer first.
60 ;; Also check out the variable `msb-display-invisible-buffers-p'.
63 ;; - Files-by-directory
64 ;; + No possibility to show client/changed buffers separately.
65 ;; + All file buffers only appear in in a file sub-menu, they will
66 ;; for instance not appear in the Mail sub-menu.
68 ;; Future enhancements:
71 ;; Mark Brader <msb@sq.com>
72 ;; Jim Berry <m1jhb00@FRB.GOV>
73 ;; Hans Chalupsky <hans@cs.Buffalo.EDU>
74 ;; Larry Rosenberg <ljr@ictv.com>
75 ;; Will Henney <will@astroscu.unam.mx>
76 ;; Jari Aalto <jaalto@tre.tele.nokia.fi>
77 ;; Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
78 ;; Gael Marziou <gael@gnlab030.grenoble.hp.com>
79 ;; Dave Gillespie <daveg@thymus.synaptics.com>
80 ;; Alon Albert <alon@milcse.rtsg.mot.com>
81 ;; Kevin Broadey, <KevinB@bartley.demon.co.uk>
82 ;; Ake Stenhof <ake@cadpoint.se>
83 ;; Richard Stallman <rms@gnu.ai.mit.edu>
84 ;; Steve Fisk <fisk@medved.bowdoin.edu>
91 ;;; Some example constants to be used for `msb-menu-cond'. See that
92 ;;; variable for more information. Please note that if the condition
93 ;;; returns `multi', then the buffer can appear in several menus.
95 (defconst msb--few-menus
96 '(((and (boundp 'server-buffer-clients)
101 ((and msb-display-invisible-buffers-p
102 (msb-invisible-buffer-p)
105 "Invisible buffers (%d)")
106 ((eq major-mode 'dired-mode)
109 msb-dired-item-handler
110 msb-sort-by-directory)
111 ((eq major-mode 'Man-mode)
114 ((eq major-mode 'w3-mode)
117 ((or (memq major-mode '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
118 (memq major-mode '(mh-letter-mode
121 (memq major-mode '(gnus-summary-mode
126 gnus-browse-killed-mode)))
129 ((not buffer-file-name)
136 (defconst msb--very-many-menus
137 '(((and (boundp 'server-buffer-clients)
138 server-buffer-clients
142 ((and (boundp 'vc-mode) vc-mode 'multi)
144 "Version Control (%d)")
145 ((and buffer-file-name
149 "Changed files (%d)")
150 ((and (get-buffer-process (current-buffer))
154 ((and msb-display-invisible-buffers-p
155 (msb-invisible-buffer-p)
158 "Invisible buffers (%d)")
159 ((eq major-mode 'dired-mode)
162 ;; Note this different menu-handler
163 msb-dired-item-handler
164 ;; Also note this item-sorter
165 msb-sort-by-directory)
166 ((eq major-mode 'Man-mode)
169 ((eq major-mode 'w3-mode)
172 ((or (memq major-mode '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
173 (memq major-mode '(mh-letter-mode
176 (memq major-mode '(gnus-summary-mode
181 gnus-browse-killed-mode)))
184 ;; Catchup for all non-file buffers
185 ((and (not buffer-file-name)
188 "Other non-file buffers (%d)")
189 ((and (string-match "/\\.[^/]*$" buffer-file-name)
193 ((memq major-mode '(c-mode c++-mode))
196 ((eq major-mode 'emacs-lisp-mode)
199 ((eq major-mode 'latex-mode)
204 "Other files (%d)")))
206 ;; msb--many-menus is obsolete
207 (defvar msb--many-menus msb--very-many-menus)
210 ;;; Customizable variables
213 (defvar msb-separator-diff 100
214 "*Non-nil means use separators.
215 The separators will appear between all menus that have a sorting key
216 that differs by this value or more.")
218 (defvar msb-files-by-directory-sort-key 0
219 "*The sort key for files sorted by directory.")
221 (defvar msb-max-menu-items 15
222 "*The maximum number of items in a menu.
223 If this variable is set to 15 for instance, then the submenu will be
224 split up in minor parts, 15 items each. If nil, there is no limit.")
226 (defvar msb-max-file-menu-items 10
227 "*The maximum number of items from different directories.
229 When the menu is of type `file by directory', this is the maximum
230 number of buffers that are clumped together from different
233 Set this to 1 if you want one menu per directory instead of clumping
236 If the value is not a number, then the value 10 is used.")
238 (defvar msb-most-recently-used-sort-key -1010
239 "*Where should the menu with the most recently used buffers be placed?")
241 (defvar msb-display-most-recently-used 15
242 "*How many buffers should be in the most-recently-used menu.
243 No buffers at all if less than 1 or nil (or any non-number).")
245 (defvar msb-most-recently-used-title "Most recently used (%d)"
246 "*The title for the most-recently-used menu.")
248 (defvar msb-horizontal-shift-function '(lambda () 0)
249 "*Function that specifies how many pixels to shift the top menu leftwards.")
251 (defvar msb-display-invisible-buffers-p nil
252 "*Show invisible buffers or not.
253 Non-nil means that the buffer menu should include buffers that have
254 names that starts with a space character.")
256 (defvar msb-item-handling-function 'msb-item-handler
257 "*The appearance of a buffer menu.
259 The default function to call for handling the appearance of a menu
260 item. It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
261 where the latter is the max length of all buffer names.
263 The function should return the string to use in the menu.
265 When the function is called, BUFFER is the current buffer. This
266 function is called for items in the variable `msb-menu-cond' that have
267 nil as ITEM-HANDLING-FUNCTION. See `msb-menu-cond' for more
270 (defvar msb-item-sort-function 'msb-sort-by-name
271 "*The order of items in a buffer menu.
273 The default function to call for handling the order of items in a menu
274 item. This function is called like a sort function. The items look
275 like (ITEM-NAME . BUFFER).
277 ITEM-NAME is the name of the item that will appear in the menu.
278 BUFFER is the buffer, this is not necessarily the current buffer.
280 Set this to nil or t if you don't want any sorting (faster).")
282 (defvar msb-files-by-directory nil
283 "*Non-nil means that files should be sorted by directory instead of
284 the groups in msb-menu-cond.")
286 (defvar msb-menu-cond msb--very-many-menus
287 "*List of criteria for splitting the mouse buffer menu.
288 The elements in the list should be of this type:
289 (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
291 When making the split, the buffers are tested one by one against the
292 CONDITION, just like a lisp cond: When hitting a true condition, the
293 other criteria are *not* tested and the buffer name will appear in the
294 menu with the menu-title corresponding to the true condition.
296 If the condition returns the symbol `multi', then the buffer will be
297 added to this menu *and* tested for other menus too. If it returns
298 `no-multi', then the buffer will only be added if it hasn't been added
301 During this test, the buffer in question is the current buffer, and
302 the test is surrounded by calls to `save-excursion' and
305 The categories are sorted by MENU-SORT-KEY. Smaller keys are on top.
306 nil means don't display this menu.
308 MENU-TITLE is really a format. If you add %d in it, the %d is
309 replaced with the number of items in that menu.
311 ITEM-HANDLING-FN, is optional. If it is supplied and is a function,
312 than it is used for displaying the items in that particular buffer
313 menu, otherwise the function pointed out by
314 `msb-item-handling-function' is used.
316 ITEM-SORT-FN, is also optional.
317 If it is not supplied, the function pointed out by
318 `msb-item-sort-function' is used.
319 If it is nil, then no sort takes place and the buffers are presented
320 in least-recently-used order.
321 If it is t, then no sort takes place and the buffers are presented in
322 most-recently-used order.
323 If it is supplied and non-nil and not t than it is used for sorting
324 the items in that particular buffer menu.
326 Note1: There should always be a `catch-all' as last element, in this
327 list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
328 Note2: A buffer menu appears only if it has at least one buffer in it.
329 Note3: If you have a CONDITION that can't be evaluated you will get an
330 error every time you do \\[msb].")
332 (defvar msb-after-load-hooks nil
333 "Hooks to be run after the msb package has been loaded.")
336 ;;; Internal variables
339 ;; Home directory for the current user
340 (defvar msb--home-dir
342 (substitute-in-file-name "$HOME")
343 ;; If $HOME isn't defined, use nil
346 ;; The last calculated menu.
347 (defvar msb--last-buffer-menu nil)
349 ;; If this is non-nil, then it is a string that describes the error.
350 (defvar msb--error nil)
353 ;;; Some example function to be used for `msb-item-handling-function'.
355 (defun msb-item-handler (buffer &optional maxbuf)
356 "Create one string item, concerning BUFFER, for the buffer menu.
359 The `*' appears only if the buffer is marked as modified.
360 The `%' appears only if the buffer is read-only.
361 Optional second argument MAXBUF is completely ignored."
362 (let ((name (buffer-name))
363 (modified (if (buffer-modified-p) "*" " "))
364 (read-only (if buffer-read-only "%" " ")))
365 (format "%s%s %s" modified read-only name)))
368 (eval-when-compile (require 'dired))
370 ;; `dired' can be called with a list of the form (directory file1 file2 ...)
371 ;; which causes `dired-directory' to be in the same form.
372 (defun msb--dired-directory ()
373 (cond ((stringp dired-directory)
374 (abbreviate-file-name (expand-file-name dired-directory)))
375 ((consp dired-directory)
376 (abbreviate-file-name (expand-file-name (car dired-directory))))
378 (error "Unknown type of `dired-directory' in buffer %s"
381 (defun msb-dired-item-handler (buffer &optional maxbuf)
382 "Create one string item, concerning a dired BUFFER, for the buffer menu.
385 The `*' appears only if the buffer is marked as modified.
386 The `%' appears only if the buffer is read-only.
387 Optional second argument MAXBUF is completely ignored."
388 (let ((name (msb--dired-directory))
389 (modified (if (buffer-modified-p) "*" " "))
390 (read-only (if buffer-read-only "%" " ")))
391 (format "%s%s %s" modified read-only name)))
393 (defun msb-alon-item-handler (buffer maxbuf)
394 "Create one string item for the buffer menu.
396 <buffer-name> *%# <file-name>
397 The `*' appears only if the buffer is marked as modified.
398 The `%' appears only if the buffer is read-only.
399 The `#' appears only version control file (SCCS/RCS)."
400 (format (format "%%%ds %%s%%s%%s %%s" maxbuf)
402 (if (buffer-modified-p) "*" " ")
403 (if buffer-read-only "%" " ")
404 (if (and (boundp 'vc-mode) vc-mode) "#" " ")
405 (or buffer-file-name "")))
408 ;;; Some example function to be used for `msb-item-sort-function'.
410 (defun msb-sort-by-name (item1 item2)
411 "Sorts the items depending on their buffer-name
412 An item look like (NAME . BUFFER)."
413 (string-lessp (buffer-name (cdr item1))
414 (buffer-name (cdr item2))))
417 (defun msb-sort-by-directory (item1 item2)
418 "Sorts the items depending on their directory. Made for dired.
419 An item look like (NAME . BUFFER)."
420 (string-lessp (save-excursion (set-buffer (cdr item1))
421 (msb--dired-directory))
422 (save-excursion (set-buffer (cdr item2))
423 (msb--dired-directory))))
428 ;;; This function can be used instead of (mouse-buffer-menu EVENT)
429 ;;; function in "mouse.el".
432 "Pop up several menus of buffers for selection with the mouse.
433 This command switches buffers in the window that you clicked on, and
436 See the function `mouse-select-buffer' and the variable
437 `msb-menu-cond' for more information about how the menus are split."
439 (let ((old-window (selected-window))
440 (window (posn-window (event-start event))))
441 (unless (framep window) (select-window window))
442 (let ((buffer (mouse-select-buffer event)))
444 (switch-to-buffer buffer)
445 (select-window old-window))))
449 ;;; Some supportive functions
451 (defun msb-invisible-buffer-p (&optional buffer)
452 "Return t if optional BUFFER is an \"invisible\" buffer.
453 If the argument is left out or nil, then the current buffer is considered."
454 (and (> (length (buffer-name buffer)) 0)
455 (eq ?\ (aref (buffer-name buffer) 0))))
457 ;; Strip one hierarchy level from the end of DIR.
458 (defun msb--strip-dir (dir)
459 (file-name-directory (directory-file-name dir)))
461 ;; Create an alist with all buffers from LIST that lies under the same
462 ;; directory will be in the same item as the directory string.
463 ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...)
464 (defun msb--init-file-alist (list)
466 ;; Make alist that looks like
467 ;; ((PATH-1 BUFFER-1) (PATH-2 BUFFER-2) ...)
472 (let ((file-name (expand-file-name (buffer-file-name buffer))))
474 (list (cons (msb--strip-dir file-name) buffer))))))
476 (function (lambda (item1 item2)
477 (string< (car item1) (car item2)))))))
478 ;; Now clump buffers together that have the same path
479 ;; Make alist that looks like
480 ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...)
488 (string= path (car item)))
489 ;; The same path as earlier: Add to current list of
491 (push (cdr item) buffers)
492 ;; This item should not be added to list
496 (let ((result (and path (cons path buffers))))
497 (setq path (car item))
498 (setq buffers (list (cdr item)))
499 ;; Add the last result the list.
500 (and result (list result)))))))
502 ;; Add the last result to the list
503 (list (cons path buffers))))))
505 ;; Format a suitable title for the menu item.
506 (defun msb--format-title (top-found-p path number-of-items)
507 (let ((new-path path))
508 (when (and msb--home-dir
509 (string-match (concat "^" msb--home-dir) path))
510 (setq new-path (concat "~/"
511 (substring path (match-end 0)))))
512 (format (if top-found-p "%s... (%d)" "%s (%d)")
513 new-path number-of-items)))
515 ;; Variables for debugging.
516 (defvar msb--choose-file-menu-list)
517 (defvar msb--choose-file-menu-arg-list)
519 ;; Choose file-menu with respect to directory for every buffer in LIST.
520 (defun msb--choose-file-menu (list)
521 (setq msb--choose-file-menu-arg-list list)
522 (let ((buffer-alist (msb--init-file-alist list))
524 (max-clumped-together (if (numberp msb-max-file-menu-items)
525 msb-max-file-menu-items
529 first rest path buffers)
530 ;; Prepare for looping over all items in buffer-alist
531 (setq first (car buffer-alist)
532 rest (cdr buffer-alist)
535 (setq msb--choose-file-menu-list (copy-list rest))
536 ;; This big loop tries to clump buffers together that have a
537 ;; similar name. Remember that buffer-alist is sorted based on the
538 ;; path for the buffers.
544 (setq item (car tmp-rest))
545 ;; Clump together the "rest"-buffers that have a path that is
546 ;; a subpath of the current one.
548 (<= (length buffers) max-clumped-together)
549 (>= (length (car item)) (length path))
550 (string= path (substring (car item) 0 (length path))))
552 (setq buffers (append buffers (cdr item))) ;nconc is faster than append
553 (setq tmp-rest (cdr tmp-rest)
554 item (car tmp-rest)))
556 ((> (length buffers) max-clumped-together)
557 ;; Oh, we failed. Too many buffers clumped together.
558 ;; Just use the original ones for the result.
559 (setq last-path (car first))
560 (push (cons (msb--format-title top-found-p
562 (length (cdr first)))
565 (setq top-found-p nil)
566 (setq first (car rest)
569 buffers (cdr first)))
571 ;; The first pass of clumping together worked out, go ahead
575 (setq first (cons path buffers)
577 ;; Now see if we can clump more buffers together if we go up
578 ;; one step in the file hierarchy.
579 (setq path (msb--strip-dir path)
582 (or (and (>= (length path) (length last-path))
584 (substring path 0 (length last-path))))
585 (and (< (length path) (length last-path))
587 (substring last-path 0 (length path))))))
588 ;; We have reached the same place in the file hierarchy as
589 ;; the last result, so we should quit at this point and
590 ;; take what we have as result.
591 (push (cons (msb--format-title top-found-p
593 (length (cdr first)))
596 (setq top-found-p nil)
597 (setq first (car rest)
600 buffers (cdr first)))))))
601 ;; Now take care of the last item.
602 (push (cons (msb--format-title top-found-p
604 (length (cdr first)))
607 (setq top-found-p nil)
608 (nreverse final-list)))
610 ;; Create a vector as:
611 ;; [BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER)
612 ;; from an element in `msb-menu-cond'. See that variable for a
613 ;; description of its elements.
614 (defun msb--create-function-info (menu-cond-elt)
615 (let* ((list-symbol (make-symbol "-msb-buffer-list"))
616 (tmp-ih (and (> (length menu-cond-elt) 3)
617 (nth 3 menu-cond-elt)))
618 (item-handler (if (and tmp-ih (fboundp tmp-ih))
620 msb-item-handling-function))
621 (tmp-s (if (> (length menu-cond-elt) 4)
622 (nth 4 menu-cond-elt)
623 msb-item-sort-function))
624 (sorter (if (or (fboundp tmp-s)
628 msb-item-sort-function)))
629 (when (< (length menu-cond-elt) 3)
630 (error "Wrong format of msb-menu-cond."))
631 (when (and (> (length menu-cond-elt) 3)
632 (not (fboundp tmp-ih)))
633 (signal 'invalid-function (list tmp-ih)))
634 (when (and (> (length menu-cond-elt) 4)
636 (not (fboundp tmp-s))
638 (signal 'invalid-function (list tmp-s)))
640 (vector list-symbol ;BUFFER-LIST-VARIABLE
641 (nth 0 menu-cond-elt) ;CONDITION
642 (nth 1 menu-cond-elt) ;SORT-KEY
643 (nth 2 menu-cond-elt) ;MENU-TITLE
644 item-handler ;ITEM-HANDLER
648 ;; This defsubst is only used in `msb--choose-menu' below. It was
649 ;; pulled out merely to make the code somewhat clearer. The indention
650 ;; level was too big.
651 (defsubst msb--collect (function-info-vector)
655 (setq function-info-list
657 across function-info-vector
659 (eval (aref fi 1))) ;Test CONDITION
660 (not (and (eq result 'no-multi)
662 (progn (when (eq result 'multi)
667 (not (eq result 'multi)))))
668 (when (and (not function-info-list)
670 (error "No catch-all in msb-menu-cond!"))
673 ;; Adds BUFFER to the menu depicted by FUNCTION-INFO
674 ;; All side-effects. Adds an element of form (BUFFER-TITLE . BUFFER)
675 ;; to the buffer-list variable in function-info.
676 (defun msb--add-to-menu (buffer function-info max-buffer-name-length)
677 (let ((list-symbol (aref function-info 0))) ;BUFFER-LIST-VARIABLE
678 ;; Here comes the hairy side-effect!
680 (cons (cons (funcall (aref function-info 4) ;ITEM-HANDLER
682 max-buffer-name-length)
684 (eval list-symbol)))))
686 ;; Selects the appropriate menu for BUFFER.
687 ;; This is all side-effects, folks!
688 ;; This should be optimized.
689 (defsubst msb--choose-menu (buffer function-info-vector max-buffer-name-length)
690 (unless (and (not msb-display-invisible-buffers-p)
691 (msb-invisible-buffer-p buffer))
695 ;; Menu found. Add to this menu
697 (lambda (function-info)
698 (msb--add-to-menu buffer function-info max-buffer-name-length)))
699 (msb--collect function-info-vector)))
700 (error (unless msb--error
703 "In msb-menu-cond, error for buffer `%s'."
704 (buffer-name buffer)))
705 (error "%s" msb--error))))))
707 ;; Return (SORT-KEY TITLE . BUFFER-LIST) or nil if the
708 ;; buffer-list is empty.
709 (defun msb--create-sort-item (function-info)
710 (let ((buffer-list (eval (aref function-info 0))))
712 (let ((sorter (aref function-info 5)) ;SORTER
713 (sort-key (aref function-info 2))) ;MENU-SORT-KEY
716 (cons (format (aref function-info 3) ;MENU-TITLE
717 (length buffer-list))
722 (nreverse buffer-list))
724 (sort buffer-list sorter))))))))))
726 ;; Returns a list on the form ((TITLE . BUFFER-LIST)) for
727 ;; the most recently used buffers.
728 (defun msb--most-recently-used-menu (max-buffer-name-length)
729 (when (and (numberp msb-display-most-recently-used)
730 (> msb-display-most-recently-used 0))
731 (let* ((buffers (cdr (buffer-list)))
734 for buffer in buffers
737 (and (not (msb-invisible-buffer-p))
738 (not (eq major-mode 'dired-mode))))
739 collect (save-excursion
741 (cons (funcall msb-item-handling-function
743 max-buffer-name-length)
746 until (>= n msb-display-most-recently-used))))
747 (cons (if (stringp msb-most-recently-used-title)
748 (format msb-most-recently-used-title
749 (length most-recently-used))
750 (signal 'wrong-type-argument (list msb-most-recently-used-title)))
751 most-recently-used))))
753 (defun msb--create-buffer-menu-2 ()
754 (let ((max-buffer-name-length 0)
756 function-info-vector)
757 ;; Calculate the longest buffer name.
761 (if (or msb-display-invisible-buffers-p
762 (not (msb-invisible-buffer-p)))
763 (setq max-buffer-name-length
764 (max max-buffer-name-length
765 (length (buffer-name buffer)))))))
767 ;; Make a list with elements of type
768 ;; (BUFFER-LIST-VARIABLE
774 ;; Uses "function-global" variables:
775 ;; function-info-vector
776 (setq function-info-vector
777 (apply (function vector)
778 (mapcar (function msb--create-function-info)
780 ;; Split the buffer-list into several lists; one list for each
781 ;; criteria. This is the most critical part with respect to time.
782 (mapc (function (lambda (buffer)
783 (cond ((and msb-files-by-directory
784 (buffer-file-name buffer))
785 (push buffer file-buffers))
787 (msb--choose-menu buffer
789 max-buffer-name-length)))))
794 (lambda (buffer-list)
795 (cons msb-files-by-directory-sort-key
796 (cons (car buffer-list)
800 (cons (save-excursion
802 (funcall msb-item-handling-function
804 max-buffer-name-length))
808 (lambda (item1 item2)
809 (string< (car item1) (car item2)))))))))
810 (msb--choose-file-menu file-buffers))))
811 ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
814 (msb--most-recently-used-menu max-buffer-name-length))
815 (others (nconc file-buffers
817 across function-info-vector
818 for value = (msb--create-sort-item elt)
819 if value collect value))))
821 (mapcar 'cdr ;Remove the SORT-KEY
822 ;; Sort the menus - not the items.
825 ;; Get a list of (SORT-KEY TITLE . BUFFER-LIST)
826 ;; Also sorts the items within the menus.
827 (if (cdr most-recently-used)
829 ;; Add most recent used buffers
830 (cons msb-most-recently-used-sort-key
834 (function (lambda (elt1 elt2)
835 (< (car elt1) (car elt2))))))))
836 ;; Now make it a keymap menu
838 '(keymap "Select Buffer")
839 (msb--make-keymap-menu menu)
840 (when msb-separator-diff
841 (list (list 'separator "--")))
844 (if msb-files-by-directory
846 "*Files by directory*")
847 'msb--toggle-menu-type)))))))
849 (defun msb--create-buffer-menu ()
852 (msb--create-buffer-menu-2))))
855 ;;; Multi purpose function for selecting a buffer with the mouse.
857 (defun msb--toggle-menu-type ()
859 (setq msb-files-by-directory (not msb-files-by-directory))
860 ;; This gets a warning, but it is correct,
861 ;; because this file redefines menu-bar-update-buffers.
862 (menu-bar-update-buffers t))
864 (defun mouse-select-buffer (event)
865 "Pop up several menus of buffers, for selection with the mouse.
866 Returns the selected buffer or nil if no buffer is selected.
868 The way the buffers are split is conveniently handled with the
869 variable `msb-menu-cond'."
870 ;; Popup the menu and return the selected buffer.
872 (not msb--last-buffer-menu)
873 (not (fboundp 'frame-or-buffer-changed-p))
874 (frame-or-buffer-changed-p))
875 (setq msb--error nil)
876 (setq msb--last-buffer-menu (msb--create-buffer-menu)))
877 (let ((position event)
879 (when (and (fboundp 'posn-x-y)
880 (fboundp 'posn-window))
881 (let ((posX (car (posn-x-y (event-start event))))
882 (posY (cdr (posn-x-y (event-start event))))
883 (posWind (posn-window (event-start event))))
885 (setq posX (- posX (funcall msb-horizontal-shift-function))
886 position (list (list posX posY) posWind))))
887 ;; This `sit-for' magically makes the menu stay up if the mouse
888 ;; button is released within 0.1 second.
891 (setq choice (x-popup-menu position msb--last-buffer-menu))
893 ((eq (car choice) 'toggle)
894 ;; Bring up the menu again with type toggled.
895 (msb--toggle-menu-type)
896 (mouse-select-buffer event))
897 ((and (numberp (car choice))
899 (let ((msb--last-buffer-menu (nthcdr 3 (assq (car choice) msb--last-buffer-menu))))
900 (mouse-select-buffer event)))
901 ((while (numberp (car choice))
902 (setq choice (cdr choice))))
903 ((and (stringp (car choice))
909 (error "Unknown form for buffer: %s" choice)))))
912 (defun msb--add-separators (sorted-list)
914 ((or (not msb-separator-diff)
915 (not (numberp msb-separator-diff)))
918 (let ((last-key nil))
923 ((and msb-separator-diff
925 (> (- (car item) last-key)
927 (setq last-key (car item))
928 (list (cons last-key 'separator)
931 (setq last-key (car item))
935 (defun msb--split-menus-2 (list mcount result)
937 ((> (length list) msb-max-menu-items)
941 (while (< count msb-max-menu-items)
942 (push (pop list) tmp-list)
944 (setq tmp-list (nreverse tmp-list))
945 (setq sub-name (concat (car (car tmp-list)) "..."))
946 (push (nconc (list mcount sub-name
950 (msb--split-menus-2 list (1+ mcount) result))
955 (setq sub-name (concat (car (car list)) "..."))
956 (push (nconc (list mcount sub-name
962 (defun msb--split-menus (list)
963 (msb--split-menus-2 list 0 nil))
966 (defun msb--make-keymap-menu (raw-menu)
967 (let ((end (cons '(nil) 'menu-bar-select-buffer))
973 ((eq 'separator sub-menu)
974 (list 'separator "--"))
976 (let ((buffers (mapcar (function
978 (let ((string (car item))
980 (cons (buffer-name buffer)
981 (cons string end)))))
983 (nconc (list (incf mcount) (car sub-menu)
984 'keymap (car sub-menu))
985 (msb--split-menus buffers)))))))
988 (defun menu-bar-update-buffers (&optional arg)
989 ;; If user discards the Buffers item, play along.
990 (when (and (lookup-key (current-global-map) [menu-bar buffer])
991 (or (not (fboundp 'frame-or-buffer-changed-p))
992 (frame-or-buffer-changed-p)
994 (let ((frames (frame-list))
995 buffers-menu frames-menu)
996 ;; Make the menu of buffers proper.
997 (setq msb--last-buffer-menu (msb--create-buffer-menu))
998 (setq buffers-menu msb--last-buffer-menu)
999 ;; Make a Frames menu if we have more than one frame.
1001 (let* ((frame-length (length frames))
1002 (f-title (format "Frames (%d)" frame-length)))
1003 ;; List only the N most recently selected frames
1004 (when (and (integerp msb-max-menu-items)
1005 (> msb-max-menu-items 1)
1006 (> frame-length msb-max-menu-items))
1007 (setcdr (nthcdr msb-max-menu-items frames) nil))
1010 (list 'frame f-title '(nil) 'keymap f-title)
1017 (frame-parameters frame)))
1019 'menu-bar-select-frame)))
1021 (define-key (current-global-map) [menu-bar buffer]
1023 (if (and buffers-menu frames-menu)
1024 ;; Combine Frame and Buffers menus with separator between
1025 (nconc (list 'keymap "Buffers and Frames" frames-menu
1026 (and msb-separator-diff '(separator "--")))
1027 (cddr buffers-menu))
1028 (or buffers-menu 'undefined)))))))
1030 (when (and (boundp 'menu-bar-update-hook)
1031 (not (fboundp 'frame-or-buffer-changed-p)))
1032 (defvar msb--buffer-count 0)
1033 (defun frame-or-buffer-changed-p ()
1034 (let ((count (length (buffer-list))))
1035 (when (/= count msb--buffer-count)
1036 (setq msb--buffer-count count)
1039 (unless (or (not (boundp 'menu-bar-update-hook))
1040 (memq 'menu-bar-update-buffers menu-bar-update-hook))
1041 (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers))
1043 (and (fboundp 'mouse-buffer-menu)
1044 (substitute-key-definition 'mouse-buffer-menu 'msb (current-global-map)))
1047 (eval-after-load 'msb (run-hooks 'msb-after-load-hooks))
1049 ;;; msb.el ends here