Question

First of all, I'll admit that I'm a complete novice at Emacs and ELisp (and for that matter Lisp in general), and I have stumbled upon an error that has got me stumped for quite a while now while trying to write my .emacs file.

Here is a minimal example of code necessary to reproduce the problem (i.e. having .emacs containing only the following):

(defun define-esc-key (keybind)
  (define-key key-translation-map (kbd keybind) 'my-esc))
(define-esc-key "M-j")

This will produce the following error with Emacs23:

Lisp error: (wrong-type-argument integer-or-marker-p keybind)
  read-kbd-macro(keybind)
  #[(keys) "\301!\207" [keys read-kbd-macro] 2 2186954](keybind)
  (kbd keybind)
  (define-key key-translation-map (kbd keybind) (quote my-esc))
  define-esc-key("M-j")

but works as I expect it to in Emacs24. It also works in Emacs23 if I replace the instance of keybind in the define-esc-key function body by "M-j".

(By the way, sorry for the bad title but I just couldn't think of anything more descriptive.)

Was it helpful?

Solution

From the NEWS file:

* Lisp Changes in Emacs 24.3
...
*** `kbd' is now a function rather than a macro.

That means that in earlier Emacs versions, the argument to kbd must be literally present in the call, as opposed to the use of a variable in your example.

Alternatively, you can use eval and backquotes to insert the value:

(eval `(kbd ,keybind))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top