commands.texi 100 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
3
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. 
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
@c See the file elisp.texi for copying conditions.
@setfilename ../info/commands
@node Command Loop, Keymaps, Minibuffers, Top
@chapter Command Loop
@cindex editor command loop
@cindex command loop

  When you run Emacs, it enters the @dfn{editor command loop} almost
immediately.  This loop reads key sequences, executes their definitions,
and displays the results.  In this chapter, we describe how these things
are done, and the subroutines that allow Lisp programs to do them.  

@menu
* Command Overview::    How the command loop reads commands.
* Defining Commands::   Specifying how a function should read arguments.
* Interactive Call::    Calling a command, so that it will read arguments.
* Command Loop Info::   Variables set by the command loop for you to examine.
* Input Events::	What input looks like when you read it.
* Reading Input::       How to read input events from the keyboard or mouse.
23
* Special Events::      Events processed immediately and individually.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
* Waiting::             Waiting for user input or elapsed time.
* Quitting::            How @kbd{C-g} works.  How to catch or defer quitting.
* Prefix Command Arguments::    How the commands to set prefix args work.
* Recursive Editing::   Entering a recursive edit,
                          and why you usually shouldn't.
* Disabling Commands::  How the command loop handles disabled commands.
* Command History::     How the command history is set up, and how accessed.
* Keyboard Macros::     How keyboard macros are implemented.
@end menu

@node Command Overview
@section Command Loop Overview

  The first thing the command loop must do is read a key sequence, which
is a sequence of events that translates into a command.  It does this by
calling the function @code{read-key-sequence}.  Your Lisp code can also
call this function (@pxref{Key Sequence Input}).  Lisp programs can also
do input at a lower level with @code{read-event} (@pxref{Reading One
Event}) or discard pending input with @code{discard-input}
43
(@pxref{Event Input Misc}).
Richard M. Stallman's avatar
Richard M. Stallman committed
44 45 46 47 48

  The key sequence is translated into a command through the currently
active keymaps.  @xref{Key Lookup}, for information on how this is done.
The result should be a keyboard macro or an interactively callable
function.  If the key is @kbd{M-x}, then it reads the name of another
49
command, which it then calls.  This is done by the command
Richard M. Stallman's avatar
Richard M. Stallman committed
50 51
@code{execute-extended-command} (@pxref{Interactive Call}).

52 53 54 55 56 57 58 59 60 61 62
  To execute a command requires first reading the arguments for it.
This is done by calling @code{command-execute} (@pxref{Interactive
Call}).  For commands written in Lisp, the @code{interactive}
specification says how to read the arguments.  This may use the prefix
argument (@pxref{Prefix Command Arguments}) or may read with prompting
in the minibuffer (@pxref{Minibuffers}).  For example, the command
@code{find-file} has an @code{interactive} specification which says to
read a file name using the minibuffer.  The command's function body does
not use the minibuffer; if you call this command from Lisp code as a
function, you must supply the file name string as an ordinary Lisp
function argument.
Richard M. Stallman's avatar
Richard M. Stallman committed
63 64 65 66 67

  If the command is a string or vector (i.e., a keyboard macro) then
@code{execute-kbd-macro} is used to execute it.  You can call this
function yourself (@pxref{Keyboard Macros}).

68 69
  To terminate the execution of a running command, type @kbd{C-g}.  This
character causes @dfn{quitting} (@pxref{Quitting}).
Richard M. Stallman's avatar
Richard M. Stallman committed
70 71

@defvar pre-command-hook
72 73 74
The editor command loop runs this normal hook before each command.  At
that time, @code{this-command} contains the command that is about to
run, and @code{last-command} describes the previous command.
75
@xref{Hooks}.
Richard M. Stallman's avatar
Richard M. Stallman committed
76 77 78
@end defvar

@defvar post-command-hook
79 80 81 82
The editor command loop runs this normal hook after each command
(including commands terminated prematurely by quitting or by errors),
and also when the command loop is first entered.  At that time,
@code{this-command} describes the command that just ran, and
83
@code{last-command} describes the command before that.  @xref{Hooks}.
Richard M. Stallman's avatar
Richard M. Stallman committed
84 85
@end defvar

86
  Quitting is suppressed while running @code{pre-command-hook} and
Karl Heuer's avatar
Karl Heuer committed
87
@code{post-command-hook}.  If an error happens while executing one of
88 89
these hooks, it terminates execution of the hook, and clears the hook
variable to @code{nil} so as to prevent an infinite loop of errors.
90

Richard M. Stallman's avatar
Richard M. Stallman committed
91 92 93 94 95 96 97 98
@node Defining Commands
@section Defining Commands
@cindex defining commands
@cindex commands, defining
@cindex functions, making them interactive
@cindex interactive function

  A Lisp function becomes a command when its body contains, at top
99
level, a form that calls the special form @code{interactive}.  This
Richard M. Stallman's avatar
Richard M. Stallman committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
form does nothing when actually executed, but its presence serves as a
flag to indicate that interactive calling is permitted.  Its argument
controls the reading of arguments for an interactive call.

@menu
* Using Interactive::     General rules for @code{interactive}.
* Interactive Codes::     The standard letter-codes for reading arguments
                             in various ways.
* Interactive Examples::  Examples of how to read interactive arguments.
@end menu

@node Using Interactive
@subsection Using @code{interactive}

  This section describes how to write the @code{interactive} form that
makes a Lisp function an interactively-callable command.

@defspec interactive arg-descriptor
@cindex argument descriptors
This special form declares that the function in which it appears is a
command, and that it may therefore be called interactively (via
@kbd{M-x} or by entering a key sequence bound to it).  The argument
122 123
@var{arg-descriptor} declares how to compute the arguments to the
command when the command is called interactively.
Richard M. Stallman's avatar
Richard M. Stallman committed
124 125

A command may be called from Lisp programs like any other function, but
126 127
then the caller supplies the arguments and @var{arg-descriptor} has no
effect.
Richard M. Stallman's avatar
Richard M. Stallman committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

The @code{interactive} form has its effect because the command loop
(actually, its subroutine @code{call-interactively}) scans through the
function definition looking for it, before calling the function.  Once
the function is called, all its body forms including the
@code{interactive} form are executed, but at this time
@code{interactive} simply returns @code{nil} without even evaluating its
argument.
@end defspec

There are three possibilities for the argument @var{arg-descriptor}:

@itemize @bullet
@item
It may be omitted or @code{nil}; then the command is called with no
arguments.  This leads quickly to an error if the command requires one
or more arguments.

@item
It may be a Lisp expression that is not a string; then it should be a
form that is evaluated to get a list of arguments to pass to the
command.
@cindex argument evaluation form

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
If this expression reads keyboard input (this includes using the
minibuffer), keep in mind that the integer value of point or the mark
before reading input may be incorrect after reading input.  This is
because the current buffer may be receiving subprocess output;
if subprocess output arrives while the command is waiting for input,
it could relocate point and the mark.

Here's an example of what @emph{not} to do:

