+(defcustom menu-max-items 25
+ "*Maximum number of items in generated menus.
+If number of entries in such a menu is larger than this value, split menu
+into submenus of nearly equal length (see `menu-submenu-max-items'). If
+nil, never split menu into submenus."
+ :group 'menu
+ :type '(choice (const :tag "no submenus" nil)
+ (integer)))
+
+(defcustom menu-submenu-max-items 20
+ "*Maximum number of items in submenus when splitting menus.
+We split large menus into submenus of this many items, and then balance
+them out as much as possible (otherwise the last submenu may have very few
+items)."
+ :group 'menu
+ :type 'integer)
+
+(defcustom menu-submenu-name-format "%-12.12s ... %.12s"
+ "*Format specification of the submenu name when splitting menus.
+Used by `menu-split-long-menu' if the number of entries in a menu is
+larger than `menu-menu-max-items'.
+This string should contain one %s for the name of the first entry and
+one %s for the name of the last entry in the submenu.
+If the value is a function, it should return the submenu name. The
+function is be called with two arguments, the names of the first and
+the last entry in the menu."
+ :group 'menu
+ :type '(choice (string :tag "Format string")
+ (function)))
+
+(defun menu-split-long-menu (menu)
+ "Split MENU according to `menu-max-items' and add accelerator specs.
+
+You should normally use the idiom
+
+\(menu-split-long-menu (menu-sort-menu menu))
+
+See also `menu-sort-menu'."
+ (let ((len (length menu)))
+ (if (or (null menu-max-items)
+ (<= len menu-max-items))
+ (submenu-generate-accelerator-spec menu)
+ (let* ((outer (/ (+ len (1- menu-submenu-max-items))
+ menu-submenu-max-items))
+ (inner (/ (+ len (1- outer)) outer))
+ (result nil))
+ (while menu
+ (let ((sub nil)
+ (from (car menu)))
+ (dotimes (foo (min inner len))
+ (setq sub (cons (car menu) sub)
+ menu (cdr menu)))
+ (setq len (- len inner))
+ (let ((to (car sub)))
+ (setq sub (nreverse sub))
+ (setq result
+ (cons (cons (if (stringp menu-submenu-name-format)
+ (format menu-submenu-name-format
+ (menu-item-strip-accelerator-spec
+ (aref from 0))
+ (menu-item-strip-accelerator-spec
+ (aref to 0)))
+ (funcall menu-submenu-name-format
+ (menu-item-strip-accelerator-spec
+ (aref from 0))
+ (menu-item-strip-accelerator-spec
+ (aref to 0))))
+ (submenu-generate-accelerator-spec sub))
+ result)))))
+ (submenu-generate-accelerator-spec (nreverse result))))))
+
+(defun menu-sort-menu (menu)
+ "Sort MENU alphabetically.
+
+You should normally use the idiom
+
+\(menu-split-long-menu (menu-sort-menu menu))
+
+See also `menu-split-long-menu'."
+ (sort menu
+ #'(lambda (a b) (string-lessp (aref a 0) (aref b 0)))))
+
+(defun menu-item-search ()
+ "Bring up a search dialog if possible and desired, else do interactive search"
+ (interactive)
+ (if (should-use-dialog-box-p)
+ (make-search-dialog)
+ (isearch-forward)))
+