From d4d6d8f335165e2bda8942b4acd45e5bab613b70 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Wed, 23 Jun 2021 15:02:52 +0200 Subject: [PATCH] Sort the items in 'mode-line-mode-menu' before displaying the menu * lisp/bindings.el (bindings--menu-item-string, bindings--sort-keymap): New functions. (mode-line-major-mode-keymap, mode-line-minor-mode-keymap): Sort 'mode-line-mode-menu'. --- lisp/bindings.el | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lisp/bindings.el b/lisp/bindings.el index 620f28970a2..8d902ba3dfe 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -330,22 +330,50 @@ of the menu's data." (defvar mode-line-mode-menu (make-sparse-keymap "Minor Modes") "\ Menu of mode operations in the mode line.") +(defun bindings--menu-item-string (item) + "Return the menu-item string for ITEM, or nil if not a menu-item." + (cond + ((not (consp item)) nil) ; Not a menu-item. + ((eq 'menu-item (car item)) + (eval (cadr item))) + ((stringp (car item)) + (car item)) + (t nil))) ; Not a menu-item either. + +(defun bindings--sort-keymap (keymap) + "Sort the bindings in KEYMAP in alphabetical order. +The order of bindings in a keymap matters only when it is used as +a menu, so this function is not useful for non-menu keymaps." + (unless (keymapp keymap) + (signal 'wrong-type-argument (list 'keymapp keymap))) + (setcdr keymap + (sort (cdr keymap) + (lambda (a b) + (string< (bindings--menu-item-string (cdr-safe a)) + (bindings--menu-item-string (cdr-safe b)))))) + keymap) + (defvar mode-line-major-mode-keymap (let ((map (make-sparse-keymap))) (bindings--define-key map [mode-line down-mouse-1] `(menu-item "Menu Bar" ignore :filter ,(lambda (_) (mouse-menu-major-mode-map)))) (define-key map [mode-line mouse-2] 'describe-mode) - (define-key map [mode-line down-mouse-3] mode-line-mode-menu) + (bindings--define-key map [mode-line down-mouse-3] + `(menu-item "Menu Bar" ,mode-line-mode-menu + :filter bindings--sort-keymap)) map) "\ Keymap to display on major mode.") (defvar mode-line-minor-mode-keymap - (let ((map (make-sparse-keymap))) + (let ((map (make-sparse-keymap)) + (mode-menu-binding + `(menu-item "Menu Bar" ,mode-line-mode-menu + :filter bindings--sort-keymap))) (define-key map [mode-line down-mouse-1] 'mouse-minor-mode-menu) (define-key map [mode-line mouse-2] 'mode-line-minor-mode-help) - (define-key map [mode-line down-mouse-3] mode-line-mode-menu) - (define-key map [header-line down-mouse-3] mode-line-mode-menu) + (define-key map [mode-line down-mouse-3] mode-menu-binding) + (define-key map [header-line down-mouse-3] mode-menu-binding) map) "\ Keymap to display on minor modes.") -- 2.39.2