@smallexample
(interactive
 (list (region-beginning) (region-end)
       (read-string "Foo: " nil 'my-history)))
@end smallexample

@noindent
Here's how to avoid the problem, by examining point and the mark only
after reading the keyboard input:

@smallexample
(interactive
 (let ((string (read-string "Foo: " nil 'my-history)))
   (list (region-beginning) (region-end) string)))
@end smallexample

Richard M. Stallman's avatar
Richard M. Stallman committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
@item
@cindex argument prompt
It may be a string; then its contents should consist of a code character
followed by a prompt (which some code characters use and some ignore).
The prompt ends either with the end of the string or with a newline.
Here is a simple example:

@smallexample
(interactive "bFrobnicate buffer: ")
@end smallexample

@noindent
The code letter @samp{b} says to read the name of an existing buffer,
with completion.  The buffer name is the sole argument passed to the
command.  The rest of the string is a prompt.

If there is a newline character in the string, it terminates the prompt.
If the string does not end there, then the rest of the string should
contain another code character and prompt, specifying another argument.
You can specify any number of arguments in this way.

@c Emacs 19 feature
The prompt string can use @samp{%} to include previous argument values
200 201 202 203
(starting with the first argument) in the prompt.  This is done using
@code{format} (@pxref{Formatting Strings}).  For example, here is how
you could read the name of an existing buffer followed by a new name to
give to that buffer:
Richard M. Stallman's avatar
Richard M. Stallman committed
204 205 206 207 208 209 210 211

@smallexample
@group
(interactive "bBuffer to rename: \nsRename buffer %s to: ")
@end group
@end smallexample

@cindex @samp{*} in interactive
212
@cindex read-only buffers in interactive
Richard M. Stallman's avatar
Richard M. Stallman committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
If the first character in the string is @samp{*}, then an error is
signaled if the buffer is read-only.

@cindex @samp{@@} in interactive
@c Emacs 19 feature
If the first character in the string is @samp{@@}, and if the key
sequence used to invoke the command includes any mouse events, then
the window associated with the first of those events is selected
before the command is run.

You can use @samp{*} and @samp{@@} together; the order does not matter.
Actual reading of arguments is controlled by the rest of the prompt
string (starting with the first character that is not @samp{*} or
@samp{@@}).
@end itemize

@node Interactive Codes
@comment  node-name,  next,  previous,  up
@subsection Code Characters for @code{interactive}
@cindex interactive code description
@cindex description for interactive codes
@cindex codes, interactive, description of
@cindex characters for interactive codes

  The code character descriptions below contain a number of key words,
defined here as follows:

@table @b
@item Completion
@cindex interactive completion
Provide completion.  @key{TAB}, @key{SPC}, and @key{RET} perform name
completion because the argument is read using @code{completing-read}
(@pxref{Completion}).  @kbd{?} displays a list of possible completions.

@item Existing
Require the name of an existing object.  An invalid name is not
accepted; the commands to exit the minibuffer do not exit if the current
input is not valid.

@item Default
@cindex default argument string
A default value of some sort is used if the user enters no text in the
minibuffer.  The default depends on the code character.

@item No I/O
This code letter computes an argument without reading any input.
Therefore, it does not use a prompt string, and any prompt string you
supply is ignored.

262 263 264
Even though the code letter doesn't use a prompt string, you must follow
it with a newline if it is not the last code character in the string.

Richard M. Stallman's avatar
Richard M. Stallman committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
@item Prompt
A prompt immediately follows the code character.  The prompt ends either
with the end of the string or with a newline.

@item Special
This code character is meaningful only at the beginning of the
interactive string, and it does not look for a prompt or a newline.
It is a single, isolated character.
@end table

@cindex reading interactive arguments
  Here are the code character descriptions for use with @code{interactive}:

@table @samp
@item *
Signal an error if the current buffer is read-only.  Special.

@item @@
Select the window mentioned in the first mouse event in the key
sequence that invoked this command.  Special.

@item a
287
A function name (i.e., a symbol satisfying @code{fboundp}).  Existing,
Richard M. Stallman's avatar
Richard M. Stallman committed
288 289 290 291 292 293 294 295 296 297
Completion, Prompt.

@item b
The name of an existing buffer.  By default, uses the name of the
current buffer (@pxref{Buffers}).  Existing, Completion, Default,
Prompt.

@item B
A buffer name.  The buffer need not exist.  By default, uses the name of
a recently used buffer other than the current buffer.  Completion,
298
Default, Prompt.
Richard M. Stallman's avatar
Richard M. Stallman committed
299 300 301 302 303 304 305 306 307 308

@item c
A character.  The cursor does not move into the echo area.  Prompt.

@item C
A command name (i.e., a symbol satisfying @code{commandp}).  Existing,
Completion, Prompt.

@item d
@cindex position argument
309
The position of point, as an integer (@pxref{Point}).  No I/O.
Richard M. Stallman's avatar
Richard M. Stallman committed
310 311 312 313 314 315 316 317

@item D
A directory name.  The default is the current default directory of the
current buffer, @code{default-directory} (@pxref{System Environment}).
Existing, Completion, Default, Prompt.

@item e
The first or next mouse event in the key sequence that invoked the command.
318
More precisely, @samp{e} gets events that are lists, so you can look at
Richard M. Stallman's avatar
Richard M. Stallman committed
319 320 321
the data in the lists.  @xref{Input Events}.  No I/O.

You can use @samp{e} more than once in a single command's interactive
322
specification.  If the key sequence that invoked the command has
323
@var{n} events that are lists, the @var{n}th @samp{e} provides the
324
@var{n}th such event.  Events that are not lists, such as function keys
325
and @sc{ascii} characters, do not count where @samp{e} is concerned.
Richard M. Stallman's avatar
Richard M. Stallman committed
326 327 328 329 330 331 332 333 334

@item f
A file name of an existing file (@pxref{File Names}).  The default
directory is @code{default-directory}.  Existing, Completion, Default,
Prompt.

@item F
A file name.  The file need not exist.  Completion, Default, Prompt.

335 336 337 338
@item i
An irrelevant argument.  This code always supplies @code{nil} as
the argument's value.  No I/O.

Richard M. Stallman's avatar
Richard M. Stallman committed
339 340 341 342 343 344 345 346 347
@item k
A key sequence (@pxref{Keymap Terminology}).  This keeps reading events
until a command (or undefined command) is found in the current key
maps.  The key sequence argument is represented as a string or vector.
The cursor does not move into the echo area.  Prompt.

This kind of input is used by commands such as @code{describe-key} and
@code{global-set-key}.

Karl Heuer's avatar
Karl Heuer committed
348 349 350 351 352 353
@item K
A key sequence, whose definition you intend to change.  This works like
@samp{k}, except that it suppresses, for the last input event in the key
sequence, the conversions that are normally used (when necessary) to
convert an undefined key into a defined one.

Richard M. Stallman's avatar
Richard M. Stallman committed
354 355
@item m
@cindex marker argument
356
The position of the mark, as an integer.  No I/O.
Richard M. Stallman's avatar
Richard M. Stallman committed
357

358 359 360 361 362
@item M
Arbitrary text, read in the minibuffer using the current buffer's input
method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU
Emacs Manual}).  Prompt.

Richard M. Stallman's avatar
Richard M. Stallman committed
363 364 365 366 367 368 369
@item n
A number read with the minibuffer.  If the input is not a number, the
user is asked to try again.  The prefix argument, if any, is not used.
Prompt.

@item N
@cindex raw prefix argument usage
370 371
The numeric prefix argument; but if there is no prefix argument, read a
number as with @kbd{n}.  Requires a number.  @xref{Prefix Command
372
Arguments}.  Prompt.
Richard M. Stallman's avatar
Richard M. Stallman committed
373 374 375 376

@item p
@cindex numeric prefix argument usage
The numeric prefix argument.  (Note that this @samp{p} is lower case.)
377
No I/O.
Richard M. Stallman's avatar
Richard M. Stallman committed
378 379

@item P
380 381
The raw prefix argument.  (Note that this @samp{P} is upper case.)  No
I/O.
Richard M. Stallman's avatar
Richard M. Stallman committed
382 383 384 385 386 387 388 389 390 391

@item r
@cindex region argument
Point and the mark, as two numeric arguments, smallest first.  This is
the only code letter that specifies two successive arguments rather than
one.  No I/O.

@item s
Arbitrary text, read in the minibuffer and returned as a string
(@pxref{Text from Minibuffer}).  Terminate the input with either
392
@kbd{C-j} or @key{RET}.  (@kbd{C-q} may be used to include either of
Richard M. Stallman's avatar
Richard M. Stallman committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406
these characters in the input.)  Prompt.

@item S
An interned symbol whose name is read in the minibuffer.  Any whitespace
character terminates the input.  (Use @kbd{C-q} to include whitespace in
the string.)  Other characters that normally terminate a symbol (e.g.,
parentheses and brackets) do not do so here.  Prompt.

@item v
A variable declared to be a user option (i.e., satisfying the predicate
@code{user-variable-p}).  @xref{High-Level Completion}.  Existing,
Completion, Prompt.

@item x
407
A Lisp object, specified with its read syntax, terminated with a
408
@kbd{C-j} or @key{RET}.  The object is not evaluated.  @xref{Object from
Richard M. Stallman's avatar
Richard M. Stallman committed
409 410 411 412 413 414
Minibuffer}.  Prompt.

@item X
@cindex evaluated expression argument
A Lisp form is read as with @kbd{x}, but then evaluated so that its
value becomes the argument for the command.  Prompt.
415 416 417 418 419 420 421 422 423 424

@item z
A coding system name (a symbol).  If the user enters null input, the
argument value is @code{nil}.  @xref{Coding Systems}.  Completion,
Existing, Prompt.

@item Z
A coding system name (a symbol)---but only if this command has a prefix
argument.  With no prefix argument, @samp{Z} provides @code{nil} as the
argument value.  Completion, Existing, Prompt.
Richard M. Stallman's avatar
Richard M. Stallman committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
@end table

@node Interactive Examples
@comment  node-name,  next,  previous,  up
@subsection Examples of Using @code{interactive}
@cindex examples of using @code{interactive}
@cindex @code{interactive}, examples of using 

  Here are some examples of @code{interactive}:

@example
@group
(defun foo1 ()              ; @r{@code{foo1} takes no arguments,}
    (interactive)           ;   @r{just moves forward two words.}
    (forward-word 2))
     @result{} foo1
@end group

@group
(defun foo2 (n)             ; @r{@code{foo2} takes one argument,}
    (interactive "p")       ;   @r{which is the numeric prefix.}
    (forward-word (* 2 n)))
     @result{} foo2
@end group

@group
(defun foo3 (n)             ; @r{@code{foo3} takes one argument,}
    (interactive "nCount:") ;   @r{which is read with the Minibuffer.}
    (forward-word (* 2 n)))
     @result{} foo3
@end group

@group
(defun three-b (b1 b2 b3)
  "Select three existing buffers.
Put them into three windows, selecting the last one."
@end group
    (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
    (delete-other-windows)
    (split-window (selected-window) 8)
    (switch-to-buffer b1)
    (other-window 1)
    (split-window (selected-window) 8)
    (switch-to-buffer b2)
    (other-window 1)
    (switch-to-buffer b3))
     @result{} three-b
@group
(three-b "*scratch*" "declarations.texi" "*mail*")
     @result{} nil
@end group
@end example

@node Interactive Call
@section Interactive Call
@cindex interactive call

482 483 484 485 486
  After the command loop has translated a key sequence into a command it
invokes that command using the function @code{command-execute}.  If the
command is a function, @code{command-execute} calls
@code{call-interactively}, which reads the arguments and calls the
command.  You can also call these functions yourself.
Richard M. Stallman's avatar
Richard M. Stallman committed
487 488 489 490 491 492 493

@defun commandp object
Returns @code{t} if @var{object} is suitable for calling interactively;
that is, if @var{object} is a command.  Otherwise, returns @code{nil}.  

The interactively callable objects include strings and vectors (treated
as keyboard macros), lambda expressions that contain a top-level call to
Karl Heuer's avatar
Karl Heuer committed
494
@code{interactive}, byte-code function objects made from such lambda
495 496 497
expressions, autoload objects that are declared as interactive
(non-@code{nil} fourth argument to @code{autoload}), and some of the
primitive functions.
Richard M. Stallman's avatar
Richard M. Stallman committed
498

499
A symbol satisfies @code{commandp} if its function definition satisfies
Richard M. Stallman's avatar
Richard M. Stallman committed
500 501 502 503 504 505 506 507 508
@code{commandp}.

Keys and keymaps are not commands.  Rather, they are used to look up
commands (@pxref{Keymaps}).

See @code{documentation} in @ref{Accessing Documentation}, for a
realistic example of using @code{commandp}.
@end defun

509
@defun call-interactively command &optional record-flag keys
Richard M. Stallman's avatar
Richard M. Stallman committed
510 511
This function calls the interactively callable function @var{command},
reading arguments according to its interactive calling specifications.
512 513 514 515
An error is signaled if @var{command} is not a function or if it cannot
be called interactively (i.e., is not a command).  Note that keyboard
macros (strings and vectors) are not accepted, even though they are
considered commands, because they are not functions.
Richard M. Stallman's avatar
Richard M. Stallman committed
516 517 518 519 520 521

@cindex record command history
If @var{record-flag} is non-@code{nil}, then this command and its
arguments are unconditionally added to the list @code{command-history}.
Otherwise, the command is added only if it uses the minibuffer to read
an argument.  @xref{Command History}.
522 523

The argument @var{keys}, if given, specifies the sequence of events to
524
supply if the command inquires which events were used to invoke it.
Richard M. Stallman's avatar
Richard M. Stallman committed
525 526
@end defun

527
@defun command-execute command &optional record-flag keys
Richard M. Stallman's avatar
Richard M. Stallman committed
528
@cindex keyboard macro execution
529 530 531
This function executes @var{command}.  The argument @var{command} must
satisfy the @code{commandp} predicate; i.e., it must be an interactively
callable function or a keyboard macro.
Richard M. Stallman's avatar
Richard M. Stallman committed
532 533 534 535 536 537 538 539 540 541

A string or vector as @var{command} is executed with
@code{execute-kbd-macro}.  A function is passed to
@code{call-interactively}, along with the optional @var{record-flag}.

A symbol is handled by using its function definition in its place.  A
symbol with an @code{autoload} definition counts as a command if it was
declared to stand for an interactively callable function.  Such a
definition is handled by loading the specified library and then
rechecking the definition of the symbol.
542 543

The argument @var{keys}, if given, specifies the sequence of events to
544
supply if the command inquires which events were used to invoke it.
Richard M. Stallman's avatar
Richard M. Stallman committed
545 546 547 548 549 550 551 552 553 554
@end defun

@deffn Command execute-extended-command prefix-argument
@cindex read command name
This function reads a command name from the minibuffer using
@code{completing-read} (@pxref{Completion}).  Then it uses
@code{command-execute} to call the specified command.  Whatever that
command returns becomes the value of @code{execute-extended-command}.

@cindex execute with prefix argument
555 556 557
If the command asks for a prefix argument, it receives the value
@var{prefix-argument}.  If @code{execute-extended-command} is called
interactively, the current raw prefix argument is used for
Richard M. Stallman's avatar
Richard M. Stallman committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
@var{prefix-argument}, and thus passed on to whatever command is run.

@c !!! Should this be @kindex?
@cindex @kbd{M-x}
@code{execute-extended-command} is the normal definition of @kbd{M-x},
so it uses the string @w{@samp{M-x }} as a prompt.  (It would be better
to take the prompt from the events used to invoke
@code{execute-extended-command}, but that is painful to implement.)  A
description of the value of the prefix argument, if any, also becomes
part of the prompt.

@example
@group
(execute-extended-command 1)
---------- Buffer: Minibuffer ----------
573
1 M-x forward-word RET
Richard M. Stallman's avatar
Richard M. Stallman committed
574 575 576 577 578 579 580
---------- Buffer: Minibuffer ----------
     @result{} t
@end group
@end example
@end deffn

@defun interactive-p
581 582 583 584 585 586 587
This function returns @code{t} if the containing function (the one whose
code includes the call to @code{interactive-p}) was called
interactively, with the function @code{call-interactively}.  (It makes
no difference whether @code{call-interactively} was called from Lisp or
directly from the editor command loop.)  If the containing function was
called by Lisp evaluation (or with @code{apply} or @code{funcall}), then
it was not called interactively.
588
@end defun
Richard M. Stallman's avatar
Richard M. Stallman committed
589

590
  The most common use of @code{interactive-p} is for deciding whether to
Richard M. Stallman's avatar
Richard M. Stallman committed
591 592 593 594 595
print an informative message.  As a special exception,
@code{interactive-p} returns @code{nil} whenever a keyboard macro is
being run.  This is to suppress the informative messages and speed
execution of the macro.

596
  For example:
Richard M. Stallman's avatar
Richard M. Stallman committed
597 598 599 600 601

@example
@group
(defun foo ()
  (interactive)
602 603
  (when (interactive-p)
    (message "foo")))
Richard M. Stallman's avatar
Richard M. Stallman committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
     @result{} foo
@end group

@group
(defun bar ()
  (interactive)
  (setq foobar (list (foo) (interactive-p))))
     @result{} bar
@end group

@group
;; @r{Type @kbd{M-x foo}.}
     @print{} foo
@end group

@group
;; @r{Type @kbd{M-x bar}.}
;; @r{This does not print anything.}
@end group

@group
foobar
     @result{} (nil t)
@end group
@end example
629 630 631 632 633 634 635 636 637 638 639 640 641 642

  The other way to do this sort of job is to make the command take an
argument @code{print-message} which should be non-@code{nil} in an
interactive call, and use the @code{interactive} spec to make sure it is
non-@code{nil}.  Here's how:

@example
(defun foo (&optional print-message)
  (interactive "p")
  (when print-message
    (message "foo")))
@end example

  The numeric prefix argument, provided by @samp{p}, is never @code{nil}.
Richard M. Stallman's avatar
Richard M. Stallman committed
643 644 645 646 647 648 649 650 651 652 653 654 655

@node Command Loop Info
@comment  node-name,  next,  previous,  up
@section Information from the Command Loop

The editor command loop sets several Lisp variables to keep status
records for itself and for commands that are run.  

@defvar last-command
This variable records the name of the previous command executed by the
command loop (the one before the current command).  Normally the value
is a symbol with a function definition, but this is not guaranteed.

656
The value is copied from @code{this-command} when a command returns to
657 658
the command loop, except when the command has specified a prefix
argument for the following command.
Karl Heuer's avatar
Karl Heuer committed
659 660 661

This variable is always local to the current terminal and cannot be
buffer-local.  @xref{Multiple Displays}.
Richard M. Stallman's avatar
Richard M. Stallman committed
662 663
@end defvar

664 665 666 667 668 669
@tindex real-last-command
@defvar real-last-command
This variable is set up by Emacs just like @code{last-command},
but never altered by Lisp programs.
@end defvar

Richard M. Stallman's avatar
Richard M. Stallman committed
670 671 672 673 674 675
@defvar this-command
@cindex current command
This variable records the name of the command now being executed by
the editor command loop.  Like @code{last-command}, it is normally a symbol
with a function definition.

676 677
The command loop sets this variable just before running a command, and
copies its value into @code{last-command} when the command finishes
678
(unless the command specified a prefix argument for the following
679
command).
Richard M. Stallman's avatar
Richard M. Stallman committed
680 681

@cindex kill command repetition
682
Some commands set this variable during their execution, as a flag for
Karl Heuer's avatar
Karl Heuer committed
683
whatever command runs next.  In particular, the functions for killing text
684 685 686
set @code{this-command} to @code{kill-region} so that any kill commands
immediately following will know to append the killed text to the
previous kill.
Richard M. Stallman's avatar
Richard M. Stallman committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
@end defvar

If you do not want a particular command to be recognized as the previous
command in the case where it got an error, you must code that command to
prevent this.  One way is to set @code{this-command} to @code{t} at the
beginning of the command, and set @code{this-command} back to its proper
value at the end, like this:

@example
(defun foo (args@dots{})
  (interactive @dots{})
  (let ((old-this-command this-command))
    (setq this-command t)
    @r{@dots{}do the work@dots{}}
    (setq this-command old-this-command)))
@end example

704 705 706 707 708
@noindent
We do not bind @code{this-command} with @code{let} because that would
restore the old value in case of error---a feature of @code{let} which
in this case does precisely what we want to avoid.

Richard M. Stallman's avatar
Richard M. Stallman committed
709 710 711 712 713 714 715 716 717
@defun this-command-keys
This function returns a string or vector containing the key sequence
that invoked the present command, plus any previous commands that
generated the prefix argument for this command.  The value is a string
if all those events were characters.  @xref{Input Events}.

@example
@group
(this-command-keys)
718
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
Richard M. Stallman's avatar
Richard M. Stallman committed
719 720 721 722 723
     @result{} "^U^X^E"
@end group
@end example
@end defun

724 725 726 727 728 729
@defun this-command-keys-vector
Like @code{this-command-keys}, except that it always returns
the events in a vector, so you do never need to deal with the complexities
of storing input events in a string (@pxref{Strings of Events}).
@end defun

730 731 732 733 734 735 736 737
@tindex clear-this-command-keys
@defun clear-this-command-keys
This function empties out the table of events for
@code{this-command-keys} to return.  This is useful after reading a
password, to prevent the password from echoing inadvertently as part of
the next command in certain cases.
@end defun

Richard M. Stallman's avatar
Richard M. Stallman committed
738
@defvar last-nonmenu-event
739 740
This variable holds the last input event read as part of a key sequence,
not counting events resulting from mouse menus.
Richard M. Stallman's avatar
Richard M. Stallman committed
741

742
One use of this variable is for telling @code{x-popup-menu} where to pop
743 744
up a menu.  It is also used internally by @code{y-or-n-p}
(@pxref{Yes-or-No Queries}).
Richard M. Stallman's avatar
Richard M. Stallman committed
745 746 747 748 749 750 751 752 753 754 755
@end defvar

@defvar last-command-event
@defvarx last-command-char
This variable is set to the last input event that was read by the
command loop as part of a command.  The principal use of this variable
is in @code{self-insert-command}, which uses it to decide which
character to insert.

@example
@group
756
last-command-event
757
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
Richard M. Stallman's avatar
Richard M. Stallman committed
758 759 760 761 762
     @result{} 5
@end group
@end example

@noindent
763
The value is 5 because that is the @sc{ascii} code for @kbd{C-e}.
Richard M. Stallman's avatar
Richard M. Stallman committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

The alias @code{last-command-char} exists for compatibility with
Emacs version 18.
@end defvar

@c Emacs 19 feature
@defvar last-event-frame
This variable records which frame the last input event was directed to.
Usually this is the frame that was selected when the event was
generated, but if that frame has redirected input focus to another
frame, the value is the frame to which the event was redirected.
@xref{Input Focus}.
@end defvar

@node Input Events
@section Input Events
@cindex events
@cindex input events

The Emacs command loop reads a sequence of @dfn{input events} that
represent keyboard or mouse activity.  The events for keyboard activity
are characters or symbols; mouse events are always lists.  This section
describes the representation and meaning of input events in detail.

@defun eventp object
789 790 791 792 793 794 795 796 797
This function returns non-@code{nil} if @var{object} is an input event
or event type.

Note that any symbol might be used as an event or an event type.
@code{eventp} cannot distinguish whether a symbol is intended by Lisp
code to be used as an event.  Instead, it distinguishes whether the
symbol has actually been used in an event that has been read as input in
the current Emacs session.  If a symbol has not yet been so used,
@code{eventp} returns @code{nil}.
Richard M. Stallman's avatar
Richard M. Stallman committed
798 799 800 801 802
@end defun

@menu
* Keyboard Events::		Ordinary characters--keys with symbols on them.
* Function Keys::		Function keys--keys with names, not symbols.
803
* Mouse Events::                Overview of mouse events.
Richard M. Stallman's avatar
Richard M. Stallman committed
804 805 806 807 808 809
* Click Events::		Pushing and releasing a mouse button.
* Drag Events::			Moving the mouse before releasing the button.
* Button-Down Events::		A button was pushed and not yet released.
* Repeat Events::               Double and triple click (or drag, or down).
* Motion Events::		Just moving the mouse, not pushing a button.
* Focus Events::		Moving the mouse between frames.
Karl Heuer's avatar
Karl Heuer committed
810
* Misc Events::                 Other events window systems can generate.
Richard M. Stallman's avatar
Richard M. Stallman committed
811 812 813 814 815 816 817 818 819 820 821 822 823
* Event Examples::		Examples of the lists for mouse events.
* Classifying Events::		Finding the modifier keys in an event symbol.
				Event types.
* Accessing Events::		Functions to extract info from events.
* Strings of Events::           Special considerations for putting
				  keyboard character events in a string.
@end menu

@node Keyboard Events
@subsection Keyboard Events

There are two kinds of input you can get from the keyboard: ordinary
keys, and function keys.  Ordinary keys correspond to characters; the
824 825 826
events they generate are represented in Lisp as characters.  The event
type of a character event is the character itself (an integer); see
@ref{Classifying Events}.
Richard M. Stallman's avatar
Richard M. Stallman committed
827 828 829 830

@cindex modifier bits (of input character)
@cindex basic code (of input character)
An input character event consists of a @dfn{basic code} between 0 and
831
524287, plus any or all of these @dfn{modifier bits}:
Richard M. Stallman's avatar
Richard M. Stallman committed
832 833 834

@table @asis
@item meta
Karl Heuer's avatar
Karl Heuer committed
835
The
836
@tex
837
@math{2^{27}}
838
@end tex
Karl Heuer's avatar
Karl Heuer committed
839 840 841 842
@ifinfo
2**27
@end ifinfo
bit in the character code indicates a character
Richard M. Stallman's avatar
Richard M. Stallman committed
843 844 845
typed with the meta key held down.

@item control
Karl Heuer's avatar
Karl Heuer committed
846
The
847
@tex
848
@math{2^{26}}
849
@end tex
Karl Heuer's avatar
Karl Heuer committed
850 851 852
@ifinfo
2**26
@end ifinfo
853
bit in the character code indicates a non-@sc{ascii}
Richard M. Stallman's avatar
Richard M. Stallman committed
854 855
control character.

856
@sc{ascii} control characters such as @kbd{C-a} have special basic
Richard M. Stallman's avatar
Richard M. Stallman committed
857 858 859
codes of their own, so Emacs needs no special bit to indicate them.
Thus, the code for @kbd{C-a} is just 1.

860
But if you type a control combination not in @sc{ascii}, such as
Richard M. Stallman's avatar
Richard M. Stallman committed
861
@kbd{%} with the control key, the numeric value you get is the code
Karl Heuer's avatar
Karl Heuer committed
862
for @kbd{%} plus
863
@tex
864
@math{2^{26}}
865
@end tex
Karl Heuer's avatar
Karl Heuer committed
866 867 868
@ifinfo
2**26
@end ifinfo
869
(assuming the terminal supports non-@sc{ascii}
Richard M. Stallman's avatar
Richard M. Stallman committed
870 871 872
control characters).

@item shift
Karl Heuer's avatar
Karl Heuer committed
873
The
874
@tex
875
@math{2^{25}}
876
@end tex
Karl Heuer's avatar
Karl Heuer committed
877 878 879
@ifinfo
2**25
@end ifinfo
880
bit in the character code indicates an @sc{ascii} control
Richard M. Stallman's avatar
Richard M. Stallman committed
881 882
character typed with the shift key held down.

883 884 885
For letters, the basic code itself indicates upper versus lower case;
for digits and punctuation, the shift key selects an entirely different
character with a different basic code.  In order to keep within the
886
@sc{ascii} character set whenever possible, Emacs avoids using the
887
@tex
888
@math{2^{25}}
889
@end tex
Karl Heuer's avatar
Karl Heuer committed
890 891 892 893
@ifinfo
2**25
@end ifinfo
bit for those characters.
Richard M. Stallman's avatar
Richard M. Stallman committed
894

895
However, @sc{ascii} provides no way to distinguish @kbd{C-A} from
Karl Heuer's avatar
Karl Heuer committed
896
@kbd{C-a}, so Emacs uses the
897
@tex
898
@math{2^{25}}
899
@end tex
Karl Heuer's avatar
Karl Heuer committed
900 901 902 903
@ifinfo
2**25
@end ifinfo
bit in @kbd{C-A} and not in
Richard M. Stallman's avatar
Richard M. Stallman committed
904 905 906
@kbd{C-a}.

@item hyper
Karl Heuer's avatar
Karl Heuer committed
907
The
908
@tex
909
@math{2^{24}}
910
@end tex
Karl Heuer's avatar
Karl Heuer committed
911 912 913 914
@ifinfo
2**24
@end ifinfo
bit in the character code indicates a character
Richard M. Stallman's avatar
Richard M. Stallman committed
915 916 917
typed with the hyper key held down.

@item super
Karl Heuer's avatar
Karl Heuer committed
918
The
919
@tex
920
@math{2^{23}}
921
@end tex
Karl Heuer's avatar
Karl Heuer committed
922 923 924 925
@ifinfo
2**23
@end ifinfo
bit in the character code indicates a character
Richard M. Stallman's avatar
Richard M. Stallman committed
926 927 928
typed with the super key held down.

@item alt
Karl Heuer's avatar
Karl Heuer committed
929
The
930
@tex
931
@math{2^{22}}
932
@end tex
Karl Heuer's avatar
Karl Heuer committed
933 934 935 936
@ifinfo
2**22
@end ifinfo
bit in the character code indicates a character typed with
Richard M. Stallman's avatar
Richard M. Stallman committed
937 938 939 940
the alt key held down.  (On some terminals, the key labeled @key{ALT}
is actually the meta key.)
@end table

Karl Heuer's avatar
Karl Heuer committed
941 942 943 944 945 946 947 948 949
  It is best to avoid mentioning specific bit numbers in your program.
To test the modifier bits of a character, use the function
@code{event-modifiers} (@pxref{Classifying Events}).  When making key
bindings, you can use the read syntax for characters with modifier bits
(@samp{\C-}, @samp{\M-}, and so on).  For making key bindings with
@code{define-key}, you can use lists such as @code{(control hyper ?x)} to
specify the characters (@pxref{Changing Key Bindings}).  The function
@code{event-convert-list} converts such a list into an event type
(@pxref{Classifying Events}).
Richard M. Stallman's avatar
Richard M. Stallman committed
950 951 952 953 954

@node Function Keys
@subsection Function Keys

@cindex function keys
955
Most keyboards also have @dfn{function keys}---keys that have names or
956 957
symbols that are not characters.  Function keys are represented in Emacs
Lisp as symbols; the symbol's name is the function key's label, in lower
958 959
case.  For example, pressing a key labeled @key{F1} places the symbol
@code{f1} in the input stream.
Richard M. Stallman's avatar
Richard M. Stallman committed
960

961 962
The event type of a function key event is the event symbol itself.
@xref{Classifying Events}.
Richard M. Stallman's avatar
Richard M. Stallman committed
963

964
Here are a few special cases in the symbol-naming convention for
Richard M. Stallman's avatar
Richard M. Stallman committed
965 966 967 968
function keys:

@table @asis
@item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete}
969
These keys correspond to common @sc{ascii} control characters that have
Richard M. Stallman's avatar
Richard M. Stallman committed
970 971
special keys on most keyboards.

972
In @sc{ascii}, @kbd{C-i} and @key{TAB} are the same character.  If the
973 974 975
terminal can distinguish between them, Emacs conveys the distinction to
Lisp programs by representing the former as the integer 9, and the
latter as the symbol @code{tab}.
Richard M. Stallman's avatar
Richard M. Stallman committed
976 977

Most of the time, it's not useful to distinguish the two.  So normally
Richard M. Stallman's avatar
Richard M. Stallman committed
978 979 980 981 982
@code{function-key-map} (@pxref{Translating Input}) is set up to map
@code{tab} into 9.  Thus, a key binding for character code 9 (the
character @kbd{C-i}) also applies to @code{tab}.  Likewise for the other
symbols in this group.  The function @code{read-char} likewise converts
these events into characters.
Richard M. Stallman's avatar
Richard M. Stallman committed
983

984
In @sc{ascii}, @key{BS} is really @kbd{C-h}.  But @code{backspace}
Richard M. Stallman's avatar
Richard M. Stallman committed
985 986 987
converts into the character code 127 (@key{DEL}), not into code 8
(@key{BS}).  This is what most users prefer.

988 989
@item @code{left}, @code{up}, @code{right}, @code{down}
Cursor arrow keys
Richard M. Stallman's avatar
Richard M. Stallman committed
990 991 992 993 994 995
@item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{}
Keypad keys (to the right of the regular keyboard).
@item @code{kp-0}, @code{kp-1}, @dots{}
Keypad keys with digits.
@item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
Keypad PF keys.
996
@item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down}
997 998
Keypad arrow keys.  Emacs normally translates these into the
corresponding non-keypad keys @code{home}, @code{left}, @dots{}
999 1000 1001
@item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete}
Additional keypad duplicates of keys ordinarily found elsewhere.  Emacs
normally translates these into the like-named non-keypad keys.
Richard M. Stallman's avatar
Richard M. Stallman committed
1002 1003
@end table

1004 1005 1006
You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER},
@key{META}, @key{SHIFT}, and @key{SUPER} with function keys.  The way to
represent them is with prefixes in the symbol name:
Richard M. Stallman's avatar
Richard M. Stallman committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

@table @samp
@item A-
The alt modifier.
@item C-
The control modifier.
@item H-
The hyper modifier.
@item M-
The meta modifier.
@item S-
The shift modifier.
@item s-
The super modifier.
@end table

Thus, the symbol for the key @key{F3} with @key{META} held down is
Richard M. Stallman's avatar
Richard M. Stallman committed
1024
@code{M-f3}.  When you use more than one prefix, we recommend you
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
write them in alphabetical order; but the order does not matter in
arguments to the key-binding lookup and modification functions.

@node Mouse Events
@subsection Mouse Events

Emacs supports four kinds of mouse events: click events, drag events,
button-down events, and motion events.  All mouse events are represented
as lists.  The @sc{car} of the list is the event type; this says which
mouse button was involved, and which modifier keys were used with it.
The event type can also distinguish double or triple button presses
(@pxref{Repeat Events}).  The rest of the list elements give position
and time information.

For key lookup, only the event type matters: two events of the same type
necessarily run the same command.  The command can access the full
values of these events using the @samp{e} interactive code.
@xref{Interactive Codes}.

A key sequence that starts with a mouse event is read using the keymaps
of the buffer in the window that the mouse was in, not the current
buffer.  This does not imply that clicking in a window selects that
window or its buffer---that is entirely under the control of the command
binding of the key sequence.
Richard M. Stallman's avatar
Richard M. Stallman committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

@node Click Events
@subsection Click Events
@cindex click event
@cindex mouse click event

When the user presses a mouse button and releases it at the same
location, that generates a @dfn{click} event.  Mouse click events have
this form:

@example
(@var{event-type}
1061
 (@var{window} @var{buffer-pos} (@var{x} . @var{y}) @var{timestamp})
Richard M. Stallman's avatar
Richard M. Stallman committed
1062 1063 1064 1065 1066
 @var{click-count})
@end example

Here is what the elements normally mean:

1067 1068
@table @asis
@item @var{event-type}
Richard M. Stallman's avatar
Richard M. Stallman committed
1069 1070
This is a symbol that indicates which mouse button was used.  It is
one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the
1071
buttons are numbered left to right.
Richard M. Stallman's avatar
Richard M. Stallman committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-},
@samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift
and super, just as you would with function keys.

