0

This question already has an answer here:

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.

marked as duplicate by sds, erikstokes, Drew, Dan Aug 30 '15 at 23:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    that lambda needs to be interactively-callable command (i.e., interactive function), you need (interactive) for it, even you don't care about argument. – xuchunyang Aug 30 '15 at 20:58
  • @xuchunyang Why not posting it as an answer? – clemera Aug 30 '15 at 21:01
  • @xuchunyang, it works, but why do lambda's need to be interactive? When pressing that key, the lambda's get evaluated, right? So I miss the part why it's necessary to make them interactive. – ReneFroger Aug 30 '15 at 21:08
  • 1
    ReneFroger: The lambda form needs an interactive declaration for exactly the same reason that you had put one into the original defun. It has nothing to do with using lambda 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:35
  • 2
    If it helps demystify things, note that defun itself is defined in terms of lambda. All functions are ultimately defined by a lambda form. The primary purpose of defun is to alias that lambda form to a symbol name. So any behaviour which applies to lambda functions also necessarily applies to defun 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

Browse other questions tagged or ask your own question.