I'm reading about some concepts of Emacs Lisp. The lambda's seems interesting to me. After reading the manuals about lambda's in Emacs Lisp, I decided to play with it.
For example, I have this snippet:
(defun my-insert-arrow ()
(interactive)
(insert "->"))
(evil-define-key 'insert php-mode map (kbd "C-<next>") 'my-insert-arrow))
Okay, then I replace that function with a lambda:
(evil-define-key 'insert php-mode map (kbd "C-<next>") (lambda () (insert "->")))
But I got the following error:
command-execute: Wrong type argument: commandp, (lambda nil (insert "->"))
So it seems I applied the lambda in the wrong way. But I found no difference with the manuals, except that the lambda's in the tutorial examples contains arguments.
(interactive)
for it, even you don't care about argument. – xuchunyang Aug 30 '15 at 20:58interactive
tells Emacs how to call the function interactively. without it emacs does not know how to pass the arguments in an interactive call. your corner case (no arguments) falls well into the general pattern. – sds Aug 30 '15 at 21:25interactive
declaration for exactly the same reason that you had put one into the originaldefun
. It has nothing to do with usinglambda
to define the function, and everything to do with the fact that you're binding it to a key. Only commands may be bound to keys, and functions are not commands unless they have an interactive declaration. – phils Aug 30 '15 at 21:35defun
itself is defined in terms oflambda
. All functions are ultimately defined by a lambda form. The primary purpose ofdefun
is to alias that lambda form to a symbol name. So any behaviour which applies tolambda
functions also necessarily applies todefun
functions, and it makes no sense to suggest that the former should have special behaviour for key bindings which doesn't also apply to the latter. There needs to be a way to differentiate command functions from non-command functions,interactive
is it, and it applies to all functions. – phils Aug 30 '15 at 21:59