This symbol also serves as the event type of the event.  Key bindings
describe events by their types; thus, if there is a key binding for
@code{mouse-1}, that binding would apply to all events whose
@var{event-type} is @code{mouse-1}.

1082
@item @var{window}
Richard M. Stallman's avatar
Richard M. Stallman committed
1083 1084
This is the window in which the click occurred.

1085
@item @var{x}, @var{y}
1086 1087
These are the pixel-denominated coordinates of the click, relative to
the top left corner of @var{window}, which is @code{(0 . 0)}.
Richard M. Stallman's avatar
Richard M. Stallman committed
1088

1089
@item @var{buffer-pos}
Richard M. Stallman's avatar
Richard M. Stallman committed
1090 1091
This is the buffer position of the character clicked on.

1092
@item @var{timestamp}
Richard M. Stallman's avatar
Richard M. Stallman committed
1093 1094 1095 1096
This is the time at which the event occurred, in milliseconds.  (Since
this value wraps around the entire range of Emacs Lisp integers in about
five hours, it is useful only for relating the times of nearby events.)

1097
@item @var{click-count}
Richard M. Stallman's avatar
Richard M. Stallman committed
1098 1099 1100 1101
This is the number of rapid repeated presses so far of the same mouse
button.  @xref{Repeat Events}.
@end table

