| | 639 | @node Distinguish Interactive |
|---|
| | 640 | @section Distinguish Interactive Calls |
|---|
| | 641 | |
|---|
| | 642 | Sometimes a command should display additional visual feedback (such |
|---|
| | 643 | as an informative message in the echo area) for interactive calls |
|---|
| | 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}. |
|---|
| | 670 | |
|---|
| | 671 | If the containing function was called by Lisp evaluation (or with |
|---|
| | 672 | @code{apply} or @code{funcall}), then it was not called interactively. |
|---|
| | 673 | @end defun |
|---|
| | 674 | |
|---|
| | 675 | Here's an example of using @code{called-interactively-p}: |
|---|
| | 676 | |
|---|
| | 677 | @example |
|---|
| | 678 | @group |
|---|
| | 679 | (defun foo () |
|---|
| | 680 | (interactive) |
|---|
| | 681 | (when (called-interactively-p) |
|---|
| | 682 | (message "foo")) |
|---|
| | 683 | 'haha) |
|---|
| | 684 | @result{} foo |
|---|
| | 685 | @end group |
|---|
| | 686 | |
|---|
| | 687 | @group |
|---|
| | 688 | ;; @r{Type @kbd{M-x foo}.} |
|---|
| | 689 | @print{} foo |
|---|
| | 690 | @end group |
|---|
| | 691 | |
|---|
| | 692 | @group |
|---|
| | 693 | (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 |
|---|
| | 707 | @end group |
|---|
| | 708 | |
|---|
| | 709 | @group |
|---|
| | 710 | ;; @r{Type @kbd{M-x bar}.} |
|---|
| | 711 | ;; @r{This does not display a message.} |
|---|
| | 712 | @end group |
|---|
| | 713 | |
|---|
| | 714 | @group |
|---|
| | 715 | foobar |
|---|
| | 716 | @result{} (nil t) |
|---|
| | 717 | @end group |
|---|
| | 718 | @end example |
|---|
| | 719 | |
|---|
| | 720 | If you want to treat commands run in keyboard macros just like calls |
|---|
| | 721 | from Lisp programs, test @code{interactive-p} instead of |
|---|
| | 722 | @code{called-interactively-p}. |
|---|
| | 723 | |
|---|
| 644 | | |
|---|
| 645 | | If the containing function was called by Lisp evaluation (or with |
|---|
| 646 | | @code{apply} or @code{funcall}), then it was not called interactively. |
|---|
| 647 | | @end defun |
|---|
| 648 | | |
|---|
| 649 | | The most common use of @code{interactive-p} is for deciding whether |
|---|
| 650 | | to give the user additional visual feedback (such as by printing an |
|---|
| 651 | | informative message). For example: |
|---|
| 652 | | |
|---|
| 653 | | @example |
|---|
| 654 | | @group |
|---|
| 655 | | ;; @r{Here's the usual way to use @code{interactive-p}.} |
|---|
| 656 | | (defun foo () |
|---|
| 657 | | (interactive) |
|---|
| 658 | | (when (interactive-p) |
|---|
| 659 | | (message "foo"))) |
|---|
| 660 | | @result{} foo |
|---|
| 661 | | @end group |
|---|
| 662 | | |
|---|
| 663 | | @group |
|---|
| 664 | | ;; @r{This function is just to illustrate the behavior.} |
|---|
| 665 | | (defun bar () |
|---|
| 666 | | (interactive) |
|---|
| 667 | | (setq foobar (list (foo) (interactive-p)))) |
|---|
| 668 | | @result{} bar |
|---|
| 669 | | @end group |
|---|
| 670 | | |
|---|
| 671 | | @group |
|---|
| 672 | | ;; @r{Type @kbd{M-x foo}.} |
|---|
| 673 | | @print{} foo |
|---|
| 674 | | @end group |
|---|
| 675 | | |
|---|
| 676 | | @group |
|---|
| 677 | | ;; @r{Type @kbd{M-x bar}.} |
|---|
| 678 | | ;; @r{This does not display a message.} |
|---|
| 679 | | @end group |
|---|
| 680 | | |
|---|
| 681 | | @group |
|---|
| 682 | | foobar |
|---|
| 683 | | @result{} (nil t) |
|---|
| 684 | | @end group |
|---|
| 685 | | @end example |
|---|
| 686 | | |
|---|
| 687 | | If you want to test @emph{only} whether the function was called |
|---|
| 688 | | using @code{call-interactively}, add an optional argument |
|---|
| 689 | | @code{print-message} which should be non-@code{nil} in an interactive |
|---|
| 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 | | |
|---|
| 709 | | When possible, instead of using this function, you should use the |
|---|
| 710 | | method in the example above; that method makes it possible for a |
|---|
| 711 | | caller to ``pretend'' that the function was called interactively. |
|---|