2008-12-18 64 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關於如何使用它,包括參數搭上弦,整數,緩衝名稱的詳細信息等