Next: Command Loop Info, Previous: Interactive Call, Up: Command Loop
Sometimes a command should display additional visual feedback (such
as an informative message in the echo area) for interactive calls
only. There are three ways to do this. The recommended way to test
whether the function was called using call-interactively is to
give it an optional argument print-message and use the
interactive spec to make it non-nil in interactive
calls. Here's an example:
(defun foo (&optional print-message)
(interactive "p")
(when print-message
(message "foo")))
We use "p" because the numeric prefix argument is never
nil. Defined in this way, the function does display the
message when called from a keyboard macro.
The above method with the additional argument is usually best,
because it allows callers to say “treat this call as interactive”.
But you can also do the job by testing called-interactively-p.
This function returns
twhen the calling function was called usingcall-interactively.The argument kind should be either the symbol
interactiveor the symbolany. If it isinteractive, thencalled-interactively-preturnstonly if the call was made directly by the user—e.g., if the user typed a key sequence bound to the calling function, but not if the user ran a keyboard macro that called the function (see Keyboard Macros). If kind isany,called-interactively-preturnstfor any kind of interactive call, including keyboard macros.If in doubt, use
any; the only known proper use ofinteractiveis if you need to decide whether to display a helpful message while a function is running.A function is never considered to be called interactively if it was called via Lisp evaluation (or with
applyorfuncall).
Here is an example of using called-interactively-p:
(defun foo ()
(interactive)
(when (called-interactively-p 'any)
(message "Interactive!")
'foo-called-interactively))
;; Type M-x foo.
-| Interactive!
(foo)
⇒ nil
Here is another example that contrasts direct and indirect calls to
called-interactively-p.
(defun bar ()
(interactive)
(message "%s" (list (foo) (called-interactively-p 'any))))
;; Type M-x bar.
-| (nil t)