|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T c
Length: 43220 (0xa8d4) Types: TextFile Names: »command-loop.texinfo«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89 └─⟦c06c473ab⟧ »./UNRELEASED/lispref.tar.Z« └─⟦1b57a2ffe⟧ └─⟦this⟧ »command-loop.texinfo«
@setfilename ../info/commands @node Command Loop, Keymaps, Minibuffers, Top @chapter Command Loop @cindex editing command loop @cindex command loop When you run Emacs, it enters the @dfn{editing command loop} almost immediately. This loop reads key sequences, executes their definitions, and displays the results. In this chapter, we describe how these things are done, and the subroutines which allow Lisp programs to do them. @menu * Command Overview:: * Defining Commands:: * Interactive Call:: * Command Loop Info:: * Keyboard Input:: * Quitting:: * Prefix Command Arguments:: * Recursive Editing:: * Disabling Commands:: * Command History:: * Keyboard Macros:: @end menu @node Command Overview, Defining Commands, Command Loop, Command Loop @section Command Loop Overview The first thing the command loop must do is read a key sequence, which is a sequence of characters that translates into a command. It does this by calling the function @code{read-key-sequence}. Your Lisp code can call this function, too (@pxref{Keyboard Input}). Lisp programs can also do input at a lower level with @code{read-char} or discard the input with @code{discard-input}. The key sequence is translated into a command through the keymaps. @xref{Keymaps}, for information on how this is done. The result is a keyboard macro or an interactively callable function, or a symbol which leads to one of them. If the key is @kbd{M-x}, then it reads the name of another command which is used instead. This is done by the command @code{execute-extended-command} (@pxref{Interactive Call}). Now it is time to execute the command, which includes reading arguments to be given to it. This is done by calling @code{command-execute} (@pxref{Interactive Call}). For commands written in Lisp, the @code{interactive} specification says how to read the arguments. This may use the prefix argument (@pxref{Prefix Command Arguments}), or may read with prompting in the minibuffer (@pxref{Minibuffers}). For example, the command @code{find-file} has an @code{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 (i.e., a keyboard macro) then the function @code{execute-keyboard-macro} is used to execute it. You can call this function yourself. @xref{Keyboard Macros}. If a command runs away, typing @kbd{C-g} will terminate its execution immediately. This is called @dfn{quitting} (@pxref{Quitting}). @node Defining Commands, Interactive Call, Command Overview, Command Loop @section Defining Commands @cindex defining commands @cindex commands, defining them @cindex functions, making them interactive A Lisp function becomes a command when its body contains, at top level, a form which calls the special form @code{interactive}. This form does nothing when actually executed, but its mere presence is a flag noticed by the command loop, and its argument controls the reading of arguments, with the minibuffer or otherwise. @menu * Using interactive:: * Interactive Codes:: * interactive Examples:: @end menu @node Using interactive, Interactive Codes, Defining Commands, Defining Commands @subsection Using @code{interactive} @defspec interactive arg-descriptor @cindex argument descriptors This special form declares that the function in which it appears is a command, and therefore it may be called interactively (via @kbd{M-x} or by entering a key sequence bound to it). @var{arg-descriptor} declares how the arguments to the command are to be computed when the command is called interactively. The command may also be called from Lisp programs like any other function, but then the arguments are supplied by the caller, and @var{arg-descriptor} has no effect. The @code{interactive} form has its effect because the command loop (actually, its subroutine @code{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 @code{interactive} form are executed, but at this time @code{interactive} simply returns @code{nil} without even evaluating its argument. @end defspec There are three possibilities for the argument @var{arg-descriptor}: @itemize @bullet @item It may be omitted or @code{nil}. Then the command is called with no arguments. This leads quickly to an error if the command requires one or more arguments. @item It may be a Lisp expression which is not a string. Then it must be a form which is evaluated to get a list of arguments to pass to the command. @cindex argument evaluation form @item @cindex argument prompt It may be a string. The string should consist of a code character, followed by a prompt (if required for that code character). The prompt ends either with the end of the string or with a newline. Here is a simple example: @example (interactive "bFrobnicate buffer: ") @end example @noindent The code letter @samp{b} says to read the name of an existing buffer, with completion. The buffer will be the sole argument passed to the command. The rest of the string is a prompt. If the prompt ends with a newline, a second code character and prompt may follow, specifying a second argument. Any number of arguments may be specified in this way. @kindex * in interactive @cindex buffer-read-only error If the first character in the string is @samp{*}, then an error is signaled if the buffer is read-only. Otherwise, the following character is the first code character. @end itemize @node Interactive Codes, interactive Examples, Using interactive, Defining Commands @comment node-name, next, previous, up @subsection Code Characters for @code{interactive} @cindex code character description @cindex interactive code description @cindex description for interactive codes @cindex codes, interactive description @cindex characters for interactive codes The code character descriptions below contain a number of key words with the following meanings. @table @b @item Completion @cindex interactive completion Do completion. @key{TAB}, @key{SPC}, and @key{RET} will perform name completion according to the function @code{completing-read} (@pxref{Completion}). @item Existing @cindex No Match error Require the name of an existing object. An incorrect name will not be accepted (Emacs will complain @samp{[No Match]} and continue to prompt for a name). @item Default @cindex default argument string A default string is supplied by Emacs, which depends on the code character. @item Prompt @cindex argument prompt A prompt immediatly follows the code character. The prompt ends either with the end of the string or with a newline. @item No I/O This code letter computes an argument without reading any input. Therefore, it does not use a prompt string, and any prompt string you supply is ignored. @end table Here are the code character descriptions for use with @code{interactive}: @table @kbd @item a @cindex read function name A function name (i.e., a symbol which is @code{fboundp}). Existing, Completion, Prompt. @item b @cindex read buffer name The name of a buffer. By default, its directory is the current buffer (@pxref{Buffers}). Existing, Completion, Default, Prompt. @item B The name of a buffer. Completion, Prompt. @item c @cindex read character A character. The cursor does not move into the echo area. Prompt. @item C @cindex read command name A command name (i.e., an interactive function). Existing, Completion, Prompt. @item d @cindex position argument Use the position of the point as a number. No I/O. @item D @cindex read directory name A directory name. Default is current directory associated with the current buffer (@pxref{Operating System Environment}). Existing, Completion, Default, Prompt. @item f @cindex read file name A file name. Default is the @code{default-directory}. Existing, Completion, Default, Prompt. @item F A file name. The file need not exist. Completion, Default, Prompt. @item k @cindex read key sequence A key sequence. This keeps reading characters until a command (or undefined command) is found in the current key maps. The key sequence is handed to the function as a string. The cursor does not move into the echo area. Prompt. @item m @cindex marker argument The position of the mark as a number. No I/O. @item n @cindex read number The minibuffer is used to read a number. If the input is not a number, the user is asked to try again. The prefix argument, if any, is not used. Prompt. @item N @cindex unprocessed prefix argument The unprocessed prefix argument is passed to the function. If the prefix argument is @code{nil}, then a number is read as with @kbd{n}. Requires a number. Prompt. @item p @cindex processed prefix argument (lower case) The processed prefix argument is passed to the function. No I/O. @item P (upper case) The unprocessed prefix argument is passed to the function. No I/O. @xref{Prefix Command Arguments} @refill @item r @cindex region argument The point and the mark are passed to the function as two numeric arguments, smallest first. No I/O. @item s @cindex read string A string is read, without quotes, terminated with either @key{LFD} or @key{RET}. @kbd{C-q} may be used to include either into the string. Prompt. @item S @cindex read symbol A string is read, without quotes, terminated with any whitespace character, and it is interned as a symbol. @kbd{C-q} may be used to include whitespace into the string. Other characters which normally terminate a symbol (e.g., @kbd{()[]}) do not do so here. Prompt. @item v @cindex read variable @cindex read user option A variable declared to be a user option (i.e., satisfying @code{user-variable-p}). Existing, Completion, Prompt. @item x @cindex read sexp A Lisp form is read and terminated with a @key{LFD} or @key{RET}. The form is not evaluated. Prompt. @item X @cindex sexp argument evaluation A Lisp form is read as with @kbd{x}, but it is then evaluated and the result is passed to the function. Prompt. @end table @node interactive Examples, , Interactive Codes, Defining Commands @comment node-name, next, previous, up @subsection Examples of Using @code{interactive} @cindex examples of using @code{interactive} @cindex @code{interactive}, examples of using Here are some more examples of @code{interactive}: @example (defun foo1 () ; @r{@code{foo1} takes no arguments,} (interactive) ; @r{just moves forward two words.} (forward-word 2)) @result{} foo (defun foo2 (n) ; @r{@code{foo2} takes one argument,} (interactive "p") ; @r{which is the processed prefix.} (forward-word (* 2 n))) @result{} foo2 (defun foo3 (n) ; @r{@code{foo3} takes one argument,} (interactive "nCount:") ; @r{which is read from the Minibuffer.} (forward-word (* 2 n))) @result{} foo3 (defun three-b (b1 b2 b3) "Select three existing buffers (prompting for them in the Minibuffer). Put them into three windows, selecting the last one." (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:") (delete-other-windows) (split-window (selected-window) 8) (switch-to-buffer b1) (other-window 1) (split-window (selected-window) 8) (switch-to-buffer b2) (other-window 1) (switch-to-buffer b3) ) @result{} three-b (three-b "*scratch*" "declarations.texinfo" "*mail*") @result{} nil @end example @node Interactive Call, Command Loop Info, Defining Commands, Command Loop @section Interactive Call @cindex interactive call After the command loop has translated a key sequence into a definition, it invokes the definition using the function @code{command-execute}. If the definition is an ordinary command, that function calls @code{call-interactively}, which reads the arguments and calls the command. You can also call these functions yourself. @defun commandp object Returns @code{t} if @var{object} is suitable for calling interactively; that is, if @var{object} is a command. Otherwise, returns @code{nil}. The interactively callable objects are strings (treated as keyboard macros), lambda expressions that contain a top-level call to @code{interactive}, autoload-objects that are declared as interactive (non-@code{nil} fourth argument to @code{autoload}), and some of the primitive functions. Also, a symbol is @code{commandp} if its function definition is @code{commandp}. Keys and keymaps are not commands. Rather, they are used to look up commands (@pxref{Keymaps}). @end defun @defun call-interactively command &optional record-flag This function calls @var{command}, reading arguments according to its interactive calling specifications. It is an error if @var{command} is not an interactively callable function. @cindex record command history If @var{record-flag} is non-@code{nil}, then this command and its arguments are unconditionally added to the @code{command-history}. Otherwise, this is done only if the minibuffer is used to read an argument. @xref{Command History}. @end defun @defun command-execute command &optional record-flag @cindex keyboard macro execution This function executes @var{command} as an editor command. @var{command} must satisfy the @code{commandp} predicate: it must be an interactively callable function or a string. A string is executed with @code{execute-kbd-macro}. Along with the optional @var{record-flag}, a function is passed to @code{call-interactively}. A symbol is handled by using its function definition in its stead. A symbol with an @code{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 library and then rechecking the definition of the symbol. @end defun @deffn Command execute-extended-command prefix-argument @cindex read command name This primitive function reads a command name from the minibuffer. It calls the @code{completing-read} function (@pxref{Completion}). Then @code{execute-extended-command} the command read by @code{completing-read}, reading the arguments according to the command's @code{interactive} specifications. Whatever that command returns becomes the value of @code{execute-extended-command}. @cindex execute with prefix argument If the command asks for a prefix argument, the value @var{prefix-argument} is supplied to it. If @code{execute-extended-command} is called interactively, the raw prefix argument is used for @var{prefix-argument}, and thus passed on to whatever command is run. @c !!! Should this be @kindex? @cindex @kbd{M-x} @code{execute-extended-command} is the normal definition of @kbd{M-x}. It uses the string @samp{M-x } as a prompt. It would be better to take the prompt from whatever characters were used to invoke @code{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. @example (execute-extended-command 1) ---------- Buffer: Minibuffer ---------- M-x @b{forward-word RET} ---------- Buffer: Minibuffer ---------- @result{} t @end example @end deffn @defun interactive-p This function returns @code{t} if the containing function (the one which called @code{interactive-p}) was called interactively, with @code{call-interactively}. It makes no difference whether @code{call-interactively} was called from Lisp or by the editor command loop. But if the containing function was called from Lisp, then it was not called interactively. The usual use of @code{interactive-p} is for deciding whether to print an informative message. As a special exception, @code{interactive-p} returns @code{nil} whenever a keyboard macro is being run. This is to suppress the informative messages and speed execution of the macro. For example: @example (defun foo () (interactive) (and (interactive-p) (message "foo"))) @result{} foo (defun bar () (interactive) (setq foobar (list (foo) (interactive-p)))) @result{} bar ;; @r{Type @kbd{M-x foo}.} @print{} foo ;; @r{Although you cannot see it, this returns @code{"foo"}.} ;; @r{Type @kbd{M-x bar}.} ;; This does not print anything. foobar @result{} (nil t) @end example @end defun @node Command Loop Info, Keyboard Input, Interactive Call, Command Loop @comment node-name, next, previous, up @section Information from the Command Loop The editor command loop sets several Lisp variables to keep status records for itself or to provide them for commands that are run. @defvar last-command This global variable records the name of the previous command executed by the command loop, the one before the current command. Normally the value is a symbol with a function definition, but this is not guaranteed. This variable is set by copying the value of @code{this-command} when each command returns to the command loop, except for commands that specify a prefix argument. @end defvar @defvar this-command @cindex current command This global variable is records the name of the command now being executed by Emacs. As for @code{last-command}, normally it is a symbol with a function definition. This variable is set by the command loop just before the command is run, and its value is copied into @code{last-command} when the command finishes (unless the command specifies a prefix argument). @cindex kill command repetition Some commands change the value of this variable during their execution, simply as a flag for whatever command runs next. In particular, the functions that kill text always set @code{this-command} to @code{kill-region} so that any kill commands immediately following will know to append the killed text to the previous kill. @end defvar @defun this-command-keys This function returns a string of the key sequence that invoked the present command. It includes the characters that generated the prefix argument, if any. @example (this-command-keys) ;; @r{Now type @kbd{C-u C-x C-e}.} @result{} "^U^X^E" @end example @end defun @defvar last-command-char This global variable is set to the last character that was typed on the terminal and was part of a command. The principal use of this variable is in @code{self-insert-command}, which uses it to decide which character to insert. @example last-command-char ;; @r{Now type @kbd{C-u C-x C-e}.} @result{} 5 @end example @noindent The value is 5 because that is the @sc{ASCII} code for @kbd{C-e}. @end defvar @defvar echo-keystrokes This global variable determines how much time should elapse before command characters are echoed. Its value must be a number. If the user has typed a prefix key (say @kbd{C-x}) and then delays this many seconds before continuing, then the key @kbd{C-x} will be echoed in the echo area. Any subsequent keys will be echoed as well. If the value is 0, then prefix keys are never echoed. @end defvar @node Keyboard Input, Quitting, Command Loop Info, Command Loop @section Keyboard Input The editor command loop reads keyboard input using @code{read-key-sequence}, which uses @code{read-char}. These functions and others are also available for use in Lisp programs. Also, see @code{momentary-string-display} in @ref{Temporary Displays}. @defun input-pending-p @cindex waiting for command key input This function determines whether command input is currently available. It returns immediately and the result is @code{t} if so, @code{nil} otherwise. On rare occasions this command may return @code{t} even when no input is available. @end defun @defun discard-input @cindex flush input @cindex terminate keyboard macro This function discards the contents of the terminal input buffer and flushes any keyboard macro that might be in the process of definition. It returns @code{nil}. In the example, the user may type a bunch of characters right after starting the evaluation of the form. After the @code{sleep-for} finishes sleeping, any characters that have been typed are discarded. @findex sleep-for @r{example} @example (progn (sleep-for 2) (discard-input)) @result{} nil @end example @end defun @defun read-char @cindex keyboard macro characters This function reads a character from the command input (which is either direct keyboard input or characters coming from an executing keyboard macro), and returns it. It does not move the cursor or provide any sort of a prompt or other indication that it is waiting to read a character. However, if the user pauses, previous keystrokes may echo; see @code{echo-keystrokes} in @ref{Command Loop Info}. In the first example, the user types @kbd{1} (which is @sc{ASCII} code 49). The second example shows a keyboard macro definition which calls @code{read-char} from the minibuffer. The keyboard macro's very next character is the digit 1. This is the character read by @code{read-char}. @example (read-char) @result{} 49 (symbol-function 'foo) @result{} "^[^[(read-char)^M1" (execute-kbd-macro foo) @print{} 49 @result{} nil @end example @end defun @defun read-quoted-char &optional prompt @cindex octal characters This function is like @code{read-char}, except that if the first character read is an octal digit, it reads up to two more octal digits (0-7) until a non-octal digit is found and returns the character represented by the octal number consisting of those digits. Also, quitting is suppressed. @xref{Quitting}. If @var{prompt} is supplied, it specifies a string to use to prompt the user. The prompt string is always printed in the echo area and followed by a single @samp{-}. In the example, the user types in the octal number 177. This is 127 in decimal. @example (read-quoted-char "What character") ---------- Echo Area ---------- What character-@b{177} ---------- Echo Area ---------- @result{} 127 @end example @end defun @defun read-key-sequence prompt This function reads a key sequence and returns it as a string. Characters are read until the sequence is sufficient to specify a (non-prefix) command using the current local and global keymaps. @cindex uppercase key sequence @cindex downcasing in @code{lookup-key} For each character, if it is an uppercase letter that does not have a binding in either the local or global keymaps, then the corresponding lowercase letter is tried; if that is successful, the character is converted. This function is used to read command input; the key lookup is different from that of @code{lookup-key} because of this automatic downcasing. @vindex quit-flag @kindex C-g Quitting is suppressed inside @code{read-key-sequence}. In other words, a @kbd{C-g} typed while reading with this function is treated like any other character, and @code{quit-flag} is not set. @xref{Quitting}. @var{prompt} is either a string that is displayed in the echo area as a prompt, or @code{nil}, in which case no prompt is displayed. In the example, the prompt @kbd{?} is displayed in the echo area, and the user types @kbd{C-x C-f}. @example (read-key-sequence "?") ---------- Echo Area ---------- ?@b{C-x C-f} ---------- Echo Area ---------- @result{} "^X^F" @end example @end defun @defvar unread-command-char @cindex next input This global variable is set to the character to be read as the next input from the command input stream, or @kbd{-1} if there is no such character. Basically, this is the character that is ``unread'', when an input function has to read an extra character to finish parsing its input. @cindex prefix argument unreading For example, the function that governs prefix arguments reads any number of digits. When it finds a non-digit character, it must unread that so that it becomes input for the next command. @end defvar For more information, see @code{waiting-for-user-input-p} in @ref{Receiving Information from Processes}. See @code{sit-for} in @ref{Waiting}. @xref{Terminal Input}, for other functions and variables related to command key input. @node Quitting, Prefix Command Arguments, Keyboard Input, Command Loop @section Quitting @kindex C-g @cindex quit error @cindex quit Typing @kbd{C-g} while the command loop has run a Lisp function causes Emacs to @dfn{quit} whatever it is doing. This means that control returns to the innermost active command loop. Typing @kbd{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 @kbd{C-g} normally runs the command @code{keyboard-quit}, whose effect is to quit. However, when @kbd{C-g} follows a prefix key, the result is an undefined key. All this does is cancel the prefix key and any prefix argument. In the minibuffer, @kbd{C-g} has a different definition, which 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 @emph{within} the minibuffer.) This feature is the reason why @kbd{C-g} does not quit directly when the command reader is reading input. @kbd{C-g} following a prefix key is not redefined in the minibuffer; it has its normal effect, canceling the prefix key and prefix argument as usual. Certain functions such as @code{read-key-sequence} or @code{read-quoted-char} prevent quitting entirely even though they wait for input. Instead of quitting, @kbd{C-g} serves as the requested input. In the case of @code{read-key-sequence}, this serves to bring about the special behavior of @kbd{C-g} in the command loop. In the case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used to quote an @kbd{C-g}. The only direct effect of typing @kbd{C-g} is to set the variable @code{quit-flag} to a non-@code{nil} value. Appropriate places inside Emacs check this variable and quit if it is not @code{nil}. Setting @code{quit-flag} non-@code{nil} in any other way also causes a quit. At the level of C code, quits cannot happen just anywhere; only at particular places which check @code{quit-flag}. This is so quitting will not leave an inconsistency in Emacs's internal state. Instead, the quit is delayed until a safe point, such as when the primitive finishes or tries to wait for input. You can prevent quitting for a portion of a Lisp function by binding the variable @code{inhibit-quit} to a non-@code{nil} value. @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, but the usual result of this---a quit---is prevented. When the binding is unwound at the end of the @code{let} form, if @code{quit-flag} is still non-@code{nil}, the requested quit happens immediately. This is exactly what you want for a ``critical section'', where you simply wish quitting not to happen at a certain point in the program. @cindex @code{isearch-forward} quitting If you wish to handle @kbd{C-g} in a completely different way, so there should be no quit at all, then you should set @code{quit-flag} to @code{nil} before unbinding @code{inhibit-quit}. This excerpt from the definition of @code{read-quoted-char} shows how this is done; it also shows that normal quitting is permitted after the first character of input. @example (defun read-quoted-char (&optional prompt) "@dots{}@var{documentation}@dots{}" (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))) @dots{}) (logand 255 code))) @end example @defvar quit-flag If this variable is non-@code{nil}, then Emacs quits immediately, unless @code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} sets @code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}. @end defvar @defvar inhibit-quit This variable determines whether Emacs should quit when @code{quit-flag} is set to @code{t}. If @code{inhibit-quit} is non-@code{nil}, then @code{quit-flag} has no special effect. @end defvar @deffn Command keyboard-quit This function signals the @code{quit} condition with @code{(signal 'quit)}. This is the same thing that quitting does. (See @code{signal} in @ref{Errors}.) @end deffn @node Prefix Command Arguments, Recursive Editing, Quitting, Command Loop @section 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 represented by a value which is always available (though it may be @code{nil}, meaning there is no prefix argument); each command may use it or ignore it. There are two representations of the prefix argument: @dfn{raw} and @dfn{numeric}. Emacs uses the raw representation internally, and so do the Lisp variables which store the information, but commands can request either representation. Here are the possible values of a raw prefix argument: @itemize @bullet @item @code{nil}, meaning there is no prefix argument. Its numeric value is 1, but numerous commands make a distinction. @item An integer, which stands for itself. @item A list of one element, that being an integer. This form of prefix argument results from a succession of @kbd{C-u}'s with no digits. The numeric value is that integer, but some commands make a distinction. @item The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was typed without digits. The numeric value is @minus{}1, but some commands make a distinction. @end itemize There are two variables used to store the prefix argument: @code{prefix-arg} and @code{current-prefix-arg}. Commands such as @code{universal-argument} which create prefix arguments store them in @code{prefix-arg}. In contrast, @code{current-prefix-arg} conveys the prefix argument to the current command; setting that variable has no effect on the prefix arguments for future commands. Normally, commands specify which kind of the argument they want to see, either processed or unprocessed, in the @code{interactive} declaration. (@xref{Interactive Call}.) Alternatively, functions may look at the value of the prefix argument directly in the variable @code{current-prefix-arg}. Don't call @code{universal-argument}, @code{digit-argument}, or @code{negative-argument} unless you intend to let the user enter the prefix argument for the @emph{next} command. @cindex next command @deffn Command universal-argument This command reads input and specifies a prefix argument for the following command. Don't call this command yourself unless you are a real wizard. @end deffn @deffn Command digit-argument arg This command constructs part of the prefix argument for the following command. The argument @var{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 are a real wizard. @end deffn @deffn Command negative-argument arg This command constructs part of the numeric argument for the next command. The argument @var{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 are a real wizard. @end deffn @defun prefix-numeric-value arg This function returns the numeric meaning of the raw prefix argument, @var{arg}. A raw prefix argument may be a symbol, a number, or a list. If it is @code{nil}, then the value 1 is returned. If it is any other symbol, then the value @minus{}1 is returned. If it is a number, that number is returned, and if it is a list, then the @sc{car} of that list (which should be a number) is returned. @end defun @defvar current-prefix-arg This variable is the value of the raw prefix argument for the @emph{current} command. Commands may examine it directly, but the usual way to access it is with @code{(interactive "P")}. @end defvar @defvar prefix-arg The value of this variable is the raw prefix argument for the @emph{next} editing command. Commands which specify prefix arguments set this variable. @end defvar @node Recursive Editing, Disabling Commands, Prefix Command Arguments, Command Loop @section Recursive Editing @cindex recursive command loop @cindex recursive editing level @cindex command loop, recursive The Emacs command loop is entered automatically when Emacs starts up. This top-level invocation of the command loop is never exited until the Emacs is killed. Lisp programs can also invoke the command loop. Since this makes more than one activation of the command loop, we call it @dfn{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 same commands are available during recursive editing as are available in the top-level editing loop, depending on the major mode, of course. Only a few special commands exit the recursive editing level; the others remain within it. (These special commands are always available, but are useless 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. @cindex minibuffer input 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. Keystrokes behave differently in the minibuffer, but that is only because of the minibuffer's local map; if you switch windows, you get the usual Emacs commands. @cindex @code{throw} example @kindex exit @cindex exit recursive editing To invoke a recursive editing level, call the function @code{recursive-edit}. This function contains the command loop. It also contains a call to @code{catch} with tag @code{exit}; this makes it possible to exit the recursive editing level by throwing to @code{exit}. @xref{Catch and Throw}. @cindex aborting If you throw a value other than @code{t}, then @code{recursive-edit} returns normally to the function which called it. The command @kbd{C-M-c} (@code{exit-recursive-edit}) does this. Throwing a @code{t} value causes @code{recursive-edit} to quit, so that control returns to the command loop one level up. This is called @dfn{aborting}, and is done by @kbd{C-]} (@code{abort-recursive-edit}). Most applications should not use recursive editing, except for the minibuffer. Usually it is better for the user to change the major mode of the current buffer temporarily to a special, new mode, which has a command to go back to the previous mode. This technique is used by the @kbd{w} command in Rmail, for example. 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 @kbd{m} command in Rmail does this. One place where recursive edits are useful is in debugging. You can insert a call to @code{recursive-edit} into a function as a sort of breakpoint, so that you can look around when you get there. Even better, instead of calling @code{recursive-edit} directly, call @code{debug}, which uses a recursive edit but also provides the other features of the debugger. Recursive editing levels are also used when you type @kbd{C-r} in @code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}). @defun recursive-edit @cindex suspend evaluation This function invokes the editor command loop. It is called automatically by the initialization of Emacs to begin editing. When called from a Lisp program, it enters a recursive editing level. In the example, the function @code{simple-rec} first advances the point one word, then enters a recursive edit, printing out a message in the echo area. The user can then do any editing desired, then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}. @example (defun simple-rec () (forward-word 1) (message "Recursive edit in progress.") (recursive-edit) (forward-word 1)) @result{} simple-rec (simple-rec) @result{} nil @end example @end defun @deffn Command exit-recursive-edit This function exits from the innermost recursive edit (including minibuffer input). Its definition is effectively @code{(throw 'exit nil)}. @end deffn @deffn Command abort-recursive-edit @cindex quit error This function aborts the command that requested the innermost recursive edit (including minibuffer input), by signaling a @code{quit} error after exiting the recursive edit. Its definition is effectively @code{(throw 'exit t)}. @end deffn @deffn Command top-level This function exits all recursive editing levels. It does not return a value, as it essentially jumps completely out of any computation directly back into the main command loop. @end deffn @defun recursion-depth This function returns the current depth of recursive edits. When no recursive edit is active, it returns 0. @end defun @node Disabling Commands, Command History, Recursive Editing, Command Loop @section Disabling Commands @cindex disabled command Disabling a command marks the command as requiring confirmation before it can be executed. The purpose of disabling a command is to prevent users from executing it by accident, which could be confusing or damaging. @cindex disabled property @pindex .emacs The direct mechanism for disabling a command is to have a non-@code{nil} @code{disabled} property on the Lisp symbol for the command. These properties are normally set up by the user's @file{.emacs} file with Lisp expressions such as @example (put 'upcase-region 'disabled t) @end example @noindent For a few commands, these properties are present by default and may be removed by the @file{.emacs} file. If the value of the @code{disabled} property is a string, that string is included in the message printed when the command is used: @example (put 'delete-region 'disabled "Text deleted this way cannot be yanked back!\n") @end example @xref{Disabling Commands,,, emacs, The GNU Emacs Manual}, for the details on what happens when a disabled command is invoked. Disabling a command has no effect on calling it as a function from Lisp programs. @deffn Command enable-command command Allow @var{command} to be executed without special confirmation from now on. The user's @file{.emacs} file is optionally altered so that this will apply to future sessions. @end deffn @deffn Command disable-command command Require special confirmation to execute @var{command} from now on. The user's @file{.emacs} file is optionally altered so that this will apply to future sessions. @end deffn @defvar disabled-command-hook The value of this variable is a function to be called instead of any command that is disabled (i.e., that has a non-@code{nil} disabled property). By default, the value of @code{disabled-command-hook} is a function defined to ask the user whether to proceed. @end defvar @node Command History, Keyboard Macros, Disabling Commands, Command Loop @section Command History @cindex command history @cindex complex command Emacs keeps a history of the complex commands that have been executed, to make it easy to repeat these commands. A @dfn{complex command} is defined to be one whose arguments are read using the minibuffer. This includes any @kbd{M-x} command, any @kbd{M-ESC} command, and any command whose @code{interactive} specification reads an argument from the minibuffer. It does @emph{not} include commands because they use the minibuffer explicitly once they are called. @defvar command-history This global variable's value is a list of recent commands. Each command is represented as a form to evaluate. It continues to accumulate all complex commands for the duration of the editing session, but at garbage collection time all but the first (most recent) thirty elements are deleted. @example command-history @result{} ((switch-to-buffer "chistory.texinfo") (describe-key "^X^[") (visit-tags-table "~/emacs/src/") (find-tag "repeat-complex-command")) @end example @end defvar There are a number of commands and even two entire modes devoted to facilitating the editing and recall of previous commands. The commands @code{repeat-complex-command}, and @code{list-command-history} are described in the user manual (@pxref{Command History,,, emacs, The GNU Emacs Manual}). @defvar repeat-complex-command-map The value of this variable is a sparse keymap used by the minibuffer when attempting to repeat a ``complex'' command. @end defvar @node Keyboard Macros, , Command History, Command Loop @section Keyboard Macros @cindex keyboard macros A keyboard macro is a canned sequence of keystrokes that can be considered a command and made the definition of a key. Don't confuse keyboard macros with Lisp macros (@pxref{Macros}). @defun execute-kbd-macro macro &optional count This function executes @var{macro} as a string of editor commands. If @var{macro} is a string, then the characters in that string are executed exactly as if they had been typed to Emacs. If @var{macro} is a symbol, then its function definition is used in place of @var{macro}. If that is another symbol, this process repeats. Eventually the result should be a string. If the result is neither a symbol nor a string, an error is signaled. The argument @var{count} is a repeat count; @var{macro} is executed that many times. If @var{count} is omitted or @code{nil}, @var{macro} is executed once. If it is 0, @var{macro} is executed over and over until it encounters an error or a failing search. @end defun @defvar last-kbd-macro This variable is the definition of the most recently defined keyboard macro. Its value should be a string of characters, or @code{nil}. @end defvar @defvar defining-kbd-macro This variable indicates whether a keyboard macro is being defined. It is set to @code{t} by @code{start-kbd-macro}, and @code{nil} by @code{end-kbd-macro}. Do not set this variable yourself! @end defvar @defvar executing-macro This variable contains the string that defines the keyboard macro that is currently executing. It is @code{nil} if no macro is currently executing. @end defvar @defvar executing-macro-index This variable is the number of characters already executed from the currently executing keyboard macro. It increments as the macro is executed. @end defvar The user-level commands for defining, running and editing keyboard macros include @code{call-last-kbd-macro}, @code{insert-kbd-macro}, @code{start-kbd-macro}, @code{end-kbd-macro}, @code{kbd-macro-query}, and @code{name-last-kbd-macro}. They are described in the user's manual (@pxref{Keyboard Macros,,, emacs, The GNU Emacs Manual}).