3

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.

ReneFroger
  • 3,855
  • 22
  • 63
  • 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: even though you *can* do this; it's not a good idea. http://stackoverflow.com/a/27264339/324105 – phils Aug 30 '15 at 21:08
  • When you press a key, it runs a command, a function is not a command. – Jordon Biondo Aug 30 '15 at 21:10
  • @JordonBiondo Clear, but why is the keybinding restricted to commands only and not lambda's? From what I understand, command includes a call to interactive. But lambda's evaluates the functions, so I don't understand why there is a restriction. When pressing a key, the functions could be evaluated anyway in my opinion. So I'm trying to understand this. – ReneFroger Aug 30 '15 at 21:15
  • @sds I also noticed that question, but thanks anyway. I wonder more why the keybinding is currently restricted to commands, and not lambda's e.g. – ReneFroger Aug 30 '15 at 21:17
  • 1
    `interactive` 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:25
  • 3
    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
  • 3
    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
  • @phils thanks for your well written answer! I learn every time something worth about Elisp after reading your comments. It's appreciated. – ReneFroger Aug 31 '15 at 10:13

0 Answers0