1102 1103 1104
The meanings of @var{buffer-pos}, @var{x} and @var{y} are somewhat
different when the event location is in a special part of the screen,
such as the mode line or a scroll bar.
Richard M. Stallman's avatar
Richard M. Stallman committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

If the location is in a scroll bar, then @var{buffer-pos} is the symbol
@code{vertical-scroll-bar} or @code{horizontal-scroll-bar}, and the pair
@code{(@var{x} . @var{y})} is replaced with a pair @code{(@var{portion}
. @var{whole})}, where @var{portion} is the distance of the click from
the top or left end of the scroll bar, and @var{whole} is the length of
the entire scroll bar.

If the position is on a mode line or the vertical line separating
@var{window} from its neighbor to the right, then @var{buffer-pos} is
the symbol @code{mode-line} or @code{vertical-line}.  For the mode line,
1116 1117
@var{y} does not have meaningful data.  For the vertical line, @var{x}
does not have meaningful data.
Richard M. Stallman's avatar
Richard M. Stallman committed
1118

1119 1120 1121 1122
In one special case, @var{buffer-pos} is a list containing a symbol (one
of the symbols listed above) instead of just the symbol.  This happens
after the imaginary prefix keys for the event are inserted into the
input stream.  @xref{Key Sequence Input}.
Richard M. Stallman's avatar
Richard M. Stallman committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

