Emacs Lisp函数通常像这样开始:
(lambda () (interactive) ...
Run Code Online (Sandbox Code Playgroud)
"(互动)"有什么作用?
Cha*_*tin 19
我的意思是你要包含一些代码,用于在绑定到键时使函数可调用所需的东西 - 比如从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.
Tör*_*bor 12
更值得一提的是,这interactive是交互式上下文中的主要目的(例如,当用户使用键绑定调用函数时)让用户指定函数参数,否则只能以编程方式给出.
例如,考虑函数sum返回两个数字的总和.
(defun sum (a b)
(+ a b))
Run Code Online (Sandbox Code Playgroud)
您可以调用它,(sum 1 2)但只能在Lisp程序(或REPL)中执行.如果interactive在函数中使用特殊形式,则可以询问用户参数.
(defun sum (a b)
(interactive
(list
(read-number "First num: ")
(read-number "Second num: ")))
(+ a b))
Run Code Online (Sandbox Code Playgroud)
现在M-x sum让你在迷你缓冲区中键入两个数字,你仍然可以这样做(sum 1 2).
interactive 如果交互式调用函数,则应返回一个列表,该列表将用作参数列表.
| 归档时间: |
|
| 查看次数: |
4075 次 |
| 最近记录: |