>> (defun my-translate-key (prompt) (interactive) (kbd "C-c"))
>> (define-key key-translation-map (kbd "C-e") (lambda (prompt)
>> (interactive) (kbd "C-c"))) ; Doesn't work
>> (define-key key-translation-map (kbd "C-e") 'my-translate-key) ; Works
> Look at the description of define-key -- the third argument has to be a
> SYMBOL for it to be treated as a function. Lambda doesn't return a
> function, it returns a list.
Yet this works:
(define-key global-map (kbd "C-e") (lambda () (interactive)
(message "Inside C-e's lambda")))
C-e displays the message without going to the end of the line, as expected.
I thought wherever functions are passed, a lambda could be passed
instead. The Elisp manual corroborates this in section 12.7 Anonymous
Functions: "Anonymous functions are valid wherever function names
are.". I don't see where my misunderstanding is.
I tried another thing in scratch:
(fset 'foo (lambda (prompt) (message "Inside lambda.") (kbd "C-c")))
(define-key key-translation-map (kbd "C-e") 'foo)
This behaves as I'd expect; I see "Inside lambda" and a translation to
C-c. Thus it would seem the type returned from the lambda form is not
The reason I ask is because I'm trying to create a generalized way to
make key translations that either translate to another key or to
itself based on an evaluated predicate. My approach was:
(defun make-conditional-key-translation (key-from key-to translate-keys-p)
"Make a Key Translation such that if the translate-keys-p function
key-from translates to key-to, else key-from translates to itself.
takes no args. "
(define-key key-translation-map key-from
(if (funcall translate-keys-p) key-to key-from)))
(defun my-translate-keys-p ()
"Returns whether conditional key translations should be active. See
make-conditional-key-translation function. "
(or (evil-motion-state-p) (evil-normal-state-p) (evil-visual-state-p))
(make-conditional-key-translation (kbd "ce") (kbd "C-e") 'my-translate-keys-p)