(lispref.info)Command Overview

Next: Defining Commands Up: Command Loop

Command Loop Overview

   The command loop in Emacs 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 Emacs that (for example)
part of its window was just uncovered and needs to be redrawn.  *Note
Input 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.  *Note
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 is used instead.  This is done by the command
`execute-extended-command' (Note: Interactive Call.).

   Once the command is chosen, it must be executed, which includes
reading arguments to be given to it.  This is done by calling
`command-execute' (Note: Interactive Call.).  For commands written in
Lisp, the `interactive' specification says how to read the arguments.
This may use the prefix argument (Note: Prefix Command Arguments.) or
may read with prompting in the minibuffer (Note: 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 (Note: Keyboard Macros.).

   If a command runs away, typing `C-g' terminates its execution
immediately.  This is called "quitting" (Note: Quitting.).

 - Variable: pre-command-hook
     The editor command loop runs this normal hook before each command.
     This may examine the `this-command' variable to find out what
     command is about to be run, or may change it to cause a different
     command to run.  Function on this hook must be careful to avoid
     signalling errors!

 - Variable: post-command-hook
     The editor command loop runs this normal hook after each command.
     (In FSF Emacs 19, it is also run when the command loop is entered,
     or reentered after an error or quit.)  This may examine the
     `this-command' variable to find out what command was just executed.

automatically generated by info2www