| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When you run XEmacs, it enters the editor command loop almost immediately. This loop reads events, executes their definitions, and displays the results. In this chapter, we describe how these things are done, and the subroutines that allow Lisp programs to do them.
| 25.1 Command Loop Overview | How the command loop reads commands. | |
| 25.2 Defining Commands | Specifying how a function should read arguments. | |
| 25.3 Interactive Call | Calling a command, so that it will read arguments. | |
| 25.4 Information from the Command Loop | Variables set by the command loop for you to examine. | |
| 25.5 Events | What input looks like when you read it. | |
| 25.6 Reading Input | How to read input events from the keyboard or mouse. | |
| 25.7 Waiting for Elapsed Time or Input | Waiting for user input or elapsed time. | |
| 25.8 Quitting | How C-g works. How to catch or defer quitting. | |
| 25.9 Prefix Command Arguments | How the commands to set prefix args work. | |
| 25.10 Recursive Editing | Entering a recursive edit, and why you usually shouldn't. | |
| 25.11 Disabling Commands | How the command loop handles disabled commands. | |
| 25.12 Command History | How the command history is set up, and how accessed. | |
| 25.13 Keyboard Macros | How keyboard macros are implemented. |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The command loop in XEmacs is a standard event loop, reading events
one at a time with next-event and handling them with
dispatch-event. An event is typically a single user action, such
as a keypress, mouse movement, or menu selection; but they can also be
notifications from the window system, informing XEmacs that (for
example) part of its window was just uncovered and needs to be redrawn.
See section 25.5 Events. Pending events are held in a first-in, first-out list
called the event queue: events are read from the head of the list,
and newly arriving events are added to the tail. In this way, events
are always processed in the order in which they arrive.
dispatch-event does most of the work of handling user actions.
The first thing it must do is put the events together into a key
sequence, which is a sequence of events that translates into a command.
It does this by consulting the active keymaps, which specify what the
valid key sequences are and how to translate them into commands.
See section 26.8 Key Lookup, for information on how this is done. The result of
the translation should be a keyboard macro or an interactively callable
function. If the key is M-x, then it reads the name of another
command, which it then calls. This is done by the command
execute-extended-command (see section 25.3 Interactive Call).
To execute a command requires first reading the arguments for it.
This is done by calling command-execute (see section 25.3 Interactive Call). For commands written in Lisp, the interactive
specification says how to read the arguments. This may use the prefix
argument (see section 25.9 Prefix Command Arguments) or may read with prompting
in the minibuffer (see section 24. Minibuffers). For example, the command
find-file has an 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.
If the command is a string or vector (i.e., a keyboard macro) then
execute-kbd-macro is used to execute it. You can call this
function yourself (see section 25.13 Keyboard Macros).
To terminate the execution of a running command, type C-g. This character causes quitting (see section 25.8 Quitting).
this-command contains the command that is about to
run, and last-command describes the previous command.
See section 33.4 Hooks.
this-command describes the command that just ran, and
last-command describes the command before that. See section 33.4 Hooks.
Quitting is suppressed while running pre-command-hook and
post-command-hook. If an error happens while executing one of
these hooks, it terminates execution of the hook, but that is all it
does.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A Lisp function becomes a command when its body contains, at top
level, a form that calls the special operator interactive. This
operator 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.
25.2.1 Using interactive | General rules for interactive. | |
25.2.2 Code Characters for interactive | The standard letter-codes for reading arguments in various ways. | |
25.2.3 Examples of Using interactive | Examples of how to read interactive arguments. |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
interactive
This section describes how to write the interactive form that
makes a Lisp function an interactively-callable command.
A command may be called from Lisp programs like any other function, but then the caller supplies the arguments and arg-descriptor has no effect.
The interactive form has its effect because the command loop
(actually, its subroutine 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
interactive form are executed, but at this time
interactive simply returns nil without even evaluating its
argument.
There are three possibilities for the argument arg-descriptor:
nil; then the command is called with no
arguments. This leads quickly to an error if the command requires one
or more arguments.
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 not to do:
(interactive
(list (region-beginning) (region-end)
(read-string "Foo: " nil 'my-history)))
|
Here's how to avoid the problem, by examining point and the mark only after reading the keyboard input:
(interactive (let ((string (read-string "Foo: " nil 'my-history))) (list (region-beginning) (region-end) string))) |
(interactive "bFrobnicate buffer: ") |
The code letter `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.
The prompt string can use `%' to include previous argument values
(starting with the first argument) in the prompt. This is done using
format (see section 10.10 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:
(interactive "bBuffer to rename: \nsRename buffer %s to: ") |
If the first character in the string is `*', then an error is signaled if the buffer is read-only.
If the first character in the string is `@', 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.
If the first character in the string is `_', then this command will
not cause the region to be deactivated when it completes; that is,
zmacs-region-stays will be set to t when the command exits
successfully.
You can use `*', `@', and `_' 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 `*', `@', or `_').
interactive and the specs. If
function is not interactive, nil will be returned.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
interactive The code character descriptions below contain a number of key words, defined here as follows:
completing-read
(see section 24.5 Completion). ? displays a list of possible completions.
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.
Here are the code character descriptions for use with interactive:
fboundp). Existing,
Completion, Prompt.
commandp). Existing,
Completion, Prompt.
default-directory (see section 57.3 Operating System Environment).
Existing, Completion, Default, Prompt.
You can use `e' more than once in a single command's interactive specification. If the key sequence that invoked the command has n mouse-button or misc-user events, the nth `e' provides the nth such event.
default-directory. Existing, Completion, Default,
Prompt.
This kind of input is used by commands such as describe-key and
global-set-key.
nil, then
read a number as with n. Requires a number. See section 25.9 Prefix Command Arguments. Prompt.
user-variable-p). See section 24.5.4 High-Level Completion Functions. Existing,
Completion, Prompt.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
interactive
Here are some examples of interactive:
(defun foo1 () ; |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
After the command loop has translated a key sequence into a
definition, it invokes that definition using the function
command-execute. If the definition is a function that is a
command, command-execute calls call-interactively, which
reads the arguments and calls the command. You can also call these
functions yourself.
t if function is suitable for calling interactively;
that is, if function is a command. Otherwise, returns nil.
The interactively callable objects include strings and vectors (treated
as keyboard macros), lambda expressions that contain a top-level call to
interactive, compiled-function objects made from such lambda
expressions, autoload objects that are declared as interactive
(non-nil fourth argument to autoload), and some of the
primitive functions.
A symbol is commandp if its function definition is
commandp.
Keys and keymaps are not commands. Rather, they are used to look up commands (see section 26. Keymaps).
See documentation in 34.2 Access to Documentation Strings, for a
realistic example of using commandp.
If record-flag is the symbol lambda, the interactive
calling arguments for command are read and returned as a list,
but the function is not called on them.
If record-flag is t, then this command and its arguments
are unconditionally added to the list command-history.
Otherwise, the command is added only if it uses the minibuffer to read
an argument. See section 25.12 Command History.
commandp predicate; i.e.,
it must be an interactively callable function or a keyboard macro.
A string or vector as command is executed with
execute-kbd-macro. A function is passed to
call-interactively, along with the optional record-flag.
A symbol is handled by using its function definition in its place. A
symbol with an 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.
completing-read (see section 24.5 Completion). Then it uses
command-execute to call the specified command. Whatever that
command returns becomes the value of execute-extended-command.
If the command asks for a prefix argument, it receives the value
prefix-argument. If execute-extended-command is called
interactively, the current raw prefix argument is used for
prefix-argument, and thus passed on to whatever command is run.
execute-extended-command is the normal definition of M-x,
so it uses the string `M-x ' as a prompt. (It would be better
to take the prompt from the events used to invoke
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.
(execute-extended-command 1)
---------- Buffer: Minibuffer ----------
1 M-x forward-word RET
---------- Buffer: Minibuffer ----------
=> t
|
t if the containing function (the one that
called interactive-p) was called interactively, with the function
call-interactively. (It makes no difference whether
call-interactively was called from Lisp or directly from the
editor command loop.) If the containing function was called by Lisp
evaluation (or with apply or funcall), then it was not
called interactively.
The most common use of interactive-p is for deciding whether to
print an informative message. As a special exception,
interactive-p returns nil whenever a keyboard macro is
being run. This is to suppress the informative messages and speed
execution of the macro.
For example:
(defun foo ()
(interactive)
(and (interactive-p)
(message "foo")))
=> foo
(defun bar ()
(interactive)
(setq foobar (list (foo) (interactive-p))))
=> bar
;; Type M-x foo.
-| foo
;; Type M-x bar.
;; This does not print anything.
foobar
=> (nil t)
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The editor command loop sets several Lisp variables to keep status records for itself and for commands that are run.
The value is copied from this-command when a command returns to
the command loop, except when the command specifies a prefix argument
for the following command.
last-command, it is normally a symbol
with a function definition.
The command loop sets this variable just before running a command, and
copies its value into last-command when the command finishes
(unless the command specifies a prefix argument for the following
command).
Some commands set this variable during their execution, as a flag for
whatever command runs next. In particular, the functions for killing text
set this-command to kill-region so that any kill commands
immediately following will know to append the killed text to the
previous kill.
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 this-command to t at the
beginning of the command, and set this-command back to its proper
value at the end, like this:
(defun foo (args...)
(interactive ...)
(let ((old-this-command this-command))
(setq this-command t)
...do the work...
(setq this-command old-this-command)))
|
This function copies the vector and the events; it is safe to keep and modify them.
(this-command-keys)
;; Now use C-u C-x C-e to evaluate that.
=> [#<keypress-event control-U> #<keypress-event control-X> #<keypress-event control-E>]
|
self-insert-command, which uses it to decide which
character to insert.
This variable is off limits: you may not set its value or modify the
event that is its value, as it is destructively modified by
read-key-sequence. If you want to keep a pointer to this value,
you must use copy-event.
Note that this variable is an alias for last-command-char in
FSF Emacs.
last-command-event
;; Now type C-u C-x C-e.
=> #<keypress-event control-E>
|
If the value of last-command-event is a keyboard event, then this
is the nearest character equivalent to it (or nil if there is no
character equivalent). last-command-char is the character that
self-insert-command will insert in the buffer. Remember that
there is not a one-to-one mapping between keyboard events and
XEmacs characters: many keyboard events have no corresponding character,
and when the Mule feature is available, most characters can not be input
on standard keyboards, except possibly with help from an input method.
So writing code that examines this variable to determine what key has
been typed is bad practice, unless you are certain that it will be one
of a small set of characters.
This variable exists for compatibility with Emacs version 18.
last-command-char
;; Now use C-u C-x C-e to evaluate that.
=> ?\^E
|
nil. This is what (interactive "e") returns.
If the value is zero, then command input is not echoed.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The XEmacs command loop reads a sequence of events that represent keyboard or mouse activity. Unlike in Emacs 18 and in FSF Emacs, events are a primitive Lisp type that must be manipulated using their own accessor and settor primitives. This section describes the representation and meaning of input events in detail.
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.
For information about how exactly the XEmacs command loop works, See section 25.6 Reading Input.
nil if object is an input event.
| 25.5.1 Event Types | Events come in different types. | |
| 25.5.2 Contents of the Different Types of Events | What the contents of each event type are. | |
| 25.5.3 Event Predicates | Querying whether an event is of a particular type. | |
| 25.5.4 Accessing the Position of a Mouse Event | Determining where a mouse event occurred, and over what. | |
| 25.5.5 Accessing the Other Contents of Events | Accessing non-positional event info. | |
| 25.5.6 Working With Events | Creating, copying, and destroying events. | |
| 25.5.7 Converting Events | Converting between events, keys, and characters. |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Events represent keyboard or mouse activity or status changes of various sorts, such as process input being available or a timeout being triggered. The different event types are as follows:
dispatch-event knows what to do with an event of this type.
dispatch-event. See section 25.6.3 Dispatching an Event.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Every event, no matter what type it is, contains a timestamp (which is typically an offset in milliseconds from when the X server was started) indicating when the event occurred. In addition, many events contain a channel, which specifies which frame the event occurred on, and/or a value indicating which modifier keys (shift, control, etc.) were held down at the time of the event.
The contents of each event are as follows:
left or right.
Note that many physical keys are actually treated as two separate keys,
depending on whether the shift key is pressed; for example, the "a"
key is treated as either "a" or "A" depending on the state of the
shift key, and the "1" key is similarly treated as either "1" or
"!" on most keyboards. In such cases, the shift key does not show up
in the modifier list. For other keys, such as backspace, the
shift key shows up as a regular modifier.
eval or call-interactively.
This will be a symbol; one of
key-press
button-press
button-release
motion
misc-user
process
timeout
eval
magic
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following predicates return whether an object is an event of a particular type.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Unlike other events, mouse events (i.e. motion, button-press, button-release, and drag or drop type misc-user events) occur in a particular location on the screen. Many primitives are provided for determining exactly where the event occurred and what is under that location.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions return frame-level information about where a mouse event occurred.
nil for non-mouse events.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions return window-level information about where a mouse event occurred.
nil
if the event occurred in the border or over a toolbar. The modeline is
considered to be within the window it describes.
nil if the event occurred in the border or over a toolbar.
The modeline is considered to be within the window it describes. This is
equivalent to calling event-window and then calling
window-buffer on the result if it is a window.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions return information about the text (including the modeline) that a mouse event occurred over or near.
t if the event is over the text area of a
window. Otherwise, nil is returned. The modeline is not
considered to be part of the text area.
t if the event is over the modeline of a window.
Otherwise, nil is returned.
nil.
Otherwise, it returns an index into the buffer visible in the event's
window.
(window-end) is returned.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions return information about the glyph (if any) that a mouse event occurred over.
t if the event is over a glyph. Otherwise,
nil is returned.
nil is returned.
nil.
nil.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
t if the event is over a toolbar. Otherwise,
nil is returned.
nil is returned.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
t if the event is over an internal toolbar.
Otherwise, nil is returned.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions allow access to the contents of events other than the position info described in the previous section.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
XEmacs provides primitives for creating, copying, and destroying event
objects. Many functions that return events take an event object as an
argument and fill in the fields of this event; or they make accept
either an event object or nil, creating the event object first in
the latter case.
empty,
key-press, button-press, button-release,
motion, or misc-user.
plist is a property list, the properties being compatible to those
returned by event-properties. For events other than
empty, it is mandatory to specify certain properties. For
empty events, plist must be nil. The list is
canonicalized, which means that if a property keyword is present
more than once, only the first instance is taken into account.
Specifying an unknown or illegal property signals an error.
The following properties are allowed:
channel
button-press, button-release and motion), this
must be a frame. For key-press events, it must be a console. If
channel is unspecified by plist, it will be set to the selected
frame or selected console, as appropriate.
key
button
modifiers
x
y
y property will have to be negative.
timestamp
WARNING: the event object returned by this function may be a
reused one; see the function deallocate-event.
The events created by make-event can be used as non-interactive
arguments to the functions with an (interactive "e")
specification.
Here are some basic examples of usage:
;; Create an empty event.
(make-event)
=> #<empty-event>
;; Try creating a key-press event.
(make-event 'key-press)
error--> Undefined key for keypress event
;; Creating a key-press event, try 2
(make-event 'key-press '(key home))
=> #<keypress-event home>
;; Create a key-press event of dubious fame.
(make-event 'key-press '(key escape modifiers (meta alt control shift)))
=> #<keypress-event control-meta-alt-shift-escape>
;; Create a M-button1 event at coordinates defined by variables
;; x and y.
(make-event 'button-press `(button 1 modifiers (meta) x ,x y ,y))
=> #<buttondown-event meta-button1>
;; Create a similar button-release event.
(make-event 'button-release `(button 1 modifiers (meta) x ,x y ,x))
=> #<buttonup-event meta-button1up>
;; Create a mouse-motion event.
(make-event 'motion '(x 20 y 30))
=> #<motion-event 20, 30>
(event-properties (make-event 'motion '(x 20 y 30)))
=> (channel #<x-frame "emacs" 0x8e2> x 20 y 30
modifiers nil timestamp 0)
|
In conjunction with event-properties, you can use
make-event to create modified copies of existing events. For
instance, the following code will return an equal copy of
event:
(make-event (event-type event)
(event-properties event))
|
Note, however, that you cannot use make-event as the generic
replacement for copy-event, because it does not allow creating
all of the event types.
To create a modified copy of an event, you can use the canonicalization
feature of plist. The following example creates a copy of
event, but with modifiers reset to nil.
(make-event (event-type event)
(append '(modifiers nil)
(event-properties event)))
|
nil) then a new event will be made, as with
make-event.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
XEmacs provides some auxiliary functions for converting between events and other ways of representing keys. These are useful when working with ASCII strings and with keymaps.
Note that key-description can be an integer, a character, a symbol
such as clear or a list such as (control backspace).
If optional arg event is non-nil, it is modified;
otherwise, a new event object is created. In both cases, the event is
returned.
Optional third arg console is the console to store in the event, and defaults to the selected console.
If key-description is an integer or character, the high bit may be
interpreted as the meta key. (This is done for backward compatibility in
lots of places.) If use-console-meta-flag is nil, this
will always be the case. If use-console-meta-flag is
non-nil, the meta flag for console affects whether
the high bit is interpreted as a meta key. (See set-input-mode.)
If you don't want this silly meta interpretation done, you should pass
in a list containing the character.
Beware that character-to-event and event-to-character are
not strictly inverse functions, since events contain much more
information than the XEmacs internal character encoding can store.
nil.
If allow-extra-modifiers is non-nil, then this is lenient
in its translation; it will ignore modifier keys other than
control and meta, and will ignore the shift modifier
on those characters which have no shifted ASCII equivalent
(Control-Shift-A for example, will be mapped to the same
ASCII code as Control-A).
If allow-meta is non-nil, then the Meta modifier will
be represented by turning on the high bit of the byte returned;
otherwise, nil will be returned for events containing the
Meta modifier.
Specifying allow-meta will give ambiguous results---M-x and oslash will return the same thing, for example--so you should probably not use it.
allow-non-ascii is ignored; in previous versions of XEmacs, it controlled whether one particular type of mapping between X11 keysyms and characters would take place. The intention was that this flag could be clear and you could be sure that if you got a Latin-1 character with the high bit set back, you could assume that the lower seven bits of the character were the ASCII code of the character in question, and that the Meta key was pressed at the same time. This didn't work in the general case, however, because it left the other type of X11 keysym-to-character mapping in place, ready to give you a Latin-1 character for a Latin-1 key. If you feel the need to use such a flag, sit back and think about abstracting your code, and if you still feel the need, bear in mind that it will be buggy in earlier versions of XEmacs.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The editor command loop reads keyboard input using the function
next-event and constructs key sequences out of the events using
dispatch-event. Lisp programs can also use the function
read-key-sequence, which reads input a key sequence at a time.
See also momentary-string-display in 52.8 Temporary Displays,
and sit-for in 25.7 Waiting for Elapsed Time or Input. See section 57.8 Terminal Input, for
functions and variables for controlling terminal input modes and
debugging terminal input.
For higher-level input facilities, see 24. Minibuffers.
| 25.6.1 Key Sequence Input | How to read one key sequence. | |
| 25.6.2 Reading One Event | How to read just one event. | |
| 25.6.3 Dispatching an Event | What to do with an event once it has been read. | |
| 25.6.4 Quoted Character Input | Asking the user to specify a character. | |
| 25.6.5 Miscellaneous Event Input Features | How to reread or throw away input events. |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Lisp programs can read input a key sequence at a time by calling
read-key-sequence; for example, describe-key uses it to
read the key to describe.
The vector and the event objects it contains are freshly created (and so will not be side-effected by subsequent calls to this function).
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. See section 25.8 Quitting.
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.
Second optional arg continue-echo non-nil means this key
echoes as a continuation of the previous key.
Third optional arg dont-downcase-last non-nil means do not
convert the last event to lower case. (Normally any upper case event is
converted to lower case if the original event is undefined and the lower
case equivalent is defined.) This argument is provided mostly for
fsf compatibility; the equivalent effect can be achieved more
generally by binding retry-undefined-key-binding-unshifted to
nil around the call to read-key-sequence.
If the user selects a menu item while we are prompting for a key
sequence, the returned value will be a vector of a single menu-selection
event (a misc-user event). An error will be signalled if you pass this
value to lookup-key or a related function.
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 ----------
=> [#<keypress-event control-X> #<keypress-event control-F>]
|
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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The lowest level functions for command input are those which read a single event. These functions often make a distinction between command events, which are user actions (keystrokes and mouse actions), and other events, which serve as communication between XEmacs and the window system.
dispatch-event to handle it. If
an event object is supplied, it is filled in and returned; otherwise a
new event object will be created.
Events can come directly from the user, from a keyboard macro, or from
unread-command-events.
In most cases, the function next-command-event is more
appropriate.
dispatch-event to
handle it. If an event object is supplied, it is filled in and
returned, otherwise a new event object will be created.
The event returned will be a keyboard, mouse press, or mouse release
event. If there are non-command events available (mouse motion,
sub-process output, etc) then these will be executed (with
dispatch-event) and discarded. This function is provided as a
convenience; it is equivalent to the Lisp code
(while (progn
(next-event event)
(not (or (key-press-event-p event)
(button-press-event-p event)
(button-release-event-p event)
(menu-event-p event))))
(dispatch-event event))
|
Here is what happens if you call next-command-event and then
press the right-arrow function key:
(next-command-event)
=> #<keypress-event right>
|
next-command-event instead.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
next-event, this function
executes it. This is the basic function that makes XEmacs respond to
user input; it also deals with notifications from the window system
(such as Expose events).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
read-char, except that if the first
character read is an octal digit (0-7), it reads up to two more octal digits
(but stopping if a non-octal digit is found) and returns the
character represented by those digits in octal.
Quitting is suppressed when the first character is read, so that the user can enter a C-g. See section 25.8 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
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 variables last-command-event and last-command-char
(25.4 Information from the Command Loop).
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.
This variable is mostly obsolete now that you can use
unread-command-events instead; it exists only to support programs
written for versions of XEmacs prior to 19.12.
t if
there is available input, nil otherwise. On rare occasions it
may return t when no input is available.
This variable is off limits: you may not set its value or modify the
event that is its value, as it is destructively modified by
read-key-sequence. If you want to keep a pointer to this value,
you must use copy-event.
Note that this variable is an alias for last-input-char in
FSF Emacs.
In the example below, a character is read (the character 1). It
becomes the value of last-input-event, while C-e (from the
C-x C-e command used to evaluate this expression) remains the
value of last-command-event.
(progn (print (next-command-event))
(print last-command-event)
last-input-event)
-| #<keypress-event 1>
-| #<keypress-event control-E>
=> #<keypress-event 1>
|
last-input-event is a keyboard event, then this
is the nearest ASCII equivalent to it. Remember that there is
not a 1:1 mapping between keyboard events and ASCII
characters: the set of keyboard events is much larger, so writing code
that examines this variable to determine what key has been typed is bad
practice, unless you are certain that it will be one of a small set of
characters.
This function exists for compatibility with Emacs version 18.
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
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
Note that in FSF Emacs, the commands sit-for and sleep-for
take two arguments to specify the time (one integer and one float
value), instead of a single argument that can be either an integer or a
float.
t if sit-for waited the full
time with no input arriving (see input-pending-p in 25.6.5 Miscellaneous Event Input Features). 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.
Redisplay is normally preempted if input arrives, and does not happen at
all if input is available before it starts. (You can force screen
updating in such a case by using force-redisplay. See section 52.1 Refreshing the Screen.) If there is no input pending, you can force an update with no
delay by using (sit-for 0).
If nodisplay is non-nil, then sit-for does not
redisplay, but it still returns as soon as input is available (or when
the timeout elapses).
The usual purpose of sit-for is to give the user time to read
text that you display.
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.
Use sleep-for when you wish to guarantee a delay.
See section 57.5 Time of Day, for functions to get the current time.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Typing C-g while a Lisp function is running causes XEmacs 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, the result is 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. XEmacs 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 XEmacs's
internal state. Because quitting is delayed until a safe place, quitting
cannot make XEmacs 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 ((count 0) (code 0) char)
(while (< count 3)
(let ((inhibit-quit (zerop count))
(help-form nil))
(and prompt (message "%s-" prompt))
(setq char (read-char))
(if inhibit-quit (setq quit-flag nil)))
...)
(logand 255 code)))
|
nil, then XEmacs quits immediately, unless
inhibit-quit is non-nil. Typing C-g ordinarily sets
quit-flag non-nil, regardless of inhibit-quit.
quit-flag
is set to a value other than nil. If inhibit-quit is
non-nil, then quit-flag has no special effect.
quit condition with (signal 'quit
nil). This is the same thing that quitting does. (See signal
in 15.5.3 Errors.)
You can specify a character other than C-g to use for quitting.
See the function set-input-mode in 57.8 Terminal Input.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Most XEmacs 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.
-. 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 |
XEmacs 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.
(See section 25.2.1 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.
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.
(interactive "P").
Do not call the functions universal-argument,
digit-argument, or negative-argument unless you intend to
let the user enter the prefix argument for the next command.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The XEmacs command loop is entered automatically when XEmacs starts up. This top-level invocation of the command loop never exits; it keeps running as long as XEmacs does. Lisp programs can also invoke the command loop. Since this makes more than one activation of the command loop, we call it 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.
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 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.)
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.
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 XEmacs commands.
To invoke a recursive editing level, call the function
recursive-edit. This function contains the command loop; it also
contains a call to catch with tag exit, which makes it
possible to exit the recursive editing level by throwing to exit
(see section 15.5.1 Explicit Nonlocal Exits: catch and throw). If you throw a value other than t,
then recursive-edit returns normally to the function that called
it. The command C-M-c (exit-recursive-edit) does this.
Throwing a t value causes recursive-edit to quit, so that
control returns to the command loop one level up. This is called
aborting, and is done by C-] (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 major mode, which should have a command to go back to the previous mode. (The 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 m command in Rmail does this.)
Recursive edits are useful in debugging. You can insert a call to
debug into a function definition as a sort of breakpoint, so that
you can look around when the function gets there. debug invokes
a recursive edit but also provides the other features of the debugger.
Recursive editing levels are also used when you type C-r in
query-replace or use C-x q (kbd-macro-query).
In the following example, the function 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 C-M-c to exit and continue executing simple-rec.
(defun simple-rec ()
(forward-word 1)
(message "Recursive edit in progress")
(recursive-edit)
(forward-word 1))
=> simple-rec
(simple-rec)
=> nil
|
(throw 'exit
nil).
quit
after exiting the recursive edit. Its definition is effectively
(throw 'exit t). See section 25.8 Quitting.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
The low-level mechanism for disabling a command is to put a
non-nil disabled property on the Lisp symbol for the
command. These properties are normally set up by the user's
`.emacs' file with Lisp expressions such as this:
(put 'upcase-region 'disabled t) |
For a few commands, these properties are present by default and may be removed by the `.emacs' file.
If the value of the disabled property is a string, the message
saying the command is disabled includes that string. For example:
(put 'delete-region 'disabled
"Text deleted this way cannot be yanked back!\n")
|
See section `Disabling' in The XEmacs User's 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.
this-command-keys to determine what the user typed to run the
command, and thus find the command itself. See section 33.4 Hooks.
By default, disabled-command-hook contains a function that asks
the user whether to proceed.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The command loop keeps a history of the complex commands that have
been executed, to make it convenient to repeat these commands. A
complex command is one for which the interactive argument reading
uses the minibuffer. This includes any M-x command, any
M-: command, and any command whose interactive
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.
command-history
=> ((switch-to-buffer "chistory.texi")
(describe-key "^X^[")
(visit-tags-table "~/emacs/src/")
(find-tag "repeat-complex-command"))
|
This history list is actually a special case of minibuffer history (see section 24.4 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 repeat-complex-command, and
list-command-history are described in the user manual
(see section `Repetition' in The XEmacs User's Manual). Within the
minibuffer, the history commands used are the same ones available in any
minibuffer.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A keyboard macro is a canned sequence of input events that can 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 (see section 18. Macros).
If macro is a symbol, then its function definition is used in place of macro. If that is another symbol, this process repeats. Eventually the result should be a string or vector. If the result is not a symbol, string, or vector, an error is signaled.
The argument count is a repeat count; macro is executed that
many times. If count is omitted or nil, macro is
executed once. If it is 0, macro is executed over and over until it
encounters an error or a failing search.
nil if no macro is
currently executing. A command can test this variable to behave
differently when run from an executing macro. Do not set this variable
yourself.
start-kbd-macro and
end-kbd-macro set this variable--do not set it yourself.
nil.
The commands are described in the user's manual (see section `Keyboard Macros' in The XEmacs User's Manual).
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |