(create_process): Restore the signal state
[bpt/emacs.git] / lispref / commands.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/commands
6 @node Command Loop, Keymaps, Minibuffers, Top
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
12 immediately. This loop reads key sequences, executes their definitions,
13 and displays the results. In this chapter, we describe how these things
14 are 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 * Command Loop Info:: Variables set by the command loop for you to examine.
21 * Input Events:: What input looks like when you read it.
22 * Reading Input:: How to read input events from the keyboard or mouse.
23 * Waiting:: Waiting for user input or elapsed time.
24 * Quitting:: How @kbd{C-g} works. How to catch or defer quitting.
25 * Prefix Command Arguments:: How the commands to set prefix args work.
26 * Recursive Editing:: Entering a recursive edit,
27 and why you usually shouldn't.
28 * Disabling Commands:: How the command loop handles disabled commands.
29 * Command History:: How the command history is set up, and how accessed.
30 * Keyboard Macros:: How keyboard macros are implemented.
31 @end menu
32
33 @node Command Overview
34 @section Command Loop Overview
35
36 The first thing the command loop must do is read a key sequence, which
37 is a sequence of events that translates into a command. It does this by
38 calling the function @code{read-key-sequence}. Your Lisp code can also
39 call this function (@pxref{Key Sequence Input}). Lisp programs can also
40 do input at a lower level with @code{read-event} (@pxref{Reading One
41 Event}) or discard pending input with @code{discard-input}
42 (@pxref{Event Input Misc}).
43
44 The key sequence is translated into a command through the currently
45 active keymaps. @xref{Key Lookup}, for information on how this is done.
46 The result should be a keyboard macro or an interactively callable
47 function. If the key is @kbd{M-x}, then it reads the name of another
48 command, which it then calls. This is done by the command
49 @code{execute-extended-command} (@pxref{Interactive Call}).
50
51 To execute a command requires first reading the arguments for it.
52 This is done by calling @code{command-execute} (@pxref{Interactive
53 Call}). For commands written in Lisp, the @code{interactive}
54 specification says how to read the arguments. This may use the prefix
55 argument (@pxref{Prefix Command Arguments}) or may read with prompting
56 in the minibuffer (@pxref{Minibuffers}). For example, the command
57 @code{find-file} has an @code{interactive} specification which says to
58 read a file name using the minibuffer. The command's function body does
59 not use the minibuffer; if you call this command from Lisp code as a
60 function, you must supply the file name string as an ordinary Lisp
61 function argument.
62
63 If the command is a string or vector (i.e., a keyboard macro) then
64 @code{execute-kbd-macro} is used to execute it. You can call this
65 function yourself (@pxref{Keyboard Macros}).
66
67 To terminate the execution of a running command, type @kbd{C-g}. This
68 character causes @dfn{quitting} (@pxref{Quitting}).
69
70 @defvar pre-command-hook
71 The editor command loop runs this normal hook before each command. At
72 that time, @code{this-command} contains the command that is about to
73 run, and @code{last-command} describes the previous command.
74 @xref{Hooks}.
75 @end defvar
76
77 @defvar post-command-hook
78 The editor command loop runs this normal hook after each command
79 (including commands terminated prematurely by quitting or by errors),
80 and also when the command loop is first entered. At that time,
81 @code{this-command} describes the command that just ran, and
82 @code{last-command} describes the command before that. @xref{Hooks}.
83 @end defvar
84
85 Quitting is suppressed while running @code{pre-command-hook} and
86 @code{post-command-hook}. If an error happens while executing one of
87 these hooks, it terminates execution of the hook, but that is all it
88 does.
89
90 @node Defining Commands
91 @section Defining Commands
92 @cindex defining commands
93 @cindex commands, defining
94 @cindex functions, making them interactive
95 @cindex interactive function
96
97 A Lisp function becomes a command when its body contains, at top
98 level, a form that calls the special form @code{interactive}. This
99 form does nothing when actually executed, but its presence serves as a
100 flag to indicate that interactive calling is permitted. Its argument
101 controls the reading of arguments for an interactive call.
102
103 @menu
104 * Using Interactive:: General rules for @code{interactive}.
105 * Interactive Codes:: The standard letter-codes for reading arguments
106 in various ways.
107 * Interactive Examples:: Examples of how to read interactive arguments.
108 @end menu
109
110 @node Using Interactive
111 @subsection Using @code{interactive}
112
113 This section describes how to write the @code{interactive} form that
114 makes a Lisp function an interactively-callable command.
115
116 @defspec interactive arg-descriptor
117 @cindex argument descriptors
118 This special form declares that the function in which it appears is a
119 command, and that it may therefore be called interactively (via
120 @kbd{M-x} or by entering a key sequence bound to it). The argument
121 @var{arg-descriptor} declares how to compute the arguments to the
122 command when the command is called interactively.
123
124 A command may be called from Lisp programs like any other function, but
125 then the caller supplies the arguments and @var{arg-descriptor} has no
126 effect.
127
128 The @code{interactive} form has its effect because the command loop
129 (actually, its subroutine @code{call-interactively}) scans through the
130 function definition looking for it, before calling the function. Once
131 the function is called, all its body forms including the
132 @code{interactive} form are executed, but at this time
133 @code{interactive} simply returns @code{nil} without even evaluating its
134 argument.
135 @end defspec
136
137 There are three possibilities for the argument @var{arg-descriptor}:
138
139 @itemize @bullet
140 @item
141 It may be omitted or @code{nil}; then the command is called with no
142 arguments. This leads quickly to an error if the command requires one
143 or more arguments.
144
145 @item
146 It may be a Lisp expression that is not a string; then it should be a
147 form that is evaluated to get a list of arguments to pass to the
148 command.
149 @cindex argument evaluation form
150
151 If this expression reads keyboard input (this includes using the
152 minibuffer), keep in mind that the integer value of point or the mark
153 before reading input may be incorrect after reading input. This is
154 because the current buffer may be receiving subprocess output;
155 if subprocess output arrives while the command is waiting for input,
156 it could relocate point and the mark.
157
158 Here's an example of what @emph{not} to do:
159
160 @smallexample
161 (interactive
162 (list (region-beginning) (region-end)
163 (read-string "Foo: " nil 'my-history)))
164 @end smallexample
165
166 @noindent
167 Here's how to avoid the problem, by examining point and the mark only
168 after reading the keyboard input:
169
170 @smallexample
171 (interactive
172 (let ((string (read-string "Foo: " nil 'my-history)))
173 (list (region-beginning) (region-end) string)))
174 @end smallexample
175
176 @item
177 @cindex argument prompt
178 It may be a string; then its contents should consist of a code character
179 followed by a prompt (which some code characters use and some ignore).
180 The prompt ends either with the end of the string or with a newline.
181 Here is a simple example:
182
183 @smallexample
184 (interactive "bFrobnicate buffer: ")
185 @end smallexample
186
187 @noindent
188 The code letter @samp{b} says to read the name of an existing buffer,
189 with completion. The buffer name is the sole argument passed to the
190 command. The rest of the string is a prompt.
191
192 If there is a newline character in the string, it terminates the prompt.
193 If the string does not end there, then the rest of the string should
194 contain another code character and prompt, specifying another argument.
195 You can specify any number of arguments in this way.
196
197 @c Emacs 19 feature
198 The prompt string can use @samp{%} to include previous argument values
199 (starting with the first argument) in the prompt. This is done using
200 @code{format} (@pxref{Formatting Strings}). For example, here is how
201 you could read the name of an existing buffer followed by a new name to
202 give to that buffer:
203
204 @smallexample
205 @group
206 (interactive "bBuffer to rename: \nsRename buffer %s to: ")
207 @end group
208 @end smallexample
209
210 @cindex @samp{*} in interactive
211 @cindex read-only buffers in interactive
212 If the first character in the string is @samp{*}, then an error is
213 signaled if the buffer is read-only.
214
215 @cindex @samp{@@} in interactive
216 @c Emacs 19 feature
217 If the first character in the string is @samp{@@}, and if the key
218 sequence used to invoke the command includes any mouse events, then
219 the window associated with the first of those events is selected
220 before the command is run.
221
222 You can use @samp{*} and @samp{@@} together; the order does not matter.
223 Actual reading of arguments is controlled by the rest of the prompt
224 string (starting with the first character that is not @samp{*} or
225 @samp{@@}).
226 @end itemize
227
228 @node Interactive Codes
229 @comment node-name, next, previous, up
230 @subsection Code Characters for @code{interactive}
231 @cindex interactive code description
232 @cindex description for interactive codes
233 @cindex codes, interactive, description of
234 @cindex characters for interactive codes
235
236 The code character descriptions below contain a number of key words,
237 defined here as follows:
238
239 @table @b
240 @item Completion
241 @cindex interactive completion
242 Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name
243 completion because the argument is read using @code{completing-read}
244 (@pxref{Completion}). @kbd{?} displays a list of possible completions.
245
246 @item Existing
247 Require the name of an existing object. An invalid name is not
248 accepted; the commands to exit the minibuffer do not exit if the current
249 input is not valid.
250
251 @item Default
252 @cindex default argument string
253 A default value of some sort is used if the user enters no text in the
254 minibuffer. The default depends on the code character.
255
256 @item No I/O
257 This code letter computes an argument without reading any input.
258 Therefore, it does not use a prompt string, and any prompt string you
259 supply is ignored.
260
261 Even though the code letter doesn't use a prompt string, you must follow
262 it with a newline if it is not the last code character in the string.
263
264 @item Prompt
265 A prompt immediately follows the code character. The prompt ends either
266 with the end of the string or with a newline.
267
268 @item Special
269 This code character is meaningful only at the beginning of the
270 interactive string, and it does not look for a prompt or a newline.
271 It is a single, isolated character.
272 @end table
273
274 @cindex reading interactive arguments
275 Here are the code character descriptions for use with @code{interactive}:
276
277 @table @samp
278 @item *
279 Signal an error if the current buffer is read-only. Special.
280
281 @item @@
282 Select the window mentioned in the first mouse event in the key
283 sequence that invoked this command. Special.
284
285 @item a
286 A function name (i.e., a symbol satisfying @code{fboundp}). Existing,
287 Completion, Prompt.
288
289 @item b
290 The name of an existing buffer. By default, uses the name of the
291 current buffer (@pxref{Buffers}). Existing, Completion, Default,
292 Prompt.
293
294 @item B
295 A buffer name. The buffer need not exist. By default, uses the name of
296 a recently used buffer other than the current buffer. Completion,
297 Default, Prompt.
298
299 @item c
300 A character. The cursor does not move into the echo area. Prompt.
301
302 @item C
303 A command name (i.e., a symbol satisfying @code{commandp}). Existing,
304 Completion, Prompt.
305
306 @item d
307 @cindex position argument
308 The position of point, as an integer (@pxref{Point}). No I/O.
309
310 @item D
311 A directory name. The default is the current default directory of the
312 current buffer, @code{default-directory} (@pxref{System Environment}).
313 Existing, Completion, Default, Prompt.
314
315 @item e
316 The first or next mouse event in the key sequence that invoked the command.
317 More precisely, @samp{e} gets events that are lists, so you can look at
318 the data in the lists. @xref{Input Events}. No I/O.
319
320 You can use @samp{e} more than once in a single command's interactive
321 specification. If the key sequence that invoked the command has
322 @var{n} events that are lists, the @var{n}th @samp{e} provides the
323 @var{n}th such event. Events that are not lists, such as function keys
324 and @sc{ASCII} characters, do not count where @samp{e} is concerned.
325
326 @item f
327 A file name of an existing file (@pxref{File Names}). The default
328 directory is @code{default-directory}. Existing, Completion, Default,
329 Prompt.
330
331 @item F
332 A file name. The file need not exist. Completion, Default, Prompt.
333
334 @item k
335 A key sequence (@pxref{Keymap Terminology}). This keeps reading events
336 until a command (or undefined command) is found in the current key
337 maps. The key sequence argument is represented as a string or vector.
338 The cursor does not move into the echo area. Prompt.
339
340 This kind of input is used by commands such as @code{describe-key} and
341 @code{global-set-key}.
342
343 @item K
344 A key sequence, whose definition you intend to change. This works like
345 @samp{k}, except that it suppresses, for the last input event in the key
346 sequence, the conversions that are normally used (when necessary) to
347 convert an undefined key into a defined one.
348
349 @item m
350 @cindex marker argument
351 The position of the mark, as an integer. No I/O.
352
353 @item n
354 A number read with the minibuffer. If the input is not a number, the
355 user is asked to try again. The prefix argument, if any, is not used.
356 Prompt.
357
358 @item N
359 @cindex raw prefix argument usage
360 The numeric prefix argument; but if there is no prefix argument, read a
361 number as with @kbd{n}. Requires a number. @xref{Prefix Command
362 Arguments}. Prompt.
363
364 @item p
365 @cindex numeric prefix argument usage
366 The numeric prefix argument. (Note that this @samp{p} is lower case.)
367 No I/O.
368
369 @item P
370 The raw prefix argument. (Note that this @samp{P} is upper case.) No
371 I/O.
372
373 @item r
374 @cindex region argument
375 Point and the mark, as two numeric arguments, smallest first. This is
376 the only code letter that specifies two successive arguments rather than
377 one. No I/O.
378
379 @item s
380 Arbitrary text, read in the minibuffer and returned as a string
381 (@pxref{Text from Minibuffer}). Terminate the input with either
382 @key{LFD} or @key{RET}. (@kbd{C-q} may be used to include either of
383 these characters in the input.) Prompt.
384
385 @item S
386 An interned symbol whose name is read in the minibuffer. Any whitespace
387 character terminates the input. (Use @kbd{C-q} to include whitespace in
388 the string.) Other characters that normally terminate a symbol (e.g.,
389 parentheses and brackets) do not do so here. Prompt.
390
391 @item v
392 A variable declared to be a user option (i.e., satisfying the predicate
393 @code{user-variable-p}). @xref{High-Level Completion}. Existing,
394 Completion, Prompt.
395
396 @item x
397 A Lisp object, specified with its read syntax, terminated with a
398 @key{LFD} or @key{RET}. The object is not evaluated. @xref{Object from
399 Minibuffer}. Prompt.
400
401 @item X
402 @cindex evaluated expression argument
403 A Lisp form is read as with @kbd{x}, but then evaluated so that its
404 value becomes the argument for the command. Prompt.
405 @end table
406
407 @node Interactive Examples
408 @comment node-name, next, previous, up
409 @subsection Examples of Using @code{interactive}
410 @cindex examples of using @code{interactive}
411 @cindex @code{interactive}, examples of using
412
413 Here are some examples of @code{interactive}:
414
415 @example
416 @group
417 (defun foo1 () ; @r{@code{foo1} takes no arguments,}
418 (interactive) ; @r{just moves forward two words.}
419 (forward-word 2))
420 @result{} foo1
421 @end group
422
423 @group
424 (defun foo2 (n) ; @r{@code{foo2} takes one argument,}
425 (interactive "p") ; @r{which is the numeric prefix.}
426 (forward-word (* 2 n)))
427 @result{} foo2
428 @end group
429
430 @group
431 (defun foo3 (n) ; @r{@code{foo3} takes one argument,}
432 (interactive "nCount:") ; @r{which is read with the Minibuffer.}
433 (forward-word (* 2 n)))
434 @result{} foo3
435 @end group
436
437 @group
438 (defun three-b (b1 b2 b3)
439 "Select three existing buffers.
440 Put them into three windows, selecting the last one."
441 @end group
442 (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
443 (delete-other-windows)
444 (split-window (selected-window) 8)
445 (switch-to-buffer b1)
446 (other-window 1)
447 (split-window (selected-window) 8)
448 (switch-to-buffer b2)
449 (other-window 1)
450 (switch-to-buffer b3))
451 @result{} three-b
452 @group
453 (three-b "*scratch*" "declarations.texi" "*mail*")
454 @result{} nil
455 @end group
456 @end example
457
458 @node Interactive Call
459 @section Interactive Call
460 @cindex interactive call
461
462 After the command loop has translated a key sequence into a
463 definition, it invokes that definition using the function
464 @code{command-execute}. If the definition is a function that is a
465 command, @code{command-execute} calls @code{call-interactively}, which
466 reads the arguments and calls the command. You can also call these
467 functions yourself.
468
469 @defun commandp object
470 Returns @code{t} if @var{object} is suitable for calling interactively;
471 that is, if @var{object} is a command. Otherwise, returns @code{nil}.
472
473 The interactively callable objects include strings and vectors (treated
474 as keyboard macros), lambda expressions that contain a top-level call to
475 @code{interactive}, byte-code function objects made from such lambda
476 expressions, autoload objects that are declared as interactive
477 (non-@code{nil} fourth argument to @code{autoload}), and some of the
478 primitive functions.
479
480 A symbol is @code{commandp} if its function definition is
481 @code{commandp}.
482
483 Keys and keymaps are not commands. Rather, they are used to look up
484 commands (@pxref{Keymaps}).
485
486 See @code{documentation} in @ref{Accessing Documentation}, for a
487 realistic example of using @code{commandp}.
488 @end defun
489
490 @defun call-interactively command &optional record-flag
491 This function calls the interactively callable function @var{command},
492 reading arguments according to its interactive calling specifications.
493 An error is signaled if @var{command} is not a function or if it cannot
494 be called interactively (i.e., is not a command). Note that keyboard
495 macros (strings and vectors) are not accepted, even though they are
496 considered commands, because they are not functions.
497
498 @cindex record command history
499 If @var{record-flag} is non-@code{nil}, then this command and its
500 arguments are unconditionally added to the list @code{command-history}.
501 Otherwise, the command is added only if it uses the minibuffer to read
502 an argument. @xref{Command History}.
503 @end defun
504
505 @defun command-execute command &optional record-flag
506 @cindex keyboard macro execution
507 This function executes @var{command} as an editing command. The
508 argument @var{command} must satisfy the @code{commandp} predicate; i.e.,
509 it must be an interactively callable function or a keyboard macro.
510
511 A string or vector as @var{command} is executed with
512 @code{execute-kbd-macro}. A function is passed to
513 @code{call-interactively}, along with the optional @var{record-flag}.
514
515 A symbol is handled by using its function definition in its place. A
516 symbol with an @code{autoload} definition counts as a command if it was
517 declared to stand for an interactively callable function. Such a
518 definition is handled by loading the specified library and then
519 rechecking the definition of the symbol.
520 @end defun
521
522 @deffn Command execute-extended-command prefix-argument
523 @cindex read command name
524 This function reads a command name from the minibuffer using
525 @code{completing-read} (@pxref{Completion}). Then it uses
526 @code{command-execute} to call the specified command. Whatever that
527 command returns becomes the value of @code{execute-extended-command}.
528
529 @cindex execute with prefix argument
530 If the command asks for a prefix argument, it receives the value
531 @var{prefix-argument}. If @code{execute-extended-command} is called
532 interactively, the current raw prefix argument is used for
533 @var{prefix-argument}, and thus passed on to whatever command is run.
534
535 @c !!! Should this be @kindex?
536 @cindex @kbd{M-x}
537 @code{execute-extended-command} is the normal definition of @kbd{M-x},
538 so it uses the string @w{@samp{M-x }} as a prompt. (It would be better
539 to take the prompt from the events used to invoke
540 @code{execute-extended-command}, but that is painful to implement.) A
541 description of the value of the prefix argument, if any, also becomes
542 part of the prompt.
543
544 @example
545 @group
546 (execute-extended-command 1)
547 ---------- Buffer: Minibuffer ----------
548 1 M-x forward-word RET
549 ---------- Buffer: Minibuffer ----------
550 @result{} t
551 @end group
552 @end example
553 @end deffn
554
555 @defun interactive-p
556 This function returns @code{t} if the containing function (the one that
557 called @code{interactive-p}) was called interactively, with the function
558 @code{call-interactively}. (It makes no difference whether
559 @code{call-interactively} was called from Lisp or directly from the
560 editor command loop.) If the containing function was called by Lisp
561 evaluation (or with @code{apply} or @code{funcall}), then it was not
562 called interactively.
563
564 The most common use of @code{interactive-p} is for deciding whether to
565 print an informative message. As a special exception,
566 @code{interactive-p} returns @code{nil} whenever a keyboard macro is
567 being run. This is to suppress the informative messages and speed
568 execution of the macro.
569
570 For example:
571
572 @example
573 @group
574 (defun foo ()
575 (interactive)
576 (and (interactive-p)
577 (message "foo")))
578 @result{} foo
579 @end group
580
581 @group
582 (defun bar ()
583 (interactive)
584 (setq foobar (list (foo) (interactive-p))))
585 @result{} bar
586 @end group
587
588 @group
589 ;; @r{Type @kbd{M-x foo}.}
590 @print{} foo
591 @end group
592
593 @group
594 ;; @r{Type @kbd{M-x bar}.}
595 ;; @r{This does not print anything.}
596 @end group
597
598 @group
599 foobar
600 @result{} (nil t)
601 @end group
602 @end example
603 @end defun
604
605 @node Command Loop Info
606 @comment node-name, next, previous, up
607 @section Information from the Command Loop
608
609 The editor command loop sets several Lisp variables to keep status
610 records for itself and for commands that are run.
611
612 @defvar last-command
613 This variable records the name of the previous command executed by the
614 command loop (the one before the current command). Normally the value
615 is a symbol with a function definition, but this is not guaranteed.
616
617 The value is copied from @code{this-command} when a command returns to
618 the command loop, except when the command specifies a prefix argument
619 for the following command.
620
621 This variable is always local to the current terminal and cannot be
622 buffer-local. @xref{Multiple Displays}.
623 @end defvar
624
625 @defvar this-command
626 @cindex current command
627 This variable records the name of the command now being executed by
628 the editor command loop. Like @code{last-command}, it is normally a symbol
629 with a function definition.
630
631 The command loop sets this variable just before running a command, and
632 copies its value into @code{last-command} when the command finishes
633 (unless the command specifies a prefix argument for the following
634 command).
635
636 @cindex kill command repetition
637 Some commands set this variable during their execution, as a flag for
638 whatever command runs next. In particular, the functions for killing text
639 set @code{this-command} to @code{kill-region} so that any kill commands
640 immediately following will know to append the killed text to the
641 previous kill.
642 @end defvar
643
644 If you do not want a particular command to be recognized as the previous
645 command in the case where it got an error, you must code that command to
646 prevent this. One way is to set @code{this-command} to @code{t} at the
647 beginning of the command, and set @code{this-command} back to its proper
648 value at the end, like this:
649
650 @example
651 (defun foo (args@dots{})
652 (interactive @dots{})
653 (let ((old-this-command this-command))
654 (setq this-command t)
655 @r{@dots{}do the work@dots{}}
656 (setq this-command old-this-command)))
657 @end example
658
659 @defun this-command-keys
660 This function returns a string or vector containing the key sequence
661 that invoked the present command, plus any previous commands that
662 generated the prefix argument for this command. The value is a string
663 if all those events were characters. @xref{Input Events}.
664
665 @example
666 @group
667 (this-command-keys)
668 ;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
669 @result{} "^U^X^E"
670 @end group
671 @end example
672 @end defun
673
674 @defvar last-nonmenu-event
675 This variable holds the last input event read as part of a key
676 sequence, not counting events resulting from mouse menus.
677
678 One use of this variable is to figure out a good default location to
679 pop up another menu.
680 @end defvar
681
682 @defvar last-command-event
683 @defvarx last-command-char
684 This variable is set to the last input event that was read by the
685 command loop as part of a command. The principal use of this variable
686 is in @code{self-insert-command}, which uses it to decide which
687 character to insert.
688
689 @example
690 @group
691 last-command-event
692 ;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
693 @result{} 5
694 @end group
695 @end example
696
697 @noindent
698 The value is 5 because that is the @sc{ASCII} code for @kbd{C-e}.
699
700 The alias @code{last-command-char} exists for compatibility with
701 Emacs version 18.
702 @end defvar
703
704 @c Emacs 19 feature
705 @defvar last-event-frame
706 This variable records which frame the last input event was directed to.
707 Usually this is the frame that was selected when the event was
708 generated, but if that frame has redirected input focus to another
709 frame, the value is the frame to which the event was redirected.
710 @xref{Input Focus}.
711 @end defvar
712
713 @node Input Events
714 @section Input Events
715 @cindex events
716 @cindex input events
717
718 The Emacs command loop reads a sequence of @dfn{input events} that
719 represent keyboard or mouse activity. The events for keyboard activity
720 are characters or symbols; mouse events are always lists. This section
721 describes the representation and meaning of input events in detail.
722
723 @defun eventp object
724 This function returns non-@code{nil} if @var{object} is an input event.
725 @end defun
726
727 @menu
728 * Keyboard Events:: Ordinary characters--keys with symbols on them.
729 * Function Keys:: Function keys--keys with names, not symbols.
730 * Mouse Events:: Overview of mouse events.
731 * Click Events:: Pushing and releasing a mouse button.
732 * Drag Events:: Moving the mouse before releasing the button.
733 * Button-Down Events:: A button was pushed and not yet released.
734 * Repeat Events:: Double and triple click (or drag, or down).
735 * Motion Events:: Just moving the mouse, not pushing a button.
736 * Focus Events:: Moving the mouse between frames.
737 * Misc Events:: Other events window systems can generate.
738 * Event Examples:: Examples of the lists for mouse events.
739 * Classifying Events:: Finding the modifier keys in an event symbol.
740 Event types.
741 * Accessing Events:: Functions to extract info from events.
742 * Strings of Events:: Special considerations for putting
743 keyboard character events in a string.
744 @end menu
745
746 @node Keyboard Events
747 @subsection Keyboard Events
748
749 There are two kinds of input you can get from the keyboard: ordinary
750 keys, and function keys. Ordinary keys correspond to characters; the
751 events they generate are represented in Lisp as characters. In Emacs
752 versions 18 and earlier, characters were the only events. The event
753 type of a character event is the character itself (an integer);
754 see @ref{Classifying Events}.
755
756 @cindex modifier bits (of input character)
757 @cindex basic code (of input character)
758 An input character event consists of a @dfn{basic code} between 0 and
759 255, plus any or all of these @dfn{modifier bits}:
760
761 @table @asis
762 @item meta
763 The
764 @iftex
765 $2^{27}$
766 @end iftex
767 @ifinfo
768 2**27
769 @end ifinfo
770 bit in the character code indicates a character
771 typed with the meta key held down.
772
773 @item control
774 The
775 @iftex
776 $2^{26}$
777 @end iftex
778 @ifinfo
779 2**26
780 @end ifinfo
781 bit in the character code indicates a non-@sc{ASCII}
782 control character.
783
784 @sc{ASCII} control characters such as @kbd{C-a} have special basic
785 codes of their own, so Emacs needs no special bit to indicate them.
786 Thus, the code for @kbd{C-a} is just 1.
787
788 But if you type a control combination not in @sc{ASCII}, such as
789 @kbd{%} with the control key, the numeric value you get is the code
790 for @kbd{%} plus
791 @iftex
792 $2^{26}$
793 @end iftex
794 @ifinfo
795 2**26
796 @end ifinfo
797 (assuming the terminal supports non-@sc{ASCII}
798 control characters).
799
800 @item shift
801 The
802 @iftex
803 $2^{25}$
804 @end iftex
805 @ifinfo
806 2**25
807 @end ifinfo
808 bit in the character code indicates an @sc{ASCII} control
809 character typed with the shift key held down.
810
811 For letters, the basic code indicates upper versus lower case; for
812 digits and punctuation, the shift key selects an entirely different
813 character with a different basic code. In order to keep within
814 the @sc{ASCII} character set whenever possible, Emacs avoids using
815 the
816 @iftex
817 $2^{25}$
818 @end iftex
819 @ifinfo
820 2**25
821 @end ifinfo
822 bit for those characters.
823
824 However, @sc{ASCII} provides no way to distinguish @kbd{C-A} from
825 @kbd{C-a}, so Emacs uses the
826 @iftex
827 $2^{25}$
828 @end iftex
829 @ifinfo
830 2**25
831 @end ifinfo
832 bit in @kbd{C-A} and not in
833 @kbd{C-a}.
834
835 @item hyper
836 The
837 @iftex
838 $2^{24}$
839 @end iftex
840 @ifinfo
841 2**24
842 @end ifinfo
843 bit in the character code indicates a character
844 typed with the hyper key held down.
845
846 @item super
847 The
848 @iftex
849 $2^{23}$
850 @end iftex
851 @ifinfo
852 2**23
853 @end ifinfo
854 bit in the character code indicates a character
855 typed with the super key held down.
856
857 @item alt
858 The
859 @iftex
860 $2^{22}$
861 @end iftex
862 @ifinfo
863 2**22
864 @end ifinfo
865 bit in the character code indicates a character typed with
866 the alt key held down. (On some terminals, the key labeled @key{ALT}
867 is actually the meta key.)
868 @end table
869
870 It is best to avoid mentioning specific bit numbers in your program.
871 To test the modifier bits of a character, use the function
872 @code{event-modifiers} (@pxref{Classifying Events}). When making key
873 bindings, you can use the read syntax for characters with modifier bits
874 (@samp{\C-}, @samp{\M-}, and so on). For making key bindings with
875 @code{define-key}, you can use lists such as @code{(control hyper ?x)} to
876 specify the characters (@pxref{Changing Key Bindings}). The function
877 @code{event-convert-list} converts such a list into an event type
878 (@pxref{Classifying Events}).
879
880 @node Function Keys
881 @subsection Function Keys
882
883 @cindex function keys
884 Most keyboards also have @dfn{function keys}---keys that have names or
885 symbols that are not characters. Function keys are represented in Lisp
886 as symbols; the symbol's name is the function key's label, in lower
887 case. For example, pressing a key labeled @key{F1} places the symbol
888 @code{f1} in the input stream.
889
890 The event type of a function key event is the event symbol itself.
891 @xref{Classifying Events}.
892
893 Here are a few special cases in the symbol-naming convention for
894 function keys:
895
896 @table @asis
897 @item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete}
898 These keys correspond to common @sc{ASCII} control characters that have
899 special keys on most keyboards.
900
901 In @sc{ASCII}, @kbd{C-i} and @key{TAB} are the same character. If the
902 terminal can distinguish between them, Emacs conveys the distinction to
903 Lisp programs by representing the former as the integer 9, and the
904 latter as the symbol @code{tab}.
905
906 Most of the time, it's not useful to distinguish the two. So normally
907 @code{function-key-map} is set up to map @code{tab} into 9. Thus, a key
908 binding for character code 9 (the character @kbd{C-i}) also applies to
909 @code{tab}. Likewise for the other symbols in this group. The function
910 @code{read-char} likewise converts these events into characters.
911
912 In @sc{ASCII}, @key{BS} is really @kbd{C-h}. But @code{backspace}
913 converts into the character code 127 (@key{DEL}), not into code 8
914 (@key{BS}). This is what most users prefer.
915
916 @item @code{left}, @code{up}, @code{right}, @code{down}
917 Cursor arrow keys
918 @item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{}
919 Keypad keys (to the right of the regular keyboard).
920 @item @code{kp-0}, @code{kp-1}, @dots{}
921 Keypad keys with digits.
922 @item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
923 Keypad PF keys.
924 @item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down}
925 Keypad arrow keys. Emacs normally translates these
926 into the non-keypad keys @code{home}, @code{left}, @dots{}
927 @item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete}
928 Additional keypad duplicates of keys ordinarily found elsewhere. Emacs
929 normally translates these into the like-named non-keypad keys.
930 @end table
931
932 You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER},
933 @key{META}, @key{SHIFT}, and @key{SUPER} with function keys. The way to
934 represent them is with prefixes in the symbol name:
935
936 @table @samp
937 @item A-
938 The alt modifier.
939 @item C-
940 The control modifier.
941 @item H-
942 The hyper modifier.
943 @item M-
944 The meta modifier.
945 @item S-
946 The shift modifier.
947 @item s-
948 The super modifier.
949 @end table
950
951 Thus, the symbol for the key @key{F3} with @key{META} held down is
952 @code{M-f3}. When you use more than one prefix, we recommend you
953 write them in alphabetical order; but the order does not matter in
954 arguments to the key-binding lookup and modification functions.
955
956 @node Mouse Events
957 @subsection Mouse Events
958
959 Emacs supports four kinds of mouse events: click events, drag events,
960 button-down events, and motion events. All mouse events are represented
961 as lists. The @sc{car} of the list is the event type; this says which
962 mouse button was involved, and which modifier keys were used with it.
963 The event type can also distinguish double or triple button presses
964 (@pxref{Repeat Events}). The rest of the list elements give position
965 and time information.
966
967 For key lookup, only the event type matters: two events of the same type
968 necessarily run the same command. The command can access the full
969 values of these events using the @samp{e} interactive code.
970 @xref{Interactive Codes}.
971
972 A key sequence that starts with a mouse event is read using the keymaps
973 of the buffer in the window that the mouse was in, not the current
974 buffer. This does not imply that clicking in a window selects that
975 window or its buffer---that is entirely under the control of the command
976 binding of the key sequence.
977
978 @node Click Events
979 @subsection Click Events
980 @cindex click event
981 @cindex mouse click event
982
983 When the user presses a mouse button and releases it at the same
984 location, that generates a @dfn{click} event. Mouse click events have
985 this form:
986
987 @example
988 (@var{event-type}
989 (@var{window} @var{buffer-pos} (@var{x} . @var{y}) @var{timestamp})
990 @var{click-count})
991 @end example
992
993 Here is what the elements normally mean:
994
995 @table @asis
996 @item @var{event-type}
997 This is a symbol that indicates which mouse button was used. It is
998 one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the
999 buttons are numbered left to right.
1000
1001 You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-},
1002 @samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift
1003 and super, just as you would with function keys.
1004
1005 This symbol also serves as the event type of the event. Key bindings
1006 describe events by their types; thus, if there is a key binding for
1007 @code{mouse-1}, that binding would apply to all events whose
1008 @var{event-type} is @code{mouse-1}.
1009
1010 @item @var{window}
1011 This is the window in which the click occurred.
1012
1013 @item @var{x}, @var{y}
1014 These are the pixel-denominated coordinates of the click, relative to
1015 the top left corner of @var{window}, which is @code{(0 . 0)}.
1016
1017 @item @var{buffer-pos}
1018 This is the buffer position of the character clicked on.
1019
1020 @item @var{timestamp}
1021 This is the time at which the event occurred, in milliseconds. (Since
1022 this value wraps around the entire range of Emacs Lisp integers in about
1023 five hours, it is useful only for relating the times of nearby events.)
1024
1025 @item @var{click-count}
1026 This is the number of rapid repeated presses so far of the same mouse
1027 button. @xref{Repeat Events}.
1028 @end table
1029
1030 The meanings of @var{buffer-pos}, @var{x} and @var{y} are somewhat
1031 different when the event location is in a special part of the screen,
1032 such as the mode line or a scroll bar.
1033
1034 If the location is in a scroll bar, then @var{buffer-pos} is the symbol
1035 @code{vertical-scroll-bar} or @code{horizontal-scroll-bar}, and the pair
1036 @code{(@var{x} . @var{y})} is replaced with a pair @code{(@var{portion}
1037 . @var{whole})}, where @var{portion} is the distance of the click from
1038 the top or left end of the scroll bar, and @var{whole} is the length of
1039 the entire scroll bar.
1040
1041 If the position is on a mode line or the vertical line separating
1042 @var{window} from its neighbor to the right, then @var{buffer-pos} is
1043 the symbol @code{mode-line} or @code{vertical-line}. For the mode line,
1044 @var{y} does not have meaningful data. For the vertical line, @var{x}
1045 does not have meaningful data.
1046
1047 In one special case, @var{buffer-pos} is a list containing a symbol (one
1048 of the symbols listed above) instead of just the symbol. This happens
1049 after the imaginary prefix keys for the event are inserted into the
1050 input stream. @xref{Key Sequence Input}.
1051
1052 @node Drag Events
1053 @subsection Drag Events
1054 @cindex drag event
1055 @cindex mouse drag event
1056
1057 With Emacs, you can have a drag event without even changing your
1058 clothes. A @dfn{drag event} happens every time the user presses a mouse
1059 button and then moves the mouse to a different character position before
1060 releasing the button. Like all mouse events, drag events are
1061 represented in Lisp as lists. The lists record both the starting mouse
1062 position and the final position, like this:
1063
1064 @example
1065 (@var{event-type}
1066 (@var{window1} @var{buffer-pos1} (@var{x1} . @var{y1}) @var{timestamp1})
1067 (@var{window2} @var{buffer-pos2} (@var{x2} . @var{y2}) @var{timestamp2})
1068 @var{click-count})
1069 @end example
1070
1071 For a drag event, the name of the symbol @var{event-type} contains the
1072 prefix @samp{drag-}. The second and third elements of the event give
1073 the starting and ending position of the drag. Aside from that, the data
1074 have the same meanings as in a click event (@pxref{Click Events}). You
1075 can access the second element of any mouse event in the same way, with
1076 no need to distinguish drag events from others.
1077
1078 The @samp{drag-} prefix follows the modifier key prefixes such as
1079 @samp{C-} and @samp{M-}.
1080
1081 If @code{read-key-sequence} receives a drag event that has no key
1082 binding, and the corresponding click event does have a binding, it
1083 changes the drag event into a click event at the drag's starting
1084 position. This means that you don't have to distinguish between click
1085 and drag events unless you want to.
1086
1087 @node Button-Down Events
1088 @subsection Button-Down Events
1089 @cindex button-down event
1090
1091 Click and drag events happen when the user releases a mouse button.
1092 They cannot happen earlier, because there is no way to distinguish a
1093 click from a drag until the button is released.
1094
1095 If you want to take action as soon as a button is pressed, you need to
1096 handle @dfn{button-down} events.@footnote{Button-down is the
1097 conservative antithesis of drag.} These occur as soon as a button is
1098 pressed. They are represented by lists that look exactly like click
1099 events (@pxref{Click Events}), except that the @var{event-type} symbol
1100 name contains the prefix @samp{down-}. The @samp{down-} prefix follows
1101 modifier key prefixes such as @samp{C-} and @samp{M-}.
1102
1103 The function @code{read-key-sequence}, and therefore the Emacs command
1104 loop as well, ignore any button-down events that don't have command
1105 bindings. This means that you need not worry about defining button-down
1106 events unless you want them to do something. The usual reason to define
1107 a button-down event is so that you can track mouse motion (by reading
1108 motion events) until the button is released. @xref{Motion Events}.
1109
1110 @node Repeat Events
1111 @subsection Repeat Events
1112 @cindex repeat events
1113 @cindex double-click events
1114 @cindex triple-click events
1115
1116 If you press the same mouse button more than once in quick succession
1117 without moving the mouse, Emacs generates special @dfn{repeat} mouse
1118 events for the second and subsequent presses.
1119
1120 The most common repeat events are @dfn{double-click} events. Emacs
1121 generates a double-click event when you click a button twice; the event
1122 happens when you release the button (as is normal for all click
1123 events).
1124
1125 The event type of a double-click event contains the prefix
1126 @samp{double-}. Thus, a double click on the second mouse button with
1127 @key{meta} held down comes to the Lisp program as
1128 @code{M-double-mouse-2}. If a double-click event has no binding, the
1129 binding of the corresponding ordinary click event is used to execute
1130 it. Thus, you need not pay attention to the double click feature
1131 unless you really want to.
1132
1133 When the user performs a double click, Emacs generates first an ordinary
1134 click event, and then a double-click event. Therefore, you must design
1135 the command binding of the double click event to assume that the
1136 single-click command has already run. It must produce the desired
1137 results of a double click, starting from the results of a single click.
1138
1139 This is convenient, if the meaning of a double click somehow ``builds
1140 on'' the meaning of a single click---which is recommended user interface
1141 design practice for double clicks.
1142
1143 If you click a button, then press it down again and start moving the
1144 mouse with the button held down, then you get a @dfn{double-drag} event
1145 when you ultimately release the button. Its event type contains
1146 @samp{double-drag} instead of just @samp{drag}. If a double-drag event
1147 has no binding, Emacs looks for an alternate binding as if the event
1148 were an ordinary drag.
1149
1150 Before the double-click or double-drag event, Emacs generates a
1151 @dfn{double-down} event when the user presses the button down for the
1152 second time. Its event type contains @samp{double-down} instead of just
1153 @samp{down}. If a double-down event has no binding, Emacs looks for an
1154 alternate binding as if the event were an ordinary button-down event.
1155 If it finds no binding that way either, the double-down event is
1156 ignored.
1157
1158 To summarize, when you click a button and then press it again right
1159 away, Emacs generates a down event and a click event for the first
1160 click, a double-down event when you press the button again, and finally
1161 either a double-click or a double-drag event.
1162
1163 If you click a button twice and then press it again, all in quick
1164 succession, Emacs generates a @dfn{triple-down} event, followed by
1165 either a @dfn{triple-click} or a @dfn{triple-drag}. The event types of
1166 these events contain @samp{triple} instead of @samp{double}. If any
1167 triple event has no binding, Emacs uses the binding that it would use
1168 for the corresponding double event.
1169
1170 If you click a button three or more times and then press it again, the
1171 events for the presses beyond the third are all triple events. Emacs
1172 does not have separate event types for quadruple, quintuple, etc.@:
1173 events. However, you can look at the event list to find out precisely
1174 how many times the button was pressed.
1175
1176 @defun event-click-count event
1177 This function returns the number of consecutive button presses that led
1178 up to @var{event}. If @var{event} is a double-down, double-click or
1179 double-drag event, the value is 2. If @var{event} is a triple event,
1180 the value is 3 or greater. If @var{event} is an ordinary mouse event
1181 (not a repeat event), the value is 1.
1182 @end defun
1183
1184 @defvar double-click-time
1185 To generate repeat events, successive mouse button presses must be at
1186 the same screen position, and the number of milliseconds between
1187 successive button presses must be less than the value of
1188 @code{double-click-time}. Setting @code{double-click-time} to
1189 @code{nil} disables multi-click detection entirely. Setting it to
1190 @code{t} removes the time limit; Emacs then detects multi-clicks by
1191 position only.
1192 @end defvar
1193
1194 @node Motion Events
1195 @subsection Motion Events
1196 @cindex motion event
1197 @cindex mouse motion events
1198
1199 Emacs sometimes generates @dfn{mouse motion} events to describe motion
1200 of the mouse without any button activity. Mouse motion events are
1201 represented by lists that look like this:
1202
1203 @example
1204 (mouse-movement
1205 (@var{window} @var{buffer-pos} (@var{x} . @var{y}) @var{timestamp}))
1206 @end example
1207
1208 The second element of the list describes the current position of the
1209 mouse, just as in a click event (@pxref{Click Events}).
1210
1211 The special form @code{track-mouse} enables generation of motion events
1212 within its body. Outside of @code{track-mouse} forms, Emacs does not
1213 generate events for mere motion of the mouse, and these events do not
1214 appear.
1215
1216 @defspec track-mouse body@dots{}
1217 This special form executes @var{body}, with generation of mouse motion
1218 events enabled. Typically @var{body} would use @code{read-event}
1219 to read the motion events and modify the display accordingly.
1220
1221 When the user releases the button, that generates a click event.
1222 Typically, @var{body} should return when it sees the click event, and
1223 discard that event.
1224 @end defspec
1225
1226 @node Focus Events
1227 @subsection Focus Events
1228 @cindex focus event
1229
1230 Window systems provide general ways for the user to control which window
1231 gets keyboard input. This choice of window is called the @dfn{focus}.
1232 When the user does something to switch between Emacs frames, that
1233 generates a @dfn{focus event}. The normal definition of a focus event,
1234 in the global keymap, is to select a new frame within Emacs, as the user
1235 would expect. @xref{Input Focus}.
1236
1237 Focus events are represented in Lisp as lists that look like this:
1238
1239 @example
1240 (switch-frame @var{new-frame})
1241 @end example
1242
1243 @noindent
1244 where @var{new-frame} is the frame switched to.
1245
1246 Most X window managers are set up so that just moving the mouse into a
1247 window is enough to set the focus there. Emacs appears to do this,
1248 because it changes the cursor to solid in the new frame. However, there
1249 is no need for the Lisp program to know about the focus change until
1250 some other kind of input arrives. So Emacs generates a focus event only
1251 when the user actually types a keyboard key or presses a mouse button in
1252 the new frame; just moving the mouse between frames does not generate a
1253 focus event.
1254
1255 A focus event in the middle of a key sequence would garble the
1256 sequence. So Emacs never generates a focus event in the middle of a key
1257 sequence. If the user changes focus in the middle of a key
1258 sequence---that is, after a prefix key---then Emacs reorders the events
1259 so that the focus event comes either before or after the multi-event key
1260 sequence, and not within it.
1261
1262 @node Misc Events
1263 @subsection Miscellaneous Window System Events
1264
1265 A few other event types represent occurrences within the window system.
1266
1267 @table @code
1268 @cindex @code{delete-frame} event
1269 @item (delete-frame (@var{frame}))
1270 This kind of event indicates that the user gave the window manager
1271 a command to delete a particular window, which happens to be an Emacs frame.
1272
1273 The standard definition of the @code{delete-frame} event is to delete @var{frame}.
1274
1275 @cindex @code{iconify-frame} event
1276 @item (iconify-frame (@var{frame}))
1277 This kind of event indicates that the user iconified @var{frame} using
1278 the window manager. Its standard definition is @code{ignore}; since the
1279 frame has already been iconified, Emacs has no work to do. The purpose
1280 of this event type is so that you can keep track of such events if you
1281 want to.
1282
1283 @cindex @code{make-frame-visible} event
1284 @item (make-frame-visible (@var{frame}))
1285 This kind of event indicates that the user deiconified @var{frame} using
1286 the window manager. Its standard definition is @code{ignore}; since the
1287 frame has already been made visible, Emacs has no work to do.
1288 @end table
1289
1290 If one of these events arrives in the middle of a key sequence---that
1291 is, after a prefix key---then Emacs reorders the events so that this
1292 event comes either before or after the multi-event key sequence, not
1293 within it.
1294
1295 @node Event Examples
1296 @subsection Event Examples
1297
1298 If the user presses and releases the left mouse button over the same
1299 location, that generates a sequence of events like this:
1300
1301 @smallexample
1302 (down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
1303 (mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180))
1304 @end smallexample
1305
1306 While holding the control key down, the user might hold down the
1307 second mouse button, and drag the mouse from one line to the next.
1308 That produces two events, as shown here:
1309
1310 @smallexample
1311 (C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219))
1312 (C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)
1313 (#<window 18 on NEWS> 3510 (0 . 28) -729648))
1314 @end smallexample
1315
1316 While holding down the meta and shift keys, the user might press the
1317 second mouse button on the window's mode line, and then drag the mouse
1318 into another window. That produces a pair of events like these:
1319
1320 @smallexample
1321 (M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844))
1322 (M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)
1323 (#<window 20 on carlton-sanskrit.tex> 161 (33 . 3)
1324 -453816))
1325 @end smallexample
1326
1327 @node Classifying Events
1328 @subsection Classifying Events
1329 @cindex event type
1330
1331 Every event has an @dfn{event type}, which classifies the event for
1332 key binding purposes. For a keyboard event, the event type equals the
1333 event value; thus, the event type for a character is the character, and
1334 the event type for a function key symbol is the symbol itself. For
1335 events that are lists, the event type is the symbol in the @sc{car} of
1336 the list. Thus, the event type is always a symbol or a character.
1337
1338 Two events of the same type are equivalent where key bindings are
1339 concerned; thus, they always run the same command. That does not
1340 necessarily mean they do the same things, however, as some commands look
1341 at the whole event to decide what to do. For example, some commands use
1342 the location of a mouse event to decide where in the buffer to act.
1343
1344 Sometimes broader classifications of events are useful. For example,
1345 you might want to ask whether an event involved the @key{META} key,
1346 regardless of which other key or mouse button was used.
1347
1348 The functions @code{event-modifiers} and @code{event-basic-type} are
1349 provided to get such information conveniently.
1350
1351 @defun event-modifiers event
1352 This function returns a list of the modifiers that @var{event} has. The
1353 modifiers are symbols; they include @code{shift}, @code{control},
1354 @code{meta}, @code{alt}, @code{hyper} and @code{super}. In addition,
1355 the modifiers list of a mouse event symbol always contains one of
1356 @code{click}, @code{drag}, and @code{down}.
1357
1358 The argument @var{event} may be an entire event object, or just an event
1359 type.
1360
1361 Here are some examples:
1362
1363 @example
1364 (event-modifiers ?a)
1365 @result{} nil
1366 (event-modifiers ?\C-a)
1367 @result{} (control)
1368 (event-modifiers ?\C-%)
1369 @result{} (control)
1370 (event-modifiers ?\C-\S-a)
1371 @result{} (control shift)
1372 (event-modifiers 'f5)
1373 @result{} nil
1374 (event-modifiers 's-f5)
1375 @result{} (super)
1376 (event-modifiers 'M-S-f5)
1377 @result{} (meta shift)
1378 (event-modifiers 'mouse-1)
1379 @result{} (click)
1380 (event-modifiers 'down-mouse-1)
1381 @result{} (down)
1382 @end example
1383
1384 The modifiers list for a click event explicitly contains @code{click},
1385 but the event symbol name itself does not contain @samp{click}.
1386 @end defun
1387
1388 @defun event-basic-type event
1389 This function returns the key or mouse button that @var{event}
1390 describes, with all modifiers removed. For example:
1391
1392 @example
1393 (event-basic-type ?a)
1394 @result{} 97
1395 (event-basic-type ?A)
1396 @result{} 97
1397 (event-basic-type ?\C-a)
1398 @result{} 97
1399 (event-basic-type ?\C-\S-a)
1400 @result{} 97
1401 (event-basic-type 'f5)
1402 @result{} f5
1403 (event-basic-type 's-f5)
1404 @result{} f5
1405 (event-basic-type 'M-S-f5)
1406 @result{} f5
1407 (event-basic-type 'down-mouse-1)
1408 @result{} mouse-1
1409 @end example
1410 @end defun
1411
1412 @defun mouse-movement-p object
1413 This function returns non-@code{nil} if @var{object} is a mouse movement
1414 event.
1415 @end defun
1416
1417 @defun event-convert-list list
1418 This function converts a list of modifier names and a basic event type
1419 to an event type which specifies all of them. For example,
1420
1421 @example
1422 (event-convert-list '(control ?a))
1423 @result{} 1
1424 (event-convert-list '(control meta ?a))
1425 @result{} -134217727
1426 (event-convert-list '(control super f1))
1427 @result{} C-s-f1
1428 @end example
1429 @end defun
1430
1431 @node Accessing Events
1432 @subsection Accessing Events
1433
1434 This section describes convenient functions for accessing the data in
1435 a mouse button or motion event.
1436
1437 These two functions return the starting or ending position of a
1438 mouse-button event. The position is a list of this form:
1439
1440 @example
1441 (@var{window} @var{buffer-position} (@var{x} . @var{y}) @var{timestamp})
1442 @end example
1443
1444 @defun event-start event
1445 This returns the starting position of @var{event}.
1446
1447 If @var{event} is a click or button-down event, this returns the
1448 location of the event. If @var{event} is a drag event, this returns the
1449 drag's starting position.
1450 @end defun
1451
1452 @defun event-end event
1453 This returns the ending position of @var{event}.
1454
1455 If @var{event} is a drag event, this returns the position where the user
1456 released the mouse button. If @var{event} is a click or button-down
1457 event, the value is actually the starting position, which is the only
1458 position such events have.
1459 @end defun
1460
1461 These four functions take a position as described above, and return
1462 various parts of it.
1463
1464 @defun posn-window position
1465 Return the window that @var{position} is in.
1466 @end defun
1467
1468 @defun posn-point position
1469 Return the buffer position in @var{position}. This is an integer.
1470 @end defun
1471
1472 @defun posn-x-y position
1473 Return the pixel-based x and y coordinates in @var{position}, as a cons
1474 cell @code{(@var{x} . @var{y})}.
1475 @end defun
1476
1477 @defun posn-col-row position
1478 Return the row and column (in units of characters) of @var{position}, as
1479 a cons cell @code{(@var{col} . @var{row})}. These are computed from the
1480 @var{x} and @var{y} values actually found in @var{position}.
1481 @end defun
1482
1483 @defun posn-timestamp position
1484 Return the timestamp in @var{position}.
1485 @end defun
1486
1487 @defun scroll-bar-event-ratio event
1488 This function returns the fractional vertical position of a scroll bar
1489 event within the scroll bar. The value is a cons cell
1490 @code{(@var{portion} . @var{whole})} containing two integers whose ratio
1491 is the fractional position.
1492 @end defun
1493
1494 @defun scroll-bar-scale ratio total
1495 This function multiplies (in effect) @var{ratio} by @var{total},
1496 rounding the result to an integer. The argument @var{ratio} is not a
1497 number, but rather a pair @code{(@var{num} . @var{denom})}---typically a
1498 value returned by @code{scroll-bar-event-ratio}.
1499
1500 This function is handy for scaling a position on a scroll bar into a
1501 buffer position. Here's how to do that:
1502
1503 @example
1504 (+ (point-min)
1505 (scroll-bar-scale
1506 (posn-x-y (event-start event))
1507 (- (point-max) (point-min))))
1508 @end example
1509
1510 Recall that scroll bar events have two integers forming ratio in place
1511 of a pair of x and y coordinates.
1512 @end defun
1513
1514 @node Strings of Events
1515 @subsection Putting Keyboard Events in Strings
1516
1517 In most of the places where strings are used, we conceptualize the
1518 string as containing text characters---the same kind of characters found
1519 in buffers or files. Occasionally Lisp programs use strings that
1520 conceptually contain keyboard characters; for example, they may be key
1521 sequences or keyboard macro definitions. There are special rules for
1522 how to put keyboard characters into a string, because they are not
1523 limited to the range of 0 to 255 as text characters are.
1524
1525 A keyboard character typed using the @key{META} key is called a
1526 @dfn{meta character}. The numeric code for such an event includes the
1527 @iftex
1528 $2^{27}$
1529 @end iftex
1530 @ifinfo
1531 2**27
1532 @end ifinfo
1533 bit; it does not even come close to fitting in a string. However,
1534 earlier Emacs versions used a different representation for these
1535 characters, which gave them codes in the range of 128 to 255. That did
1536 fit in a string, and many Lisp programs contain string constants that
1537 use @samp{\M-} to express meta characters, especially as the argument to
1538 @code{define-key} and similar functions.
1539
1540 We provide backward compatibility to run those programs using special
1541 rules for how to put a keyboard character event in a string. Here are
1542 the rules:
1543
1544 @itemize @bullet
1545 @item
1546 If the keyboard character value is in the range of 0 to 127, it can go
1547 in the string unchanged.
1548
1549 @item
1550 The meta variants of those characters, with codes in the range of
1551 @iftex
1552 $2^{27}$
1553 @end iftex
1554 @ifinfo
1555 2**27
1556 @end ifinfo
1557 to
1558 @iftex
1559 $2^{27} + 127$,
1560 @end iftex
1561 @ifinfo
1562 2**27+127,
1563 @end ifinfo
1564 can also go in the string, but you must change their
1565 numeric values. You must set the
1566 @iftex
1567 $2^{7}$
1568 @end iftex
1569 @ifinfo
1570 2**7
1571 @end ifinfo
1572 bit instead of the
1573 @iftex
1574 $2^{27}$
1575 @end iftex
1576 @ifinfo
1577 2**27
1578 @end ifinfo
1579 bit,
1580 resulting in a value between 128 and 255.
1581
1582 @item
1583 Other keyboard character events cannot fit in a string. This includes
1584 keyboard events in the range of 128 to 255.
1585 @end itemize
1586
1587 Functions such as @code{read-key-sequence} that can construct strings
1588 of keyboard input characters follow these rules. They construct vectors
1589 instead of strings, when the events won't fit in a string.
1590
1591 When you use the read syntax @samp{\M-} in a string, it produces a
1592 code in the range of 128 to 255---the same code that you get if you
1593 modify the corresponding keyboard event to put it in the string. Thus,
1594 meta events in strings work consistently regardless of how they get into
1595 the strings.
1596
1597 The reason we changed the representation of meta characters as
1598 keyboard events is to make room for basic character codes beyond 127,
1599 and support meta variants of such larger character codes.
1600
1601 New programs can avoid dealing with these special compatibility rules
1602 by using vectors instead of strings for key sequences when there is any
1603 possibility that they might contain meta characters, and by using
1604 @code{listify-key-sequence} to access a string of events.
1605
1606 @defun listify-key-sequence key
1607 This function converts the string or vector @var{key} to a list of
1608 events, which you can put in @code{unread-command-events}. Converting a
1609 vector is simple, but converting a string is tricky because of the
1610 special representation used for meta characters in a string.
1611 @end defun
1612
1613 @node Reading Input
1614 @section Reading Input
1615
1616 The editor command loop reads keyboard input using the function
1617 @code{read-key-sequence}, which uses @code{read-event}. These and other
1618 functions for keyboard input are also available for use in Lisp
1619 programs. See also @code{momentary-string-display} in @ref{Temporary
1620 Displays}, and @code{sit-for} in @ref{Waiting}. @xref{Terminal Input},
1621 for functions and variables for controlling terminal input modes and
1622 debugging terminal input.
1623
1624 For higher-level input facilities, see @ref{Minibuffers}.
1625
1626 @menu
1627 * Key Sequence Input:: How to read one key sequence.
1628 * Reading One Event:: How to read just one event.
1629 * Quoted Character Input:: Asking the user to specify a character.
1630 * Event Input Misc:: How to reread or throw away input events.
1631 @end menu
1632
1633 @node Key Sequence Input
1634 @subsection Key Sequence Input
1635 @cindex key sequence input
1636
1637 The command loop reads input a key sequence at a time, by calling
1638 @code{read-key-sequence}. Lisp programs can also call this function;
1639 for example, @code{describe-key} uses it to read the key to describe.
1640
1641 @defun read-key-sequence prompt
1642 @cindex key sequence
1643 This function reads a key sequence and returns it as a string or
1644 vector. It keeps reading events until it has accumulated a full key
1645 sequence; that is, enough to specify a non-prefix command using the
1646 currently active keymaps.
1647
1648 If the events are all characters and all can fit in a string, then
1649 @code{read-key-sequence} returns a string (@pxref{Strings of Events}).
1650 Otherwise, it returns a vector, since a vector can hold all kinds of
1651 events---characters, symbols, and lists. The elements of the string or
1652 vector are the events in the key sequence.
1653
1654 The function @code{read-key-sequence} suppresses quitting: @kbd{C-g}
1655 typed while reading with this function works like any other character,
1656 and does not set @code{quit-flag}. @xref{Quitting}.
1657
1658 The argument @var{prompt} is either a string to be displayed in the echo
1659 area as a prompt, or @code{nil}, meaning not to display a prompt.
1660
1661 In the example below, the prompt @samp{?} is displayed in the echo area,
1662 and the user types @kbd{C-x C-f}.
1663
1664 @example
1665 (read-key-sequence "?")
1666
1667 @group
1668 ---------- Echo Area ----------
1669 ?@kbd{C-x C-f}
1670 ---------- Echo Area ----------
1671
1672 @result{} "^X^F"
1673 @end group
1674 @end example
1675 @end defun
1676
1677 @defvar num-input-keys
1678 @c Emacs 19 feature
1679 This variable's value is the number of key sequences processed so far in
1680 this Emacs session. This includes key sequences read from the terminal
1681 and key sequences read from keyboard macros being executed.
1682 @end defvar
1683
1684 @cindex upper case key sequence
1685 @cindex downcasing in @code{lookup-key}
1686 If an input character is an upper-case letter and has no key binding,
1687 but its lower-case equivalent has one, then @code{read-key-sequence}
1688 converts the character to lower case. Note that @code{lookup-key} does
1689 not perform case conversion in this way.
1690
1691 The function @code{read-key-sequence} also transforms some mouse events.
1692 It converts unbound drag events into click events, and discards unbound
1693 button-down events entirely. It also reshuffles focus events and
1694 miscellaneous window events so that they never appear in a key sequence
1695 with any other events.
1696
1697 When mouse events occur in special parts of a window, such as a mode
1698 line or a scroll bar, the event type shows nothing special---it is the
1699 same symbol that would normally represent that combination of mouse
1700 button and modifier keys. The information about the window part is
1701 kept elsewhere in the event---in the coordinates. But
1702 @code{read-key-sequence} translates this information into imaginary
1703 prefix keys, all of which are symbols: @code{mode-line},
1704 @code{vertical-line}, @code{horizontal-scroll-bar} and
1705 @code{vertical-scroll-bar}.
1706
1707 You can define meanings for mouse clicks in special window parts by
1708 defining key sequences using these imaginary prefix keys.
1709
1710 For example, if you call @code{read-key-sequence} and then click the
1711 mouse on the window's mode line, you get two events, like this:
1712
1713 @example
1714 (read-key-sequence "Click on the mode line: ")
1715 @result{} [mode-line
1716 (mouse-1
1717 (#<window 6 on NEWS> mode-line
1718 (40 . 63) 5959987))]
1719 @end example
1720
1721 @node Reading One Event
1722 @subsection Reading One Event
1723
1724 The lowest level functions for command input are those that read a
1725 single event.
1726
1727 @defun read-event
1728 This function reads and returns the next event of command input, waiting
1729 if necessary until an event is available. Events can come directly from
1730 the user or from a keyboard macro.
1731
1732 The function @code{read-event} does not display any message to indicate
1733 it is waiting for input; use @code{message} first, if you wish to
1734 display one. If you have not displayed a message, @code{read-event}
1735 prompts by echoing: it displays descriptions of the events that led to
1736 or were read by the current command. @xref{The Echo Area}.
1737
1738 If @code{cursor-in-echo-area} is non-@code{nil}, then @code{read-event}
1739 moves the cursor temporarily to the echo area, to the end of any message
1740 displayed there. Otherwise @code{read-event} does not move the cursor.
1741
1742 Here is what happens if you call @code{read-event} and then press the
1743 right-arrow function key:
1744
1745 @example
1746 @group
1747 (read-event)
1748 @result{} right
1749 @end group
1750 @end example
1751 @end defun
1752
1753 @defun read-char
1754 This function reads and returns a character of command input. It
1755 discards any events that are not characters, until it gets a character.
1756
1757 In the first example, the user types the character @kbd{1} (@sc{ASCII}
1758 code 49). The second example shows a keyboard macro definition that
1759 calls @code{read-char} from the minibuffer using @code{eval-expression}.
1760 @code{read-char} reads the keyboard macro's very next character, which
1761 is @kbd{1}. Then @code{eval-expression} displays its return value in
1762 the echo area.
1763
1764 @example
1765 @group
1766 (read-char)
1767 @result{} 49
1768 @end group
1769
1770 @group
1771 ;; @r{We assume here you use @kbd{M-:} to evaluate this.}
1772 (symbol-function 'foo)
1773 @result{} "^[:(read-char)^M1"
1774 @end group
1775 @group
1776 (execute-kbd-macro 'foo)
1777 @print{} 49
1778 @result{} nil
1779 @end group
1780 @end example
1781 @end defun
1782
1783 @node Quoted Character Input
1784 @subsection Quoted Character Input
1785 @cindex quoted character input
1786
1787 You can use the function @code{read-quoted-char} to ask the user to
1788 specify a character, and allow the user to specify a control or meta
1789 character conveniently, either literally or as an octal character code.
1790 The command @code{quoted-insert} uses this function.
1791
1792 @defun read-quoted-char &optional prompt
1793 @cindex octal character input
1794 @cindex control characters, reading
1795 @cindex nonprinting characters, reading
1796 This function is like @code{read-char}, except that if the first
1797 character read is an octal digit (0-7), it reads up to two more octal digits
1798 (but stopping if a non-octal digit is found) and returns the
1799 character represented by those digits in octal.
1800
1801 Quitting is suppressed when the first character is read, so that the
1802 user can enter a @kbd{C-g}. @xref{Quitting}.
1803
1804 If @var{prompt} is supplied, it specifies a string for prompting the
1805 user. The prompt string is always displayed in the echo area, followed
1806 by a single @samp{-}.
1807
1808 In the following example, the user types in the octal number 177 (which
1809 is 127 in decimal).
1810
1811 @example
1812 (read-quoted-char "What character")
1813
1814 @group
1815 ---------- Echo Area ----------
1816 What character-@kbd{177}
1817 ---------- Echo Area ----------
1818
1819 @result{} 127
1820 @end group
1821 @end example
1822 @end defun
1823
1824 @need 2000
1825 @node Event Input Misc
1826 @subsection Miscellaneous Event Input Features
1827
1828 This section describes how to ``peek ahead'' at events without using
1829 them up, how to check for pending input, and how to discard pending
1830 input.
1831
1832 @defvar unread-command-events
1833 @cindex next input
1834 @cindex peeking at input
1835 This variable holds a list of events waiting to be read as command
1836 input. The events are used in the order they appear in the list, and
1837 removed one by one as they are used.
1838
1839 The variable is needed because in some cases a function reads a event
1840 and then decides not to use it. Storing the event in this variable
1841 causes it to be processed normally, by the command loop or by the
1842 functions to read command input.
1843
1844 @cindex prefix argument unreading
1845 For example, the function that implements numeric prefix arguments reads
1846 any number of digits. When it finds a non-digit event, it must unread
1847 the event so that it can be read normally by the command loop.
1848 Likewise, incremental search uses this feature to unread events with no
1849 special meaning in a search, because these events should exit the search
1850 and then execute normally.
1851
1852 The reliable and easy way to extract events from a key sequence so as to
1853 put them in @code{unread-command-events} is to use
1854 @code{listify-key-sequence} (@pxref{Strings of Events}).
1855 @end defvar
1856
1857 @defvar unread-command-char
1858 This variable holds a character to be read as command input.
1859 A value of -1 means ``empty''.
1860
1861 This variable is mostly obsolete now that you can use
1862 @code{unread-command-events} instead; it exists only to support programs
1863 written for Emacs versions 18 and earlier.
1864 @end defvar
1865
1866 @defun input-pending-p
1867 @cindex waiting for command key input
1868 This function determines whether any command input is currently
1869 available to be read. It returns immediately, with value @code{t} if
1870 there is available input, @code{nil} otherwise. On rare occasions it
1871 may return @code{t} when no input is available.
1872 @end defun
1873
1874 @defvar last-input-event
1875 This variable records the last terminal input event read, whether
1876 as part of a command or explicitly by a Lisp program.
1877
1878 In the example below, the Lisp program reads the character @kbd{1},
1879 @sc{ASCII} code 49. It becomes the value of @code{last-input-event},
1880 while @kbd{C-e} (we assume @kbd{C-x C-e} command is used to evaluate
1881 this expression) remains the value of @code{last-command-event}.
1882
1883 @example
1884 @group
1885 (progn (print (read-char))
1886 (print last-command-event)
1887 last-input-event)
1888 @print{} 49
1889 @print{} 5
1890 @result{} 49
1891 @end group
1892 @end example
1893
1894 @vindex last-input-char
1895 The alias @code{last-input-char} exists for compatibility with
1896 Emacs version 18.
1897 @end defvar
1898
1899 @defun discard-input
1900 @cindex flush input
1901 @cindex discard input
1902 @cindex terminate keyboard macro
1903 This function discards the contents of the terminal input buffer and
1904 cancels any keyboard macro that might be in the process of definition.
1905 It returns @code{nil}.
1906
1907 In the following example, the user may type a number of characters right
1908 after starting the evaluation of the form. After the @code{sleep-for}
1909 finishes sleeping, @code{discard-input} discards any characters typed
1910 during the sleep.
1911
1912 @example
1913 (progn (sleep-for 2)
1914 (discard-input))
1915 @result{} nil
1916 @end example
1917 @end defun
1918
1919 @node Waiting
1920 @section Waiting for Elapsed Time or Input
1921 @cindex pausing
1922 @cindex waiting
1923
1924 The wait functions are designed to wait for a certain amount of time
1925 to pass or until there is input. For example, you may wish to pause in
1926 the middle of a computation to allow the user time to view the display.
1927 @code{sit-for} pauses and updates the screen, and returns immediately if
1928 input comes in, while @code{sleep-for} pauses without updating the
1929 screen.
1930
1931 @defun sit-for seconds &optional millisec nodisp
1932 This function performs redisplay (provided there is no pending input
1933 from the user), then waits @var{seconds} seconds, or until input is
1934 available. The value is @code{t} if @code{sit-for} waited the full
1935 time with no input arriving (see @code{input-pending-p} in @ref{Event
1936 Input Misc}). Otherwise, the value is @code{nil}.
1937
1938 The argument @var{seconds} need not be an integer. If it is a floating
1939 point number, @code{sit-for} waits for a fractional number of seconds.
1940 Some systems support only a whole number of seconds; on these systems,
1941 @var{seconds} is rounded down.
1942
1943 The optional argument @var{millisec} specifies an additional waiting
1944 period measured in milliseconds. This adds to the period specified by
1945 @var{seconds}. If the system doesn't support waiting fractions of a
1946 second, you get an error if you specify nonzero @var{millisec}.
1947
1948 @cindex forcing redisplay
1949 Redisplay is always preempted if input arrives, and does not happen at
1950 all if input is available before it starts. Thus, there is no way to
1951 force screen updating if there is pending input; however, if there is no
1952 input pending, you can force an update with no delay by using
1953 @code{(sit-for 0)}.
1954
1955 If @var{nodisp} is non-@code{nil}, then @code{sit-for} does not
1956 redisplay, but it still returns as soon as input is available (or when
1957 the timeout elapses).
1958
1959 Iconifying or deiconifying a frame makes @code{sit-for} return, because
1960 that generates an event. @xref{Misc Events}.
1961
1962 The usual purpose of @code{sit-for} is to give the user time to read
1963 text that you display.
1964 @end defun
1965
1966 @defun sleep-for seconds &optional millisec
1967 This function simply pauses for @var{seconds} seconds without updating
1968 the display. It pays no attention to available input. It returns
1969 @code{nil}.
1970
1971 The argument @var{seconds} need not be an integer. If it is a floating
1972 point number, @code{sleep-for} waits for a fractional number of seconds.
1973 Some systems support only a whole number of seconds; on these systems,
1974 @var{seconds} is rounded down.
1975
1976 The optional argument @var{millisec} specifies an additional waiting
1977 period measured in milliseconds. This adds to the period specified by
1978 @var{seconds}. If the system doesn't support waiting fractions of a
1979 second, you get an error if you specify nonzero @var{millisec}.
1980
1981 Use @code{sleep-for} when you wish to guarantee a delay.
1982 @end defun
1983
1984 @xref{Time of Day}, for functions to get the current time.
1985
1986 @node Quitting
1987 @section Quitting
1988 @cindex @kbd{C-g}
1989 @cindex quitting
1990
1991 Typing @kbd{C-g} while a Lisp function is running causes Emacs to
1992 @dfn{quit} whatever it is doing. This means that control returns to the
1993 innermost active command loop.
1994
1995 Typing @kbd{C-g} while the command loop is waiting for keyboard input
1996 does not cause a quit; it acts as an ordinary input character. In the
1997 simplest case, you cannot tell the difference, because @kbd{C-g}
1998 normally runs the command @code{keyboard-quit}, whose effect is to quit.
1999 However, when @kbd{C-g} follows a prefix key, the result is an undefined
2000 key. The effect is to cancel the prefix key as well as any prefix
2001 argument.
2002
2003 In the minibuffer, @kbd{C-g} has a different definition: it aborts out
2004 of the minibuffer. This means, in effect, that it exits the minibuffer
2005 and then quits. (Simply quitting would return to the command loop
2006 @emph{within} the minibuffer.) The reason why @kbd{C-g} does not quit
2007 directly when the command reader is reading input is so that its meaning
2008 can be redefined in the minibuffer in this way. @kbd{C-g} following a
2009 prefix key is not redefined in the minibuffer, and it has its normal
2010 effect of canceling the prefix key and prefix argument. This too
2011 would not be possible if @kbd{C-g} always quit directly.
2012
2013 When @kbd{C-g} does directly quit, it does so by setting the variable
2014 @code{quit-flag} to @code{t}. Emacs checks this variable at appropriate
2015 times and quits if it is not @code{nil}. Setting @code{quit-flag}
2016 non-@code{nil} in any way thus causes a quit.
2017
2018 At the level of C code, quitting cannot happen just anywhere; only at the
2019 special places that check @code{quit-flag}. The reason for this is
2020 that quitting at other places might leave an inconsistency in Emacs's
2021 internal state. Because quitting is delayed until a safe place, quitting
2022 cannot make Emacs crash.
2023
2024 Certain functions such as @code{read-key-sequence} or
2025 @code{read-quoted-char} prevent quitting entirely even though they wait
2026 for input. Instead of quitting, @kbd{C-g} serves as the requested
2027 input. In the case of @code{read-key-sequence}, this serves to bring
2028 about the special behavior of @kbd{C-g} in the command loop. In the
2029 case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used
2030 to quote a @kbd{C-g}.
2031
2032 You can prevent quitting for a portion of a Lisp function by binding
2033 the variable @code{inhibit-quit} to a non-@code{nil} value. Then,
2034 although @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, the
2035 usual result of this---a quit---is prevented. Eventually,
2036 @code{inhibit-quit} will become @code{nil} again, such as when its
2037 binding is unwound at the end of a @code{let} form. At that time, if
2038 @code{quit-flag} is still non-@code{nil}, the requested quit happens
2039 immediately. This behavior is ideal when you wish to make sure that
2040 quitting does not happen within a ``critical section'' of the program.
2041
2042 @cindex @code{read-quoted-char} quitting
2043 In some functions (such as @code{read-quoted-char}), @kbd{C-g} is
2044 handled in a special way that does not involve quitting. This is done
2045 by reading the input with @code{inhibit-quit} bound to @code{t}, and
2046 setting @code{quit-flag} to @code{nil} before @code{inhibit-quit}
2047 becomes @code{nil} again. This excerpt from the definition of
2048 @code{read-quoted-char} shows how this is done; it also shows that
2049 normal quitting is permitted after the first character of input.
2050
2051 @example
2052 (defun read-quoted-char (&optional prompt)
2053 "@dots{}@var{documentation}@dots{}"
2054 (let ((count 0) (code 0) char)
2055 (while (< count 3)
2056 (let ((inhibit-quit (zerop count))
2057 (help-form nil))
2058 (and prompt (message "%s-" prompt))
2059 (setq char (read-char))
2060 (if inhibit-quit (setq quit-flag nil)))
2061 @dots{})
2062 (logand 255 code)))
2063 @end example
2064
2065 @defvar quit-flag
2066 If this variable is non-@code{nil}, then Emacs quits immediately, unless
2067 @code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} ordinarily sets
2068 @code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}.
2069 @end defvar
2070
2071 @defvar inhibit-quit
2072 This variable determines whether Emacs should quit when @code{quit-flag}
2073 is set to a value other than @code{nil}. If @code{inhibit-quit} is
2074 non-@code{nil}, then @code{quit-flag} has no special effect.
2075 @end defvar
2076
2077 @deffn Command keyboard-quit
2078 This function signals the @code{quit} condition with @code{(signal 'quit
2079 nil)}. This is the same thing that quitting does. (See @code{signal}
2080 in @ref{Errors}.)
2081 @end deffn
2082
2083 You can specify a character other than @kbd{C-g} to use for quitting.
2084 See the function @code{set-input-mode} in @ref{Terminal Input}.
2085
2086 @node Prefix Command Arguments
2087 @section Prefix Command Arguments
2088 @cindex prefix argument
2089 @cindex raw prefix argument
2090 @cindex numeric prefix argument
2091
2092 Most Emacs commands can use a @dfn{prefix argument}, a number
2093 specified before the command itself. (Don't confuse prefix arguments
2094 with prefix keys.) The prefix argument is at all times represented by a
2095 value, which may be @code{nil}, meaning there is currently no prefix
2096 argument. Each command may use the prefix argument or ignore it.
2097
2098 There are two representations of the prefix argument: @dfn{raw} and
2099 @dfn{numeric}. The editor command loop uses the raw representation
2100 internally, and so do the Lisp variables that store the information, but
2101 commands can request either representation.
2102
2103 Here are the possible values of a raw prefix argument:
2104
2105 @itemize @bullet
2106 @item
2107 @code{nil}, meaning there is no prefix argument. Its numeric value is
2108 1, but numerous commands make a distinction between @code{nil} and the
2109 integer 1.
2110
2111 @item
2112 An integer, which stands for itself.
2113
2114 @item
2115 A list of one element, which is an integer. This form of prefix
2116 argument results from one or a succession of @kbd{C-u}'s with no
2117 digits. The numeric value is the integer in the list, but some
2118 commands make a distinction between such a list and an integer alone.
2119
2120 @item
2121 The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was
2122 typed, without following digits. The equivalent numeric value is
2123 @minus{}1, but some commands make a distinction between the integer
2124 @minus{}1 and the symbol @code{-}.
2125 @end itemize
2126
2127 We illustrate these possibilities by calling the following function with
2128 various prefixes:
2129
2130 @example
2131 @group
2132 (defun display-prefix (arg)
2133 "Display the value of the raw prefix arg."
2134 (interactive "P")
2135 (message "%s" arg))
2136 @end group
2137 @end example
2138
2139 @noindent
2140 Here are the results of calling @code{display-prefix} with various
2141 raw prefix arguments:
2142
2143 @example
2144 M-x display-prefix @print{} nil
2145
2146 C-u M-x display-prefix @print{} (4)
2147
2148 C-u C-u M-x display-prefix @print{} (16)
2149
2150 C-u 3 M-x display-prefix @print{} 3
2151
2152 M-3 M-x display-prefix @print{} 3 ; @r{(Same as @code{C-u 3}.)}
2153
2154 C-u - M-x display-prefix @print{} -
2155
2156 M-- M-x display-prefix @print{} - ; @r{(Same as @code{C-u -}.)}
2157
2158 C-u - 7 M-x display-prefix @print{} -7
2159
2160 M-- 7 M-x display-prefix @print{} -7 ; @r{(Same as @code{C-u -7}.)}
2161 @end example
2162
2163 Emacs uses two variables to store the prefix argument:
2164 @code{prefix-arg} and @code{current-prefix-arg}. Commands such as
2165 @code{universal-argument} that set up prefix arguments for other
2166 commands store them in @code{prefix-arg}. In contrast,
2167 @code{current-prefix-arg} conveys the prefix argument to the current
2168 command, so setting it has no effect on the prefix arguments for future
2169 commands.
2170
2171 Normally, commands specify which representation to use for the prefix
2172 argument, either numeric or raw, in the @code{interactive} declaration.
2173 (@xref{Using Interactive}.) Alternatively, functions may look at the
2174 value of the prefix argument directly in the variable
2175 @code{current-prefix-arg}, but this is less clean.
2176
2177 @defun prefix-numeric-value arg
2178 This function returns the numeric meaning of a valid raw prefix argument
2179 value, @var{arg}. The argument may be a symbol, a number, or a list.
2180 If it is @code{nil}, the value 1 is returned; if it is @code{-}, the
2181 value @minus{}1 is returned; if it is a number, that number is returned;
2182 if it is a list, the @sc{car} of that list (which should be a number) is
2183 returned.
2184 @end defun
2185
2186 @defvar current-prefix-arg
2187 This variable holds the raw prefix argument for the @emph{current}
2188 command. Commands may examine it directly, but the usual way to access
2189 it is with @code{(interactive "P")}.
2190 @end defvar
2191
2192 @defvar prefix-arg
2193 The value of this variable is the raw prefix argument for the
2194 @emph{next} editing command. Commands that specify prefix arguments for
2195 the following command work by setting this variable.
2196 @end defvar
2197
2198 Do not call the functions @code{universal-argument},
2199 @code{digit-argument}, or @code{negative-argument} unless you intend to
2200 let the user enter the prefix argument for the @emph{next} command.
2201
2202 @deffn Command universal-argument
2203 This command reads input and specifies a prefix argument for the
2204 following command. Don't call this command yourself unless you know
2205 what you are doing.
2206 @end deffn
2207
2208 @deffn Command digit-argument arg
2209 This command adds to the prefix argument for the following command. The
2210 argument @var{arg} is the raw prefix argument as it was before this
2211 command; it is used to compute the updated prefix argument. Don't call
2212 this command yourself unless you know what you are doing.
2213 @end deffn
2214
2215 @deffn Command negative-argument arg
2216 This command adds to the numeric argument for the next command. The
2217 argument @var{arg} is the raw prefix argument as it was before this
2218 command; its value is negated to form the new prefix argument. Don't
2219 call this command yourself unless you know what you are doing.
2220 @end deffn
2221
2222 @node Recursive Editing
2223 @section Recursive Editing
2224 @cindex recursive command loop
2225 @cindex recursive editing level
2226 @cindex command loop, recursive
2227
2228 The Emacs command loop is entered automatically when Emacs starts up.
2229 This top-level invocation of the command loop never exits; it keeps
2230 running as long as Emacs does. Lisp programs can also invoke the
2231 command loop. Since this makes more than one activation of the command
2232 loop, we call it @dfn{recursive editing}. A recursive editing level has
2233 the effect of suspending whatever command invoked it and permitting the
2234 user to do arbitrary editing before resuming that command.
2235
2236 The commands available during recursive editing are the same ones
2237 available in the top-level editing loop and defined in the keymaps.
2238 Only a few special commands exit the recursive editing level; the others
2239 return to the recursive editing level when they finish. (The special
2240 commands for exiting are always available, but they do nothing when
2241 recursive editing is not in progress.)
2242
2243 All command loops, including recursive ones, set up all-purpose error
2244 handlers so that an error in a command run from the command loop will
2245 not exit the loop.
2246
2247 @cindex minibuffer input
2248 Minibuffer input is a special kind of recursive editing. It has a few
2249 special wrinkles, such as enabling display of the minibuffer and the
2250 minibuffer window, but fewer than you might suppose. Certain keys
2251 behave differently in the minibuffer, but that is only because of the
2252 minibuffer's local map; if you switch windows, you get the usual Emacs
2253 commands.
2254
2255 @cindex @code{throw} example
2256 @kindex exit
2257 @cindex exit recursive editing
2258 @cindex aborting
2259 To invoke a recursive editing level, call the function
2260 @code{recursive-edit}. This function contains the command loop; it also
2261 contains a call to @code{catch} with tag @code{exit}, which makes it
2262 possible to exit the recursive editing level by throwing to @code{exit}
2263 (@pxref{Catch and Throw}). If you throw a value other than @code{t},
2264 then @code{recursive-edit} returns normally to the function that called
2265 it. The command @kbd{C-M-c} (@code{exit-recursive-edit}) does this.
2266 Throwing a @code{t} value causes @code{recursive-edit} to quit, so that
2267 control returns to the command loop one level up. This is called
2268 @dfn{aborting}, and is done by @kbd{C-]} (@code{abort-recursive-edit}).
2269
2270 Most applications should not use recursive editing, except as part of
2271 using the minibuffer. Usually it is more convenient for the user if you
2272 change the major mode of the current buffer temporarily to a special
2273 major mode, which should have a command to go back to the previous mode.
2274 (The @kbd{e} command in Rmail uses this technique.) Or, if you wish to
2275 give the user different text to edit ``recursively'', create and select
2276 a new buffer in a special mode. In this mode, define a command to
2277 complete the processing and go back to the previous buffer. (The
2278 @kbd{m} command in Rmail does this.)
2279
2280 Recursive edits are useful in debugging. You can insert a call to
2281 @code{debug} into a function definition as a sort of breakpoint, so that
2282 you can look around when the function gets there. @code{debug} invokes
2283 a recursive edit but also provides the other features of the debugger.
2284
2285 Recursive editing levels are also used when you type @kbd{C-r} in
2286 @code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}).
2287
2288 @defun recursive-edit
2289 @cindex suspend evaluation
2290 This function invokes the editor command loop. It is called
2291 automatically by the initialization of Emacs, to let the user begin
2292 editing. When called from a Lisp program, it enters a recursive editing
2293 level.
2294
2295 In the following example, the function @code{simple-rec} first
2296 advances point one word, then enters a recursive edit, printing out a
2297 message in the echo area. The user can then do any editing desired, and
2298 then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}.
2299
2300 @example
2301 (defun simple-rec ()
2302 (forward-word 1)
2303 (message "Recursive edit in progress")
2304 (recursive-edit)
2305 (forward-word 1))
2306 @result{} simple-rec
2307 (simple-rec)
2308 @result{} nil
2309 @end example
2310 @end defun
2311
2312 @deffn Command exit-recursive-edit
2313 This function exits from the innermost recursive edit (including
2314 minibuffer input). Its definition is effectively @code{(throw 'exit
2315 nil)}.
2316 @end deffn
2317
2318 @deffn Command abort-recursive-edit
2319 This function aborts the command that requested the innermost recursive
2320 edit (including minibuffer input), by signaling @code{quit}
2321 after exiting the recursive edit. Its definition is effectively
2322 @code{(throw 'exit t)}. @xref{Quitting}.
2323 @end deffn
2324
2325 @deffn Command top-level
2326 This function exits all recursive editing levels; it does not return a
2327 value, as it jumps completely out of any computation directly back to
2328 the main command loop.
2329 @end deffn
2330
2331 @defun recursion-depth
2332 This function returns the current depth of recursive edits. When no
2333 recursive edit is active, it returns 0.
2334 @end defun
2335
2336 @node Disabling Commands
2337 @section Disabling Commands
2338 @cindex disabled command
2339
2340 @dfn{Disabling a command} marks the command as requiring user
2341 confirmation before it can be executed. Disabling is used for commands
2342 which might be confusing to beginning users, to prevent them from using
2343 the commands by accident.
2344
2345 @kindex disabled
2346 The low-level mechanism for disabling a command is to put a
2347 non-@code{nil} @code{disabled} property on the Lisp symbol for the
2348 command. These properties are normally set up by the user's
2349 @file{.emacs} file with Lisp expressions such as this:
2350
2351 @example
2352 (put 'upcase-region 'disabled t)
2353 @end example
2354
2355 @noindent
2356 For a few commands, these properties are present by default and may be
2357 removed by the @file{.emacs} file.
2358
2359 If the value of the @code{disabled} property is a string, the message
2360 saying the command is disabled includes that string. For example:
2361
2362 @example
2363 (put 'delete-region 'disabled
2364 "Text deleted this way cannot be yanked back!\n")
2365 @end example
2366
2367 @xref{Disabling,,, emacs, The GNU Emacs Manual}, for the details on
2368 what happens when a disabled command is invoked interactively.
2369 Disabling a command has no effect on calling it as a function from Lisp
2370 programs.
2371
2372 @deffn Command enable-command command
2373 Allow @var{command} to be executed without special confirmation from now
2374 on, and (if the user confirms) alter the user's @file{.emacs} file so
2375 that this will apply to future sessions.
2376 @end deffn
2377
2378 @deffn Command disable-command command
2379 Require special confirmation to execute @var{command} from now on, and
2380 (if the user confirms) alter the user's @file{.emacs} file so that this
2381 will apply to future sessions.
2382 @end deffn
2383
2384 @defvar disabled-command-hook
2385 This normal hook is run instead of a disabled command, when the user
2386 invokes the disabled command interactively. The hook functions can use
2387 @code{this-command-keys} to determine what the user typed to run the
2388 command, and thus find the command itself. @xref{Hooks}.
2389
2390 By default, @code{disabled-command-hook} contains a function that asks
2391 the user whether to proceed.
2392 @end defvar
2393
2394 @node Command History
2395 @section Command History
2396 @cindex command history
2397 @cindex complex command
2398 @cindex history of commands
2399
2400 The command loop keeps a history of the complex commands that have
2401 been executed, to make it convenient to repeat these commands. A
2402 @dfn{complex command} is one for which the interactive argument reading
2403 uses the minibuffer. This includes any @kbd{M-x} command, any
2404 @kbd{M-:} command, and any command whose @code{interactive}
2405 specification reads an argument from the minibuffer. Explicit use of
2406 the minibuffer during the execution of the command itself does not cause
2407 the command to be considered complex.
2408
2409 @defvar command-history
2410 This variable's value is a list of recent complex commands, each
2411 represented as a form to evaluate. It continues to accumulate all
2412 complex commands for the duration of the editing session, but all but
2413 the first (most recent) thirty elements are deleted when a garbage
2414 collection takes place (@pxref{Garbage Collection}).
2415
2416 @example
2417 @group
2418 command-history
2419 @result{} ((switch-to-buffer "chistory.texi")
2420 (describe-key "^X^[")
2421 (visit-tags-table "~/emacs/src/")
2422 (find-tag "repeat-complex-command"))
2423 @end group
2424 @end example
2425 @end defvar
2426
2427 This history list is actually a special case of minibuffer history
2428 (@pxref{Minibuffer History}), with one special twist: the elements are
2429 expressions rather than strings.
2430
2431 There are a number of commands devoted to the editing and recall of
2432 previous commands. The commands @code{repeat-complex-command}, and
2433 @code{list-command-history} are described in the user manual
2434 (@pxref{Repetition,,, emacs, The GNU Emacs Manual}). Within the
2435 minibuffer, the history commands used are the same ones available in any
2436 minibuffer.
2437
2438 @node Keyboard Macros
2439 @section Keyboard Macros
2440 @cindex keyboard macros
2441
2442 A @dfn{keyboard macro} is a canned sequence of input events that can
2443 be considered a command and made the definition of a key. The Lisp
2444 representation of a keyboard macro is a string or vector containing the
2445 events. Don't confuse keyboard macros with Lisp macros
2446 (@pxref{Macros}).
2447
2448 @defun execute-kbd-macro macro &optional count
2449 This function executes @var{macro} as a sequence of events. If
2450 @var{macro} is a string or vector, then the events in it are executed
2451 exactly as if they had been input by the user. The sequence is
2452 @emph{not} expected to be a single key sequence; normally a keyboard
2453 macro definition consists of several key sequences concatenated.
2454
2455 If @var{macro} is a symbol, then its function definition is used in
2456 place of @var{macro}. If that is another symbol, this process repeats.
2457 Eventually the result should be a string or vector. If the result is
2458 not a symbol, string, or vector, an error is signaled.
2459
2460 The argument @var{count} is a repeat count; @var{macro} is executed that
2461 many times. If @var{count} is omitted or @code{nil}, @var{macro} is
2462 executed once. If it is 0, @var{macro} is executed over and over until it
2463 encounters an error or a failing search.
2464 @end defun
2465
2466 @defvar executing-macro
2467 This variable contains the string or vector that defines the keyboard
2468 macro that is currently executing. It is @code{nil} if no macro is
2469 currently executing. A command can test this variable to behave
2470 differently when run from an executing macro. Do not set this variable
2471 yourself.
2472 @end defvar
2473
2474 @defvar defining-kbd-macro
2475 This variable indicates whether a keyboard macro is being defined. A
2476 command can test this variable to behave differently while a macro is
2477 being defined. The commands @code{start-kbd-macro} and
2478 @code{end-kbd-macro} set this variable---do not set it yourself.
2479
2480 The variable is always local to the current terminal and cannot be
2481 buffer-local. @xref{Multiple Displays}.
2482 @end defvar
2483
2484 @defvar last-kbd-macro
2485 This variable is the definition of the most recently defined keyboard
2486 macro. Its value is a string or vector, or @code{nil}.
2487
2488 The variable is always local to the current terminal and cannot be
2489 buffer-local. @xref{Multiple Displays}.
2490 @end defvar
2491