@node Drag Events
@subsection Drag Events
@cindex drag event
@cindex mouse drag event

With Emacs, you can have a drag event without even changing your
clothes.  A @dfn{drag event} happens every time the user presses a mouse
button and then moves the mouse to a different character position before
releasing the button.  Like all mouse events, drag events are
represented in Lisp as lists.  The lists record both the starting mouse
position and the final position, like this:

@example
(@var{event-type}
1138 1139
 (@var{window1} @var{buffer-pos1} (@var{x1} . @var{y1}) @var{timestamp1})
 (@var{window2} @var{buffer-pos2} (@var{x2} . @var{y2}) @var{timestamp2})
Richard M. Stallman's avatar
Richard M. Stallman committed
1140 1141 1142 1143
 @var{click-count})
@end example

For a drag event, the name of the symbol @var{event-type} contains the
1144 1145 1146 1147 1148 1149 1150
prefix @samp{drag-}.  For example, dragging the mouse with button 2 held
down generates a @code{drag-mouse-2} event.  The second and third
elements of the event give the starting and ending position of the drag.
Aside from that, the data have the same meanings as in a click event
(@pxref{Click Events}).  You can access the second element of any mouse
event in the same way, with no need to distinguish drag events from
others.
Richard M. Stallman's avatar
Richard M. Stallman committed
1151 1152 1153 1154

