This is elisp, produced by makeinfo version 4.0f from ./elisp.texi.
INFO-DIR-SECTION Editors
START-INFO-DIR-ENTRY
* Elisp: (elisp). The Emacs Lisp Reference Manual.
END-INFO-DIR-ENTRY
This Info file contains edition 2.8 of the GNU Emacs Lisp Reference
Manual, corresponding to Emacs version 21.2.
Published by the Free Software Foundation 59 Temple Place, Suite 330
Boston, MA 02111-1307 USA
Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
2000, 2001, 2002 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Copying", with the Front-Cover texts being "A
GNU Manual", and with the Back-Cover Texts as in (a) below. A copy of
the license is included in the section entitled "GNU Free Documentation
License".
(a) The FSF's Back-Cover Text is: "You have freedom to copy and
modify this GNU Manual, like GNU software. Copies published by the Free
Software Foundation raise funds for GNU development."
File: elisp, Node: Repeat Events, Next: Motion Events, Prev: Button-Down Events, Up: Input Events
Repeat Events
-------------
If you press the same mouse button more than once in quick succession
without moving the mouse, Emacs generates special "repeat" mouse events
for the second and subsequent presses.
The most common repeat events are "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
`double-'. Thus, a double click on the second mouse button with
held down comes to the Lisp program as `M-double-mouse-2'. If a
double-click event has no binding, the binding of the corresponding
ordinary click event is used to execute it. Thus, you need not pay
attention to the double click feature unless you really want to.
When the user performs a double click, Emacs generates first an
ordinary click event, and then a double-click event. Therefore, you
must design the command binding of the double click event to assume
that the single-click command has already run. It must produce the
desired results of a double click, starting from the results of a
single click.
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.
If you click a button, then press it down again and start moving the
mouse with the button held down, then you get a "double-drag" event
when you ultimately release the button. Its event type contains
`double-drag' instead of just `drag'. If a double-drag event has no
binding, Emacs looks for an alternate binding as if the event were an
ordinary drag.
Before the double-click or double-drag event, Emacs generates a
"double-down" event when the user presses the button down for the
second time. Its event type contains `double-down' instead of just
`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.
If it finds no binding that way either, the double-down event is
ignored.
To summarize, when you click a button and then press it again right
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.
If you click a button twice and then press it again, all in quick
succession, Emacs generates a "triple-down" event, followed by either a
"triple-click" or a "triple-drag". The event types of these events
contain `triple' instead of `double'. If any triple event has no
binding, Emacs uses the binding that it would use for the corresponding
double event.
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.
- Function: event-click-count event
This function returns the number of consecutive button presses
that led up to EVENT. If EVENT is a double-down, double-click or
double-drag event, the value is 2. If EVENT is a triple event,
the value is 3 or greater. If EVENT is an ordinary mouse event
(not a repeat event), the value is 1.
- Variable: double-click-fuzz
To generate repeat events, successive mouse button presses must be
at approximately the same screen position. The value of
`double-click-fuzz' specifies the maximum number of pixels the
mouse may be moved between two successive clicks to make a
double-click.
- Variable: double-click-time
To generate repeat events, the number of milliseconds between
successive button presses must be less than the value of
`double-click-time'. Setting `double-click-time' to `nil'
disables multi-click detection entirely. Setting it to `t'
removes the time limit; Emacs then detects multi-clicks by
position only.
File: elisp, Node: Motion Events, Next: Focus Events, Prev: Repeat Events, Up: Input Events
Motion Events
-------------
Emacs sometimes generates "mouse motion" events to describe motion
of the mouse without any button activity. Mouse motion events are
represented by lists that look like this:
(mouse-movement (WINDOW BUFFER-POS (X . Y) TIMESTAMP))
The second element of the list describes the current position of the
mouse, just as in a click event (*note Click Events::).
The special form `track-mouse' enables generation of motion events
within its body. Outside of `track-mouse' forms, Emacs does not
generate events for mere motion of the mouse, and these events do not
appear. *Note Mouse Tracking::.
File: elisp, Node: Focus Events, Next: Misc Events, Prev: Motion Events, Up: Input Events
Focus Events
------------
Window systems provide general ways for the user to control which
window gets keyboard input. This choice of window is called the
"focus". When the user does something to switch between Emacs frames,
that generates a "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. *Note Input Focus::.
Focus events are represented in Lisp as lists that look like this:
(switch-frame NEW-FRAME)
where NEW-FRAME is the frame switched to.
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.
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.
File: elisp, Node: Misc Events, Next: Event Examples, Prev: Focus Events, Up: Input Events
Miscellaneous Window System Events
----------------------------------
A few other event types represent occurrences within the window
system.
`(delete-frame (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 `delete-frame' event is to delete
FRAME.
`(iconify-frame (FRAME))'
This kind of event indicates that the user iconified FRAME using
the window manager. Its standard definition is `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.
`(make-frame-visible (FRAME))'
This kind of event indicates that the user deiconified FRAME using
the window manager. Its standard definition is `ignore'; since the
frame has already been made visible, Emacs has no work to do.
`(mouse-wheel POSITION 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 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 DELTA indicates that the wheel
was rotated backwards, towards the user, and a positive DELTA
indicates that the wheel was rotated forward, away from the user.
The element 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.
`(drag-n-drop POSITION 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 POSITION is a list describing the position of the
event, in the same format as used in a mouse-click event, and
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.
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.
File: elisp, Node: Event Examples, Next: Classifying Events, Prev: Misc Events, Up: Input Events
Event Examples
--------------
If the user presses and releases the left mouse button over the same
location, that generates a sequence of events like this:
(down-mouse-1 (# 2613 (0 . 38) -864320))
(mouse-1 (# 2613 (0 . 38) -864180))
While holding the control key down, the user might hold down the
second mouse button, and drag the mouse from one line to the next.
That produces two events, as shown here:
(C-down-mouse-2 (# 3440 (0 . 27) -731219))
(C-drag-mouse-2 (# 3440 (0 . 27) -731219)
(# 3510 (0 . 28) -729648))
While holding down the meta and shift keys, the user might press the
second mouse button on the window's mode line, and then drag the mouse
into another window. That produces a pair of events like these:
(M-S-down-mouse-2 (# mode-line (33 . 31) -457844))
(M-S-drag-mouse-2 (# mode-line (33 . 31) -457844)
(# 161 (33 . 3)
-453816))
File: elisp, Node: Classifying Events, Next: Accessing Events, Prev: Event Examples, Up: Input Events
Classifying Events
------------------
Every event has an "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 CAR of the
list. Thus, the event type is always a symbol or a character.
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
the location of a mouse event to decide where in the buffer to act.
Sometimes broader classifications of events are useful. For example,
you might want to ask whether an event involved the key,
regardless of which other key or mouse button was used.
The functions `event-modifiers' and `event-basic-type' are provided
to get such information conveniently.
- Function: event-modifiers event
This function returns a list of the modifiers that EVENT has. The
modifiers are symbols; they include `shift', `control', `meta',
`alt', `hyper' and `super'. In addition, the modifiers list of a
mouse event symbol always contains one of `click', `drag', and
`down'.
The argument EVENT may be an entire event object, or just an event
type.
Here are some examples:
(event-modifiers ?a)
=> nil
(event-modifiers ?\C-a)
=> (control)
(event-modifiers ?\C-%)
=> (control)
(event-modifiers ?\C-\S-a)
=> (control shift)
(event-modifiers 'f5)
=> nil
(event-modifiers 's-f5)
=> (super)
(event-modifiers 'M-S-f5)
=> (meta shift)
(event-modifiers 'mouse-1)
=> (click)
(event-modifiers 'down-mouse-1)
=> (down)
The modifiers list for a click event explicitly contains `click',
but the event symbol name itself does not contain `click'.
- Function: event-basic-type event
This function returns the key or mouse button that EVENT
describes, with all modifiers removed. For example:
(event-basic-type ?a)
=> 97
(event-basic-type ?A)
=> 97
(event-basic-type ?\C-a)
=> 97
(event-basic-type ?\C-\S-a)
=> 97
(event-basic-type 'f5)
=> f5
(event-basic-type 's-f5)
=> f5
(event-basic-type 'M-S-f5)
=> f5
(event-basic-type 'down-mouse-1)
=> mouse-1
- Function: mouse-movement-p object
This function returns non-`nil' if OBJECT is a mouse movement
event.
- Function: 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,
(event-convert-list '(control ?a))
=> 1
(event-convert-list '(control meta ?a))
=> -134217727
(event-convert-list '(control super f1))
=> C-s-f1
File: elisp, Node: Accessing Events, Next: Strings of Events, Prev: Classifying Events, Up: Input Events
Accessing Events
----------------
This section describes convenient functions for accessing the data in
a mouse button or motion event.
These two functions return the starting or ending position of a
mouse-button event, as a list of this form:
(WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
- Function: event-start event
This returns the starting position of EVENT.
If EVENT is a click or button-down event, this returns the
location of the event. If EVENT is a drag event, this returns the
drag's starting position.
- Function: event-end event
This returns the ending position of EVENT.
If EVENT is a drag event, this returns the position where the user
released the mouse button. If EVENT is a click or button-down
event, the value is actually the starting position, which is the
only position such events have.
These five functions take a position list as described above, and
return various parts of it.
- Function: posn-window position
Return the window that POSITION is in.
- Function: posn-point position
Return the buffer position in POSITION. This is an integer.
- Function: posn-x-y position
Return the pixel-based x and y coordinates in POSITION, as a cons
cell `(X . Y)'.
- Function: posn-col-row position
Return the row and column (in units of characters) of POSITION, as
a cons cell `(COL . ROW)'. These are computed from the X and Y
values actually found in POSITION.
- Function: posn-timestamp position
Return the timestamp in POSITION.
These functions are useful for decoding scroll bar events.
- Function: 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
`(PORTION . WHOLE)' containing two integers whose ratio is the
fractional position.
- Function: scroll-bar-scale ratio total
This function multiplies (in effect) RATIO by TOTAL, rounding the
result to an integer. The argument RATIO is not a number, but
rather a pair `(NUM . DENOM)'--typically a value returned by
`scroll-bar-event-ratio'.
This function is handy for scaling a position on a scroll bar into
a buffer position. Here's how to do that:
(+ (point-min)
(scroll-bar-scale
(posn-x-y (event-start event))
(- (point-max) (point-min))))
Recall that scroll bar events have two integers forming a ratio,
in place of a pair of x and y coordinates.
File: elisp, Node: Strings of Events, Prev: Accessing Events, Up: Input Events
Putting Keyboard Events in Strings
----------------------------------
In most of the places where strings are used, we conceptualize the
string as containing text characters--the same kind of characters found
in buffers or files. Occasionally Lisp programs use strings that
conceptually contain keyboard characters; for example, they may be key
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:
* Use vectors instead of strings for key sequences, when you plan to
use them for anything other than as arguments to `lookup-key' and
`define-key'. For example, you can use `read-key-sequence-vector'
instead of `read-key-sequence', and `this-command-keys-vector'
instead of `this-command-keys'.
* Use vectors to write key sequence constants containing meta
characters, even when passing them directly to `define-key'.
* When you have to look at the contents of a key sequence that might
be a string, use `listify-key-sequence' (*note Event Input Misc::)
first, to convert it to a list.
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 `\M-' in string constants to stand for
meta characters, especially in arguments to `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 2**27 and such numbers cannot be included in a string.
To support programs with `\M-' in string constants, there are
special rules for including certain meta characters in a string. Here
are the rules for interpreting a string as a sequence of input
characters:
* If the keyboard character value is in the range of 0 to 127, it
can go in the string unchanged.
* The meta variants of those characters, with codes in the range of
2**27 to 2**27+127, can also go in the string, but you must change
their numeric values. You must set the 2**7 bit instead of the
2**27 bit, resulting in a value between 128 and 255. Only a
unibyte string can include these codes.
* Non-ASCII characters above 256 can be included in a multibyte
string.
* Other keyboard character events cannot fit in a string. This
includes keyboard events in the range of 128 to 255.
Functions such as `read-key-sequence' that construct strings of
keyboard input characters follow these rules: they construct vectors
instead of strings, when the events won't fit in a string.
When you use the read syntax `\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.
However, most programs would do well to avoid these issues by
following the recommendations at the beginning of this section.
File: elisp, Node: Reading Input, Next: Special Events, Prev: Input Events, Up: Command Loop
Reading Input
=============
The editor command loop reads key sequences using the function
`read-key-sequence', which uses `read-event'. These and other
functions for event input are also available for use in Lisp programs.
See also `momentary-string-display' in *Note Temporary Displays::, and
`sit-for' in *Note Waiting::. *Note Terminal Input::, for functions
and variables for controlling terminal input modes and debugging
terminal input. *Note Translating Input::, for features you can use
for translating or modifying input events while reading them.
For higher-level input facilities, see *Note Minibuffers::.
* Menu:
* Key Sequence Input:: How to read one key sequence.
* Reading One Event:: How to read just one event.
* Invoking the Input Method:: How reading an event uses the input method.
* Quoted Character Input:: Asking the user to specify a character.
* Event Input Misc:: How to reread or throw away input events.
File: elisp, Node: Key Sequence Input, Next: Reading One Event, Up: Reading Input
Key Sequence Input
------------------
The command loop reads input a key sequence at a time, by calling
`read-key-sequence'. Lisp programs can also call this function; for
example, `describe-key' uses it to read the key to describe.
- Function: read-key-sequence prompt
This function reads a key sequence and returns it as a string or
vector. It keeps reading events until it has accumulated a
complete key 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
`read-key-sequence' returns a string (*note 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 PROMPT is either a string to be displayed in the echo
area as a prompt, or `nil', meaning not to display a prompt.
In the example below, the prompt `?' is displayed in the echo area,
and the user types `C-x C-f'.
(read-key-sequence "?")
---------- Echo Area ----------
?C-x C-f
---------- Echo Area ----------
=> "^X^F"
The function `read-key-sequence' suppresses quitting: `C-g' typed
while reading with this function works like any other character,
and does not set `quit-flag'. *Note Quitting::.
- Function: read-key-sequence-vector prompt
This is like `read-key-sequence' except that it always returns the
key sequence as a vector, never as a string. *Note Strings of
Events::.
If an input character is an upper-case letter and has no key binding,
but its lower-case equivalent has one, then `read-key-sequence'
converts the character to lower case. Note that `lookup-key' does not
perform case conversion in this way.
The function `read-key-sequence' also transforms some mouse events.
It converts unbound drag events into click events, and discards unbound
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.
When mouse events occur in special parts of a window, such as a mode
line or a scroll bar, the event type shows nothing special--it is the
same symbol that would normally represent that combination of mouse
button and modifier keys. The information about the window part is kept
elsewhere in the event--in the coordinates. But `read-key-sequence'
translates this information into imaginary "prefix keys", all of which
are symbols: `header-line', `horizontal-scroll-bar', `menu-bar',
`mode-line', `vertical-line', and `vertical-scroll-bar'. You can define
meanings for mouse clicks in special window parts by defining key
sequences using these imaginary prefix keys.
For example, if you call `read-key-sequence' and then click the
mouse on the window's mode line, you get two events, like this:
(read-key-sequence "Click on the mode line: ")
=> [mode-line
(mouse-1
(# mode-line
(40 . 63) 5959987))]
- Variable: num-input-keys
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.
- Variable: 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.
File: elisp, Node: Reading One Event, Next: Invoking the Input Method, Prev: Key Sequence Input, Up: Reading Input
Reading One Event
-----------------
The lowest level functions for command input are those that read a
single event.
- Function: read-event &optional prompt inherit-input-method
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.
If the optional argument PROMPT is non-`nil', it should be a
string to display in the echo area as a prompt. Otherwise,
`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. *Note The Echo Area::.
If INHERIT-INPUT-METHOD is non-`nil', then the current input
method (if any) is employed to make it possible to enter a
non-ASCII character. Otherwise, input method handling is disabled
for reading this event.
If `cursor-in-echo-area' is non-`nil', then `read-event' moves the
cursor temporarily to the echo area, to the end of any message
displayed there. Otherwise `read-event' does not move the cursor.
If `read-event' gets an event that is defined as a help character,
in some cases `read-event' processes the event directly without
returning. *Note Help Functions::. Certain other events, called
"special events", are also processed directly within `read-event'
(*note Special Events::).
Here is what happens if you call `read-event' and then press the
right-arrow function key:
(read-event)
=> right
- Function: 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), `read-char' signals an error. The
arguments work as in `read-event'.
In the first example, the user types the character `1' (ASCII code
49). The second example shows a keyboard macro definition that
calls `read-char' from the minibuffer using `eval-expression'.
`read-char' reads the keyboard macro's very next character, which
is `1'. Then `eval-expression' displays its return value in the
echo area.
(read-char)
=> 49
;; We assume here you use `M-:' to evaluate this.
(symbol-function 'foo)
=> "^[:(read-char)^M1"
(execute-kbd-macro 'foo)
-| 49
=> nil
- Function: 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,
`read-char-exclusive' ignores it and reads another event, until it
gets a character. The arguments work as in `read-event'.
File: elisp, Node: Invoking the Input Method, Next: Quoted Character Input, Prev: Reading One Event, Up: Reading Input
Invoking the Input Method
-------------------------
The event-reading functions invoke the current input method, if any
(*note Input Methods::). If the value of `input-method-function' is
non-`nil', it should be a function; when `read-event' reads a printing
character (including ) with no modifier bits, it calls that
function, passing the character as an argument.
- Variable: input-method-function
If this is non-`nil', its value specifies the current input method
function.
*Note:* Don't bind this variable with `let'. It is often
buffer-local, and if you bind it around reading input (which is
exactly when you _would_ bind it), switching buffers
asynchronously while Emacs is waiting will cause the value to be
restored in the wrong buffer.
The input method function should return a list of events which should
be used as input. (If the list is `nil', that means there is no input,
so `read-event' waits for another event.) These events are processed
before the events in `unread-command-events' (*note Event Input
Misc::). Events 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 `read-event' or
`read-key-sequence', it should bind `input-method-function' to `nil'
first, to prevent recursion.
The input method function is not called when reading the second and
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 `overriding-local-map' and
`overriding-terminal-local-map'; if either of these variables is
non-`nil', the input method should put its argument into a list and
return that list with no further processing.
File: elisp, Node: Quoted Character Input, Next: Event Input Misc, Prev: Invoking the Input Method, Up: Reading Input
Quoted Character Input
----------------------
You can use the function `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 `quoted-insert' uses this function.
- Function: read-quoted-char &optional prompt
This function is like `read-char', except that if the first
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.
Quitting is suppressed when the first character is read, so that
the user can enter a `C-g'. *Note Quitting::.
If PROMPT is supplied, it specifies a string for prompting the
user. The prompt string is always displayed in the echo area,
followed by a single `-'.
In the following example, the user types in the octal number 177
(which is 127 in decimal).
(read-quoted-char "What character")
---------- Echo Area ----------
What character-177
---------- Echo Area ----------
=> 127
File: elisp, Node: Event Input Misc, Prev: Quoted Character Input, Up: Reading Input
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
input. See also the function `read-passwd' (*note Reading a
Password::).
- Variable: unread-command-events
This variable holds a list of events waiting to be read as command
input. The events are used in the order they appear in the list,
and removed one by one as they are used.
The variable is needed because in some cases a function reads an
event 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.
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. Likewise, incremental search uses this feature to
unread events with no special meaning in a search, because these
events should exit the search and then execute normally.
The reliable and easy way to extract events from a key sequence so
as to put them in `unread-command-events' is to use
`listify-key-sequence' (*note Strings of Events::).
Normally you add events to the front of this list, so that the
events most recently unread will be reread first.
- Function: listify-key-sequence key
This function converts the string or vector KEY to a list of
individual events, which you can put in `unread-command-events'.
- Variable: unread-command-char
This variable holds a character to be read as command input. A
value of -1 means "empty".
This variable is mostly obsolete now that you can use
`unread-command-events' instead; it exists only to support programs
written for Emacs versions 18 and earlier.
- Function: input-pending-p
This function determines whether any command input is currently
available to be read. It returns immediately, with value `t' if
there is available input, `nil' otherwise. On rare occasions it
may return `t' when no input is available.
- Variable: last-input-event
- Variable: last-input-char
This variable records the last terminal input event read, whether
as part of a command or explicitly by a Lisp program.
In the example below, the Lisp program reads the character `1',
ASCII code 49. It becomes the value of `last-input-event', while
`C-e' (we assume `C-x C-e' command is used to evaluate this
expression) remains the value of `last-command-event'.
(progn (print (read-char))
(print last-command-event)
last-input-event)
-| 49
-| 5
=> 49
The alias `last-input-char' exists for compatibility with Emacs
version 18.
- Function: discard-input
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 `nil'.
In the following example, the user may type a number of characters
right after starting the evaluation of the form. After the
`sleep-for' finishes sleeping, `discard-input' discards any
characters typed during the sleep.
(progn (sleep-for 2)
(discard-input))
=> nil
File: elisp, Node: Special Events, Next: Waiting, Prev: Reading Input, Up: Command Loop
Special Events
==============
Special events are handled at a very low level--as soon as they are
read. The `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
`last-command-event' or `(this-command-keys)'. They do not discard a
numeric argument, they cannot be unread with `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 `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 `iconify-frame', `make-frame-visible' and
`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 `special-event-map' (*note Active Keymaps::).
File: elisp, Node: Waiting, Next: Quitting, Prev: Special Events, Up: Command Loop
Waiting for Elapsed Time or Input
=================================
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.
`sit-for' pauses and updates the screen, and returns immediately if
input comes in, while `sleep-for' pauses without updating the screen.
- Function: sit-for seconds &optional millisec nodisp
This function performs redisplay (provided there is no pending
input from the user), then waits SECONDS seconds, or until input is
available. The value is `t' if `sit-for' waited the full time
with no input arriving (see `input-pending-p' in *Note Event Input
Misc::). Otherwise, the value is `nil'.
The argument SECONDS need not be an integer. If it is a floating
point number, `sit-for' waits for a fractional number of seconds.
Some systems support only a whole number of seconds; on these
systems, SECONDS is rounded down.
The optional argument MILLISEC specifies an additional waiting
period measured in milliseconds. This adds to the period
specified by SECONDS. If the system doesn't support waiting
fractions of a second, you get an error if you specify nonzero
MILLISEC.
The expression `(sit-for 0)' is a convenient way to request a
redisplay, without any delay. *Note Forcing Redisplay::.
If NODISP is non-`nil', then `sit-for' does not redisplay, but it
still returns as soon as input is available (or when the timeout
elapses).
Iconifying or deiconifying a frame makes `sit-for' return, because
that generates an event. *Note Misc Events::.
The usual purpose of `sit-for' is to give the user time to read
text that you display.
- Function: sleep-for seconds &optional millisec
This function simply pauses for SECONDS seconds without updating
the display. It pays no attention to available input. It returns
`nil'.
The argument SECONDS need not be an integer. If it is a floating
point number, `sleep-for' waits for a fractional number of seconds.
Some systems support only a whole number of seconds; on these
systems, SECONDS is rounded down.
The optional argument MILLISEC specifies an additional waiting
period measured in milliseconds. This adds to the period
specified by SECONDS. If the system doesn't support waiting
fractions of a second, you get an error if you specify nonzero
MILLISEC.
Use `sleep-for' when you wish to guarantee a delay.
*Note Time of Day::, for functions to get the current time.
File: elisp, Node: Quitting, Next: Prefix Command Arguments, Prev: Waiting, Up: Command Loop
Quitting
========
Typing `C-g' while a Lisp function is running causes Emacs to "quit"
whatever it is doing. This means that control returns to the innermost
active command loop.
Typing `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 `C-g' normally
runs the command `keyboard-quit', whose effect is to quit. However,
when `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.
In the minibuffer, `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
_within_ the minibuffer.) The reason why `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. `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 would not be
possible if `C-g' always quit directly.
When `C-g' does directly quit, it does so by setting the variable
`quit-flag' to `t'. Emacs checks this variable at appropriate times
and quits if it is not `nil'. Setting `quit-flag' non-`nil' in any way
thus causes a quit.
At the level of C code, quitting cannot happen just anywhere; only
at the special places that check `quit-flag'. The reason for this is
that quitting at other places might leave an inconsistency in Emacs's
internal state. Because quitting is delayed until a safe place,
quitting cannot make Emacs crash.
Certain functions such as `read-key-sequence' or `read-quoted-char'
prevent quitting entirely even though they wait for input. Instead of
quitting, `C-g' serves as the requested input. In the case of
`read-key-sequence', this serves to bring about the special behavior of
`C-g' in the command loop. In the case of `read-quoted-char', this is
so that `C-q' can be used to quote a `C-g'.
You can prevent quitting for a portion of a Lisp function by binding
the variable `inhibit-quit' to a non-`nil' value. Then, although `C-g'
still sets `quit-flag' to `t' as usual, the usual result of this--a
quit--is prevented. Eventually, `inhibit-quit' will become `nil'
again, such as when its binding is unwound at the end of a `let' form.
At that time, if `quit-flag' is still non-`nil', the requested quit
happens immediately. This behavior is ideal when you wish to make sure
that quitting does not happen within a "critical section" of the
program.
In some functions (such as `read-quoted-char'), `C-g' is handled in
a special way that does not involve quitting. This is done by reading
the input with `inhibit-quit' bound to `t', and setting `quit-flag' to
`nil' before `inhibit-quit' becomes `nil' again. This excerpt from the
definition of `read-quoted-char' shows how this is done; it also shows
that normal quitting is permitted after the first character of input.
(defun read-quoted-char (&optional prompt)
"...DOCUMENTATION..."
(let ((message-log-max nil) done (first t) (code 0) char)
(while (not done)
(let ((inhibit-quit first)
...)
(and prompt (message "%s-" prompt))
(setq char (read-event))
(if inhibit-quit (setq quit-flag nil)))
...set the variable `code'...)
code))
- Variable: quit-flag
If this variable is non-`nil', then Emacs quits immediately, unless
`inhibit-quit' is non-`nil'. Typing `C-g' ordinarily sets
`quit-flag' non-`nil', regardless of `inhibit-quit'.
- Variable: inhibit-quit
This variable determines whether Emacs should quit when `quit-flag'
is set to a value other than `nil'. If `inhibit-quit' is
non-`nil', then `quit-flag' has no special effect.
- Command: keyboard-quit
This function signals the `quit' condition with `(signal 'quit
nil)'. This is the same thing that quitting does. (See `signal'
in *Note Errors::.)
You can specify a character other than `C-g' to use for quitting.
See the function `set-input-mode' in *Note Terminal Input::.
File: elisp, Node: Prefix Command Arguments, Next: Recursive Editing, Prev: Quitting, Up: Command Loop
Prefix Command Arguments
========================
Most Emacs commands can use a "prefix argument", a number specified
before the command itself. (Don't confuse prefix arguments with prefix
keys.) The prefix argument is at all times represented by a value,
which may be `nil', meaning there is currently no prefix argument.
Each command may use the prefix argument or ignore it.
There are two representations of the prefix argument: "raw" and
"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:
* `nil', meaning there is no prefix argument. Its numeric value is
1, but numerous commands make a distinction between `nil' and the
integer 1.
* An integer, which stands for itself.
* A list of one element, which is an integer. This form of prefix
argument results from one or a succession of `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.
* The symbol `-'. This indicates that `M--' or `C-u -' was typed,
without following digits. The equivalent numeric value is -1, but
some commands make a distinction between the integer -1 and the
symbol `-'.
We illustrate these possibilities by calling the following function
with various prefixes:
(defun display-prefix (arg)
"Display the value of the raw prefix arg."
(interactive "P")
(message "%s" arg))
Here are the results of calling `display-prefix' with various raw
prefix arguments:
M-x display-prefix -| nil
C-u M-x display-prefix -| (4)
C-u C-u M-x display-prefix -| (16)
C-u 3 M-x display-prefix -| 3
M-3 M-x display-prefix -| 3 ; (Same as `C-u 3'.)
C-u - M-x display-prefix -| -
M-- M-x display-prefix -| - ; (Same as `C-u -'.)
C-u - 7 M-x display-prefix -| -7
M-- 7 M-x display-prefix -| -7 ; (Same as `C-u -7'.)
Emacs uses two variables to store the prefix argument: `prefix-arg'
and `current-prefix-arg'. Commands such as `universal-argument' that
set up prefix arguments for other commands store them in `prefix-arg'.
In contrast, `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 `interactive' declaration.
(*Note Using Interactive::.) Alternatively, functions may look at the
value of the prefix argument directly in the variable
`current-prefix-arg', but this is less clean.
- Function: prefix-numeric-value arg
This function returns the numeric meaning of a valid raw prefix
argument value, ARG. The argument may be a symbol, a number, or a
list. If it is `nil', the value 1 is returned; if it is `-', the
value -1 is returned; if it is a number, that number is returned;
if it is a list, the CAR of that list (which should be a number) is
returned.
- Variable: current-prefix-arg
This variable holds the raw prefix argument for the _current_
command. Commands may examine it directly, but the usual method
for accessing it is with `(interactive "P")'.
- Variable: prefix-arg
The value of this variable is the raw prefix argument for the
_next_ editing command. Commands such as `universal-argument'
that specify prefix arguments for the following command work by
setting this variable.
- Variable: last-prefix-arg
The raw prefix argument value used by the previous command.
The following commands exist to set up prefix arguments for the
following command. Do not call them for any other reason.
- 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.
- Command: digit-argument arg
This command adds to the prefix argument for the following
command. The argument 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.
- Command: negative-argument arg
This command adds to the numeric argument for the next command.
The argument 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.