Skip to main content


elisp nonsense

I've been playing around with keymaps. Apparently they can be used to create menus that give the user a visual list of options. The canonical way to make them is aparently with make-sparse-keymap to create the menu and define-key to add options to it, but this causes some confusing behaviour.

Take the following example:

(let ((menu (make-sparse-keymap "My menu")))
  (define-key menu "a"
    '(menu-item "Foo" foo))
  (define-key menu "b"
    '(menu-item "Bar" bar))
  menu)

Yields the following:
(keymap (98 menu-item "Bar" bar) (97 menu-item "Foo" foo) "My menu")

Each new entry is added to the top of the list, so when the menu is displayed, they're listed in reverse order. This is very counter intuitive.

Now, I understand that the nature of lists in lisp make inserting an element at the top of the list less computationally expensive, but when you've already got to walk the whole list anyway to ensure the key binding isn't already present, this no longer feels like an adequate excuse.

Am I missing something?

#emacs #elisp

reshared this

in reply to Jonathan Lamothe

elisp nonsense

@Robert Pluim 🇪🇺 It looks though like it's primarily meant to build a menu for a menu bar or pop up menu, which is not what I'm looking for.

I'm tempted to just build the structure by hand, but the docs are very emphatic that I must not do this for some reason.

in reply to Jonathan Lamothe

elisp nonsense

Sensitive content

in reply to Robert Pluim 🇪🇺

elisp nonsense
@Robert Pluim 🇪🇺 Yeah, it is for a keymap. I just want a menu on the screen to display what key corresponds to what option. I'm guessing defvar-keymap is worth investigating.
in reply to Jonathan Lamothe

elisp nonsense

Sensitive content

in reply to Robert Pluim 🇪🇺

elisp nonsense
@Robert Pluim 🇪🇺 I seem not to have function definitions for either defvar-keymap or which-key. I'm using Emacs 28.2 (as found in the Debian 12 repositories).
in reply to Jonathan Lamothe

elisp nonsense

Sensitive content

in reply to Jonathan Lamothe

Define key is my least favorite way to make a keymap.

I like defvar-keymap, bind-keys, if you've got a map create already. Like a sparce map.

General is nice too. But then you have to have that installed.

This website uses cookies. If you continue browsing this website, you agree to the usage of cookies.