The @samp{drag-} prefix follows the modifier key prefixes such as
@samp{C-} and @samp{M-}.

1155
If @code{read-key-sequence} receives a drag event that has no key
Richard M. Stallman's avatar
Richard M. Stallman committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
binding, and the corresponding click event does have a binding, it
changes the drag event into a click event at the drag's starting
position.  This means that you don't have to distinguish between click
and drag events unless you want to.

@node Button-Down Events
@subsection Button-Down Events
@cindex button-down event

Click and drag events happen when the user releases a mouse button.
They cannot happen earlier, because there is no way to distinguish a
click from a drag until the button is released.

If you want to take action as soon as a button is pressed, you need to
handle @dfn{button-down} events.@footnote{Button-down is the
1171
conservative antithesis of drag.}  These occur as soon as a button is
1172
pressed.  They are represented by lists that look exactly like click
1173 1174
events (@pxref{Click Events}), except that the @var{event-type} symbol
name contains the prefix @samp{down-}.  The @samp{down-} prefix follows
Richard M. Stallman's avatar
Richard M. Stallman committed
1175 1176
modifier key prefixes such as @samp{C-} and @samp{M-}.

1177 1178 1179 1180 1181 1182 1183
The function @code{read-key-sequence} ignores any button-down events
that don't have command bindings; therefore, the Emacs command loop
ignores them too.  This means that you need not worry about defining
button-down events unless you want them to do something.  The usual
reason to define a button-down event is so that you can track mouse
motion (by reading motion events) until the button is released.
@xref{Motion Events}.
Richard M. Stallman's avatar
Richard M. Stallman committed
1184 1185 1186 1187 1188 1189 1190 1191

