Show
Ignore:
Timestamp:
04/04/08 22:04:40 (8 months ago)
Author:
miyoshi
Message:

Sync up with Emacs22.2.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lispref/commands.texi

    r4200 r4220  
    22@c This is part of the GNU Emacs Lisp Reference Manual. 
    33@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001, 2002, 
    4 @c   2003, 2004, 2005, 2006, 2007  Free Software Foundation, Inc. 
     4@c   2003, 2004, 2005, 2006, 2007, 2008  Free Software Foundation, Inc. 
    55@c See the file elisp.texi for copying conditions. 
    66@setfilename ../info/commands 
     
    1919* Defining Commands::   Specifying how a function should read arguments. 
    2020* Interactive Call::    Calling a command, so that it will read arguments. 
     21* Distinguish Interactive::     Making a command distinguish interactive calls. 
    2122* Command Loop Info::   Variables set by the command loop for you to examine. 
    2223* Adjusting Point::     Adjustment of point after a command. 
     
    636637@end deffn 
    637638 
     639@node Distinguish Interactive 
     640@section Distinguish Interactive Calls 
     641 
     642  Sometimes a command should display additional visual feedback (such 
     643as an informative message in the echo area) for interactive calls 
     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}. 
     670 
     671If 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 
     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 
     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 
     715foobar 
     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 
     721from Lisp programs, test @code{interactive-p} instead of 
     722@code{called-interactively-p}. 
     723 
    638724@defun interactive-p 
    639725This function returns @code{t} if the containing function (the one 
     
    642728function @code{call-interactively}, and that a keyboard macro is 
    643729not running, and that Emacs is not running in batch mode. 
    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. 
    712730@end defun 
    713731 
     
    717735 
    718736The editor command loop sets several Lisp variables to keep status 
    719 records for itself and for commands that are run. 
     737records for itself and for commands that are run.  With the exception of 
     738@code{this-command} and @code{last-command} it's generally a bad idea to 
     739change any of these variables in a Lisp program. 
    720740 
    721741@defvar last-command 
     
    735755This variable is set up by Emacs just like @code{last-command}, 
    736756but never altered by Lisp programs. 
     757@end defvar 
     758 
     759@defvar last-repeatable-command 
     760This variable stores the most recently executed command that was not 
     761part of an input event.  This is the command @code{repeat} will try to 
     762repeat, @xref{Repeating,,, emacs, The GNU Emacs Manual}. 
    737763@end defvar 
    738764 
     
    929955* Classifying Events::          Finding the modifier keys in an event symbol. 
    930956                                Event types. 
    931 * Accessing Events::            Functions to extract info from events. 
     957* Accessing Mouse::             Functions to extract info from mouse events. 
     958* Accessing Scroll::            Functions to get info from scroll bar events. 
    932959* Strings of Events::           Special considerations for putting 
    933960                                  keyboard character events in a string. 
     
    17781805@end defun 
    17791806 
    1780 @node Accessing Events 
    1781 @subsection Accessing Events 
     1807@node Accessing Mouse 
     1808@subsection Accessing Mouse Events 
    17821809@cindex mouse events, data in 
    17831810 
     
    19241951the entire window area including scroll bars, margins and fringes. 
    19251952@end defun 
     1953 
     1954@node Accessing Scroll 
     1955@subsection Accessing Scroll Bar Events 
     1956@cindex scroll bar events, data in 
    19261957 
    19271958  These functions are useful for decoding scroll bar events.