(xscheme-control-g-synchronization-p): Doc fix.
[bpt/emacs.git] / lisp / msb.el
index 8cee0b7..ae9b738 100644 (file)
@@ -1,10 +1,9 @@
 ;;; msb.el --- Customizable buffer-selection with multiple menus.
-;; Copyright (C) 1993, 1994 Lars Lindberg <Lars.Lindberg@sypro.cap.se>
+;; Copyright (C) 1993, 1994, 1995 Lars Lindberg <Lars.Lindberg@sypro.cap.se>
 ;;
 ;; Author: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
 ;; Created: 8 Oct 1993
-;; $Revision: 3.21 $
-;; $Date: 1994/12/22 07:58:27 $
+;; Lindberg's last update version: 3.31
 ;; Keywords: mouse buffer menu 
 ;;
 ;; This program is free software; you can redistribute it and/or modify
 ;; along with this program; if not, write to the Free Software
 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-;; LCD Archive Entry:
-;; msb|Lars Lindberg|Lars.Lindberg@sypro.cap.se|
-;; Choose buffer with the mouse.
-;; $Date: 1994/12/22 07:58:27 $|$Revision: 3.21 $|~/packages/msb.el.Z|
-
 ;;; Commentary:
 ;;
 ;; Purpose of this package:
 ;;   2. Make a better mouse-buffer-menu.
 ;;
 ;; Installation:
