From: Jim Porter <jporterbugs@gmail.com>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: emacs-devel@gnu.org
Subject: Re: [WIP PATCH] Adding keys to keymaps in alphabetical order (for use with `mode-line-mode-menu')
Date: Tue, 22 Jun 2021 22:17:34 -0700 [thread overview]
Message-ID: <44969496-4f6c-e299-9fee-7bad01c17815@gmail.com> (raw)
In-Reply-To: <87bl7ztn4p.fsf@gnus.org>
[-- Attachment #1: Type: text/plain, Size: 870 bytes --]
On 6/21/2021 5:31 AM, Lars Ingebrigtsen wrote:
> Jim Porter <jporterbugs@gmail.com> writes:
>
>> I'll look into the best way to trigger a sort just before opening the
>> menu. Ideally, we could avoid re-sorting every time (unless the list
>> has changed), but that might be more work than it's worth, given that
>> the list is unlikely to be more than a few dozen elements.
>
> Yeah, menus are usually short, so just sorting should be fine, I think.
Attached is a patch that I think should do this. I've only tested the
new functions with `mode-line-mode-menu', so they might not work on all
menus (hence, I've made them private for now). It's possible these
functions would be more broadly useful, though.
Performance-wise, the cost is negligible for the default value of
`mode-line-mode-menu': ~10ms for the first time and 0.02ms for
subsequent calls.
- Jim
[-- Attachment #2: 0001-Sort-the-items-in-mode-line-mode-menu-before-display.patch --]
[-- Type: text/plain, Size: 3004 bytes --]
From f25ba15da1148ee24c70e44f3d144fdf879edb0e Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Tue, 22 Jun 2021 21:52:37 -0700
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 | 37 +++++++++++++++++++++++++++++++++----
1 file changed, 33 insertions(+), 4 deletions(-)
diff --git a/lisp/bindings.el b/lisp/bindings.el
index 4e5497cc79..f3c4f42f78 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -330,22 +330,51 @@ bindings--define-key
(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."
+ (or (keymapp keymap)
+ (signal 'wrong-type-argument (list 'keymapp keymap)))
+ (let ((menu-items
+ (sort (cdr keymap)
+ (lambda (a b)
+ (string< (bindings--menu-item-string (cdr-safe a))
+ (bindings--menu-item-string (cdr-safe b)))))))
+ (setcdr keymap menu-items)
+ 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.25.1
next prev parent reply other threads:[~2021-06-23 5:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-18 3:39 [WIP PATCH] Adding keys to keymaps in alphabetical order (for use with `mode-line-mode-menu') Jim Porter
2021-06-19 12:31 ` Lars Ingebrigtsen
2021-06-21 5:31 ` Jim Porter
2021-06-21 12:31 ` Lars Ingebrigtsen
2021-06-23 5:17 ` Jim Porter [this message]
2021-06-23 13:06 ` Lars Ingebrigtsen
2021-06-23 13:58 ` Stefan Monnier
2021-06-23 14:10 ` Lars Ingebrigtsen
2021-06-23 16:50 ` [Updated Patch] " Jim Porter
2021-06-24 14:51 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=44969496-4f6c-e299-9fee-7bad01c17815@gmail.com \
--to=jporterbugs@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=larsi@gnus.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).