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")))
"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
This function returns
twhen the calling function was called using
The argument kind should be either the symbol
interactiveor the symbol
any. If it is
tonly 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 is
tfor any kind of interactive call, including keyboard macros.
If in doubt, use
any; the only known proper use of
interactiveis 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
Here is an example of using
(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
(defun bar () (interactive) (message "%s" (list (foo) (called-interactively-p 'any)))) ;; Type M-x bar. -| (nil t)