Emacs name of the current local layout?

I am writing an elisp function that constantly associates a given key with a given command in the current key map of the main mode. For instance,

(define-key python-mode-map [C-f1] 'python-describe-symbol) 

The command and key sequence are collected interactively from the user. However, I am having trouble creating a KEYMAP name (for example, "python-mode-map") that matches the current main mode.

I tried the function (current-local-map), but this function returns the keymap object itself, not its name.

I understand that many basic layouts of mode keys are named according to the convention '' main-mode-name '-mode-map', however this is not always the case (e.g. python-shell-map), so I would prefer that my the code did not rely on this convention. (I'm not even sure how to access the name of the current main mode).

(define-key ...) should be added to the initialization file, therefore, although

(define-key (current-local-map) key command)

seems to work, it doesn't work like code in the initialization file.

+6
source share
3 answers

There is no direct way to find the name of the current local keyboard layout - more precisely, the character to which its value is bound, because the keyboard map should not even bind to the character. However, mode keymaps are usually bound to a global symbol, and you can find which one it executes by iterating through all the characters and stopping at whose eq value for the mapmap object.

This should look at all the characters (although this is the minimum work with each), but has the advantage of not relying on any particular naming convention.

 (defun keymap-symbol (keymap) "Return the symbol to which KEYMAP is bound, or nil if no such symbol exists." (catch 'gotit (mapatoms (lambda (sym) (and (boundp sym) (eq (symbol-value sym) keymap) (not (eq sym 'keymap)) (throw 'gotit sym)))))) ;; in *scratch*: (keymap-symbol (current-local-map)) ==> lisp-interaction-mode-map 
+9
source

The local-set-key function exists to bind keys in the current local layout.

+2
source

Perhaps you could try:

(define-key (concat (symbol-name major-mode) "-map") [C-f1] 'python-describe-symbol)

edit: Although this will result in the correct STRING, it still needs to be converted back to a character.

+1
source

All Articles