-;;   (require 'msb)
-;;   Note! You now use msb instead of mouse-buffer-menu.
-;;
-;;   Now try c-mouse-down-1 (Press <CTRL> and mouse button 1 at the
-;;   same time).
+
+;;   1. Byte compile msb first.  It uses things in the cl package that
+;;      are slow if not compiled, but blazingly fast when compiled.  I
+;;      have also had one report that said that msb malfunctioned when
+;;      not compiled.
+;;   2. (require 'msb)
+;;      Note! You now use msb instead of mouse-buffer-menu.
+;;   3. Now try the menu bar Buffers menu.
 ;;
 ;; Customization:
-;;   Look at the variable 'msb-menu-cond' for deciding what menus you
-;;   want. It's not that hard to customize, despite my not-so-good
-;;   doc-string. Feel free to send me a better doc-string.
+;;   Look at the variable `msb-menu-cond' for deciding what menus you
+;;   want.  It's not that hard to customize, despite my not-so-good
+;;   doc-string.  Feel free to send me a better doc-string.
 ;;   There are some constants for you to try here:
 ;;   msb--few-menus
 ;;   msb--very-many-menus (default)
 ;;   
-;;   Look at the variable 'msb-item-handling-function' for customization
-;;   of the appearance of every menu item. Try for instance setting
-;;   it to 'msb-alon-item-handler.
+;;   Look at the variable `msb-item-handling-function' for customization
+;;   of the appearance of every menu item.  Try for instance setting
+;;   it to `msb-alon-item-handler'.
 ;;   
-;;   Look at the variable 'msb-item-sort-function' for customization
-;;   of sorting the menus. Set it to t for instance, which means no
+;;   Look at the variable `msb-item-sort-function' for customization
+;;   of sorting the menus.  Set it to t for instance, which means no
 ;;   sorting - you will get latest used buffer first.
 ;;
-;;   Also check out the variable 'msb-display-invisible-buffers-p'
+;;   Also check out the variable `msb-display-invisible-buffers-p'.
 
 ;; Known bugs:
-;; - `msb' does not work on a non-X-toolkit Emacs.
+;; - Files-by-directory
+;;   + No possibility to show client/changed buffers separately.
+;;   + All file buffers only appear in in a file sub-menu, they will
+;;     for instance not appear in the Mail sub-menu.
+
 ;; Future enhancements:
-;; - [Mattes] had a suggestion about sorting files by extension.
-;;   I (Lars Lindberg) think this case could be solved if msb.el was
-;;   rewritten to handle more dynamic splitting.  It's now completely
-;;   static, depending on the menu-cond.  If the splitting could also
-;;   be done by a user-defined function a lot of cases would be
-;;   solved.
-;; - [Jim] suggested that the Frame menu became a part of the buffer menu.
-
-;;; Change Log
-;; 3.21 22/12-94
-;;     Fixed bug that occured in non X-toolkit versions of Emacs.
-;;     [Chalupsky] pointed out that a global `save-match-data' is
-;;     necessary.
-;;     Bug found.  Thanks [kifer].
-;; 3.16 20/12-94
-;;     Added separators to the menu.  New variable `msb-separator-diff'.
-;;     New variable `msb-files-by-directory-sort-key'.
-;;     Removed `msb--many-menus.'
-;;     Fixed bugs.
-;; 3.13 20/12-94
-;;     Menu fix for non-X-toolkit Emacsen and new "process"
-;;     menu. Thanks [jim].
-;;      Bug for 'files-by-type'.
-;;     Restored the call to `msb-after-load-hooks'.  Thanks [larry].
-;;      Major fixes by [Ake].
-;;      Menu-bar buffer-menu now has menu-toggle at top level.
-;;  3.6 16/12-94
-;;     Added variable `msb-max-file-menu-items'.
-;;     Removed a large part of the change log.
-;;     Found bug.
-;;  3.3 16/12-94
-;;      Found bugs.
-;;  3.1 16/12-94
-;;      Now has two types of menus - "files by directory" and "files
-;;      by type".
-;;      Added variable `msb-files-by-directory'.
-;;      Fixed a number of bugs for older versions.
-;;  2.11 16/12-94
-;;      Added 'no-multi to msb-menu-cond.
-;;      Added possibility to shift the menu leftwards. Thanks [kifer].
-;;  2.8 15/12-94
-;;      Now aware of earlier versions of Emacs that doesn't have the
-;;      function `frame-or-buffer-changed-p' or the variable
-;;      `menu-bar-update-hook'.  Thanks [will].
-;;  2.7 14/12-94
-;;     Better installation.
-;;  2.6 14/12-94
-;;     Now only makes up the menu when necessary.
-;;     Added menu-bar support.
-;;     Now handles errors in msb-menu-cond better. Thanks [jaalto].
-;;      Added MH-awareness. Thanks [kifer].
-;;      Added autoload statements.
-;;  2.3 8/12-94
-;;     Now uses RCS version numbering for msb.el version number.
-;;     Submitted this to LCD.
 
 ;;; Thanks goes to
-;;  [msb] - Mark Brader <msb@sq.com>
-;;  [Chalupsky] - Hans Chalupsky <hans@cs.Buffalo.EDU>
-;;  [jim] - Jim Berry <m1jhb00@FRB.GOV>
-;;  [larry] - Larry Rosenberg <ljr@ictv.com>
-;;  [will] - Will Henney <will@astroscu.unam.mx>
-;;  [jaalto] - Jari Aalto <jaalto@tre.tele.nokia.fi>
-;;  [kifer] - Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
-;;  [Gael] - Gael Marziou <gael@gnlab030.grenoble.hp.com>
-;;  [Gillespie] - Dave Gillespie <daveg@thymus.synaptics.com>
-;;  [Alon] - Alon Albert <alon@milcse.rtsg.mot.com>
-;;  [KevinB] - Kevin Broadey, <KevinB@bartley.demon.co.uk>
-;;  [Ake] - Ake Stenhof <ake@cadpoint.se>
-;;  [RMS] - Richard Stallman <rms@gnu.ai.mit.edu>
-;;  [Fisk] - Steve Fisk <fisk@medved.bowdoin.edu>
+;;  Mark Brader <msb@sq.com>
+;;  Jim Berry <m1jhb00@FRB.GOV>
+;;  Hans Chalupsky <hans@cs.Buffalo.EDU>
+;;  Larry Rosenberg <ljr@ictv.com>
+;;  Will Henney <will@astroscu.unam.mx>
+;;  Jari Aalto <jaalto@tre.tele.nokia.fi>
+;;  Michael Kifer <kifer@sbkifer.cs.sunysb.edu>
+;;  Gael Marziou <gael@gnlab030.grenoble.hp.com>
+;;  Dave Gillespie <daveg@thymus.synaptics.com>
+;;  Alon Albert <alon@milcse.rtsg.mot.com>
+;;  Kevin Broadey, <KevinB@bartley.demon.co.uk>
+;;  Ake Stenhof <ake@cadpoint.se>
+;;  Richard Stallman <rms@gnu.ai.mit.edu>
+;;  Steve Fisk <fisk@medved.bowdoin.edu>
 
 ;;; Code:
 
 (require 'cl)
 
 ;;;
-;;; Some example constants to be used for 'msb-menu-cond'. See that
-;;; variable for more information. Please note that if the condition
-;;; returns 'multi, then the buffer can appear in several menus.
+;;; Some example constants to be used for `msb-menu-cond'.  See that
+;;; variable for more information.  Please note that if the condition
+;;; returns `multi', then the buffer can appear in several menus.
 ;;;
 (defconst msb--few-menus
   '(((and (boundp 'server-buffer-clients)
     ((eq major-mode 'w3-mode)
      4020
      "WWW (%d)")
-    ((or (memq major-mode '(rmail-mode vm-summary-mode vm-mode mail-mode))
+    ((or (memq major-mode '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
         (memq major-mode '(mh-letter-mode
                            mh-show-mode
                            mh-folder-mode))     
     ((eq major-mode 'w3-mode)
      4020
      "WWW (%d)")
-    ((or (memq major-mode '(rmail-mode vm-summary-mode vm-mode mail-mode))
+    ((or (memq major-mode '(rmail-mode rmail-edit-mode vm-summary-mode vm-mode mail-mode))
         (memq major-mode '(mh-letter-mode
                            mh-show-mode
                            mh-folder-mode))     
@@ -267,28 +213,29 @@ The separators will appear between all menus that have a sorting key that differ
 (defvar msb-files-by-directory-sort-key 0
   "*The sort key for files sorted by directory")
 
-(defvar msb-max-menu-items 25
+(defvar msb-max-menu-items 15
   "*The maximum number of items in a menu.
-If this variable is set to 15 for instance, then the 15 latest used
-buffer that fits in a certain submenu will appear in that submenu.
+If this variable is set to 15 for instance, then the submenu will be split up in minor parts, 15 items each.
 Nil means no limit.")
 
 (defvar msb-max-file-menu-items 10
   "*The maximum number of items from different directories.
 
-When the menu is of type 'file by directory', this is the maximum
-number of buffers that are clumped togehter from different
+When the menu is of type `file by directory', this is the maximum
+number of buffers that are clumped together from different
 directories.
 
+Set this to 1 if you want one menu per directory instead of clumping
+them together.
+
 If the value is not a number, then the value 10 is used.")
 
 (defvar msb-most-recently-used-sort-key -1010
   "*Where should the menu with the most recently used buffers be placed?")
 
-(defvar msb-display-most-recently-used t
+(defvar msb-display-most-recently-used 15
   "*How many buffers should be in the most-recently-used menu.
-No buffers at all if less than 1 or nil.
-T means use the value of `msb-max-menu-items' in the way it is defined.")
+ No buffers at all if less than 1 or nil (or any non-number).")
 
 (defvar msb-most-recently-used-title "Most recently used (%d)"
   "*The title for the most-recently-used menu.")
@@ -306,17 +253,20 @@ names that starts with a space character.")
   "*The appearance of a buffer menu.
 
 The default function to call for handling the appearance of a menu
-item. It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
+item.  It should take to arguments, BUFFER and MAX-BUFFER-NAME-LENGTH,
 where the latter is the max length of all buffer names.
+
+The function should return the string to use in the menu.
+
 When the function is called, BUFFER is the current buffer.
-This function is called for items in the variable 'msb-menu-cond' that
-have nil as ITEM-HANDLING-FUNCTION. See 'msb-menu-cond' for more
+This function is called for items in the variable `msb-menu-cond' that
+have nil as ITEM-HANDLING-FUNCTION.  See `msb-menu-cond' for more
 information.")
 
 (defvar msb-item-sort-function 'msb-sort-by-name
   "*The order of items in a buffer menu.
 The default function to call for handling the order of items in a menu
-item. This function is called like a sort function. The items
+item.  This function is called like a sort function.  The items
 look like (ITEM-NAME . BUFFER).
 ITEM-NAME is the name of the item that will appear in the menu.
 BUFFER is the buffer, this is not necessarily the current buffer.
@@ -328,38 +278,38 @@ Set this to nil or t if you don't want any sorting (faster).")
 the groups in msb-menu-cond.")
 
 (defvar msb-menu-cond msb--very-many-menus
-  "*List of criterias for splitting the mouse buffer menu.
+  "*List of criteria for splitting the mouse buffer menu.
 The elements in the list should be of this type:
  (CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLING-FN ITEM-SORT-FN).
 
 When making the split, the buffers are tested one by one against the
 CONDITION, just like a lisp cond: When hitting a true condition, the
-other criterias are *not* tested and the buffer name will appear in
+other criteria are *not* tested and the buffer name will appear in
 the menu with the menu-title corresponding to the true condition.
 
-If the condition returns the symbol 'multi, then the buffer will be
+If the condition returns the symbol `multi', then the buffer will be
 added to this menu *and* tested for other menus too.  If it returns
-'no-multi, then the buffer will only be added if it hasn't been added
+`no-multi', then the buffer will only be added if it hasn't been added
 to any other menu.
 
 During this test, the buffer in question is the current buffer, and
 the test is surrounded by calls to `save-excursion' and
-`save-match-data'
+`save-match-data'.
 
-The categories are sorted by MENU-SORT-KEY. Smaller keys are on
-top. nil means don't display this menu.
+The categories are sorted by MENU-SORT-KEY.  Smaller keys are on
+top.  nil means don't display this menu.
 
-MENU-TITLE is really a format. If you add %d in it, the %d is replaced
+MENU-TITLE is really a format.  If you add %d in it, the %d is replaced
 with the number of items in that menu.
 
-ITEM-HANDLING-FN, is optional. If it is supplied and is a
+ITEM-HANDLING-FN, is optional.  If it is supplied and is a
 function, than it is used for displaying the items in that particular
 buffer menu, otherwise the function pointed out by
-'msb-item-handling-function' is used.
+`msb-item-handling-function' is used.
 
 ITEM-SORT-FN, is also optional.
 If it is not supplied, the function pointed out by
-'msb-item-sort-function' is used.
+`msb-item-sort-function' is used.
 If it is nil, then no sort takes place and the buffers are presented
 in least-recently-used order.
 If it is t, then no sort takes place and the buffers are presented in
@@ -367,8 +317,8 @@ most-recently-used order.
 If it is supplied and non-nil and not t than it is used for sorting
 the items in that particular buffer menu.
 
-Note1: There should always be a 'catch-all' as last element,
-in this list. That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
+Note1: There should always be a `catch-all' as last element,
+in this list.  That is an element like (t TITLE ITEM-HANDLING-FUNCTION).
 Note2: A buffer menu appears only if it has at least one buffer in it.
 Note3: If you have a CONDITION that can't be evaluated you will get an
 error every time you do \\[msb].")
@@ -387,14 +337,14 @@ error every time you do \\[msb].")
 (defvar msb--error nil)
 
 ;;;
-;;; Some example function to be used for 'msb-item-sort-function'.
+;;; Some example function to be used for `msb-item-handling-function'.
 ;;;
 (defun msb-item-handler (buffer &optional maxbuf)
   "Create one string item, concerning BUFFER, for the buffer menu.
 The item looks like:
 *% <buffer-name>
-The '*' appears only if the buffer is marked as modified.
-The '%' appears only if the buffer is read-only.
+The `*' appears only if the buffer is marked as modified.
+The `%' appears only if the buffer is read-only.
 Optional second argument MAXBUF is completely ignored."
   (let ((name (buffer-name))
        (modified (if (buffer-modified-p) "*" " "))
@@ -404,23 +354,23 @@ Optional second argument MAXBUF is completely ignored."
 
 (eval-when-compile (require 'dired))
 
-;; 'dired' can be called with a list of the form (directory file1 file2 ...)
-;; which causes 'dired-directory' to be in the same form.
+;; `dired' can be called with a list of the form (directory file1 file2 ...)
+;; which causes `dired-directory' to be in the same form.
 (defun msb--dired-directory ()
   (cond ((stringp dired-directory)
         (abbreviate-file-name (expand-file-name dired-directory)))
        ((consp dired-directory)
         (abbreviate-file-name (expand-file-name (car dired-directory))))
        (t
-        (error "Unknown type of 'dired-directory' in buffer %s"
+        (error "Unknown type of `dired-directory' in buffer %s"
                (buffer-name)))))
 
 (defun msb-dired-item-handler (buffer &optional maxbuf)
   "Create one string item, concerning a dired BUFFER, for the buffer menu.
 The item looks like:
 *% <buffer-name>
-The '*' appears only if the buffer is marked as modified.
-The '%' appears only if the buffer is read-only.
+The `*' appears only if the buffer is marked as modified.
+The `%' appears only if the buffer is read-only.
 Optional second argument MAXBUF is completely ignored."
   (let ((name (msb--dired-directory))
        (modified (if (buffer-modified-p) "*" " "))
@@ -431,9 +381,9 @@ Optional second argument MAXBUF is completely ignored."
   "Create one string item for the buffer menu.
 The item looks like:
 <buffer-name> *%# <file-name>
-The '*' appears only if the buffer is marked as modified.
-The '%' appears only if the buffer is read-only.
-The '#' appears only version control file (SCCS/RCS)."
+The `*' appears only if the buffer is marked as modified.
+The `%' appears only if the buffer is read-only.
+The `#' appears only version control file (SCCS/RCS)."
   (format (format "%%%ds  %%s%%s%%s  %%s" maxbuf)
           (buffer-name buffer)
           (if (buffer-modified-p) "*" " ")
@@ -442,7 +392,7 @@ The '#' appears only version control file (SCCS/RCS)."
           (or buffer-file-name "")))
 
 ;;;
-;;; Some example function to be used for 'msb-item-handling-function'.
+;;; Some example function to be used for `msb-item-sort-function'.
 ;;;
 (defun msb-sort-by-name (item1 item2)
   "Sorts the items depending on their buffer-name
@@ -452,7 +402,7 @@ An item look like (NAME . BUFFER)."
 
 
 (defun msb-sort-by-directory (item1 item2)
-  "Sorts the items depending on their directory. Made for dired.
+  "Sorts the items depending on their directory.  Made for dired.
 An item look like (NAME . BUFFER)."
   (string-lessp (save-excursion (set-buffer (cdr item1)) (msb--dired-directory))
                (save-excursion (set-buffer (cdr item2)) (msb--dired-directory))))
@@ -468,15 +418,16 @@ An item look like (NAME . BUFFER)."
 This command switches buffers in the window that you clicked on, and
 selects that window.
 
-See the function 'mouse-select-buffer' and the variable
-'msb-menu-cond' for more information about how the menus are split."
+See the function `mouse-select-buffer' and the variable
+`msb-menu-cond' for more information about how the menus are split."
   (interactive "e")
-  (let ((buffer (mouse-select-buffer event))
+  (let ((old-window (selected-window))
        (window (posn-window (event-start event))))
-    (cond
-     (buffer
-      (or (framep window) (select-window window))
-      (switch-to-buffer (car (cdr buffer))))))
+    (unless (framep window) (select-window window))
+    (let ((buffer (mouse-select-buffer event)))
+      (if buffer
+         (switch-to-buffer buffer)
+       (select-window old-window))))
   nil)
 
 ;;;
@@ -488,7 +439,7 @@ If the argument is left out or nil, then the current buffer is considered."
   (and (> (length (buffer-name buffer)) 0)
        (eq ?\ (aref (buffer-name buffer) 0))))
 
-;; Strip one hierarcy level from the end of PATH.
+;; Strip one hierarchy level from the end of PATH.
 (defun msb--strip-path (path)
   (save-match-data
     (if (string-match "\\(.+\\)/[^/]+$" path)
@@ -497,7 +448,7 @@ If the argument is left out or nil, then the current buffer is considered."
 
 ;; Create an alist with all buffers from LIST that lies under the same
 ;; directory will be in the same item as the directory string as
-;;'((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...)
+;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K BUFFER-K+1...)) ...)
 (defun msb--init-file-alist (list)
   (let ((buffer-alist
         (sort (mapcan
@@ -510,7 +461,7 @@ If the argument is left out or nil, then the current buffer is considered."
               (function (lambda (item1 item2)
                           (string< (car item1) (car item2)))))))
     ;; Make alist that looks like
-    ;;'((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...)
+    ;; ((PATH1 . (BUFFER-1 BUFFER-2 ...)) (PATH2 . (BUFFER-K)) ...)
     (let ((path nil)
          (buffers nil)
          (result nil))
@@ -519,8 +470,6 @@ If the argument is left out or nil, then the current buffer is considered."
               (lambda (item)
                 (cond
                  ((and path
-                       msb-max-menu-items
-                       (< (length buffers) msb-max-menu-items)
                        (string= path (car item)))
                   (push (cdr item) buffers)
                   nil)
@@ -563,10 +512,14 @@ If the argument is left out or nil, then the current buffer is considered."
        (cond
         ((> (length buffers) max-clumped-together)
          (setq last-path (car first))
-         (when top-found-p
-           (setq first (cons (concat (car first) "/...")
-                             (cdr first)))
-           (setq top-found-p nil))
+         (setq first
+               (cons (format (if top-found-p
+                                 "%s/... (%d)"
+                               "%s (%d)")
+                             (car first)
+                             (length (cdr first)))
+                     (cdr first)))
+         (setq top-found-p nil)
          (push first final-list)
          (setq first (car rest)
                rest (cdr rest))
@@ -587,29 +540,34 @@ If the argument is left out or nil, then the current buffer is considered."
                              (string= path
                                       (substring last-path 0 (length path))))))
                         
-           (when top-found-p
-             (setq first (cons (concat (car first) "/...")
-                               (cdr first)))
-             (setq top-found-p nil))
+           (setq first
+                 (cons (format (if top-found-p
+                                   "%s/... (%d)"
+                                 "%s (%d)")
+                               (car first)
+                               (length (cdr first)))
+                       (cdr first)))
+           (setq top-found-p nil)
            (push first final-list)
            (setq first (car rest)
                  rest (cdr rest))
            (setq path (car first)
                buffers (cdr first)))))))
-    (when top-found-p
-      (setq first (cons (concat (car first)
-                               (if (string-match "/$" (car first))
-                                   "..."
-                                 "/..."))
-                       (cdr first)))
-      (setq top-found-p nil))
+    (setq first
+         (cons (format (if top-found-p
+                           "%s/... (%d)"
+                         "%s (%d)")
+                       (car first)
+                       (length (cdr first)))
+               (cdr first)))
+    (setq top-found-p nil)
     (push first final-list)
     (nreverse final-list)))
 
 ;; Create a vector as:
 ;; [BUFFER-LIST-VARIABLE CONDITION MENU-SORT-KEY MENU-TITLE ITEM-HANDLER SORTER)
-;; from an element in 'msb-menu-cond'. See that variable for a
-;; description of it's elements.
+;; from an element in `msb-menu-cond'.  See that variable for a
+;; description of its elements.
 (defun msb--create-function-info (menu-cond-elt)
   (let* ((list-symbol (make-symbol "-msb-buffer-list"))
         (tmp-ih (and (> (length menu-cond-elt) 3)
@@ -622,7 +580,7 @@ If the argument is left out or nil, then the current buffer is considered."
                  msb-item-sort-function))
         (sorter (if (or (fboundp tmp-s)
                         (null tmp-s)
-                        (eq tmp-s 't))
+                        (eq tmp-s t))
                    tmp-s
                   msb-item-sort-function)))
     (when (< (length menu-cond-elt) 3)
@@ -633,9 +591,9 @@ If the argument is left out or nil, then the current buffer is considered."
     (when (and (> (length menu-cond-elt) 4)
               tmp-s
               (not (fboundp tmp-s))
-              (not (eq tmp-s 't)))
+              (not (eq tmp-s t)))
       (signal 'invalid-function (list tmp-s)))
-    (set list-symbol '())
+    (set list-symbol ())
     (vector list-symbol                        ;BUFFER-LIST-VARIABLE
            (nth 0 menu-cond-elt)       ;CONDITION
            (nth 1 menu-cond-elt)       ;SORT-KEY
@@ -645,7 +603,7 @@ If the argument is left out or nil, then the current buffer is considered."
     ))
 
 ;; This defsubst is only used in `msb--choose-menu' below.  It was
-;; pulled out merely to make the code somewhat clearer. The indention
+;; pulled out merely to make the code somewhat clearer.  The indention
 ;; level was too big.
 (defsubst msb--collect (function-info-vector)
   (let ((result nil)
@@ -660,10 +618,7 @@ If the argument is left out or nil, then the current buffer is considered."
                                  multi-flag))
                        (progn (when (eq result 'multi)
                                 (setq multi-flag t))
-                              t)
-                       (or (not msb-max-menu-items)
-                           (< (length (eval (aref fi 0)))
-                              msb-max-menu-items)))
+                              t))
                collect fi
                until (and result
                           (not (eq result 'multi)))))
@@ -673,7 +628,7 @@ If the argument is left out or nil, then the current buffer is considered."
     function-info-list))
 
 ;; Adds BUFFER to the menu depicted by FUNCTION-INFO
-;; All side-effects.  Adds an element of type '(BUFFER-TITLE . BUFFER)
+;; All side-effects.  Adds an element of form (BUFFER-TITLE . BUFFER)
 ;; to the buffer-list variable in function-info.
 (defun msb--add-to-menu (buffer function-info max-buffer-name-length)
   (let ((list-symbol (aref function-info 0))) ;BUFFER-LIST-VARIABLE
@@ -694,7 +649,7 @@ If the argument is left out or nil, then the current buffer is considered."
     (condition-case nil
        (save-excursion
          (set-buffer buffer)
-         ;; Menu found. Add to this menu
+         ;; Menu found.  Add to this menu
          (mapc (function
                 (lambda (function-info)
                   (msb--add-to-menu buffer function-info max-buffer-name-length)))
@@ -702,7 +657,7 @@ If the argument is left out or nil, then the current buffer is considered."
       (error (unless msb--error
               (setq msb--error
                     (format
-                     "Variable `msb-menu-cond': Error for buffer \"%s\"."
+                     "In msb-menu-cond, error for buffer `%s'."
                      (buffer-name buffer)))
               (error msb--error))))))
 
@@ -720,7 +675,7 @@ If the argument is left out or nil, then the current buffer is considered."
                      (cond
                       ((null sorter)
                        buffer-list)
-                      ((eq sorter 't)
+                      ((eq sorter t)
                        (nreverse buffer-list))
                       (t
                        (sort buffer-list sorter))))))))))
@@ -728,17 +683,12 @@ If the argument is left out or nil, then the current buffer is considered."
 ;; Returns a list on the form ((TITLE . BUFFER-LIST)) for
 ;; the most recently used buffers.
 (defun msb--most-recently-used-menu (max-buffer-name-length)
-  (when (and msb-display-most-recently-used
-            (or (not (numberp msb-display-most-recently-used))
-                (> msb-display-most-recently-used 0)))
-    (let* ((max-in-menu
-           (if (numberp msb-display-most-recently-used)
-               msb-display-most-recently-used
-             msb-max-menu-items))
-
+  (when (and (numberp msb-display-most-recently-used)
+            (> msb-display-most-recently-used 0))
+    (let* ((buffers (cdr (buffer-list)))
           (most-recently-used
            (loop with n = 0
-                 for buffer in (cdr (buffer-list))
+                 for buffer in buffers
                  if (save-excursion
                       (set-buffer buffer)
                       (and (not (msb-invisible-buffer-p))
@@ -750,7 +700,7 @@ If the argument is left out or nil, then the current buffer is considered."
                                           max-buffer-name-length)
                                  buffer))
                  and do (incf n)
-                 until (and max-in-menu (>= n max-in-menu)))))
+                 until (>= n msb-display-most-recently-used))))
       (cons (if (stringp msb-most-recently-used-title)
                (format msb-most-recently-used-title
                        (length most-recently-used))
@@ -785,7 +735,7 @@ If the argument is left out or nil, then the current buffer is considered."
                 (mapcar (function msb--create-function-info)
                         msb-menu-cond)))
     ;; Split the buffer-list into several lists; one list for each
-    ;; criteria. This is the most critical part with respect to time.
+    ;; criteria.  This is the most critical part with respect to time.
     (mapc (function (lambda (buffer)
                      (cond ((and msb-files-by-directory
                                  (buffer-file-name buffer))
@@ -804,7 +754,11 @@ If the argument is left out or nil, then the current buffer is considered."
                                   (sort
                                    (mapcar (function
                                             (lambda (buffer)
-                                              (cons (buffer-name buffer)
+                                              (cons (save-excursion
+                                                      (set-buffer buffer)
+                                                      (funcall msb-item-handling-function
+                                                             buffer
+                                                             max-buffer-name-length))
                                                     buffer)))
                                            (cdr buffer-list))
                                    (function
@@ -812,15 +766,14 @@ If the argument is left out or nil, then the current buffer is considered."
                                       (string< (car item1) (car item2)))))))))
                     (msb--choose-file-menu file-buffers))))
     ;; Now make the menu - a list of (TITLE . BUFFER-LIST)
-    (let* ((buffers (buffer-list))
-          menu
+    (let* (menu
           (most-recently-used
            (msb--most-recently-used-menu max-buffer-name-length))
           (others (append file-buffers
                           (loop for elt
-                        across function-info-vector
-                        for value = (msb--create-sort-item elt)
-                        if value collect value))))
+                                across function-info-vector
+                                for value = (msb--create-sort-item elt)
+                                if value collect value))))
       (setq menu
            (mapcar 'cdr                ;Remove the SORT-KEY
                    ;; Sort the menus - not the items.
@@ -867,8 +820,8 @@ If the argument is left out or nil, then the current buffer is considered."
   "Pop up several menus of buffers, for selection with the mouse.
 Returns the selected buffer or nil if no buffer is selected.
 
-The way the buffers are splitted is conveniently handled with the
-variable 'msb-menu-cond'."
+The way the buffers are split is conveniently handled with the
+variable `msb-menu-cond'."
   ;; Popup the menu and return the selected buffer.
   (when (or msb--error
            (not msb--last-buffer-menu)
@@ -876,31 +829,39 @@ variable 'msb-menu-cond'."
            (frame-or-buffer-changed-p))
     (setq msb--error nil)
     (setq msb--last-buffer-menu (msb--create-buffer-menu)))
-  (let ((position event))
+  (let ((position event)
+       choice)
     (when (and (fboundp 'posn-x-y)
               (fboundp 'posn-window))
       (let ((posX (car (posn-x-y (event-start event))))
            (posY (cdr (posn-x-y (event-start event))))
-           (posWind (posn-window (event-start event)))
-           name)
+           (posWind (posn-window (event-start event))))
        ;; adjust position
        (setq posX (- posX (funcall msb-horizontal-shift-function))
              position (list (list posX posY) posWind))))
-    (setq name (x-popup-menu position msb--last-buffer-menu))
-    ;; If toggle bring up the
+    ;; This `sit-for' magically makes the menu stay up if the mouse
+    ;; button is released within 0.1 second.
+    (sit-for 0 100)
+    ;; Popup the menu
+    (setq choice (x-popup-menu position msb--last-buffer-menu))
     (cond
-     ((eq (car name) 'toggle)
-       (msb--toggle-menu-type)
-       (mouse-select-buffer event))
-     ((and (numberp (car name))
-          (null (cdr name)))
-      (let ((msb--last-buffer-menu (nthcdr 3 (assq (car name) msb--last-buffer-menu))))
+     ((eq (car choice) 'toggle)
+      ;; Bring up the menu again with type toggled.
+      (msb--toggle-menu-type)
+      (mouse-select-buffer event))
+     ((and (numberp (car choice))
+          (null (cdr choice)))
+      (let ((msb--last-buffer-menu (nthcdr 3 (assq (car choice) msb--last-buffer-menu))))
        (mouse-select-buffer event)))
-      ((and (stringp (car name))
-          (null (cdr name)))
-      (cons nil name))
-      (t
-       name))))
+     ((while (numberp (car choice))
+       (setq choice (cdr choice))))
+     ((and (stringp (car choice))
+          (null (cdr choice)))
+      (car choice))
+     ((null choice)
+      choice)
+     (t
+      (error "Unknown form for buffer: %s" choice)))))
                    
 ;; Add separators
 (defun msb--add-separators (sorted-list)
@@ -926,6 +887,37 @@ variable 'msb-menu-cond'."
            (list item)))))
        sorted-list)))))
 
