| 1 |
@c -*-texinfo-*- |
|---|
| 2 |
@c This is part of the GNU Emacs Lisp Reference Manual. |
|---|
| 3 |
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001, 2002, |
|---|
| 4 |
@c 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
|---|
| 5 |
@c See the file elisp.texi for copying conditions. |
|---|
| 6 |
@setfilename ../info/commands |
|---|
| 7 |
@node Command Loop, Keymaps, Minibuffers, Top |
|---|
| 8 |
@chapter Command Loop |
|---|
| 9 |
@cindex editor command loop |
|---|
| 10 |
@cindex command loop |
|---|
| 11 |
|
|---|
| 12 |
When you run Emacs, it enters the @dfn{editor command loop} almost |
|---|
| 13 |
immediately. This loop reads key sequences, executes their definitions, |
|---|
| 14 |
and displays the results. In this chapter, we describe how these things |
|---|
| 15 |
are done, and the subroutines that allow Lisp programs to do them. |
|---|
| 16 |
|
|---|
| 17 |
@menu |
|---|
| 18 |
* Command Overview:: How the command loop reads commands. |
|---|
| 19 |
* Defining Commands:: Specifying how a function should read arguments. |
|---|
| 20 |
* Interactive Call:: Calling a command, so that it will read arguments. |
|---|
| 21 |
* Distinguish Interactive:: Making a command distinguish interactive calls. |
|---|
| 22 |
* Command Loop Info:: Variables set by the command loop for you to examine. |
|---|
| 23 |
* Adjusting Point:: Adjustment of point after a command. |
|---|
| 24 |
* Input Events:: What input looks like when you read it. |
|---|
| 25 |
* Reading Input:: How to read input events from the keyboard or mouse. |
|---|
| 26 |
* Special Events:: Events processed immediately and individually. |
|---|
| 27 |
* Waiting:: Waiting for user input or elapsed time. |
|---|
| 28 |
* Quitting:: How @kbd{C-g} works. How to catch or defer quitting. |
|---|
| 29 |
* Prefix Command Arguments:: How the commands to set prefix args work. |
|---|
| 30 |
* Recursive Editing:: Entering a recursive edit, |
|---|
| 31 |
and why you usually shouldn't. |
|---|
| 32 |
* Disabling Commands:: How the command loop handles disabled commands. |
|---|
| 33 |
* Command History:: How the command history is set up, and how accessed. |
|---|
| 34 |
* Keyboard Macros:: How keyboard macros are implemented. |
|---|
| 35 |
@end menu |
|---|
| 36 |
|
|---|
| 37 |
@node Command Overview |
|---|
| 38 |
@section Command Loop Overview |
|---|
| 39 |
|
|---|
| 40 |
The first thing the command loop must do is read a key sequence, which |
|---|
| 41 |
is a sequence of events that translates into a command. It does this by |
|---|
| 42 |
calling the function @code{read-key-sequence}. Your Lisp code can also |
|---|
| 43 |
call this function (@pxref{Key Sequence Input}). Lisp programs can also |
|---|
| 44 |
do input at a lower level with @code{read-event} (@pxref{Reading One |
|---|
| 45 |
Event}) or discard pending input with @code{discard-input} |
|---|
| 46 |
(@pxref{Event Input Misc}). |
|---|
| 47 |
|
|---|
| 48 |
The key sequence is translated into a command through the currently |
|---|
| 49 |
active keymaps. @xref{Key Lookup}, for information on how this is done. |
|---|
| 50 |
The result should be a keyboard macro or an interactively callable |
|---|
| 51 |
function. If the key is @kbd{M-x}, then it reads the name of another |
|---|
| 52 |
command, which it then calls. This is done by the command |
|---|
| 53 |
@code{execute-extended-command} (@pxref{Interactive Call}). |
|---|
| 54 |
|
|---|
| 55 |
To execute a command requires first reading the arguments for it. |
|---|
| 56 |
This is done by calling @code{command-execute} (@pxref{Interactive |
|---|
| 57 |
Call}). For commands written in Lisp, the @code{interactive} |
|---|
| 58 |
specification says how to read the arguments. This may use the prefix |
|---|
| 59 |
argument (@pxref{Prefix Command Arguments}) or may read with prompting |
|---|
| 60 |
in the minibuffer (@pxref{Minibuffers}). For example, the command |
|---|
| 61 |
@code{find-file} has an @code{interactive} specification which says to |
|---|
| 62 |
read a file name using the minibuffer. The command's function body does |
|---|
| 63 |
not use the minibuffer; if you call this command from Lisp code as a |
|---|
| 64 |
function, you must supply the file name string as an ordinary Lisp |
|---|
| 65 |
function argument. |
|---|
| 66 |
|
|---|
| 67 |
If the command is a string or vector (i.e., a keyboard macro) then |
|---|
| 68 |
@code{execute-kbd-macro} is used to execute it. You can call this |
|---|
| 69 |
function yourself (@pxref{Keyboard Macros}). |
|---|
| 70 |
|
|---|
| 71 |
To terminate the execution of a running command, type @kbd{C-g}. This |
|---|
| 72 |
character causes @dfn{quitting} (@pxref{Quitting}). |
|---|
| 73 |
|
|---|
| 74 |
@defvar pre-command-hook |
|---|
| 75 |
The editor command loop runs this normal hook before each command. At |
|---|
| 76 |
that time, @code{this-command} contains the command that is about to |
|---|
| 77 |
run, and @code{last-command} describes the previous command. |
|---|
| 78 |
@xref{Command Loop Info}. |
|---|
| 79 |
@end defvar |
|---|
| 80 |
|
|---|
| 81 |
@defvar post-command-hook |
|---|
| 82 |
The editor command loop runs this normal hook after each command |
|---|
| 83 |
(including commands terminated prematurely by quitting or by errors), |
|---|
| 84 |
and also when the command loop is first entered. At that time, |
|---|
| 85 |
@code{this-command} refers to the command that just ran, and |
|---|
| 86 |
@code{last-command} refers to the command before that. |
|---|
| 87 |
@end defvar |
|---|
| 88 |
|
|---|
| 89 |
Quitting is suppressed while running @code{pre-command-hook} and |
|---|
| 90 |
@code{post-command-hook}. If an error happens while executing one of |
|---|
| 91 |
these hooks, it terminates execution of the hook, and clears the hook |
|---|
| 92 |
variable to @code{nil} so as to prevent an infinite loop of errors. |
|---|
| 93 |
|
|---|
| 94 |
A request coming into the Emacs server (@pxref{Emacs Server,,, |
|---|
| 95 |
emacs, The GNU Emacs Manual}) runs these two hooks just as a keyboard |
|---|
| 96 |
command does. |
|---|
| 97 |
|
|---|
| 98 |
@node Defining Commands |
|---|
| 99 |
@section Defining Commands |
|---|
| 100 |
@cindex defining commands |
|---|
| 101 |
@cindex commands, defining |
|---|
| 102 |
@cindex functions, making them interactive |
|---|
| 103 |
@cindex interactive function |
|---|
| 104 |
|
|---|
| 105 |
A Lisp function becomes a command when its body contains, at top |
|---|
| 106 |
level, a form that calls the special form @code{interactive}. This |
|---|
| 107 |
form does nothing when actually executed, but its presence serves as a |
|---|
| 108 |
flag to indicate that interactive calling is permitted. Its argument |
|---|
| 109 |
controls the reading of arguments for an interactive call. |
|---|
| 110 |
|
|---|
| 111 |
@menu |
|---|
| 112 |
* Using Interactive:: General rules for @code{interactive}. |
|---|
| 113 |
* Interactive Codes:: The standard letter-codes for reading arguments |
|---|
| 114 |
in various ways. |
|---|
| 115 |
* Interactive Examples:: Examples of how to read interactive arguments. |
|---|
| 116 |
@end menu |
|---|
| 117 |
|
|---|
| 118 |
@node Using Interactive |
|---|
| 119 |
@subsection Using @code{interactive} |
|---|
| 120 |
@cindex arguments, interactive entry |
|---|
| 121 |
|
|---|
| 122 |
This section describes how to write the @code{interactive} form that |
|---|
| 123 |
makes a Lisp function an interactively-callable command, and how to |
|---|
| 124 |
examine a command's @code{interactive} form. |
|---|
| 125 |
|
|---|
| 126 |
@defspec interactive arg-descriptor |
|---|
| 127 |
This special form declares that the function in which it appears is a |
|---|
| 128 |
command, and that it may therefore be called interactively (via |
|---|
| 129 |
@kbd{M-x} or by entering a key sequence bound to it). The argument |
|---|
| 130 |
@var{arg-descriptor} declares how to compute the arguments to the |
|---|
| 131 |
command when the command is called interactively. |
|---|
| 132 |
|
|---|
| 133 |
A command may be called from Lisp programs like any other function, but |
|---|
| 134 |
then the caller supplies the arguments and @var{arg-descriptor} has no |
|---|
| 135 |
effect. |
|---|
| 136 |
|
|---|
| 137 |
The @code{interactive} form has its effect because the command loop |
|---|
| 138 |
(actually, its subroutine @code{call-interactively}) scans through the |
|---|
| 139 |
function definition looking for it, before calling the function. Once |
|---|
| 140 |
the function is called, all its body forms including the |
|---|
| 141 |
@code{interactive} form are executed, but at this time |
|---|
| 142 |
@code{interactive} simply returns @code{nil} without even evaluating its |
|---|
| 143 |
argument. |
|---|
| 144 |
@end defspec |
|---|
| 145 |
|
|---|
| 146 |
There are three possibilities for the argument @var{arg-descriptor}: |
|---|
| 147 |
|
|---|
| 148 |
@itemize @bullet |
|---|
| 149 |
@item |
|---|
| 150 |
It may be omitted or @code{nil}; then the command is called with no |
|---|
| 151 |
arguments. This leads quickly to an error if the command requires one |
|---|
| 152 |
or more arguments. |
|---|
| 153 |
|
|---|
| 154 |
@item |
|---|
| 155 |
It may be a string; then its contents should consist of a code character |
|---|
| 156 |
followed by a prompt (which some code characters use and some ignore). |
|---|
| 157 |
The prompt ends either with the end of the string or with a newline. |
|---|
| 158 |
Here is a simple example: |
|---|
| 159 |
|
|---|
| 160 |
@smallexample |
|---|
| 161 |
(interactive "bFrobnicate buffer: ") |
|---|
| 162 |
@end smallexample |
|---|
| 163 |
|
|---|
| 164 |
@noindent |
|---|
| 165 |
The code letter @samp{b} says to read the name of an existing buffer, |
|---|
| 166 |
with completion. The buffer name is the sole argument passed to the |
|---|
| 167 |
command. The rest of the string is a prompt. |
|---|
| 168 |
|
|---|
| 169 |
If there is a newline character in the string, it terminates the prompt. |
|---|
| 170 |
If the string does not end there, then the rest of the string should |
|---|
| 171 |
contain another code character and prompt, specifying another argument. |
|---|
| 172 |
You can specify any number of arguments in this way. |
|---|
| 173 |
|
|---|
| 174 |
@c Emacs 19 feature |
|---|
| 175 |
The prompt string can use @samp{%} to include previous argument values |
|---|
| 176 |
(starting with the first argument) in the prompt. This is done using |
|---|
| 177 |
@code{format} (@pxref{Formatting Strings}). For example, here is how |
|---|
| 178 |
you could read the name of an existing buffer followed by a new name to |
|---|
| 179 |
give to that buffer: |
|---|
| 180 |
|
|---|
| 181 |
@smallexample |
|---|
| 182 |
@group |
|---|
| 183 |
(interactive "bBuffer to rename: \nsRename buffer %s to: ") |
|---|
| 184 |
@end group |
|---|
| 185 |
@end smallexample |
|---|
| 186 |
|
|---|
| 187 |
@cindex @samp{*} in @code{interactive} |
|---|
| 188 |
@cindex read-only buffers in interactive |
|---|
| 189 |
If the first character in the string is @samp{*}, then an error is |
|---|
| 190 |
signaled if the buffer is read-only. |
|---|
| 191 |
|
|---|
| 192 |
@cindex @samp{@@} in @code{interactive} |
|---|
| 193 |
@c Emacs 19 feature |
|---|
| 194 |
If the first character in the string is @samp{@@}, and if the key |
|---|
| 195 |
sequence used to invoke the command includes any mouse events, then |
|---|
| 196 |
the window associated with the first of those events is selected |
|---|
| 197 |
before the command is run. |
|---|
| 198 |
|
|---|
| 199 |
You can use @samp{*} and @samp{@@} together; the order does not matter. |
|---|
| 200 |
Actual reading of arguments is controlled by the rest of the prompt |
|---|
| 201 |
string (starting with the first character that is not @samp{*} or |
|---|
| 202 |
@samp{@@}). |
|---|
| 203 |
|
|---|
| 204 |
@item |
|---|
| 205 |
It may be a Lisp expression that is not a string; then it should be a |
|---|
| 206 |
form that is evaluated to get a list of arguments to pass to the |
|---|
| 207 |
command. Usually this form will call various functions to read input |
|---|
| 208 |
from the user, most often through the minibuffer (@pxref{Minibuffers}) |
|---|
| 209 |
or directly from the keyboard (@pxref{Reading Input}). |
|---|
| 210 |
|
|---|
| 211 |
Providing point or the mark as an argument value is also common, but |
|---|
| 212 |
if you do this @emph{and} read input (whether using the minibuffer or |
|---|
| 213 |
not), be sure to get the integer values of point or the mark after |
|---|
| 214 |
reading. The current buffer may be receiving subprocess output; if |
|---|
| 215 |
subprocess output arrives while the command is waiting for input, it |
|---|
| 216 |
could relocate point and the mark. |
|---|
| 217 |
|
|---|
| 218 |
Here's an example of what @emph{not} to do: |
|---|
| 219 |
|
|---|
| 220 |
@smallexample |
|---|
| 221 |
(interactive |
|---|
| 222 |
(list (region-beginning) (region-end) |
|---|
| 223 |
(read-string "Foo: " nil 'my-history))) |
|---|
| 224 |
@end smallexample |
|---|
| 225 |
|
|---|
| 226 |
@noindent |
|---|
| 227 |
Here's how to avoid the problem, by examining point and the mark after |
|---|
| 228 |
reading the keyboard input: |
|---|
| 229 |
|
|---|
| 230 |
@smallexample |
|---|
| 231 |
(interactive |
|---|
| 232 |
(let ((string (read-string "Foo: " nil 'my-history))) |
|---|
| 233 |
(list (region-beginning) (region-end) string))) |
|---|
| 234 |
@end smallexample |
|---|
| 235 |
|
|---|
| 236 |
@strong{Warning:} the argument values should not include any data |
|---|
| 237 |
types that can't be printed and then read. Some facilities save |
|---|
| 238 |
@code{command-history} in a file to be read in the subsequent |
|---|
| 239 |
sessions; if a command's arguments contain a data type that prints |
|---|
| 240 |
using @samp{#<@dots{}>} syntax, those facilities won't work. |
|---|
| 241 |
|
|---|
| 242 |
There are, however, a few exceptions: it is ok to use a limited set of |
|---|
| 243 |
expressions such as @code{(point)}, @code{(mark)}, |
|---|
| 244 |
@code{(region-beginning)}, and @code{(region-end)}, because Emacs |
|---|
| 245 |
recognizes them specially and puts the expression (rather than its |
|---|
| 246 |
value) into the command history. To see whether the expression you |
|---|
| 247 |
wrote is one of these exceptions, run the command, then examine |
|---|
| 248 |
@code{(car command-history)}. |
|---|
| 249 |
@end itemize |
|---|
| 250 |
|
|---|
| 251 |
@cindex examining the @code{interactive} form |
|---|
| 252 |
@defun interactive-form function |
|---|
| 253 |
This function returns the @code{interactive} form of @var{function}. |
|---|
| 254 |
If @var{function} is an interactively callable function |
|---|
| 255 |
(@pxref{Interactive Call}), the value is the command's |
|---|
| 256 |
@code{interactive} form @code{(interactive @var{spec})}, which |
|---|
| 257 |
specifies how to compute its arguments. Otherwise, the value is |
|---|
| 258 |
@code{nil}. If @var{function} is a symbol, its function definition is |
|---|
| 259 |
used. |
|---|
| 260 |
@end defun |
|---|
| 261 |
|
|---|
| 262 |
@node Interactive Codes |
|---|
| 263 |
@comment node-name, next, previous, up |
|---|
| 264 |
@subsection Code Characters for @code{interactive} |
|---|
| 265 |
@cindex interactive code description |
|---|
| 266 |
@cindex description for interactive codes |
|---|
| 267 |
@cindex codes, interactive, description of |
|---|
| 268 |
@cindex characters for interactive codes |
|---|
| 269 |
|
|---|
| 270 |
The code character descriptions below contain a number of key words, |
|---|
| 271 |
defined here as follows: |
|---|
| 272 |
|
|---|
| 273 |
@table @b |
|---|
| 274 |
@item Completion |
|---|
| 275 |
@cindex interactive completion |
|---|
| 276 |
Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name |
|---|
| 277 |
completion because the argument is read using @code{completing-read} |
|---|
| 278 |
(@pxref{Completion}). @kbd{?} displays a list of possible completions. |
|---|
| 279 |
|
|---|
| 280 |
@item Existing |
|---|
| 281 |
Require the name of an existing object. An invalid name is not |
|---|
| 282 |
accepted; the commands to exit the minibuffer do not exit if the current |
|---|
| 283 |
input is not valid. |
|---|
| 284 |
|
|---|
| 285 |
@item Default |
|---|
| 286 |
@cindex default argument string |
|---|
| 287 |
A default value of some sort is used if the user enters no text in the |
|---|
| 288 |
minibuffer. The default depends on the code character. |
|---|
| 289 |
|
|---|
| 290 |
@item No I/O |
|---|
| 291 |
This code letter computes an argument without reading any input. |
|---|
| 292 |
Therefore, it does not use a prompt string, and any prompt string you |
|---|
| 293 |
supply is ignored. |
|---|
| 294 |
|
|---|
| 295 |
Even though the code letter doesn't use a prompt string, you must follow |
|---|
| 296 |
it with a newline if it is not the last code character in the string. |
|---|
| 297 |
|
|---|
| 298 |
@item Prompt |
|---|
| 299 |
A prompt immediately follows the code character. The prompt ends either |
|---|
| 300 |
with the end of the string or with a newline. |
|---|
| 301 |
|
|---|
| 302 |
@item Special |
|---|
| 303 |
This code character is meaningful only at the beginning of the |
|---|
| 304 |
interactive string, and it does not look for a prompt or a newline. |
|---|
| 305 |
It is a single, isolated character. |
|---|
| 306 |
@end table |
|---|
| 307 |
|
|---|
| 308 |
@cindex reading interactive arguments |
|---|
| 309 |
Here are the code character descriptions for use with @code{interactive}: |
|---|
| 310 |
|
|---|
| 311 |
@table @samp |
|---|
| 312 |
@item * |
|---|
| 313 |
Signal an error if the current buffer is read-only. Special. |
|---|
| 314 |
|
|---|
| 315 |
@item @@ |
|---|
| 316 |
Select the window mentioned in the first mouse event in the key |
|---|
| 317 |
sequence that invoked this command. Special. |
|---|
| 318 |
|
|---|
| 319 |
@item a |
|---|
| 320 |
A function name (i.e., a symbol satisfying @code{fboundp}). Existing, |
|---|
| 321 |
Completion, Prompt. |
|---|
| 322 |
|
|---|
| 323 |
@item b |
|---|
| 324 |
The name of an existing buffer. By default, uses the name of the |
|---|
| 325 |
current buffer (@pxref{Buffers}). Existing, Completion, Default, |
|---|
| 326 |
Prompt. |
|---|
| 327 |
|
|---|
| 328 |
@item B |
|---|
| 329 |
A buffer name. The buffer need not exist. By default, uses the name of |
|---|
| 330 |
a recently used buffer other than the current buffer. Completion, |
|---|
| 331 |
Default, Prompt. |
|---|
| 332 |
|
|---|
| 333 |
@item c |
|---|
| 334 |
A character. The cursor does not move into the echo area. Prompt. |
|---|
| 335 |
|
|---|
| 336 |
@item C |
|---|
| 337 |
A command name (i.e., a symbol satisfying @code{commandp}). Existing, |
|---|
| 338 |
Completion, Prompt. |
|---|
| 339 |
|
|---|
| 340 |
@item d |
|---|
| 341 |
@cindex position argument |
|---|
| 342 |
The position of point, as an integer (@pxref{Point}). No I/O. |
|---|
| 343 |
|
|---|
| 344 |
@item D |
|---|
| 345 |
A directory name. The default is the current default directory of the |
|---|
| 346 |
current buffer, @code{default-directory} (@pxref{File Name Expansion}). |
|---|
| 347 |
Existing, Completion, Default, Prompt. |
|---|
| 348 |
|
|---|
| 349 |
@item e |
|---|
| 350 |
The first or next mouse event in the key sequence that invoked the command. |
|---|
| 351 |
More precisely, @samp{e} gets events that are lists, so you can look at |
|---|
| 352 |
the data in the lists. @xref{Input Events}. No I/O. |
|---|
| 353 |
|
|---|
| 354 |
You can use @samp{e} more than once in a single command's interactive |
|---|
| 355 |
specification. If the key sequence that invoked the command has |
|---|
| 356 |
@var{n} events that are lists, the @var{n}th @samp{e} provides the |
|---|
| 357 |
@var{n}th such event. Events that are not lists, such as function keys |
|---|
| 358 |
and @acronym{ASCII} characters, do not count where @samp{e} is concerned. |
|---|
| 359 |
|
|---|
| 360 |
@item f |
|---|
| 361 |
A file name of an existing file (@pxref{File Names}). The default |
|---|
| 362 |
directory is @code{default-directory}. Existing, Completion, Default, |
|---|
| 363 |
Prompt. |
|---|
| 364 |
|
|---|
| 365 |
@item F |
|---|
| 366 |
A file name. The file need not exist. Completion, Default, Prompt. |
|---|
| 367 |
|
|---|
| 368 |
@item G |
|---|
| 369 |
A file name. The file need not exist. If the user enters just a |
|---|
| 370 |
directory name, then the value is just that directory name, with no |
|---|
| 371 |
file name within the directory added. Completion, Default, Prompt. |
|---|
| 372 |
|
|---|
| 373 |
@item i |
|---|
| 374 |
An irrelevant argument. This code always supplies @code{nil} as |
|---|
| 375 |
the argument's value. No I/O. |
|---|
| 376 |
|
|---|
| 377 |
@item k |
|---|
| 378 |
A key sequence (@pxref{Key Sequences}). This keeps reading events |
|---|
| 379 |
until a command (or undefined command) is found in the current key |
|---|
| 380 |
maps. The key sequence argument is represented as a string or vector. |
|---|
| 381 |
The cursor does not move into the echo area. Prompt. |
|---|
| 382 |
|
|---|
| 383 |
If @samp{k} reads a key sequence that ends with a down-event, it also |
|---|
| 384 |
reads and discards the following up-event. You can get access to that |
|---|
| 385 |
up-event with the @samp{U} code character. |
|---|
| 386 |
|
|---|
| 387 |
This kind of input is used by commands such as @code{describe-key} and |
|---|
| 388 |
@code{global-set-key}. |
|---|
| 389 |
|
|---|
| 390 |
@item K |
|---|
| 391 |
A key sequence, whose definition you intend to change. This works like |
|---|
| 392 |
@samp{k}, except that it suppresses, for the last input event in the key |
|---|
| 393 |
sequence, the conversions that are normally used (when necessary) to |
|---|
| 394 |
convert an undefined key into a defined one. |
|---|
| 395 |
|
|---|
| 396 |
@item m |
|---|
| 397 |
@cindex marker argument |
|---|
| 398 |
The position of the mark, as an integer. No I/O. |
|---|
| 399 |
|
|---|
| 400 |
@item M |
|---|
| 401 |
Arbitrary text, read in the minibuffer using the current buffer's input |
|---|
| 402 |
method, and returned as a string (@pxref{Input Methods,,, emacs, The GNU |
|---|
| 403 |
Emacs Manual}). Prompt. |
|---|
| 404 |
|
|---|
| 405 |
@item n |
|---|
| 406 |
A number, read with the minibuffer. If the input is not a number, the |
|---|
| 407 |
user has to try again. @samp{n} never uses the prefix argument. |
|---|
| 408 |
Prompt. |
|---|
| 409 |
|
|---|
| 410 |
@item N |
|---|
| 411 |
The numeric prefix argument; but if there is no prefix argument, read |
|---|
| 412 |
a number as with @kbd{n}. The value is always a number. @xref{Prefix |
|---|
| 413 |
Command Arguments}. Prompt. |
|---|
| 414 |
|
|---|
| 415 |
@item p |
|---|
| 416 |
@cindex numeric prefix argument usage |
|---|
| 417 |
The numeric prefix argument. (Note that this @samp{p} is lower case.) |
|---|
| 418 |
No I/O. |
|---|
| 419 |
|
|---|
| 420 |
@item P |
|---|
| 421 |
@cindex raw prefix argument usage |
|---|
| 422 |
The raw prefix argument. (Note that this @samp{P} is upper case.) No |
|---|
| 423 |
I/O. |
|---|
| 424 |
|
|---|
| 425 |
@item r |
|---|
| 426 |
@cindex region argument |
|---|
| 427 |
Point and the mark, as two numeric arguments, smallest first. This is |
|---|
| 428 |
the only code letter that specifies two successive arguments rather than |
|---|
| 429 |
one. No I/O. |
|---|
| 430 |
|
|---|
| 431 |
@item s |
|---|
| 432 |
Arbitrary text, read in the minibuffer and returned as a string |
|---|
| 433 |
(@pxref{Text from Minibuffer}). Terminate the input with either |
|---|
| 434 |
@kbd{C-j} or @key{RET}. (@kbd{C-q} may be used to include either of |
|---|
| 435 |
these characters in the input.) Prompt. |
|---|
| 436 |
|
|---|
| 437 |
@item S |
|---|
| 438 |
An interned symbol whose name is read in the minibuffer. Any whitespace |
|---|
| 439 |
character terminates the input. (Use @kbd{C-q} to include whitespace in |
|---|
| 440 |
the string.) Other characters that normally terminate a symbol (e.g., |
|---|
| 441 |
parentheses and brackets) do not do so here. Prompt. |
|---|
| 442 |
|
|---|
| 443 |
@item U |
|---|
| 444 |
A key sequence or @code{nil}. Can be used after a @samp{k} or |
|---|
| 445 |
@samp{K} argument to get the up-event that was discarded (if any) |
|---|
| 446 |
after @samp{k} or @samp{K} read a down-event. If no up-event has been |
|---|
| 447 |
discarded, @samp{U} provides @code{nil} as the argument. No I/O. |
|---|
| 448 |
|
|---|
| 449 |
@item v |
|---|
| 450 |
A variable declared to be a user option (i.e., satisfying the |
|---|
| 451 |
predicate @code{user-variable-p}). This reads the variable using |
|---|
| 452 |
@code{read-variable}. @xref{Definition of read-variable}. Existing, |
|---|
| 453 |
Completion, Prompt. |
|---|
| 454 |
|
|---|
| 455 |
@item x |
|---|
| 456 |
A Lisp object, specified with its read syntax, terminated with a |
|---|
| 457 |
@kbd{C-j} or @key{RET}. The object is not evaluated. @xref{Object from |
|---|
| 458 |
Minibuffer}. Prompt. |
|---|
| 459 |
|
|---|
| 460 |
@item X |
|---|
| 461 |
@cindex evaluated expression argument |
|---|
| 462 |
A Lisp form's value. @samp{X} reads as @samp{x} does, then evaluates |
|---|
| 463 |
the form so that its value becomes the argument for the command. |
|---|
| 464 |
Prompt. |
|---|
| 465 |
|
|---|
| 466 |
@item z |
|---|
| 467 |
A coding system name (a symbol). If the user enters null input, the |
|---|
| 468 |
argument value is @code{nil}. @xref{Coding Systems}. Completion, |
|---|
| 469 |
Existing, Prompt. |
|---|
| 470 |
|
|---|
| 471 |
@item Z |
|---|
| 472 |
A coding system name (a symbol)---but only if this command has a prefix |
|---|
| 473 |
argument. With no prefix argument, @samp{Z} provides @code{nil} as the |
|---|
| 474 |
argument value. Completion, Existing, Prompt. |
|---|
| 475 |
@end table |
|---|
| 476 |
|
|---|
| 477 |
@node Interactive Examples |
|---|
| 478 |
@comment node-name, next, previous, up |
|---|
| 479 |
@subsection Examples of Using @code{interactive} |
|---|
| 480 |
@cindex examples of using @code{interactive} |
|---|
| 481 |
@cindex @code{interactive}, examples of using |
|---|
| 482 |
|
|---|
| 483 |
Here are some examples of @code{interactive}: |
|---|
| 484 |
|
|---|
| 485 |
@example |
|---|
| 486 |
@group |
|---|
| 487 |
(defun foo1 () ; @r{@code{foo1} takes no arguments,} |
|---|
| 488 |
(interactive) ; @r{just moves forward two words.} |
|---|
| 489 |
(forward-word 2)) |
|---|
| 490 |
@result{} foo1 |
|---|
| 491 |
@end group |
|---|
| 492 |
|
|---|
| 493 |
@group |
|---|
| 494 |
(defun foo2 (n) ; @r{@code{foo2} takes one argument,} |
|---|
| 495 |
(interactive "p") ; @r{which is the numeric prefix.} |
|---|
| 496 |
(forward-word (* 2 n))) |
|---|
| 497 |
@result{} foo2 |
|---|
| 498 |
@end group |
|---|
| 499 |
|
|---|
| 500 |
@group |
|---|
| 501 |
(defun foo3 (n) ; @r{@code{foo3} takes one argument,} |
|---|
| 502 |
(interactive "nCount:") ; @r{which is read with the Minibuffer.} |
|---|
| 503 |
(forward-word (* 2 n))) |
|---|
| 504 |
@result{} foo3 |
|---|
| 505 |
@end group |
|---|
| 506 |
|
|---|
| 507 |
@group |
|---|
| 508 |
(defun three-b (b1 b2 b3) |
|---|
| 509 |
"Select three existing buffers. |
|---|
| 510 |
Put them into three windows, selecting the last one." |
|---|
| 511 |
@end group |
|---|
| 512 |
(interactive "bBuffer1:\nbBuffer2:\nbBuffer3:") |
|---|
| 513 |
(delete-other-windows) |
|---|
| 514 |
(split-window (selected-window) 8) |
|---|
| 515 |
(switch-to-buffer b1) |
|---|
| 516 |
(other-window 1) |
|---|
| 517 |
(split-window (selected-window) 8) |
|---|
| 518 |
(switch-to-buffer b2) |
|---|
| 519 |
(other-window 1) |
|---|
| 520 |
(switch-to-buffer b3)) |
|---|
| 521 |
@result{} three-b |
|---|
| 522 |
@group |
|---|
| 523 |
(three-b "*scratch*" "declarations.texi" "*mail*") |
|---|
| 524 |
@result{} nil |
|---|
| 525 |
@end group |
|---|
| 526 |
@end example |
|---|
| 527 |
|
|---|
| 528 |
@node Interactive Call |
|---|
| 529 |
@section Interactive Call |
|---|
| 530 |
@cindex interactive call |
|---|
| 531 |
|
|---|
| 532 |
After the command loop has translated a key sequence into a command it |
|---|
| 533 |
invokes that command using the function @code{command-execute}. If the |
|---|
| 534 |
command is a function, @code{command-execute} calls |
|---|
| 535 |
@code{call-interactively}, which reads the arguments and calls the |
|---|
| 536 |
command. You can also call these functions yourself. |
|---|
| 537 |
|
|---|
| 538 |
@defun commandp object &optional for-call-interactively |
|---|
| 539 |
Returns @code{t} if @var{object} is suitable for calling interactively; |
|---|
| 540 |
that is, if @var{object} is a command. Otherwise, returns @code{nil}. |
|---|
| 541 |
|
|---|
| 542 |
The interactively callable objects include strings and vectors (treated |
|---|
| 543 |
as keyboard macros), lambda expressions that contain a top-level call to |
|---|
| 544 |
@code{interactive}, byte-code function objects made from such lambda |
|---|
| 545 |
expressions, autoload objects that are declared as interactive |
|---|
| 546 |
(non-@code{nil} fourth argument to @code{autoload}), and some of the |
|---|
| 547 |
primitive functions. |
|---|
| 548 |
|
|---|
| 549 |
A symbol satisfies @code{commandp} if its function definition |
|---|
| 550 |
satisfies @code{commandp}. Keys and keymaps are not commands. |
|---|
| 551 |
Rather, they are used to look up commands (@pxref{Keymaps}). |
|---|
| 552 |
|
|---|
| 553 |
If @var{for-call-interactively} is non-@code{nil}, then |
|---|
| 554 |
@code{commandp} returns @code{t} only for objects that |
|---|
| 555 |
@code{call-interactively} could call---thus, not for keyboard macros. |
|---|
| 556 |
|
|---|
| 557 |
See @code{documentation} in @ref{Accessing Documentation}, for a |
|---|
| 558 |
realistic example of using @code{commandp}. |
|---|
| 559 |
@end defun |
|---|
| 560 |
|
|---|
| 561 |
@defun call-interactively command &optional record-flag keys |
|---|
| 562 |
This function calls the interactively callable function @var{command}, |
|---|
| 563 |
reading arguments according to its interactive calling specifications. |
|---|
| 564 |
It returns whatever @var{command} returns. An error is signaled if |
|---|
| 565 |
@var{command} is not a function or if it cannot be called |
|---|
| 566 |
interactively (i.e., is not a command). Note that keyboard macros |
|---|
| 567 |
(strings and vectors) are not accepted, even though they are |
|---|
| 568 |
considered commands, because they are not functions. If @var{command} |
|---|
| 569 |
is a symbol, then @code{call-interactively} uses its function definition. |
|---|
| 570 |
|
|---|
| 571 |
@cindex record command history |
|---|
| 572 |
If @var{record-flag} is non-@code{nil}, then this command and its |
|---|
| 573 |
arguments are unconditionally added to the list @code{command-history}. |
|---|
| 574 |
Otherwise, the command is added only if it uses the minibuffer to read |
|---|
| 575 |
an argument. @xref{Command History}. |
|---|
| 576 |
|
|---|
| 577 |
The argument @var{keys}, if given, should be a vector which specifies |
|---|
| 578 |
the sequence of events to supply if the command inquires which events |
|---|
| 579 |
were used to invoke it. If @var{keys} is omitted or @code{nil}, the |
|---|
| 580 |
default is the return value of @code{this-command-keys-vector}. |
|---|
| 581 |
@xref{Definition of this-command-keys-vector}. |
|---|
| 582 |
@end defun |
|---|
| 583 |
|
|---|
| 584 |
@defun command-execute command &optional record-flag keys special |
|---|
| 585 |
@cindex keyboard macro execution |
|---|
| 586 |
This function executes @var{command}. The argument @var{command} must |
|---|
| 587 |
satisfy the @code{commandp} predicate; i.e., it must be an interactively |
|---|
| 588 |
callable function or a keyboard macro. |
|---|
| 589 |
|
|---|
| 590 |
A string or vector as @var{command} is executed with |
|---|
| 591 |
@code{execute-kbd-macro}. A function is passed to |
|---|
| 592 |
@code{call-interactively}, along with the optional @var{record-flag} |
|---|
| 593 |
and @var{keys}. |
|---|
| 594 |
|
|---|
| 595 |
A symbol is handled by using its function definition in its place. A |
|---|
| 596 |
symbol with an @code{autoload} definition counts as a command if it was |
|---|
| 597 |
declared to stand for an interactively callable function. Such a |
|---|
| 598 |
definition is handled by loading the specified library and then |
|---|
| 599 |
rechecking the definition of the symbol. |
|---|
| 600 |
|
|---|
| 601 |
The argument @var{special}, if given, means to ignore the prefix |
|---|
| 602 |
argument and not clear it. This is used for executing special events |
|---|
| 603 |
(@pxref{Special Events}). |
|---|
| 604 |
@end defun |
|---|
| 605 |
|
|---|
| 606 |
@deffn Command execute-extended-command prefix-argument |
|---|
| 607 |
@cindex read command name |
|---|
| 608 |
This function reads a command name from the minibuffer using |
|---|
| 609 |
@code{completing-read} (@pxref{Completion}). Then it uses |
|---|
| 610 |
@code{command-execute} to call the specified command. Whatever that |
|---|
| 611 |
command returns becomes the value of @code{execute-extended-command}. |
|---|
| 612 |
|
|---|
| 613 |
@cindex execute with prefix argument |
|---|
| 614 |
If the command asks for a prefix argument, it receives the value |
|---|
| 615 |
@var{prefix-argument}. If @code{execute-extended-command} is called |
|---|
| 616 |
interactively, the current raw prefix argument is used for |
|---|
| 617 |
@var{prefix-argument}, and thus passed on to whatever command is run. |
|---|
| 618 |
|
|---|
| 619 |
@c !!! Should this be @kindex? |
|---|
| 620 |
@cindex @kbd{M-x} |
|---|
| 621 |
@code{execute-extended-command} is the normal definition of @kbd{M-x}, |
|---|
| 622 |
so it uses the string @w{@samp{M-x }} as a prompt. (It would be better |
|---|
| 623 |
to take the prompt from the events used to invoke |
|---|
| 624 |
@code{execute-extended-command}, but that is painful to implement.) A |
|---|
| 625 |
description of the value of the prefix argument, if any, also becomes |
|---|
| 626 |
part of the prompt. |
|---|
| 627 |
|
|---|
| 628 |
@example |
|---|
| 629 |
@group |
|---|
| 630 |
(execute-extended-command 3) |
|---|
| 631 |
---------- Buffer: Minibuffer ---------- |
|---|
| 632 |
3 M-x forward-word RET |
|---|
| 633 |
---------- Buffer: Minibuffer ---------- |
|---|
| 634 |
@result{} t |
|---|
| 635 |
@end group |
|---|
| 636 |
@end example |
|---|
| 637 |
@end deffn |
|---|
| 638 |
|
|---|
| 639 |
@node Distinguish Interactive |
|---|
| 640 |
@section Distinguish Interactive Calls |
|---|
| 641 |
|
|---|
| 642 |
Sometimes a command should display additional visual feedback (such |
|---|
| 643 |
as an informative message in the echo area) for interactive calls |
|---|
| 644 |
only. There are three ways to do this. The recommended way to test |
|---|
| 645 |
whether the function was called using @code{call-interactively} is to |
|---|
| 646 |
give it an optional argument @code{print-message} and use the |
|---|
| 647 |
@code{interactive} spec to make it non-@code{nil} in interactive |
|---|
| 648 |
calls. Here's an example: |
|---|
| 649 |
|
|---|
| 650 |
@example |
|---|
| 651 |
(defun foo (&optional print-message) |
|---|
| 652 |
(interactive "p") |
|---|
| 653 |
(when print-message |
|---|
| 654 |
(message "foo"))) |
|---|
| 655 |
@end example |
|---|
| 656 |
|
|---|
| 657 |
@noindent |
|---|
| 658 |
We use @code{"p"} because the numeric prefix argument is never |
|---|
| 659 |
@code{nil}. Defined in this way, the function does display the |
|---|
| 660 |
message when called from a keyboard macro. |
|---|
| 661 |
|
|---|
| 662 |
The above method with the additional argument is usually best, |
|---|
| 663 |
because it allows callers to say ``treat this call as interactive.'' |
|---|
| 664 |
But you can also do the job in a simpler way by testing |
|---|
| 665 |
@code{called-interactively-p}. |
|---|
| 666 |
|
|---|
| 667 |
@defun called-interactively-p |
|---|
| 668 |
This function returns @code{t} when the calling function was called |
|---|
| 669 |
using @code{call-interactively}. |
|---|
| 670 |
|
|---|
| 671 |
If the containing function was called by Lisp evaluation (or with |
|---|
| 672 |
@code{apply} or @code{funcall}), then it was not called interactively. |
|---|
| 673 |
@end defun |
|---|
| 674 |
|
|---|
| 675 |
Here's an example of using @code{called-interactively-p}: |
|---|
| 676 |
|
|---|
| 677 |
@example |
|---|
| 678 |
@group |
|---|
| 679 |
(defun foo () |
|---|
| 680 |
(interactive) |
|---|
| 681 |
(when (called-interactively-p) |
|---|
| 682 |
(message "foo")) |
|---|
| 683 |
'haha) |
|---|
| 684 |
@result{} foo |
|---|
| 685 |
@end group |
|---|
| 686 |
|
|---|
| 687 |
@group |
|---|
| 688 |
;; @r{Type @kbd{M-x foo}.} |
|---|
| 689 |
@print{} foo |
|---|
| 690 |
@end group |
|---|
| 691 |
|
|---|
| 692 |
@group |
|---|
| 693 |
(foo) |
|---|
| 694 |
@result{} haha |
|---|
| 695 |
@end group |
|---|
| 696 |
@end example |
|---|
| 697 |
|
|---|
| 698 |
Here is another example that contrasts direct and indirect |
|---|
| 699 |
calls to @code{called-interactively-p}. |
|---|
| 700 |
|
|---|
| 701 |
@example |
|---|
| 702 |
@group |
|---|
| 703 |
(defun bar () |
|---|
| 704 |
(interactive) |
|---|
| 705 |
(setq foobar (list (foo) (called-interactively-p)))) |
|---|
| 706 |
@result{} bar |
|---|
| 707 |
@end group |
|---|
| 708 |
|
|---|
| 709 |
@group |
|---|
| 710 |
;; @r{Type @kbd{M-x bar}.} |
|---|
| 711 |
;; @r{This does not display a message.} |
|---|
| 712 |
@end group |
|---|
| 713 |
|
|---|
| 714 |
@group |
|---|
| 715 |
foobar |
|---|
| 716 |
@result{} (nil t) |
|---|
| 717 |
@end group |
|---|
| 718 |
@end example |
|---|
| 719 |
|
|---|
| 720 |
If you want to treat commands run in keyboard macros just like calls |
|---|
| 721 |
from Lisp programs, test @code{interactive-p} instead of |
|---|
| 722 |
@code{called-interactively-p}. |
|---|
| 723 |
|
|---|
| 724 |
@defun interactive-p |
|---|
| 725 |
This function returns @code{t} if the containing function (the one |
|---|
| 726 |
whose code includes the call to @code{interactive-p}) was called in |
|---|
| 727 |
direct response to user input. This means that it was called with the |
|---|
| 728 |
function @code{call-interactively}, and that a keyboard macro is |
|---|
| 729 |
not running, and that Emacs is not running in batch mode. |
|---|
| 730 |
@end defun |
|---|
| 731 |
|
|---|
| 732 |
@node Command Loop Info |
|---|
| 733 |
@comment node-name, next, previous, up |
|---|
| 734 |
@section Information from the Command Loop |
|---|
| 735 |
|
|---|
| 736 |
The editor command loop sets several Lisp variables to keep status |
|---|
| 737 |
records for itself and for commands that are run. With the exception of |
|---|
| 738 |
@code{this-command} and @code{last-command} it's generally a bad idea to |
|---|
| 739 |
change any of these variables in a Lisp program. |
|---|
| 740 |
|
|---|
| 741 |
@defvar last-command |
|---|
| 742 |
This variable records the name of the previous command executed by the |
|---|
| 743 |
command loop (the one before the current command). Normally the value |
|---|
| 744 |
is a symbol with a function definition, but this is not guaranteed. |
|---|
| 745 |
|
|---|
| 746 |
The value is copied from @code{this-command} when a command returns to |
|---|
| 747 |
the command loop, except when the command has specified a prefix |
|---|
| 748 |
argument for the following command. |
|---|
| 749 |
|
|---|
| 750 |
This variable is always local to the current terminal and cannot be |
|---|
| 751 |
buffer-local. @xref{Multiple Displays}. |
|---|
| 752 |
@end defvar |
|---|
| 753 |
|
|---|
| 754 |
@defvar real-last-command |
|---|
| 755 |
This variable is set up by Emacs just like @code{last-command}, |
|---|
| 756 |
but never altered by Lisp programs. |
|---|
| 757 |
@end defvar |
|---|
| 758 |
|
|---|
| 759 |
@defvar last-repeatable-command |
|---|
| 760 |
This variable stores the most recently executed command that was not |
|---|
| 761 |
part of an input event. This is the command @code{repeat} will try to |
|---|
| 762 |
repeat, @xref{Repeating,,, emacs, The GNU Emacs Manual}. |
|---|
| 763 |
@end defvar |
|---|
| 764 |
|
|---|
| 765 |
@defvar this-command |
|---|
| 766 |
@cindex current command |
|---|
| 767 |
This variable records the name of the command now being executed by |
|---|
| 768 |
the editor command loop. Like @code{last-command}, it is normally a symbol |
|---|
| 769 |
with a function definition. |
|---|
| 770 |
|
|---|
| 771 |
The command loop sets this variable just before running a command, and |
|---|
| 772 |
copies its value into @code{last-command} when the command finishes |
|---|
| 773 |
(unless the command specified a prefix argument for the following |
|---|
| 774 |
command). |
|---|
| 775 |
|
|---|
| 776 |
@cindex kill command repetition |
|---|
| 777 |
Some commands set this variable during their execution, as a flag for |
|---|
| 778 |
whatever command runs next. In particular, the functions for killing text |
|---|
| 779 |
set @code{this-command} to @code{kill-region} so that any kill commands |
|---|
| 780 |
immediately following will know to append the killed text to the |
|---|
| 781 |
previous kill. |
|---|
| 782 |
@end defvar |
|---|
| 783 |
|
|---|
| 784 |
If you do not want a particular command to be recognized as the previous |
|---|
| 785 |
command in the case where it got an error, you must code that command to |
|---|
| 786 |
prevent this. One way is to set @code{this-command} to @code{t} at the |
|---|
| 787 |
beginning of the command, and set @code{this-command} back to its proper |
|---|
| 788 |
value at the end, like this: |
|---|
| 789 |
|
|---|
| 790 |
@example |
|---|
| 791 |
(defun foo (args@dots{}) |
|---|
| 792 |
(interactive @dots{}) |
|---|
| 793 |
(let ((old-this-command this-command)) |
|---|
| 794 |
(setq this-command t) |
|---|
| 795 |
@r{@dots{}do the work@dots{}} |
|---|
| 796 |
(setq this-command old-this-command))) |
|---|
| 797 |
@end example |
|---|
| 798 |
|
|---|
| 799 |
@noindent |
|---|
| 800 |
We do not bind @code{this-command} with @code{let} because that would |
|---|
| 801 |
restore the old value in case of error---a feature of @code{let} which |
|---|
| 802 |
in this case does precisely what we want to avoid. |
|---|
| 803 |
|
|---|
| 804 |
@defvar this-original-command |
|---|
| 805 |
This has the same value as @code{this-command} except when command |
|---|
| 806 |
remapping occurs (@pxref{Remapping Commands}). In that case, |
|---|
| 807 |
@code{this-command} gives the command actually run (the result of |
|---|
| 808 |
remapping), and @code{this-original-command} gives the command that |
|---|
| 809 |
was specified to run but remapped into another command. |
|---|
| 810 |
@end defvar |
|---|
| 811 |
|
|---|
| 812 |
@defun this-command-keys |
|---|
| 813 |
This function returns a string or vector containing the key sequence |
|---|
| 814 |
that invoked the present command, plus any previous commands that |
|---|
| 815 |
generated the prefix argument for this command. Any events read by the |
|---|
| 816 |
command using @code{read-event} without a timeout get tacked on to the end. |
|---|
| 817 |
|
|---|
| 818 |
However, if the command has called @code{read-key-sequence}, it |
|---|
| 819 |
returns the last read key sequence. @xref{Key Sequence Input}. The |
|---|
| 820 |
value is a string if all events in the sequence were characters that |
|---|
| 821 |
fit in a string. @xref{Input Events}. |
|---|
| 822 |
|
|---|
| 823 |
@example |
|---|
| 824 |
@group |
|---|
| 825 |
(this-command-keys) |
|---|
| 826 |
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.} |
|---|
| 827 |
@result{} "^U^X^E" |
|---|
| 828 |
@end group |
|---|
| 829 |
@end example |
|---|
| 830 |
@end defun |
|---|
| 831 |
|
|---|
| 832 |
@defun this-command-keys-vector |
|---|
| 833 |
@anchor{Definition of this-command-keys-vector} |
|---|
| 834 |
Like @code{this-command-keys}, except that it always returns the events |
|---|
| 835 |
in a vector, so you don't need to deal with the complexities of storing |
|---|
| 836 |
input events in a string (@pxref{Strings of Events}). |
|---|
| 837 |
@end defun |
|---|
| 838 |
|
|---|
| 839 |
@defun clear-this-command-keys &optional keep-record |
|---|
| 840 |
This function empties out the table of events for |
|---|
| 841 |
@code{this-command-keys} to return. Unless @var{keep-record} is |
|---|
| 842 |
non-@code{nil}, it also empties the records that the function |
|---|
| 843 |
@code{recent-keys} (@pxref{Recording Input}) will subsequently return. |
|---|
| 844 |
This is useful after reading a password, to prevent the password from |
|---|
| 845 |
echoing inadvertently as part of the next command in certain cases. |
|---|
| 846 |
@end defun |
|---|
| 847 |
|
|---|
| 848 |
@defvar last-nonmenu-event |
|---|
| 849 |
This variable holds the last input event read as part of a key sequence, |
|---|
| 850 |
not counting events resulting from mouse menus. |
|---|
| 851 |
|
|---|
| 852 |
One use of this variable is for telling @code{x-popup-menu} where to pop |
|---|
| 853 |
up a menu. It is also used internally by @code{y-or-n-p} |
|---|
| 854 |
(@pxref{Yes-or-No Queries}). |
|---|
| 855 |
@end defvar |
|---|
| 856 |
|
|---|
| 857 |
@defvar last-command-event |
|---|
| 858 |
@defvarx last-command-char |
|---|
| 859 |
This variable is set to the last input event that was read by the |
|---|
| 860 |
command loop as part of a command. The principal use of this variable |
|---|
| 861 |
is in @code{self-insert-command}, which uses it to decide which |
|---|
| 862 |
character to insert. |
|---|
| 863 |
|
|---|
| 864 |
@example |
|---|
| 865 |
@group |
|---|
| 866 |
last-command-event |
|---|
| 867 |
;; @r{Now use @kbd{C-u C-x C-e} to evaluate that.} |
|---|
| 868 |
@result{} 5 |
|---|
| 869 |
@end group |
|---|
| 870 |
@end example |
|---|
| 871 |
|
|---|
| 872 |
@noindent |
|---|
| 873 |
The value is 5 because that is the @acronym{ASCII} code for @kbd{C-e}. |
|---|
| 874 |
|
|---|
| 875 |
The alias @code{last-command-char} exists for compatibility with |
|---|
| 876 |
Emacs version 18. |
|---|
| 877 |
@end defvar |
|---|
| 878 |
|
|---|
| 879 |
@c Emacs 19 feature |
|---|
| 880 |
@defvar last-event-frame |
|---|
| 881 |
This variable records which frame the last input event was directed to. |
|---|
| 882 |
Usually this is the frame that was selected when the event was |
|---|
| 883 |
generated, but if that frame has redirected input focus to another |
|---|
| 884 |
frame, the value is the frame to which the event was redirected. |
|---|
| 885 |
@xref{Input Focus}. |
|---|
| 886 |
|
|---|
| 887 |
If the last event came from a keyboard macro, the value is @code{macro}. |
|---|
| 888 |
@end defvar |
|---|
| 889 |
|
|---|
| 890 |
@node Adjusting Point |
|---|
| 891 |
@section Adjusting Point After Commands |
|---|
| 892 |
@cindex adjusting point |
|---|
| 893 |
@cindex invisible/intangible text, and point |
|---|
| 894 |
@cindex @code{display} property, and point display |
|---|
| 895 |
@cindex @code{composition} property, and point display |
|---|
| 896 |
|
|---|
| 897 |
It is not easy to display a value of point in the middle of a |
|---|
| 898 |
sequence of text that has the @code{display}, @code{composition} or |
|---|
| 899 |
@code{intangible} property, or is invisible. Therefore, after a |
|---|
| 900 |
command finishes and returns to the command loop, if point is within |
|---|
| 901 |
such a sequence, the command loop normally moves point to the edge of |
|---|
| 902 |
the sequence. |
|---|
| 903 |
|
|---|
| 904 |
A command can inhibit this feature by setting the variable |
|---|
| 905 |
@code{disable-point-adjustment}: |
|---|
| 906 |
|
|---|
| 907 |
@defvar disable-point-adjustment |
|---|
| 908 |
If this variable is non-@code{nil} when a command returns to the |
|---|
| 909 |
command loop, then the command loop does not check for those text |
|---|
| 910 |
properties, and does not move point out of sequences that have them. |
|---|
| 911 |
|
|---|
| 912 |
The command loop sets this variable to @code{nil} before each command, |
|---|
| 913 |
so if a command sets it, the effect applies only to that command. |
|---|
| 914 |
@end defvar |
|---|
| 915 |
|
|---|
| 916 |
@defvar global-disable-point-adjustment |
|---|
| 917 |
If you set this variable to a non-@code{nil} value, the feature of |
|---|
| 918 |
moving point out of these sequences is completely turned off. |
|---|
| 919 |
@end defvar |
|---|
| 920 |
|
|---|
| 921 |
@node Input Events |
|---|
| 922 |
@section Input Events |
|---|
| 923 |
@cindex events |
|---|
| 924 |
@cindex input events |
|---|
| 925 |
|
|---|
| 926 |
The Emacs command loop reads a sequence of @dfn{input events} that |
|---|
| 927 |
represent keyboard or mouse activity. The events for keyboard activity |
|---|
| 928 |
are characters or symbols; mouse events are always lists. This section |
|---|
| 929 |
describes the representation and meaning of input events in detail. |
|---|
| 930 |
|
|---|
| 931 |
@defun eventp object |
|---|
| 932 |
This function returns non-@code{nil} if @var{object} is an input event |
|---|
| 933 |
or event type. |
|---|
| 934 |
|
|---|
| 935 |
Note that any symbol might be used as an event or an event type. |
|---|
| 936 |
@code{eventp} cannot distinguish whether a symbol is intended by Lisp |
|---|
| 937 |
code to be used as an event. Instead, it distinguishes whether the |
|---|
| 938 |
symbol has actually been used in an event that has been read as input in |
|---|
| 939 |
the current Emacs session. If a symbol has not yet been so used, |
|---|
| 940 |
@code{eventp} returns @code{nil}. |
|---|
| 941 |
@end defun |
|---|
| 942 |
|
|---|
| 943 |
@menu |
|---|
| 944 |
* Keyboard Events:: Ordinary characters--keys with symbols on them. |
|---|
| 945 |
* Function Keys:: Function keys--keys with names, not symbols. |
|---|
| 946 |
* Mouse Events:: Overview of mouse events. |
|---|
| 947 |
* Click Events:: Pushing and releasing a mouse button. |
|---|
| 948 |
* Drag Events:: Moving the mouse before releasing the button. |
|---|
| 949 |
* Button-Down Events:: A button was pushed and not yet released. |
|---|
| 950 |
* Repeat Events:: Double and triple click (or drag, or down). |
|---|
| 951 |
* Motion Events:: Just moving the mouse, not pushing a button. |
|---|
| 952 |
* Focus Events:: Moving the mouse between frames. |
|---|
| 953 |
* Misc Events:: Other events the system can generate. |
|---|
| 954 |
* Event Examples:: Examples of the lists for mouse events. |
|---|
| 955 |
* Classifying Events:: Finding the modifier keys in an event symbol. |
|---|
| 956 |
Event types. |
|---|
| 957 |
* Accessing Mouse:: Functions to extract info from mouse events. |
|---|
| 958 |
* Accessing Scroll:: Functions to get info from scroll bar events. |
|---|
| 959 |
* Strings of Events:: Special considerations for putting |
|---|
| 960 |
keyboard character events in a string. |
|---|
| 961 |
@end menu |
|---|
| 962 |
|
|---|
| 963 |
@node Keyboard Events |
|---|
| 964 |
@subsection Keyboard Events |
|---|
| 965 |
@cindex keyboard events |
|---|
| 966 |
|
|---|
| 967 |
There are two kinds of input you can get from the keyboard: ordinary |
|---|
| 968 |
keys, and function keys. Ordinary keys correspond to characters; the |
|---|
| 969 |
events they generate are represented in Lisp as characters. The event |
|---|
| 970 |
type of a character event is the character itself (an integer); see |
|---|
| 971 |
@ref{Classifying Events}. |
|---|
| 972 |
|
|---|
| 973 |
@cindex modifier bits (of input character) |
|---|
| 974 |
@cindex basic code (of input character) |
|---|
| 975 |
An input character event consists of a @dfn{basic code} between 0 and |
|---|
| 976 |
524287, plus any or all of these @dfn{modifier bits}: |
|---|
| 977 |
|
|---|
| 978 |
@table @asis |
|---|
| 979 |
@item meta |
|---|
| 980 |
The |
|---|
| 981 |
@tex |
|---|
| 982 |
@math{2^{27}} |
|---|
| 983 |
@end tex |
|---|
| 984 |
@ifnottex |
|---|
| 985 |
2**27 |
|---|
| 986 |
@end ifnottex |
|---|
| 987 |
bit in the character code indicates a character |
|---|
| 988 |
typed with the meta key held down. |
|---|
| 989 |
|
|---|
| 990 |
@item control |
|---|
| 991 |
The |
|---|
| 992 |
@tex |
|---|
| 993 |
@math{2^{26}} |
|---|
| 994 |
@end tex |
|---|
| 995 |
@ifnottex |
|---|
| 996 |
2**26 |
|---|
| 997 |
@end ifnottex |
|---|
| 998 |
bit in the character code indicates a non-@acronym{ASCII} |
|---|
| 999 |
control character. |
|---|
| 1000 |
|
|---|
| 1001 |
@sc{ascii} control characters such as @kbd{C-a} have special basic |
|---|
| 1002 |
codes of their own, so Emacs needs no special bit to indicate them. |
|---|
| 1003 |
Thus, the code for @kbd{C-a} is just 1. |
|---|
| 1004 |
|
|---|
| 1005 |
But if you type a control combination not in @acronym{ASCII}, such as |
|---|
| 1006 |
@kbd{%} with the control key, the numeric value you get is the code |
|---|
| 1007 |
for @kbd{%} plus |
|---|
| 1008 |
@tex |
|---|
| 1009 |
@math{2^{26}} |
|---|
| 1010 |
@end tex |
|---|
| 1011 |
@ifnottex |
|---|
| 1012 |
2**26 |
|---|
| 1013 |
@end ifnottex |
|---|
| 1014 |
(assuming the terminal supports non-@acronym{ASCII} |
|---|
| 1015 |
control characters). |
|---|
| 1016 |
|
|---|
| 1017 |
@item shift |
|---|
| 1018 |
The |
|---|
| 1019 |
@tex |
|---|
| 1020 |
@math{2^{25}} |
|---|
| 1021 |
@end tex |
|---|
| 1022 |
@ifnottex |
|---|
| 1023 |
2**25 |
|---|
| 1024 |
@end ifnottex |
|---|
| 1025 |
bit in the character code indicates an @acronym{ASCII} control |
|---|
| 1026 |
character typed with the shift key held down. |
|---|
| 1027 |
|
|---|
| 1028 |
For letters, the basic code itself indicates upper versus lower case; |
|---|
| 1029 |
for digits and punctuation, the shift key selects an entirely different |
|---|
| 1030 |
character with a different basic code. In order to keep within the |
|---|
| 1031 |
@acronym{ASCII} character set whenever possible, Emacs avoids using the |
|---|
| 1032 |
@tex |
|---|
| 1033 |
@math{2^{25}} |
|---|
| 1034 |
@end tex |
|---|
| 1035 |
@ifnottex |
|---|
| 1036 |
2**25 |
|---|
| 1037 |
@end ifnottex |
|---|
| 1038 |
bit for those characters. |
|---|
| 1039 |
|
|---|
| 1040 |
However, @acronym{ASCII} provides no way to distinguish @kbd{C-A} from |
|---|
| 1041 |
@kbd{C-a}, so Emacs uses the |
|---|
| 1042 |
@tex |
|---|
| 1043 |
@math{2^{25}} |
|---|
| 1044 |
@end tex |
|---|
| 1045 |
@ifnottex |
|---|
| 1046 |
2**25 |
|---|
| 1047 |
@end ifnottex |
|---|
| 1048 |
bit in @kbd{C-A} and not in |
|---|
| 1049 |
@kbd{C-a}. |
|---|
| 1050 |
|
|---|
| 1051 |
@item hyper |
|---|
| 1052 |
The |
|---|
| 1053 |
@tex |
|---|
| 1054 |
@math{2^{24}} |
|---|
| 1055 |
@end tex |
|---|
| 1056 |
@ifnottex |
|---|
| 1057 |
2**24 |
|---|
| 1058 |
@end ifnottex |
|---|
| 1059 |
bit in the character code indicates a character |
|---|
| 1060 |
typed with the hyper key held down. |
|---|
| 1061 |
|
|---|
| 1062 |
@item super |
|---|
| 1063 |
The |
|---|
| 1064 |
@tex |
|---|
| 1065 |
@math{2^{23}} |
|---|
| 1066 |
@end tex |
|---|
| 1067 |
@ifnottex |
|---|
| 1068 |
2**23 |
|---|
| 1069 |
@end ifnottex |
|---|
| 1070 |
bit in the character code indicates a character |
|---|
| 1071 |
typed with the super key held down. |
|---|
| 1072 |
|
|---|
| 1073 |
@item alt |
|---|
| 1074 |
The |
|---|
| 1075 |
@tex |
|---|
| 1076 |
@math{2^{22}} |
|---|
| 1077 |
@end tex |
|---|
| 1078 |
@ifnottex |
|---|
| 1079 |
2**22 |
|---|
| 1080 |
@end ifnottex |
|---|
| 1081 |
bit in the character code indicates a character typed with |
|---|
| 1082 |
the alt key held down. (On some terminals, the key labeled @key{ALT} |
|---|
| 1083 |
is actually the meta key.) |
|---|
| 1084 |
@end table |
|---|
| 1085 |
|
|---|
| 1086 |
It is best to avoid mentioning specific bit numbers in your program. |
|---|
| 1087 |
To test the modifier bits of a character, use the function |
|---|
| 1088 |
@code{event-modifiers} (@pxref{Classifying Events}). When making key |
|---|
| 1089 |
bindings, you can use the read syntax for characters with modifier bits |
|---|
| 1090 |
(@samp{\C-}, @samp{\M-}, and so on). For making key bindings with |
|---|
| 1091 |
@code{define-key}, you can use lists such as @code{(control hyper ?x)} to |
|---|
| 1092 |
specify the characters (@pxref{Changing Key Bindings}). The function |
|---|
| 1093 |
@code{event-convert-list} converts such a list into an event type |
|---|
| 1094 |
(@pxref{Classifying Events}). |
|---|
| 1095 |
|
|---|
| 1096 |
@node Function Keys |
|---|
| 1097 |
@subsection Function Keys |
|---|
| 1098 |
|
|---|
| 1099 |
@cindex function keys |
|---|
| 1100 |
Most keyboards also have @dfn{function keys}---keys that have names or |
|---|
| 1101 |
symbols that are not characters. Function keys are represented in Emacs |
|---|
| 1102 |
Lisp as symbols; the symbol's name is the function key's label, in lower |
|---|
| 1103 |
case. For example, pressing a key labeled @key{F1} places the symbol |
|---|
| 1104 |
@code{f1} in the input stream. |
|---|
| 1105 |
|
|---|
| 1106 |
The event type of a function key event is the event symbol itself. |
|---|
| 1107 |
@xref{Classifying Events}. |
|---|
| 1108 |
|
|---|
| 1109 |
Here are a few special cases in the symbol-naming convention for |
|---|
| 1110 |
function keys: |
|---|
| 1111 |
|
|---|
| 1112 |
@table @asis |
|---|
| 1113 |
@item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete} |
|---|
| 1114 |
These keys correspond to common @acronym{ASCII} control characters that have |
|---|
| 1115 |
special keys on most keyboards. |
|---|
| 1116 |
|
|---|
| 1117 |
In @acronym{ASCII}, @kbd{C-i} and @key{TAB} are the same character. If the |
|---|
| 1118 |
terminal can distinguish between them, Emacs conveys the distinction to |
|---|
| 1119 |
Lisp programs by representing the former as the integer 9, and the |
|---|
| 1120 |
latter as the symbol @code{tab}. |
|---|
| 1121 |
|
|---|
| 1122 |
Most of the time, it's not useful to distinguish the two. So normally |
|---|
| 1123 |
@code{function-key-map} (@pxref{Translation Keymaps}) is set up to map |
|---|
| 1124 |
@code{tab} into 9. Thus, a key binding for character code 9 (the |
|---|
| 1125 |
character @kbd{C-i}) also applies to @code{tab}. Likewise for the other |
|---|
| 1126 |
symbols in this group. The function @code{read-char} likewise converts |
|---|
| 1127 |
these events into characters. |
|---|
| 1128 |
|
|---|
| 1129 |
In @acronym{ASCII}, @key{BS} is really @kbd{C-h}. But @code{backspace} |
|---|
| 1130 |
converts into the character code 127 (@key{DEL}), not into code 8 |
|---|
| 1131 |
(@key{BS}). This is what most users prefer. |
|---|
| 1132 |
|
|---|
| 1133 |
@item @code{left}, @code{up}, @code{right}, @code{down} |
|---|
| 1134 |
Cursor arrow keys |
|---|
| 1135 |
@item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{} |
|---|
| 1136 |
Keypad keys (to the right of the regular keyboard). |
|---|
| 1137 |
@item @code{kp-0}, @code{kp-1}, @dots{} |
|---|
| 1138 |
Keypad keys with digits. |
|---|
| 1139 |
@item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4} |
|---|
| 1140 |
Keypad PF keys. |
|---|
| 1141 |
@item @code{kp-home}, @code{kp-left}, @code{kp-up}, @code{kp-right}, @code{kp-down} |
|---|
| 1142 |
Keypad arrow keys. Emacs normally translates these into the |
|---|
| 1143 |
corresponding non-keypad keys @code{home}, @code{left}, @dots{} |
|---|
| 1144 |
@item @code{kp-prior}, @code{kp-next}, @code{kp-end}, @code{kp-begin}, @code{kp-insert}, @code{kp-delete} |
|---|
| 1145 |
Additional keypad duplicates of keys ordinarily found elsewhere. Emacs |
|---|
| 1146 |
normally translates these into the like-named non-keypad keys. |
|---|
| 1147 |
@end table |
|---|
| 1148 |
|
|---|
| 1149 |
You can use the modifier keys @key{ALT}, @key{CTRL}, @key{HYPER}, |
|---|
| 1150 |
@key{META}, @key{SHIFT}, and @key{SUPER} with function keys. The way to |
|---|
| 1151 |
represent them is with prefixes in the symbol name: |
|---|
| 1152 |
|
|---|
| 1153 |
@table @samp |
|---|
| 1154 |
@item A- |
|---|
| 1155 |
The alt modifier. |
|---|
| 1156 |
@item C- |
|---|
| 1157 |
The control modifier. |
|---|
| 1158 |
@item H- |
|---|
| 1159 |
The hyper modifier. |
|---|
| 1160 |
@item M- |
|---|
| 1161 |
The meta modifier. |
|---|
| 1162 |
@item S- |
|---|
| 1163 |
The shift modifier. |
|---|
| 1164 |
@item s- |
|---|
| 1165 |
The super modifier. |
|---|
| 1166 |
@end table |
|---|
| 1167 |
|
|---|
| 1168 |
Thus, the symbol for the key @key{F3} with @key{META} held down is |
|---|
| 1169 |
@code{M-f3}. When you use more than one prefix, we recommend you |
|---|
| 1170 |
write them in alphabetical order; but the order does not matter in |
|---|
| 1171 |
arguments to the key-binding lookup and modification functions. |
|---|
| 1172 |
|
|---|
| 1173 |
@node Mouse Events |
|---|
| 1174 |
@subsection Mouse Events |
|---|
| 1175 |
|
|---|
| 1176 |
Emacs supports four kinds of mouse events: click events, drag events, |
|---|
| 1177 |
button-down events, and motion events. All mouse events are represented |
|---|
| 1178 |
as lists. The @sc{car} of the list is the event type; this says which |
|---|
| 1179 |
mouse button was involved, and which modifier keys were used with it. |
|---|
| 1180 |
The event type can also distinguish double or triple button presses |
|---|
| 1181 |
(@pxref{Repeat Events}). The rest of the list elements give position |
|---|
| 1182 |
and time information. |
|---|
| 1183 |
|
|---|
| 1184 |
For key lookup, only the event type matters: two events of the same type |
|---|
| 1185 |
necessarily run the same command. The command can access the full |
|---|
| 1186 |
values of these events using the @samp{e} interactive code. |
|---|
| 1187 |
@xref{Interactive Codes}. |
|---|
| 1188 |
|
|---|
| 1189 |
A key sequence that starts with a mouse event is read using the keymaps |
|---|
| 1190 |
of the buffer in the window that the mouse was in, not the current |
|---|
| 1191 |
buffer. This does not imply that clicking in a window selects that |
|---|
| 1192 |
window or its buffer---that is entirely under the control of the command |
|---|
| 1193 |
binding of the key sequence. |
|---|
| 1194 |
|
|---|
| 1195 |
@node Click Events |
|---|
| 1196 |
@subsection Click Events |
|---|
| 1197 |
@cindex click event |
|---|
| 1198 |
@cindex mouse click event |
|---|
| 1199 |
|
|---|
| 1200 |
When the user presses a mouse button and releases it at the same |
|---|
| 1201 |
location, that generates a @dfn{click} event. All mouse click event |
|---|
| 1202 |
share the same format: |
|---|
| 1203 |
|
|---|
| 1204 |
@example |
|---|
| 1205 |
(@var{event-type} @var{position} @var{click-count}) |
|---|
| 1206 |
@end example |
|---|
| 1207 |
|
|---|
| 1208 |
@table @asis |
|---|
| 1209 |
@item @var{event-type} |
|---|
| 1210 |
This is a symbol that indicates which mouse button was used. It is |
|---|
| 1211 |
one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the |
|---|
| 1212 |
buttons are numbered left to right. |
|---|
| 1213 |
|
|---|
| 1214 |
You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-}, |
|---|
| 1215 |
@samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift |
|---|
| 1216 |
and super, just as you would with function keys. |
|---|
| 1217 |
|
|---|
| 1218 |
This symbol also serves as the event type of the event. Key bindings |
|---|
| 1219 |
describe events by their types; thus, if there is a key binding for |
|---|
| 1220 |
@code{mouse-1}, that binding would apply to all events whose |
|---|
| 1221 |
@var{event-type} is @code{mouse-1}. |
|---|
| 1222 |
|
|---|
| 1223 |
@item @var{position} |
|---|
| 1224 |
This is the position where the mouse click occurred. The actual |
|---|
| 1225 |
format of @var{position} depends on what part of a window was clicked |
|---|
| 1226 |
on. The various formats are described below. |
|---|
| 1227 |
|
|---|
| 1228 |
@item @var{click-count} |
|---|
| 1229 |
This is the number of rapid repeated presses so far of the same mouse |
|---|
| 1230 |
button. @xref{Repeat Events}. |
|---|
| 1231 |
@end table |
|---|
| 1232 |
|
|---|
| 1233 |
For mouse click events in the text area, mode line, header line, or in |
|---|
| 1234 |
the marginal areas, @var{position} has this form: |
|---|
|
|---|