Emacs Lisp函数通常像这样开始:
(lambda () (interactive) ...
"(互动)"有什么作用?
只是为了澄清(它在Charlie引用的引用文档中)(interactive)
不仅适用于键绑定函数,而且适用于任何函数.没有(interactive)
,它只能以编程方式调用,而不能从M-x
(或通过键绑定)调用.
编辑:请注意,只是将"(交互式)"添加到函数中并不一定会使它以这种方式工作 - 可能有很多原因函数不是交互式的.范围,依赖关系,参数等
我的意思是你要包含一些代码,用于在绑定到键时使函数可调用所需的东西 - 比如从CTRL-u获取参数.
看看CTRL-h f interactive
细节:
interactive is a special form in `C source code'. (interactive args) Specify a way of parsing arguments for interactive use of a function. For example, write (defun foo (arg) "Doc string" (interactive "p") ...use arg...) to make ARG be the prefix argument when `foo' is called as a command. The "call" to `interactive' is actually a declaration rather than a function; it tells `call-interactively' how to read arguments to pass to the function. When actually called, `interactive' just returns nil. The argument of `interactive' is usually a string containing a code letter followed by a prompt. (Some code letters do not use I/O to get the argument and do not need prompts.) To prompt for multiple arguments, give a code letter, its prompt, a newline, and another code letter, etc. Prompts are passed to format, and may use % escapes to print the arguments that have already been read.
更值得一提的是,这interactive
是交互式上下文中的主要目的(例如,当用户使用键绑定调用函数时)让用户指定函数参数,否则只能以编程方式给出.
例如,考虑函数sum
返回两个数字的总和.
(defun sum (a b) (+ a b))
您可以调用它,(sum 1 2)
但只能在Lisp程序(或REPL)中执行.如果interactive
在函数中使用特殊形式,则可以询问用户参数.
(defun sum (a b) (interactive (list (read-number "First num: ") (read-number "Second num: "))) (+ a b))
现在M-x sum
让你在迷你缓冲区中键入两个数字,你仍然可以这样做(sum 1 2)
.
interactive
如果交互式调用函数,则应返回一个列表,该列表将用作参数列表.