+(defun msb--split-menus-2 (list mcount result)
+  (cond
+   ((> (length list) msb-max-menu-items)
+    (let ((count 0)
+         sub-name
+         (tmp-list nil))
+      (while (< count msb-max-menu-items)
+       (push (pop list) tmp-list)
+       (incf count))
+    (setq tmp-list (nreverse tmp-list))
+    (setq sub-name (concat (car (car tmp-list)) "..."))
+    (push (append (list mcount sub-name
+                       'keymap sub-name)
+                 tmp-list)
+         result))
+    (msb--split-menus-2 list (1+ mcount) result))
+   ((null result)
+    list)
+   (t
+    (let (sub-name)
+      (setq sub-name (concat (car (car list)) "..."))
+      (push (append (list mcount sub-name
+                       'keymap sub-name)
+                 list)
+         result))
+    (nreverse result))))
+    
+(defun msb--split-menus (list)
+ (msb--split-menus-2 list 0 nil))
+
+
 (defun msb--make-keymap-menu (raw-menu)
   (let ((end (cons '(nil) 'menu-bar-select-buffer))
        (mcount 0))
@@ -936,15 +928,16 @@ variable 'msb-menu-cond'."
         ((eq 'separator sub-menu)
          (list 'separator "---"))
         (t
-         (append (list (incf mcount) (car sub-menu)
-                       'keymap (car sub-menu))
-                 (mapcar (function
-                          (lambda (item)
-                            (let ((string (car item))
-                                  (buffer (cdr item)))
-                              (cons (buffer-name buffer)
-                                    (cons string end)))))
-                  (cdr sub-menu)))))))
+         (let ((buffers (mapcar (function
+                                 (lambda (item)
+                                   (let ((string (car item))
+                                         (buffer (cdr item)))
+                                     (cons (buffer-name buffer)
+                                           (cons string end)))))
+                                (cdr sub-menu))))
+           (append (list (incf mcount) (car sub-menu)
+                         'keymap (car sub-menu))
+                   (msb--split-menus buffers)))))))
      raw-menu)))
 
 (defun menu-bar-update-buffers (&optional arg)
@@ -953,40 +946,41 @@ variable 'msb-menu-cond'."
             (or (not (fboundp 'frame-or-buffer-changed-p))
                 (frame-or-buffer-changed-p)
                 arg))
-    (let ((buffers (buffer-list))
-         (frames (frame-list))
+    (let ((frames (frame-list))
          buffers-menu frames-menu)
-      ;; If requested, list only the N most recently selected buffers.
-      (when (and (integerp buffers-menu-max-size)
-                (> buffers-menu-max-size 1)
-                (> (length buffers) buffers-menu-max-size))
-       (setcdr (nthcdr buffers-menu-max-size buffers) nil))
       ;; Make the menu of buffers proper.
       (setq msb--last-buffer-menu (msb--create-buffer-menu))
       (setq buffers-menu msb--last-buffer-menu)
       ;; Make a Frames menu if we have more than one frame.
-      (if (cdr frames)
+      (when (cdr frames)
+       (let* ((frame-length (length frames))
+              (f-title  (format "Frames (%d)" frame-length)))
+         ;; List only the N most recently selected frames
+         (when (and (integerp msb-max-menu-items)
+                    (>  msb-max-menu-items 1)
+                    (> frame-length msb-max-menu-items))
+           (setcdr (nthcdr msb-max-menu-items frames) nil))
          (setq frames-menu
-               (cons "Select Frame"
-                     (mapcar
-                      (function
-                       (lambda (frame)
-                         (nconc
-                          (list frame
-                                (cdr (assq 'name
-                                           (frame-parameters frame)))
-                                (cons nil nil))
-                          'menu-bar-select-frame)))
-                      frames))))
-      (when frames-menu
-       (setq frames-menu (cons 'keymap frames-menu)))
+               (nconc
+                (list 'frame f-title '(nil) 'keymap f-title)
+                (mapcar
+                 (function
+                  (lambda (frame)
+                    (nconc
+                     (list frame
+                           (cdr (assq 'name
+                                      (frame-parameters frame)))
+                           (cons nil nil))
+                     'menu-bar-select-frame)))
+                 frames)))))
       (define-key (current-global-map) [menu-bar buffer]
        (cons "Buffers"
              (if (and buffers-menu frames-menu)
-                 (list 'keymap "Buffers and Frames"
-                       (cons 'buffers (cons "Buffers" buffers-menu))
-                       (cons 'frames (cons "Frames" frames-menu)))
-               (or buffers-menu frames-menu 'undefined)))))))
+                 ;; Combine Frame and Buffers menus with separator between
+                 (nconc (list 'keymap "Buffers and Frames" frames-menu
+                              (and msb-separator-diff '(separator "---")))
+                        (cddr buffers-menu))
+               (or buffers-menu 'undefined)))))))
 
 (when (and (boundp 'menu-bar-update-hook)
           (not (fboundp 'frame-or-buffer-changed-p)))