10

What is wrong with the following code:

(defun test
  (interactive)
  (message "hello"))
(global-set-key '[f4]  'test)

When evaluating this with eval-region and then pressing F4 I get the error:

Wrong type argument: commandp, test
2
  • 4
    Try (defun test () ...) with an empty parameter list.
    – uselpa
    Dec 12, 2013 at 18:54
  • Yes it works. Thank you very much! You may give it as an answer, and I will accept it.. Dec 12, 2013 at 18:55

1 Answer 1

17

You are missing the argument list of your test function, so Emacs interprets the (interactive) form as the arglist. Thus you have defined a non-interactive function of 1 argument instead of interactive command of no arguments.

What you want is:

(defun test ()
  "My command test"
  (interactive)
  (message "hello"))

Lessons learned:

  1. Always add a doc string - if you did, Emacs would have complained
  2. Use elint (comes with Emacs, try C-h a elint RET).
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.