(define-key mode map symbol)
Normally what you see a quoted value (which means literally that value).
(define-key evil-normal-state-map "i" 'evil-insert-state)
That means when the key is pressed, it actually looks up for that value as a variable and then execute it, a bit like what getattr(Object, 'string')
does in Python. The subtle distinction is that 'string' is literally that value however getattr
returns the function object with that name.
However I can perfectly replace this with an interactive lambda.
(define-key evil-normal-state-map "i"
(lambda () (interactive) (message "%s" major-mode)))
How is it possible that a 'value
(which I think is a bit like string
in Python) can be replaced with a function object. Is it that (interactive)
is doing something special?
Can someone please explain why there is an equivalence between 'string and the lambda in this case is it because the define-key
knows to handle both these datatype appropriately?
FYKI: I bumped into this problem because I wanted to conditionally bind some key depending on the major-mode.
'foo
(or equivalently(quote foo)
), it evaluates to the symbolfoo
then emacs looks up the function definition of that symbol and uses it. If you give(lambda (...) (interactive) ...)
, it evaluates to an interactive function, i.e. a command, which is used as is.