@node Repeat Events
@subsection Repeat Events
@cindex repeat events
@cindex double-click events
@cindex triple-click events

If you press the same mouse button more than once in quick succession
1192 1193
without moving the mouse, Emacs generates special @dfn{repeat} mouse
events for the second and subsequent presses.
Richard M. Stallman's avatar
Richard M. Stallman committed
1194 1195 1196 1197 1198 1199 1200

The most common repeat events are @dfn{double-click} events.  Emacs
generates a double-click event when you click a button twice; the event
happens when you release the button (as is normal for all click
events).

The event type of a double-click event contains the prefix
1201
@samp{double-}.  Thus, a double click on the second mouse button with
Richard M. Stallman's avatar
Richard M. Stallman committed
1202 1203 1204 1205 1206 1207 1208
@key{meta} held down comes to the Lisp program as
@code{M-double-mouse-2}.  If a double-click event has no binding, the
binding of the corresponding ordinary click event is used to execute
it.  Thus, you need not pay attention to the double click feature 
unless you really want to.

When the user performs a double click, Emacs generates first an ordinary
1209 1210
click event, and then a double-click event.  Therefore, you must design
the command binding of the double click event to assume that the
Richard M. Stallman's avatar
Richard M. Stallman committed
1211 1212 1213
single-click command has already run.  It must produce the desired
results of a double click, starting from the results of a single click.

1214 1215 1216
This is convenient, if the meaning of a double click somehow ``builds
on'' the meaning of a single click---which is recommended user interface
design practice for double clicks.
Richard M. Stallman's avatar
Richard M. Stallman committed
1217 1218 1219 1220 1221 1222

If you click a button, then press it down again and start moving the
mouse with the button held down, then you get a @dfn{double-drag} event
when you ultimately release the button.  Its event type contains
@samp{double-drag} instead of just @samp{drag}.  If a double-drag event
has no binding, Emacs looks for an alternate binding as if the event
1223
were an ordinary drag.
Richard M. Stallman's avatar
Richard M. Stallman committed
1224 1225

Before the double-click or double-drag event, Emacs generates a
1226 1227
@dfn{double-down} event when the user presses the button down for the
second time.  Its event type contains @samp{double-down} instead of just
Richard M. Stallman's avatar
Richard M. Stallman committed
1228 1229
@samp{down}.  If a double-down event has no binding, Emacs looks for an
alternate binding as if the event were an ordinary button-down event.
1230 1231
If it finds no binding that way either, the double-down event is
ignored.
Richard M. Stallman's avatar
Richard M. Stallman committed
1232 1233

