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 Event Input Misc).
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.
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-commandcontains the command that is about to run, and
last-commanddescribes the previous command. See Command Loop Info.
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-commandrefers to the command that just ran, and
last-commandrefers to the command before that.
This hook is also run when Emacs first enters the command loop (at which point
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) runs these two hooks just as a keyboard command does.