1

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.)

mwnx
  • 97
  • 4
  • The escape key is normally used as a prefix shortcut key which precedes other keys in a series of keyboard shortcut sequences. Is it your intention to disable the escape prefix key so that it only does one thing -- i.e., launch your own function `my-esc` and that's it? Do you just want the escape key to `indent-new-comment-line`? If not, then what specific function do you want the escape key to run when it is pressed? – lawlist Jan 20 '14 at 15:16
  • If you want to disable the usability of the escape key as a keyboard shortcut prefix so that the escape key only does one thing, then you could do something like this: `(global-set-key (kbd "") 'indent-new-comment-line)`. However, the better approach would be to learn a little bit of `elisp` and create some conditions that takes advantage of a slightly more complex code written by `Stefan` in this thread: http://stackoverflow.com/questions/20026083/how-to-use-escape-conditionally-as-a-modifier-key   Or, just keep the escape key the way it is normally as a prefix key. – lawlist Jan 20 '14 at 15:24
  • @lawlist: I'm not sure why you mention the escape key but if you're talking about my weird rebinding of `M-j` ("meta j"?), that's because I'm a long time Vim user who's been convinced to switch to Emacs for its scripting capabilities and for the "Evil" Vi emulation mode. I'm used to using Alt+j/k/l/m/o/I/O/... in a terminal running vim to switch from insert to normal mode (instead of using the hard to reach escape key) and wanted to duplicate this with Evil. – mwnx Jan 20 '14 at 16:22

1 Answers1

1

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))
legoscia
  • 39,593
  • 22
  • 116
  • 167