2008-12-18 65 views

回答

18

我的意思是说,当你将某个函数绑定到一个键时,你需要包含一些你需要的函数的可调用代码 - 比如从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. 
23

只是为了澄清(它是在引用文档that Charlie cites(interactive)不仅仅是键绑定功能,但对于任何功能。没有(interactive),它只能以编程方式调用,而不是从M-x(或通过键绑定)调用。

编辑:注意,只是加入“(互动)”的功能不一定会使其工作方式,无论是 - 可能有许多原因,功能都没有互动。范围,依赖关系,参数等。

12

更值得一提的是,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会让你在minibuffer输入两个数字,你仍然可以做(sum 1 2)为好。

interactive应该返回将用作参数列表,如果所谓的交互功能的列表。

+0

Török,如果我要问另一个之前测试的参数值题? http://stackoverflow.com/questions/25447312/emacs-lisp-how-to-use-interactive-for-conditional-arguments – yPhil 2014-08-22 12:52:31

2

(交互式)用于通过M-x或键盘绑定与用户交互的功能。

的Mx描述功能RET互动RET关于如何使用它,包括参数搭上弦,整数,缓冲名称的详细信息等