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