Emacs Lisp错误"错误的类型参数:commandp"

Håk*_*and 6 emacs elisp

以下代码有什么问题:

(defun test
  (interactive)
  (message "hello"))
(global-set-key '[f4]  'test)
Run Code Online (Sandbox Code Playgroud)

在评估时eval-region,然后按F4我得到错误:

Wrong type argument: commandp, test
Run Code Online (Sandbox Code Playgroud)

sds*_*sds 11

您缺少test函数的参数列表,因此Emacs将(interactive)表单解释为arglist.因此,您已经定义了1个参数的非交互式函数,而不是没有参数的交互式命令.

你想要的是:

(defun test ()
  "My command test"
  (interactive)
  (message "hello"))
Run Code Online (Sandbox Code Playgroud)

得到教训:

  1. 总是添加一个文档字符串 - 如果你这样做,Emacs会抱怨
  2. 使用elint(Emacs附带,试试C-h a elint RET).