1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-30 08:09:04 +00:00
emacs/lispref/commands.texi

2819 lines
104 KiB
Plaintext
Raw Normal View History

1994-03-08 22:47:20 +00:00
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
2000-01-05 14:59:32 +00:00
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
2003-02-04 14:56:31 +00:00
@c Free Software Foundation, Inc.
1994-03-08 22:47:20 +00:00
@c See the file elisp.texi for copying conditions.
@setfilename ../info/commands
@node Command Loop, Keymaps, Minibuffers, Top
@chapter Command Loop
@cindex editor command loop
@cindex command loop
When you run Emacs, it enters the @dfn{editor command loop} almost
immediately. This loop reads key sequences, executes their definitions,
and displays the results. In this chapter, we describe how these things
2003-02-04 14:56:31 +00:00
are done, and the subroutines that allow Lisp programs to do them.
1994-03-08 22:47:20 +00:00
@menu
* Command Overview:: How the command loop reads commands.
* Defining Commands:: Specifying how a function should read arguments.
* Interactive Call:: Calling a command, so that it will read arguments.
* Command Loop Info:: Variables set by the command loop for you to examine.
1999-12-03 19:11:12 +00:00
* Adjusting Point:: Adjustment of point after a command.
1994-03-08 22:47:20 +00:00
* Input Events:: What input looks like when you read it.
* Reading Input:: How to read input events from the keyboard or mouse.
1998-02-28 01:53:53 +00:00
* Special Events:: Events processed immediately and individually.
1994-03-08 22:47:20 +00:00
* Waiting:: Waiting for user input or elapsed time.
* Quitting:: How @kbd{C-g} works. How to catch or defer quitting.
* Prefix Command Arguments:: How the commands to set prefix args work.
* Recursive Editing:: Entering a recursive edit,
and why you usually shouldn't.
* Disabling Commands:: How the command loop handles disabled commands.
* Command History:: How the command history is set up, and how accessed.
* Keyboard Macros:: How keyboard macros are implemented.
@end menu
@node Command Overview
@section Command Loop Overview
The first thing the command loop must do is read a key sequence, which
is a sequence of events that translates into a command. It does this by
calling the function @code{read-key-sequence}. Your Lisp code can also
call this function (@pxref{Key Sequence Input}). Lisp programs can also
do input at a lower level with @code{read-event} (@pxref{Reading One
Event}) or discard pending input with @code{discard-input}
1994-03-28 20:02:03 +00:00
(@pxref{Event Input Misc}).
1994-03-08 22:47:20 +00:00
The key sequence is translated into a command through the currently
active keymaps. @xref{Key Lookup}, for information on how this is done.
The result should be a keyboard macro or an interactively callable
function. If the key is @kbd{M-x}, then it reads the name of another
1994-03-28 20:02:03 +00:00
command, which it then calls. This is done by the command
1994-03-08 22:47:20 +00:00
@code{execute-extended-command} (@pxref{Interactive Call}).
1994-03-28 20:02:03 +00:00
To execute a command requires first reading the arguments for it.
This is done by calling @code{command-execute} (@pxref{Interactive
Call}). For commands written in Lisp, the @code{interactive}
specification says how to read the arguments. This may use the prefix
argument (@pxref{Prefix Command Arguments}) or may read with prompting
in the minibuffer (@pxref{Minibuffers}). For example, the command
@code{find-file} has an @code{interactive} specification which says to
read a file name using the minibuffer. The command's function body does
not use the minibuffer; if you call this command from Lisp code as a
function, you must supply the file name string as an ordinary Lisp
function argument.
1994-03-08 22:47:20 +00:00
If the command is a string or vector (i.e., a keyboard macro) then
@code{execute-kbd-macro} is used to execute it. You can call this
function yourself (@pxref{Keyboard Macros}).
1994-03-28 20:02:03 +00:00
To terminate the execution of a running command, type @kbd{C-g}. This
character causes @dfn{quitting} (@pxref{Quitting}).
1994-03-08 22:47:20 +00:00
@defvar pre-command-hook
1994-03-28 20:02:03 +00:00
The editor command loop runs this normal hook before each command. At
that time, @code{this-command} contains the command that is about to
run, and @code{last-command} describes the previous command.
1994-05-28 15:27:52 +00:00
@xref{Hooks}.
1994-03-08 22:47:20 +00:00
@end defvar
@defvar post-command-hook
1994-03-28 20:02:03 +00:00
The editor command loop runs this normal hook after each command
(including commands terminated prematurely by quitting or by errors),
and also when the command loop is first entered. At that time,
@code{this-command} describes the command that just ran, and
1994-05-28 15:27:52 +00:00
@code{last-command} describes the command before that. @xref{Hooks}.
1994-03-08 22:47:20 +00:00
@end defvar
1994-05-28 15:27:52 +00:00
Quitting is suppressed while running @code{pre-command-hook} and
1995-06-06 19:21:15 +00:00
@code{post-command-hook}. If an error happens while executing one of
1998-02-28 01:53:53 +00:00
these hooks, it terminates execution of the hook, and clears the hook
variable to @code{nil} so as to prevent an infinite loop of errors.
1994-05-28 15:27:52 +00:00
1994-03-08 22:47:20 +00:00
@node Defining Commands
@section Defining Commands
@cindex defining commands
@cindex commands, defining
@cindex functions, making them interactive
@cindex interactive function
A Lisp function becomes a command when its body contains, at top
1994-05-28 15:27:52 +00:00
level, a form that calls the special form @code{interactive}. This
1994-03-08 22:47:20 +00:00
form does nothing when actually executed, but its presence serves as a
flag to indicate that interactive calling is permitted. Its argument
controls the reading of arguments for an interactive call.
@menu
* Using Interactive:: General rules for @code{interactive}.
* Interactive Codes:: The standard letter-codes for reading arguments
in various ways.
* Interactive Examples:: Examples of how to read interactive arguments.
@end menu
@node Using Interactive
@subsection Using @code{interactive}
This section describes how to write the @code{interactive} form that
makes a Lisp function an interactively-callable command, and how to
2001-09-10 05:34:15 +00:00
examine a commands's @code{interactive} form.
1994-03-08 22:47:20 +00:00
@defspec interactive arg-descriptor
@cindex argument descriptors
This special form declares that the function in which it appears is a
command, and that it may therefore be called interactively (via
@kbd{M-x} or by entering a key sequence bound to it). The argument
1994-03-28 20:02:03 +00:00
@var{arg-descriptor} declares how to compute the arguments to the
command when the command is called interactively.
1994-03-08 22:47:20 +00:00
A command may be called from Lisp programs like any other function, but
1994-03-28 20:02:03 +00:00
then the caller supplies the arguments and @var{arg-descriptor} has no
effect.
1994-03-08 22:47:20 +00:00
The @code{interactive} form has its effect because the command loop
(actually, its subroutine @code{call-interactively}) scans through the
function definition looking for it, before calling the function. Once
the function is called, all its body forms including the
@code{interactive} form are executed, but at this time
@code{interactive} simply returns @code{nil} without even evaluating its
argument.
@end defspec
There are three possibilities for the argument @var{arg-descriptor}:
@itemize @bullet
@item
It may be omitted or @code{nil}; then the command is called with no
arguments. This leads quickly to an error if the command requires one
or more arguments.
@item
It may be a Lisp expression that is not a string; then it should be a
form that is evaluated to get a list of arguments to pass to the
command.
@cindex argument evaluation form
If this expression reads keyboard input (this includes using the
minibuffer), keep in mind that the integer value of point or the mark
before reading input may be incorrect after reading input. This is
because the current buffer may be receiving subprocess output;
if subprocess output arrives while the command is waiting for input,
it could relocate point and the mark.
Here's an example of what @emph{not} to do:
@smallexample
(interactive
(list (region-beginning) (region-end)
(read-string "Foo: " nil 'my-history)))
@end smallexample
@noindent
Here's how to avoid the problem, by examining point and the mark only
after reading the keyboard input:
@smallexample
(interactive
(let ((string (read-string "Foo: " nil 'my-history)))
(list (region-beginning) (region-end) string)))
@end smallexample
1994-03-08 22:47:20 +00:00
@item
@cindex argument prompt
It may be a string; then its contents should consist of a code character
followed by a prompt (which some code characters use and some ignore).
The prompt ends either with the end of the string or with a newline.
Here is a simple example:
@smallexample
(interactive "bFrobnicate buffer: ")
@end smallexample
@noindent
The code letter @samp{b} says to read the name of an existing buffer,
with completion. The buffer name is the sole argument passed to the
command. The rest of the string is a prompt.
If there is a newline character in the string, it terminates the prompt.
If the string does not end there, then the rest of the string should
contain another code character and prompt, specifying another argument.
You can specify any number of arguments in this way.
@c Emacs 19 feature
The prompt string can use @samp{%} to include previous argument values
1994-05-28 15:27:52 +00:00
(starting with the first argument) in the prompt. This is done using
@code{format} (@pxref{Formatting Strings}). For example, here is how
you could read the name of an existing buffer followed by a new name to
give to that buffer:
1994-03-08 22:47:20 +00:00
@smallexample
@group
(interactive "bBuffer to rename: \nsRename buffer %s to: ")
@end group
@end smallexample
2001-09-10 05:34:15 +00:00
@cindex @samp{*} in @code{interactive}
1994-03-28 20:02:03 +00:00
@cindex read-only buffers in interactive
1994-03-08 22:47:20 +00:00
If the first character in the string is @samp{*}, then an error is
signaled if the buffer is read-only.
2001-09-10 05:34:15 +00:00
@cindex @samp{@@} in @code{interactive}
1994-03-08 22:47:20 +00:00
@c Emacs 19 feature
If the first character in the string is @samp{@@}, and if the key
sequence used to invoke the command includes any mouse events, then
the window associated with the first of those events is selected
before the command is run.
You can use @samp{*} and @samp{@@} together; the order does not matter.
Actual reading of arguments is controlled by the rest of the prompt
string (starting with the first character that is not @samp{*} or
@samp{@@}).
@end itemize
2001-09-10 05:34:15 +00:00
@cindex examining the @code{interactive} form
@defun interactive-form function
2001-09-10 05:34:15 +00:00
This function returns the @code{interactive} form of @var{function}. If
@var{function} is a command (@pxref{Interactive Call}), the value is a
list of the form @code{(interactive @var{spec})}, where @var{spec} is
the descriptor specification used by the command's @code{interactive}
form to compute the function's arguments (@pxref{Using Interactive}).
If @var{function} is not a command, @code{interactive-form} returns
@code{nil}.
@end defun
1994-03-08 22:47:20 +00:00
@node Interactive Codes
@comment node-name, next, previous, up
@subsection Code Characters for @code{interactive}
@cindex interactive code description
@cindex description for interactive codes
@cindex codes, interactive, description of
@cindex characters for interactive codes
The code character descriptions below contain a number of key words,
defined here as follows:
@table @b
@item Completion
@cindex interactive completion
Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name
completion because the argument is read using @code{completing-read}
(@pxref{Completion}). @kbd{?} displays a list of possible completions.
@item Existing
Require the name of an existing object. An invalid name is not
accepted; the commands to exit the minibuffer do not exit if the current
input is not valid.
@item Default
@cindex default argument string
A default value of some sort is used if the user enters no text in the
minibuffer. The default depends on the code character.
@item No I/O
This code letter computes an argument without reading any input.
Therefore, it does not use a prompt string, and any prompt string you
supply is ignored.
1994-03-28 20:02:03 +00:00
Even though the code letter doesn't use a prompt string, you must follow
it with a newline if it is not the last code character in the string.
1994-03-08 22:47:20 +00:00
@item Prompt
A prompt immediately follows the code character. The prompt ends either
with the end of the string or with a newline.
@item Special
This code character is meaningful only at the beginning of the
interactive string, and it does not look for a prompt or a newline.
It is a single, isolated character.
@end table
@cindex reading interactive arguments
Here are the code character descriptions for use with @code{interactive}:
@table @samp
@item *
Signal an error if the current buffer is read-only. Special.
@item @@
Select the window mentioned in the first mouse event in the key
sequence that invoked this command. Special.
@item a
1994-05-28 15:27:52 +00:00
A function name (i.e., a symbol satisfying @code{fboundp}). Existing,
1994-03-08 22:47:20 +00:00
Completion, Prompt.
@item b
The name of an existing buffer. By default, uses the name of the
current buffer (@pxref{Buffers}). Existing, Completion, Default,
Prompt.
@item B
A buffer name. The buffer need not exist. By default, uses the name of
a recently used buffer other than the current buffer. Completion,
1994-05-28 15:27:52 +00:00
Default, Prompt.
1994-03-08 22:47:20 +00:00
@item c
A character. The cursor does not move into the echo area. Prompt.
@item C
A command name (i.e., a symbol satisfying @code{commandp}). Existing,
Completion, Prompt.
@item d
@cindex position argument
1994-03-28 20:02:03 +00:00
The position of point, as an integer (@pxref{Point}). No I/O.
1994-03-08 22:47:20 +00:00
@item D
A directory name. The default is the current default directory of the
current buffer, @code{default-directory} (@pxref{System Environment}).
Existing, Completion, Default, Prompt.
@item e
The first or next mouse event in the key sequence that invoked the command.
1994-05-28 15:27:52 +00:00
More precisely, @samp{e} gets events that are lists, so you can look at
1994-03-08 22:47:20 +00:00
the data in the lists. @xref{Input Events}. No I/O.
You can use @samp{e} more than once in a single command's interactive
1994-05-28 15:27:52 +00:00
specification. If the key sequence that invoked the command has
1994-03-28 20:02:03 +00:00
@var{n} events that are lists, the @var{n}th @samp{e} provides the
1994-05-28 15:27:52 +00:00
@var{n}th such event. Events that are not lists, such as function keys
1999-09-17 06:59:04 +00:00
and @sc{ascii} characters, do not count where @samp{e} is concerned.
1994-03-08 22:47:20 +00:00
@item f
A file name of an existing file (@pxref{File Names}). The default
directory is @code{default-directory}. Existing, Completion, Default,
Prompt.
@item F
A file name. The file need not exist. Completion, Default, Prompt.
1998-05-19 03:45:57 +00:00
@item i
An irrelevant argument. This code always supplies @code{nil} as
the argument's value. No I/O.
1994-03-08 22:47:20 +00:00
@item k
A key sequence (@pxref{Keymap Terminology}). This keeps reading events
until a command (or undefined command) is found in the current key
maps. The key sequence argument is represented as a string or vector.
The cursor does not move into the echo area. Prompt.
This kind of input is used by commands such as @code{describe-key} and
@code{global-set-key}.
1995-06-05 12:23:13 +00:00
@item K
A key sequence, whose definition you intend to change. This works like
@samp{k}, except that it suppresses, for the last input event in the key
sequence, the conversions that are normally used (when necessary) to
convert an undefined key into a defined one.
1994-03-08 22:47:20 +00:00
@item m
@cindex marker argument
1994-03-28 20:02:03 +00:00
The position of the mark, as an integer. No I/O.
1994-03-08 22:47:20 +00:00
1998-02-28 01:53:53 +00:00
@item M
Arbitrary text, read in the minibuffer using the current buffer's input
method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU
Emacs Manual}). Prompt.
1994-03-08 22:47:20 +00:00
@item n
A number read with the minibuffer. If the input is not a number, the
user is asked to try again. The prefix argument, if any, is not used.
Prompt.
@item N
@cindex raw prefix argument usage
1995-12-25 18:50:01 +00:00
The numeric prefix argument; but if there is no prefix argument, read a
number as with @kbd{n}. Requires a number. @xref{Prefix Command
1994-05-28 15:27:52 +00:00
Arguments}. Prompt.
1994-03-08 22:47:20 +00:00
@item p
@cindex numeric prefix argument usage
The numeric prefix argument. (Note that this @samp{p} is lower case.)
1994-05-28 15:27:52 +00:00
No I/O.
1994-03-08 22:47:20 +00:00
@item P
1994-05-28 15:27:52 +00:00
The raw prefix argument. (Note that this @samp{P} is upper case.) No
I/O.
1994-03-08 22:47:20 +00:00
@item r
@cindex region argument
Point and the mark, as two numeric arguments, smallest first. This is
the only code letter that specifies two successive arguments rather than
one. No I/O.
@item s
Arbitrary text, read in the minibuffer and returned as a string
(@pxref{Text from Minibuffer}). Terminate the input with either
1998-04-20 17:43:57 +00:00
@kbd{C-j} or @key{RET}. (@kbd{C-q} may be used to include either of
1994-03-08 22:47:20 +00:00
these characters in the input.) Prompt.
@item S
An interned symbol whose name is read in the minibuffer. Any whitespace
character terminates the input. (Use @kbd{C-q} to include whitespace in
the string.) Other characters that normally terminate a symbol (e.g.,
parentheses and brackets) do not do so here. Prompt.
@item v
A variable declared to be a user option (i.e., satisfying the predicate
@code{user-variable-p}). @xref{High-Level Completion}. Existing,
Completion, Prompt.
@item x
1994-05-28 15:27:52 +00:00
A Lisp object, specified with its read syntax, terminated with a
1998-04-20 17:43:57 +00:00
@kbd{C-j} or @key{RET}. The object is not evaluated. @xref{Object from
1994-03-08 22:47:20 +00:00
Minibuffer}. Prompt.
@item X
@cindex evaluated expression argument
A Lisp form is read as with @kbd{x}, but then evaluated so that its
value becomes the argument for the command. Prompt.
1998-05-19 03:45:57 +00:00
@item z
A coding system name (a symbol). If the user enters null input, the
argument value is @code{nil}. @xref{Coding Systems}. Completion,
Existing, Prompt.
@item Z
A coding system name (a symbol)---but only if this command has a prefix
argument. With no prefix argument, @samp{Z} provides @code{nil} as the
argument value. Completion, Existing, Prompt.
1994-03-08 22:47:20 +00:00
@end table
@node Interactive Examples
@comment node-name, next, previous, up
@subsection Examples of Using @code{interactive}
@cindex examples of using @code{interactive}
2003-02-04 14:56:31 +00:00
@cindex @code{interactive}, examples of using
1994-03-08 22:47:20 +00:00
Here are some examples of @code{interactive}:
@example
@group
(defun foo1 () ; @r{@code{foo1} takes no arguments,}
(interactive) ; @r{just moves forward two words.}
(forward-word 2))
@result{} foo1
@end group
@group
(defun foo2 (n) ; @r{@code{foo2} takes one argument,}
(interactive "p") ; @r{which is the numeric prefix.}
(forward-word (* 2 n)))
@result{} foo2
@end group
@group
(defun foo3 (n) ; @r{@code{foo3} takes one argument,}
(interactive "nCount:") ; @r{which is read with the Minibuffer.}
(forward-word (* 2 n)))
@result{} foo3
@end group
@group
(defun three-b (b1 b2 b3)
"Select three existing buffers.
Put them into three windows, selecting the last one."
@end group
(interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
(delete-other-windows)
(split-window (selected-window) 8)
(switch-to-buffer b1)
(other-window 1)
(split-window (selected-window) 8)
(switch-to-buffer b2)
(other-window 1)
(switch-to-buffer b3))
@result{} three-b
@group
(three-b "*scratch*" "declarations.texi" "*mail*")
@result{} nil
@end group
@end example
@node Interactive Call
@section Interactive Call
@cindex interactive call
1998-02-28 01:53:53 +00:00
After the command loop has translated a key sequence into a command it
invokes that command using the function @code{command-execute}. If the
command is a function, @code{command-execute} calls
@code{call-interactively}, which reads the arguments and calls the
command. You can also call these functions yourself.
1994-03-08 22:47:20 +00:00
@defun commandp object
Returns @code{t} if @var{object} is suitable for calling interactively;
2003-02-04 14:56:31 +00:00
that is, if @var{object} is a command. Otherwise, returns @code{nil}.
1994-03-08 22:47:20 +00:00
The interactively callable objects include strings and vectors (treated
as keyboard macros), lambda expressions that contain a top-level call to
1995-06-06 19:21:15 +00:00
@code{interactive}, byte-code function objects made from such lambda
1994-03-28 20:02:03 +00:00
expressions, autoload objects that are declared as interactive
(non-@code{nil} fourth argument to @code{autoload}), and some of the
primitive functions.
1994-03-08 22:47:20 +00:00
1998-04-20 17:43:57 +00:00
A symbol satisfies @code{commandp} if its function definition satisfies
1994-03-08 22:47:20 +00:00
@code{commandp}.
Keys and keymaps are not commands. Rather, they are used to look up
commands (@pxref{Keymaps}).
See @code{documentation} in @ref{Accessing Documentation}, for a
realistic example of using @code{commandp}.
@end defun
1998-02-28 01:53:53 +00:00
@defun call-interactively command &optional record-flag keys
1994-03-08 22:47:20 +00:00
This function calls the interactively callable function @var{command},
reading arguments according to its interactive calling specifications.
1994-05-28 15:27:52 +00:00
An error is signaled if @var{command} is not a function or if it cannot
be called interactively (i.e., is not a command). Note that keyboard
macros (strings and vectors) are not accepted, even though they are
considered commands, because they are not functions.
1994-03-08 22:47:20 +00:00
@cindex record command history
If @var{record-flag} is non-@code{nil}, then this command and its
arguments are unconditionally added to the list @code{command-history}.
Otherwise, the command is added only if it uses the minibuffer to read
an argument. @xref{Command History}.
1998-02-28 01:53:53 +00:00
The argument @var{keys}, if given, specifies the sequence of events to
1998-04-20 17:43:57 +00:00
supply if the command inquires which events were used to invoke it.
1994-03-08 22:47:20 +00:00
@end defun
1999-11-01 15:52:18 +00:00
@defun command-execute command &optional record-flag keys special
1994-03-08 22:47:20 +00:00
@cindex keyboard macro execution
1998-02-28 01:53:53 +00:00
This function executes @var{command}. The argument @var{command} must
satisfy the @code{commandp} predicate; i.e., it must be an interactively
callable function or a keyboard macro.
1994-03-08 22:47:20 +00:00
A string or vector as @var{command} is executed with
@code{execute-kbd-macro}. A function is passed to
@code{call-interactively}, along with the optional @var{record-flag}.
A symbol is handled by using its function definition in its place. A
symbol with an @code{autoload} definition counts as a command if it was
declared to stand for an interactively callable function. Such a
definition is handled by loading the specified library and then
rechecking the definition of the symbol.
1998-02-28 01:53:53 +00:00
The argument @var{keys}, if given, specifies the sequence of events to
1998-04-20 17:43:57 +00:00
supply if the command inquires which events were used to invoke it.
1999-11-01 15:52:18 +00:00
The argument @var{special}, if given, means to ignore the prefix
argument and not clear it. This is used for executing special events
(@pxref{Special Events}).
1994-03-08 22:47:20 +00:00
@end defun
@deffn Command execute-extended-command prefix-argument
@cindex read command name
This function reads a command name from the minibuffer using
@code{completing-read} (@pxref{Completion}). Then it uses
@code{command-execute} to call the specified command. Whatever that
command returns becomes the value of @code{execute-extended-command}.
@cindex execute with prefix argument
1994-03-28 20:02:03 +00:00
If the command asks for a prefix argument, it receives the value
@var{prefix-argument}. If @code{execute-extended-command} is called
interactively, the current raw prefix argument is used for
1994-03-08 22:47:20 +00:00
@var{prefix-argument}, and thus passed on to whatever command is run.
@c !!! Should this be @kindex?
@cindex @kbd{M-x}
@code{execute-extended-command} is the normal definition of @kbd{M-x},
so it uses the string @w{@samp{M-x }} as a prompt. (It would be better
to take the prompt from the events used to invoke
@code{execute-extended-command}, but that is painful to implement.) A
description of the value of the prefix argument, if any, also becomes
part of the prompt.
@example
@group
(execute-extended-command 1)
---------- Buffer: Minibuffer ----------
1994-05-28 15:27:52 +00:00
1 M-x forward-word RET
1994-03-08 22:47:20 +00:00
---------- Buffer: Minibuffer ----------
@result{} t
@end group
@end example
@end deffn
@defun interactive-p
1996-12-20 00:09:23 +00:00
This function returns @code{t} if the containing function (the one whose
code includes the call to @code{interactive-p}) was called
interactively, with the function @code{call-interactively}. (It makes
no difference whether @code{call-interactively} was called from Lisp or
directly from the editor command loop.) If the containing function was
called by Lisp evaluation (or with @code{apply} or @code{funcall}), then
it was not called interactively.
1998-04-20 17:43:57 +00:00
@end defun
1994-03-08 22:47:20 +00:00
1998-04-20 17:43:57 +00:00
The most common use of @code{interactive-p} is for deciding whether to
1994-03-08 22:47:20 +00:00
print an informative message. As a special exception,
@code{interactive-p} returns @code{nil} whenever a keyboard macro is
being run. This is to suppress the informative messages and speed
execution of the macro.
1998-04-20 17:43:57 +00:00
For example:
1994-03-08 22:47:20 +00:00
@example
@group
(defun foo ()
(interactive)
1998-04-20 17:43:57 +00:00
(when (interactive-p)
(message "foo")))
1994-03-08 22:47:20 +00:00
@result{} foo
@end group
@group
(defun bar ()
(interactive)
(setq foobar (list (foo) (interactive-p))))
@result{} bar
@end group
@group
;; @r{Type @kbd{M-x foo}.}
@print{} foo
@end group
@group
;; @r{Type @kbd{M-x bar}.}
;; @r{This does not print anything.}
@end group
@group
foobar
@result{} (nil t)
@end group
@end example
1998-04-20 17:43:57 +00:00
The other way to do this sort of job is to make the command take an
argument @code{print-message} which should be non-@code{nil} in an
interactive call, and use the @code{interactive} spec to make sure it is
non-@code{nil}. Here's how:
@example
(defun foo (&optional print-message)
(interactive "p")
(when print-message
(message "foo")))
@end example
The numeric prefix argument, provided by @samp{p}, is never @code{nil}.
1994-03-08 22:47:20 +00:00
@node Command Loop Info
@comment node-name, next, previous, up
@section Information from the Command Loop
The editor command loop sets several Lisp variables to keep status
2003-02-04 14:56:31 +00:00
records for itself and for commands that are run.
1994-03-08 22:47:20 +00:00
@defvar last-command
This variable records the name of the previous command executed by the
command loop (the one before the current command). Normally the value
is a symbol with a function definition, but this is not guaranteed.
1994-03-28 20:02:03 +00:00
The value is copied from @code{this-command} when a command returns to
1998-02-28 01:53:53 +00:00
the command loop, except when the command has specified a prefix
argument for the following command.
1995-06-06 19:21:15 +00:00
This variable is always local to the current terminal and cannot be
buffer-local. @xref{Multiple Displays}.
1994-03-08 22:47:20 +00:00
@end defvar
1998-06-11 20:20:07 +00:00
@defvar real-last-command
This variable is set up by Emacs just like @code{last-command},
but never altered by Lisp programs.
@end defvar
1994-03-08 22:47:20 +00:00
@defvar this-command
@cindex current command
This variable records the name of the command now being executed by
the editor command loop. Like @code{last-command}, it is normally a symbol
with a function definition.
1994-03-28 20:02:03 +00:00
The command loop sets this variable just before running a command, and
copies its value into @code{last-command} when the command finishes
1998-02-28 01:53:53 +00:00
(unless the command specified a prefix argument for the following
1994-03-28 20:02:03 +00:00
command).
1994-03-08 22:47:20 +00:00
@cindex kill command repetition
1994-03-28 20:02:03 +00:00
Some commands set this variable during their execution, as a flag for
1995-06-06 19:21:15 +00:00
whatever command runs next. In particular, the functions for killing text
1994-03-28 20:02:03 +00:00
set @code{this-command} to @code{kill-region} so that any kill commands
immediately following will know to append the killed text to the
previous kill.
1994-03-08 22:47:20 +00:00
@end defvar
If you do not want a particular command to be recognized as the previous
command in the case where it got an error, you must code that command to
prevent this. One way is to set @code{this-command} to @code{t} at the
beginning of the command, and set @code{this-command} back to its proper
value at the end, like this:
@example
(defun foo (args@dots{})
(interactive @dots{})
(let ((old-this-command this-command))
(setq this-command t)
@r{@dots{}do the work@dots{}}
(setq this-command old-this-command)))
@end example
1998-02-28 01:53:53 +00:00
@noindent
We do not bind @code{this-command} with @code{let} because that would
restore the old value in case of error---a feature of @code{let} which
in this case does precisely what we want to avoid.
1994-03-08 22:47:20 +00:00
@defun this-command-keys
This function returns a string or vector containing the key sequence
that invoked the present command, plus any previous commands that
generated the prefix argument for this command. The value is a string
if all those events were characters. @xref{Input Events}.
@example
@group
(this-command-keys)
1994-05-28 15:27:52 +00:00
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
1994-03-08 22:47:20 +00:00
@result{} "^U^X^E"
@end group
@end example
@end defun
1998-04-20 17:43:57 +00:00
@defun this-command-keys-vector
1999-12-03 19:11:12 +00:00
Like @code{this-command-keys}, except that it always returns the events
in a vector, so you don't need to deal with the complexities of storing
input events in a string (@pxref{Strings of Events}).
1998-04-20 17:43:57 +00:00
@end defun
1999-07-17 02:15:13 +00:00
@tindex clear-this-command-keys
@defun clear-this-command-keys
This function empties out the table of events for
2001-09-10 05:34:15 +00:00
@code{this-command-keys} to return, and also empties the records that
the function @code{recent-keys} (@pxref{Recording Input}) will
subsequently return. This is useful after reading a password, to
prevent the password from echoing inadvertently as part of the next
command in certain cases.
1999-07-17 02:15:13 +00:00
@end defun
1994-03-08 22:47:20 +00:00
@defvar last-nonmenu-event
1998-04-20 17:43:57 +00:00
This variable holds the last input event read as part of a key sequence,
not counting events resulting from mouse menus.
1994-03-08 22:47:20 +00:00
1998-02-28 01:53:53 +00:00
One use of this variable is for telling @code{x-popup-menu} where to pop
1998-04-20 17:43:57 +00:00
up a menu. It is also used internally by @code{y-or-n-p}
(@pxref{Yes-or-No Queries}).
1994-03-08 22:47:20 +00:00
@end defvar
@defvar last-command-event
@defvarx last-command-char
This variable is set to the last input event that was read by the
command loop as part of a command. The principal use of this variable
is in @code{self-insert-command}, which uses it to decide which
character to insert.
@example
@group
1994-03-28 20:02:03 +00:00
last-command-event
1994-05-28 15:27:52 +00:00
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.}
1994-03-08 22:47:20 +00:00
@result{} 5
@end group
@end example
@noindent
1999-09-17 06:59:04 +00:00
The value is 5 because that is the @sc{ascii} code for @kbd{C-e}.
1994-03-08 22:47:20 +00:00
The alias @code{last-command-char} exists for compatibility with
Emacs version 18.
@end defvar
@c Emacs 19 feature
@defvar last-event-frame
This variable records which frame the last input event was directed to.
Usually this is the frame that was selected when the event was
generated, but if that frame has redirected input focus to another
frame, the value is the frame to which the event was redirected.
@xref{Input Focus}.
@end defvar
1999-12-03 19:11:12 +00:00
@node Adjusting Point
@section Adjusting Point After Commands
It is not easy to display a value of point in the middle of a sequence
of text that has the @code{display} or @code{composition} property. So
after a command finishes and returns to the command loop, if point is
within such a sequence, the command loop normally moves point to the
edge of the sequence.
A command can inhibit this feature by setting the variable
@code{disable-point-adjustment}:
@defvar disable-point-adjustment
@tindex disable-point-adjustment
If this variable is non-@code{nil} when a command returns to the command
loop, then the command loop does not check for text properties such as
@code{display} and @code{composition}, and does not move point out of
sequences that have these properties.
The command loop sets this variable to @code{nil} before each command,
so if a command sets it, the effect applies only to that command.
@end defvar
@defvar global-disable-point-adjustment
@tindex global-disable-point-adjustment
If you set this variable to a non-@code{nil} value, the feature of
moving point out of these sequences is completely turned off.
@end defvar
1994-03-08 22:47:20 +00:00
@node Input Events
@section Input Events
@cindex events
@cindex input events
The Emacs command loop reads a sequence of @dfn{input events} that
represent keyboard or mouse activity. The events for keyboard activity
are characters or symbols; mouse events are always lists. This section
describes the representation and meaning of input events in detail.
@defun eventp object
1998-05-19 03:45:57 +00:00
This function returns non-@code{nil} if @var{object} is an input event
or event type.
Note that any symbol might be used as an event or an event type.
@code{eventp} cannot distinguish whether a symbol is intended by Lisp
code to be used as an event. Instead, it distinguishes whether the
symbol has actually been used in an event that has been read as input in
the current Emacs session. If a symbol has not yet been so used,
@code{eventp} returns @code{nil}.
1994-03-08 22:47:20 +00:00
@end defun
@menu
* Keyboard Events:: Ordinary characters--keys with symbols on them.
* Function Keys:: Function keys--keys with names, not symbols.
1994-03-28 20:02:03 +00:00
* Mouse Events:: Overview of mouse events.
1994-03-08 22:47:20 +00:00
* Click Events:: Pushing and releasing a mouse button.
* Drag Events:: Moving the mouse before releasing the button.
* Button-Down Events:: A button was pushed and not yet released.
* Repeat Events:: Double and triple click (or drag, or down).
* Motion Events:: Just moving the mouse, not pushing a button.
* Focus Events:: Moving the mouse between frames.
1995-06-05 12:23:13 +00:00
* Misc Events:: Other events window systems can generate.
1994-03-08 22:47:20 +00:00
* Event Examples:: Examples of the lists for mouse events.
* Classifying Events:: Finding the modifier keys in an event symbol.
Event types.
* Accessing Events:: Functions to extract info from events.
* Strings of Events:: Special considerations for putting
keyboard character events in a string.
@end menu
@node Keyboard Events
@subsection Keyboard Events
There are two kinds of input you can get from the keyboard: ordinary
keys, and function keys. Ordinary keys correspond to characters; the
1998-04-20 17:43:57 +00:00
events they generate are represented in Lisp as characters. The event
type of a character event is the character itself (an integer); see
@ref{Classifying Events}.
1994-03-08 22:47:20 +00:00
@cindex modifier bits (of input character)
@cindex basic code (of input character)
An input character event consists of a @dfn{basic code} between 0 and
1998-02-28 01:53:53 +00:00
524287, plus any or all of these @dfn{modifier bits}:
1994-03-08 22:47:20 +00:00
@table @asis
@item meta
1995-06-06 19:21:15 +00:00
The
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{27}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**27
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit in the character code indicates a character
1994-03-08 22:47:20 +00:00
typed with the meta key held down.
@item control
1995-06-06 19:21:15 +00:00
The
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{26}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**26
2000-01-05 15:18:01 +00:00
@end ifnottex
1999-09-17 06:59:04 +00:00
bit in the character code indicates a non-@sc{ascii}
1994-03-08 22:47:20 +00:00
control character.
1999-09-17 06:59:04 +00:00
@sc{ascii} control characters such as @kbd{C-a} have special basic
1994-03-08 22:47:20 +00:00
codes of their own, so Emacs needs no special bit to indicate them.
Thus, the code for @kbd{C-a} is just 1.
1999-09-17 06:59:04 +00:00
But if you type a control combination not in @sc{ascii}, such as
1994-03-08 22:47:20 +00:00
@kbd{%} with the control key, the numeric value you get is the code
1995-06-06 19:21:15 +00:00
for @kbd{%} plus
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{26}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**26
2000-01-05 15:18:01 +00:00
@end ifnottex
1999-09-17 06:59:04 +00:00
(assuming the terminal supports non-@sc{ascii}
1994-03-08 22:47:20 +00:00
control characters).
@item shift
1995-06-06 19:21:15 +00:00
The
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{25}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**25
2000-01-05 15:18:01 +00:00
@end ifnottex
1999-09-17 06:59:04 +00:00
bit in the character code indicates an @sc{ascii} control
1994-03-08 22:47:20 +00:00
character typed with the shift key held down.
1998-02-28 01:53:53 +00:00
For letters, the basic code itself indicates upper versus lower case;
for digits and punctuation, the shift key selects an entirely different
character with a different basic code. In order to keep within the
1999-09-17 06:59:04 +00:00
@sc{ascii} character set whenever possible, Emacs avoids using the
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{25}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**25
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit for those characters.
1994-03-08 22:47:20 +00:00
1999-09-17 06:59:04 +00:00
However, @sc{ascii} provides no way to distinguish @kbd{C-A} from
1995-06-06 19:21:15 +00:00
@kbd{C-a}, so Emacs uses the
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{25}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**25
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit in @kbd{C-A} and not in
1994-03-08 22:47:20 +00:00
@kbd{C-a}.
@item hyper
1995-06-06 19:21:15 +00:00
The
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{24}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**24
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit in the character code indicates a character
1994-03-08 22:47:20 +00:00
typed with the hyper key held down.
@item super
1995-06-06 19:21:15 +00:00
The
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{23}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**23
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit in the character code indicates a character
1994-03-08 22:47:20 +00:00
typed with the super key held down.
@item alt
1995-06-06 19:21:15 +00:00
The
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{22}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**22
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit in the character code indicates a character typed with
1994-03-08 22:47:20 +00:00
the alt key held down. (On some terminals, the key labeled @key{ALT}
is actually the meta key.)
@end table
1995-06-06 19:21:15 +00:00
It is best to avoid mentioning specific bit numbers in your program.
To test the modifier bits of a character, use the function
@code{event-modifiers} (@pxref{Classifying Events}). When making key
bindings, you can use the read syntax for characters with modifier bits
(@samp{\C-}, @samp{\M-}, and so on). For making key bindings with
@code{define-key}, you can use lists such as @code{(control hyper ?x)} to
specify the characters (@pxref{Changing Key Bindings}). The function
@code{event-convert-list} converts such a list into an event type
(@pxref{Classifying Events}).
1994-03-08 22:47:20 +00:00
@node Function Keys
@subsection Function Keys
@cindex function keys
1994-05-28 15:27:52 +00:00
Most keyboards also have @dfn{function keys}---keys that have names or
1998-02-28 01:53:53 +00:00
symbols that are not characters. Function keys are represented in Emacs
Lisp as symbols; the symbol's name is the function key's label, in lower
1994-03-28 20:02:03 +00:00
case. For example, pressing a key labeled @key{F1} places the symbol
@code{f1} in the input stream.
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
The event type of a function key event is the event symbol itself.
@xref{Classifying Events}.
1994-03-08 22:47:20 +00:00
1994-05-28 15:27:52 +00:00
Here are a few special cases in the symbol-naming convention for
1994-03-08 22:47:20 +00:00
function keys:
@table @asis
@item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete}
1999-09-17 06:59:04 +00:00
These keys correspond to common @sc{ascii} control characters that have
1994-03-08 22:47:20 +00:00
special keys on most keyboards.
1999-09-17 06:59:04 +00:00
In @sc{ascii}, @kbd{C-i} and @key{TAB} are the same character. If the
1994-03-28 20:02:03 +00:00
terminal can distinguish between them, Emacs conveys the distinction to
Lisp programs by representing the former as the integer 9, and the
latter as the symbol @code{tab}.
1994-03-08 22:47:20 +00:00
Most of the time, it's not useful to distinguish the two. So normally
1996-07-23 15:41:53 +00:00
@code{function-key-map} (@pxref{Translating Input}) is set up to map
@code{tab} into 9. Thus, a key binding for character code 9 (the
character @kbd{C-i}) also applies to @code{tab}. Likewise for the other
symbols in this group. The function @code{read-char} likewise converts
these events into characters.
1994-03-08 22:47:20 +00:00
1999-09-17 06:59:04 +00:00
In @sc{ascii}, @key{BS} is really @kbd{C-h}. But @code{backspace}
1994-03-08 22:47:20 +00:00
converts into the character code 127 (@key{DEL}), not into code 8
(@key{BS}). This is what most users prefer.
1994-05-28 15:27:52 +00:00
@item @code{left}, @code{up}, @code{right}, @code{down}
Cursor arrow keys
1994-03-08 22:47:20 +00:00
@item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{}
Keypad keys (to the right of the regular keyboard).
@item @code{kp-0}, @code{kp-1}, @dots{}
Keypad keys with digits.
@item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4}
Keypad PF keys.
1994-05-28 15:27:52 +00:00
@item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down}
1998-02-28 01:53:53 +00:00
Keypad arrow keys. Emacs normally translates these into the
corresponding non-keypad keys @code{home}, @code{left}, @dots{}
1994-05-28 15:27:52 +00:00
@item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete}
Additional keypad duplicates of keys ordinarily found elsewhere. Emacs
normally translates these into the like-named non-keypad keys.
1994-03-08 22:47:20 +00:00
@end table
1994-05-28 15:27:52 +00:00
You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER},
@key{META}, @key{SHIFT}, and @key{SUPER} with function keys. The way to
represent them is with prefixes in the symbol name:
1994-03-08 22:47:20 +00:00
@table @samp
@item A-
The alt modifier.
@item C-
The control modifier.
@item H-
The hyper modifier.
@item M-
The meta modifier.
@item S-
The shift modifier.
@item s-
The super modifier.
@end table
Thus, the symbol for the key @key{F3} with @key{META} held down is
1994-08-13 05:26:42 +00:00
@code{M-f3}. When you use more than one prefix, we recommend you
1994-03-28 20:02:03 +00:00
write them in alphabetical order; but the order does not matter in
arguments to the key-binding lookup and modification functions.
@node Mouse Events
@subsection Mouse Events
Emacs supports four kinds of mouse events: click events, drag events,
button-down events, and motion events. All mouse events are represented
as lists. The @sc{car} of the list is the event type; this says which
mouse button was involved, and which modifier keys were used with it.
The event type can also distinguish double or triple button presses
(@pxref{Repeat Events}). The rest of the list elements give position
and time information.
For key lookup, only the event type matters: two events of the same type
necessarily run the same command. The command can access the full
values of these events using the @samp{e} interactive code.
@xref{Interactive Codes}.
A key sequence that starts with a mouse event is read using the keymaps
of the buffer in the window that the mouse was in, not the current
buffer. This does not imply that clicking in a window selects that
window or its buffer---that is entirely under the control of the command
binding of the key sequence.
1994-03-08 22:47:20 +00:00
@node Click Events
@subsection Click Events
@cindex click event
@cindex mouse click event
When the user presses a mouse button and releases it at the same
location, that generates a @dfn{click} event. Mouse click events have
this form:
@example
(@var{event-type}
2002-04-19 00:18:22 +00:00
(@var{window} @var{buffer-pos} (@var{x} . @var{y})
@var{timestamp})
1994-03-08 22:47:20 +00:00
@var{click-count})
@end example
or, for clicks on strings in the mode line, header line or marginal
areas:
@example
(@var{event-type}
(@var{window} @var{buffer-pos} (@var{x} . @var{y}) @var{timestamp} (@var{string} . @var{string-pos})
@var{click-count})
@end example
1994-03-08 22:47:20 +00:00
Here is what the elements normally mean:
1994-03-28 20:02:03 +00:00
@table @asis
@item @var{event-type}
1994-03-08 22:47:20 +00:00
This is a symbol that indicates which mouse button was used. It is
one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the
1994-03-28 20:02:03 +00:00
buttons are numbered left to right.
1994-03-08 22:47:20 +00:00
You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-},
@samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift
and super, just as you would with function keys.
This symbol also serves as the event type of the event. Key bindings
describe events by their types; thus, if there is a key binding for
@code{mouse-1}, that binding would apply to all events whose
@var{event-type} is @code{mouse-1}.
1994-03-28 20:02:03 +00:00
@item @var{window}
1994-03-08 22:47:20 +00:00
This is the window in which the click occurred.
1994-03-28 20:02:03 +00:00
@item @var{x}, @var{y}
1994-05-28 15:27:52 +00:00
These are the pixel-denominated coordinates of the click, relative to
the top left corner of @var{window}, which is @code{(0 . 0)}.
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
@item @var{buffer-pos}
1994-03-08 22:47:20 +00:00
This is the buffer position of the character clicked on.
1994-03-28 20:02:03 +00:00
@item @var{timestamp}
1994-03-08 22:47:20 +00:00
This is the time at which the event occurred, in milliseconds. (Since
this value wraps around the entire range of Emacs Lisp integers in about
five hours, it is useful only for relating the times of nearby
events.)
@item @var{string}
This is the string on which the click occurred, including any
properties.
@item @var{string-pos}
This is the position in the string on which the click occurred,
relevant if properties at the click need to be looked up.
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
@item @var{click-count}
1994-03-08 22:47:20 +00:00
This is the number of rapid repeated presses so far of the same mouse
button. @xref{Repeat Events}.
@end table
1994-03-28 20:02:03 +00:00
The meanings of @var{buffer-pos}, @var{x} and @var{y} are somewhat
different when the event location is in a special part of the screen,
such as the mode line or a scroll bar.
1994-03-08 22:47:20 +00:00
If the location is in a scroll bar, then @var{buffer-pos} is the symbol
@code{vertical-scroll-bar} or @code{horizontal-scroll-bar}, and the pair
@code{(@var{x} . @var{y})} is replaced with a pair @code{(@var{portion}
. @var{whole})}, where @var{portion} is the distance of the click from
the top or left end of the scroll bar, and @var{whole} is the length of
the entire scroll bar.
If the position is on a mode line, the vertical line separating
@var{window} from its neighbor to the right, or in a marginal area,
then @var{buffer-pos} is the symbol @code{mode-line},
@code{header-line}, @code{vertical-line}, @code{left-margin}, or
@code{right-margin}. For the mode line, @var{y} does not have
1999-11-01 15:52:18 +00:00
meaningful data. For the vertical line, @var{x} does not have
meaningful data.
1994-03-08 22:47:20 +00:00
1994-05-28 15:27:52 +00:00
In one special case, @var{buffer-pos} is a list containing a symbol (one
of the symbols listed above) instead of just the symbol. This happens
after the imaginary prefix keys for the event are inserted into the
input stream. @xref{Key Sequence Input}.
1994-03-08 22:47:20 +00:00
@node Drag Events
@subsection Drag Events
@cindex drag event
@cindex mouse drag event
With Emacs, you can have a drag event without even changing your
clothes. A @dfn{drag event} happens every time the user presses a mouse
button and then moves the mouse to a different character position before
releasing the button. Like all mouse events, drag events are
represented in Lisp as lists. The lists record both the starting mouse
position and the final position, like this:
@example
(@var{event-type}
1994-03-28 20:02:03 +00:00
(@var{window1} @var{buffer-pos1} (@var{x1} . @var{y1}) @var{timestamp1})
(@var{window2} @var{buffer-pos2} (@var{x2} . @var{y2}) @var{timestamp2})
1994-03-08 22:47:20 +00:00
@var{click-count})
@end example
For a drag event, the name of the symbol @var{event-type} contains the
1998-02-28 01:53:53 +00:00
prefix @samp{drag-}. For example, dragging the mouse with button 2 held
down generates a @code{drag-mouse-2} event. The second and third
elements of the event give the starting and ending position of the drag.
Aside from that, the data have the same meanings as in a click event
(@pxref{Click Events}). You can access the second element of any mouse
event in the same way, with no need to distinguish drag events from
others.
1994-03-08 22:47:20 +00:00
The @samp{drag-} prefix follows the modifier key prefixes such as
@samp{C-} and @samp{M-}.
1994-05-28 15:27:52 +00:00
If @code{read-key-sequence} receives a drag event that has no key
1994-03-08 22:47:20 +00:00
binding, and the corresponding click event does have a binding, it
changes the drag event into a click event at the drag's starting
position. This means that you don't have to distinguish between click
and drag events unless you want to.
@node Button-Down Events
@subsection Button-Down Events
@cindex button-down event
Click and drag events happen when the user releases a mouse button.
They cannot happen earlier, because there is no way to distinguish a
click from a drag until the button is released.
If you want to take action as soon as a button is pressed, you need to
handle @dfn{button-down} events.@footnote{Button-down is the
1994-03-28 20:02:03 +00:00
conservative antithesis of drag.} These occur as soon as a button is
1994-05-28 15:27:52 +00:00
pressed. They are represented by lists that look exactly like click
1994-03-28 20:02:03 +00:00
events (@pxref{Click Events}), except that the @var{event-type} symbol
name contains the prefix @samp{down-}. The @samp{down-} prefix follows
1994-03-08 22:47:20 +00:00
modifier key prefixes such as @samp{C-} and @samp{M-}.
1998-04-20 17:43:57 +00:00
The function @code{read-key-sequence} ignores any button-down events
that don't have command bindings; therefore, the Emacs command loop
ignores them too. This means that you need not worry about defining
button-down events unless you want them to do something. The usual
reason to define a button-down event is so that you can track mouse
motion (by reading motion events) until the button is released.
@xref{Motion Events}.
1994-03-08 22:47:20 +00:00
@node Repeat Events
@subsection Repeat Events
@cindex repeat events
@cindex double-click events
@cindex triple-click events
1999-12-03 19:11:12 +00:00
@cindex mouse events, repeated
1994-03-08 22:47:20 +00:00
If you press the same mouse button more than once in quick succession
1994-03-28 20:02:03 +00:00
without moving the mouse, Emacs generates special @dfn{repeat} mouse
events for the second and subsequent presses.
1994-03-08 22:47:20 +00:00
The most common repeat events are @dfn{double-click} events. Emacs
generates a double-click event when you click a button twice; the event
happens when you release the button (as is normal for all click
events).
The event type of a double-click event contains the prefix
1994-03-28 20:02:03 +00:00
@samp{double-}. Thus, a double click on the second mouse button with
1994-03-08 22:47:20 +00:00
@key{meta} held down comes to the Lisp program as
@code{M-double-mouse-2}. If a double-click event has no binding, the
binding of the corresponding ordinary click event is used to execute
2003-02-04 14:56:31 +00:00
it. Thus, you need not pay attention to the double click feature
1994-03-08 22:47:20 +00:00
unless you really want to.
When the user performs a double click, Emacs generates first an ordinary
1994-03-28 20:02:03 +00:00
click event, and then a double-click event. Therefore, you must design
the command binding of the double click event to assume that the
1994-03-08 22:47:20 +00:00
single-click command has already run. It must produce the desired
results of a double click, starting from the results of a single click.
1994-03-28 20:02:03 +00:00
This is convenient, if the meaning of a double click somehow ``builds
on'' the meaning of a single click---which is recommended user interface
design practice for double clicks.
1994-03-08 22:47:20 +00:00
If you click a button, then press it down again and start moving the
mouse with the button held down, then you get a @dfn{double-drag} event
when you ultimately release the button. Its event type contains
@samp{double-drag} instead of just @samp{drag}. If a double-drag event
has no binding, Emacs looks for an alternate binding as if the event
1994-05-28 15:27:52 +00:00
were an ordinary drag.
1994-03-08 22:47:20 +00:00
Before the double-click or double-drag event, Emacs generates a
1994-03-28 20:02:03 +00:00
@dfn{double-down} event when the user presses the button down for the
second time. Its event type contains @samp{double-down} instead of just
1994-03-08 22:47:20 +00:00
@samp{down}. If a double-down event has no binding, Emacs looks for an
alternate binding as if the event were an ordinary button-down event.
1994-03-28 20:02:03 +00:00
If it finds no binding that way either, the double-down event is
ignored.
1994-03-08 22:47:20 +00:00
To summarize, when you click a button and then press it again right
1994-05-28 15:27:52 +00:00
away, Emacs generates a down event and a click event for the first
click, a double-down event when you press the button again, and finally
either a double-click or a double-drag event.
1994-03-08 22:47:20 +00:00
If you click a button twice and then press it again, all in quick
succession, Emacs generates a @dfn{triple-down} event, followed by
either a @dfn{triple-click} or a @dfn{triple-drag}. The event types of
these events contain @samp{triple} instead of @samp{double}. If any
triple event has no binding, Emacs uses the binding that it would use
for the corresponding double event.
1994-03-28 20:02:03 +00:00
If you click a button three or more times and then press it again, the
events for the presses beyond the third are all triple events. Emacs
does not have separate event types for quadruple, quintuple, etc.@:
events. However, you can look at the event list to find out precisely
how many times the button was pressed.
1994-03-08 22:47:20 +00:00
@defun event-click-count event
This function returns the number of consecutive button presses that led
up to @var{event}. If @var{event} is a double-down, double-click or
double-drag event, the value is 2. If @var{event} is a triple event,
the value is 3 or greater. If @var{event} is an ordinary mouse event
(not a repeat event), the value is 1.
@end defun
@defvar double-click-fuzz
1994-03-28 20:02:03 +00:00
To generate repeat events, successive mouse button presses must be at
approximately the same screen position. The value of
@code{double-click-fuzz} specifies the maximum number of pixels the
mouse may be moved between two successive clicks to make a
double-click.
@end defvar
@defvar double-click-time
To generate repeat events, the number of milliseconds between
1994-03-28 20:02:03 +00:00
successive button presses must be less than the value of
1994-03-08 22:47:20 +00:00
@code{double-click-time}. Setting @code{double-click-time} to
@code{nil} disables multi-click detection entirely. Setting it to
@code{t} removes the time limit; Emacs then detects multi-clicks by
position only.
@end defvar
@node Motion Events
@subsection Motion Events
@cindex motion event
@cindex mouse motion events
Emacs sometimes generates @dfn{mouse motion} events to describe motion
of the mouse without any button activity. Mouse motion events are
represented by lists that look like this:
@example
1998-04-20 17:43:57 +00:00
(mouse-movement (@var{window} @var{buffer-pos} (@var{x} . @var{y}) @var{timestamp}))
1994-03-08 22:47:20 +00:00
@end example
The second element of the list describes the current position of the
mouse, just as in a click event (@pxref{Click Events}).
The special form @code{track-mouse} enables generation of motion events
within its body. Outside of @code{track-mouse} forms, Emacs does not
generate events for mere motion of the mouse, and these events do not
1998-04-20 17:43:57 +00:00
appear. @xref{Mouse Tracking}.
1994-03-08 22:47:20 +00:00
@node Focus Events
@subsection Focus Events
@cindex focus event
Window systems provide general ways for the user to control which window
gets keyboard input. This choice of window is called the @dfn{focus}.
When the user does something to switch between Emacs frames, that
generates a @dfn{focus event}. The normal definition of a focus event,
in the global keymap, is to select a new frame within Emacs, as the user
would expect. @xref{Input Focus}.
Focus events are represented in Lisp as lists that look like this:
@example
(switch-frame @var{new-frame})
@end example
@noindent
where @var{new-frame} is the frame switched to.
1994-05-28 15:27:52 +00:00
Most X window managers are set up so that just moving the mouse into a
window is enough to set the focus there. Emacs appears to do this,
because it changes the cursor to solid in the new frame. However, there
is no need for the Lisp program to know about the focus change until
some other kind of input arrives. So Emacs generates a focus event only
when the user actually types a keyboard key or presses a mouse button in
the new frame; just moving the mouse between frames does not generate a
focus event.
1994-03-08 22:47:20 +00:00
A focus event in the middle of a key sequence would garble the
sequence. So Emacs never generates a focus event in the middle of a key
sequence. If the user changes focus in the middle of a key
sequence---that is, after a prefix key---then Emacs reorders the events
so that the focus event comes either before or after the multi-event key
sequence, and not within it.
1995-06-05 12:23:13 +00:00
@node Misc Events
@subsection Miscellaneous Window System Events
A few other event types represent occurrences within the window system.
@table @code
@cindex @code{delete-frame} event
@item (delete-frame (@var{frame}))
This kind of event indicates that the user gave the window manager
a command to delete a particular window, which happens to be an Emacs frame.
The standard definition of the @code{delete-frame} event is to delete @var{frame}.
@cindex @code{iconify-frame} event
@item (iconify-frame (@var{frame}))
This kind of event indicates that the user iconified @var{frame} using
1995-09-08 16:58:39 +00:00
the window manager. Its standard definition is @code{ignore}; since the
frame has already been iconified, Emacs has no work to do. The purpose
of this event type is so that you can keep track of such events if you
want to.
1995-06-05 12:23:13 +00:00
@cindex @code{make-frame-visible} event
@item (make-frame-visible (@var{frame}))
1995-06-05 12:23:13 +00:00
This kind of event indicates that the user deiconified @var{frame} using
the window manager. Its standard definition is @code{ignore}; since the
1995-09-08 16:58:39 +00:00
frame has already been made visible, Emacs has no work to do.
1998-05-19 03:45:57 +00:00
@cindex @code{mouse-wheel} event
@item (mouse-wheel @var{position} @var{delta})
This kind of event is generated by moving a wheel on a mouse (such as
the MS Intellimouse). Its effect is typically a kind of scroll or zoom.
The element @var{delta} describes the amount and direction of the wheel
rotation. Its absolute value is the number of increments by which the
wheel was rotated. A negative @var{delta} indicates that the wheel was
rotated backwards, towards the user, and a positive @var{delta}
indicates that the wheel was rotated forward, away from the user.
The element @var{position} is a list describing the position of the
event, in the same format as used in a mouse-click event.
This kind of event is generated only on some kinds of systems.
@cindex @code{drag-n-drop} event
@item (drag-n-drop @var{position} @var{files})
This kind of event is generated when a group of files is
selected in an application outside of Emacs, and then dragged and
dropped onto an Emacs frame.
The element @var{position} is a list describing the position of the
event, in the same format as used in a mouse-click event, and
@var{files} is the list of file names that were dragged and dropped.
The usual way to handle this event is by visiting these files.
This kind of event is generated, at present, only on some kinds of
systems.
1995-06-05 12:23:13 +00:00
@end table
1995-06-06 19:21:15 +00:00
If one of these events arrives in the middle of a key sequence---that
is, after a prefix key---then Emacs reorders the events so that this
event comes either before or after the multi-event key sequence, not
within it.
1994-03-08 22:47:20 +00:00
@node Event Examples
@subsection Event Examples
If the user presses and releases the left mouse button over the same
location, that generates a sequence of events like this:
@smallexample
(down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
(mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180))
@end smallexample
1994-03-28 20:02:03 +00:00
While holding the control key down, the user might hold down the
1994-03-08 22:47:20 +00:00
second mouse button, and drag the mouse from one line to the next.
That produces two events, as shown here:
@smallexample
(C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219))
(C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)
(#<window 18 on NEWS> 3510 (0 . 28) -729648))
@end smallexample
1994-03-28 20:02:03 +00:00
While holding down the meta and shift keys, the user might press the
1994-03-08 22:47:20 +00:00
second mouse button on the window's mode line, and then drag the mouse
1994-03-28 20:02:03 +00:00
into another window. That produces a pair of events like these:
1994-03-08 22:47:20 +00:00
@smallexample
(M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844))
(M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)
(#<window 20 on carlton-sanskrit.tex> 161 (33 . 3)
-453816))
@end smallexample
@node Classifying Events
@subsection Classifying Events
@cindex event type
1994-05-28 15:27:52 +00:00
Every event has an @dfn{event type}, which classifies the event for
key binding purposes. For a keyboard event, the event type equals the
event value; thus, the event type for a character is the character, and
the event type for a function key symbol is the symbol itself. For
events that are lists, the event type is the symbol in the @sc{car} of
the list. Thus, the event type is always a symbol or a character.
1994-03-08 22:47:20 +00:00
Two events of the same type are equivalent where key bindings are
concerned; thus, they always run the same command. That does not
necessarily mean they do the same things, however, as some commands look
at the whole event to decide what to do. For example, some commands use
1994-03-28 20:02:03 +00:00
the location of a mouse event to decide where in the buffer to act.
1994-03-08 22:47:20 +00:00
Sometimes broader classifications of events are useful. For example,
you might want to ask whether an event involved the @key{META} key,
regardless of which other key or mouse button was used.
The functions @code{event-modifiers} and @code{event-basic-type} are
provided to get such information conveniently.
@defun event-modifiers event
1994-03-28 20:02:03 +00:00
This function returns a list of the modifiers that @var{event} has. The
modifiers are symbols; they include @code{shift}, @code{control},
1994-03-08 22:47:20 +00:00
@code{meta}, @code{alt}, @code{hyper} and @code{super}. In addition,
1994-03-28 20:02:03 +00:00
the modifiers list of a mouse event symbol always contains one of
@code{click}, @code{drag}, and @code{down}.
The argument @var{event} may be an entire event object, or just an event
type.
Here are some examples:
1994-03-08 22:47:20 +00:00
@example
(event-modifiers ?a)
@result{} nil
(event-modifiers ?\C-a)
@result{} (control)
(event-modifiers ?\C-%)
@result{} (control)
(event-modifiers ?\C-\S-a)
@result{} (control shift)
(event-modifiers 'f5)
@result{} nil
(event-modifiers 's-f5)
@result{} (super)
(event-modifiers 'M-S-f5)
@result{} (meta shift)
(event-modifiers 'mouse-1)
@result{} (click)
(event-modifiers 'down-mouse-1)
@result{} (down)
@end example
The modifiers list for a click event explicitly contains @code{click},
but the event symbol name itself does not contain @samp{click}.
@end defun
@defun event-basic-type event
This function returns the key or mouse button that @var{event}
describes, with all modifiers removed. For example:
@example
(event-basic-type ?a)
@result{} 97
(event-basic-type ?A)
@result{} 97
(event-basic-type ?\C-a)
@result{} 97
(event-basic-type ?\C-\S-a)
@result{} 97
(event-basic-type 'f5)
@result{} f5
(event-basic-type 's-f5)
@result{} f5
(event-basic-type 'M-S-f5)
@result{} f5
(event-basic-type 'down-mouse-1)
@result{} mouse-1
@end example
@end defun
@defun mouse-movement-p object
This function returns non-@code{nil} if @var{object} is a mouse movement
event.
@end defun
1995-06-06 19:21:15 +00:00
@defun event-convert-list list
This function converts a list of modifier names and a basic event type
to an event type which specifies all of them. For example,
@example
(event-convert-list '(control ?a))
@result{} 1
(event-convert-list '(control meta ?a))
@result{} -134217727
(event-convert-list '(control super f1))
@result{} C-s-f1
@end example
@end defun
1994-03-08 22:47:20 +00:00
@node Accessing Events
@subsection Accessing Events
1999-12-03 19:11:12 +00:00
@cindex mouse events, accessing the data
@cindex accessing data of mouse events
1994-03-08 22:47:20 +00:00
This section describes convenient functions for accessing the data in
1994-03-28 20:02:03 +00:00
a mouse button or motion event.
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
These two functions return the starting or ending position of a
1998-04-20 17:43:57 +00:00
mouse-button event, as a list of this form:
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
@example
1994-05-28 15:27:52 +00:00
(@var{window} @var{buffer-position} (@var{x} . @var{y}) @var{timestamp})
1994-03-28 20:02:03 +00:00
@end example
1994-03-08 22:47:20 +00:00
@defun event-start event
This returns the starting position of @var{event}.
If @var{event} is a click or button-down event, this returns the
location of the event. If @var{event} is a drag event, this returns the
drag's starting position.
@end defun
@defun event-end event
This returns the ending position of @var{event}.
If @var{event} is a drag event, this returns the position where the user
released the mouse button. If @var{event} is a click or button-down
event, the value is actually the starting position, which is the only
position such events have.
@end defun
1999-12-03 19:11:12 +00:00
@cindex mouse position list, accessing
1998-04-20 17:43:57 +00:00
These five functions take a position list as described above, and
return various parts of it.
1994-03-08 22:47:20 +00:00
@defun posn-window position
Return the window that @var{position} is in.
@end defun
@defun posn-point position
1994-03-28 20:02:03 +00:00
Return the buffer position in @var{position}. This is an integer.
1994-03-08 22:47:20 +00:00
@end defun
@defun posn-x-y position
1994-05-28 15:27:52 +00:00
Return the pixel-based x and y coordinates in @var{position}, as a cons
cell @code{(@var{x} . @var{y})}.
1994-03-08 22:47:20 +00:00
@end defun
@defun posn-col-row position
1994-03-28 20:02:03 +00:00
Return the row and column (in units of characters) of @var{position}, as
1994-03-08 22:47:20 +00:00
a cons cell @code{(@var{col} . @var{row})}. These are computed from the
@var{x} and @var{y} values actually found in @var{position}.
@end defun
1999-12-03 19:11:12 +00:00
@cindex mouse event, timestamp
@cindex timestamp of a mouse event
1994-03-08 22:47:20 +00:00
@defun posn-timestamp position
1994-03-28 20:02:03 +00:00
Return the timestamp in @var{position}.
1994-03-08 22:47:20 +00:00
@end defun
1998-04-20 17:43:57 +00:00
These functions are useful for decoding scroll bar events.
1994-05-28 15:27:52 +00:00
@defun scroll-bar-event-ratio event
This function returns the fractional vertical position of a scroll bar
event within the scroll bar. The value is a cons cell
@code{(@var{portion} . @var{whole})} containing two integers whose ratio
is the fractional position.
@end defun
1994-03-08 22:47:20 +00:00
@defun scroll-bar-scale ratio total
1994-05-28 15:27:52 +00:00
This function multiplies (in effect) @var{ratio} by @var{total},
rounding the result to an integer. The argument @var{ratio} is not a
number, but rather a pair @code{(@var{num} . @var{denom})}---typically a
value returned by @code{scroll-bar-event-ratio}.
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
This function is handy for scaling a position on a scroll bar into a
buffer position. Here's how to do that:
1994-03-08 22:47:20 +00:00
@example
(+ (point-min)
(scroll-bar-scale
1994-05-28 15:27:52 +00:00
(posn-x-y (event-start event))
1994-03-08 22:47:20 +00:00
(- (point-max) (point-min))))
@end example
1994-05-28 15:27:52 +00:00
1998-05-26 18:56:56 +00:00
Recall that scroll bar events have two integers forming a ratio, in place
1994-05-28 15:27:52 +00:00
of a pair of x and y coordinates.
1994-03-08 22:47:20 +00:00
@end defun
@node Strings of Events
@subsection Putting Keyboard Events in Strings
1999-12-03 19:11:12 +00:00
@cindex keyboard events in strings
@cindex strings with keyboard events
1994-03-08 22:47:20 +00:00
In most of the places where strings are used, we conceptualize the
string as containing text characters---the same kind of characters found
1994-05-28 15:27:52 +00:00
in buffers or files. Occasionally Lisp programs use strings that
1994-03-08 22:47:20 +00:00
conceptually contain keyboard characters; for example, they may be key
1998-04-20 17:43:57 +00:00
sequences or keyboard macro definitions. However, storing keyboard
characters in a string is a complex matter, for reasons of historical
compatibility, and it is not always possible.
We recommend that new programs avoid dealing with these complexities
by not storing keyboard events in strings. Here is how to do that:
@itemize @bullet
@item
Use vectors instead of strings for key sequences, when you plan to use
1998-05-19 03:45:57 +00:00
them for anything other than as arguments to @code{lookup-key} and
1998-04-20 17:43:57 +00:00
@code{define-key}. For example, you can use
@code{read-key-sequence-vector} instead of @code{read-key-sequence}, and
@code{this-command-keys-vector} instead of @code{this-command-keys}.
@item
Use vectors to write key sequence constants containing meta characters,
even when passing them directly to @code{define-key}.
@item
When you have to look at the contents of a key sequence that might be a
string, use @code{listify-key-sequence} (@pxref{Event Input Misc})
first, to convert it to a list.
@end itemize
1994-03-08 22:47:20 +00:00
1998-04-20 17:43:57 +00:00
The complexities stem from the modifier bits that keyboard input
characters can include. Aside from the Meta modifier, none of these
modifier bits can be included in a string, and the Meta modifier is
allowed only in special cases.
The earliest GNU Emacs versions represented meta characters as codes
in the range of 128 to 255. At that time, the basic character codes
ranged from 0 to 127, so all keyboard character codes did fit in a
string. Many Lisp programs used @samp{\M-} in string constants to stand
for meta characters, especially in arguments to @code{define-key} and
similar functions, and key sequences and sequences of events were always
represented as strings.
When we added support for larger basic character codes beyond 127, and
additional modifier bits, we had to change the representation of meta
characters. Now the flag that represents the Meta modifier in a
character is
@tex
1999-09-17 06:59:04 +00:00
@math{2^{27}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**27
2000-01-05 15:18:01 +00:00
@end ifnottex
1998-04-20 17:43:57 +00:00
and such numbers cannot be included in a string.
1994-03-08 22:47:20 +00:00
1998-04-20 17:43:57 +00:00
To support programs with @samp{\M-} in string constants, there are
special rules for including certain meta characters in a string.
1998-05-26 18:56:56 +00:00
Here are the rules for interpreting a string as a sequence of input
characters:
1994-03-08 22:47:20 +00:00
@itemize @bullet
@item
1994-03-28 20:02:03 +00:00
If the keyboard character value is in the range of 0 to 127, it can go
in the string unchanged.
1994-03-08 22:47:20 +00:00
@item
1995-06-06 19:21:15 +00:00
The meta variants of those characters, with codes in the range of
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{27}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**27
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
to
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{27} + 127},
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**27+127,
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
can also go in the string, but you must change their
numeric values. You must set the
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{7}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**7
2000-01-05 15:18:01 +00:00
@end ifnottex
1995-06-06 19:21:15 +00:00
bit instead of the
1998-04-20 17:43:57 +00:00
@tex
1999-09-17 06:59:04 +00:00
@math{2^{27}}
1998-04-20 17:43:57 +00:00
@end tex
2000-01-05 15:18:01 +00:00
@ifnottex
1995-06-06 19:21:15 +00:00
2**27
2000-01-05 15:18:01 +00:00
@end ifnottex
1998-04-20 17:43:57 +00:00
bit, resulting in a value between 128 and 255. Only a unibyte string
can include these codes.
@item
1999-09-17 06:59:04 +00:00
Non-@sc{ascii} characters above 256 can be included in a multibyte string.
1994-03-08 22:47:20 +00:00
@item
Other keyboard character events cannot fit in a string. This includes
keyboard events in the range of 128 to 255.
@end itemize
1998-02-28 01:53:53 +00:00
Functions such as @code{read-key-sequence} that construct strings of
keyboard input characters follow these rules: they construct vectors
1994-03-28 20:02:03 +00:00
instead of strings, when the events won't fit in a string.
1994-03-08 22:47:20 +00:00
When you use the read syntax @samp{\M-} in a string, it produces a
code in the range of 128 to 255---the same code that you get if you
modify the corresponding keyboard event to put it in the string. Thus,
meta events in strings work consistently regardless of how they get into
the strings.
1998-04-20 17:43:57 +00:00
However, most programs would do well to avoid these issues by
following the recommendations at the beginning of this section.
1994-03-28 20:02:03 +00:00
1994-03-08 22:47:20 +00:00
@node Reading Input
@section Reading Input
1998-04-20 17:43:57 +00:00
The editor command loop reads key sequences using the function
1994-03-08 22:47:20 +00:00
@code{read-key-sequence}, which uses @code{read-event}. These and other
1998-04-20 17:43:57 +00:00
functions for event input are also available for use in Lisp programs.
See also @code{momentary-string-display} in @ref{Temporary Displays},
and @code{sit-for} in @ref{Waiting}. @xref{Terminal Input}, for
functions and variables for controlling terminal input modes and
1996-07-23 15:41:53 +00:00
debugging terminal input. @xref{Translating Input}, for features you
can use for translating or modifying input events while reading them.
1994-03-08 22:47:20 +00:00
For higher-level input facilities, see @ref{Minibuffers}.
@menu
* Key Sequence Input:: How to read one key sequence.
* Reading One Event:: How to read just one event.
1999-07-17 02:15:13 +00:00
* Invoking the Input Method:: How reading an event uses the input method.
1994-03-08 22:47:20 +00:00
* Quoted Character Input:: Asking the user to specify a character.
1994-03-28 20:02:03 +00:00
* Event Input Misc:: How to reread or throw away input events.
1994-03-08 22:47:20 +00:00
@end menu
@node Key Sequence Input
@subsection Key Sequence Input
@cindex key sequence input
The command loop reads input a key sequence at a time, by calling
@code{read-key-sequence}. Lisp programs can also call this function;
for example, @code{describe-key} uses it to read the key to describe.
@defun read-key-sequence prompt
@cindex key sequence
This function reads a key sequence and returns it as a string or
1998-02-28 01:53:53 +00:00
vector. It keeps reading events until it has accumulated a complete key
1994-03-08 22:47:20 +00:00
sequence; that is, enough to specify a non-prefix command using the
currently active keymaps.
If the events are all characters and all can fit in a string, then
@code{read-key-sequence} returns a string (@pxref{Strings of Events}).
Otherwise, it returns a vector, since a vector can hold all kinds of
events---characters, symbols, and lists. The elements of the string or
vector are the events in the key sequence.
The argument @var{prompt} is either a string to be displayed in the echo
area as a prompt, or @code{nil}, meaning not to display a prompt.
In the example below, the prompt @samp{?} is displayed in the echo area,
and the user types @kbd{C-x C-f}.
@example
(read-key-sequence "?")
@group
---------- Echo Area ----------
?@kbd{C-x C-f}
---------- Echo Area ----------
@result{} "^X^F"
@end group
@end example
1998-04-20 17:43:57 +00:00
The function @code{read-key-sequence} suppresses quitting: @kbd{C-g}
typed while reading with this function works like any other character,
and does not set @code{quit-flag}. @xref{Quitting}.
@end defun
@defun read-key-sequence-vector prompt
This is like @code{read-key-sequence} except that it always
returns the key sequence as a vector, never as a string.
@xref{Strings of Events}.
1994-03-08 22:47:20 +00:00
@end defun
@cindex upper case key sequence
@cindex downcasing in @code{lookup-key}
1994-05-28 15:27:52 +00:00
If an input character is an upper-case letter and has no key binding,
but its lower-case equivalent has one, then @code{read-key-sequence}
1994-03-08 22:47:20 +00:00
converts the character to lower case. Note that @code{lookup-key} does
not perform case conversion in this way.
The function @code{read-key-sequence} also transforms some mouse events.
It converts unbound drag events into click events, and discards unbound
1995-06-06 19:21:15 +00:00
button-down events entirely. It also reshuffles focus events and
miscellaneous window events so that they never appear in a key sequence
with any other events.
1994-03-08 22:47:20 +00:00
1999-12-03 19:11:12 +00:00
@cindex @code{header-line} prefix key
@cindex @code{mode-line} prefix key
@cindex @code{vertical-line} prefix key
@cindex @code{horizontal-scroll-bar} prefix key
@cindex @code{vertical-scroll-bar} prefix key
@cindex @code{menu-bar} prefix key
@cindex mouse events, in special parts of frame
1994-03-08 22:47:20 +00:00
When mouse events occur in special parts of a window, such as a mode
1994-03-28 20:02:03 +00:00
line or a scroll bar, the event type shows nothing special---it is the
same symbol that would normally represent that combination of mouse
1998-02-28 01:53:53 +00:00
button and modifier keys. The information about the window part is kept
elsewhere in the event---in the coordinates. But
1994-03-28 20:02:03 +00:00
@code{read-key-sequence} translates this information into imaginary
2000-01-12 05:22:30 +00:00
``prefix keys'', all of which are symbols: @code{header-line},
1999-12-03 19:11:12 +00:00
@code{horizontal-scroll-bar}, @code{menu-bar}, @code{mode-line},
@code{vertical-line}, and @code{vertical-scroll-bar}. You can define
meanings for mouse clicks in special window parts by defining key
sequences using these imaginary prefix keys.
1994-03-28 20:02:03 +00:00
1994-03-08 22:47:20 +00:00
For example, if you call @code{read-key-sequence} and then click the
1995-06-06 19:21:15 +00:00
mouse on the window's mode line, you get two events, like this:
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
@example
1994-03-08 22:47:20 +00:00
(read-key-sequence "Click on the mode line: ")
@result{} [mode-line
1994-03-28 20:02:03 +00:00
(mouse-1
(#<window 6 on NEWS> mode-line
(40 . 63) 5959987))]
@end example
1994-03-08 22:47:20 +00:00
1998-02-28 01:53:53 +00:00
@defvar num-input-keys
@c Emacs 19 feature
This variable's value is the number of key sequences processed so far in
this Emacs session. This includes key sequences read from the terminal
and key sequences read from keyboard macros being executed.
@end defvar
@defvar num-nonmacro-input-events
This variable holds the total number of input events received so far
from the terminal---not counting those generated by keyboard macros.
@end defvar
1994-03-08 22:47:20 +00:00
@node Reading One Event
@subsection Reading One Event
1999-12-03 19:11:12 +00:00
@cindex reading a single event
@cindex event, reading only one
1994-03-08 22:47:20 +00:00
1994-05-28 15:27:52 +00:00
The lowest level functions for command input are those that read a
1994-03-08 22:47:20 +00:00
single event.
1999-07-17 02:15:13 +00:00
@defun read-event &optional prompt inherit-input-method
1994-03-08 22:47:20 +00:00
This function reads and returns the next event of command input, waiting
if necessary until an event is available. Events can come directly from
the user or from a keyboard macro.
1999-07-17 02:15:13 +00:00
If the optional argument @var{prompt} is non-@code{nil}, it should be a
string to display in the echo area as a prompt. Otherwise,
@code{read-event} does not display any message to indicate it is waiting
for input; instead, it prompts by echoing: it displays descriptions of
the events that led to or were read by the current command. @xref{The
Echo Area}.
1994-03-08 22:47:20 +00:00
1999-07-17 02:15:13 +00:00
If @var{inherit-input-method} is non-@code{nil}, then the current input
method (if any) is employed to make it possible to enter a
1999-09-17 06:59:04 +00:00
non-@sc{ascii} character. Otherwise, input method handling is disabled
1999-07-17 02:15:13 +00:00
for reading this event.
1998-08-26 21:05:38 +00:00
1994-03-08 22:47:20 +00:00
If @code{cursor-in-echo-area} is non-@code{nil}, then @code{read-event}
moves the cursor temporarily to the echo area, to the end of any message
displayed there. Otherwise @code{read-event} does not move the cursor.
1998-05-26 18:56:56 +00:00
If @code{read-event} gets an event that is defined as a help character, in
1998-05-19 03:45:57 +00:00
some cases @code{read-event} processes the event directly without
returning. @xref{Help Functions}. Certain other events, called
@dfn{special events}, are also processed directly within
@code{read-event} (@pxref{Special Events}).
1994-03-08 22:47:20 +00:00
Here is what happens if you call @code{read-event} and then press the
right-arrow function key:
@example
@group
(read-event)
@result{} right
@end group
@end example
1994-03-28 20:02:03 +00:00
@end defun
1994-03-08 22:47:20 +00:00
1999-07-17 02:15:13 +00:00
@defun read-char &optional prompt inherit-input-method
This function reads and returns a character of command input. If the
user generates an event which is not a character (i.e. a mouse click or
function key event), @code{read-char} signals an error. The arguments
work as in @code{read-event}.
1994-03-08 22:47:20 +00:00
1999-09-17 06:59:04 +00:00
In the first example, the user types the character @kbd{1} (@sc{ascii}
1994-03-28 20:02:03 +00:00
code 49). The second example shows a keyboard macro definition that
calls @code{read-char} from the minibuffer using @code{eval-expression}.
@code{read-char} reads the keyboard macro's very next character, which
is @kbd{1}. Then @code{eval-expression} displays its return value in
the echo area.
1994-03-08 22:47:20 +00:00
@example
@group
(read-char)
@result{} 49
@end group
@group
1995-06-06 19:21:15 +00:00
;; @r{We assume here you use @kbd{M-:} to evaluate this.}
1994-03-08 22:47:20 +00:00
(symbol-function 'foo)
1995-06-06 19:21:15 +00:00
@result{} "^[:(read-char)^M1"
1994-03-08 22:47:20 +00:00
@end group
@group
1994-03-28 20:02:03 +00:00
(execute-kbd-macro 'foo)
1994-03-08 22:47:20 +00:00
@print{} 49
@result{} nil
@end group
@end example
@end defun
1999-07-17 02:15:13 +00:00
@defun read-char-exclusive &optional prompt inherit-input-method
This function reads and returns a character of command input. If the
user generates an event which is not a character,
@code{read-char-exclusive} ignores it and reads another event, until it
gets a character. The arguments work as in @code{read-event}.
@end defun
@node Invoking the Input Method
@subsection Invoking the Input Method
The event-reading functions invoke the current input method, if any
(@pxref{Input Methods}). If the value of @code{input-method-function}
is non-@code{nil}, it should be a function; when @code{read-event} reads
a printing character (including @key{SPC}) with no modifier bits, it
calls that function, passing the character as an argument.
1998-07-29 03:01:39 +00:00
@defvar input-method-function
If this is non-@code{nil}, its value specifies the current input method
function.
1998-08-26 21:05:38 +00:00
@strong{Note:} Don't bind this variable with @code{let}. It is often
buffer-local, and if you bind it around reading input (which is exactly
when you @emph{would} bind it), switching buffers asynchronously while
Emacs is waiting will cause the value to be restored in the wrong
buffer.
1998-07-29 03:01:39 +00:00
@end defvar
The input method function should return a list of events which should
be used as input. (If the list is @code{nil}, that means there is no
input, so @code{read-event} waits for another event.) These events are
1999-12-03 19:11:12 +00:00
processed before the events in @code{unread-command-events}
(@pxref{Event Input Misc}). Events
1998-07-29 03:01:39 +00:00
returned by the input method function are not passed to the input method
function again, even if they are printing characters with no modifier
bits.
If the input method function calls @code{read-event} or
@code{read-key-sequence}, it should bind @code{input-method-function} to
@code{nil} first, to prevent recursion.
The input method function is not called when reading the second and
1999-07-17 02:15:13 +00:00
subsequent events of a key sequence. Thus, these characters are not
subject to input method processing. The input method function should
test the values of @code{overriding-local-map} and
@code{overriding-terminal-local-map}; if either of these variables is
non-@code{nil}, the input method should put its argument into a list and
return that list with no further processing.
1998-07-29 03:01:39 +00:00
1994-03-08 22:47:20 +00:00
@node Quoted Character Input
@subsection Quoted Character Input
@cindex quoted character input
1994-05-28 15:27:52 +00:00
You can use the function @code{read-quoted-char} to ask the user to
specify a character, and allow the user to specify a control or meta
character conveniently, either literally or as an octal character code.
The command @code{quoted-insert} uses this function.
1994-03-08 22:47:20 +00:00
@defun read-quoted-char &optional prompt
@cindex octal character input
@cindex control characters, reading
@cindex nonprinting characters, reading
This function is like @code{read-char}, except that if the first
1998-04-20 17:43:57 +00:00
character read is an octal digit (0-7), it reads any number of octal
digits (but stopping if a non-octal digit is found), and returns the
character represented by that numeric character code.
1994-03-08 22:47:20 +00:00
Quitting is suppressed when the first character is read, so that the
user can enter a @kbd{C-g}. @xref{Quitting}.
If @var{prompt} is supplied, it specifies a string for prompting the
1994-03-28 20:02:03 +00:00
user. The prompt string is always displayed in the echo area, followed
1994-03-08 22:47:20 +00:00
by a single @samp{-}.
In the following example, the user types in the octal number 177 (which
is 127 in decimal).
@example
(read-quoted-char "What character")
@group
---------- Echo Area ----------
What character-@kbd{177}
---------- Echo Area ----------
@result{} 127
@end group
@end example
@end defun
1994-05-28 15:27:52 +00:00
@need 2000
1994-03-28 20:02:03 +00:00
@node Event Input Misc
@subsection Miscellaneous Event Input Features
This section describes how to ``peek ahead'' at events without using
them up, how to check for pending input, and how to discard pending
1998-05-26 18:56:56 +00:00
input. See also the function @code{read-passwd} (@pxref{Reading a
Password}).
1994-03-08 22:47:20 +00:00
@defvar unread-command-events
@cindex next input
@cindex peeking at input
This variable holds a list of events waiting to be read as command
1994-03-28 20:02:03 +00:00
input. The events are used in the order they appear in the list, and
removed one by one as they are used.
1994-03-08 22:47:20 +00:00
1998-02-28 01:53:53 +00:00
The variable is needed because in some cases a function reads an event
1994-03-28 20:02:03 +00:00
and then decides not to use it. Storing the event in this variable
causes it to be processed normally, by the command loop or by the
functions to read command input.
1994-03-08 22:47:20 +00:00
@cindex prefix argument unreading
For example, the function that implements numeric prefix arguments reads
any number of digits. When it finds a non-digit event, it must unread
the event so that it can be read normally by the command loop.
2003-02-04 14:56:31 +00:00
Likewise, incremental search uses this feature to unread events with no
1994-03-28 20:02:03 +00:00
special meaning in a search, because these events should exit the search
and then execute normally.
1994-05-28 15:27:52 +00:00
The reliable and easy way to extract events from a key sequence so as to
put them in @code{unread-command-events} is to use
1994-03-28 20:02:03 +00:00
@code{listify-key-sequence} (@pxref{Strings of Events}).
1998-02-28 01:53:53 +00:00
Normally you add events to the front of this list, so that the events
most recently unread will be reread first.
1994-03-08 22:47:20 +00:00
@end defvar
1998-04-20 17:43:57 +00:00
@defun listify-key-sequence key
This function converts the string or vector @var{key} to a list of
individual events, which you can put in @code{unread-command-events}.
@end defun
1994-03-08 22:47:20 +00:00
@defvar unread-command-char
This variable holds a character to be read as command input.
A value of -1 means ``empty''.
1994-03-28 20:02:03 +00:00
This variable is mostly obsolete now that you can use
1994-03-08 22:47:20 +00:00
@code{unread-command-events} instead; it exists only to support programs
written for Emacs versions 18 and earlier.
@end defvar
@defun input-pending-p
@cindex waiting for command key input
This function determines whether any command input is currently
available to be read. It returns immediately, with value @code{t} if
1994-03-28 20:02:03 +00:00
there is available input, @code{nil} otherwise. On rare occasions it
may return @code{t} when no input is available.
1994-03-08 22:47:20 +00:00
@end defun
@defvar last-input-event
1998-04-20 17:43:57 +00:00
@defvarx last-input-char
1994-03-28 20:02:03 +00:00
This variable records the last terminal input event read, whether
1994-03-08 22:47:20 +00:00
as part of a command or explicitly by a Lisp program.
1994-03-28 20:02:03 +00:00
In the example below, the Lisp program reads the character @kbd{1},
1999-09-17 06:59:04 +00:00
@sc{ascii} code 49. It becomes the value of @code{last-input-event},
1995-06-06 19:21:15 +00:00
while @kbd{C-e} (we assume @kbd{C-x C-e} command is used to evaluate
this expression) remains the value of @code{last-command-event}.
1994-03-08 22:47:20 +00:00
@example
@group
(progn (print (read-char))
1994-03-28 20:02:03 +00:00
(print last-command-event)
last-input-event)
1994-03-08 22:47:20 +00:00
@print{} 49
@print{} 5
@result{} 49
@end group
@end example
The alias @code{last-input-char} exists for compatibility with
Emacs version 18.
@end defvar
@defun discard-input
@cindex flush input
@cindex discard input
@cindex terminate keyboard macro
This function discards the contents of the terminal input buffer and
cancels any keyboard macro that might be in the process of definition.
It returns @code{nil}.
In the following example, the user may type a number of characters right
after starting the evaluation of the form. After the @code{sleep-for}
2003-02-04 14:56:31 +00:00
finishes sleeping, @code{discard-input} discards any characters typed
1994-03-28 20:02:03 +00:00
during the sleep.
1994-03-08 22:47:20 +00:00
@example
(progn (sleep-for 2)
1994-03-28 20:02:03 +00:00
(discard-input))
1994-03-08 22:47:20 +00:00
@result{} nil
@end example
@end defun
1998-02-28 01:53:53 +00:00
@node Special Events
@section Special Events
@cindex special events
Special events are handled at a very low level---as soon as they are
read. The @code{read-event} function processes these events itself, and
never returns them.
Events that are handled in this way do not echo, they are never grouped
into key sequences, and they never appear in the value of
@code{last-command-event} or @code{(this-command-keys)}. They do not
discard a numeric argument, they cannot be unread with
@code{unread-command-events}, they may not appear in a keyboard macro,
and they are not recorded in a keyboard macro while you are defining
one.
These events do, however, appear in @code{last-input-event} immediately
after they are read, and this is the way for the event's definition to
find the actual event.
The events types @code{iconify-frame}, @code{make-frame-visible} and
@code{delete-frame} are normally handled in this way. The keymap which
defines how to handle special events---and which events are special---is
in the variable @code{special-event-map} (@pxref{Active Keymaps}).
1994-03-08 22:47:20 +00:00
@node Waiting
@section Waiting for Elapsed Time or Input
@cindex pausing
@cindex waiting
1994-03-28 20:02:03 +00:00
The wait functions are designed to wait for a certain amount of time
to pass or until there is input. For example, you may wish to pause in
the middle of a computation to allow the user time to view the display.
@code{sit-for} pauses and updates the screen, and returns immediately if
input comes in, while @code{sleep-for} pauses without updating the
screen.
1994-03-08 22:47:20 +00:00
@defun sit-for seconds &optional millisec nodisp
This function performs redisplay (provided there is no pending input
from the user), then waits @var{seconds} seconds, or until input is
1994-03-28 20:02:03 +00:00
available. The value is @code{t} if @code{sit-for} waited the full
2003-02-04 14:56:31 +00:00
time with no input arriving (see @code{input-pending-p} in @ref{Event
1994-03-28 20:02:03 +00:00
Input Misc}). Otherwise, the value is @code{nil}.
1994-03-08 22:47:20 +00:00
1995-06-06 19:21:15 +00:00
The argument @var{seconds} need not be an integer. If it is a floating
point number, @code{sit-for} waits for a fractional number of seconds.
Some systems support only a whole number of seconds; on these systems,
@var{seconds} is rounded down.
1994-03-08 22:47:20 +00:00
The optional argument @var{millisec} specifies an additional waiting
period measured in milliseconds. This adds to the period specified by
1995-06-06 19:21:15 +00:00
@var{seconds}. If the system doesn't support waiting fractions of a
second, you get an error if you specify nonzero @var{millisec}.
1994-03-08 22:47:20 +00:00
1999-09-17 06:59:04 +00:00
The expression @code{(sit-for 0)} is a convenient way to request a
redisplay, without any delay. @xref{Forcing Redisplay}.
1994-03-08 22:47:20 +00:00
If @var{nodisp} is non-@code{nil}, then @code{sit-for} does not
redisplay, but it still returns as soon as input is available (or when
the timeout elapses).
1995-06-05 12:23:13 +00:00
Iconifying or deiconifying a frame makes @code{sit-for} return, because
that generates an event. @xref{Misc Events}.
1994-03-08 22:47:20 +00:00
The usual purpose of @code{sit-for} is to give the user time to read
text that you display.
@end defun
@defun sleep-for seconds &optional millisec
This function simply pauses for @var{seconds} seconds without updating
the display. It pays no attention to available input. It returns
@code{nil}.
1995-06-06 19:21:15 +00:00
The argument @var{seconds} need not be an integer. If it is a floating
point number, @code{sleep-for} waits for a fractional number of seconds.
Some systems support only a whole number of seconds; on these systems,
@var{seconds} is rounded down.
1994-03-08 22:47:20 +00:00
The optional argument @var{millisec} specifies an additional waiting
period measured in milliseconds. This adds to the period specified by
1995-06-06 19:21:15 +00:00
@var{seconds}. If the system doesn't support waiting fractions of a
second, you get an error if you specify nonzero @var{millisec}.
1994-03-08 22:47:20 +00:00
Use @code{sleep-for} when you wish to guarantee a delay.
@end defun
@xref{Time of Day}, for functions to get the current time.
@node Quitting
@section Quitting
@cindex @kbd{C-g}
@cindex quitting
@cindex interrupt Lisp functions
1994-03-08 22:47:20 +00:00
1994-05-28 15:27:52 +00:00
Typing @kbd{C-g} while a Lisp function is running causes Emacs to
@dfn{quit} whatever it is doing. This means that control returns to the
innermost active command loop.
1994-03-08 22:47:20 +00:00
Typing @kbd{C-g} while the command loop is waiting for keyboard input
does not cause a quit; it acts as an ordinary input character. In the
simplest case, you cannot tell the difference, because @kbd{C-g}
normally runs the command @code{keyboard-quit}, whose effect is to quit.
1998-04-20 17:43:57 +00:00
However, when @kbd{C-g} follows a prefix key, they combine to form an
undefined key. The effect is to cancel the prefix key as well as any
prefix argument.
1994-03-08 22:47:20 +00:00
In the minibuffer, @kbd{C-g} has a different definition: it aborts out
of the minibuffer. This means, in effect, that it exits the minibuffer
and then quits. (Simply quitting would return to the command loop
@emph{within} the minibuffer.) The reason why @kbd{C-g} does not quit
directly when the command reader is reading input is so that its meaning
can be redefined in the minibuffer in this way. @kbd{C-g} following a
prefix key is not redefined in the minibuffer, and it has its normal
effect of canceling the prefix key and prefix argument. This too
1994-03-28 20:02:03 +00:00
would not be possible if @kbd{C-g} always quit directly.
1994-03-08 22:47:20 +00:00
1994-05-28 15:27:52 +00:00
When @kbd{C-g} does directly quit, it does so by setting the variable
1994-03-28 20:02:03 +00:00
@code{quit-flag} to @code{t}. Emacs checks this variable at appropriate
times and quits if it is not @code{nil}. Setting @code{quit-flag}
1994-03-08 22:47:20 +00:00
non-@code{nil} in any way thus causes a quit.
1994-03-28 20:02:03 +00:00
At the level of C code, quitting cannot happen just anywhere; only at the
1994-05-28 15:27:52 +00:00
special places that check @code{quit-flag}. The reason for this is
1994-03-08 22:47:20 +00:00
that quitting at other places might leave an inconsistency in Emacs's
2003-02-04 14:56:31 +00:00
internal state. Because quitting is delayed until a safe place, quitting
1994-03-08 22:47:20 +00:00
cannot make Emacs crash.
Certain functions such as @code{read-key-sequence} or
@code{read-quoted-char} prevent quitting entirely even though they wait
for input. Instead of quitting, @kbd{C-g} serves as the requested
input. In the case of @code{read-key-sequence}, this serves to bring
about the special behavior of @kbd{C-g} in the command loop. In the
case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used
2003-02-04 14:56:31 +00:00
to quote a @kbd{C-g}.
1994-03-08 22:47:20 +00:00
@cindex prevent quitting
1994-03-08 22:47:20 +00:00
You can prevent quitting for a portion of a Lisp function by binding
the variable @code{inhibit-quit} to a non-@code{nil} value. Then,
although @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, the
usual result of this---a quit---is prevented. Eventually,
@code{inhibit-quit} will become @code{nil} again, such as when its
binding is unwound at the end of a @code{let} form. At that time, if
@code{quit-flag} is still non-@code{nil}, the requested quit happens
1994-05-28 15:27:52 +00:00
immediately. This behavior is ideal when you wish to make sure that
quitting does not happen within a ``critical section'' of the program.
1994-03-08 22:47:20 +00:00
@cindex @code{read-quoted-char} quitting
In some functions (such as @code{read-quoted-char}), @kbd{C-g} is
1994-05-28 15:27:52 +00:00
handled in a special way that does not involve quitting. This is done
1994-03-28 20:02:03 +00:00
by reading the input with @code{inhibit-quit} bound to @code{t}, and
1994-03-08 22:47:20 +00:00
setting @code{quit-flag} to @code{nil} before @code{inhibit-quit}
becomes @code{nil} again. This excerpt from the definition of
@code{read-quoted-char} shows how this is done; it also shows that
normal quitting is permitted after the first character of input.
@example
(defun read-quoted-char (&optional prompt)
"@dots{}@var{documentation}@dots{}"
1998-04-20 17:43:57 +00:00
(let ((message-log-max nil) done (first t) (code 0) char)
(while (not done)
(let ((inhibit-quit first)
@dots{})
(and prompt (message "%s-" prompt))
(setq char (read-event))
(if inhibit-quit (setq quit-flag nil)))
@r{@dots{}set the variable @code{code}@dots{}})
code))
1994-03-08 22:47:20 +00:00
@end example
@defvar quit-flag
1994-03-28 20:02:03 +00:00
If this variable is non-@code{nil}, then Emacs quits immediately, unless
@code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} ordinarily sets
1994-03-08 22:47:20 +00:00
@code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}.
@end defvar
@defvar inhibit-quit
This variable determines whether Emacs should quit when @code{quit-flag}
is set to a value other than @code{nil}. If @code{inhibit-quit} is
non-@code{nil}, then @code{quit-flag} has no special effect.
@end defvar
@deffn Command keyboard-quit
This function signals the @code{quit} condition with @code{(signal 'quit
nil)}. This is the same thing that quitting does. (See @code{signal}
in @ref{Errors}.)
@end deffn
You can specify a character other than @kbd{C-g} to use for quitting.
See the function @code{set-input-mode} in @ref{Terminal Input}.
2003-02-04 14:56:31 +00:00
1994-03-08 22:47:20 +00:00
@node Prefix Command Arguments
@section Prefix Command Arguments
@cindex prefix argument
@cindex raw prefix argument
@cindex numeric prefix argument
Most Emacs commands can use a @dfn{prefix argument}, a number
specified before the command itself. (Don't confuse prefix arguments
1994-05-28 15:27:52 +00:00
with prefix keys.) The prefix argument is at all times represented by a
value, which may be @code{nil}, meaning there is currently no prefix
argument. Each command may use the prefix argument or ignore it.
1994-03-08 22:47:20 +00:00
There are two representations of the prefix argument: @dfn{raw} and
@dfn{numeric}. The editor command loop uses the raw representation
internally, and so do the Lisp variables that store the information, but
commands can request either representation.
Here are the possible values of a raw prefix argument:
@itemize @bullet
@item
@code{nil}, meaning there is no prefix argument. Its numeric value is
1, but numerous commands make a distinction between @code{nil} and the
integer 1.
@item
An integer, which stands for itself.
@item
A list of one element, which is an integer. This form of prefix
argument results from one or a succession of @kbd{C-u}'s with no
digits. The numeric value is the integer in the list, but some
commands make a distinction between such a list and an integer alone.
@item
The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was
typed, without following digits. The equivalent numeric value is
@minus{}1, but some commands make a distinction between the integer
@minus{}1 and the symbol @code{-}.
@end itemize
1994-03-28 20:02:03 +00:00
We illustrate these possibilities by calling the following function with
various prefixes:
1994-03-08 22:47:20 +00:00
@example
@group
(defun display-prefix (arg)
"Display the value of the raw prefix arg."
(interactive "P")
(message "%s" arg))
@end group
@end example
@noindent
Here are the results of calling @code{display-prefix} with various
raw prefix arguments:
@example
M-x display-prefix @print{} nil
C-u M-x display-prefix @print{} (4)
C-u C-u M-x display-prefix @print{} (16)
C-u 3 M-x display-prefix @print{} 3
M-3 M-x display-prefix @print{} 3 ; @r{(Same as @code{C-u 3}.)}
2003-02-04 14:56:31 +00:00
C-u - M-x display-prefix @print{} -
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
M-- M-x display-prefix @print{} - ; @r{(Same as @code{C-u -}.)}
1994-03-08 22:47:20 +00:00
2003-02-04 14:56:31 +00:00
C-u - 7 M-x display-prefix @print{} -7
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
M-- 7 M-x display-prefix @print{} -7 ; @r{(Same as @code{C-u -7}.)}
1994-03-08 22:47:20 +00:00
@end example
Emacs uses two variables to store the prefix argument:
@code{prefix-arg} and @code{current-prefix-arg}. Commands such as
@code{universal-argument} that set up prefix arguments for other
commands store them in @code{prefix-arg}. In contrast,
@code{current-prefix-arg} conveys the prefix argument to the current
command, so setting it has no effect on the prefix arguments for future
commands.
Normally, commands specify which representation to use for the prefix
argument, either numeric or raw, in the @code{interactive} declaration.
1994-05-28 15:27:52 +00:00
(@xref{Using Interactive}.) Alternatively, functions may look at the
1994-03-08 22:47:20 +00:00
value of the prefix argument directly in the variable
@code{current-prefix-arg}, but this is less clean.
1994-03-28 20:02:03 +00:00
@defun prefix-numeric-value arg
This function returns the numeric meaning of a valid raw prefix argument
value, @var{arg}. The argument may be a symbol, a number, or a list.
1994-05-28 15:27:52 +00:00
If it is @code{nil}, the value 1 is returned; if it is @code{-}, the
value @minus{}1 is returned; if it is a number, that number is returned;
if it is a list, the @sc{car} of that list (which should be a number) is
returned.
1994-03-28 20:02:03 +00:00
@end defun
@defvar current-prefix-arg
This variable holds the raw prefix argument for the @emph{current}
1996-12-20 00:09:23 +00:00
command. Commands may examine it directly, but the usual method for
accessing it is with @code{(interactive "P")}.
1994-03-28 20:02:03 +00:00
@end defvar
@defvar prefix-arg
The value of this variable is the raw prefix argument for the
1998-02-28 01:53:53 +00:00
@emph{next} editing command. Commands such as @code{universal-argument}
that specify prefix arguments for the following command work by setting
this variable.
1998-06-11 20:20:07 +00:00
@end defvar
@defvar last-prefix-arg
The raw prefix argument value used by the previous command.
1994-03-28 20:02:03 +00:00
@end defvar
1998-02-28 01:53:53 +00:00
The following commands exist to set up prefix arguments for the
following command. Do not call them for any other reason.
1994-03-08 22:47:20 +00:00
@deffn Command universal-argument
This command reads input and specifies a prefix argument for the
following command. Don't call this command yourself unless you know
what you are doing.
@end deffn
@deffn Command digit-argument arg
This command adds to the prefix argument for the following command. The
argument @var{arg} is the raw prefix argument as it was before this
command; it is used to compute the updated prefix argument. Don't call
this command yourself unless you know what you are doing.
@end deffn
@deffn Command negative-argument arg
This command adds to the numeric argument for the next command. The
argument @var{arg} is the raw prefix argument as it was before this
command; its value is negated to form the new prefix argument. Don't
call this command yourself unless you know what you are doing.
@end deffn
@node Recursive Editing
@section Recursive Editing
@cindex recursive command loop
@cindex recursive editing level
@cindex command loop, recursive
1994-03-28 20:02:03 +00:00
The Emacs command loop is entered automatically when Emacs starts up.
This top-level invocation of the command loop never exits; it keeps
running as long as Emacs does. Lisp programs can also invoke the
command loop. Since this makes more than one activation of the command
loop, we call it @dfn{recursive editing}. A recursive editing level has
the effect of suspending whatever command invoked it and permitting the
user to do arbitrary editing before resuming that command.
1994-03-08 22:47:20 +00:00
The commands available during recursive editing are the same ones
available in the top-level editing loop and defined in the keymaps.
Only a few special commands exit the recursive editing level; the others
1994-03-28 20:02:03 +00:00
return to the recursive editing level when they finish. (The special
commands for exiting are always available, but they do nothing when
recursive editing is not in progress.)
1994-03-08 22:47:20 +00:00
All command loops, including recursive ones, set up all-purpose error
handlers so that an error in a command run from the command loop will
not exit the loop.
@cindex minibuffer input
Minibuffer input is a special kind of recursive editing. It has a few
special wrinkles, such as enabling display of the minibuffer and the
minibuffer window, but fewer than you might suppose. Certain keys
behave differently in the minibuffer, but that is only because of the
minibuffer's local map; if you switch windows, you get the usual Emacs
commands.
@cindex @code{throw} example
@kindex exit
@cindex exit recursive editing
@cindex aborting
To invoke a recursive editing level, call the function
@code{recursive-edit}. This function contains the command loop; it also
contains a call to @code{catch} with tag @code{exit}, which makes it
possible to exit the recursive editing level by throwing to @code{exit}
(@pxref{Catch and Throw}). If you throw a value other than @code{t},
then @code{recursive-edit} returns normally to the function that called
it. The command @kbd{C-M-c} (@code{exit-recursive-edit}) does this.
Throwing a @code{t} value causes @code{recursive-edit} to quit, so that
control returns to the command loop one level up. This is called
@dfn{aborting}, and is done by @kbd{C-]} (@code{abort-recursive-edit}).
Most applications should not use recursive editing, except as part of
using the minibuffer. Usually it is more convenient for the user if you
change the major mode of the current buffer temporarily to a special
1994-05-28 15:27:52 +00:00
major mode, which should have a command to go back to the previous mode.
(The @kbd{e} command in Rmail uses this technique.) Or, if you wish to
give the user different text to edit ``recursively'', create and select
a new buffer in a special mode. In this mode, define a command to
complete the processing and go back to the previous buffer. (The
@kbd{m} command in Rmail does this.)
1994-03-08 22:47:20 +00:00
Recursive edits are useful in debugging. You can insert a call to
@code{debug} into a function definition as a sort of breakpoint, so that
you can look around when the function gets there. @code{debug} invokes
a recursive edit but also provides the other features of the debugger.
Recursive editing levels are also used when you type @kbd{C-r} in
@code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}).
@defun recursive-edit
@cindex suspend evaluation
This function invokes the editor command loop. It is called
automatically by the initialization of Emacs, to let the user begin
editing. When called from a Lisp program, it enters a recursive editing
level.
In the following example, the function @code{simple-rec} first
advances point one word, then enters a recursive edit, printing out a
message in the echo area. The user can then do any editing desired, and
then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}.
@example
(defun simple-rec ()
(forward-word 1)
1994-03-28 20:02:03 +00:00
(message "Recursive edit in progress")
1994-03-08 22:47:20 +00:00
(recursive-edit)
(forward-word 1))
@result{} simple-rec
(simple-rec)
@result{} nil
@end example
@end defun
@deffn Command exit-recursive-edit
This function exits from the innermost recursive edit (including
minibuffer input). Its definition is effectively @code{(throw 'exit
2003-02-04 14:56:31 +00:00
nil)}.
1994-03-08 22:47:20 +00:00
@end deffn
@deffn Command abort-recursive-edit
This function aborts the command that requested the innermost recursive
2003-02-04 14:56:31 +00:00
edit (including minibuffer input), by signaling @code{quit}
1994-03-08 22:47:20 +00:00
after exiting the recursive edit. Its definition is effectively
@code{(throw 'exit t)}. @xref{Quitting}.
@end deffn
@deffn Command top-level
This function exits all recursive editing levels; it does not return a
value, as it jumps completely out of any computation directly back to
the main command loop.
@end deffn
@defun recursion-depth
This function returns the current depth of recursive edits. When no
recursive edit is active, it returns 0.
@end defun
@node Disabling Commands
@section Disabling Commands
@cindex disabled command
@dfn{Disabling a command} marks the command as requiring user
confirmation before it can be executed. Disabling is used for commands
which might be confusing to beginning users, to prevent them from using
the commands by accident.
@kindex disabled
The low-level mechanism for disabling a command is to put a
non-@code{nil} @code{disabled} property on the Lisp symbol for the
command. These properties are normally set up by the user's
1999-10-05 23:26:05 +00:00
init file (@pxref{Init File}) with Lisp expressions such as this:
1994-03-08 22:47:20 +00:00
@example
(put 'upcase-region 'disabled t)
@end example
@noindent
1999-10-05 23:26:05 +00:00
For a few commands, these properties are present by default (you can
remove them in your init file if you wish).
1994-03-08 22:47:20 +00:00
1994-03-28 20:02:03 +00:00
If the value of the @code{disabled} property is a string, the message
saying the command is disabled includes that string. For example:
1994-03-08 22:47:20 +00:00
@example
(put 'delete-region 'disabled
"Text deleted this way cannot be yanked back!\n")
@end example
@xref{Disabling,,, emacs, The GNU Emacs Manual}, for the details on
what happens when a disabled command is invoked interactively.
Disabling a command has no effect on calling it as a function from Lisp
programs.
@deffn Command enable-command command
Allow @var{command} to be executed without special confirmation from now
1999-10-05 23:26:05 +00:00
on, and (if the user confirms) alter the user's init file (@pxref{Init
File}) so that this will apply to future sessions.
1994-03-08 22:47:20 +00:00
@end deffn
@deffn Command disable-command command
1994-03-28 20:02:03 +00:00
Require special confirmation to execute @var{command} from now on, and
1999-10-05 23:26:05 +00:00
(if the user confirms) alter the user's init file so that this
1994-05-28 15:27:52 +00:00
will apply to future sessions.
1994-03-08 22:47:20 +00:00
@end deffn
@defvar disabled-command-hook
1998-02-28 01:53:53 +00:00
When the user invokes a disabled command interactively, this normal hook
is run instead of the disabled command. The hook functions can use
1994-03-28 20:02:03 +00:00
@code{this-command-keys} to determine what the user typed to run the
1994-05-28 15:27:52 +00:00
command, and thus find the command itself. @xref{Hooks}.
1994-03-08 22:47:20 +00:00
By default, @code{disabled-command-hook} contains a function that asks
the user whether to proceed.
@end defvar
@node Command History
@section Command History
@cindex command history
@cindex complex command
@cindex history of commands
The command loop keeps a history of the complex commands that have
been executed, to make it convenient to repeat these commands. A
@dfn{complex command} is one for which the interactive argument reading
uses the minibuffer. This includes any @kbd{M-x} command, any
1995-06-06 19:21:15 +00:00
@kbd{M-:} command, and any command whose @code{interactive}
1994-03-08 22:47:20 +00:00
specification reads an argument from the minibuffer. Explicit use of
the minibuffer during the execution of the command itself does not cause
the command to be considered complex.
@defvar command-history
This variable's value is a list of recent complex commands, each
represented as a form to evaluate. It continues to accumulate all
1998-05-19 03:45:57 +00:00
complex commands for the duration of the editing session, but when it
2002-12-07 21:42:43 +00:00
reaches the maximum size (@pxref{Minibuffer History}), the oldest
elements are deleted as new ones are added.
1994-03-08 22:47:20 +00:00
@example
@group
command-history
@result{} ((switch-to-buffer "chistory.texi")
(describe-key "^X^[")
(visit-tags-table "~/emacs/src/")
(find-tag "repeat-complex-command"))
@end group
@end example
@end defvar
This history list is actually a special case of minibuffer history
(@pxref{Minibuffer History}), with one special twist: the elements are
expressions rather than strings.
There are a number of commands devoted to the editing and recall of
previous commands. The commands @code{repeat-complex-command}, and
@code{list-command-history} are described in the user manual
(@pxref{Repetition,,, emacs, The GNU Emacs Manual}). Within the
1998-02-28 01:53:53 +00:00
minibuffer, the usual minibuffer history commands are available.
1994-03-08 22:47:20 +00:00
@node Keyboard Macros
@section Keyboard Macros
@cindex keyboard macros
A @dfn{keyboard macro} is a canned sequence of input events that can
1994-03-28 20:02:03 +00:00
be considered a command and made the definition of a key. The Lisp
representation of a keyboard macro is a string or vector containing the
events. Don't confuse keyboard macros with Lisp macros
(@pxref{Macros}).
1994-03-08 22:47:20 +00:00
1998-02-28 01:53:53 +00:00
@defun execute-kbd-macro kbdmacro &optional count
This function executes @var{kbdmacro} as a sequence of events. If
@var{kbdmacro} is a string or vector, then the events in it are executed
1994-03-08 22:47:20 +00:00
exactly as if they had been input by the user. The sequence is
@emph{not} expected to be a single key sequence; normally a keyboard
macro definition consists of several key sequences concatenated.
1998-02-28 01:53:53 +00:00
If @var{kbdmacro} is a symbol, then its function definition is used in
place of @var{kbdmacro}. If that is another symbol, this process repeats.
1994-03-08 22:47:20 +00:00
Eventually the result should be a string or vector. If the result is
not a symbol, string, or vector, an error is signaled.
1998-02-28 01:53:53 +00:00
The argument @var{count} is a repeat count; @var{kbdmacro} is executed that
many times. If @var{count} is omitted or @code{nil}, @var{kbdmacro} is
executed once. If it is 0, @var{kbdmacro} is executed over and over until it
2003-02-04 14:56:31 +00:00
encounters an error or a failing search.
1998-02-28 01:53:53 +00:00
@xref{Reading One Event}, for an example of using @code{execute-kbd-macro}.
1994-03-08 22:47:20 +00:00
@end defun
@defvar executing-macro
This variable contains the string or vector that defines the keyboard
macro that is currently executing. It is @code{nil} if no macro is
1998-02-28 01:53:53 +00:00
currently executing. A command can test this variable so as to behave
1994-03-28 20:02:03 +00:00
differently when run from an executing macro. Do not set this variable
yourself.
1994-03-08 22:47:20 +00:00
@end defvar
@defvar defining-kbd-macro
1994-03-28 20:02:03 +00:00
This variable indicates whether a keyboard macro is being defined. A
1998-02-28 01:53:53 +00:00
command can test this variable so as to behave differently while a macro
is being defined. The commands @code{start-kbd-macro} and
1994-03-28 20:02:03 +00:00
@code{end-kbd-macro} set this variable---do not set it yourself.
1995-06-05 12:23:13 +00:00
1995-06-06 19:21:15 +00:00
The variable is always local to the current terminal and cannot be
buffer-local. @xref{Multiple Displays}.
@end defvar
@defvar last-kbd-macro
This variable is the definition of the most recently defined keyboard
macro. Its value is a string or vector, or @code{nil}.
The variable is always local to the current terminal and cannot be
1995-06-05 12:23:13 +00:00
buffer-local. @xref{Multiple Displays}.
1994-03-08 22:47:20 +00:00
@end defvar
@defvar kbd-macro-termination-hook
This normal hook (@pxref{Standard Hooks}) is run when a keyboard
macro terminates, regardless of what caused it to terminate (reaching
the macro end or an error which ended the macro prematurely).
@end defvar