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
Drew
  • 29,895
  • 7
  • 74
  • 104
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174

1 Answers1

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).
sds
  • 58,617
  • 29
  • 161
  • 278