diff options
| author | Richard M. Stallman | 2007-10-12 04:52:06 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2007-10-12 04:52:06 +0000 |
| commit | 77832c61d08963df38c638ab02ca6f5b9e30ba4f (patch) | |
| tree | 644ccb8073efe203b0adfe117b50b91ceda62e2a | |
| parent | e27e48b582a4a383a4912f3054cb5ab2da653acb (diff) | |
| download | emacs-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.texi | 104 |
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 |
| 639 | This function returns @code{t} if the containing function (the one | 640 | @section Distinguish Interactive Calls |
| 640 | whose code includes the call to @code{interactive-p}) was called in | 641 | |
| 641 | direct response to user input. This means that it was called with the | 642 | Sometimes a command should display additional visual feedback (such |
| 642 | function @code{call-interactively}, and that a keyboard macro is | 643 | as an informative message in the echo area) for interactive calls |
| 643 | not running, and that Emacs is not running in batch mode. | 644 | only. There are three ways to do this. The recommended way to test |
| 645 | whether the function was called using @code{call-interactively} is to | ||
| 646 | give it an optional argument @code{print-message} and use the | ||
| 647 | @code{interactive} spec to make it non-@code{nil} in interactive | ||
| 648 | calls. 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 | ||
| 658 | We use @code{"p"} because the numeric prefix argument is never | ||
| 659 | @code{nil}. Defined in this way, the function does display the | ||
| 660 | message when called from a keyboard macro. | ||
| 661 | |||
| 662 | The above method with the additional argument is usually best, | ||
| 663 | because it allows callers to say ``treat this call as interactive.'' | ||
| 664 | But you can also do the job in a simpler way by testing | ||
| 665 | @code{called-interactively-p}. | ||
| 666 | |||
| 667 | @defun called-interactively-p | ||
| 668 | This function returns @code{t} when the calling function was called | ||
| 669 | using @code{call-interactively}. | ||
| 644 | 670 | ||
| 645 | If the containing function was called by Lisp evaluation (or with | 671 | If 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}: |
| 650 | to give the user additional visual feedback (such as by printing an | ||
| 651 | informative 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 | ||
| 699 | calls 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 |
| 688 | using @code{call-interactively}, add an optional argument | 721 | from 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}. |
| 690 | call, and use the @code{interactive} spec to make sure it is | ||
| 691 | non-@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 | ||
| 701 | Defined in this way, the function does display the message when called | ||
| 702 | from a keyboard macro. We use @code{"p"} because the numeric prefix | ||
| 703 | argument is never @code{nil}. | ||
| 704 | |||
| 705 | @defun called-interactively-p | ||
| 706 | This function returns @code{t} when the calling function was called | ||
| 707 | using @code{call-interactively}. | ||
| 708 | 723 | ||
| 709 | When possible, instead of using this function, you should use the | 724 | @defun interactive-p |
| 710 | method in the example above; that method makes it possible for a | 725 | This function returns @code{t} if the containing function (the one |
| 711 | caller to ``pretend'' that the function was called interactively. | 726 | whose code includes the call to @code{interactive-p}) was called in |
| 727 | direct response to user input. This means that it was called with the | ||
| 728 | function @code{call-interactively}, and that a keyboard macro is | ||
| 729 | not 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 |