aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2007-10-12 04:52:06 +0000
committerRichard M. Stallman2007-10-12 04:52:06 +0000
commit77832c61d08963df38c638ab02ca6f5b9e30ba4f (patch)
tree644ccb8073efe203b0adfe117b50b91ceda62e2a
parente27e48b582a4a383a4912f3054cb5ab2da653acb (diff)
downloademacs-77832c61d08963df38c638ab02ca6f5b9e30ba4f.tar.gz
emacs-77832c61d08963df38c638ab02ca6f5b9e30ba4f.zip
(Distinguish Interactive): New node,
broken out from Interactive Call and rewritten. (Command Loop): Put Distinguish Interactive in menu.
-rw-r--r--lispref/commands.texi104
1 files changed, 61 insertions, 43 deletions
diff --git a/lispref/commands.texi b/lispref/commands.texi
index b6a6929ad6b..f54edba9175 100644
--- a/lispref/commands.texi
+++ b/lispref/commands.texi
@@ -18,6 +18,7 @@ are done, and the subroutines that allow Lisp programs to do them.
18* Command Overview:: How the command loop reads commands. 18* Command Overview:: How the command loop reads commands.
19* Defining Commands:: Specifying how a function should read arguments. 19* Defining Commands:: Specifying how a function should read arguments.
20* Interactive Call:: Calling a command, so that it will read arguments. 20* Interactive Call:: Calling a command, so that it will read arguments.
21* Distinguish Interactive:: Making a command distinguish interactive calls.
21* Command Loop Info:: Variables set by the command loop for you to examine. 22* Command Loop Info:: Variables set by the command loop for you to examine.
22* Adjusting Point:: Adjustment of point after a command. 23* Adjusting Point:: Adjustment of point after a command.
23* Input Events:: What input looks like when you read it. 24* Input Events:: What input looks like when you read it.
@@ -635,42 +636,74 @@ part of the prompt.
635@end example 636@end example
636@end deffn 637@end deffn
637 638
638@defun interactive-p 639@node Distinguish Interactive
639This function returns @code{t} if the containing function (the one 640@section Distinguish Interactive Calls
640whose code includes the call to @code{interactive-p}) was called in 641
641direct response to user input. This means that it was called with the 642 Sometimes a command should display additional visual feedback (such
642function @code{call-interactively}, and that a keyboard macro is 643as an informative message in the echo area) for interactive calls
643not running, and that Emacs is not running in batch mode. 644only. There are three ways to do this. The recommended way to test
645whether the function was called using @code{call-interactively} is to
646give it an optional argument @code{print-message} and use the
647@code{interactive} spec to make it non-@code{nil} in interactive
648calls. Here's an example:
649
650@example
651(defun foo (&optional print-message)
652 (interactive "p")
653 (when print-message
654 (message "foo")))
655@end example
656
657@noindent
658We use @code{"p"} because the numeric prefix argument is never
659@code{nil}. Defined in this way, the function does display the
660message when called from a keyboard macro.
661
662 The above method with the additional argument is usually best,
663because it allows callers to say ``treat this call as interactive.''
664But you can also do the job in a simpler way by testing
665@code{called-interactively-p}.
666
667@defun called-interactively-p
668This function returns @code{t} when the calling function was called
669using @code{call-interactively}.
644 670
645If the containing function was called by Lisp evaluation (or with 671If the containing function was called by Lisp evaluation (or with
646@code{apply} or @code{funcall}), then it was not called interactively. 672@code{apply} or @code{funcall}), then it was not called interactively.
647@end defun 673@end defun
648 674
649 The most common use of @code{interactive-p} is for deciding whether 675 Here's an example of using @code{called-interactively-p}:
650to give the user additional visual feedback (such as by printing an
651informative message). For example:
652 676
653@example 677@example
654@group 678@group
655;; @r{Here's the usual way to use @code{interactive-p}.}
656(defun foo () 679(defun foo ()
657 (interactive) 680 (interactive)
658 (when (interactive-p) 681 (when (called-interactively-p)
659 (message "foo"))) 682 (message "foo"))
683 'haha)
660 @result{} foo 684 @result{} foo
661@end group 685@end group
662 686
663@group 687@group
664;; @r{This function is just to illustrate the behavior.} 688;; @r{Type @kbd{M-x foo}.}
665(defun bar () 689 @print{} foo
666 (interactive)
667 (setq foobar (list (foo) (interactive-p))))
668 @result{} bar
669@end group 690@end group
670 691
671@group 692@group
672;; @r{Type @kbd{M-x foo}.} 693(foo)
673 @print{} foo 694 @result{} haha
695@end group
696@end example
697
698 Here is another example that contrasts direct and indirect
699calls to @code{called-interactively-p}.
700
701@example
702@group
703(defun bar ()
704 (interactive)
705 (setq foobar (list (foo) (called-interactively-p))))
706 @result{} bar
674@end group 707@end group
675 708
676@group 709@group
@@ -684,31 +717,16 @@ foobar
684@end group 717@end group
685@end example 718@end example
686 719
687 If you want to test @emph{only} whether the function was called 720 If you want to treat commands run in keyboard macros just like calls
688using @code{call-interactively}, add an optional argument 721from Lisp programs, test @code{interactive-p} instead of
689@code{print-message} which should be non-@code{nil} in an interactive 722@code{called-interactively-p}.
690call, and use the @code{interactive} spec to make sure it is
691non-@code{nil}. Here's an example:
692
693@example
694(defun foo (&optional print-message)
695 (interactive "p")
696 (when print-message
697 (message "foo")))
698@end example
699
700@noindent
701Defined in this way, the function does display the message when called
702from a keyboard macro. We use @code{"p"} because the numeric prefix
703argument is never @code{nil}.
704
705@defun called-interactively-p
706This function returns @code{t} when the calling function was called
707using @code{call-interactively}.
708 723
709When possible, instead of using this function, you should use the 724@defun interactive-p
710method in the example above; that method makes it possible for a 725This function returns @code{t} if the containing function (the one
711caller to ``pretend'' that the function was called interactively. 726whose code includes the call to @code{interactive-p}) was called in
727direct response to user input. This means that it was called with the
728function @code{call-interactively}, and that a keyboard macro is
729not running, and that Emacs is not running in batch mode.
712@end defun 730@end defun
713 731
714@node Command Loop Info 732@node Command Loop Info