11

I am writing an elisp function that permanently binds a given key to a given command in the current major mode's keymap. For example,

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

The command and the key sequence are gathered interactively from the user. However, I am having trouble producing the name of the KEYMAP (e.g. 'python-mode-map') that corresponds to the current major mode.

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

I understand that many major mode keymaps are named according to the convention ''major-mode-name'-mode-map', however, this is not always the case (for example, python-shell-map), so I would rather my code not rely on this convention. (I am not even sure how to access the name of the current major mode).

The (define-key ...) is to be added to an init file, so although

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

seems to work, it does not work as code on an initialization file.

ealfonso
  • 6,622
  • 5
  • 39
  • 67
  • Why are you writing code that generates other code? What's the deeper issue you're trying to solve? Could you perhaps write some libraries that others could simply `require`? Would you be able to write a single mode that does these kinds of functions `magically-describe-symbol` that does the right thing based on the current mode? etc. – Trey Jackson Jan 23 '13 at 22:23
  • My point/question is, if you can programatically add these, you might be able to solve it in a more general, more elegant way - one that doesn't involve a bunch of snippets of code that look nearly identical except for the mode map... – Trey Jackson Jan 23 '13 at 22:45
  • 2
    @TreyJackson Those "bunch of snippets" could be pieces of code that do belong in one's `.emacs` (within reasonable numbers, of course). Code that generates other code is not frowned-upon in Lisp. – user4815162342 Jan 24 '13 at 09:18

3 Answers3

14

There is no direct way to find the name of the current local keymap—more precisely, the symbol to which its value is bound—because the keymap doesn't even have to be bound to a symbol. However, mode keymaps typically are bound to a global symbol, and one can find which one it is by iterating through all symbols and stopping on the one whose value is eq to the keymap object.

This has to look at all the symbols (although it does 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
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • 1
    Thank you. I thought about trying this as a last resort, but I was hoping that there be another way (also, I was not sure against which symbols to match). The performance is not a concern, since the code is run only once, so this is a solution for me. – ealfonso Jan 23 '13 at 22:18
5

The function local-set-key exists for the purpose of binding keys in the current local keymap.

phils
  • 71,335
  • 11
  • 153
  • 198
  • 1
    Thank you. I was aware of that. However, as I mentioned, my purpose is to generate code that will be added to an initialization file. Therefore, at the time of initialization, the `local-set-key` will not bind the key to the intended key-map. – ealfonso Jan 23 '13 at 22:08
2

Maybe you could try:

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

edit: Though this would yield the correct STRING, it should still be converted back to a symbol.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • Thank you. As I mentioned, I do not want to rely on this convention, but this is a good guess! I think most major modes do follow the convention. – ealfonso Jan 23 '13 at 22:27
  • 1
    Coming back to this after a couple of years of elisp hackery, you would just `(intern (concat ...)))` to intern the string as a symbol – ealfonso Aug 08 '15 at 17:42