Next: Command Loop Info, Previous: Defining Commands, Up: Command Loop
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.
Returns
tif object is suitable for calling interactively; that is, if object is a command. Otherwise, returnsnil.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-nilfourth argument toautoload), and some of the primitive functions.A symbol satisfies
commandpif its function definition satisfiescommandp. Keys and keymaps are not commands. Rather, they are used to look up commands (see Keymaps).If for-call-interactively is non-
nil, thencommandpreturnstonly for objects thatcall-interactivelycould call—thus, not for keyboard macros.See
documentationin Accessing Documentation, for a realistic example of usingcommandp.
This function calls the interactively callable function command, reading arguments according to its interactive calling specifications. It returns whatever command returns. 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 command is a symbol, then
call-interactivelyuses its function definition.If record-flag is non-
nil, then this command and its arguments are unconditionally added to the listcommand-history. Otherwise, the command is added only if it uses the minibuffer to read an argument. See 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. If keys is omitted or
nil, the return value ofthis-command-keysis used. See Definition of this-command-keys.
This function executes command. The argument command must satisfy the
commandppredicate; 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 tocall-interactively, along with the optional record-flag and keys.A symbol is handled by using its function definition in its place. A symbol with an
autoloaddefinition 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 special, if given, means to ignore the prefix argument and not clear it. This is used for executing special events (see Special Events).
This function reads a command name from the minibuffer using
completing-read(see Completion). Then it usescommand-executeto call the specified command. Whatever that command returns becomes the value ofexecute-extended-command.If the command asks for a prefix argument, it receives the value prefix-argument. If
execute-extended-commandis called interactively, the current raw prefix argument is used for prefix-argument, and thus passed on to whatever command is run.
execute-extended-commandis 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 invokeexecute-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 3) ---------- Buffer: Minibuffer ---------- 3 M-x forward-word RET ---------- Buffer: Minibuffer ---------- ⇒ t
This function returns
tif the containing function (the one whose code includes the call tointeractive-p) was called in direct response to user input. This means that it was called with the functioncall-interactively, and that a keyboard macro is not running, and that Emacs is not running in batch mode.If the containing function was called by Lisp evaluation (or with
applyorfuncall), then it was not called interactively.
The most common use of interactive-p is for deciding whether
to give the user additional visual feedback (such as by printing an
informative message). For example:
;; Here's the usual way to use interactive-p.
(defun foo ()
(interactive)
(when (interactive-p)
(message "foo")))
⇒ foo
;; This function is just to illustrate the behavior.
(defun bar ()
(interactive)
(setq foobar (list (foo) (interactive-p))))
⇒ bar
;; Type M-x foo.
-| foo
;; Type M-x bar.
;; This does not display a message.
foobar
⇒ (nil t)
If you want to test only whether the function was called
using call-interactively, add an optional 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 an example:
(defun foo (&optional print-message)
(interactive "p")
(when print-message
(message "foo")))
Defined in this way, the function does display the message when called
from a keyboard macro. We use "p" because the numeric prefix
argument is never nil.
This function returns
twhen the calling function was called usingcall-interactively.When possible, instead of using this function, you should use the method in the example above; that method makes it possible for a caller to “pretend” that the function was called interactively.