To summarize, when you click a button and then press it again right
1234 1235 1236
away, Emacs generates a down event and a click event for the first
click, a double-down event when you press the button again, and finally
either a double-click or a double-drag event.
Richard M. Stallman's avatar
Richard M. Stallman committed
1237 1238 1239 1240 1241 1242 1243 1244

If you click a button twice and then press it again, all in quick
succession, Emacs generates a @dfn{triple-down} event, followed by
either a @dfn{triple-click} or a @dfn{triple-drag}.  The event types of
these events contain @samp{triple} instead of @samp{double}.  If any
triple event has no binding, Emacs uses the binding that it would use
for the corresponding double event.

1245 1246 1247 1248 1249
If you click a button three or more times and then press it again, the
events for the presses beyond the third are all triple events.  Emacs
does not have separate event types for quadruple, quintuple, etc.@:
events.  However, you can look at the event list to find out precisely
how many times the button was pressed.
Richard M. Stallman's avatar
Richard M. Stallman committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

@defun event-click-count event
This function returns the number of consecutive button presses that led
up to @var{event}.  If @var{event} is a double-down, double-click or
double-drag event, the value is 2.  If @var{event} is a triple event,
the value is 3 or greater.  If @var{event} is an ordinary mouse event
(not a repeat event), the value is 1.
@end defun

@defvar double-click-time
1260 1261 1262
To generate repeat events, successive mouse button presses must be at
the same screen position, and the number of milliseconds between
successive button presses must be less than the value of
Richard M. Stallman's avatar
Richard M. Stallman committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
@code{double-click-time}.  Setting @code{double-click-time} to
@code{nil} disables multi-click detection entirely.  Setting it to
@code{t} removes the time limit; Emacs then detects multi-clicks by
position only.
@end defvar

@node Motion Events
@subsection Motion Events
@cindex motion event
@cindex mouse motion events

Emacs sometimes generates @dfn{mouse motion} events to describe motion
of the mouse without any button activity.  Mouse motion events are
represented by lists that look like this:

@example
1279
(mouse-movement (@var{window} @var{buffer-pos} (@var{x} . @var{y}) @var{timestamp}))
Richard M. Stallman's avatar
Richard M. Stallman committed
1280 1281 1282 1283 1284 1285 1286 1287
@end example

The second element of the list describes the current position of the
mouse, just as in a click event (@pxref{Click Events}).

The special form @code{track-mouse} enables generation of motion events
within its body.  Outside of @code{track-mouse} forms, Emacs does not
generate events for mere motion of the mouse, and these events do not
1288
appear.  @xref{Mouse Tracking}.
Richard M. Stallman's avatar
Richard M. Stallman committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

@node Focus Events
@subsection Focus Events
@cindex focus event

Window systems provide general ways for the user to control which window
gets keyboard input.  This choice of window is called the @dfn{focus}.
When the user does something to switch between Emacs frames, that
generates a @dfn{focus event}.  The normal definition of a focus event,
in the global keymap, is to select a new frame within Emacs, as the user
would expect.  @xref{Input Focus}.

Focus events are represented in Lisp as lists that look like this:

@example
(switch-frame @var{new-frame})
@end example

@noindent
where @var{new-frame} is the frame switched to.

1310 1311 1312 1313 1314 1315 1316 1317
Most X window managers are set up so that just moving the mouse into a
window is enough to set the focus there.  Emacs appears to do this,
because it changes the cursor to solid in the new frame.  However, there
is no need for the Lisp program to know about the focus change until
some other kind of input arrives.  So Emacs generates a focus event only
when the user actually types a keyboard key or presses a mouse button in
the new frame; just moving the mouse between frames does not generate a
focus event.
Richard M. Stallman's avatar
Richard M. Stallman committed
1318 1319 1320 1321 1322 1323 1324 1325

A focus event in the middle of a key sequence would garble the
sequence.  So Emacs never generates a focus event in the middle of a key
sequence.  If the user changes focus in the middle of a key
sequence---that is, after a prefix key---then Emacs reorders the events
so that the focus event comes either before or after the multi-event key
sequence, and not within it.

Karl Heuer's avatar
Karl Heuer committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
@node Misc Events
@subsection Miscellaneous Window System Events

A few other event types represent occurrences within the window system.

@table @code
@cindex @code{delete-frame} event
@item (delete-frame (@var{frame}))
This kind of event indicates that the user gave the window manager
a command to delete a particular window, which happens to be an Emacs frame.

The standard definition of the @code{delete-frame} event is to delete @var{frame}.

@cindex @code{iconify-frame} event
@item (iconify-frame (@var{frame}))
This kind of event indicates that the user iconified @var{frame} using
Richard M. Stallman's avatar
Richard M. Stallman committed
1342 1343 1344 1345
the window manager.  Its standard definition is @code{ignore}; since the
frame has already been iconified, Emacs has no work to do.  The purpose
of this event type is so that you can keep track of such events if you
want to.
Karl Heuer's avatar
Karl Heuer committed
1346

1347 1348
@cindex @code{make-frame-visible} event
@item (make-frame-visible (@var{frame}))
Karl Heuer's avatar
Karl Heuer committed
1349 1350
This kind of event indicates that the user deiconified @var{frame} using
the window manager.  Its standard definition is @code{ignore}; since the
Richard M. Stallman's avatar
Richard M. Stallman committed
1351
frame has already been made visible, Emacs has no work to do.
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381

@cindex @code{mouse-wheel} event
@item (mouse-wheel @var{position} @var{delta})
This kind of event is generated by moving a wheel on a mouse (such as
the MS Intellimouse).  Its effect is typically a kind of scroll or zoom.

The element @var{delta} describes the amount and direction of the wheel
rotation.  Its absolute value is the number of increments by which the
wheel was rotated.  A negative @var{delta} indicates that the wheel was
rotated backwards, towards the user, and a positive @var{delta}
indicates that the wheel was rotated forward, away from the user.

The element @var{position} is a list describing the position of the
event, in the same format as used in a mouse-click event.

This kind of event is generated only on some kinds of systems.

@cindex @code{drag-n-drop} event
@item (drag-n-drop @var{position} @var{files})
This kind of event is generated when a group of files is
selected in an application outside of Emacs, and then dragged and
dropped onto an Emacs frame.

The element @var{position} is a list describing the position of the
event, in the same format as used in a mouse-click event, and
@var{files} is the list of file names that were dragged and dropped.
The usual way to handle this event is by visiting these files.

This kind of event is generated, at present, only on some kinds of
systems.
Karl Heuer's avatar
Karl Heuer committed
1382 1383
@end table

Karl Heuer's avatar
Karl Heuer committed
1384 1385 1386 1387 1388
  If one of these events arrives in the middle of a key sequence---that
is, after a prefix key---then Emacs reorders the events so that this
event comes either before or after the multi-event key sequence, not
within it.

Richard M. Stallman's avatar
Richard M. Stallman committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
@node Event Examples
@subsection Event Examples

If the user presses and releases the left mouse button over the same
location, that generates a sequence of events like this:

@smallexample
(down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
(mouse-1      (#<window 18 on NEWS> 2613 (0 . 38) -864180))
@end smallexample