[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

21.3 Interactive Call

After the command loop has translated a key sequence into a command it invokes that command using the function command-execute. If the command is a function, command-execute calls call-interactively, which reads the arguments and calls the command. You can also call these functions yourself.

Function: commandp object
Returns t if object is suitable for calling interactively; that is, if object is a command. Otherwise, returns nil.

The interactively callable objects include strings and vectors (treated as keyboard macros), lambda expressions that contain a top-level call to interactive, byte-code function objects made from such lambda expressions, autoload objects that are declared as interactive (non-nil fourth argument to autoload), and some of the primitive functions.

A symbol satisfies commandp if its function definition satisfies commandp.

Keys and keymaps are not commands. Rather, they are used to look up commands (see section 22. Keymaps).

See documentation in 24.2 Access to Documentation Strings, for a realistic example of using commandp.

Function: call-interactively command &optional record-flag keys
This function calls the interactively callable function command, reading arguments according to its interactive calling specifications. An error is signaled if command is not a function or if it cannot be called interactively (i.e., is not a command). Note that keyboard macros (strings and vectors) are not accepted, even though they are considered commands, because they are not functions.

If record-flag is non-nil, then this command and its arguments are unconditionally added to the list command-history. Otherwise, the command is added only if it uses the minibuffer to read an argument. See section 21.14 Command History.

The argument keys, if given, specifies the sequence of events to supply if the command inquires which events were used to invoke it.

Function: command-execute command &optional record-flag keys special
This function executes command. The argument command must satisfy the commandp predicate; i.e., it must be an interactively callable function or a keyboard macro.

A string or vector as command is executed with execute-kbd-macro. A function is passed to call-interactively, along with the optional record-flag.

A symbol is handled by using its function definition in its place. A symbol with an autoload definition counts as a command if it was declared to stand for an interactively callable function. Such a definition is handled by loading the specified library and then rechecking the definition of the symbol.

The argument keys, if given, specifies the sequence of events to supply if the command inquires which events were used to invoke it.

The argument special, if given, means to ignore the prefix argument and not clear it. This is used for executing special events (see section 21.8 Special Events).

Command: execute-extended-command prefix-argument
This function reads a command name from the minibuffer using completing-read (see section 20.5 Completion). Then it uses command-execute to call the specified command. Whatever that command returns becomes the value of execute-extended-command.

If the command asks for a prefix argument, it receives the value prefix-argument. If execute-extended-command is called interactively, the current raw prefix argument is used for prefix-argument, and thus passed on to whatever command is run.

execute-extended-command is the normal definition of M-x, so it uses the string `M-x ' as a prompt. (It would be better to take the prompt from the events used to invoke execute-extended-command, but that is painful to implement.) A description of the value of the prefix argument, if any, also becomes part of the prompt.

(execute-extended-command 1)
---------- Buffer: Minibuffer ----------
1 M-x forward-word RET
---------- Buffer: Minibuffer ----------
     => t

Function: interactive-p
This function returns t if the containing function (the one whose code includes the call to interactive-p) was called interactively, with the function call-interactively. (It makes no difference whether call-interactively was called from Lisp or directly from the editor command loop.) If the containing function was called by Lisp evaluation (or with apply or funcall), then it was not called interactively.

The most common use of interactive-p is for deciding whether to print an informative message. As a special exception, interactive-p returns nil whenever a keyboard macro is being run. This is to suppress the informative messages and speed execution of the macro.

For example:

(defun foo ()
  (when (interactive-p)
    (message "foo")))
     => foo

(defun bar ()
  (setq foobar (list (foo) (interactive-p))))
     => bar

;; Type M-x foo.
     -| foo

;; Type M-x bar.
;; This does not print anything.

     => (nil t)

The other way to do this sort of job is to make the command take an argument print-message which should be non-nil in an interactive call, and use the interactive spec to make sure it is non-nil. Here's how:

(defun foo (&optional print-message)
  (interactive "p")
  (when print-message
    (message "foo")))

The numeric prefix argument, provided by `p', is never nil.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on May 2, 2002 using texi2html