2

How do I extract the names for all of the keymaps in Emacs?

I use English layout for programming. However I sometimes have to use a Hungarian layout, and I want preserve my keybindings in a physical form, not a symbolic one. I find quail rather uncomfortable, because it is impossible to make a perfect transition: there are more characters in the Hungarian layout than the English one.

Instead of converting the OS configured English layout characters to Hungarian inside Emacs, I want to translate all of the keymaps to the physical layout of a Hungarian keyboard.

Hopefully, I can write an elisp script for that. However, what I do not know is: how do I extract all of the global, minor, and major mode keymaps from Emacs?

  • The function current-active-maps can be of some help but it is not the full answer (because it only gives currently active maps, as its name implies). – YoungFrog Mar 18 '16 at 19:09
  • (The above comment of mine was initially an answer. Someone decided that it should be a comment instead, so now I can't edit it. Here's some more information, still.) current-active-keymaps doens't give you the name of the keymap, but even though you asked for names (in the first part of your question), they are not actually required for doing what you want (which is rebinding keys). – YoungFrog Mar 18 '16 at 20:55
  • I now realize that the initial question to which I was answering did not mention names at all, which might very well be why I didn't mention them in my first answer? @Dan Why did you introduce keymap names in your edit ? – YoungFrog Mar 18 '16 at 21:18
2

The question is flawed:

  1. A keymap does not necessarily have a name. A keymap can be a list - see the Elisp manual, node Format of Keymaps - and there need not be any name associated with the list.

  2. For those keymaps that might be said to "have a name", there are two different meanings:

    • Symbols whose symbol-value is a keymap (i.e., keymap variables). For these, you can use:

      (mapatoms (lambda (m) (and (boundp m)  (keymapp (symbol-value m))))
                obarray)
      
    • Symbols whose symbol-function is a keymap (so the symbol itself is also a keymap). For these you can use just (mapatoms #'keymapp obarray)
1

Here's a function that will extract all of the keymaps from the global obarray:

(defun get-all-the-keymaps ()
  "Return list of all the keymaps in the global obarray."
  (let (maps)
    (mapatoms (lambda (x)
                (when (keymapp x)
                  (push x maps)))
              obarray)
    maps))

Here's another version that uses the heuristic that any symbol ending in ...-map is a keymap:

(defun really-get-all-the-keymaps ()
  "Return list of all the keymaps in the global obarray."
  (let (maps)
    (mapatoms (lambda (x)
                (when (string-match ".*-map$" (symbol-name x))
                  (push x maps)))
              obarray)
    maps))

Just a heads-up: there's a lot of maps. For example, running it on my machine returns 90 keymaps with the first function, and 624 with the second.

  • Many symbols will not satisfy keymapp but their value will, so you may want to test (or (keymapp x) (keymapp (symbol-value x))) instead. – YoungFrog Mar 18 '16 at 18:59
  • @YoungFrog: throws a bunch of errors, sorry. I've edited the post with another, more permissive function that returns anything ending in ...-map. – Dan Mar 18 '16 at 19:15
  • @YoungFrog: Wrt the second definition given: You cannot tell whether a symbol has any relation to a keymap by whether its symbol-name ends in -map, and certainly not that it "names" a keymap. – Drew Mar 18 '16 at 19:37
  • @Dan Oh yeah you have to test for (boundp x) too before taking symbol-value. Drew gave some code in his answer. – YoungFrog Mar 18 '16 at 20:45
  • @Drew I think you meant to write to Dan. – YoungFrog Mar 18 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.