The first thing the command loop must do is read a key sequence,
which is a sequence of input events that translates into a command.
It does this by calling the function
programs can also call this function (see Key Sequence Input).
They can also read input at a lower level with
read-event (see Reading One Event), or discard pending
discard-input (see Miscellaneous Event Input Features).
The key sequence is translated into a command through the currently
active keymaps. See Key Lookup, for information on how this is done.
The result should be a keyboard macro or an interactively callable
function. If the key is M-x, then it reads the name of another
command, which it then calls. This is done by the command
execute-extended-command (see Interactive Call).
Prior to executing the command, Emacs runs
create an undo boundary. See Maintaining Undo Lists.
To execute a command, Emacs first reads its arguments by calling
command-execute (see Interactive Call). For commands
written in Lisp, the
interactive specification says how to read
the arguments. This may use the prefix argument (see Prefix Command Arguments) or may read with prompting in the minibuffer
(see Minibuffers). For example, the command
interactive specification which says to read a file name
using the minibuffer. The function body of
find-file does not
use the minibuffer, so if you call
find-file as a function from
Lisp code, you must supply the file name string as an ordinary Lisp
If the command is a keyboard macro (i.e., a string or vector),
Emacs executes it using
execute-kbd-macro (see Keyboard Macros).
This normal hook is run by the editor command loop before it executes
each command. At that time,
this-command contains the command
that is about to run, and
last-command describes the previous
command. See Information from the Command Loop.
This normal hook is run by the editor command loop after it executes
each command (including commands terminated prematurely by quitting or
by errors). At that time,
this-command refers to the command
that just ran, and
last-command refers to the command before
This hook is also run when Emacs first enters the command loop (at
last-command are both
Quitting is suppressed while running
post-command-hook. If an error happens while executing one of
these hooks, it does not terminate execution of the hook; instead
the error is silenced and the function in which the error occurred
is removed from the hook.
A request coming into the Emacs server (see Emacs Server in The GNU Emacs Manual) runs these two hooks just as a keyboard command does.
Note that, when the buffer text includes very long lines, these two
hooks are called as if they were in a
(see Narrowing), with a
long-line-optimizations-in-command-hooks label and with the
buffer narrowed to a portion around point.