1@c -*-texinfo-*- 2@c This is part of the GNU Emacs Lisp Reference Manual. 3@c Copyright (C) 1990--1995, 1998--1999, 2001--2021 Free Software 4@c Foundation, Inc. 5@c See the file elisp.texi for copying conditions. 6@node Command Loop 7@chapter Command Loop 8@cindex editor command loop 9@cindex command loop 10 11 When you run Emacs, it enters the @dfn{editor command loop} almost 12immediately. This loop reads key sequences, executes their definitions, 13and displays the results. In this chapter, we describe how these things 14are done, and the subroutines that allow Lisp programs to do them. 15 16@menu 17* Command Overview:: How the command loop reads commands. 18* Defining Commands:: Specifying how a function should read arguments. 19* Interactive Call:: Calling a command, so that it will read arguments. 20* Distinguish Interactive:: Making a command distinguish interactive calls. 21* Command Loop Info:: Variables set by the command loop for you to examine. 22* Adjusting Point:: Adjustment of point after a command. 23* Input Events:: What input looks like when you read it. 24* Reading Input:: How to read input events from the keyboard or mouse. 25* Special Events:: Events processed immediately and individually. 26* Waiting:: Waiting for user input or elapsed time. 27* Quitting:: How @kbd{C-g} works. How to catch or defer quitting. 28* Prefix Command Arguments:: How the commands to set prefix args work. 29* Recursive Editing:: Entering a recursive edit, 30 and why you usually shouldn't. 31* Disabling Commands:: How the command loop handles disabled commands. 32* Command History:: How the command history is set up, and how accessed. 33* Keyboard Macros:: How keyboard macros are implemented. 34@end menu 35 36@node Command Overview 37@section Command Loop Overview 38 39 The first thing the command loop must do is read a key sequence, 40which is a sequence of input events that translates into a command. 41It does this by calling the function @code{read-key-sequence}. Lisp 42programs can also call this function (@pxref{Key Sequence Input}). 43They can also read input at a lower level with @code{read-key} or 44@code{read-event} (@pxref{Reading One Event}), or discard pending 45input with @code{discard-input} (@pxref{Event Input Misc}). 46 47 The key sequence is translated into a command through the currently 48active keymaps. @xref{Key Lookup}, for information on how this is done. 49The result should be a keyboard macro or an interactively callable 50function. If the key is @kbd{M-x}, then it reads the name of another 51command, which it then calls. This is done by the command 52@code{execute-extended-command} (@pxref{Interactive Call}). 53 54 Prior to executing the command, Emacs runs @code{undo-boundary} to 55create an undo boundary. @xref{Maintaining Undo}. 56 57 To execute a command, Emacs first reads its arguments by calling 58@code{command-execute} (@pxref{Interactive Call}). For commands 59written in Lisp, the @code{interactive} specification says how to read 60the arguments. This may use the prefix argument (@pxref{Prefix 61Command Arguments}) or may read with prompting in the minibuffer 62(@pxref{Minibuffers}). For example, the command @code{find-file} has 63an @code{interactive} specification which says to read a file name 64using the minibuffer. The function body of @code{find-file} does not 65use the minibuffer, so if you call @code{find-file} as a function from 66Lisp code, you must supply the file name string as an ordinary Lisp 67function argument. 68 69 If the command is a keyboard macro (i.e., a string or vector), 70Emacs executes it using @code{execute-kbd-macro} (@pxref{Keyboard 71Macros}). 72 73@defvar pre-command-hook 74This normal hook is run by the editor command loop before it executes 75each command. At that time, @code{this-command} contains the command 76that is about to run, and @code{last-command} describes the previous 77command. @xref{Command Loop Info}. 78@end defvar 79 80@defvar post-command-hook 81This normal hook is run by the editor command loop after it executes 82each command (including commands terminated prematurely by quitting or 83by errors). At that time, @code{this-command} refers to the command 84that just ran, and @code{last-command} refers to the command before 85that. 86 87This hook is also run when Emacs first enters the command loop (at 88which point @code{this-command} and @code{last-command} are both 89@code{nil}). 90@end defvar 91 92 Quitting is suppressed while running @code{pre-command-hook} and 93@code{post-command-hook}. If an error happens while executing one of 94these hooks, it does not terminate execution of the hook; instead 95the error is silenced and the function in which the error occurred 96is removed from the hook. 97 98 A request coming into the Emacs server (@pxref{Emacs Server,,, 99emacs, The GNU Emacs Manual}) runs these two hooks just as a keyboard 100command does. 101 102@node Defining Commands 103@section Defining Commands 104@cindex defining commands 105@cindex commands, defining 106@cindex functions, making them interactive 107@cindex interactive function 108 109 The special form @code{interactive} turns a Lisp function into a 110command. The @code{interactive} form must be located at top-level in 111the function body, usually as the first form in the body; this applies 112to both lambda expressions (@pxref{Lambda Expressions}) and 113@code{defun} forms (@pxref{Defining Functions}). This form does 114nothing during the actual execution of the function; its presence 115serves as a flag, telling the Emacs command loop that the function can 116be called interactively. The argument of the @code{interactive} form 117specifies how the arguments for an interactive call should be read. 118 119@cindex @code{interactive-form} property 120 Alternatively, an @code{interactive} form may be specified in a 121function symbol's @code{interactive-form} property. A non-@code{nil} 122value for this property takes precedence over any @code{interactive} 123form in the function body itself. This feature is seldom used. 124 125@anchor{The interactive-only property} 126@cindex @code{interactive-only} property 127 Sometimes, a function is only intended to be called interactively, 128never directly from Lisp. In that case, give the function a 129non-@code{nil} @code{interactive-only} property, either directly 130or via @code{declare} (@pxref{Declare Form}). This causes the 131byte compiler to warn if the command is called from Lisp. The output 132of @code{describe-function} will include similar information. 133The value of the property can be: a string, which the byte-compiler 134will use directly in its warning (it should end with a period, and not 135start with a capital, e.g., @code{"use (system-name) instead."}); @code{t}; any 136other symbol, which should be an alternative function to use in Lisp 137code. 138 139Generic functions (@pxref{Generic Functions}) cannot be turned into 140commands by adding the @code{interactive} form to them. 141 142@menu 143* Using Interactive:: General rules for @code{interactive}. 144* Interactive Codes:: The standard letter-codes for reading arguments 145 in various ways. 146* Interactive Examples:: Examples of how to read interactive arguments. 147* Generic Commands:: Select among command alternatives. 148@end menu 149 150@node Using Interactive 151@subsection Using @code{interactive} 152@cindex arguments, interactive entry 153@cindex interactive spec, using 154 155 This section describes how to write the @code{interactive} form that 156makes a Lisp function an interactively-callable command, and how to 157examine a command's @code{interactive} form. 158 159@defspec interactive arg-descriptor 160This special form declares that a function is a command, and that it 161may therefore be called interactively (via @kbd{M-x} or by entering a 162key sequence bound to it). The argument @var{arg-descriptor} declares 163how to compute the arguments to the command when the command is called 164interactively. 165 166A command may be called from Lisp programs like any other function, but 167then the caller supplies the arguments and @var{arg-descriptor} has no 168effect. 169 170@cindex @code{interactive-form}, symbol property 171The @code{interactive} form must be located at top-level in the 172function body, or in the function symbol's @code{interactive-form} 173property (@pxref{Symbol Properties}). It has its effect because the 174command loop looks for it before calling the function 175(@pxref{Interactive Call}). Once the function is called, all its body 176forms are executed; at this time, if the @code{interactive} form 177occurs within the body, the form simply returns @code{nil} without 178even evaluating its argument. 179 180By convention, you should put the @code{interactive} form in the 181function body, as the first top-level form. If there is an 182@code{interactive} form in both the @code{interactive-form} symbol 183property and the function body, the former takes precedence. The 184@code{interactive-form} symbol property can be used to add an 185interactive form to an existing function, or change how its arguments 186are processed interactively, without redefining the function. 187@end defspec 188 189There are three possibilities for the argument @var{arg-descriptor}: 190 191@itemize @bullet 192@item 193It may be omitted or @code{nil}; then the command is called with no 194arguments. This leads quickly to an error if the command requires one 195or more arguments. 196 197@item 198It may be a string; its contents are a sequence of elements separated 199by newlines, one for each argument@footnote{Some elements actually 200supply two arguments.}. Each element consists of a code character 201(@pxref{Interactive Codes}) optionally followed by a prompt (which 202some code characters use and some ignore). Here is an example: 203 204@smallexample 205(interactive "P\nbFrobnicate buffer: ") 206@end smallexample 207 208@noindent 209The code letter @samp{P} sets the command's first argument to the raw 210command prefix (@pxref{Prefix Command Arguments}). @samp{bFrobnicate 211buffer: } prompts the user with @samp{Frobnicate buffer: } to enter 212the name of an existing buffer, which becomes the second and final 213argument. 214 215The prompt string can use @samp{%} to include previous argument values 216(starting with the first argument) in the prompt. This is done using 217@code{format-message} (@pxref{Formatting Strings}). For example, here is how 218you could read the name of an existing buffer followed by a new name to 219give to that buffer: 220 221@smallexample 222@group 223(interactive "bBuffer to rename: \nsRename buffer %s to: ") 224@end group 225@end smallexample 226 227@cindex @samp{*} in @code{interactive} 228@cindex read-only buffers in interactive 229If @samp{*} appears at the beginning of the string, then an error is 230signaled if the buffer is read-only. 231 232@cindex @samp{@@} in @code{interactive} 233If @samp{@@} appears at the beginning of the string, and if the key 234sequence used to invoke the command includes any mouse events, then 235the window associated with the first of those events is selected 236before the command is run. 237 238@cindex @samp{^} in @code{interactive} 239@cindex shift-selection, and @code{interactive} spec 240If @samp{^} appears at the beginning of the string, and if the command 241was invoked through @dfn{shift-translation}, set the mark and activate 242the region temporarily, or extend an already active region, before the 243command is run. If the command was invoked without shift-translation, 244and the region is temporarily active, deactivate the region before the 245command is run. Shift-translation is controlled on the user level by 246@code{shift-select-mode}; see @ref{Shift Selection,,, emacs, The GNU 247Emacs Manual}. 248 249You can use @samp{*}, @samp{@@}, and @code{^} together; the order does 250not matter. Actual reading of arguments is controlled by the rest of 251the prompt string (starting with the first character that is not 252@samp{*}, @samp{@@}, or @samp{^}). 253 254@item 255It may be a Lisp expression that is not a string; then it should be a 256form that is evaluated to get a list of arguments to pass to the 257command. Usually this form will call various functions to read input 258from the user, most often through the minibuffer (@pxref{Minibuffers}) 259or directly from the keyboard (@pxref{Reading Input}). 260 261Providing point or the mark as an argument value is also common, but 262if you do this @emph{and} read input (whether using the minibuffer or 263not), be sure to get the integer values of point or the mark after 264reading. The current buffer may be receiving subprocess output; if 265subprocess output arrives while the command is waiting for input, it 266could relocate point and the mark. 267 268Here's an example of what @emph{not} to do: 269 270@smallexample 271(interactive 272 (list (region-beginning) (region-end) 273 (read-string "Foo: " nil 'my-history))) 274@end smallexample 275 276@noindent 277Here's how to avoid the problem, by examining point and the mark after 278reading the keyboard input: 279 280@smallexample 281(interactive 282 (let ((string (read-string "Foo: " nil 'my-history))) 283 (list (region-beginning) (region-end) string))) 284@end smallexample 285 286@strong{Warning:} the argument values should not include any data 287types that can't be printed and then read. Some facilities save 288@code{command-history} in a file to be read in the subsequent 289sessions; if a command's arguments contain a data type that prints 290using @samp{#<@dots{}>} syntax, those facilities won't work. 291 292There are, however, a few exceptions: it is ok to use a limited set of 293expressions such as @code{(point)}, @code{(mark)}, 294@code{(region-beginning)}, and @code{(region-end)}, because Emacs 295recognizes them specially and puts the expression (rather than its 296value) into the command history. To see whether the expression you 297wrote is one of these exceptions, run the command, then examine 298@code{(car command-history)}. 299@end itemize 300 301@cindex examining the @code{interactive} form 302@defun interactive-form function 303This function returns the @code{interactive} form of @var{function}. 304If @var{function} is an interactively callable function 305(@pxref{Interactive Call}), the value is the command's 306@code{interactive} form @code{(interactive @var{spec})}, which 307specifies how to compute its arguments. Otherwise, the value is 308@code{nil}. If @var{function} is a symbol, its function definition is 309used. 310@end defun 311 312@node Interactive Codes 313@subsection Code Characters for @code{interactive} 314@cindex interactive code description 315@cindex description for interactive codes 316@cindex codes, interactive, description of 317@cindex characters for interactive codes 318 319 The code character descriptions below contain a number of key words, 320defined here as follows: 321 322@table @b 323@item Completion 324@cindex interactive completion 325Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name 326completion because the argument is read using @code{completing-read} 327(@pxref{Completion}). @kbd{?} displays a list of possible completions. 328 329@item Existing 330Require the name of an existing object. An invalid name is not 331accepted; the commands to exit the minibuffer do not exit if the current 332input is not valid. 333 334@item Default 335@cindex default argument string 336A default value of some sort is used if the user enters no text in the 337minibuffer. The default depends on the code character. 338 339@item No I/O 340This code letter computes an argument without reading any input. 341Therefore, it does not use a prompt string, and any prompt string you 342supply is ignored. 343 344Even though the code letter doesn't use a prompt string, you must follow 345it with a newline if it is not the last code character in the string. 346 347@item Prompt 348A prompt immediately follows the code character. The prompt ends either 349with the end of the string or with a newline. 350 351@item Special 352This code character is meaningful only at the beginning of the 353interactive string, and it does not look for a prompt or a newline. 354It is a single, isolated character. 355@end table 356 357@cindex reading interactive arguments 358 Here are the code character descriptions for use with @code{interactive}: 359 360@table @samp 361@item * 362Signal an error if the current buffer is read-only. Special. 363 364@item @@ 365Select the window mentioned in the first mouse event in the key 366sequence that invoked this command. Special. 367 368@item ^ 369If the command was invoked through shift-translation, set the mark and 370activate the region temporarily, or extend an already active region, 371before the command is run. If the command was invoked without 372shift-translation, and the region is temporarily active, deactivate 373the region before the command is run. Special. 374 375@item a 376A function name (i.e., a symbol satisfying @code{fboundp}). Existing, 377Completion, Prompt. 378 379@item b 380The name of an existing buffer. By default, uses the name of the 381current buffer (@pxref{Buffers}). Existing, Completion, Default, 382Prompt. 383 384@item B 385A buffer name. The buffer need not exist. By default, uses the name of 386a recently used buffer other than the current buffer. Completion, 387Default, Prompt. 388 389@item c 390A character. The cursor does not move into the echo area. Prompt. 391 392@item C 393A command name (i.e., a symbol satisfying @code{commandp}). Existing, 394Completion, Prompt. 395 396@item d 397@cindex position argument 398The position of point, as an integer (@pxref{Point}). No I/O. 399 400@item D 401A directory. The default is the current default directory of the 402current buffer, @code{default-directory} (@pxref{File Name Expansion}). 403Existing, Completion, Default, Prompt. 404 405@item e 406The first or next non-keyboard event in the key sequence that invoked 407the command. More precisely, @samp{e} gets events that are lists, so 408you can look at the data in the lists. @xref{Input Events}. No I/O. 409 410You use @samp{e} for mouse events and for special system events 411(@pxref{Misc Events}). The event list that the command receives 412depends on the event. @xref{Input Events}, which describes the forms 413of the list for each event in the corresponding subsections. 414 415You can use @samp{e} more than once in a single command's interactive 416specification. If the key sequence that invoked the command has 417@var{n} events that are lists, the @var{n}th @samp{e} provides the 418@var{n}th such event. Events that are not lists, such as function keys 419and @acronym{ASCII} characters, do not count where @samp{e} is concerned. 420 421@item f 422A file name of an existing file (@pxref{File Names}). The default 423directory is @code{default-directory}. Existing, Completion, Default, 424Prompt. 425 426@item F 427A file name. The file need not exist. Completion, Default, Prompt. 428 429@item G 430A file name. The file need not exist. If the user enters just a 431directory name, then the value is just that directory name, with no 432file name within the directory added. Completion, Default, Prompt. 433 434@item i 435An irrelevant argument. This code always supplies @code{nil} as 436the argument's value. No I/O. 437 438@item k 439A key sequence (@pxref{Key Sequences}). This keeps reading events 440until a command (or undefined command) is found in the current key 441maps. The key sequence argument is represented as a string or vector. 442The cursor does not move into the echo area. Prompt. 443 444If @samp{k} reads a key sequence that ends with a down-event, it also 445reads and discards the following up-event. You can get access to that 446up-event with the @samp{U} code character. 447 448This kind of input is used by commands such as @code{describe-key} and 449@code{global-set-key}. 450 451@item K 452A key sequence, whose definition you intend to change. This works like 453@samp{k}, except that it suppresses, for the last input event in the key 454sequence, the conversions that are normally used (when necessary) to 455convert an undefined key into a defined one. 456 457@item m 458@cindex marker argument 459The position of the mark, as an integer. No I/O. 460 461@item M 462Arbitrary text, read in the minibuffer using the current buffer's input 463method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU 464Emacs Manual}). Prompt. 465 466@item n 467A number, read with the minibuffer. If the input is not a number, the 468user has to try again. @samp{n} never uses the prefix argument. 469Prompt. 470 471@item N 472The numeric prefix argument; but if there is no prefix argument, read 473a number as with @kbd{n}. The value is always a number. @xref{Prefix 474Command Arguments}. Prompt. 475 476@item p 477@cindex numeric prefix argument usage 478The numeric prefix argument. (Note that this @samp{p} is lower case.) 479No I/O. 480 481@item P 482@cindex raw prefix argument usage 483The raw prefix argument. (Note that this @samp{P} is upper case.) No 484I/O. 485 486@item r 487@cindex region argument 488Point and the mark, as two numeric arguments, smallest first. This is 489the only code letter that specifies two successive arguments rather than 490one. This will signal an error if the mark is not set in the buffer 491which is current when the command is invoked. If Transient Mark mode 492is turned on (@pxref{The Mark}) --- as it is by default --- and user 493option @code{mark-even-if-inactive} is @code{nil}, Emacs will signal 494an error even if the mark @emph{is} set, but is inactive. No I/O. 495 496@item s 497Arbitrary text, read in the minibuffer and returned as a string 498(@pxref{Text from Minibuffer}). Terminate the input with either 499@kbd{C-j} or @key{RET}. (@kbd{C-q} may be used to include either of 500these characters in the input.) Prompt. 501 502@item S 503An interned symbol whose name is read in the minibuffer. Terminate 504the input with either @kbd{C-j} or @key{RET}. Other characters that 505normally terminate a symbol (e.g., whitespace, parentheses and 506brackets) do not do so here. Prompt. 507 508@item U 509A key sequence or @code{nil}. Can be used after a @samp{k} or 510@samp{K} argument to get the up-event that was discarded (if any) 511after @samp{k} or @samp{K} read a down-event. If no up-event has been 512discarded, @samp{U} provides @code{nil} as the argument. No I/O. 513 514@item v 515A variable declared to be a user option (i.e., satisfying the 516predicate @code{custom-variable-p}). This reads the variable using 517@code{read-variable}. @xref{Definition of read-variable}. Existing, 518Completion, Prompt. 519 520@item x 521A Lisp object, specified with its read syntax, terminated with a 522@kbd{C-j} or @key{RET}. The object is not evaluated. @xref{Object from 523Minibuffer}. Prompt. 524 525@item X 526@cindex evaluated expression argument 527A Lisp form's value. @samp{X} reads as @samp{x} does, then evaluates 528the form so that its value becomes the argument for the command. 529Prompt. 530 531@item z 532A coding system name (a symbol). If the user enters null input, the 533argument value is @code{nil}. @xref{Coding Systems}. Completion, 534Existing, Prompt. 535 536@item Z 537A coding system name (a symbol)---but only if this command has a prefix 538argument. With no prefix argument, @samp{Z} provides @code{nil} as the 539argument value. Completion, Existing, Prompt. 540@end table 541 542@node Interactive Examples 543@subsection Examples of Using @code{interactive} 544@cindex examples of using @code{interactive} 545@cindex @code{interactive}, examples of using 546 547 Here are some examples of @code{interactive}: 548 549@example 550@group 551(defun foo1 () ; @r{@code{foo1} takes no arguments,} 552 (interactive) ; @r{just moves forward two words.} 553 (forward-word 2)) 554 @result{} foo1 555@end group 556 557@group 558(defun foo2 (n) ; @r{@code{foo2} takes one argument,} 559 (interactive "^p") ; @r{which is the numeric prefix.} 560 ; @r{under @code{shift-select-mode},} 561 ; @r{will activate or extend region.} 562 (forward-word (* 2 n))) 563 @result{} foo2 564@end group 565 566@group 567(defun foo3 (n) ; @r{@code{foo3} takes one argument,} 568 (interactive "nCount:") ; @r{which is read with the Minibuffer.} 569 (forward-word (* 2 n))) 570 @result{} foo3 571@end group 572 573@group 574(defun three-b (b1 b2 b3) 575 "Select three existing buffers. 576Put them into three windows, selecting the last one." 577@end group 578 (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:") 579 (delete-other-windows) 580 (split-window (selected-window) 8) 581 (switch-to-buffer b1) 582 (other-window 1) 583 (split-window (selected-window) 8) 584 (switch-to-buffer b2) 585 (other-window 1) 586 (switch-to-buffer b3)) 587 @result{} three-b 588@group 589(three-b "*scratch*" "declarations.texi" "*mail*") 590 @result{} nil 591@end group 592@end example 593 594@node Generic Commands 595@subsection Select among Command Alternatives 596@cindex generic commands 597@cindex alternatives, defining 598 599The macro @code{define-alternatives} can be used to define 600@dfn{generic commands}. These are interactive functions whose 601implementation can be selected from several alternatives, as a matter 602of user preference. 603 604@defmac define-alternatives command &rest customizations 605Define the new command @var{command}, a symbol. 606 607When a user runs @kbd{M-x @var{command} @key{RET}} for the first time, 608Emacs prompts for which real form of the command to use, and records 609the selection by way of a custom variable. Using a prefix argument 610repeats this process of choosing an alternative. 611 612The variable @code{@var{command}-alternatives} should contain an alist 613with alternative implementations of @var{command}. 614Until this variable is set, @code{define-alternatives} has no effect. 615 616If @var{customizations} is non-@code{nil}, it should consist of 617alternating @code{defcustom} keywords (typically @code{:group} and 618@code{:version}) and values to add to the declaration of 619@code{@var{command}-alternatives}. 620@end defmac 621 622@node Interactive Call 623@section Interactive Call 624@cindex interactive call 625 626 After the command loop has translated a key sequence into a command, 627it invokes that command using the function @code{command-execute}. If 628the command is a function, @code{command-execute} calls 629@code{call-interactively}, which reads the arguments and calls the 630command. You can also call these functions yourself. 631 632 Note that the term ``command'', in this context, refers to an 633interactively callable function (or function-like object), or a 634keyboard macro. It does not refer to the key sequence used to invoke 635a command (@pxref{Keymaps}). 636 637@defun commandp object &optional for-call-interactively 638This function returns @code{t} if @var{object} is a command. 639Otherwise, it returns @code{nil}. 640 641Commands include strings and vectors (which are treated as keyboard 642macros), lambda expressions that contain a top-level 643@code{interactive} form (@pxref{Using Interactive}), byte-code 644function objects made from such lambda expressions, autoload objects 645that are declared as interactive (non-@code{nil} fourth argument to 646@code{autoload}), and some primitive functions. Also, a symbol is 647considered a command if it has a non-@code{nil} 648@code{interactive-form} property, or if its function definition 649satisfies @code{commandp}. 650 651If @var{for-call-interactively} is non-@code{nil}, then 652@code{commandp} returns @code{t} only for objects that 653@code{call-interactively} could call---thus, not for keyboard macros. 654 655See @code{documentation} in @ref{Accessing Documentation}, for a 656realistic example of using @code{commandp}. 657@end defun 658 659@defun call-interactively command &optional record-flag keys 660This function calls the interactively callable function @var{command}, 661providing arguments according to its interactive calling specifications. 662It returns whatever @var{command} returns. 663 664If, for instance, you have a function with the following signature: 665 666@example 667(defun foo (begin end) 668 (interactive "r") 669 ...) 670@end example 671 672then saying 673 674@example 675(call-interactively 'foo) 676@end example 677 678will call @code{foo} with the region (@code{point} and @code{mark}) as 679the arguments. 680 681An error is signaled if @var{command} is not a function or if it 682cannot be called interactively (i.e., is not a command). Note that 683keyboard macros (strings and vectors) are not accepted, even though 684they are considered commands, because they are not functions. If 685@var{command} is a symbol, then @code{call-interactively} uses its 686function definition. 687 688@cindex record command history 689If @var{record-flag} is non-@code{nil}, then this command and its 690arguments are unconditionally added to the list @code{command-history}. 691Otherwise, the command is added only if it uses the minibuffer to read 692an argument. @xref{Command History}. 693 694The argument @var{keys}, if given, should be a vector which specifies 695the sequence of events to supply if the command inquires which events 696were used to invoke it. If @var{keys} is omitted or @code{nil}, the 697default is the return value of @code{this-command-keys-vector}. 698@xref{Definition of this-command-keys-vector}. 699@end defun 700 701@defun funcall-interactively function &rest arguments 702This function works like @code{funcall} (@pxref{Calling Functions}), 703but it makes the call look like an interactive invocation: a call to 704@code{called-interactively-p} inside @var{function} will return 705@code{t}. If @var{function} is not a command, it is called without 706signaling an error. 707@end defun 708 709@defun command-execute command &optional record-flag keys special 710@cindex keyboard macro execution 711This function executes @var{command}. The argument @var{command} must 712satisfy the @code{commandp} predicate; i.e., it must be an interactively 713callable function or a keyboard macro. 714 715A string or vector as @var{command} is executed with 716@code{execute-kbd-macro}. A function is passed to 717@code{call-interactively} (see above), along with the 718@var{record-flag} and @var{keys} arguments. 719 720If @var{command} is a symbol, its function definition is used in its 721place. A symbol with an @code{autoload} definition counts as a 722command if it was declared to stand for an interactively callable 723function. Such a definition is handled by loading the specified 724library and then rechecking the definition of the symbol. 725 726The argument @var{special}, if given, means to ignore the prefix 727argument and not clear it. This is used for executing special events 728(@pxref{Special Events}). 729@end defun 730 731@deffn Command execute-extended-command prefix-argument 732@cindex read command name 733This function reads a command name from the minibuffer using 734@code{completing-read} (@pxref{Completion}). Then it uses 735@code{command-execute} to call the specified command. Whatever that 736command returns becomes the value of @code{execute-extended-command}. 737 738@cindex execute with prefix argument 739If the command asks for a prefix argument, it receives the value 740@var{prefix-argument}. If @code{execute-extended-command} is called 741interactively, the current raw prefix argument is used for 742@var{prefix-argument}, and thus passed on to whatever command is run. 743 744@c !!! Should this be @kindex? 745@cindex @kbd{M-x} 746@code{execute-extended-command} is the normal definition of @kbd{M-x}, 747so it uses the string @w{@samp{M-x }} as a prompt. (It would be better 748to take the prompt from the events used to invoke 749@code{execute-extended-command}, but that is painful to implement.) A 750description of the value of the prefix argument, if any, also becomes 751part of the prompt. 752 753@example 754@group 755(execute-extended-command 3) 756---------- Buffer: Minibuffer ---------- 7573 M-x forward-word @key{RET} 758---------- Buffer: Minibuffer ---------- 759 @result{} t 760@end group 761@end example 762@end deffn 763 764@node Distinguish Interactive 765@section Distinguish Interactive Calls 766@cindex distinguish interactive calls 767@cindex is this call interactive 768 769 Sometimes a command should display additional visual feedback (such 770as an informative message in the echo area) for interactive calls 771only. There are three ways to do this. The recommended way to test 772whether the function was called using @code{call-interactively} is to 773give it an optional argument @code{print-message} and use the 774@code{interactive} spec to make it non-@code{nil} in interactive 775calls. Here's an example: 776 777@example 778(defun foo (&optional print-message) 779 (interactive "p") 780 (when print-message 781 (message "foo"))) 782@end example 783 784@noindent 785We use @code{"p"} because the numeric prefix argument is never 786@code{nil}. Defined in this way, the function does display the 787message when called from a keyboard macro. 788 789 The above method with the additional argument is usually best, 790because it allows callers to say ``treat this call as interactive''. 791But you can also do the job by testing @code{called-interactively-p}. 792 793@defun called-interactively-p kind 794This function returns @code{t} when the calling function was called 795using @code{call-interactively}. 796 797The argument @var{kind} should be either the symbol @code{interactive} 798or the symbol @code{any}. If it is @code{interactive}, then 799@code{called-interactively-p} returns @code{t} only if the call was 800made directly by the user---e.g., if the user typed a key sequence 801bound to the calling function, but @emph{not} if the user ran a 802keyboard macro that called the function (@pxref{Keyboard Macros}). If 803@var{kind} is @code{any}, @code{called-interactively-p} returns 804@code{t} for any kind of interactive call, including keyboard macros. 805 806If in doubt, use @code{any}; the only known proper use of 807@code{interactive} is if you need to decide whether to display a 808helpful message while a function is running. 809 810A function is never considered to be called interactively if it was 811called via Lisp evaluation (or with @code{apply} or @code{funcall}). 812@end defun 813 814@noindent 815Here is an example of using @code{called-interactively-p}: 816 817@example 818@group 819(defun foo () 820 (interactive) 821 (when (called-interactively-p 'any) 822 (message "Interactive!") 823 'foo-called-interactively)) 824@end group 825 826@group 827;; @r{Type @kbd{M-x foo}.} 828 @print{} Interactive! 829@end group 830 831@group 832(foo) 833 @result{} nil 834@end group 835@end example 836 837@noindent 838Here is another example that contrasts direct and indirect calls to 839@code{called-interactively-p}. 840 841@example 842@group 843(defun bar () 844 (interactive) 845 (message "%s" (list (foo) (called-interactively-p 'any)))) 846@end group 847 848@group 849;; @r{Type @kbd{M-x bar}.} 850 @print{} (nil t) 851@end group 852@end example 853 854@node Command Loop Info 855@section Information from the Command Loop 856@cindex command loop variables 857 858The editor command loop sets several Lisp variables to keep status 859records for itself and for commands that are run. With the exception of 860@code{this-command} and @code{last-command} it's generally a bad idea to 861change any of these variables in a Lisp program. 862 863@defvar last-command 864This variable records the name of the previous command executed by the 865command loop (the one before the current command). Normally the value 866is a symbol with a function definition, but this is not guaranteed. 867 868The value is copied from @code{this-command} when a command returns to 869the command loop, except when the command has specified a prefix 870argument for the following command. 871 872This variable is always local to the current terminal and cannot be 873buffer-local. @xref{Multiple Terminals}. 874@end defvar 875 876@defvar real-last-command 877This variable is set up by Emacs just like @code{last-command}, 878but never altered by Lisp programs. 879@end defvar 880 881@defvar last-repeatable-command 882This variable stores the most recently executed command that was not 883part of an input event. This is the command @code{repeat} will try to 884repeat, @xref{Repeating,,, emacs, The GNU Emacs Manual}. 885@end defvar 886 887@defvar this-command 888@cindex current command 889This variable records the name of the command now being executed by 890the editor command loop. Like @code{last-command}, it is normally a symbol 891with a function definition. 892 893The command loop sets this variable just before running a command, and 894copies its value into @code{last-command} when the command finishes 895(unless the command specified a prefix argument for the following 896command). 897 898@cindex kill command repetition 899Some commands set this variable during their execution, as a flag for 900whatever command runs next. In particular, the functions for killing text 901set @code{this-command} to @code{kill-region} so that any kill commands 902immediately following will know to append the killed text to the 903previous kill. 904@end defvar 905 906If you do not want a particular command to be recognized as the previous 907command in the case where it got an error, you must code that command to 908prevent this. One way is to set @code{this-command} to @code{t} at the 909beginning of the command, and set @code{this-command} back to its proper 910value at the end, like this: 911 912@example 913(defun foo (args@dots{}) 914 (interactive @dots{}) 915 (let ((old-this-command this-command)) 916 (setq this-command t) 917 @r{@dots{}do the work@dots{}} 918 (setq this-command old-this-command))) 919@end example 920 921@noindent 922We do not bind @code{this-command} with @code{let} because that would 923restore the old value in case of error---a feature of @code{let} which 924in this case does precisely what we want to avoid. 925 926@defvar this-original-command 927This has the same value as @code{this-command} except when command 928remapping occurs (@pxref{Remapping Commands}). In that case, 929@code{this-command} gives the command actually run (the result of 930remapping), and @code{this-original-command} gives the command that 931was specified to run but remapped into another command. 932@end defvar 933 934@defun this-command-keys 935This function returns a string or vector containing the key sequence 936that invoked the present command, plus any previous commands that 937generated the prefix argument for this command. Any events read by the 938command using @code{read-event} without a timeout get tacked on to the end. 939 940However, if the command has called @code{read-key-sequence}, it 941returns the last read key sequence. @xref{Key Sequence Input}. The 942value is a string if all events in the sequence were characters that 943fit in a string. @xref{Input Events}. 944 945@example 946@group 947(this-command-keys) 948;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.} 949 @result{} "^U^X^E" 950@end group 951@end example 952@end defun 953 954@defun this-command-keys-vector 955@anchor{Definition of this-command-keys-vector} 956Like @code{this-command-keys}, except that it always returns the events 957in a vector, so you don't need to deal with the complexities of storing 958input events in a string (@pxref{Strings of Events}). 959@end defun 960 961@defun clear-this-command-keys &optional keep-record 962This function empties out the table of events for 963@code{this-command-keys} to return. Unless @var{keep-record} is 964non-@code{nil}, it also empties the records that the function 965@code{recent-keys} (@pxref{Recording Input}) will subsequently return. 966This is useful after reading a password, to prevent the password from 967echoing inadvertently as part of the next command in certain cases. 968@end defun 969 970@defvar last-nonmenu-event 971This variable holds the last input event read as part of a key sequence, 972not counting events resulting from mouse menus. 973 974One use of this variable is for telling @code{x-popup-menu} where to pop 975up a menu. It is also used internally by @code{y-or-n-p} 976(@pxref{Yes-or-No Queries}). 977@end defvar 978 979@defvar last-command-event 980This variable is set to the last input event that was read by the 981command loop as part of a command. The principal use of this variable 982is in @code{self-insert-command}, which uses it to decide which 983character to insert. 984 985@example 986@group 987last-command-event 988;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.} 989 @result{} 5 990@end group 991@end example 992 993@noindent 994The value is 5 because that is the @acronym{ASCII} code for @kbd{C-e}. 995@end defvar 996 997@defvar last-event-frame 998This variable records which frame the last input event was directed to. 999Usually this is the frame that was selected when the event was 1000generated, but if that frame has redirected input focus to another 1001frame, the value is the frame to which the event was redirected. 1002@xref{Input Focus}. 1003 1004If the last event came from a keyboard macro, the value is @code{macro}. 1005@end defvar 1006 1007@node Adjusting Point 1008@section Adjusting Point After Commands 1009@cindex adjusting point 1010@cindex invisible/intangible text, and point 1011@cindex @code{display} property, and point display 1012@cindex @code{composition} property, and point display 1013 1014 Emacs cannot display the cursor when point is in the middle of a 1015sequence of text that has the @code{display} or @code{composition} 1016property, or is invisible. Therefore, after a command finishes and 1017returns to the command loop, if point is within such a sequence, the 1018command loop normally moves point to the edge of the sequence, making this 1019sequence effectively intangible. 1020 1021 A command can inhibit this feature by setting the variable 1022@code{disable-point-adjustment}: 1023 1024@defvar disable-point-adjustment 1025If this variable is non-@code{nil} when a command returns to the 1026command loop, then the command loop does not check for those text 1027properties, and does not move point out of sequences that have them. 1028 1029The command loop sets this variable to @code{nil} before each command, 1030so if a command sets it, the effect applies only to that command. 1031@end defvar 1032 1033@defvar global-disable-point-adjustment 1034If you set this variable to a non-@code{nil} value, the feature of 1035moving point out of these sequences is completely turned off. 1036@end defvar 1037 1038@node Input Events 1039@section Input Events 1040@cindex events 1041@cindex input events 1042 1043The Emacs command loop reads a sequence of @dfn{input events} that 1044represent keyboard or mouse activity, or system events sent to Emacs. 1045The events for keyboard activity are characters or symbols; other 1046events are always lists. This section describes the representation 1047and meaning of input events in detail. 1048 1049@defun eventp object 1050This function returns non-@code{nil} if @var{object} is an input event 1051or event type. 1052 1053Note that any non-@code{nil} symbol might be used as an event or an 1054event type; @code{eventp} cannot distinguish whether a symbol is 1055intended by Lisp code to be used as an event. 1056@end defun 1057 1058@menu 1059* Keyboard Events:: Ordinary characters -- keys with symbols on them. 1060* Function Keys:: Function keys -- keys with names, not symbols. 1061* Mouse Events:: Overview of mouse events. 1062* Click Events:: Pushing and releasing a mouse button. 1063* Drag Events:: Moving the mouse before releasing the button. 1064* Button-Down Events:: A button was pushed and not yet released. 1065* Repeat Events:: Double and triple click (or drag, or down). 1066* Motion Events:: Just moving the mouse, not pushing a button. 1067* Focus Events:: Moving the mouse between frames. 1068* Misc Events:: Other events the system can generate. 1069* Event Examples:: Examples of the lists for mouse events. 1070* Classifying Events:: Finding the modifier keys in an event symbol. 1071 Event types. 1072* Accessing Mouse:: Functions to extract info from mouse events. 1073* Accessing Scroll:: Functions to get info from scroll bar events. 1074* Strings of Events:: Special considerations for putting 1075 keyboard character events in a string. 1076@end menu 1077 1078@node Keyboard Events 1079@subsection Keyboard Events 1080@cindex keyboard events 1081 1082@cindex character event 1083There are two kinds of input you can get from the keyboard: ordinary 1084keys, and function keys. Ordinary keys correspond to (possibly 1085modified) characters; the events they generate are represented in Lisp 1086as characters. The event type of a @dfn{character event} is the 1087character itself (an integer), which might have some modifier bits 1088set; see @ref{Classifying Events}. 1089 1090@cindex modifier bits (of input character) 1091@cindex basic code (of input character) 1092An input character event consists of a @dfn{basic code} between 0 and 1093524287, plus any or all of these @dfn{modifier bits}: 1094 1095@table @asis 1096@item meta 1097The 1098@tex 1099@math{2^{27}} 1100@end tex 1101@ifnottex 11022**27 1103@end ifnottex 1104bit in the character code indicates a character 1105typed with the meta key held down. 1106 1107@item control 1108The 1109@tex 1110@math{2^{26}} 1111@end tex 1112@ifnottex 11132**26 1114@end ifnottex 1115bit in the character code indicates a non-@acronym{ASCII} 1116control character. 1117 1118@sc{ascii} control characters such as @kbd{C-a} have special basic 1119codes of their own, so Emacs needs no special bit to indicate them. 1120Thus, the code for @kbd{C-a} is just 1. 1121 1122But if you type a control combination not in @acronym{ASCII}, such as 1123@kbd{%} with the control key, the numeric value you get is the code 1124for @kbd{%} plus 1125@tex 1126@math{2^{26}} 1127@end tex 1128@ifnottex 11292**26 1130@end ifnottex 1131(assuming the terminal supports non-@acronym{ASCII} 1132control characters), i.e.@: with the 27th bit set. 1133 1134@item shift 1135The 1136@tex 1137@math{2^{25}} 1138@end tex 1139@ifnottex 11402**25 1141@end ifnottex 1142bit (the 26th bit) in the character event code indicates an 1143@acronym{ASCII} control character typed with the shift key held down. 1144 1145For letters, the basic code itself indicates upper versus lower case; 1146for digits and punctuation, the shift key selects an entirely different 1147character with a different basic code. In order to keep within the 1148@acronym{ASCII} character set whenever possible, Emacs avoids using the 1149@tex 1150@math{2^{25}} 1151@end tex 1152@ifnottex 11532**25 1154@end ifnottex 1155bit for those character events. 1156 1157However, @acronym{ASCII} provides no way to distinguish @kbd{C-A} from 1158@kbd{C-a}, so Emacs uses the 1159@tex 1160@math{2^{25}} 1161@end tex 1162@ifnottex 11632**25 1164@end ifnottex 1165bit in @kbd{C-A} and not in 1166@kbd{C-a}. 1167 1168@item hyper 1169The 1170@tex 1171@math{2^{24}} 1172@end tex 1173@ifnottex 11742**24 1175@end ifnottex 1176bit in the character event code indicates a character 1177typed with the hyper key held down. 1178 1179@item super 1180The 1181@tex 1182@math{2^{23}} 1183@end tex 1184@ifnottex 11852**23 1186@end ifnottex 1187bit in the character event code indicates a character 1188typed with the super key held down. 1189 1190@item alt 1191The 1192@tex 1193@math{2^{22}} 1194@end tex 1195@ifnottex 11962**22 1197@end ifnottex 1198bit in the character event code indicates a character typed with the 1199alt key held down. (The key labeled @key{Alt} on most keyboards is 1200actually treated as the meta key, not this.) 1201@end table 1202 1203 It is best to avoid mentioning specific bit numbers in your program. 1204To test the modifier bits of a character, use the function 1205@code{event-modifiers} (@pxref{Classifying Events}). When making key 1206bindings, you can use the read syntax for characters with modifier bits 1207(@samp{\C-}, @samp{\M-}, and so on). For making key bindings with 1208@code{define-key}, you can use lists such as @code{(control hyper ?x)} to 1209specify the characters (@pxref{Changing Key Bindings}). The function 1210@code{event-convert-list} converts such a list into an event type 1211(@pxref{Classifying Events}). 1212 1213@node Function Keys 1214@subsection Function Keys 1215 1216@cindex function keys 1217Most keyboards also have @dfn{function keys}---keys that have names or 1218symbols that are not characters. Function keys are represented in 1219Emacs Lisp as symbols; the symbol's name is the function key's label, 1220in lower case. For example, pressing a key labeled @key{F1} generates 1221an input event represented by the symbol @code{f1}. 1222 1223The event type of a function key event is the event symbol itself. 1224@xref{Classifying Events}. 1225 1226Here are a few special cases in the symbol-naming convention for 1227function keys: 1228 1229@table @asis 1230@item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete} 1231These keys correspond to common @acronym{ASCII} control characters that have 1232special keys on most keyboards. 1233 1234In @acronym{ASCII}, @kbd{C-i} and @key{TAB} are the same character. If the 1235terminal can distinguish between them, Emacs conveys the distinction to 1236Lisp programs by representing the former as the integer 9, and the 1237latter as the symbol @code{tab}. 1238 1239Most of the time, it's not useful to distinguish the two. So normally 1240@code{local-function-key-map} (@pxref{Translation Keymaps}) is set up 1241to map @code{tab} into 9. Thus, a key binding for character code 9 1242(the character @kbd{C-i}) also applies to @code{tab}. Likewise for 1243the other symbols in this group. The function @code{read-char} 1244likewise converts these events into characters. 1245 1246In @acronym{ASCII}, @key{BS} is really @kbd{C-h}. But @code{backspace} 1247converts into the character code 127 (@key{DEL}), not into code 8 1248(@key{BS}). This is what most users prefer. 1249 1250@item @code{left}, @code{up}, @code{right}, @code{down} 1251Cursor arrow keys 1252@item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{} 1253Keypad keys (to the right of the regular keyboard). 1254@item @code{kp-0}, @code{kp-1}, @dots{} 1255Keypad keys with digits. 1256@item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4} 1257Keypad PF keys. 1258@item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down} 1259Keypad arrow keys. Emacs normally translates these into the 1260corresponding non-keypad keys @code{home}, @code{left}, @dots{} 1261@item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete} 1262Additional keypad duplicates of keys ordinarily found elsewhere. Emacs 1263normally translates these into the like-named non-keypad keys. 1264@end table 1265 1266You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER}, 1267@key{META}, @key{SHIFT}, and @key{SUPER} with function keys. The way to 1268represent them is with prefixes in the symbol name: 1269 1270@table @samp 1271@item A- 1272The alt modifier. 1273@item C- 1274The control modifier. 1275@item H- 1276The hyper modifier. 1277@item M- 1278The meta modifier. 1279@item S- 1280The shift modifier. 1281@item s- 1282The super modifier. 1283@end table 1284 1285Thus, the symbol for the key @key{F3} with @key{META} held down is 1286@code{M-f3}. When you use more than one prefix, we recommend you 1287write them in alphabetical order; but the order does not matter in 1288arguments to the key-binding lookup and modification functions. 1289 1290@node Mouse Events 1291@subsection Mouse Events 1292 1293Emacs supports four kinds of mouse events: click events, drag events, 1294button-down events, and motion events. All mouse events are represented 1295as lists. The @sc{car} of the list is the event type; this says which 1296mouse button was involved, and which modifier keys were used with it. 1297The event type can also distinguish double or triple button presses 1298(@pxref{Repeat Events}). The rest of the list elements give position 1299and time information. 1300 1301For key lookup, only the event type matters: two events of the same type 1302necessarily run the same command. The command can access the full 1303values of these events using the @samp{e} interactive code. 1304@xref{Interactive Codes}. 1305 1306A key sequence that starts with a mouse event is read using the keymaps 1307of the buffer in the window that the mouse was in, not the current 1308buffer. This does not imply that clicking in a window selects that 1309window or its buffer---that is entirely under the control of the command 1310binding of the key sequence. 1311 1312@node Click Events 1313@subsection Click Events 1314@cindex click event 1315@cindex mouse click event 1316 1317When the user presses a mouse button and releases it at the same 1318location, that generates a @dfn{click} event. All mouse click event 1319share the same format: 1320 1321@example 1322(@var{event-type} @var{position} @var{click-count}) 1323@end example 1324 1325@table @asis 1326@item @var{event-type} 1327This is a symbol that indicates which mouse button was used. It is 1328one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the 1329buttons are numbered left to right. 1330 1331You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-}, 1332@samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift 1333and super, just as you would with function keys. 1334 1335This symbol also serves as the event type of the event. Key bindings 1336describe events by their types; thus, if there is a key binding for 1337@code{mouse-1}, that binding would apply to all events whose 1338@var{event-type} is @code{mouse-1}. 1339 1340@item @var{position} 1341@cindex mouse position list 1342This is a @dfn{mouse position list} specifying where the mouse click 1343occurred; see below for details. 1344 1345@item @var{click-count} 1346This is the number of rapid repeated presses so far of the same mouse 1347button. @xref{Repeat Events}. 1348@end table 1349 1350 To access the contents of a mouse position list in the 1351@var{position} slot of a click event, you should typically use the 1352functions documented in @ref{Accessing Mouse}. 1353 1354The explicit format of the list depends on where the click occurred. 1355For clicks in the text area, mode line, header line, tab line, or in 1356the fringe or marginal areas, the mouse position list has the form 1357 1358@example 1359(@var{window} @var{pos-or-area} (@var{x} . @var{y}) @var{timestamp} 1360 @var{object} @var{text-pos} (@var{col} . @var{row}) 1361 @var{image} (@var{dx} . @var{dy}) (@var{width} . @var{height})) 1362@end example 1363 1364@noindent 1365The meanings of these list elements are as follows: 1366 1367@table @asis 1368@item @var{window} 1369The window in which the click occurred. 1370 1371@item @var{pos-or-area} 1372The buffer position of the character clicked on in the text area; or, 1373if the click was outside the text area, the window area where it 1374occurred. It is one of the symbols @code{mode-line}, 1375@code{header-line}, @code{tab-line}, @code{vertical-line}, 1376@code{left-margin}, @code{right-margin}, @code{left-fringe}, or 1377@code{right-fringe}. 1378 1379In one special case, @var{pos-or-area} is a list containing a symbol 1380(one of the symbols listed above) instead of just the symbol. This 1381happens after the imaginary prefix keys for the event are registered 1382by Emacs. @xref{Key Sequence Input}. 1383 1384@item @var{x}, @var{y} 1385The relative pixel coordinates of the click. For clicks in the text 1386area of a window, the coordinate origin @code{(0 . 0)} is taken to be 1387the top left corner of the text area. @xref{Window Sizes}. For 1388clicks in a mode line, header line or tab line, the coordinate origin 1389is the top left corner of the window itself. For fringes, margins, 1390and the vertical border, @var{x} does not have meaningful data. 1391For fringes and margins, @var{y} is relative to the bottom edge of the 1392header line. In all cases, the @var{x} and @var{y} coordinates 1393increase rightward and downward respectively. 1394 1395@item @var{timestamp} 1396The time at which the event occurred, as an integer number of 1397milliseconds since a system-dependent initial time. 1398 1399@item @var{object} 1400Either @code{nil}, which means the click occurred on buffer text, or a 1401cons cell of the form @w{(@var{string} . @var{string-pos})} if there 1402is a string from a text property or an overlay at the click position. 1403 1404@table @asis 1405@item @var{string} 1406The string which was clicked on, including any properties. 1407 1408@item @var{string-pos} 1409The position in the string where the click occurred. 1410@end table 1411 1412@item @var{text-pos} 1413For clicks on a marginal area or on a fringe, this is the buffer 1414position of the first visible character in the corresponding line in 1415the window. For clicks on the mode line, the header line or the tab 1416line, this is @code{nil}. For other events, it is the buffer position 1417closest to the click. 1418 1419@item @var{col}, @var{row} 1420These are the actual column and row coordinate numbers of the glyph 1421under the @var{x}, @var{y} position. If @var{x} lies beyond the last 1422column of actual text on its line, @var{col} is reported by adding 1423fictional extra columns that have the default character width. 1424Row 0 is taken to be the header line if the window has one, or Row 1 1425if the window also has the tab line, or the topmost row of 1426the text area otherwise. Column 0 is taken to be the leftmost 1427column of the text area for clicks on a window text area, or the 1428leftmost mode line or header line column for clicks there. For clicks 1429on fringes or vertical borders, these have no meaningful data. For 1430clicks on margins, @var{col} is measured from the left edge of the 1431margin area and @var{row} is measured from the top of the margin area. 1432 1433@item @var{image} 1434If there is an image at the click location, this is the image object 1435as returned by @code{find-image} (@pxref{Defining Images}); otherwise 1436this is @code{nil}. 1437 1438@item @var{dx}, @var{dy} 1439These are the pixel coordinates of the click, relative to the top left 1440corner of @var{object}, which is @code{(0 . 0)}. If @var{object} is 1441@code{nil}, which stands for a buffer, the coordinates are relative to 1442the top left corner of the character glyph clicked on. 1443 1444@item @var{width}, @var{height} 1445These are the pixel width and height of @var{object} or, if this is 1446@code{nil}, those of the character glyph clicked on. 1447@end table 1448 1449For clicks on a scroll bar, @var{position} has this form: 1450 1451@example 1452(@var{window} @var{area} (@var{portion} . @var{whole}) @var{timestamp} @var{part}) 1453@end example 1454 1455@table @asis 1456@item @var{window} 1457The window whose scroll bar was clicked on. 1458 1459@item @var{area} 1460This is the symbol @code{vertical-scroll-bar}. 1461 1462@item @var{portion} 1463The number of pixels from the top of the scroll bar to the click 1464position. On some toolkits, including GTK+, Emacs cannot extract this 1465data, so the value is always @code{0}. 1466 1467@item @var{whole} 1468The total length, in pixels, of the scroll bar. On some toolkits, 1469including GTK+, Emacs cannot extract this data, so the value is always 1470@code{0}. 1471 1472@item @var{timestamp} 1473The time at which the event occurred, in milliseconds. On some 1474toolkits, including GTK+, Emacs cannot extract this data, so the value 1475is always @code{0}. 1476 1477@item @var{part} 1478The part of the scroll bar on which the click occurred. It is one of 1479the symbols @code{handle} (the scroll bar handle), @code{above-handle} 1480(the area above the handle), @code{below-handle} (the area below the 1481handle), @code{up} (the up arrow at one end of the scroll bar), or 1482@code{down} (the down arrow at one end of the scroll bar). 1483@c The 'top', 'bottom', and 'end-scroll' codes don't seem to be used. 1484@end table 1485 1486For clicks on the frame's internal border (@pxref{Frame Layout}), 1487@var{position} has this form: 1488 1489@example 1490 (@var{frame} @var{part} (@var{X} . @var{Y}) @var{timestamp}) 1491@end example 1492 1493@table @asis 1494@item @var{frame} 1495The frame whose internal border was clicked on. 1496 1497@item @var{part} 1498The part of the internal border which was clicked on. This can be one 1499of the following: 1500 1501@table @code 1502@item nil 1503The frame does not have an internal border. This usually happens on 1504text-mode frames. This can also happen on GUI frames with internal 1505border if the frame doesn't have its @code{drag-internal-border} 1506parameter (@pxref{Mouse Dragging Parameters}) set to a non-@code{nil} 1507value. 1508 1509@item left-edge 1510@itemx top-edge 1511@itemx right-edge 1512@itemx bottom-edge 1513The click was on the corresponding border at an offset of at least one 1514canonical character from the border's nearest corner. 1515 1516@item top-left-corner 1517@itemx top-right-corner 1518@itemx bottom-right-corner 1519@itemx bottom-left-corner 1520The click was on the corresponding corner of the internal border. 1521@end table 1522 1523@end table 1524 1525 1526@node Drag Events 1527@subsection Drag Events 1528@cindex drag event 1529@cindex mouse drag event 1530 1531With Emacs, you can have a drag event without even changing your 1532clothes. A @dfn{drag event} happens every time the user presses a mouse 1533button and then moves the mouse to a different character position before 1534releasing the button. Like all mouse events, drag events are 1535represented in Lisp as lists. The lists record both the starting mouse 1536position and the final position, like this: 1537 1538@example 1539(@var{event-type} 1540 (@var{window1} START-POSITION) 1541 (@var{window2} END-POSITION)) 1542@end example 1543 1544For a drag event, the name of the symbol @var{event-type} contains the 1545prefix @samp{drag-}. For example, dragging the mouse with button 2 1546held down generates a @code{drag-mouse-2} event. The second and third 1547elements of the event give the starting and ending position of the 1548drag, as mouse position lists (@pxref{Click Events}). You can access 1549the second element of any mouse event in the same way. However, the 1550drag event may end outside the boundaries of the frame that was 1551initially selected. In that case, the third element's position list 1552contains that frame in place of a window. 1553 1554The @samp{drag-} prefix follows the modifier key prefixes such as 1555@samp{C-} and @samp{M-}. 1556 1557If @code{read-key-sequence} receives a drag event that has no key 1558binding, and the corresponding click event does have a binding, it 1559changes the drag event into a click event at the drag's starting 1560position. This means that you don't have to distinguish between click 1561and drag events unless you want to. 1562 1563@node Button-Down Events 1564@subsection Button-Down Events 1565@cindex button-down event 1566 1567Click and drag events happen when the user releases a mouse button. 1568They cannot happen earlier, because there is no way to distinguish a 1569click from a drag until the button is released. 1570 1571If you want to take action as soon as a button is pressed, you need to 1572handle @dfn{button-down} events.@footnote{Button-down is the 1573conservative antithesis of drag.} These occur as soon as a button is 1574pressed. They are represented by lists that look exactly like click 1575events (@pxref{Click Events}), except that the @var{event-type} symbol 1576name contains the prefix @samp{down-}. The @samp{down-} prefix follows 1577modifier key prefixes such as @samp{C-} and @samp{M-}. 1578 1579The function @code{read-key-sequence} ignores any button-down events 1580that don't have command bindings; therefore, the Emacs command loop 1581ignores them too. This means that you need not worry about defining 1582button-down events unless you want them to do something. The usual 1583reason to define a button-down event is so that you can track mouse 1584motion (by reading motion events) until the button is released. 1585@xref{Motion Events}. 1586 1587@node Repeat Events 1588@subsection Repeat Events 1589@cindex repeat events 1590@cindex double-click events 1591@cindex triple-click events 1592@cindex mouse events, repeated 1593 1594If you press the same mouse button more than once in quick succession 1595without moving the mouse, Emacs generates special @dfn{repeat} mouse 1596events for the second and subsequent presses. 1597 1598The most common repeat events are @dfn{double-click} events. Emacs 1599generates a double-click event when you click a button twice; the event 1600happens when you release the button (as is normal for all click 1601events). 1602 1603The event type of a double-click event contains the prefix 1604@samp{double-}. Thus, a double click on the second mouse button with 1605@key{meta} held down comes to the Lisp program as 1606@code{M-double-mouse-2}. If a double-click event has no binding, the 1607binding of the corresponding ordinary click event is used to execute 1608it. Thus, you need not pay attention to the double click feature 1609unless you really want to. 1610 1611When the user performs a double click, Emacs generates first an ordinary 1612click event, and then a double-click event. Therefore, you must design 1613the command binding of the double click event to assume that the 1614single-click command has already run. It must produce the desired 1615results of a double click, starting from the results of a single click. 1616 1617This is convenient, if the meaning of a double click somehow builds 1618on the meaning of a single click---which is recommended user interface 1619design practice for double clicks. 1620 1621If you click a button, then press it down again and start moving the 1622mouse with the button held down, then you get a @dfn{double-drag} event 1623when you ultimately release the button. Its event type contains 1624@samp{double-drag} instead of just @samp{drag}. If a double-drag event 1625has no binding, Emacs looks for an alternate binding as if the event 1626were an ordinary drag. 1627 1628Before the double-click or double-drag event, Emacs generates a 1629@dfn{double-down} event when the user presses the button down for the 1630second time. Its event type contains @samp{double-down} instead of just 1631@samp{down}. If a double-down event has no binding, Emacs looks for an 1632alternate binding as if the event were an ordinary button-down event. 1633If it finds no binding that way either, the double-down event is 1634ignored. 1635 1636To summarize, when you click a button and then press it again right 1637away, Emacs generates a down event and a click event for the first 1638click, a double-down event when you press the button again, and finally 1639either a double-click or a double-drag event. 1640 1641If you click a button twice and then press it again, all in quick 1642succession, Emacs generates a @dfn{triple-down} event, followed by 1643either a @dfn{triple-click} or a @dfn{triple-drag}. The event types of 1644these events contain @samp{triple} instead of @samp{double}. If any 1645triple event has no binding, Emacs uses the binding that it would use 1646for the corresponding double event. 1647 1648If you click a button three or more times and then press it again, the 1649events for the presses beyond the third are all triple events. Emacs 1650does not have separate event types for quadruple, quintuple, etc.@: 1651events. However, you can look at the event list to find out precisely 1652how many times the button was pressed. 1653 1654@defun event-click-count event 1655This function returns the number of consecutive button presses that led 1656up to @var{event}. If @var{event} is a double-down, double-click or 1657double-drag event, the value is 2. If @var{event} is a triple event, 1658the value is 3 or greater. If @var{event} is an ordinary mouse event 1659(not a repeat event), the value is 1. 1660@end defun 1661 1662@defopt double-click-fuzz 1663To generate repeat events, successive mouse button presses must be at 1664approximately the same screen position. The value of 1665@code{double-click-fuzz} specifies the maximum number of pixels the 1666mouse may be moved (horizontally or vertically) between two successive 1667clicks to make a double-click. 1668 1669This variable is also the threshold for motion of the mouse to count 1670as a drag. 1671@end defopt 1672 1673@defopt double-click-time 1674To generate repeat events, the number of milliseconds between 1675successive button presses must be less than the value of 1676@code{double-click-time}. Setting @code{double-click-time} to 1677@code{nil} disables multi-click detection entirely. Setting it to 1678@code{t} removes the time limit; Emacs then detects multi-clicks by 1679position only. 1680@end defopt 1681 1682@node Motion Events 1683@subsection Motion Events 1684@cindex motion event 1685@cindex mouse motion events 1686 1687Emacs sometimes generates @dfn{mouse motion} events to describe motion 1688of the mouse without any button activity. Mouse motion events are 1689represented by lists that look like this: 1690 1691@example 1692(mouse-movement POSITION) 1693@end example 1694 1695@noindent 1696@var{position} is a mouse position list (@pxref{Click Events}), 1697specifying the current position of the mouse cursor. As with the 1698end-position of a drag event, this position list may represent a 1699location outside the boundaries of the initially selected frame, in 1700which case the list contains that frame in place of a window. 1701 1702The special form @code{track-mouse} enables generation of motion 1703events within its body. Outside of @code{track-mouse} forms, Emacs 1704does not generate events for mere motion of the mouse, and these 1705events do not appear. @xref{Mouse Tracking}. 1706 1707@defvar mouse-fine-grained-tracking 1708When non-@code{nil}, mouse motion events are generated even for very 1709small movements. Otherwise, motion events are not generated as long 1710as the mouse cursor remains pointing to the same glyph in the text. 1711@end defvar 1712 1713@node Focus Events 1714@subsection Focus Events 1715@cindex focus event 1716 1717Window systems provide general ways for the user to control which window 1718gets keyboard input. This choice of window is called the @dfn{focus}. 1719When the user does something to switch between Emacs frames, that 1720generates a @dfn{focus event}. The normal definition of a focus event, 1721in the global keymap, is to select a new frame within Emacs, as the user 1722would expect. @xref{Input Focus}, which also describes hooks related 1723to focus events. 1724 1725Focus events are represented in Lisp as lists that look like this: 1726 1727@example 1728(switch-frame @var{new-frame}) 1729@end example 1730 1731@noindent 1732where @var{new-frame} is the frame switched to. 1733 1734Some X window managers are set up so that just moving the mouse into a 1735window is enough to set the focus there. Usually, there is no need 1736for a Lisp program to know about the focus change until some other 1737kind of input arrives. Emacs generates a focus event only when the 1738user actually types a keyboard key or presses a mouse button in the 1739new frame; just moving the mouse between frames does not generate a 1740focus event. 1741 1742A focus event in the middle of a key sequence would garble the 1743sequence. So Emacs never generates a focus event in the middle of a key 1744sequence. If the user changes focus in the middle of a key 1745sequence---that is, after a prefix key---then Emacs reorders the events 1746so that the focus event comes either before or after the multi-event key 1747sequence, and not within it. 1748 1749@node Misc Events 1750@subsection Miscellaneous System Events 1751 1752A few other event types represent occurrences within the system. 1753 1754@table @code 1755@cindex @code{delete-frame} event 1756@item (delete-frame (@var{frame})) 1757This kind of event indicates that the user gave the window manager 1758a command to delete a particular window, which happens to be an Emacs frame. 1759 1760The standard definition of the @code{delete-frame} event is to delete @var{frame}. 1761 1762@cindex @code{iconify-frame} event 1763@item (iconify-frame (@var{frame})) 1764This kind of event indicates that the user iconified @var{frame} using 1765the window manager. Its standard definition is @code{ignore}; since the 1766frame has already been iconified, Emacs has no work to do. The purpose 1767of this event type is so that you can keep track of such events if you 1768want to. 1769 1770@cindex @code{make-frame-visible} event 1771@item (make-frame-visible (@var{frame})) 1772This kind of event indicates that the user deiconified @var{frame} using 1773the window manager. Its standard definition is @code{ignore}; since the 1774frame has already been made visible, Emacs has no work to do. 1775 1776@cindex @code{wheel-up} event 1777@cindex @code{wheel-down} event 1778@item (wheel-up @var{position}) 1779@itemx (wheel-down @var{position}) 1780These kinds of event are generated by moving a mouse wheel. The 1781@var{position} element is a mouse position list (@pxref{Click 1782Events}), specifying the position of the mouse cursor when the event 1783occurred. 1784 1785@vindex mouse-wheel-up-event 1786@vindex mouse-wheel-down-event 1787This kind of event is generated only on some kinds of systems. On some 1788systems, @code{mouse-4} and @code{mouse-5} are used instead. For 1789portable code, use the variables @code{mouse-wheel-up-event} and 1790@code{mouse-wheel-down-event} defined in @file{mwheel.el} to determine 1791what event types to expect for the mouse wheel. 1792 1793@cindex @code{drag-n-drop} event 1794@item (drag-n-drop @var{position} @var{files}) 1795This kind of event is generated when a group of files is 1796selected in an application outside of Emacs, and then dragged and 1797dropped onto an Emacs frame. 1798 1799The element @var{position} is a list describing the position of the 1800event, in the same format as used in a mouse-click event (@pxref{Click 1801Events}), and @var{files} is the list of file names that were dragged 1802and dropped. The usual way to handle this event is by visiting these 1803files. 1804 1805This kind of event is generated, at present, only on some kinds of 1806systems. 1807 1808@cindex @code{help-echo} event 1809@item help-echo 1810This kind of event is generated when a mouse pointer moves onto a 1811portion of buffer text which has a @code{help-echo} text property. 1812The generated event has this form: 1813 1814@example 1815(help-echo @var{frame} @var{help} @var{window} @var{object} @var{pos}) 1816@end example 1817 1818@noindent 1819The precise meaning of the event parameters and the way these 1820parameters are used to display the help-echo text are described in 1821@ref{Text help-echo}. 1822 1823@cindex @code{sigusr1} event 1824@cindex @code{sigusr2} event 1825@cindex user signals 1826@item sigusr1 1827@itemx sigusr2 1828These events are generated when the Emacs process receives 1829the signals @code{SIGUSR1} and @code{SIGUSR2}. They contain no 1830additional data because signals do not carry additional information. 1831They can be useful for debugging (@pxref{Error Debugging}). 1832 1833To catch a user signal, bind the corresponding event to an interactive 1834command in the @code{special-event-map} (@pxref{Controlling Active Maps}). 1835The command is called with no arguments, and the specific signal event is 1836available in @code{last-input-event} (@pxref{Event Input Misc}. For 1837example: 1838 1839@smallexample 1840(defun sigusr-handler () 1841 (interactive) 1842 (message "Caught signal %S" last-input-event)) 1843 1844(define-key special-event-map [sigusr1] 'sigusr-handler) 1845@end smallexample 1846 1847To test the signal handler, you can make Emacs send a signal to itself: 1848 1849@smallexample 1850(signal-process (emacs-pid) 'sigusr1) 1851@end smallexample 1852 1853@cindex @code{language-change} event 1854@item language-change 1855This kind of event is generated on MS-Windows when the input language 1856has changed. This typically means that the keyboard keys will send to 1857Emacs characters from a different language. The generated event has 1858this form: 1859 1860@smallexample 1861(language-change @var{frame} @var{codepage} @var{language-id}) 1862@end smallexample 1863 1864@noindent 1865Here @var{frame} is the frame which was current when the input 1866language changed; @var{codepage} is the new codepage number; and 1867@var{language-id} is the numerical ID of the new input language. The 1868coding-system (@pxref{Coding Systems}) that corresponds to 1869@var{codepage} is @code{cp@var{codepage}} or 1870@code{windows-@var{codepage}}. To convert @var{language-id} to a 1871string (e.g., to use it for various language-dependent features, such 1872as @code{set-language-environment}), use the 1873@code{w32-get-locale-info} function, like this: 1874 1875@smallexample 1876;; Get the abbreviated language name, such as "ENU" for English 1877(w32-get-locale-info language-id) 1878;; Get the full English name of the language, 1879;; such as "English (United States)" 1880(w32-get-locale-info language-id 4097) 1881;; Get the full localized name of the language 1882(w32-get-locale-info language-id t) 1883@end smallexample 1884@end table 1885 1886 If one of these events arrives in the middle of a key sequence---that 1887is, after a prefix key---then Emacs reorders the events so that this 1888event comes either before or after the multi-event key sequence, not 1889within it. 1890 1891 Some of these special events, such as @code{delete-frame}, invoke 1892Emacs commands by default; others are not bound. If you want to 1893arrange for a special event to invoke a command, you can do that via 1894@code{special-event-map}. The command you bind to a function key in 1895that map can then examine the full event which invoked it in 1896@code{last-input-event}. @xref{Special Events}. 1897 1898@node Event Examples 1899@subsection Event Examples 1900 1901If the user presses and releases the left mouse button over the same 1902location, that generates a sequence of events like this: 1903 1904@smallexample 1905(down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320)) 1906(mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180)) 1907@end smallexample 1908 1909While holding the control key down, the user might hold down the 1910second mouse button, and drag the mouse from one line to the next. 1911That produces two events, as shown here: 1912 1913@smallexample 1914(C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)) 1915(C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219) 1916 (#<window 18 on NEWS> 3510 (0 . 28) -729648)) 1917@end smallexample 1918 1919While holding down the meta and shift keys, the user might press the 1920second mouse button on the window's mode line, and then drag the mouse 1921into another window. That produces a pair of events like these: 1922 1923@smallexample 1924(M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)) 1925(M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844) 1926 (#<window 20 on carlton-sanskrit.tex> 161 (33 . 3) 1927 -453816)) 1928@end smallexample 1929 1930The frame with input focus might not take up the entire screen, and 1931the user might move the mouse outside the scope of the frame. Inside 1932the @code{track-mouse} special form, that produces an event like this: 1933 1934@smallexample 1935(mouse-movement (#<frame *ielm* 0x102849a30> nil (563 . 205) 532301936)) 1936@end smallexample 1937 1938To handle a SIGUSR1 signal, define an interactive function, and 1939bind it to the @code{signal usr1} event sequence: 1940 1941@smallexample 1942(defun usr1-handler () 1943 (interactive) 1944 (message "Got USR1 signal")) 1945(global-set-key [signal usr1] 'usr1-handler) 1946@end smallexample 1947 1948@node Classifying Events 1949@subsection Classifying Events 1950@cindex event type 1951@cindex classifying events 1952 1953 Every event has an @dfn{event type}, which classifies the event for 1954key binding purposes. For a keyboard event, the event type equals the 1955event value; thus, the event type for a character is the character, and 1956the event type for a function key symbol is the symbol itself. For 1957events that are lists, the event type is the symbol in the @sc{car} of 1958the list. Thus, the event type is always a symbol or a character. 1959 1960 Two events of the same type are equivalent where key bindings are 1961concerned; thus, they always run the same command. That does not 1962necessarily mean they do the same things, however, as some commands look 1963at the whole event to decide what to do. For example, some commands use 1964the location of a mouse event to decide where in the buffer to act. 1965 1966 Sometimes broader classifications of events are useful. For example, 1967you might want to ask whether an event involved the @key{META} key, 1968regardless of which other key or mouse button was used. 1969 1970 The functions @code{event-modifiers} and @code{event-basic-type} are 1971provided to get such information conveniently. 1972 1973@defun event-modifiers event 1974This function returns a list of the modifiers that @var{event} has. The 1975modifiers are symbols; they include @code{shift}, @code{control}, 1976@code{meta}, @code{alt}, @code{hyper} and @code{super}. In addition, 1977the modifiers list of a mouse event symbol always contains one of 1978@code{click}, @code{drag}, and @code{down}. For double or triple 1979events, it also contains @code{double} or @code{triple}. 1980 1981The argument @var{event} may be an entire event object, or just an 1982event type. If @var{event} is a symbol that has never been used in an 1983event that has been read as input in the current Emacs session, then 1984@code{event-modifiers} can return @code{nil}, even when @var{event} 1985actually has modifiers. 1986 1987Here are some examples: 1988 1989@example 1990(event-modifiers ?a) 1991 @result{} nil 1992(event-modifiers ?A) 1993 @result{} (shift) 1994(event-modifiers ?\C-a) 1995 @result{} (control) 1996(event-modifiers ?\C-%) 1997 @result{} (control) 1998(event-modifiers ?\C-\S-a) 1999 @result{} (control shift) 2000(event-modifiers 'f5) 2001 @result{} nil 2002(event-modifiers 's-f5) 2003 @result{} (super) 2004(event-modifiers 'M-S-f5) 2005 @result{} (meta shift) 2006(event-modifiers 'mouse-1) 2007 @result{} (click) 2008(event-modifiers 'down-mouse-1) 2009 @result{} (down) 2010@end example 2011 2012The modifiers list for a click event explicitly contains @code{click}, 2013but the event symbol name itself does not contain @samp{click}. 2014Similarly, the modifiers list for an @acronym{ASCII} control 2015character, such as @samp{C-a}, contains @code{control}, even though 2016reading such an event via @code{read-char} will return the value 1 2017with the control modifier bit removed. 2018@end defun 2019 2020@defun event-basic-type event 2021This function returns the key or mouse button that @var{event} 2022describes, with all modifiers removed. The @var{event} argument is as 2023in @code{event-modifiers}. For example: 2024 2025@example 2026(event-basic-type ?a) 2027 @result{} 97 2028(event-basic-type ?A) 2029 @result{} 97 2030(event-basic-type ?\C-a) 2031 @result{} 97 2032(event-basic-type ?\C-\S-a) 2033 @result{} 97 2034(event-basic-type 'f5) 2035 @result{} f5 2036(event-basic-type 's-f5) 2037 @result{} f5 2038(event-basic-type 'M-S-f5) 2039 @result{} f5 2040(event-basic-type 'down-mouse-1) 2041 @result{} mouse-1 2042@end example 2043@end defun 2044 2045@defun mouse-movement-p object 2046This function returns non-@code{nil} if @var{object} is a mouse movement 2047event. @xref{Motion Events}. 2048@end defun 2049 2050@defun event-convert-list list 2051This function converts a list of modifier names and a basic event type 2052to an event type which specifies all of them. The basic event type 2053must be the last element of the list. For example, 2054 2055@example 2056(event-convert-list '(control ?a)) 2057 @result{} 1 2058(event-convert-list '(control meta ?a)) 2059 @result{} -134217727 2060(event-convert-list '(control super f1)) 2061 @result{} C-s-f1 2062@end example 2063@end defun 2064 2065@node Accessing Mouse 2066@subsection Accessing Mouse Events 2067@cindex mouse events, data in 2068@cindex keyboard events, data in 2069 2070 This section describes convenient functions for accessing the data in 2071a mouse button or motion event. Keyboard event data can be accessed 2072using the same functions, but data elements that aren't applicable to 2073keyboard events are zero or @code{nil}. 2074 2075 The following two functions return a mouse position list 2076(@pxref{Click Events}), specifying the position of a mouse event. 2077 2078@defun event-start event 2079This returns the starting position of @var{event}. 2080 2081If @var{event} is a click or button-down event, this returns the 2082location of the event. If @var{event} is a drag event, this returns the 2083drag's starting position. 2084@end defun 2085 2086@defun event-end event 2087This returns the ending position of @var{event}. 2088 2089If @var{event} is a drag event, this returns the position where the user 2090released the mouse button. If @var{event} is a click or button-down 2091event, the value is actually the starting position, which is the only 2092position such events have. 2093@end defun 2094 2095@defun posnp object 2096This function returns non-@code{nil} if @var{object} is a mouse 2097position list, in the format documented in @ref{Click Events}); and 2098@code{nil} otherwise. 2099@end defun 2100 2101@cindex mouse position list, accessing 2102 These functions take a mouse position list as argument, and return 2103various parts of it: 2104 2105@defun posn-window position 2106Return the window that @var{position} is in. If @var{position} 2107represents a location outside the frame where the event was initiated, 2108return that frame instead. 2109@end defun 2110 2111@defun posn-area position 2112Return the window area recorded in @var{position}. It returns @code{nil} 2113when the event occurred in the text area of the window; otherwise, it 2114is a symbol identifying the area in which the event occurred. 2115@end defun 2116 2117@defun posn-point position 2118Return the buffer position in @var{position}. When the event occurred 2119in the text area of the window, in a marginal area, or on a fringe, 2120this is an integer specifying a buffer position. Otherwise, the value 2121is undefined. 2122@end defun 2123 2124@defun posn-x-y position 2125Return the pixel-based x and y coordinates in @var{position}, as a 2126cons cell @w{@code{(@var{x} . @var{y})}}. These coordinates are 2127relative to the window given by @code{posn-window}. 2128 2129This example shows how to convert the window-relative coordinates in 2130the text area of a window into frame-relative coordinates: 2131 2132@example 2133(defun frame-relative-coordinates (position) 2134 "Return frame-relative coordinates from POSITION. 2135POSITION is assumed to lie in a window text area." 2136 (let* ((x-y (posn-x-y position)) 2137 (window (posn-window position)) 2138 (edges (window-inside-pixel-edges window))) 2139 (cons (+ (car x-y) (car edges)) 2140 (+ (cdr x-y) (cadr edges))))) 2141@end example 2142@end defun 2143 2144@defun posn-col-row position 2145This function returns a cons cell @w{@code{(@var{col} . @var{row})}}, 2146containing the estimated column and row corresponding to buffer 2147position described by @var{position}. The return value is given in 2148units of the frame's default character width and default line height 2149(including spacing), as computed from the @var{x} and @var{y} values 2150corresponding to @var{position}. (So, if the actual characters have 2151non-default sizes, the actual row and column may differ from these 2152computed values.) 2153 2154Note that @var{row} is counted from the top of the text area. If the 2155window given by @var{position} possesses a header line (@pxref{Header 2156Lines}) or a tab line, they are @emph{not} included in the @var{row} 2157count. 2158@end defun 2159 2160@defun posn-actual-col-row position 2161Return the actual row and column in @var{position}, as a cons cell 2162@w{@code{(@var{col} . @var{row})}}. The values are the actual row and 2163column numbers in the window given by @var{position}. @xref{Click 2164Events}, for details. The function returns @code{nil} if 2165@var{position} does not include actual position values; in that case 2166@code{posn-col-row} can be used to get approximate values. 2167 2168Note that this function doesn't account for the visual width of 2169characters on display, like the number of visual columns taken by a 2170tab character or an image. If you need the coordinates in canonical 2171character units, use @code{posn-col-row} instead. 2172@end defun 2173 2174@defun posn-string position 2175Return the string object described by @var{position}, either 2176@code{nil} (which means @var{position} describes buffer text), or a 2177cons cell @w{@code{(@var{string} . @var{string-pos})}}. 2178@end defun 2179 2180@defun posn-image position 2181Return the image object in @var{position}, either @code{nil} (if 2182there's no image at @var{position}), or an image spec @w{@code{(image 2183@dots{})}}. 2184@end defun 2185 2186@defun posn-object position 2187Return the image or string object described by @var{position}, either 2188@code{nil} (which means @var{position} describes buffer text), an 2189image @w{@code{(image @dots{})}}, or a cons cell 2190@w{@code{(@var{string} . @var{string-pos})}}. 2191@end defun 2192 2193@defun posn-object-x-y position 2194Return the pixel-based x and y coordinates relative to the upper left 2195corner of the object described by @var{position}, as a cons cell 2196@w{@code{(@var{dx} . @var{dy})}}. If the @var{position} describes 2197buffer text, return the relative coordinates of the buffer-text character 2198closest to that position. 2199@end defun 2200 2201@defun posn-object-width-height position 2202Return the pixel width and height of the object described by 2203@var{position}, as a cons cell @code{(@var{width} . @var{height})}. 2204If the @var{position} describes a buffer position, return the size of 2205the character at that position. 2206@end defun 2207 2208@cindex timestamp of a mouse event 2209@defun posn-timestamp position 2210Return the timestamp in @var{position}. This is the time at which the 2211event occurred, in milliseconds. 2212@end defun 2213 2214 These functions compute a position list given particular buffer 2215position or screen position. You can access the data in this position 2216list with the functions described above. 2217 2218@defun posn-at-point &optional pos window 2219This function returns a position list for position @var{pos} in 2220@var{window}. @var{pos} defaults to point in @var{window}; 2221@var{window} defaults to the selected window. 2222 2223@code{posn-at-point} returns @code{nil} if @var{pos} is not visible in 2224@var{window}. 2225@end defun 2226 2227@defun posn-at-x-y x y &optional frame-or-window whole 2228This function returns position information corresponding to pixel 2229coordinates @var{x} and @var{y} in a specified frame or window, 2230@var{frame-or-window}, which defaults to the selected window. 2231The coordinates @var{x} and @var{y} are relative to the 2232frame or window used. 2233If @var{whole} is @code{nil}, the coordinates are relative 2234to the window text area, otherwise they are relative to 2235the entire window area including scroll bars, margins and fringes. 2236@end defun 2237 2238@node Accessing Scroll 2239@subsection Accessing Scroll Bar Events 2240@cindex scroll bar events, data in 2241 2242 These functions are useful for decoding scroll bar events. 2243 2244@defun scroll-bar-event-ratio event 2245This function returns the fractional vertical position of a scroll bar 2246event within the scroll bar. The value is a cons cell 2247@code{(@var{portion} . @var{whole})} containing two integers whose ratio 2248is the fractional position. 2249@end defun 2250 2251@defun scroll-bar-scale ratio total 2252This function multiplies (in effect) @var{ratio} by @var{total}, 2253rounding the result to an integer. The argument @var{ratio} is not a 2254number, but rather a pair @code{(@var{num} . @var{denom})}---typically a 2255value returned by @code{scroll-bar-event-ratio}. 2256 2257This function is handy for scaling a position on a scroll bar into a 2258buffer position. Here's how to do that: 2259 2260@example 2261(+ (point-min) 2262 (scroll-bar-scale 2263 (posn-x-y (event-start event)) 2264 (- (point-max) (point-min)))) 2265@end example 2266 2267Recall that scroll bar events have two integers forming a ratio, in place 2268of a pair of x and y coordinates. 2269@end defun 2270 2271@node Strings of Events 2272@subsection Putting Keyboard Events in Strings 2273@cindex keyboard events in strings 2274@cindex strings with keyboard events 2275 2276 In most of the places where strings are used, we conceptualize the 2277string as containing text characters---the same kind of characters found 2278in buffers or files. Occasionally Lisp programs use strings that 2279conceptually contain keyboard characters; for example, they may be key 2280sequences or keyboard macro definitions. However, storing keyboard 2281characters in a string is a complex matter, for reasons of historical 2282compatibility, and it is not always possible. 2283 2284 We recommend that new programs avoid dealing with these complexities 2285by not storing keyboard events in strings. Here is how to do that: 2286 2287@itemize @bullet 2288@item 2289Use vectors instead of strings for key sequences, when you plan to use 2290them for anything other than as arguments to @code{lookup-key} and 2291@code{define-key}. For example, you can use 2292@code{read-key-sequence-vector} instead of @code{read-key-sequence}, and 2293@code{this-command-keys-vector} instead of @code{this-command-keys}. 2294 2295@item 2296Use vectors to write key sequence constants containing meta characters, 2297even when passing them directly to @code{define-key}. 2298 2299@item 2300When you have to look at the contents of a key sequence that might be a 2301string, use @code{listify-key-sequence} (@pxref{Event Input Misc}) 2302first, to convert it to a list. 2303@end itemize 2304 2305 The complexities stem from the modifier bits that keyboard input 2306characters can include. Aside from the Meta modifier, none of these 2307modifier bits can be included in a string, and the Meta modifier is 2308allowed only in special cases. 2309 2310 The earliest GNU Emacs versions represented meta characters as codes 2311in the range of 128 to 255. At that time, the basic character codes 2312ranged from 0 to 127, so all keyboard character codes did fit in a 2313string. Many Lisp programs used @samp{\M-} in string constants to stand 2314for meta characters, especially in arguments to @code{define-key} and 2315similar functions, and key sequences and sequences of events were always 2316represented as strings. 2317 2318 When we added support for larger basic character codes beyond 127, and 2319additional modifier bits, we had to change the representation of meta 2320characters. Now the flag that represents the Meta modifier in a 2321character is 2322@tex 2323@math{2^{27}} 2324@end tex 2325@ifnottex 23262**27 2327@end ifnottex 2328and such numbers cannot be included in a string. 2329 2330 To support programs with @samp{\M-} in string constants, there are 2331special rules for including certain meta characters in a string. 2332Here are the rules for interpreting a string as a sequence of input 2333characters: 2334 2335@itemize @bullet 2336@item 2337If the keyboard character value is in the range of 0 to 127, it can go 2338in the string unchanged. 2339 2340@item 2341The meta variants of those characters, with codes in the range of 2342@tex 2343@math{2^{27}} 2344@end tex 2345@ifnottex 23462**27 2347@end ifnottex 2348to 2349@tex 2350@math{2^{27} + 127}, 2351@end tex 2352@ifnottex 23532**27+127, 2354@end ifnottex 2355can also go in the string, but you must change their 2356numeric values. You must set the 2357@tex 2358@math{2^{7}} 2359@end tex 2360@ifnottex 23612**7 2362@end ifnottex 2363bit instead of the 2364@tex 2365@math{2^{27}} 2366@end tex 2367@ifnottex 23682**27 2369@end ifnottex 2370bit, resulting in a value between 128 and 255. Only a unibyte string 2371can include these codes. 2372 2373@item 2374Non-@acronym{ASCII} characters above 256 can be included in a multibyte string. 2375 2376@item 2377Other keyboard character events cannot fit in a string. This includes 2378keyboard events in the range of 128 to 255. 2379@end itemize 2380 2381 Functions such as @code{read-key-sequence} that construct strings of 2382keyboard input characters follow these rules: they construct vectors 2383instead of strings, when the events won't fit in a string. 2384 2385 When you use the read syntax @samp{\M-} in a string, it produces a 2386code in the range of 128 to 255---the same code that you get if you 2387modify the corresponding keyboard event to put it in the string. Thus, 2388meta events in strings work consistently regardless of how they get into 2389the strings. 2390 2391 However, most programs would do well to avoid these issues by 2392following the recommendations at the beginning of this section. 2393 2394@node Reading Input 2395@section Reading Input 2396@cindex read input 2397@cindex keyboard input 2398 2399 The editor command loop reads key sequences using the function 2400@code{read-key-sequence}, which uses @code{read-event}. These and other 2401functions for event input are also available for use in Lisp programs. 2402See also @code{momentary-string-display} in @ref{Temporary Displays}, 2403and @code{sit-for} in @ref{Waiting}. @xref{Terminal Input}, for 2404functions and variables for controlling terminal input modes and 2405debugging terminal input. 2406 2407 For higher-level input facilities, see @ref{Minibuffers}. 2408 2409@menu 2410* Key Sequence Input:: How to read one key sequence. 2411* Reading One Event:: How to read just one event. 2412* Event Mod:: How Emacs modifies events as they are read. 2413* Invoking the Input Method:: How reading an event uses the input method. 2414* Quoted Character Input:: Asking the user to specify a character. 2415* Event Input Misc:: How to reread or throw away input events. 2416@end menu 2417 2418@node Key Sequence Input 2419@subsection Key Sequence Input 2420@cindex key sequence input 2421 2422 The command loop reads input a key sequence at a time, by calling 2423@code{read-key-sequence}. Lisp programs can also call this function; 2424for example, @code{describe-key} uses it to read the key to describe. 2425 2426@defun read-key-sequence prompt &optional continue-echo dont-downcase-last switch-frame-ok command-loop 2427This function reads a key sequence and returns it as a string or 2428vector. It keeps reading events until it has accumulated a complete key 2429sequence; that is, enough to specify a non-prefix command using the 2430currently active keymaps. (Remember that a key sequence that starts 2431with a mouse event is read using the keymaps of the buffer in the 2432window that the mouse was in, not the current buffer.) 2433 2434If the events are all characters and all can fit in a string, then 2435@code{read-key-sequence} returns a string (@pxref{Strings of Events}). 2436Otherwise, it returns a vector, since a vector can hold all kinds of 2437events---characters, symbols, and lists. The elements of the string or 2438vector are the events in the key sequence. 2439 2440Reading a key sequence includes translating the events in various 2441ways. @xref{Translation Keymaps}. 2442 2443The argument @var{prompt} is either a string to be displayed in the 2444echo area as a prompt, or @code{nil}, meaning not to display a prompt. 2445The argument @var{continue-echo}, if non-@code{nil}, means to echo 2446this key as a continuation of the previous key. 2447 2448Normally any upper case event is converted to lower case if the 2449original event is undefined and the lower case equivalent is defined. 2450The argument @var{dont-downcase-last}, if non-@code{nil}, means do not 2451convert the last event to lower case. This is appropriate for reading 2452a key sequence to be defined. 2453 2454The argument @var{switch-frame-ok}, if non-@code{nil}, means that this 2455function should process a @code{switch-frame} event if the user 2456switches frames before typing anything. If the user switches frames 2457in the middle of a key sequence, or at the start of the sequence but 2458@var{switch-frame-ok} is @code{nil}, then the event will be put off 2459until after the current key sequence. 2460 2461The argument @var{command-loop}, if non-@code{nil}, means that this 2462key sequence is being read by something that will read commands one 2463after another. It should be @code{nil} if the caller will read just 2464one key sequence. 2465 2466In the following example, Emacs displays the prompt @samp{?} in the 2467echo area, and then the user types @kbd{C-x C-f}. 2468 2469@example 2470(read-key-sequence "?") 2471 2472@group 2473---------- Echo Area ---------- 2474?@kbd{C-x C-f} 2475---------- Echo Area ---------- 2476 2477 @result{} "^X^F" 2478@end group 2479@end example 2480 2481The function @code{read-key-sequence} suppresses quitting: @kbd{C-g} 2482typed while reading with this function works like any other character, 2483and does not set @code{quit-flag}. @xref{Quitting}. 2484@end defun 2485 2486@defun read-key-sequence-vector prompt &optional continue-echo dont-downcase-last switch-frame-ok command-loop 2487This is like @code{read-key-sequence} except that it always 2488returns the key sequence as a vector, never as a string. 2489@xref{Strings of Events}. 2490@end defun 2491 2492@cindex upper case key sequence 2493@cindex downcasing in @code{lookup-key} 2494@cindex shift-translation 2495If an input character is upper-case (or has the shift modifier) and 2496has no key binding, but its lower-case equivalent has one, then 2497@code{read-key-sequence} converts the character to lower case. Note 2498that @code{lookup-key} does not perform case conversion in this way. 2499 2500@vindex this-command-keys-shift-translated 2501When reading input results in such a @dfn{shift-translation}, Emacs 2502sets the variable @code{this-command-keys-shift-translated} to a 2503non-@code{nil} value. Lisp programs can examine this variable if they 2504need to modify their behavior when invoked by shift-translated keys. 2505For example, the function @code{handle-shift-selection} examines the 2506value of this variable to determine how to activate or deactivate the 2507region (@pxref{The Mark, handle-shift-selection}). 2508 2509The function @code{read-key-sequence} also transforms some mouse events. 2510It converts unbound drag events into click events, and discards unbound 2511button-down events entirely. It also reshuffles focus events and 2512miscellaneous window events so that they never appear in a key sequence 2513with any other events. 2514 2515@cindex @code{tab-line}, prefix key 2516@cindex @code{header-line}, prefix key 2517@cindex @code{mode-line}, prefix key 2518@cindex @code{vertical-line}, prefix key 2519@cindex @code{horizontal-scroll-bar}, prefix key 2520@cindex @code{vertical-scroll-bar}, prefix key 2521@cindex @code{menu-bar}, prefix key 2522@cindex @code{tab-bar}, prefix key 2523@cindex @code{left-margin}, prefix key 2524@cindex @code{right-margin}, prefix key 2525@cindex @code{left-fringe}, prefix key 2526@cindex @code{right-fringe}, prefix key 2527@cindex @code{right-divider}, prefix key 2528@cindex @code{bottom-divider}, prefix key 2529@cindex mouse events, in special parts of window or frame 2530When mouse events occur in special parts of a window or frame, such as a mode 2531line or a scroll bar, the event type shows nothing special---it is the 2532same symbol that would normally represent that combination of mouse 2533button and modifier keys. The information about the window part is kept 2534elsewhere in the event---in the coordinates. But 2535@code{read-key-sequence} translates this information into imaginary 2536prefix keys, all of which are symbols: @code{tab-line}, @code{header-line}, 2537@code{horizontal-scroll-bar}, @code{menu-bar}, @code{tab-bar}, @code{mode-line}, 2538@code{vertical-line}, @code{vertical-scroll-bar}, @code{left-margin}, 2539@code{right-margin}, @code{left-fringe}, @code{right-fringe}, 2540@code{right-divider}, and @code{bottom-divider}. You can define meanings for 2541mouse clicks in special window parts by defining key sequences using these 2542imaginary prefix keys. 2543 2544For example, if you call @code{read-key-sequence} and then click the 2545mouse on the window's mode line, you get two events, like this: 2546 2547@example 2548(read-key-sequence "Click on the mode line: ") 2549 @result{} [mode-line 2550 (mouse-1 2551 (#<window 6 on NEWS> mode-line 2552 (40 . 63) 5959987))] 2553@end example 2554 2555@defvar num-input-keys 2556This variable's value is the number of key sequences processed so far in 2557this Emacs session. This includes key sequences read from the terminal 2558and key sequences read from keyboard macros being executed. 2559@end defvar 2560 2561@node Reading One Event 2562@subsection Reading One Event 2563@cindex reading a single event 2564@cindex event, reading only one 2565 2566 The lowest level functions for command input are @code{read-event}, 2567@code{read-char}, and @code{read-char-exclusive}. 2568 2569If you need a function to read a character using the minibuffer, use 2570@code{read-char-from-minibuffer} (@pxref{Multiple Queries}). 2571 2572@defun read-event &optional prompt inherit-input-method seconds 2573This function reads and returns the next event of command input, 2574waiting if necessary until an event is available. 2575 2576The returned event may come directly from the user, or from a keyboard 2577macro. It is not decoded by the keyboard's input coding system 2578(@pxref{Terminal I/O Encoding}). 2579 2580If the optional argument @var{prompt} is non-@code{nil}, it should be 2581a string to display in the echo area as a prompt. If @var{prompt} is 2582@code{nil} or the string @samp{""}, @code{read-event} does not display 2583any message to indicate it is waiting for input; instead, it prompts 2584by echoing: it displays descriptions of the events that led to or were 2585read by the current command. @xref{The Echo Area}. 2586 2587If @var{inherit-input-method} is non-@code{nil}, then the current input 2588method (if any) is employed to make it possible to enter a 2589non-@acronym{ASCII} character. Otherwise, input method handling is disabled 2590for reading this event. 2591 2592If @code{cursor-in-echo-area} is non-@code{nil}, then @code{read-event} 2593moves the cursor temporarily to the echo area, to the end of any message 2594displayed there. Otherwise @code{read-event} does not move the cursor. 2595 2596If @var{seconds} is non-@code{nil}, it should be a number specifying 2597the maximum time to wait for input, in seconds. If no input arrives 2598within that time, @code{read-event} stops waiting and returns 2599@code{nil}. A floating point @var{seconds} means to wait 2600for a fractional number of seconds. Some systems support only a whole 2601number of seconds; on these systems, @var{seconds} is rounded down. 2602If @var{seconds} is @code{nil}, @code{read-event} waits as long as 2603necessary for input to arrive. 2604 2605If @var{seconds} is @code{nil}, Emacs is considered idle while waiting 2606for user input to arrive. Idle timers---those created with 2607@code{run-with-idle-timer} (@pxref{Idle Timers})---can run during this 2608period. However, if @var{seconds} is non-@code{nil}, the state of 2609idleness remains unchanged. If Emacs is non-idle when 2610@code{read-event} is called, it remains non-idle throughout the 2611operation of @code{read-event}; if Emacs is idle (which can happen if 2612the call happens inside an idle timer), it remains idle. 2613 2614If @code{read-event} gets an event that is defined as a help character, 2615then in some cases @code{read-event} processes the event directly without 2616returning. @xref{Help Functions}. Certain other events, called 2617@dfn{special events}, are also processed directly within 2618@code{read-event} (@pxref{Special Events}). 2619 2620Here is what happens if you call @code{read-event} and then press the 2621right-arrow function key: 2622 2623@example 2624@group 2625(read-event) 2626 @result{} right 2627@end group 2628@end example 2629@end defun 2630 2631@defun read-char &optional prompt inherit-input-method seconds 2632This function reads and returns a character input event. If the 2633user generates an event which is not a character (i.e., a mouse click or 2634function key event), @code{read-char} signals an error. The arguments 2635work as in @code{read-event}. 2636 2637If the event has modifiers, Emacs attempts to resolve them and return 2638the code of the corresponding character. For example, if the user 2639types @kbd{C-a}, the function returns 1, which is the @acronym{ASCII} 2640code of the @samp{C-a} character. If some of the modifiers cannot be 2641reflected in the character code, @code{read-char} leaves the 2642unresolved modifier bits set in the returned event. For example, if 2643the user types @kbd{C-M-a}, the function returns 134217729, 8000001 in 2644hex, i.e.@: @samp{C-a} with the Meta modifier bit set. This value is 2645not a valid character code: it fails the @code{characterp} test 2646(@pxref{Character Codes}). Use @code{event-basic-type} 2647(@pxref{Classifying Events}) to recover the character code with the 2648modifier bits removed; use @code{event-modifiers} to test for 2649modifiers in the character event returned by @code{read-char}. 2650 2651In the first example below, the user types the character @kbd{1} 2652(@acronym{ASCII} code 49). The second example shows a keyboard macro 2653definition that calls @code{read-char} from the minibuffer using 2654@code{eval-expression}. @code{read-char} reads the keyboard macro's 2655very next character, which is @kbd{1}. Then @code{eval-expression} 2656displays its return value in the echo area. 2657 2658@example 2659@group 2660(read-char) 2661 @result{} 49 2662@end group 2663 2664@group 2665;; @r{We assume here you use @kbd{M-:} to evaluate this.} 2666(symbol-function 'foo) 2667 @result{} "^[:(read-char)^M1" 2668@end group 2669@group 2670(execute-kbd-macro 'foo) 2671 @print{} 49 2672 @result{} nil 2673@end group 2674@end example 2675@end defun 2676 2677@defun read-char-exclusive &optional prompt inherit-input-method seconds 2678This function reads and returns a character input event. If the 2679user generates an event which is not a character event, 2680@code{read-char-exclusive} ignores it and reads another event, until it 2681gets a character. The arguments work as in @code{read-event}. The 2682returned value may include modifier bits, as with @code{read-char}. 2683@end defun 2684 2685 None of the above functions suppress quitting. 2686 2687@defvar num-nonmacro-input-events 2688This variable holds the total number of input events received so far 2689from the terminal---not counting those generated by keyboard macros. 2690@end defvar 2691 2692 We emphasize that, unlike @code{read-key-sequence}, the functions 2693@code{read-event}, @code{read-char}, and @code{read-char-exclusive} do 2694not perform the translations described in @ref{Translation Keymaps}. 2695If you wish to read a single key taking these translations into 2696account, use the function @code{read-key}: 2697 2698@defun read-key &optional prompt 2699This function reads a single key. It is intermediate between 2700@code{read-key-sequence} and @code{read-event}. Unlike the former, it 2701reads a single key, not a key sequence. Unlike the latter, it does 2702not return a raw event, but decodes and translates the user input 2703according to @code{input-decode-map}, @code{local-function-key-map}, 2704and @code{key-translation-map} (@pxref{Translation Keymaps}). 2705 2706The argument @var{prompt} is either a string to be displayed in the 2707echo area as a prompt, or @code{nil}, meaning not to display a prompt. 2708@end defun 2709 2710@defun read-char-choice prompt chars &optional inhibit-quit 2711This function uses @code{read-key} to read and return a single 2712character. It ignores any input that is not a member of @var{chars}, 2713a list of accepted characters. Optionally, it will also ignore 2714keyboard-quit events while it is waiting for valid input. If you bind 2715@code{help-form} (@pxref{Help Functions}) to a non-@code{nil} value 2716while calling @code{read-char-choice}, then pressing @code{help-char} 2717causes it to evaluate @code{help-form} and display the result. It 2718then continues to wait for a valid input character, or keyboard-quit. 2719@end defun 2720 2721@defun read-multiple-choice prompt choices 2722Ask user a multiple choice question. @var{prompt} should be a string 2723that will be displayed as the prompt. 2724 2725@var{choices} is an alist where the first element in each entry is a 2726character to be entered, the second element is a short name for the 2727entry to be displayed while prompting (if there's room, it might be 2728shortened), and the third, optional entry is a longer explanation that 2729will be displayed in a help buffer if the user requests more help. 2730 2731The return value is the matching value from @var{choices}. 2732 2733@lisp 2734(read-multiple-choice 2735 "Continue connecting?" 2736 '((?a "always" "Accept certificate for this and future sessions.") 2737 (?s "session only" "Accept certificate this session only.") 2738 (?n "no" "Refuse to use certificate, close connection."))) 2739@end lisp 2740 2741The @code{read-multiple-choice-face} face is used to highlight the 2742matching characters in the name string on graphical terminals. 2743 2744@end defun 2745 2746@node Event Mod 2747@subsection Modifying and Translating Input Events 2748@cindex modifiers of events 2749@cindex translating input events 2750@cindex event translation 2751 2752 Emacs modifies every event it reads according to 2753@code{extra-keyboard-modifiers}, then translates it through 2754@code{keyboard-translate-table} (if applicable), before returning it 2755from @code{read-event}. 2756 2757@defvar extra-keyboard-modifiers 2758This variable lets Lisp programs ``press'' the modifier keys on the 2759keyboard. The value is a character. Only the modifiers of the 2760character matter. Each time the user types a keyboard key, it is 2761altered as if those modifier keys were held down. For instance, if 2762you bind @code{extra-keyboard-modifiers} to @code{?\C-\M-a}, then all 2763keyboard input characters typed during the scope of the binding will 2764have the control and meta modifiers applied to them. The character 2765@code{?\C-@@}, equivalent to the integer 0, does not count as a control 2766character for this purpose, but as a character with no modifiers. 2767Thus, setting @code{extra-keyboard-modifiers} to zero cancels any 2768modification. 2769 2770When using a window system, the program can press any of the 2771modifier keys in this way. Otherwise, only the @key{CTL} and @key{META} 2772keys can be virtually pressed. 2773 2774Note that this variable applies only to events that really come from 2775the keyboard, and has no effect on mouse events or any other events. 2776@end defvar 2777 2778@defvar keyboard-translate-table 2779This terminal-local variable is the translate table for keyboard 2780characters. It lets you reshuffle the keys on the keyboard without 2781changing any command bindings. Its value is normally a char-table, or 2782else @code{nil}. (It can also be a string or vector, but this is 2783considered obsolete.) 2784 2785If @code{keyboard-translate-table} is a char-table 2786(@pxref{Char-Tables}), then each character read from the keyboard is 2787looked up in this char-table. If the value found there is 2788non-@code{nil}, then it is used instead of the actual input character. 2789 2790Note that this translation is the first thing that happens to a 2791character after it is read from the terminal. Record-keeping features 2792such as @code{recent-keys} and dribble files record the characters after 2793translation. 2794 2795Note also that this translation is done before the characters are 2796supplied to input methods (@pxref{Input Methods}). Use 2797@code{translation-table-for-input} (@pxref{Translation of Characters}), 2798if you want to translate characters after input methods operate. 2799@end defvar 2800 2801@defun keyboard-translate from to 2802This function modifies @code{keyboard-translate-table} to translate 2803character code @var{from} into character code @var{to}. It creates 2804the keyboard translate table if necessary. 2805@end defun 2806 2807 Here's an example of using the @code{keyboard-translate-table} to 2808make @kbd{C-x}, @kbd{C-c} and @kbd{C-v} perform the cut, copy and paste 2809operations: 2810 2811@example 2812(keyboard-translate ?\C-x 'control-x) 2813(keyboard-translate ?\C-c 'control-c) 2814(keyboard-translate ?\C-v 'control-v) 2815(global-set-key [control-x] 'kill-region) 2816(global-set-key [control-c] 'kill-ring-save) 2817(global-set-key [control-v] 'yank) 2818@end example 2819 2820@noindent 2821On a graphical terminal that supports extended @acronym{ASCII} input, 2822you can still get the standard Emacs meanings of one of those 2823characters by typing it with the shift key. That makes it a different 2824character as far as keyboard translation is concerned, but it has the 2825same usual meaning. 2826 2827 @xref{Translation Keymaps}, for mechanisms that translate event sequences 2828at the level of @code{read-key-sequence}. 2829 2830@node Invoking the Input Method 2831@subsection Invoking the Input Method 2832@cindex invoking input method 2833 2834 The event-reading functions invoke the current input method, if any 2835(@pxref{Input Methods}). If the value of @code{input-method-function} 2836is non-@code{nil}, it should be a function; when @code{read-event} reads 2837a printing character (including @key{SPC}) with no modifier bits, it 2838calls that function, passing the character as an argument. 2839 2840@defvar input-method-function 2841If this is non-@code{nil}, its value specifies the current input method 2842function. 2843 2844@strong{Warning:} don't bind this variable with @code{let}. It is often 2845buffer-local, and if you bind it around reading input (which is exactly 2846when you @emph{would} bind it), switching buffers asynchronously while 2847Emacs is waiting will cause the value to be restored in the wrong 2848buffer. 2849@end defvar 2850 2851 The input method function should return a list of events which should 2852be used as input. (If the list is @code{nil}, that means there is no 2853input, so @code{read-event} waits for another event.) These events are 2854processed before the events in @code{unread-command-events} 2855(@pxref{Event Input Misc}). Events 2856returned by the input method function are not passed to the input method 2857function again, even if they are printing characters with no modifier 2858bits. 2859 2860 If the input method function calls @code{read-event} or 2861@code{read-key-sequence}, it should bind @code{input-method-function} to 2862@code{nil} first, to prevent recursion. 2863 2864 The input method function is not called when reading the second and 2865subsequent events of a key sequence. Thus, these characters are not 2866subject to input method processing. The input method function should 2867test the values of @code{overriding-local-map} and 2868@code{overriding-terminal-local-map}; if either of these variables is 2869non-@code{nil}, the input method should put its argument into a list and 2870return that list with no further processing. 2871 2872@node Quoted Character Input 2873@subsection Quoted Character Input 2874@cindex quoted character input 2875 2876 You can use the function @code{read-quoted-char} to ask the user to 2877specify a character, and allow the user to specify a control or meta 2878character conveniently, either literally or as an octal character code. 2879The command @code{quoted-insert} uses this function. 2880 2881@defun read-quoted-char &optional prompt 2882@cindex octal character input 2883@cindex control characters, reading 2884@cindex nonprinting characters, reading 2885This function is like @code{read-char}, except that if the first 2886character read is an octal digit (0--7), it reads any number of octal 2887digits (but stopping if a non-octal digit is found), and returns the 2888character represented by that numeric character code. If the 2889character that terminates the sequence of octal digits is @key{RET}, 2890it is discarded. Any other terminating character is used as input 2891after this function returns. 2892 2893Quitting is suppressed when the first character is read, so that the 2894user can enter a @kbd{C-g}. @xref{Quitting}. 2895 2896If @var{prompt} is supplied, it specifies a string for prompting the 2897user. The prompt string is always displayed in the echo area, followed 2898by a single @samp{-}. 2899 2900In the following example, the user types in the octal number 177 (which 2901is 127 in decimal). 2902 2903@example 2904(read-quoted-char "What character") 2905 2906@group 2907---------- Echo Area ---------- 2908What character @kbd{1 7 7}- 2909---------- Echo Area ---------- 2910 2911 @result{} 127 2912@end group 2913@end example 2914@end defun 2915 2916@need 2000 2917@node Event Input Misc 2918@subsection Miscellaneous Event Input Features 2919 2920This section describes how to peek ahead at events without using 2921them up, how to check for pending input, and how to discard pending 2922input. See also the function @code{read-passwd} (@pxref{Reading a 2923Password}). 2924 2925@defvar unread-command-events 2926@cindex next input 2927@cindex peeking at input 2928This variable holds a list of events waiting to be read as command 2929input. The events are used in the order they appear in the list, and 2930removed one by one as they are used. 2931 2932The variable is needed because in some cases a function reads an event 2933and then decides not to use it. Storing the event in this variable 2934causes it to be processed normally, by the command loop or by the 2935functions to read command input. 2936 2937@cindex prefix argument unreading 2938For example, the function that implements numeric prefix arguments reads 2939any number of digits. When it finds a non-digit event, it must unread 2940the event so that it can be read normally by the command loop. 2941Likewise, incremental search uses this feature to unread events with no 2942special meaning in a search, because these events should exit the search 2943and then execute normally. 2944 2945The reliable and easy way to extract events from a key sequence so as 2946to put them in @code{unread-command-events} is to use 2947@code{listify-key-sequence} (see below). 2948 2949Normally you add events to the front of this list, so that the events 2950most recently unread will be reread first. 2951 2952Events read from this list are not normally added to the current 2953command's key sequence (as returned by, e.g., @code{this-command-keys}), 2954as the events will already have been added once as they were read for 2955the first time. An element of the form @w{@code{(t . @var{event})}} 2956forces @var{event} to be added to the current command's key sequence. 2957 2958@cindex not recording input events 2959@cindex input events, prevent recording 2960Elements read from this list are normally recorded by the 2961record-keeping features (@pxref{Recording Input}) and while defining a 2962keyboard macro (@pxref{Keyboard Macros}). However, an element of the 2963form @w{@code{(no-record . @var{event})}} causes @var{event} to be 2964processed normally without recording it. 2965@end defvar 2966 2967@defun listify-key-sequence key 2968This function converts the string or vector @var{key} to a list of 2969individual events, which you can put in @code{unread-command-events}. 2970@end defun 2971 2972@defun input-pending-p &optional check-timers 2973@cindex waiting for command key input 2974This function determines whether any command input is currently 2975available to be read. It returns immediately, with value @code{t} if 2976there is available input, @code{nil} otherwise. On rare occasions it 2977may return @code{t} when no input is available. 2978 2979If the optional argument @var{check-timers} is non-@code{nil}, then if 2980no input is available, Emacs runs any timers which are ready. 2981@xref{Timers}. 2982@end defun 2983 2984@defvar last-input-event 2985This variable records the last terminal input event read, whether 2986as part of a command or explicitly by a Lisp program. 2987 2988In the example below, the Lisp program reads the character @kbd{1}, 2989@acronym{ASCII} code 49. It becomes the value of @code{last-input-event}, 2990while @kbd{C-e} (we assume @kbd{C-x C-e} command is used to evaluate 2991this expression) remains the value of @code{last-command-event}. 2992 2993@example 2994@group 2995(progn (print (read-char)) 2996 (print last-command-event) 2997 last-input-event) 2998 @print{} 49 2999 @print{} 5 3000 @result{} 49 3001@end group 3002@end example 3003@end defvar 3004 3005@defmac while-no-input body@dots{} 3006This construct runs the @var{body} forms and returns the value of the 3007last one---but only if no input arrives. If any input arrives during 3008the execution of the @var{body} forms, it aborts them (working much 3009like a quit). The @code{while-no-input} form returns @code{nil} if 3010aborted by a real quit, and returns @code{t} if aborted by arrival of 3011other input. 3012 3013If a part of @var{body} binds @code{inhibit-quit} to non-@code{nil}, 3014arrival of input during those parts won't cause an abort until 3015the end of that part. 3016 3017If you want to be able to distinguish all possible values computed 3018by @var{body} from both kinds of abort conditions, write the code 3019like this: 3020 3021@example 3022(while-no-input 3023 (list 3024 (progn . @var{body}))) 3025@end example 3026@end defmac 3027 3028@defvar while-no-input-ignore-events 3029This variable allow setting which special events @code{while-no-input} 3030should ignore. It is a list of event symbols (@pxref{Event Examples}). 3031 3032@end defvar 3033 3034@defun discard-input 3035@cindex flushing input 3036@cindex discarding input 3037@cindex keyboard macro, terminating 3038This function discards the contents of the terminal input buffer and 3039cancels any keyboard macro that might be in the process of definition. 3040It returns @code{nil}. 3041 3042In the following example, the user may type a number of characters right 3043after starting the evaluation of the form. After the @code{sleep-for} 3044finishes sleeping, @code{discard-input} discards any characters typed 3045during the sleep. 3046 3047@example 3048(progn (sleep-for 2) 3049 (discard-input)) 3050 @result{} nil 3051@end example 3052@end defun 3053 3054@node Special Events 3055@section Special Events 3056 3057@cindex special events 3058Certain @dfn{special events} are handled at a very low level---as soon 3059as they are read. The @code{read-event} function processes these 3060events itself, and never returns them. Instead, it keeps waiting for 3061the first event that is not special and returns that one. 3062 3063 Special events do not echo, they are never grouped into key 3064sequences, and they never appear in the value of 3065@code{last-command-event} or @code{(this-command-keys)}. They do not 3066discard a numeric argument, they cannot be unread with 3067@code{unread-command-events}, they may not appear in a keyboard macro, 3068and they are not recorded in a keyboard macro while you are defining 3069one. 3070 3071 Special events do, however, appear in @code{last-input-event} 3072immediately after they are read, and this is the way for the event's 3073definition to find the actual event. 3074 3075 The events types @code{iconify-frame}, @code{make-frame-visible}, 3076@code{delete-frame}, @code{drag-n-drop}, @code{language-change}, and 3077user signals like @code{sigusr1} are normally handled in this way. 3078The keymap which defines how to handle special events---and which 3079events are special---is in the variable @code{special-event-map} 3080(@pxref{Controlling Active Maps}). 3081 3082@node Waiting 3083@section Waiting for Elapsed Time or Input 3084@cindex waiting 3085 3086 The wait functions are designed to wait for a certain amount of time 3087to pass or until there is input. For example, you may wish to pause in 3088the middle of a computation to allow the user time to view the display. 3089@code{sit-for} pauses and updates the screen, and returns immediately if 3090input comes in, while @code{sleep-for} pauses without updating the 3091screen. 3092 3093@defun sit-for seconds &optional nodisp 3094This function performs redisplay (provided there is no pending input 3095from the user), then waits @var{seconds} seconds, or until input is 3096available. The usual purpose of @code{sit-for} is to give the user 3097time to read text that you display. The value is @code{t} if 3098@code{sit-for} waited the full time with no input arriving 3099(@pxref{Event Input Misc}). Otherwise, the value is @code{nil}. 3100 3101The argument @var{seconds} need not be an integer. If it is floating 3102point, @code{sit-for} waits for a fractional number of seconds. 3103Some systems support only a whole number of seconds; on these systems, 3104@var{seconds} is rounded down. 3105 3106The expression @code{(sit-for 0)} is equivalent to @code{(redisplay)}, 3107i.e., it requests a redisplay, without any delay, if there is no pending input. 3108@xref{Forcing Redisplay}. 3109 3110If @var{nodisp} is non-@code{nil}, then @code{sit-for} does not 3111redisplay, but it still returns as soon as input is available (or when 3112the timeout elapses). 3113 3114In batch mode (@pxref{Batch Mode}), @code{sit-for} cannot be 3115interrupted, even by input from the standard input descriptor. It is 3116thus equivalent to @code{sleep-for}, which is described below. 3117 3118It is also possible to call @code{sit-for} with three arguments, 3119as @code{(sit-for @var{seconds} @var{millisec} @var{nodisp})}, 3120but that is considered obsolete. 3121@end defun 3122 3123@defun sleep-for seconds &optional millisec 3124This function simply pauses for @var{seconds} seconds without updating 3125the display. It pays no attention to available input. It returns 3126@code{nil}. 3127 3128The argument @var{seconds} need not be an integer. If it is floating 3129point, @code{sleep-for} waits for a fractional number of seconds. 3130Some systems support only a whole number of seconds; on these systems, 3131@var{seconds} is rounded down. 3132 3133The optional argument @var{millisec} specifies an additional waiting 3134period measured in milliseconds. This adds to the period specified by 3135@var{seconds}. If the system doesn't support waiting fractions of a 3136second, you get an error if you specify nonzero @var{millisec}. 3137 3138Use @code{sleep-for} when you wish to guarantee a delay. 3139@end defun 3140 3141 @xref{Time of Day}, for functions to get the current time. 3142 3143@node Quitting 3144@section Quitting 3145@cindex @kbd{C-g} 3146@cindex quitting 3147@cindex interrupt Lisp functions 3148 3149 Typing @kbd{C-g} while a Lisp function is running causes Emacs to 3150@dfn{quit} whatever it is doing. This means that control returns to the 3151innermost active command loop. 3152 3153 Typing @kbd{C-g} while the command loop is waiting for keyboard input 3154does not cause a quit; it acts as an ordinary input character. In the 3155simplest case, you cannot tell the difference, because @kbd{C-g} 3156normally runs the command @code{keyboard-quit}, whose effect is to quit. 3157However, when @kbd{C-g} follows a prefix key, they combine to form an 3158undefined key. The effect is to cancel the prefix key as well as any 3159prefix argument. 3160 3161 In the minibuffer, @kbd{C-g} has a different definition: it aborts out 3162of the minibuffer. This means, in effect, that it exits the minibuffer 3163and then quits. (Simply quitting would return to the command loop 3164@emph{within} the minibuffer.) The reason why @kbd{C-g} does not quit 3165directly when the command reader is reading input is so that its meaning 3166can be redefined in the minibuffer in this way. @kbd{C-g} following a 3167prefix key is not redefined in the minibuffer, and it has its normal 3168effect of canceling the prefix key and prefix argument. This too 3169would not be possible if @kbd{C-g} always quit directly. 3170 3171 When @kbd{C-g} does directly quit, it does so by setting the variable 3172@code{quit-flag} to @code{t}. Emacs checks this variable at appropriate 3173times and quits if it is not @code{nil}. Setting @code{quit-flag} 3174non-@code{nil} in any way thus causes a quit. 3175 3176 At the level of C code, quitting cannot happen just anywhere; only at the 3177special places that check @code{quit-flag}. The reason for this is 3178that quitting at other places might leave an inconsistency in Emacs's 3179internal state. Because quitting is delayed until a safe place, quitting 3180cannot make Emacs crash. 3181 3182 Certain functions such as @code{read-key-sequence} or 3183@code{read-quoted-char} prevent quitting entirely even though they wait 3184for input. Instead of quitting, @kbd{C-g} serves as the requested 3185input. In the case of @code{read-key-sequence}, this serves to bring 3186about the special behavior of @kbd{C-g} in the command loop. In the 3187case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used 3188to quote a @kbd{C-g}. 3189 3190@cindex preventing quitting 3191 You can prevent quitting for a portion of a Lisp function by binding 3192the variable @code{inhibit-quit} to a non-@code{nil} value. Then, 3193although @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, the 3194usual result of this---a quit---is prevented. Eventually, 3195@code{inhibit-quit} will become @code{nil} again, such as when its 3196binding is unwound at the end of a @code{let} form. At that time, if 3197@code{quit-flag} is still non-@code{nil}, the requested quit happens 3198immediately. This behavior is ideal when you wish to make sure that 3199quitting does not happen within a critical section of the program. 3200 3201@cindex @code{read-quoted-char} quitting 3202 In some functions (such as @code{read-quoted-char}), @kbd{C-g} is 3203handled in a special way that does not involve quitting. This is done 3204by reading the input with @code{inhibit-quit} bound to @code{t}, and 3205setting @code{quit-flag} to @code{nil} before @code{inhibit-quit} 3206becomes @code{nil} again. This excerpt from the definition of 3207@code{read-quoted-char} shows how this is done; it also shows that 3208normal quitting is permitted after the first character of input. 3209 3210@example 3211(defun read-quoted-char (&optional prompt) 3212 "@dots{}@var{documentation}@dots{}" 3213 (let ((message-log-max nil) done (first t) (code 0) char) 3214 (while (not done) 3215 (let ((inhibit-quit first) 3216 @dots{}) 3217 (and prompt (message "%s-" prompt)) 3218 (setq char (read-event)) 3219 (if inhibit-quit (setq quit-flag nil))) 3220 @r{@dots{}set the variable @code{code}@dots{}}) 3221 code)) 3222@end example 3223 3224@defvar quit-flag 3225If this variable is non-@code{nil}, then Emacs quits immediately, unless 3226@code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} ordinarily sets 3227@code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}. 3228@end defvar 3229 3230@defvar inhibit-quit 3231This variable determines whether Emacs should quit when @code{quit-flag} 3232is set to a value other than @code{nil}. If @code{inhibit-quit} is 3233non-@code{nil}, then @code{quit-flag} has no special effect. 3234@end defvar 3235 3236@defmac with-local-quit body@dots{} 3237This macro executes @var{body} forms in sequence, but allows quitting, at 3238least locally, within @var{body} even if @code{inhibit-quit} was 3239non-@code{nil} outside this construct. It returns the value of the 3240last form in @var{body}, unless exited by quitting, in which case 3241it returns @code{nil}. 3242 3243If @code{inhibit-quit} is @code{nil} on entry to @code{with-local-quit}, 3244it only executes the @var{body}, and setting @code{quit-flag} causes 3245a normal quit. However, if @code{inhibit-quit} is non-@code{nil} so 3246that ordinary quitting is delayed, a non-@code{nil} @code{quit-flag} 3247triggers a special kind of local quit. This ends the execution of 3248@var{body} and exits the @code{with-local-quit} body with 3249@code{quit-flag} still non-@code{nil}, so that another (ordinary) quit 3250will happen as soon as that is allowed. If @code{quit-flag} is 3251already non-@code{nil} at the beginning of @var{body}, the local quit 3252happens immediately and the body doesn't execute at all. 3253 3254This macro is mainly useful in functions that can be called from 3255timers, process filters, process sentinels, @code{pre-command-hook}, 3256@code{post-command-hook}, and other places where @code{inhibit-quit} is 3257normally bound to @code{t}. 3258@end defmac 3259 3260@deffn Command keyboard-quit 3261This function signals the @code{quit} condition with @code{(signal 'quit 3262nil)}. This is the same thing that quitting does. (See @code{signal} 3263in @ref{Errors}.) 3264@end deffn 3265 3266 You can specify a character other than @kbd{C-g} to use for quitting. 3267See the function @code{set-input-mode} in @ref{Input Modes}. 3268 3269@node Prefix Command Arguments 3270@section Prefix Command Arguments 3271@cindex prefix argument 3272@cindex raw prefix argument 3273@cindex numeric prefix argument 3274 3275 Most Emacs commands can use a @dfn{prefix argument}, a number 3276specified before the command itself. (Don't confuse prefix arguments 3277with prefix keys.) The prefix argument is at all times represented by a 3278value, which may be @code{nil}, meaning there is currently no prefix 3279argument. Each command may use the prefix argument or ignore it. 3280 3281 There are two representations of the prefix argument: @dfn{raw} and 3282@dfn{numeric}. The editor command loop uses the raw representation 3283internally, and so do the Lisp variables that store the information, but 3284commands can request either representation. 3285 3286 Here are the possible values of a raw prefix argument: 3287 3288@itemize @bullet 3289@item 3290@code{nil}, meaning there is no prefix argument. Its numeric value is 32911, but numerous commands make a distinction between @code{nil} and the 3292integer 1. 3293 3294@item 3295An integer, which stands for itself. 3296 3297@item 3298A list of one element, which is an integer. This form of prefix 3299argument results from one or a succession of @kbd{C-u}s with no 3300digits. The numeric value is the integer in the list, but some 3301commands make a distinction between such a list and an integer alone. 3302 3303@item 3304The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was 3305typed, without following digits. The equivalent numeric value is 3306@minus{}1, but some commands make a distinction between the integer 3307@minus{}1 and the symbol @code{-}. 3308@end itemize 3309 3310We illustrate these possibilities by calling the following function with 3311various prefixes: 3312 3313@example 3314@group 3315(defun display-prefix (arg) 3316 "Display the value of the raw prefix arg." 3317 (interactive "P") 3318 (message "%s" arg)) 3319@end group 3320@end example 3321 3322@noindent 3323Here are the results of calling @code{display-prefix} with various 3324raw prefix arguments: 3325 3326@example 3327 M-x display-prefix @print{} nil 3328 3329C-u M-x display-prefix @print{} (4) 3330 3331C-u C-u M-x display-prefix @print{} (16) 3332 3333C-u 3 M-x display-prefix @print{} 3 3334 3335M-3 M-x display-prefix @print{} 3 ; @r{(Same as @code{C-u 3}.)} 3336 3337C-u - M-x display-prefix @print{} - 3338 3339M-- M-x display-prefix @print{} - ; @r{(Same as @code{C-u -}.)} 3340 3341C-u - 7 M-x display-prefix @print{} -7 3342 3343M-- 7 M-x display-prefix @print{} -7 ; @r{(Same as @code{C-u -7}.)} 3344@end example 3345 3346 Emacs uses two variables to store the prefix argument: 3347@code{prefix-arg} and @code{current-prefix-arg}. Commands such as 3348@code{universal-argument} that set up prefix arguments for other 3349commands store them in @code{prefix-arg}. In contrast, 3350@code{current-prefix-arg} conveys the prefix argument to the current 3351command, so setting it has no effect on the prefix arguments for future 3352commands. 3353 3354 Normally, commands specify which representation to use for the prefix 3355argument, either numeric or raw, in the @code{interactive} specification. 3356(@xref{Using Interactive}.) Alternatively, functions may look at the 3357value of the prefix argument directly in the variable 3358@code{current-prefix-arg}, but this is less clean. 3359 3360@defun prefix-numeric-value arg 3361This function returns the numeric meaning of a valid raw prefix argument 3362value, @var{arg}. The argument may be a symbol, a number, or a list. 3363If it is @code{nil}, the value 1 is returned; if it is @code{-}, the 3364value @minus{}1 is returned; if it is a number, that number is returned; 3365if it is a list, the @sc{car} of that list (which should be a number) is 3366returned. 3367@end defun 3368 3369@defvar current-prefix-arg 3370This variable holds the raw prefix argument for the @emph{current} 3371command. Commands may examine it directly, but the usual method for 3372accessing it is with @code{(interactive "P")}. 3373@end defvar 3374 3375@defvar prefix-arg 3376The value of this variable is the raw prefix argument for the 3377@emph{next} editing command. Commands such as @code{universal-argument} 3378that specify prefix arguments for the following command work by setting 3379this variable. 3380@end defvar 3381 3382@defvar last-prefix-arg 3383The raw prefix argument value used by the previous command. 3384@end defvar 3385 3386 The following commands exist to set up prefix arguments for the 3387following command. Do not call them for any other reason. 3388 3389@deffn Command universal-argument 3390This command reads input and specifies a prefix argument for the 3391following command. Don't call this command yourself unless you know 3392what you are doing. 3393@end deffn 3394 3395@deffn Command digit-argument arg 3396This command adds to the prefix argument for the following command. The 3397argument @var{arg} is the raw prefix argument as it was before this 3398command; it is used to compute the updated prefix argument. Don't call 3399this command yourself unless you know what you are doing. 3400@end deffn 3401 3402@deffn Command negative-argument arg 3403This command adds to the numeric argument for the next command. The 3404argument @var{arg} is the raw prefix argument as it was before this 3405command; its value is negated to form the new prefix argument. Don't 3406call this command yourself unless you know what you are doing. 3407@end deffn 3408 3409@node Recursive Editing 3410@section Recursive Editing 3411@cindex recursive command loop 3412@cindex recursive editing level 3413@cindex command loop, recursive 3414 3415 The Emacs command loop is entered automatically when Emacs starts up. 3416This top-level invocation of the command loop never exits; it keeps 3417running as long as Emacs does. Lisp programs can also invoke the 3418command loop. Since this makes more than one activation of the command 3419loop, we call it @dfn{recursive editing}. A recursive editing level has 3420the effect of suspending whatever command invoked it and permitting the 3421user to do arbitrary editing before resuming that command. 3422 3423 The commands available during recursive editing are the same ones 3424available in the top-level editing loop and defined in the keymaps. 3425Only a few special commands exit the recursive editing level; the others 3426return to the recursive editing level when they finish. (The special 3427commands for exiting are always available, but they do nothing when 3428recursive editing is not in progress.) 3429 3430 All command loops, including recursive ones, set up all-purpose error 3431handlers so that an error in a command run from the command loop will 3432not exit the loop. 3433 3434@cindex minibuffer input 3435 Minibuffer input is a special kind of recursive editing. It has a few 3436special wrinkles, such as enabling display of the minibuffer and the 3437minibuffer window, but fewer than you might suppose. Certain keys 3438behave differently in the minibuffer, but that is only because of the 3439minibuffer's local map; if you switch windows, you get the usual Emacs 3440commands. 3441 3442@cindex @code{throw} example 3443@kindex exit 3444@cindex exit recursive editing 3445@cindex aborting 3446 To invoke a recursive editing level, call the function 3447@code{recursive-edit}. This function contains the command loop; it also 3448contains a call to @code{catch} with tag @code{exit}, which makes it 3449possible to exit the recursive editing level by throwing to @code{exit} 3450(@pxref{Catch and Throw}). If you throw a value other than @code{t}, 3451then @code{recursive-edit} returns normally to the function that called 3452it. The command @kbd{C-M-c} (@code{exit-recursive-edit}) does this. 3453Throwing a @code{t} value causes @code{recursive-edit} to quit, so that 3454control returns to the command loop one level up. This is called 3455@dfn{aborting}, and is done by @kbd{C-]} (@code{abort-recursive-edit}). 3456 3457 Most applications should not use recursive editing, except as part of 3458using the minibuffer. Usually it is more convenient for the user if you 3459change the major mode of the current buffer temporarily to a special 3460major mode, which should have a command to go back to the previous mode. 3461(The @kbd{e} command in Rmail uses this technique.) Or, if you wish to 3462give the user different text to edit recursively, create and select 3463a new buffer in a special mode. In this mode, define a command to 3464complete the processing and go back to the previous buffer. (The 3465@kbd{m} command in Rmail does this.) 3466 3467 Recursive edits are useful in debugging. You can insert a call to 3468@code{debug} into a function definition as a sort of breakpoint, so that 3469you can look around when the function gets there. @code{debug} invokes 3470a recursive edit but also provides the other features of the debugger. 3471 3472 Recursive editing levels are also used when you type @kbd{C-r} in 3473@code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}). 3474 3475@deffn Command recursive-edit 3476@cindex suspend evaluation 3477This function invokes the editor command loop. It is called 3478automatically by the initialization of Emacs, to let the user begin 3479editing. When called from a Lisp program, it enters a recursive editing 3480level. 3481 3482If the current buffer is not the same as the selected window's buffer, 3483@code{recursive-edit} saves and restores the current buffer. Otherwise, 3484if you switch buffers, the buffer you switched to is current after 3485@code{recursive-edit} returns. 3486 3487In the following example, the function @code{simple-rec} first 3488advances point one word, then enters a recursive edit, printing out a 3489message in the echo area. The user can then do any editing desired, and 3490then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}. 3491 3492@example 3493(defun simple-rec () 3494 (forward-word 1) 3495 (message "Recursive edit in progress") 3496 (recursive-edit) 3497 (forward-word 1)) 3498 @result{} simple-rec 3499(simple-rec) 3500 @result{} nil 3501@end example 3502@end deffn 3503 3504@deffn Command exit-recursive-edit 3505This function exits from the innermost recursive edit (including 3506minibuffer input). Its definition is effectively @code{(throw 'exit 3507nil)}. 3508@end deffn 3509 3510@deffn Command abort-recursive-edit 3511This function aborts the command that requested the innermost recursive 3512edit (including minibuffer input), by signaling @code{quit} 3513after exiting the recursive edit. Its definition is effectively 3514@code{(throw 'exit t)}. @xref{Quitting}. 3515@end deffn 3516 3517@deffn Command top-level 3518This function exits all recursive editing levels; it does not return a 3519value, as it jumps completely out of any computation directly back to 3520the main command loop. 3521@end deffn 3522 3523@defun recursion-depth 3524This function returns the current depth of recursive edits. When no 3525recursive edit is active, it returns 0. 3526@end defun 3527 3528@node Disabling Commands 3529@section Disabling Commands 3530@cindex disabled command 3531 3532 @dfn{Disabling a command} marks the command as requiring user 3533confirmation before it can be executed. Disabling is used for commands 3534which might be confusing to beginning users, to prevent them from using 3535the commands by accident. 3536 3537@kindex disabled 3538 The low-level mechanism for disabling a command is to put a 3539non-@code{nil} @code{disabled} property on the Lisp symbol for the 3540command. These properties are normally set up by the user's 3541init file (@pxref{Init File}) with Lisp expressions such as this: 3542 3543@example 3544(put 'upcase-region 'disabled t) 3545@end example 3546 3547@noindent 3548For a few commands, these properties are present by default (you can 3549remove them in your init file if you wish). 3550 3551 If the value of the @code{disabled} property is a string, the message 3552saying the command is disabled includes that string. For example: 3553 3554@example 3555(put 'delete-region 'disabled 3556 "Text deleted this way cannot be yanked back!\n") 3557@end example 3558 3559 @xref{Disabling,,, emacs, The GNU Emacs Manual}, for the details on 3560what happens when a disabled command is invoked interactively. 3561Disabling a command has no effect on calling it as a function from Lisp 3562programs. 3563 3564@deffn Command enable-command command 3565Allow @var{command} (a symbol) to be executed without special 3566confirmation from now on, and alter the user's init file (@pxref{Init 3567File}) so that this will apply to future sessions. 3568@end deffn 3569 3570@deffn Command disable-command command 3571Require special confirmation to execute @var{command} from now on, and 3572alter the user's init file so that this will apply to future sessions. 3573@end deffn 3574 3575@defvar disabled-command-function 3576The value of this variable should be a function. When the user 3577invokes a disabled command interactively, this function is called 3578instead of the disabled command. It can use @code{this-command-keys} 3579to determine what the user typed to run the command, and thus find the 3580command itself. 3581 3582The value may also be @code{nil}. Then all commands work normally, 3583even disabled ones. 3584 3585By default, the value is a function that asks the user whether to 3586proceed. 3587@end defvar 3588 3589@node Command History 3590@section Command History 3591@cindex command history 3592@cindex complex command 3593@cindex history of commands 3594 3595 The command loop keeps a history of the complex commands that have 3596been executed, to make it convenient to repeat these commands. A 3597@dfn{complex command} is one for which the interactive argument reading 3598uses the minibuffer. This includes any @kbd{M-x} command, any 3599@kbd{M-:} command, and any command whose @code{interactive} 3600specification reads an argument from the minibuffer. Explicit use of 3601the minibuffer during the execution of the command itself does not cause 3602the command to be considered complex. 3603 3604@defvar command-history 3605This variable's value is a list of recent complex commands, each 3606represented as a form to evaluate. It continues to accumulate all 3607complex commands for the duration of the editing session, but when it 3608reaches the maximum size (@pxref{Minibuffer History}), the oldest 3609elements are deleted as new ones are added. 3610 3611@example 3612@group 3613command-history 3614@result{} ((switch-to-buffer "chistory.texi") 3615 (describe-key "^X^[") 3616 (visit-tags-table "~/emacs/src/") 3617 (find-tag "repeat-complex-command")) 3618@end group 3619@end example 3620@end defvar 3621 3622 This history list is actually a special case of minibuffer history 3623(@pxref{Minibuffer History}), with one special twist: the elements are 3624expressions rather than strings. 3625 3626 There are a number of commands devoted to the editing and recall of 3627previous commands. The commands @code{repeat-complex-command}, and 3628@code{list-command-history} are described in the user manual 3629(@pxref{Repetition,,, emacs, The GNU Emacs Manual}). Within the 3630minibuffer, the usual minibuffer history commands are available. 3631 3632@node Keyboard Macros 3633@section Keyboard Macros 3634@cindex keyboard macros 3635 3636 A @dfn{keyboard macro} is a canned sequence of input events that can 3637be considered a command and made the definition of a key. The Lisp 3638representation of a keyboard macro is a string or vector containing the 3639events. Don't confuse keyboard macros with Lisp macros 3640(@pxref{Macros}). 3641 3642@defun execute-kbd-macro kbdmacro &optional count loopfunc 3643This function executes @var{kbdmacro} as a sequence of events. If 3644@var{kbdmacro} is a string or vector, then the events in it are executed 3645exactly as if they had been input by the user. The sequence is 3646@emph{not} expected to be a single key sequence; normally a keyboard 3647macro definition consists of several key sequences concatenated. 3648 3649If @var{kbdmacro} is a symbol, then its function definition is used in 3650place of @var{kbdmacro}. If that is another symbol, this process repeats. 3651Eventually the result should be a string or vector. If the result is 3652not a symbol, string, or vector, an error is signaled. 3653 3654The argument @var{count} is a repeat count; @var{kbdmacro} is executed that 3655many times. If @var{count} is omitted or @code{nil}, @var{kbdmacro} is 3656executed once. If it is 0, @var{kbdmacro} is executed over and over until it 3657encounters an error or a failing search. 3658 3659If @var{loopfunc} is non-@code{nil}, it is a function that is called, 3660without arguments, prior to each iteration of the macro. If 3661@var{loopfunc} returns @code{nil}, then this stops execution of the macro. 3662 3663@xref{Reading One Event}, for an example of using @code{execute-kbd-macro}. 3664@end defun 3665 3666@defvar executing-kbd-macro 3667This variable contains the string or vector that defines the keyboard 3668macro that is currently executing. It is @code{nil} if no macro is 3669currently executing. A command can test this variable so as to behave 3670differently when run from an executing macro. Do not set this variable 3671yourself. 3672@end defvar 3673 3674@defvar defining-kbd-macro 3675This variable is non-@code{nil} if and only if a keyboard macro is 3676being defined. A command can test this variable so as to behave 3677differently while a macro is being defined. The value is 3678@code{append} while appending to the definition of an existing macro. 3679The commands @code{start-kbd-macro}, @code{kmacro-start-macro} and 3680@code{end-kbd-macro} set this variable---do not set it yourself. 3681 3682The variable is always local to the current terminal and cannot be 3683buffer-local. @xref{Multiple Terminals}. 3684@end defvar 3685 3686@defvar last-kbd-macro 3687This variable is the definition of the most recently defined keyboard 3688macro. Its value is a string or vector, or @code{nil}. 3689 3690The variable is always local to the current terminal and cannot be 3691buffer-local. @xref{Multiple Terminals}. 3692@end defvar 3693 3694@defvar kbd-macro-termination-hook 3695This normal hook is run when a keyboard macro terminates, regardless 3696of what caused it to terminate (reaching the macro end or an error 3697which ended the macro prematurely). 3698@end defvar 3699