diff options
| author | Richard M. Stallman | 1994-03-08 22:47:20 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1994-03-08 22:47:20 +0000 |
| commit | 8db970a4e755ce9a49d6498c5302d91ea84d9909 (patch) | |
| tree | 468d9e133ae7e3554796cd28cabb73d9d7d79f88 | |
| parent | a117e51e29128cdd4d00dfbf5ff7ba0d28934866 (diff) | |
| download | emacs-8db970a4e755ce9a49d6498c5302d91ea84d9909.tar.gz emacs-8db970a4e755ce9a49d6498c5302d91ea84d9909.zip | |
Initial revision
| -rw-r--r-- | lispref/commands.texi | 2257 |
1 files changed, 2257 insertions, 0 deletions
diff --git a/lispref/commands.texi b/lispref/commands.texi new file mode 100644 index 00000000000..19eb0340df7 --- /dev/null +++ b/lispref/commands.texi | |||
| @@ -0,0 +1,2257 @@ | |||
| 1 | @c -*-texinfo-*- | ||
| 2 | @c This is part of the GNU Emacs Lisp Reference Manual. | ||
| 3 | @c Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | ||
| 4 | @c See the file elisp.texi for copying conditions. | ||
| 5 | @setfilename ../info/commands | ||
| 6 | @node Command Loop, Keymaps, Minibuffers, Top | ||
| 7 | @chapter Command Loop | ||
| 8 | @cindex editor command loop | ||
| 9 | @cindex command loop | ||
| 10 | |||
| 11 | When you run Emacs, it enters the @dfn{editor command loop} almost | ||
| 12 | immediately. This loop reads key sequences, executes their definitions, | ||
| 13 | and displays the results. In this chapter, we describe how these things | ||
| 14 | are done, and the subroutines that allow Lisp programs to do them. | ||
| 15 | |||
| 16 | @menu | ||
| 17 | * Command Overview:: How the command loop reads commands. | ||
| 18 | * Defining Commands:: Specifying how a function should read arguments. | ||
| 19 | * Interactive Call:: Calling a command, so that it will read arguments. | ||
| 20 | * Command Loop Info:: Variables set by the command loop for you to examine. | ||
| 21 | * Input Events:: What input looks like when you read it. | ||
| 22 | * Reading Input:: How to read input events from the keyboard or mouse. | ||
| 23 | * Waiting:: Waiting for user input or elapsed time. | ||
| 24 | * Quitting:: How @kbd{C-g} works. How to catch or defer quitting. | ||
| 25 | * Prefix Command Arguments:: How the commands to set prefix args work. | ||
| 26 | * Recursive Editing:: Entering a recursive edit, | ||
| 27 | and why you usually shouldn't. | ||
| 28 | * Disabling Commands:: How the command loop handles disabled commands. | ||
| 29 | * Command History:: How the command history is set up, and how accessed. | ||
| 30 | * Keyboard Macros:: How keyboard macros are implemented. | ||
| 31 | @end menu | ||
| 32 | |||
| 33 | @node Command Overview | ||
| 34 | @section Command Loop Overview | ||
| 35 | |||
| 36 | The first thing the command loop must do is read a key sequence, which | ||
| 37 | is a sequence of events that translates into a command. It does this by | ||
| 38 | calling the function @code{read-key-sequence}. Your Lisp code can also | ||
| 39 | call this function (@pxref{Key Sequence Input}). Lisp programs can also | ||
| 40 | do input at a lower level with @code{read-event} (@pxref{Reading One | ||
| 41 | Event}) or discard pending input with @code{discard-input} | ||
| 42 | (@pxref{Peeking and Discarding}). | ||
| 43 | |||
| 44 | The key sequence is translated into a command through the currently | ||
| 45 | active keymaps. @xref{Key Lookup}, for information on how this is done. | ||
| 46 | The result should be a keyboard macro or an interactively callable | ||
| 47 | function. If the key is @kbd{M-x}, then it reads the name of another | ||
| 48 | command, which is used instead. This is done by the command | ||
| 49 | @code{execute-extended-command} (@pxref{Interactive Call}). | ||
| 50 | |||
| 51 | Once the command is chosen, it must be executed, which includes | ||
| 52 | reading arguments to be given to it. This is done by calling | ||
| 53 | @code{command-execute} (@pxref{Interactive Call}). For commands written | ||
| 54 | in Lisp, the @code{interactive} specification says how to read the | ||
| 55 | arguments. This may use the prefix argument (@pxref{Prefix Command | ||
| 56 | Arguments}) or may read with prompting in the minibuffer | ||
| 57 | (@pxref{Minibuffers}). For example, the command @code{find-file} has an | ||
| 58 | @code{interactive} specification which says to read a file name using | ||
| 59 | the minibuffer. The command's function body does not use the | ||
| 60 | minibuffer; if you call this command from Lisp code as a function, you | ||
| 61 | must supply the file name string as an ordinary Lisp function argument. | ||
| 62 | |||
| 63 | If the command is a string or vector (i.e., a keyboard macro) then | ||
| 64 | @code{execute-kbd-macro} is used to execute it. You can call this | ||
| 65 | function yourself (@pxref{Keyboard Macros}). | ||
| 66 | |||
| 67 | If a command runs away, typing @kbd{C-g} terminates its execution | ||
| 68 | immediately. This is called @dfn{quitting} (@pxref{Quitting}). | ||
| 69 | |||
| 70 | @defvar pre-command-hook | ||
| 71 | The editor command loop runs this normal hook before each command. | ||
| 72 | @end defvar | ||
| 73 | |||
| 74 | @defvar post-command-hook | ||
| 75 | The editor command loop runs this normal hook after each command, | ||
| 76 | and also when the command loop is entered, or reentered after | ||
| 77 | an error or quit. | ||
| 78 | @end defvar | ||
| 79 | |||
| 80 | @node Defining Commands | ||
| 81 | @section Defining Commands | ||
| 82 | @cindex defining commands | ||
| 83 | @cindex commands, defining | ||
| 84 | @cindex functions, making them interactive | ||
| 85 | @cindex interactive function | ||
| 86 | |||
| 87 | A Lisp function becomes a command when its body contains, at top | ||
| 88 | level, a form which calls the special form @code{interactive}. This | ||
| 89 | form does nothing when actually executed, but its presence serves as a | ||
| 90 | flag to indicate that interactive calling is permitted. Its argument | ||
| 91 | controls the reading of arguments for an interactive call. | ||
| 92 | |||
| 93 | @menu | ||
| 94 | * Using Interactive:: General rules for @code{interactive}. | ||
| 95 | * Interactive Codes:: The standard letter-codes for reading arguments | ||
| 96 | in various ways. | ||
| 97 | * Interactive Examples:: Examples of how to read interactive arguments. | ||
| 98 | @end menu | ||
| 99 | |||
| 100 | @node Using Interactive | ||
| 101 | @subsection Using @code{interactive} | ||
| 102 | |||
| 103 | This section describes how to write the @code{interactive} form that | ||
| 104 | makes a Lisp function an interactively-callable command. | ||
| 105 | |||
| 106 | @defspec interactive arg-descriptor | ||
| 107 | @cindex argument descriptors | ||
| 108 | This special form declares that the function in which it appears is a | ||
| 109 | command, and that it may therefore be called interactively (via | ||
| 110 | @kbd{M-x} or by entering a key sequence bound to it). The argument | ||
| 111 | @var{arg-descriptor} declares the way the arguments to the command are | ||
| 112 | to be computed when the command is called interactively. | ||
| 113 | |||
| 114 | A command may be called from Lisp programs like any other function, but | ||
| 115 | then the arguments are supplied by the caller and @var{arg-descriptor} | ||
| 116 | has no effect. | ||
| 117 | |||
| 118 | The @code{interactive} form has its effect because the command loop | ||
| 119 | (actually, its subroutine @code{call-interactively}) scans through the | ||
| 120 | function definition looking for it, before calling the function. Once | ||
| 121 | the function is called, all its body forms including the | ||
| 122 | @code{interactive} form are executed, but at this time | ||
| 123 | @code{interactive} simply returns @code{nil} without even evaluating its | ||
| 124 | argument. | ||
| 125 | @end defspec | ||
| 126 | |||
| 127 | There are three possibilities for the argument @var{arg-descriptor}: | ||
| 128 | |||
| 129 | @itemize @bullet | ||
| 130 | @item | ||
| 131 | It may be omitted or @code{nil}; then the command is called with no | ||
| 132 | arguments. This leads quickly to an error if the command requires one | ||
| 133 | or more arguments. | ||
| 134 | |||
| 135 | @item | ||
| 136 | It may be a Lisp expression that is not a string; then it should be a | ||
| 137 | form that is evaluated to get a list of arguments to pass to the | ||
| 138 | command. | ||
| 139 | @cindex argument evaluation form | ||
| 140 | |||
| 141 | @item | ||
| 142 | @cindex argument prompt | ||
| 143 | It may be a string; then its contents should consist of a code character | ||
| 144 | followed by a prompt (which some code characters use and some ignore). | ||
| 145 | The prompt ends either with the end of the string or with a newline. | ||
| 146 | Here is a simple example: | ||
| 147 | |||
| 148 | @smallexample | ||
| 149 | (interactive "bFrobnicate buffer: ") | ||
| 150 | @end smallexample | ||
| 151 | |||
| 152 | @noindent | ||
| 153 | The code letter @samp{b} says to read the name of an existing buffer, | ||
| 154 | with completion. The buffer name is the sole argument passed to the | ||
| 155 | command. The rest of the string is a prompt. | ||
| 156 | |||
| 157 | If there is a newline character in the string, it terminates the prompt. | ||
| 158 | If the string does not end there, then the rest of the string should | ||
| 159 | contain another code character and prompt, specifying another argument. | ||
| 160 | You can specify any number of arguments in this way. | ||
| 161 | |||
| 162 | @c Emacs 19 feature | ||
| 163 | The prompt string can use @samp{%} to include previous argument values | ||
| 164 | in the prompt. This is done using @code{format} (@pxref{Formatting | ||
| 165 | Strings}). For example, here is how you could read the name of an | ||
| 166 | existing buffer followed by a new name to give to that buffer: | ||
| 167 | |||
| 168 | @smallexample | ||
| 169 | @group | ||
| 170 | (interactive "bBuffer to rename: \nsRename buffer %s to: ") | ||
| 171 | @end group | ||
| 172 | @end smallexample | ||
| 173 | |||
| 174 | @cindex @samp{*} in interactive | ||
| 175 | @kindex buffer-read-only | ||
| 176 | If the first character in the string is @samp{*}, then an error is | ||
| 177 | signaled if the buffer is read-only. | ||
| 178 | |||
| 179 | @cindex @samp{@@} in interactive | ||
| 180 | @c Emacs 19 feature | ||
| 181 | If the first character in the string is @samp{@@}, and if the key | ||
| 182 | sequence used to invoke the command includes any mouse events, then | ||
| 183 | the window associated with the first of those events is selected | ||
| 184 | before the command is run. | ||
| 185 | |||
| 186 | You can use @samp{*} and @samp{@@} together; the order does not matter. | ||
| 187 | Actual reading of arguments is controlled by the rest of the prompt | ||
| 188 | string (starting with the first character that is not @samp{*} or | ||
| 189 | @samp{@@}). | ||
| 190 | @end itemize | ||
| 191 | |||
| 192 | @node Interactive Codes | ||
| 193 | @comment node-name, next, previous, up | ||
| 194 | @subsection Code Characters for @code{interactive} | ||
| 195 | @cindex interactive code description | ||
| 196 | @cindex description for interactive codes | ||
| 197 | @cindex codes, interactive, description of | ||
| 198 | @cindex characters for interactive codes | ||
| 199 | |||
| 200 | The code character descriptions below contain a number of key words, | ||
| 201 | defined here as follows: | ||
| 202 | |||
| 203 | @table @b | ||
| 204 | @item Completion | ||
| 205 | @cindex interactive completion | ||
| 206 | Provide completion. @key{TAB}, @key{SPC}, and @key{RET} perform name | ||
| 207 | completion because the argument is read using @code{completing-read} | ||
| 208 | (@pxref{Completion}). @kbd{?} displays a list of possible completions. | ||
| 209 | |||
| 210 | @item Existing | ||
| 211 | Require the name of an existing object. An invalid name is not | ||
| 212 | accepted; the commands to exit the minibuffer do not exit if the current | ||
| 213 | input is not valid. | ||
| 214 | |||
| 215 | @item Default | ||
| 216 | @cindex default argument string | ||
| 217 | A default value of some sort is used if the user enters no text in the | ||
| 218 | minibuffer. The default depends on the code character. | ||
| 219 | |||
| 220 | @item No I/O | ||
| 221 | This code letter computes an argument without reading any input. | ||
| 222 | Therefore, it does not use a prompt string, and any prompt string you | ||
| 223 | supply is ignored. | ||
| 224 | |||
| 225 | @item Prompt | ||
| 226 | A prompt immediately follows the code character. The prompt ends either | ||
| 227 | with the end of the string or with a newline. | ||
| 228 | |||
| 229 | @item Special | ||
| 230 | This code character is meaningful only at the beginning of the | ||
| 231 | interactive string, and it does not look for a prompt or a newline. | ||
| 232 | It is a single, isolated character. | ||
| 233 | @end table | ||
| 234 | |||
| 235 | @cindex reading interactive arguments | ||
| 236 | Here are the code character descriptions for use with @code{interactive}: | ||
| 237 | |||
| 238 | @table @samp | ||
| 239 | @item * | ||
| 240 | Signal an error if the current buffer is read-only. Special. | ||
| 241 | |||
| 242 | @item @@ | ||
| 243 | Select the window mentioned in the first mouse event in the key | ||
| 244 | sequence that invoked this command. Special. | ||
| 245 | |||
| 246 | @item a | ||
| 247 | A function name (i.e., a symbol which is @code{fboundp}). Existing, | ||
| 248 | Completion, Prompt. | ||
| 249 | |||
| 250 | @item b | ||
| 251 | The name of an existing buffer. By default, uses the name of the | ||
| 252 | current buffer (@pxref{Buffers}). Existing, Completion, Default, | ||
| 253 | Prompt. | ||
| 254 | |||
| 255 | @item B | ||
| 256 | A buffer name. The buffer need not exist. By default, uses the name of | ||
| 257 | a recently used buffer other than the current buffer. Completion, | ||
| 258 | Prompt. | ||
| 259 | |||
| 260 | @item c | ||
| 261 | A character. The cursor does not move into the echo area. Prompt. | ||
| 262 | |||
| 263 | @item C | ||
| 264 | A command name (i.e., a symbol satisfying @code{commandp}). Existing, | ||
| 265 | Completion, Prompt. | ||
| 266 | |||
| 267 | @item d | ||
| 268 | @cindex position argument | ||
| 269 | The position of point as a number (@pxref{Point}). No I/O. | ||
| 270 | |||
| 271 | @item D | ||
| 272 | A directory name. The default is the current default directory of the | ||
| 273 | current buffer, @code{default-directory} (@pxref{System Environment}). | ||
| 274 | Existing, Completion, Default, Prompt. | ||
| 275 | |||
| 276 | @item e | ||
| 277 | The first or next mouse event in the key sequence that invoked the command. | ||
| 278 | More precisely, @samp{e} gets events which are lists, so you can look at | ||
| 279 | the data in the lists. @xref{Input Events}. No I/O. | ||
| 280 | |||
| 281 | You can use @samp{e} more than once in a single command's interactive | ||
| 282 | specification. If the key sequence which invoked the command has | ||
| 283 | @var{n} events with parameters, the @var{n}th @samp{e} provides the | ||
| 284 | @var{n}th list event. Events which are not lists, such as function keys | ||
| 285 | and @sc{ASCII} characters, do not count where @samp{e} is concerned. | ||
| 286 | |||
| 287 | Even though @samp{e} does not use a prompt string, you must follow | ||
| 288 | it with a newline if it is not the last code character. | ||
| 289 | |||
| 290 | @item f | ||
| 291 | A file name of an existing file (@pxref{File Names}). The default | ||
| 292 | directory is @code{default-directory}. Existing, Completion, Default, | ||
| 293 | Prompt. | ||
| 294 | |||
| 295 | @item F | ||
| 296 | A file name. The file need not exist. Completion, Default, Prompt. | ||
| 297 | |||
| 298 | @item k | ||
| 299 | A key sequence (@pxref{Keymap Terminology}). This keeps reading events | ||
| 300 | until a command (or undefined command) is found in the current key | ||
| 301 | maps. The key sequence argument is represented as a string or vector. | ||
| 302 | The cursor does not move into the echo area. Prompt. | ||
| 303 | |||
| 304 | This kind of input is used by commands such as @code{describe-key} and | ||
| 305 | @code{global-set-key}. | ||
| 306 | |||
| 307 | @item m | ||
| 308 | @cindex marker argument | ||
| 309 | The position of the mark as a number. No I/O. | ||
| 310 | |||
| 311 | @item n | ||
| 312 | A number read with the minibuffer. If the input is not a number, the | ||
| 313 | user is asked to try again. The prefix argument, if any, is not used. | ||
| 314 | Prompt. | ||
| 315 | |||
| 316 | @item N | ||
| 317 | @cindex raw prefix argument usage | ||
| 318 | The raw prefix argument. If the prefix argument is @code{nil}, then a | ||
| 319 | number is read as with @kbd{n}. Requires a number. Prompt. | ||
| 320 | |||
| 321 | @item p | ||
| 322 | @cindex numeric prefix argument usage | ||
| 323 | The numeric prefix argument. (Note that this @samp{p} is lower case.) | ||
| 324 | No I/O.@refill | ||
| 325 | |||
| 326 | @item P | ||
| 327 | The raw prefix argument. (Note that this @samp{P} is upper case.) | ||
| 328 | @xref{Prefix Command Arguments}. No I/O.@refill | ||
| 329 | |||
| 330 | @item r | ||
| 331 | @cindex region argument | ||
| 332 | Point and the mark, as two numeric arguments, smallest first. This is | ||
| 333 | the only code letter that specifies two successive arguments rather than | ||
| 334 | one. No I/O. | ||
| 335 | |||
| 336 | @item s | ||
| 337 | Arbitrary text, read in the minibuffer and returned as a string | ||
| 338 | (@pxref{Text from Minibuffer}). Terminate the input with either | ||
| 339 | @key{LFD} or @key{RET}. (@kbd{C-q} may be used to include either of | ||
| 340 | these characters in the input.) Prompt. | ||
| 341 | |||
| 342 | @item S | ||
| 343 | An interned symbol whose name is read in the minibuffer. Any whitespace | ||
| 344 | character terminates the input. (Use @kbd{C-q} to include whitespace in | ||
| 345 | the string.) Other characters that normally terminate a symbol (e.g., | ||
| 346 | parentheses and brackets) do not do so here. Prompt. | ||
| 347 | |||
| 348 | @item v | ||
| 349 | A variable declared to be a user option (i.e., satisfying the predicate | ||
| 350 | @code{user-variable-p}). @xref{High-Level Completion}. Existing, | ||
| 351 | Completion, Prompt. | ||
| 352 | |||
| 353 | @item x | ||
| 354 | A Lisp object specified in printed representation, terminated with a | ||
| 355 | @key{LFD} or @key{RET}. The object is not evaluated. @xref{Object from | ||
| 356 | Minibuffer}. Prompt. | ||
| 357 | |||
| 358 | @item X | ||
| 359 | @cindex evaluated expression argument | ||
| 360 | A Lisp form is read as with @kbd{x}, but then evaluated so that its | ||
| 361 | value becomes the argument for the command. Prompt. | ||
| 362 | @end table | ||
| 363 | |||
| 364 | @node Interactive Examples | ||
| 365 | @comment node-name, next, previous, up | ||
| 366 | @subsection Examples of Using @code{interactive} | ||
| 367 | @cindex examples of using @code{interactive} | ||
| 368 | @cindex @code{interactive}, examples of using | ||
| 369 | |||
| 370 | Here are some examples of @code{interactive}: | ||
| 371 | |||
| 372 | @example | ||
| 373 | @group | ||
| 374 | (defun foo1 () ; @r{@code{foo1} takes no arguments,} | ||
| 375 | (interactive) ; @r{just moves forward two words.} | ||
| 376 | (forward-word 2)) | ||
| 377 | @result{} foo1 | ||
| 378 | @end group | ||
| 379 | |||
| 380 | @group | ||
| 381 | (defun foo2 (n) ; @r{@code{foo2} takes one argument,} | ||
| 382 | (interactive "p") ; @r{which is the numeric prefix.} | ||
| 383 | (forward-word (* 2 n))) | ||
| 384 | @result{} foo2 | ||
| 385 | @end group | ||
| 386 | |||
| 387 | @group | ||
| 388 | (defun foo3 (n) ; @r{@code{foo3} takes one argument,} | ||
| 389 | (interactive "nCount:") ; @r{which is read with the Minibuffer.} | ||
| 390 | (forward-word (* 2 n))) | ||
| 391 | @result{} foo3 | ||
| 392 | @end group | ||
| 393 | |||
| 394 | @group | ||
| 395 | (defun three-b (b1 b2 b3) | ||
| 396 | "Select three existing buffers. | ||
| 397 | Put them into three windows, selecting the last one." | ||
| 398 | @end group | ||
| 399 | (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:") | ||
| 400 | (delete-other-windows) | ||
| 401 | (split-window (selected-window) 8) | ||
| 402 | (switch-to-buffer b1) | ||
| 403 | (other-window 1) | ||
| 404 | (split-window (selected-window) 8) | ||
| 405 | (switch-to-buffer b2) | ||
| 406 | (other-window 1) | ||
| 407 | (switch-to-buffer b3)) | ||
| 408 | @result{} three-b | ||
| 409 | @group | ||
| 410 | (three-b "*scratch*" "declarations.texi" "*mail*") | ||
| 411 | @result{} nil | ||
| 412 | @end group | ||
| 413 | @end example | ||
| 414 | |||
| 415 | @node Interactive Call | ||
| 416 | @section Interactive Call | ||
| 417 | @cindex interactive call | ||
| 418 | |||
| 419 | After the command loop has translated a key sequence into a | ||
| 420 | definition, it invokes that definition using the function | ||
| 421 | @code{command-execute}. If the definition is a function that is a | ||
| 422 | command, @code{command-execute} calls @code{call-interactively}, which | ||
| 423 | reads the arguments and calls the command. You can also call these | ||
| 424 | functions yourself. | ||
| 425 | |||
| 426 | @defun commandp object | ||
| 427 | Returns @code{t} if @var{object} is suitable for calling interactively; | ||
| 428 | that is, if @var{object} is a command. Otherwise, returns @code{nil}. | ||
| 429 | |||
| 430 | The interactively callable objects include strings and vectors (treated | ||
| 431 | as keyboard macros), lambda expressions that contain a top-level call to | ||
| 432 | @code{interactive}, byte-code function objects, autoload objects that | ||
| 433 | are declared as interactive (non-@code{nil} fourth argument to | ||
| 434 | @code{autoload}), and some of the primitive functions. | ||
| 435 | |||
| 436 | A symbol is @code{commandp} if its function definition is | ||
| 437 | @code{commandp}. | ||
| 438 | |||
| 439 | Keys and keymaps are not commands. Rather, they are used to look up | ||
| 440 | commands (@pxref{Keymaps}). | ||
| 441 | |||
| 442 | See @code{documentation} in @ref{Accessing Documentation}, for a | ||
| 443 | realistic example of using @code{commandp}. | ||
| 444 | @end defun | ||
| 445 | |||
| 446 | @defun call-interactively command &optional record-flag | ||
| 447 | This function calls the interactively callable function @var{command}, | ||
| 448 | reading arguments according to its interactive calling specifications. | ||
| 449 | An error is signaled if @var{command} cannot be called interactively | ||
| 450 | (i.e., it is not a command). Note that keyboard macros (strings and | ||
| 451 | vectors) are not accepted, even though they are considered commands. | ||
| 452 | |||
| 453 | @cindex record command history | ||
| 454 | If @var{record-flag} is non-@code{nil}, then this command and its | ||
| 455 | arguments are unconditionally added to the list @code{command-history}. | ||
| 456 | Otherwise, the command is added only if it uses the minibuffer to read | ||
| 457 | an argument. @xref{Command History}. | ||
| 458 | @end defun | ||
| 459 | |||
| 460 | @defun command-execute command &optional record-flag | ||
| 461 | @cindex keyboard macro execution | ||
| 462 | This function executes @var{command} as an editing command. The | ||
| 463 | argument @var{command} must satisfy the @code{commandp} predicate; i.e., | ||
| 464 | it must be an interactively callable function or a string. | ||
| 465 | |||
| 466 | A string or vector as @var{command} is executed with | ||
| 467 | @code{execute-kbd-macro}. A function is passed to | ||
| 468 | @code{call-interactively}, along with the optional @var{record-flag}. | ||
| 469 | |||
| 470 | A symbol is handled by using its function definition in its place. A | ||
| 471 | symbol with an @code{autoload} definition counts as a command if it was | ||
| 472 | declared to stand for an interactively callable function. Such a | ||
| 473 | definition is handled by loading the specified library and then | ||
| 474 | rechecking the definition of the symbol. | ||
| 475 | @end defun | ||
| 476 | |||
| 477 | @deffn Command execute-extended-command prefix-argument | ||
| 478 | @cindex read command name | ||
| 479 | This function reads a command name from the minibuffer using | ||
| 480 | @code{completing-read} (@pxref{Completion}). Then it uses | ||
| 481 | @code{command-execute} to call the specified command. Whatever that | ||
| 482 | command returns becomes the value of @code{execute-extended-command}. | ||
| 483 | |||
| 484 | @cindex execute with prefix argument | ||
| 485 | If the command asks for a prefix argument, the value | ||
| 486 | @var{prefix-argument} is supplied. If @code{execute-extended-command} | ||
| 487 | is called interactively, the current raw prefix argument is used for | ||
| 488 | @var{prefix-argument}, and thus passed on to whatever command is run. | ||
| 489 | |||
| 490 | @c !!! Should this be @kindex? | ||
| 491 | @cindex @kbd{M-x} | ||
| 492 | @code{execute-extended-command} is the normal definition of @kbd{M-x}, | ||
| 493 | so it uses the string @w{@samp{M-x }} as a prompt. (It would be better | ||
| 494 | to take the prompt from the events used to invoke | ||
| 495 | @code{execute-extended-command}, but that is painful to implement.) A | ||
| 496 | description of the value of the prefix argument, if any, also becomes | ||
| 497 | part of the prompt. | ||
| 498 | |||
| 499 | @example | ||
| 500 | @group | ||
| 501 | (execute-extended-command 1) | ||
| 502 | ---------- Buffer: Minibuffer ---------- | ||
| 503 | M-x forward-word RET | ||
| 504 | ---------- Buffer: Minibuffer ---------- | ||
| 505 | @result{} t | ||
| 506 | @end group | ||
| 507 | @end example | ||
| 508 | @end deffn | ||
| 509 | |||
| 510 | @defun interactive-p | ||
| 511 | This function returns @code{t} if the containing function (the one that | ||
| 512 | called @code{interactive-p}) was called interactively, with the function | ||
| 513 | @code{call-interactively}. (It makes no difference whether | ||
| 514 | @code{call-interactively} was called from Lisp or directly from the | ||
| 515 | editor command loop.) Note that if the containing function was called | ||
| 516 | by Lisp evaluation (or with @code{apply} or @code{funcall}), then it was | ||
| 517 | not called interactively. | ||
| 518 | |||
| 519 | The usual application of @code{interactive-p} is for deciding whether to | ||
| 520 | print an informative message. As a special exception, | ||
| 521 | @code{interactive-p} returns @code{nil} whenever a keyboard macro is | ||
| 522 | being run. This is to suppress the informative messages and speed | ||
| 523 | execution of the macro. | ||
| 524 | |||
| 525 | For example: | ||
| 526 | |||
| 527 | @example | ||
| 528 | @group | ||
| 529 | (defun foo () | ||
| 530 | (interactive) | ||
| 531 | (and (interactive-p) | ||
| 532 | (message "foo"))) | ||
| 533 | @result{} foo | ||
| 534 | @end group | ||
| 535 | |||
| 536 | @group | ||
| 537 | (defun bar () | ||
| 538 | (interactive) | ||
| 539 | (setq foobar (list (foo) (interactive-p)))) | ||
| 540 | @result{} bar | ||
| 541 | @end group | ||
| 542 | |||
| 543 | @group | ||
| 544 | ;; @r{Type @kbd{M-x foo}.} | ||
| 545 | @print{} foo | ||
| 546 | @end group | ||
| 547 | |||
| 548 | @group | ||
| 549 | ;; @r{Type @kbd{M-x bar}.} | ||
| 550 | ;; @r{This does not print anything.} | ||
| 551 | @end group | ||
| 552 | |||
| 553 | @group | ||
| 554 | foobar | ||
| 555 | @result{} (nil t) | ||
| 556 | @end group | ||
| 557 | @end example | ||
| 558 | @end defun | ||
| 559 | |||
| 560 | @node Command Loop Info | ||
| 561 | @comment node-name, next, previous, up | ||
| 562 | @section Information from the Command Loop | ||
| 563 | |||
| 564 | The editor command loop sets several Lisp variables to keep status | ||
| 565 | records for itself and for commands that are run. | ||
| 566 | |||
| 567 | @defvar last-command | ||
| 568 | This variable records the name of the previous command executed by the | ||
| 569 | command loop (the one before the current command). Normally the value | ||
| 570 | is a symbol with a function definition, but this is not guaranteed. | ||
| 571 | |||
| 572 | The value is set by copying the value of @code{this-command} when a | ||
| 573 | command returns to the command loop, except when the command specifies a | ||
| 574 | prefix argument for the following command. | ||
| 575 | @end defvar | ||
| 576 | |||
| 577 | @defvar this-command | ||
| 578 | @cindex current command | ||
| 579 | This variable records the name of the command now being executed by | ||
| 580 | the editor command loop. Like @code{last-command}, it is normally a symbol | ||
| 581 | with a function definition. | ||
| 582 | |||
| 583 | This variable is set by the command loop just before the command is run, | ||
| 584 | and its value is copied into @code{last-command} when the command | ||
| 585 | finishes (unless the command specifies a prefix argument for the | ||
| 586 | following command). | ||
| 587 | |||
| 588 | @cindex kill command repetition | ||
| 589 | Some commands change the value of this variable during their execution, | ||
| 590 | simply as a flag for whatever command runs next. In particular, the | ||
| 591 | functions that kill text set @code{this-command} to @code{kill-region} | ||
| 592 | so that any kill commands immediately following will know to append the | ||
| 593 | killed text to the previous kill. | ||
| 594 | @end defvar | ||
| 595 | |||
| 596 | If you do not want a particular command to be recognized as the previous | ||
| 597 | command in the case where it got an error, you must code that command to | ||
| 598 | prevent this. One way is to set @code{this-command} to @code{t} at the | ||
| 599 | beginning of the command, and set @code{this-command} back to its proper | ||
| 600 | value at the end, like this: | ||
| 601 | |||
| 602 | @example | ||
| 603 | (defun foo (args@dots{}) | ||
| 604 | (interactive @dots{}) | ||
| 605 | (let ((old-this-command this-command)) | ||
| 606 | (setq this-command t) | ||
| 607 | @r{@dots{}do the work@dots{}} | ||
| 608 | (setq this-command old-this-command))) | ||
| 609 | @end example | ||
| 610 | |||
| 611 | @defun this-command-keys | ||
| 612 | This function returns a string or vector containing the key sequence | ||
| 613 | that invoked the present command, plus any previous commands that | ||
| 614 | generated the prefix argument for this command. The value is a string | ||
| 615 | if all those events were characters. @xref{Input Events}. | ||
| 616 | |||
| 617 | @example | ||
| 618 | @group | ||
| 619 | (this-command-keys) | ||
| 620 | ;; @r{Now type @kbd{C-u C-x C-e}.} | ||
| 621 | @result{} "^U^X^E" | ||
| 622 | @end group | ||
| 623 | @end example | ||
| 624 | @end defun | ||
| 625 | |||
| 626 | @defvar last-nonmenu-event | ||
| 627 | This variable holds the last input event read as part of a key | ||
| 628 | sequence, aside from events resulting from mouse menus. | ||
| 629 | |||
| 630 | One use of this variable is to figure out a good default location to | ||
| 631 | pop up another menu. | ||
| 632 | @end defvar | ||
| 633 | |||
| 634 | @defvar last-command-event | ||
| 635 | @defvarx last-command-char | ||
| 636 | This variable is set to the last input event that was read by the | ||
| 637 | command loop as part of a command. The principal use of this variable | ||
| 638 | is in @code{self-insert-command}, which uses it to decide which | ||
| 639 | character to insert. | ||
| 640 | |||
| 641 | @example | ||
| 642 | @group | ||
| 643 | last-command-char | ||
| 644 | ;; @r{Now type @kbd{C-u C-x C-e}.} | ||
| 645 | @result{} 5 | ||
| 646 | @end group | ||
| 647 | @end example | ||
| 648 | |||
| 649 | @noindent | ||
| 650 | The value is 5 because that is the @sc{ASCII} code for @kbd{C-e}. | ||
| 651 | |||
| 652 | The alias @code{last-command-char} exists for compatibility with | ||
| 653 | Emacs version 18. | ||
| 654 | @end defvar | ||
| 655 | |||
| 656 | @c Emacs 19 feature | ||
| 657 | @defvar last-event-frame | ||
| 658 | This variable records which frame the last input event was directed to. | ||
| 659 | Usually this is the frame that was selected when the event was | ||
| 660 | generated, but if that frame has redirected input focus to another | ||
| 661 | frame, the value is the frame to which the event was redirected. | ||
| 662 | @xref{Input Focus}. | ||
| 663 | @end defvar | ||
| 664 | |||
| 665 | @defvar echo-keystrokes | ||
| 666 | This variable determines how much time should elapse before command | ||
| 667 | characters echo. Its value must be an integer, which specifies the | ||
| 668 | number of seconds to wait before echoing. If the user types a prefix | ||
| 669 | key (say @kbd{C-x}) and then delays this many seconds before continuing, | ||
| 670 | the key @kbd{C-x} is echoed in the echo area. Any subsequent characters | ||
| 671 | in the same command will be echoed as well. | ||
| 672 | |||
| 673 | If the value is zero, then command input is not echoed. | ||
| 674 | @end defvar | ||
| 675 | |||
| 676 | @node Input Events | ||
| 677 | @section Input Events | ||
| 678 | @cindex events | ||
| 679 | @cindex input events | ||
| 680 | |||
| 681 | The Emacs command loop reads a sequence of @dfn{input events} that | ||
| 682 | represent keyboard or mouse activity. The events for keyboard activity | ||
| 683 | are characters or symbols; mouse events are always lists. This section | ||
| 684 | describes the representation and meaning of input events in detail. | ||
| 685 | |||
| 686 | A command invoked using events that are lists can get the full values of | ||
| 687 | these events using the @samp{e} interactive code. @xref{Interactive | ||
| 688 | Codes}. | ||
| 689 | |||
| 690 | A key sequence that starts with a mouse event is read using the keymaps | ||
| 691 | of the buffer in the window that the mouse was in, not the current | ||
| 692 | buffer. This does not imply that clicking in a window selects that | ||
| 693 | window or its buffer---that is entirely under the control of the command | ||
| 694 | binding of the key sequence. | ||
| 695 | |||
| 696 | @defun eventp object | ||
| 697 | This function returns non-@code{nil} if @var{event} is an input event. | ||
| 698 | @end defun | ||
| 699 | |||
| 700 | @menu | ||
| 701 | * Keyboard Events:: Ordinary characters--keys with symbols on them. | ||
| 702 | * Function Keys:: Function keys--keys with names, not symbols. | ||
| 703 | * Click Events:: Pushing and releasing a mouse button. | ||
| 704 | * Drag Events:: Moving the mouse before releasing the button. | ||
| 705 | * Button-Down Events:: A button was pushed and not yet released. | ||
| 706 | * Repeat Events:: Double and triple click (or drag, or down). | ||
| 707 | * Motion Events:: Just moving the mouse, not pushing a button. | ||
| 708 | * Focus Events:: Moving the mouse between frames. | ||
| 709 | * Event Examples:: Examples of the lists for mouse events. | ||
| 710 | * Classifying Events:: Finding the modifier keys in an event symbol. | ||
| 711 | Event types. | ||
| 712 | * Accessing Events:: Functions to extract info from events. | ||
| 713 | * Strings of Events:: Special considerations for putting | ||
| 714 | keyboard character events in a string. | ||
| 715 | @end menu | ||
| 716 | |||
| 717 | @node Keyboard Events | ||
| 718 | @subsection Keyboard Events | ||
| 719 | |||
| 720 | There are two kinds of input you can get from the keyboard: ordinary | ||
| 721 | keys, and function keys. Ordinary keys correspond to characters; the | ||
| 722 | events they generate are represented in Lisp as characters. In Emacs | ||
| 723 | versions 18 and earlier, characters were the only events. | ||
| 724 | |||
| 725 | @cindex modifier bits (of input character) | ||
| 726 | @cindex basic code (of input character) | ||
| 727 | An input character event consists of a @dfn{basic code} between 0 and | ||
| 728 | 255, plus any or all of these @dfn{modifier bits}: | ||
| 729 | |||
| 730 | @table @asis | ||
| 731 | @item meta | ||
| 732 | The 2**23 bit in the character code indicates a character | ||
| 733 | typed with the meta key held down. | ||
| 734 | |||
| 735 | @item control | ||
| 736 | The 2**22 bit in the character code indicates a non-@sc{ASCII} | ||
| 737 | control character. | ||
| 738 | |||
| 739 | @sc{ASCII} control characters such as @kbd{C-a} have special basic | ||
| 740 | codes of their own, so Emacs needs no special bit to indicate them. | ||
| 741 | Thus, the code for @kbd{C-a} is just 1. | ||
| 742 | |||
| 743 | But if you type a control combination not in @sc{ASCII}, such as | ||
| 744 | @kbd{%} with the control key, the numeric value you get is the code | ||
| 745 | for @kbd{%} plus 2**22 (assuming the terminal supports non-@sc{ASCII} | ||
| 746 | control characters). | ||
| 747 | |||
| 748 | @item shift | ||
| 749 | The 2**21 bit in the character code indicates an @sc{ASCII} control | ||
| 750 | character typed with the shift key held down. | ||
| 751 | |||
| 752 | For letters, the basic code indicates upper versus lower case; for | ||
| 753 | digits and punctuation, the shift key selects an entirely different | ||
| 754 | character with a different basic code. In order to keep within | ||
| 755 | the @sc{ASCII} character set whenever possible, Emacs avoids using | ||
| 756 | the 2**21 bit for those characters. | ||
| 757 | |||
| 758 | However, @sc{ASCII} provides no way to distinguish @kbd{C-A} from | ||
| 759 | @kbd{C-A}, so Emacs uses the 2**21 bit in @kbd{C-A} and not in | ||
| 760 | @kbd{C-a}. | ||
| 761 | |||
| 762 | @item hyper | ||
| 763 | The 2**20 bit in the character code indicates a character | ||
| 764 | typed with the hyper key held down. | ||
| 765 | |||
| 766 | @item super | ||
| 767 | The 2**19 bit in the character code indicates a character | ||
| 768 | typed with the super key held down. | ||
| 769 | |||
| 770 | @item alt | ||
| 771 | The 2**18 bit in the character code indicates a character typed with | ||
| 772 | the alt key held down. (On some terminals, the key labeled @key{ALT} | ||
| 773 | is actually the meta key.) | ||
| 774 | @end table | ||
| 775 | |||
| 776 | In the future, Emacs may support a larger range of basic codes. We | ||
| 777 | may also move the modifier bits to larger bit numbers. Therefore, you | ||
| 778 | should avoid mentioning specific bit numbers in your program. | ||
| 779 | Instead, the way to test the modifier bits of a character is with the | ||
| 780 | function @code{event-modifiers} (@pxref{Classifying Events}). | ||
| 781 | |||
| 782 | @node Function Keys | ||
| 783 | @subsection Function Keys | ||
| 784 | |||
| 785 | @cindex function keys | ||
| 786 | Most keyboards also have @dfn{function keys}---keys which have names or | ||
| 787 | symbols that are not characters. Function keys are represented in Lisp | ||
| 788 | as symbols; the symbol's name is the function key's label. For example, | ||
| 789 | pressing a key labeled @key{F1} places the symbol @code{f1} in the input | ||
| 790 | stream. | ||
| 791 | |||
| 792 | For all keyboard events, the event type (which classifies the event for | ||
| 793 | key lookup purposes) is identical to the event---it is the character or | ||
| 794 | the symbol. @xref{Classifying Events}. | ||
| 795 | |||
| 796 | Here are a few special cases in the symbol naming convention for | ||
| 797 | function keys: | ||
| 798 | |||
| 799 | @table @asis | ||
| 800 | @item @code{backspace}, @code{tab}, @code{newline}, @code{return}, @code{delete} | ||
| 801 | These keys correspond to common @sc{ASCII} control characters that have | ||
| 802 | special keys on most keyboards. | ||
| 803 | |||
| 804 | In @sc{ASCII}, @kbd{C-i} and @key{TAB} are the same character. Emacs | ||
| 805 | lets you distinguish them if you wish, by returning the former as the | ||
| 806 | integer 9, and the latter as the symbol @code{tab}. | ||
| 807 | |||
| 808 | Most of the time, it's not useful to distinguish the two. So normally | ||
| 809 | @code{function-key-map} is set up to map @code{tab} into 9. Thus, a | ||
| 810 | key binding for character code 9 also applies to @code{tab}. Likewise | ||
| 811 | for the other symbols in this group. The function @code{read-char} | ||
| 812 | also converts these events into characters. | ||
| 813 | |||
| 814 | In @sc{ASCII}, @key{BS} is really @kbd{C-h}. But @code{backspace} | ||
| 815 | converts into the character code 127 (@key{DEL}), not into code 8 | ||
| 816 | (@key{BS}). This is what most users prefer. | ||
| 817 | |||
| 818 | @item @code{kp-add}, @code{kp-decimal}, @code{kp-divide}, @dots{} | ||
| 819 | Keypad keys (to the right of the regular keyboard). | ||
| 820 | @item @code{kp-0}, @code{kp-1}, @dots{} | ||
| 821 | Keypad keys with digits. | ||
| 822 | @item @code{kp-f1}, @code{kp-f2}, @code{kp-f3}, @code{kp-f4} | ||
| 823 | Keypad PF keys. | ||
| 824 | @item @code{left}, @code{up}, @code{right}, @code{down} | ||
| 825 | Cursor arrow keys | ||
| 826 | @end table | ||
| 827 | |||
| 828 | You can use the modifier keys @key{CTRL}, @key{META}, @key{HYPER}, | ||
| 829 | @key{SUPER}, @key{ALT} and @key{SHIFT} with function keys. The way | ||
| 830 | to represent them is with prefixes in the symbol name: | ||
| 831 | |||
| 832 | @table @samp | ||
| 833 | @item A- | ||
| 834 | The alt modifier. | ||
| 835 | @item C- | ||
| 836 | The control modifier. | ||
| 837 | @item H- | ||
| 838 | The hyper modifier. | ||
| 839 | @item M- | ||
| 840 | The meta modifier. | ||
| 841 | @item S- | ||
| 842 | The shift modifier. | ||
| 843 | @item s- | ||
| 844 | The super modifier. | ||
| 845 | @end table | ||
| 846 | |||
| 847 | Thus, the symbol for the key @key{F3} with @key{META} held down is | ||
| 848 | @kbd{M-@key{F3}}. When you use more than one prefix, we recommend you | ||
| 849 | write them in alphabetical order (though the order does not matter in | ||
| 850 | arguments to the key-binding lookup and modification functions). | ||
| 851 | |||
| 852 | @node Click Events | ||
| 853 | @subsection Click Events | ||
| 854 | @cindex click event | ||
| 855 | @cindex mouse click event | ||
| 856 | |||
| 857 | When the user presses a mouse button and releases it at the same | ||
| 858 | location, that generates a @dfn{click} event. Mouse click events have | ||
| 859 | this form: | ||
| 860 | |||
| 861 | @example | ||
| 862 | (@var{event-type} | ||
| 863 | (@var{window} @var{buffer-pos} | ||
| 864 | (@var{x} . @var{y}) @var{timestamp}) | ||
| 865 | @var{click-count}) | ||
| 866 | @end example | ||
| 867 | |||
| 868 | Here is what the elements normally mean: | ||
| 869 | |||
| 870 | @table @var | ||
| 871 | @item event-type | ||
| 872 | This is a symbol that indicates which mouse button was used. It is | ||
| 873 | one of the symbols @code{mouse-1}, @code{mouse-2}, @dots{}, where the | ||
| 874 | buttons are numbered numbered left to right. | ||
| 875 | |||
| 876 | You can also use prefixes @samp{A-}, @samp{C-}, @samp{H-}, @samp{M-}, | ||
| 877 | @samp{S-} and @samp{s-} for modifiers alt, control, hyper, meta, shift | ||
| 878 | and super, just as you would with function keys. | ||
| 879 | |||
| 880 | This symbol also serves as the event type of the event. Key bindings | ||
| 881 | describe events by their types; thus, if there is a key binding for | ||
| 882 | @code{mouse-1}, that binding would apply to all events whose | ||
| 883 | @var{event-type} is @code{mouse-1}. | ||
| 884 | |||
| 885 | @item window | ||
| 886 | This is the window in which the click occurred. | ||
| 887 | |||
| 888 | @item x | ||
| 889 | @itemx y | ||
| 890 | These are the pixel-based coordinates of the click, relative to the top | ||
| 891 | left corner of @var{window}, which is @code{(0 . 0)}. | ||
| 892 | |||
| 893 | @item buffer-pos | ||
| 894 | This is the buffer position of the character clicked on. | ||
| 895 | |||
| 896 | @item timestamp | ||
| 897 | This is the time at which the event occurred, in milliseconds. (Since | ||
| 898 | this value wraps around the entire range of Emacs Lisp integers in about | ||
| 899 | five hours, it is useful only for relating the times of nearby events.) | ||
| 900 | |||
| 901 | @item click-count | ||
| 902 | This is the number of rapid repeated presses so far of the same mouse | ||
| 903 | button. @xref{Repeat Events}. | ||
| 904 | @end table | ||
| 905 | |||
| 906 | The meanings of @var{buffer-pos}, @var{row} and @var{column} are | ||
| 907 | somewhat different when the event location is in a special part of the | ||
| 908 | screen, such as the mode line or a scroll bar. | ||
| 909 | |||
| 910 | If the location is in a scroll bar, then @var{buffer-pos} is the symbol | ||
| 911 | @code{vertical-scroll-bar} or @code{horizontal-scroll-bar}, and the pair | ||
| 912 | @code{(@var{x} . @var{y})} is replaced with a pair @code{(@var{portion} | ||
| 913 | . @var{whole})}, where @var{portion} is the distance of the click from | ||
| 914 | the top or left end of the scroll bar, and @var{whole} is the length of | ||
| 915 | the entire scroll bar. | ||
| 916 | |||
| 917 | If the position is on a mode line or the vertical line separating | ||
| 918 | @var{window} from its neighbor to the right, then @var{buffer-pos} is | ||
| 919 | the symbol @code{mode-line} or @code{vertical-line}. For the mode line, | ||
| 920 | @var{row} does not have meaningful data. For the vertical line, | ||
| 921 | @var{column} does not have meaningful data. | ||
| 922 | |||
| 923 | @var{buffer-pos} may be a list containing a symbol (one of the symbols | ||
| 924 | listed above) instead of just the symbol. This is what happens after | ||
| 925 | the imaginary prefix keys for these events are inserted into the input | ||
| 926 | stream. @xref{Key Sequence Input}. | ||
| 927 | |||
| 928 | @node Drag Events | ||
| 929 | @subsection Drag Events | ||
| 930 | @cindex drag event | ||
| 931 | @cindex mouse drag event | ||
| 932 | |||
| 933 | With Emacs, you can have a drag event without even changing your | ||
| 934 | clothes. A @dfn{drag event} happens every time the user presses a mouse | ||
| 935 | button and then moves the mouse to a different character position before | ||
| 936 | releasing the button. Like all mouse events, drag events are | ||
| 937 | represented in Lisp as lists. The lists record both the starting mouse | ||
| 938 | position and the final position, like this: | ||
| 939 | |||
| 940 | @example | ||
| 941 | (@var{event-type} | ||
| 942 | (@var{window1} @var{buffer-pos1} | ||
| 943 | (@var{x1} . @var{y1}) @var{timestamp1}) | ||
| 944 | (@var{window2} @var{buffer-pos2} | ||
| 945 | (@var{x2} . @var{y2}) @var{timestamp2}) | ||
| 946 | @var{click-count}) | ||
| 947 | @end example | ||
| 948 | |||
| 949 | For a drag event, the name of the symbol @var{event-type} contains the | ||
| 950 | prefix @samp{drag-}. The second and third elements of the event give | ||
| 951 | the starting and ending position of the drag. Aside from that, the data | ||
| 952 | have the same meanings as in a click event (@pxref{Click Events}). You | ||
| 953 | can access the second element of any mouse event in the same way, with | ||
| 954 | no need to distinguish drag events from others. | ||
| 955 | |||
| 956 | The @samp{drag-} prefix follows the modifier key prefixes such as | ||
| 957 | @samp{C-} and @samp{M-}. | ||
| 958 | |||
| 959 | If @code{read-key-sequence} receives a drag event which has no key | ||
| 960 | binding, and the corresponding click event does have a binding, it | ||
| 961 | changes the drag event into a click event at the drag's starting | ||
| 962 | position. This means that you don't have to distinguish between click | ||
| 963 | and drag events unless you want to. | ||
| 964 | |||
| 965 | @node Button-Down Events | ||
| 966 | @subsection Button-Down Events | ||
| 967 | @cindex button-down event | ||
| 968 | |||
| 969 | Click and drag events happen when the user releases a mouse button. | ||
| 970 | They cannot happen earlier, because there is no way to distinguish a | ||
| 971 | click from a drag until the button is released. | ||
| 972 | |||
| 973 | If you want to take action as soon as a button is pressed, you need to | ||
| 974 | handle @dfn{button-down} events.@footnote{Button-down is the | ||
| 975 | conservative antithesis of drag.}. These occur as soon as a button is | ||
| 976 | pressed. They are represented by lists which look exactly like click | ||
| 977 | events (@pxref{Click Events}), except that the name of @var{event-type} | ||
| 978 | contains the prefix @samp{down-}. The @samp{down-} prefix follows the | ||
| 979 | modifier key prefixes such as @samp{C-} and @samp{M-}. | ||
| 980 | |||
| 981 | The function @code{read-key-sequence}, and the Emacs command loop, | ||
| 982 | ignore any button-down events that don't have command bindings. This | ||
| 983 | means that you need not worry about defining button-down events unless | ||
| 984 | you want them to do something. The usual reason to define a button-down | ||
| 985 | event is so that you can track mouse motion (by reading motion events) | ||
| 986 | until the button is released. | ||
| 987 | @ifinfo | ||
| 988 | @xref{Motion Events}. | ||
| 989 | @end ifinfo | ||
| 990 | |||
| 991 | @node Repeat Events | ||
| 992 | @subsection Repeat Events | ||
| 993 | @cindex repeat events | ||
| 994 | @cindex double-click events | ||
| 995 | @cindex triple-click events | ||
| 996 | |||
| 997 | If you press the same mouse button more than once in quick succession | ||
| 998 | without moving the mouse, Emacs uses special @dfn{repeat} mouse events | ||
| 999 | for the second and subsequent presses. | ||
| 1000 | |||
| 1001 | The most common repeat events are @dfn{double-click} events. Emacs | ||
| 1002 | generates a double-click event when you click a button twice; the event | ||
| 1003 | happens when you release the button (as is normal for all click | ||
| 1004 | events). | ||
| 1005 | |||
| 1006 | The event type of a double-click event contains the prefix | ||
| 1007 | @code{double}. Thus, a double click on the second mouse button with | ||
| 1008 | @key{meta} held down comes to the Lisp program as | ||
| 1009 | @code{M-double-mouse-2}. If a double-click event has no binding, the | ||
| 1010 | binding of the corresponding ordinary click event is used to execute | ||
| 1011 | it. Thus, you need not pay attention to the double click feature | ||
| 1012 | unless you really want to. | ||
| 1013 | |||
| 1014 | When the user performs a double click, Emacs generates first an ordinary | ||
| 1015 | click event, and then a double-click event. Therefore, the command | ||
| 1016 | binding of the double click event must be written to assume that the | ||
| 1017 | single-click command has already run. It must produce the desired | ||
| 1018 | results of a double click, starting from the results of a single click. | ||
| 1019 | |||
| 1020 | This means that it is most convenient to give double clicks a meaning | ||
| 1021 | that somehow ``builds on'' the meaning of a single click. This is what | ||
| 1022 | user interface experts recommend that double clicks should do. | ||
| 1023 | |||
| 1024 | If you click a button, then press it down again and start moving the | ||
| 1025 | mouse with the button held down, then you get a @dfn{double-drag} event | ||
| 1026 | when you ultimately release the button. Its event type contains | ||
| 1027 | @samp{double-drag} instead of just @samp{drag}. If a double-drag event | ||
| 1028 | has no binding, Emacs looks for an alternate binding as if the event | ||
| 1029 | were an ordinary click. | ||
| 1030 | |||
| 1031 | Before the double-click or double-drag event, Emacs generates a | ||
| 1032 | @dfn{double-down} event when the button is pressed down for the second | ||
| 1033 | time. Its event type contains @samp{double-down} instead of just | ||
| 1034 | @samp{down}. If a double-down event has no binding, Emacs looks for an | ||
| 1035 | alternate binding as if the event were an ordinary button-down event. | ||
| 1036 | If it finds no binding that way either, the double-down event is ignored. | ||
| 1037 | |||
| 1038 | To summarize, when you click a button and then press it again right | ||
| 1039 | away, Emacs generates a double-down event, followed by either a | ||
| 1040 | double-click or a double-drag. | ||
| 1041 | |||
| 1042 | If you click a button twice and then press it again, all in quick | ||
| 1043 | succession, Emacs generates a @dfn{triple-down} event, followed by | ||
| 1044 | either a @dfn{triple-click} or a @dfn{triple-drag}. The event types of | ||
| 1045 | these events contain @samp{triple} instead of @samp{double}. If any | ||
| 1046 | triple event has no binding, Emacs uses the binding that it would use | ||
| 1047 | for the corresponding double event. | ||
| 1048 | |||
| 1049 | If you click a button three or more times and then press it again, | ||
| 1050 | the events for the presses beyond the third are all triple events. | ||
| 1051 | Emacs does not have quadruple, quintuple, etc. events as separate | ||
| 1052 | event types. However, you can look at the event list to find out | ||
| 1053 | precisely how many times the button was pressed. | ||
| 1054 | |||
| 1055 | @defun event-click-count event | ||
| 1056 | This function returns the number of consecutive button presses that led | ||
| 1057 | up to @var{event}. If @var{event} is a double-down, double-click or | ||
| 1058 | double-drag event, the value is 2. If @var{event} is a triple event, | ||
| 1059 | the value is 3 or greater. If @var{event} is an ordinary mouse event | ||
| 1060 | (not a repeat event), the value is 1. | ||
| 1061 | @end defun | ||
| 1062 | |||
| 1063 | @defvar double-click-time | ||
| 1064 | To count as double- and triple-clicks, mouse clicks must be at the same | ||
| 1065 | location as the first click, and the number of milliseconds between the | ||
| 1066 | first release and the second must be less than the value of | ||
| 1067 | @code{double-click-time}. Setting @code{double-click-time} to | ||
| 1068 | @code{nil} disables multi-click detection entirely. Setting it to | ||
| 1069 | @code{t} removes the time limit; Emacs then detects multi-clicks by | ||
| 1070 | position only. | ||
| 1071 | @end defvar | ||
| 1072 | |||
| 1073 | @node Motion Events | ||
| 1074 | @subsection Motion Events | ||
| 1075 | @cindex motion event | ||
| 1076 | @cindex mouse motion events | ||
| 1077 | |||
| 1078 | Emacs sometimes generates @dfn{mouse motion} events to describe motion | ||
| 1079 | of the mouse without any button activity. Mouse motion events are | ||
| 1080 | represented by lists that look like this: | ||
| 1081 | |||
| 1082 | @example | ||
| 1083 | (mouse-movement | ||
| 1084 | (@var{window} @var{buffer-pos} | ||
| 1085 | (@var{x} . @var{y}) @var{timestamp})) | ||
| 1086 | @end example | ||
| 1087 | |||
| 1088 | The second element of the list describes the current position of the | ||
| 1089 | mouse, just as in a click event (@pxref{Click Events}). | ||
| 1090 | |||
| 1091 | The special form @code{track-mouse} enables generation of motion events | ||
| 1092 | within its body. Outside of @code{track-mouse} forms, Emacs does not | ||
| 1093 | generate events for mere motion of the mouse, and these events do not | ||
| 1094 | appear. | ||
| 1095 | |||
| 1096 | @defspec track-mouse body@dots{} | ||
| 1097 | This special form executes @var{body}, with generation of mouse motion | ||
| 1098 | events enabled. Typically @var{body} would use @code{read-event} | ||
| 1099 | to read the motion events and modify the display accordingly. | ||
| 1100 | |||
| 1101 | When the user releases the button, that generates a click event. | ||
| 1102 | Normally @var{body} should return when it sees the click event, and | ||
| 1103 | discard the event. | ||
| 1104 | @end defspec | ||
| 1105 | |||
| 1106 | @node Focus Events | ||
| 1107 | @subsection Focus Events | ||
| 1108 | @cindex focus event | ||
| 1109 | |||
| 1110 | Window systems provide general ways for the user to control which window | ||
| 1111 | gets keyboard input. This choice of window is called the @dfn{focus}. | ||
| 1112 | When the user does something to switch between Emacs frames, that | ||
| 1113 | generates a @dfn{focus event}. The normal definition of a focus event, | ||
| 1114 | in the global keymap, is to select a new frame within Emacs, as the user | ||
| 1115 | would expect. @xref{Input Focus}. | ||
| 1116 | |||
| 1117 | Focus events are represented in Lisp as lists that look like this: | ||
| 1118 | |||
| 1119 | @example | ||
| 1120 | (switch-frame @var{new-frame}) | ||
| 1121 | @end example | ||
| 1122 | |||
| 1123 | @noindent | ||
| 1124 | where @var{new-frame} is the frame switched to. | ||
| 1125 | |||
| 1126 | In X windows, most window managers are set up so that just moving the | ||
| 1127 | mouse into a window is enough to set the focus there. Emacs appears to | ||
| 1128 | do this, because it changes the cursor to solid in the new frame. | ||
| 1129 | However, there is no need for the Lisp program to know about the focus | ||
| 1130 | change until some other kind of input arrives. So Emacs generates the | ||
| 1131 | focus event only when the user actually types a keyboard key or presses | ||
| 1132 | a mouse button in the new frame; just moving the mouse between frames | ||
| 1133 | does not generate a focus event. | ||
| 1134 | |||
| 1135 | A focus event in the middle of a key sequence would garble the | ||
| 1136 | sequence. So Emacs never generates a focus event in the middle of a key | ||
| 1137 | sequence. If the user changes focus in the middle of a key | ||
| 1138 | sequence---that is, after a prefix key---then Emacs reorders the events | ||
| 1139 | so that the focus event comes either before or after the multi-event key | ||
| 1140 | sequence, and not within it. | ||
| 1141 | |||
| 1142 | @node Event Examples | ||
| 1143 | @subsection Event Examples | ||
| 1144 | |||
| 1145 | If the user presses and releases the left mouse button over the same | ||
| 1146 | location, that generates a sequence of events like this: | ||
| 1147 | |||
| 1148 | @smallexample | ||
| 1149 | (down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320)) | ||
| 1150 | (mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180)) | ||
| 1151 | @end smallexample | ||
| 1152 | |||
| 1153 | Or, while holding the control key down, the user might hold down the | ||
| 1154 | second mouse button, and drag the mouse from one line to the next. | ||
| 1155 | That produces two events, as shown here: | ||
| 1156 | |||
| 1157 | @smallexample | ||
| 1158 | (C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)) | ||
| 1159 | (C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219) | ||
| 1160 | (#<window 18 on NEWS> 3510 (0 . 28) -729648)) | ||
| 1161 | @end smallexample | ||
| 1162 | |||
| 1163 | Or, while holding down the meta and shift keys, the user might press the | ||
| 1164 | second mouse button on the window's mode line, and then drag the mouse | ||
| 1165 | into another window. That produces the following pair of events: | ||
| 1166 | |||
| 1167 | @smallexample | ||
| 1168 | (M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)) | ||
| 1169 | (M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844) | ||
| 1170 | (#<window 20 on carlton-sanskrit.tex> 161 (33 . 3) | ||
| 1171 | -453816)) | ||
| 1172 | @end smallexample | ||
| 1173 | |||
| 1174 | @node Classifying Events | ||
| 1175 | @subsection Classifying Events | ||
| 1176 | @cindex event type | ||
| 1177 | |||
| 1178 | Every event has an @dfn{event type} which classifies the event for key | ||
| 1179 | binding purposes. For a keyboard event, the event type equals the event | ||
| 1180 | value; thus, the event type for a character is the character, and the | ||
| 1181 | event type for a function key symbol is the symbol itself. For events | ||
| 1182 | which are lists, the event type is the symbol in the @sc{car} of the | ||
| 1183 | list. Thus, the event type is always a symbol or a character. | ||
| 1184 | |||
| 1185 | Two events of the same type are equivalent where key bindings are | ||
| 1186 | concerned; thus, they always run the same command. That does not | ||
| 1187 | necessarily mean they do the same things, however, as some commands look | ||
| 1188 | at the whole event to decide what to do. For example, some commands use | ||
| 1189 | the location of a mouse event to decide what text to act on. | ||
| 1190 | |||
| 1191 | Sometimes broader classifications of events are useful. For example, | ||
| 1192 | you might want to ask whether an event involved the @key{META} key, | ||
| 1193 | regardless of which other key or mouse button was used. | ||
| 1194 | |||
| 1195 | The functions @code{event-modifiers} and @code{event-basic-type} are | ||
| 1196 | provided to get such information conveniently. | ||
| 1197 | |||
| 1198 | @defun event-modifiers event | ||
| 1199 | This function returns a list of the modifiers that @var{event} has. | ||
| 1200 | The modifiers are symbols; they include @code{shift}, @code{control}, | ||
| 1201 | @code{meta}, @code{alt}, @code{hyper} and @code{super}. In addition, | ||
| 1202 | the property of a mouse event symbol always has one of @code{click}, | ||
| 1203 | @code{drag}, and @code{down} among the modifiers. For example: | ||
| 1204 | |||
| 1205 | @example | ||
| 1206 | (event-modifiers ?a) | ||
| 1207 | @result{} nil | ||
| 1208 | (event-modifiers ?\C-a) | ||
| 1209 | @result{} (control) | ||
| 1210 | (event-modifiers ?\C-%) | ||
| 1211 | @result{} (control) | ||
| 1212 | (event-modifiers ?\C-\S-a) | ||
| 1213 | @result{} (control shift) | ||
| 1214 | (event-modifiers 'f5) | ||
| 1215 | @result{} nil | ||
| 1216 | (event-modifiers 's-f5) | ||
| 1217 | @result{} (super) | ||
| 1218 | (event-modifiers 'M-S-f5) | ||
| 1219 | @result{} (meta shift) | ||
| 1220 | (event-modifiers 'mouse-1) | ||
| 1221 | @result{} (click) | ||
| 1222 | (event-modifiers 'down-mouse-1) | ||
| 1223 | @result{} (down) | ||
| 1224 | @end example | ||
| 1225 | |||
| 1226 | The modifiers list for a click event explicitly contains @code{click}, | ||
| 1227 | but the event symbol name itself does not contain @samp{click}. | ||
| 1228 | @end defun | ||
| 1229 | |||
| 1230 | @defun event-basic-type event | ||
| 1231 | This function returns the key or mouse button that @var{event} | ||
| 1232 | describes, with all modifiers removed. For example: | ||
| 1233 | |||
| 1234 | @example | ||
| 1235 | (event-basic-type ?a) | ||
| 1236 | @result{} 97 | ||
| 1237 | (event-basic-type ?A) | ||
| 1238 | @result{} 97 | ||
| 1239 | (event-basic-type ?\C-a) | ||
| 1240 | @result{} 97 | ||
| 1241 | (event-basic-type ?\C-\S-a) | ||
| 1242 | @result{} 97 | ||
| 1243 | (event-basic-type 'f5) | ||
| 1244 | @result{} f5 | ||
| 1245 | (event-basic-type 's-f5) | ||
| 1246 | @result{} f5 | ||
| 1247 | (event-basic-type 'M-S-f5) | ||
| 1248 | @result{} f5 | ||
| 1249 | (event-basic-type 'down-mouse-1) | ||
| 1250 | @result{} mouse-1 | ||
| 1251 | @end example | ||
| 1252 | @end defun | ||
| 1253 | |||
| 1254 | @defun mouse-movement-p object | ||
| 1255 | This function returns non-@code{nil} if @var{object} is a mouse movement | ||
| 1256 | event. | ||
| 1257 | @end defun | ||
| 1258 | |||
| 1259 | @node Accessing Events | ||
| 1260 | @subsection Accessing Events | ||
| 1261 | |||
| 1262 | This section describes convenient functions for accessing the data in | ||
| 1263 | an event which is a list. | ||
| 1264 | |||
| 1265 | The following functions return the starting or ending position of a | ||
| 1266 | mouse-button event. The position is a list of this form: | ||
| 1267 | |||
| 1268 | @smallexample | ||
| 1269 | (@var{window} @var{buffer-position} (@var{col} . @var{row}) @var{timestamp}) | ||
| 1270 | @end smallexample | ||
| 1271 | |||
| 1272 | @defun event-start event | ||
| 1273 | This returns the starting position of @var{event}. | ||
| 1274 | |||
| 1275 | If @var{event} is a click or button-down event, this returns the | ||
| 1276 | location of the event. If @var{event} is a drag event, this returns the | ||
| 1277 | drag's starting position. | ||
| 1278 | @end defun | ||
| 1279 | |||
| 1280 | @defun event-end event | ||
| 1281 | This returns the ending position of @var{event}. | ||
| 1282 | |||
| 1283 | If @var{event} is a drag event, this returns the position where the user | ||
| 1284 | released the mouse button. If @var{event} is a click or button-down | ||
| 1285 | event, the value is actually the starting position, which is the only | ||
| 1286 | position such events have. | ||
| 1287 | @end defun | ||
| 1288 | |||
| 1289 | These four functions take a position-list as described above, and | ||
| 1290 | return various parts of it. | ||
| 1291 | |||
| 1292 | @defun posn-window position | ||
| 1293 | Return the window that @var{position} is in. | ||
| 1294 | @end defun | ||
| 1295 | |||
| 1296 | @defun posn-point position | ||
| 1297 | Return the buffer location in @var{position}. | ||
| 1298 | @end defun | ||
| 1299 | |||
| 1300 | @defun posn-x-y position | ||
| 1301 | Return the pixel-based x and y coordinates column in @var{position}, as | ||
| 1302 | a cons cell @code{(@var{x} . @var{y})}. | ||
| 1303 | @end defun | ||
| 1304 | |||
| 1305 | @defun posn-col-row position | ||
| 1306 | Return the row and column (in units of characters) in @var{position}, as | ||
| 1307 | a cons cell @code{(@var{col} . @var{row})}. These are computed from the | ||
| 1308 | @var{x} and @var{y} values actually found in @var{position}. | ||
| 1309 | @end defun | ||
| 1310 | |||
| 1311 | @defun posn-timestamp position | ||
| 1312 | Return the timestamp of @var{position}. | ||
| 1313 | @end defun | ||
| 1314 | |||
| 1315 | @defun scroll-bar-scale ratio total | ||
| 1316 | This function multiples (in effect) @var{ratio} by @var{total}, | ||
| 1317 | rounding the result to an integer. @var{ratio} is not a number, | ||
| 1318 | but rather a pair @code{(@var{num} . @var{denom})}. | ||
| 1319 | |||
| 1320 | This is handy for scaling a position on a scroll bar into a buffer | ||
| 1321 | position. Here's how to do that: | ||
| 1322 | |||
| 1323 | @example | ||
| 1324 | (+ (point-min) | ||
| 1325 | (scroll-bar-scale | ||
| 1326 | (posn-col-row (event-start event)) | ||
| 1327 | (- (point-max) (point-min)))) | ||
| 1328 | @end example | ||
| 1329 | @end defun | ||
| 1330 | |||
| 1331 | @node Strings of Events | ||
| 1332 | @subsection Putting Keyboard Events in Strings | ||
| 1333 | |||
| 1334 | In most of the places where strings are used, we conceptualize the | ||
| 1335 | string as containing text characters---the same kind of characters found | ||
| 1336 | in buffers or files. Occasionally Lisp programs use strings which | ||
| 1337 | conceptually contain keyboard characters; for example, they may be key | ||
| 1338 | sequences or keyboard macro definitions. There are special rules for | ||
| 1339 | how to put keyboard characters into a string, because they are not | ||
| 1340 | limited to the range of 0 to 255 as text characters are. | ||
| 1341 | |||
| 1342 | A keyboard character typed using the @key{META} key is called a | ||
| 1343 | @dfn{meta character}. The numeric code for such an event includes the | ||
| 1344 | 2**23 bit; it does not even come close to fitting in a string. However, | ||
| 1345 | earlier Emacs versions used a different representation for these | ||
| 1346 | characters, which gave them codes in the range of 128 to 255. That did | ||
| 1347 | fit in a string, and many Lisp programs contain string constants that | ||
| 1348 | use @samp{\M-} to express meta characters, especially as the argument to | ||
| 1349 | @code{define-key} and similar functions. | ||
| 1350 | |||
| 1351 | We provide backward compatibility to run those programs with special | ||
| 1352 | rules for how to put a keyboard character event in a string. Here are | ||
| 1353 | the rules: | ||
| 1354 | |||
| 1355 | @itemize @bullet | ||
| 1356 | @item | ||
| 1357 | If the keyboard event value is in the range of 0 to 127, it can go in the | ||
| 1358 | string unchanged. | ||
| 1359 | |||
| 1360 | @item | ||
| 1361 | The meta variants of those events, with codes in the range of 2**23 to | ||
| 1362 | 2**23+127, can also go in the string, but you must change their numeric | ||
| 1363 | values. You must set the 2**7 bit instead of the 2**23 bit, resulting | ||
| 1364 | in a value between 128 and 255. | ||
| 1365 | |||
| 1366 | @item | ||
| 1367 | Other keyboard character events cannot fit in a string. This includes | ||
| 1368 | keyboard events in the range of 128 to 255. | ||
| 1369 | @end itemize | ||
| 1370 | |||
| 1371 | Functions such as @code{read-key-sequence} that can construct strings | ||
| 1372 | containing events follow these rules. | ||
| 1373 | |||
| 1374 | When you use the read syntax @samp{\M-} in a string, it produces a | ||
| 1375 | code in the range of 128 to 255---the same code that you get if you | ||
| 1376 | modify the corresponding keyboard event to put it in the string. Thus, | ||
| 1377 | meta events in strings work consistently regardless of how they get into | ||
| 1378 | the strings. | ||
| 1379 | |||
| 1380 | New programs can avoid dealing with these rules by using vectors | ||
| 1381 | instead of strings for key sequences when there is any possibility that | ||
| 1382 | these issues might arise. | ||
| 1383 | |||
| 1384 | The reason we changed the representation of meta characters as | ||
| 1385 | keyboard events is to make room for basic character codes beyond 127, | ||
| 1386 | and support meta variants of such larger character codes. | ||
| 1387 | |||
| 1388 | @node Reading Input | ||
| 1389 | @section Reading Input | ||
| 1390 | |||
| 1391 | The editor command loop reads keyboard input using the function | ||
| 1392 | @code{read-key-sequence}, which uses @code{read-event}. These and other | ||
| 1393 | functions for keyboard input are also available for use in Lisp | ||
| 1394 | programs. See also @code{momentary-string-display} in @ref{Temporary | ||
| 1395 | Displays}, and @code{sit-for} in @ref{Waiting}. @xref{Terminal Input}, | ||
| 1396 | for functions and variables for controlling terminal input modes and | ||
| 1397 | debugging terminal input. | ||
| 1398 | |||
| 1399 | For higher-level input facilities, see @ref{Minibuffers}. | ||
| 1400 | |||
| 1401 | @menu | ||
| 1402 | * Key Sequence Input:: How to read one key sequence. | ||
| 1403 | * Reading One Event:: How to read just one event. | ||
| 1404 | * Quoted Character Input:: Asking the user to specify a character. | ||
| 1405 | * Peeking and Discarding:: How to reread or throw away input events. | ||
| 1406 | @end menu | ||
| 1407 | |||
| 1408 | @node Key Sequence Input | ||
| 1409 | @subsection Key Sequence Input | ||
| 1410 | @cindex key sequence input | ||
| 1411 | |||
| 1412 | The command loop reads input a key sequence at a time, by calling | ||
| 1413 | @code{read-key-sequence}. Lisp programs can also call this function; | ||
| 1414 | for example, @code{describe-key} uses it to read the key to describe. | ||
| 1415 | |||
| 1416 | @defun read-key-sequence prompt | ||
| 1417 | @cindex key sequence | ||
| 1418 | This function reads a key sequence and returns it as a string or | ||
| 1419 | vector. It keeps reading events until it has accumulated a full key | ||
| 1420 | sequence; that is, enough to specify a non-prefix command using the | ||
| 1421 | currently active keymaps. | ||
| 1422 | |||
| 1423 | If the events are all characters and all can fit in a string, then | ||
| 1424 | @code{read-key-sequence} returns a string (@pxref{Strings of Events}). | ||
| 1425 | Otherwise, it returns a vector, since a vector can hold all kinds of | ||
| 1426 | events---characters, symbols, and lists. The elements of the string or | ||
| 1427 | vector are the events in the key sequence. | ||
| 1428 | |||
| 1429 | Quitting is suppressed inside @code{read-key-sequence}. In other words, | ||
| 1430 | a @kbd{C-g} typed while reading with this function is treated like any | ||
| 1431 | other character, and does not set @code{quit-flag}. @xref{Quitting}. | ||
| 1432 | |||
| 1433 | The argument @var{prompt} is either a string to be displayed in the echo | ||
| 1434 | area as a prompt, or @code{nil}, meaning not to display a prompt. | ||
| 1435 | |||
| 1436 | In the example below, the prompt @samp{?} is displayed in the echo area, | ||
| 1437 | and the user types @kbd{C-x C-f}. | ||
| 1438 | |||
| 1439 | @example | ||
| 1440 | (read-key-sequence "?") | ||
| 1441 | |||
| 1442 | @group | ||
| 1443 | ---------- Echo Area ---------- | ||
| 1444 | ?@kbd{C-x C-f} | ||
| 1445 | ---------- Echo Area ---------- | ||
| 1446 | |||
| 1447 | @result{} "^X^F" | ||
| 1448 | @end group | ||
| 1449 | @end example | ||
| 1450 | @end defun | ||
| 1451 | |||
| 1452 | @defvar num-input-keys | ||
| 1453 | @c Emacs 19 feature | ||
| 1454 | This variable's value is the number of key sequences processed so far in | ||
| 1455 | this Emacs session. This includes key sequences read from the terminal | ||
| 1456 | and key sequences read from keyboard macros being executed. | ||
| 1457 | @end defvar | ||
| 1458 | |||
| 1459 | @cindex upper case key sequence | ||
| 1460 | @cindex downcasing in @code{lookup-key} | ||
| 1461 | If an input character is an upper case letter and has no key binding, | ||
| 1462 | but the lower case equivalent has one, then @code{read-key-sequence} | ||
| 1463 | converts the character to lower case. Note that @code{lookup-key} does | ||
| 1464 | not perform case conversion in this way. | ||
| 1465 | |||
| 1466 | The function @code{read-key-sequence} also transforms some mouse events. | ||
| 1467 | It converts unbound drag events into click events, and discards unbound | ||
| 1468 | button-down events entirely. It also reshuffles focus events so that they | ||
| 1469 | never appear in a key sequence with any other events. | ||
| 1470 | |||
| 1471 | When mouse events occur in special parts of a window, such as a mode | ||
| 1472 | line or a scroll bar, the event itself shows nothing special---only the | ||
| 1473 | symbol that would normally represent that mouse button and modifier | ||
| 1474 | keys. The information about the screen region is kept elsewhere in the | ||
| 1475 | event---in the coordinates. But @code{read-key-sequence} translates | ||
| 1476 | this information into imaginary prefix keys, all of which are symbols: | ||
| 1477 | @code{mode-line}, @code{vertical-line}, @code{horizontal-scroll-bar} and | ||
| 1478 | @code{vertical-scroll-bar}. | ||
| 1479 | |||
| 1480 | For example, if you call @code{read-key-sequence} and then click the | ||
| 1481 | mouse on the window's mode line, this is what happens: | ||
| 1482 | |||
| 1483 | @smallexample | ||
| 1484 | (read-key-sequence "Click on the mode line: ") | ||
| 1485 | @result{} [mode-line | ||
| 1486 | (mouse-1 | ||
| 1487 | (#<window 6 on NEWS> mode-line | ||
| 1488 | (40 . 63) 5959987))] | ||
| 1489 | @end smallexample | ||
| 1490 | |||
| 1491 | You can define meanings for mouse clicks in special window regions by | ||
| 1492 | defining key sequences using these imaginary prefix keys. | ||
| 1493 | |||
| 1494 | @node Reading One Event | ||
| 1495 | @subsection Reading One Event | ||
| 1496 | |||
| 1497 | The lowest level functions for command input are those which read a | ||
| 1498 | single event. | ||
| 1499 | |||
| 1500 | @defun read-event | ||
| 1501 | This function reads and returns the next event of command input, waiting | ||
| 1502 | if necessary until an event is available. Events can come directly from | ||
| 1503 | the user or from a keyboard macro. | ||
| 1504 | |||
| 1505 | The function @code{read-event} does not display any message to indicate | ||
| 1506 | it is waiting for input; use @code{message} first, if you wish to | ||
| 1507 | display one. If you have not displayed a message, @code{read-event} | ||
| 1508 | does @dfn{prompting}: it displays descriptions of the events that led to | ||
| 1509 | or were read by the current command. @xref{The Echo Area}. | ||
| 1510 | |||
| 1511 | If @code{cursor-in-echo-area} is non-@code{nil}, then @code{read-event} | ||
| 1512 | moves the cursor temporarily to the echo area, to the end of any message | ||
| 1513 | displayed there. Otherwise @code{read-event} does not move the cursor. | ||
| 1514 | @end defun | ||
| 1515 | |||
| 1516 | Here is what happens if you call @code{read-event} and then press the | ||
| 1517 | right-arrow function key: | ||
| 1518 | |||
| 1519 | @example | ||
| 1520 | @group | ||
| 1521 | (read-event) | ||
| 1522 | @result{} right | ||
| 1523 | @end group | ||
| 1524 | @end example | ||
| 1525 | |||
| 1526 | @defun read-char | ||
| 1527 | This function reads and returns a character of command input. It | ||
| 1528 | discards any events that are not characters until it gets a character. | ||
| 1529 | |||
| 1530 | In the first example, the user types @kbd{1} (which is @sc{ASCII} code | ||
| 1531 | 49). The second example shows a keyboard macro definition that calls | ||
| 1532 | @code{read-char} from the minibuffer. @code{read-char} reads the | ||
| 1533 | keyboard macro's very next character, which is @kbd{1}. The value of | ||
| 1534 | this function is displayed in the echo area by the command | ||
| 1535 | @code{eval-expression}. | ||
| 1536 | |||
| 1537 | @example | ||
| 1538 | @group | ||
| 1539 | (read-char) | ||
| 1540 | @result{} 49 | ||
| 1541 | @end group | ||
| 1542 | |||
| 1543 | @group | ||
| 1544 | (symbol-function 'foo) | ||
| 1545 | @result{} "^[^[(read-char)^M1" | ||
| 1546 | @end group | ||
| 1547 | @group | ||
| 1548 | (execute-kbd-macro foo) | ||
| 1549 | @print{} 49 | ||
| 1550 | @result{} nil | ||
| 1551 | @end group | ||
| 1552 | @end example | ||
| 1553 | @end defun | ||
| 1554 | |||
| 1555 | @node Quoted Character Input | ||
| 1556 | @subsection Quoted Character Input | ||
| 1557 | @cindex quoted character input | ||
| 1558 | |||
| 1559 | You can use the function @code{read-quoted-char} when you want the user | ||
| 1560 | to specify a character, and allow the user to specify a control or meta | ||
| 1561 | character conveniently with quoting or as an octal character code. The | ||
| 1562 | command @code{quoted-insert} calls this function. | ||
| 1563 | |||
| 1564 | @defun read-quoted-char &optional prompt | ||
| 1565 | @cindex octal character input | ||
| 1566 | @cindex control characters, reading | ||
| 1567 | @cindex nonprinting characters, reading | ||
| 1568 | This function is like @code{read-char}, except that if the first | ||
| 1569 | character read is an octal digit (0-7), it reads up to two more octal digits | ||
| 1570 | (but stopping if a non-octal digit is found) and returns the | ||
| 1571 | character represented by those digits as an octal number. | ||
| 1572 | |||
| 1573 | Quitting is suppressed when the first character is read, so that the | ||
| 1574 | user can enter a @kbd{C-g}. @xref{Quitting}. | ||
| 1575 | |||
| 1576 | If @var{prompt} is supplied, it specifies a string for prompting the | ||
| 1577 | user. The prompt string is always printed in the echo area and followed | ||
| 1578 | by a single @samp{-}. | ||
| 1579 | |||
| 1580 | In the following example, the user types in the octal number 177 (which | ||
| 1581 | is 127 in decimal). | ||
| 1582 | |||
| 1583 | @example | ||
| 1584 | (read-quoted-char "What character") | ||
| 1585 | |||
| 1586 | @group | ||
| 1587 | ---------- Echo Area ---------- | ||
| 1588 | What character-@kbd{177} | ||
| 1589 | ---------- Echo Area ---------- | ||
| 1590 | |||
| 1591 | @result{} 127 | ||
| 1592 | @end group | ||
| 1593 | @end example | ||
| 1594 | @end defun | ||
| 1595 | |||
| 1596 | @need 3000 | ||
| 1597 | |||
| 1598 | @node Peeking and Discarding | ||
| 1599 | @subsection Peeking and Discarding | ||
| 1600 | |||
| 1601 | @defvar unread-command-events | ||
| 1602 | @cindex next input | ||
| 1603 | @cindex peeking at input | ||
| 1604 | This variable holds a list of events waiting to be read as command | ||
| 1605 | input. The events are used in the order they appear in the list. | ||
| 1606 | |||
| 1607 | The variable is used because in some cases a function reads a event and | ||
| 1608 | then decides not to use it. Storing the event in this variable causes | ||
| 1609 | it to be processed normally by the command loop or when the functions to | ||
| 1610 | read command input are called. | ||
| 1611 | |||
| 1612 | @cindex prefix argument unreading | ||
| 1613 | For example, the function that implements numeric prefix arguments reads | ||
| 1614 | any number of digits. When it finds a non-digit event, it must unread | ||
| 1615 | the event so that it can be read normally by the command loop. | ||
| 1616 | Likewise, incremental search uses this feature to unread events it does | ||
| 1617 | not recognize. | ||
| 1618 | @end defvar | ||
| 1619 | |||
| 1620 | @defvar unread-command-char | ||
| 1621 | This variable holds a character to be read as command input. | ||
| 1622 | A value of -1 means ``empty''. | ||
| 1623 | |||
| 1624 | This variable is pretty much obsolete now that you can use | ||
| 1625 | @code{unread-command-events} instead; it exists only to support programs | ||
| 1626 | written for Emacs versions 18 and earlier. | ||
| 1627 | @end defvar | ||
| 1628 | |||
| 1629 | @defun listify-key-sequence key | ||
| 1630 | This function converts the string or vector @var{key} to a list of | ||
| 1631 | events which you can put in @code{unread-command-events}. Converting a | ||
| 1632 | vector is simple, but converting a string is tricky because of the | ||
| 1633 | special representation used for meta characters in a string | ||
| 1634 | (@pxref{Strings of Events}). | ||
| 1635 | @end defun | ||
| 1636 | |||
| 1637 | @defun input-pending-p | ||
| 1638 | @cindex waiting for command key input | ||
| 1639 | This function determines whether any command input is currently | ||
| 1640 | available to be read. It returns immediately, with value @code{t} if | ||
| 1641 | there is input, @code{nil} otherwise. On rare occasions it may return | ||
| 1642 | @code{t} when no input is available. | ||
| 1643 | @end defun | ||
| 1644 | |||
| 1645 | @defvar last-input-event | ||
| 1646 | @defvarx last-input-char | ||
| 1647 | This variable records the last terminal input event read, whether | ||
| 1648 | as part of a command or explicitly by a Lisp program. | ||
| 1649 | |||
| 1650 | In the example below, a character is read (the character @kbd{1}, | ||
| 1651 | @sc{ASCII} code 49). It becomes the value of @code{last-input-char}, | ||
| 1652 | while @kbd{C-e} (from the @kbd{C-x C-e} command used to evaluate this | ||
| 1653 | expression) remains the value of @code{last-command-char}. | ||
| 1654 | |||
| 1655 | @example | ||
| 1656 | @group | ||
| 1657 | (progn (print (read-char)) | ||
| 1658 | (print last-command-char) | ||
| 1659 | last-input-char) | ||
| 1660 | @print{} 49 | ||
| 1661 | @print{} 5 | ||
| 1662 | @result{} 49 | ||
| 1663 | @end group | ||
| 1664 | @end example | ||
| 1665 | |||
| 1666 | The alias @code{last-input-char} exists for compatibility with | ||
| 1667 | Emacs version 18. | ||
| 1668 | @end defvar | ||
| 1669 | |||
| 1670 | @defun discard-input | ||
| 1671 | @cindex flush input | ||
| 1672 | @cindex discard input | ||
| 1673 | @cindex terminate keyboard macro | ||
| 1674 | This function discards the contents of the terminal input buffer and | ||
| 1675 | cancels any keyboard macro that might be in the process of definition. | ||
| 1676 | It returns @code{nil}. | ||
| 1677 | |||
| 1678 | In the following example, the user may type a number of characters right | ||
| 1679 | after starting the evaluation of the form. After the @code{sleep-for} | ||
| 1680 | finishes sleeping, any characters that have been typed are discarded. | ||
| 1681 | |||
| 1682 | @example | ||
| 1683 | (progn (sleep-for 2) | ||
| 1684 | (discard-input)) | ||
| 1685 | @result{} nil | ||
| 1686 | @end example | ||
| 1687 | @end defun | ||
| 1688 | |||
| 1689 | @node Waiting | ||
| 1690 | @section Waiting for Elapsed Time or Input | ||
| 1691 | @cindex pausing | ||
| 1692 | @cindex waiting | ||
| 1693 | |||
| 1694 | The waiting commands are designed to make Emacs wait for a certain | ||
| 1695 | amount of time to pass or until there is input. For example, you may | ||
| 1696 | wish to pause in the middle of a computation to allow the user time to | ||
| 1697 | view the display. @code{sit-for} pauses and updates the screen, and | ||
| 1698 | returns immediately if input comes in, while @code{sleep-for} pauses | ||
| 1699 | without updating the screen. | ||
| 1700 | |||
| 1701 | @defun sit-for seconds &optional millisec nodisp | ||
| 1702 | This function performs redisplay (provided there is no pending input | ||
| 1703 | from the user), then waits @var{seconds} seconds, or until input is | ||
| 1704 | available. The result is @code{t} if @code{sit-for} waited the full | ||
| 1705 | time with no input arriving (see @code{input-pending-p} in @ref{Peeking | ||
| 1706 | and Discarding}). Otherwise, the value is @code{nil}. | ||
| 1707 | |||
| 1708 | @c Emacs 19 feature ??? maybe not working yet | ||
| 1709 | The optional argument @var{millisec} specifies an additional waiting | ||
| 1710 | period measured in milliseconds. This adds to the period specified by | ||
| 1711 | @var{seconds}. Not all operating systems support waiting periods other | ||
| 1712 | than multiples of a second; on those that do not, you get an error if | ||
| 1713 | you specify nonzero @var{millisec}. | ||
| 1714 | |||
| 1715 | @cindex forcing redisplay | ||
| 1716 | Redisplay is always preempted if input arrives, and does not happen at | ||
| 1717 | all if input is available before it starts. Thus, there is no way to | ||
| 1718 | force screen updating if there is pending input; however, if there is no | ||
| 1719 | input pending, you can force an update with no delay by using | ||
| 1720 | @code{(sit-for 0)}. | ||
| 1721 | |||
| 1722 | If @var{nodisp} is non-@code{nil}, then @code{sit-for} does not | ||
| 1723 | redisplay, but it still returns as soon as input is available (or when | ||
| 1724 | the timeout elapses). | ||
| 1725 | |||
| 1726 | The usual purpose of @code{sit-for} is to give the user time to read | ||
| 1727 | text that you display. | ||
| 1728 | @end defun | ||
| 1729 | |||
| 1730 | @defun sleep-for seconds &optional millisec | ||
| 1731 | This function simply pauses for @var{seconds} seconds without updating | ||
| 1732 | the display. It pays no attention to available input. It returns | ||
| 1733 | @code{nil}. | ||
| 1734 | |||
| 1735 | @c Emacs 19 feature ??? maybe not working yet | ||
| 1736 | The optional argument @var{millisec} specifies an additional waiting | ||
| 1737 | period measured in milliseconds. This adds to the period specified by | ||
| 1738 | @var{seconds}. Not all operating systems support waiting periods other | ||
| 1739 | than multiples of a second; on those that do not, you get an error if | ||
| 1740 | you specify nonzero @var{millisec}. | ||
| 1741 | |||
| 1742 | Use @code{sleep-for} when you wish to guarantee a delay. | ||
| 1743 | @end defun | ||
| 1744 | |||
| 1745 | @xref{Time of Day}, for functions to get the current time. | ||
| 1746 | |||
| 1747 | @node Quitting | ||
| 1748 | @section Quitting | ||
| 1749 | @cindex @kbd{C-g} | ||
| 1750 | @cindex quitting | ||
| 1751 | |||
| 1752 | Typing @kbd{C-g} while the command loop has run a Lisp function causes | ||
| 1753 | Emacs to @dfn{quit} whatever it is doing. This means that control | ||
| 1754 | returns to the innermost active command loop. | ||
| 1755 | |||
| 1756 | Typing @kbd{C-g} while the command loop is waiting for keyboard input | ||
| 1757 | does not cause a quit; it acts as an ordinary input character. In the | ||
| 1758 | simplest case, you cannot tell the difference, because @kbd{C-g} | ||
| 1759 | normally runs the command @code{keyboard-quit}, whose effect is to quit. | ||
| 1760 | However, when @kbd{C-g} follows a prefix key, the result is an undefined | ||
| 1761 | key. The effect is to cancel the prefix key as well as any prefix | ||
| 1762 | argument. | ||
| 1763 | |||
| 1764 | In the minibuffer, @kbd{C-g} has a different definition: it aborts out | ||
| 1765 | of the minibuffer. This means, in effect, that it exits the minibuffer | ||
| 1766 | and then quits. (Simply quitting would return to the command loop | ||
| 1767 | @emph{within} the minibuffer.) The reason why @kbd{C-g} does not quit | ||
| 1768 | directly when the command reader is reading input is so that its meaning | ||
| 1769 | can be redefined in the minibuffer in this way. @kbd{C-g} following a | ||
| 1770 | prefix key is not redefined in the minibuffer, and it has its normal | ||
| 1771 | effect of canceling the prefix key and prefix argument. This too | ||
| 1772 | would not be possible if @kbd{C-g} quit directly. | ||
| 1773 | |||
| 1774 | @kbd{C-g} causes a quit by setting the variable @code{quit-flag} to a | ||
| 1775 | non-@code{nil} value. Emacs checks this variable at appropriate times | ||
| 1776 | and quits if it is not @code{nil}. Setting @code{quit-flag} | ||
| 1777 | non-@code{nil} in any way thus causes a quit. | ||
| 1778 | |||
| 1779 | At the level of C code, quits cannot happen just anywhere; only at the | ||
| 1780 | special places which check @code{quit-flag}. The reason for this is | ||
| 1781 | that quitting at other places might leave an inconsistency in Emacs's | ||
| 1782 | internal state. Because quitting is delayed until a safe place, quitting | ||
| 1783 | cannot make Emacs crash. | ||
| 1784 | |||
| 1785 | Certain functions such as @code{read-key-sequence} or | ||
| 1786 | @code{read-quoted-char} prevent quitting entirely even though they wait | ||
| 1787 | for input. Instead of quitting, @kbd{C-g} serves as the requested | ||
| 1788 | input. In the case of @code{read-key-sequence}, this serves to bring | ||
| 1789 | about the special behavior of @kbd{C-g} in the command loop. In the | ||
| 1790 | case of @code{read-quoted-char}, this is so that @kbd{C-q} can be used | ||
| 1791 | to quote a @kbd{C-g}. | ||
| 1792 | |||
| 1793 | You can prevent quitting for a portion of a Lisp function by binding | ||
| 1794 | the variable @code{inhibit-quit} to a non-@code{nil} value. Then, | ||
| 1795 | although @kbd{C-g} still sets @code{quit-flag} to @code{t} as usual, the | ||
| 1796 | usual result of this---a quit---is prevented. Eventually, | ||
| 1797 | @code{inhibit-quit} will become @code{nil} again, such as when its | ||
| 1798 | binding is unwound at the end of a @code{let} form. At that time, if | ||
| 1799 | @code{quit-flag} is still non-@code{nil}, the requested quit happens | ||
| 1800 | immediately. This behavior is ideal for a ``critical section'', where | ||
| 1801 | you wish to make sure that quitting does not happen within that part of | ||
| 1802 | the program. | ||
| 1803 | |||
| 1804 | @cindex @code{read-quoted-char} quitting | ||
| 1805 | In some functions (such as @code{read-quoted-char}), @kbd{C-g} is | ||
| 1806 | handled in a special way which does not involve quitting. This is done | ||
| 1807 | by reading the input with @code{inhibit-quit} bound to @code{t} and | ||
| 1808 | setting @code{quit-flag} to @code{nil} before @code{inhibit-quit} | ||
| 1809 | becomes @code{nil} again. This excerpt from the definition of | ||
| 1810 | @code{read-quoted-char} shows how this is done; it also shows that | ||
| 1811 | normal quitting is permitted after the first character of input. | ||
| 1812 | |||
| 1813 | @example | ||
| 1814 | (defun read-quoted-char (&optional prompt) | ||
| 1815 | "@dots{}@var{documentation}@dots{}" | ||
| 1816 | (let ((count 0) (code 0) char) | ||
| 1817 | (while (< count 3) | ||
| 1818 | (let ((inhibit-quit (zerop count)) | ||
| 1819 | (help-form nil)) | ||
| 1820 | (and prompt (message "%s-" prompt)) | ||
| 1821 | (setq char (read-char)) | ||
| 1822 | (if inhibit-quit (setq quit-flag nil))) | ||
| 1823 | @dots{}) | ||
| 1824 | (logand 255 code))) | ||
| 1825 | @end example | ||
| 1826 | |||
| 1827 | @defvar quit-flag | ||
| 1828 | If this variable is non-@code{nil}, then Emacs quits immediately, | ||
| 1829 | unless @code{inhibit-quit} is non-@code{nil}. Typing @kbd{C-g} sets | ||
| 1830 | @code{quit-flag} non-@code{nil}, regardless of @code{inhibit-quit}. | ||
| 1831 | @end defvar | ||
| 1832 | |||
| 1833 | @defvar inhibit-quit | ||
| 1834 | This variable determines whether Emacs should quit when @code{quit-flag} | ||
| 1835 | is set to a value other than @code{nil}. If @code{inhibit-quit} is | ||
| 1836 | non-@code{nil}, then @code{quit-flag} has no special effect. | ||
| 1837 | @end defvar | ||
| 1838 | |||
| 1839 | @deffn Command keyboard-quit | ||
| 1840 | This function signals the @code{quit} condition with @code{(signal 'quit | ||
| 1841 | nil)}. This is the same thing that quitting does. (See @code{signal} | ||
| 1842 | in @ref{Errors}.) | ||
| 1843 | @end deffn | ||
| 1844 | |||
| 1845 | You can specify a character other than @kbd{C-g} to use for quitting. | ||
| 1846 | See the function @code{set-input-mode} in @ref{Terminal Input}. | ||
| 1847 | |||
| 1848 | @node Prefix Command Arguments | ||
| 1849 | @section Prefix Command Arguments | ||
| 1850 | @cindex prefix argument | ||
| 1851 | @cindex raw prefix argument | ||
| 1852 | @cindex numeric prefix argument | ||
| 1853 | |||
| 1854 | Most Emacs commands can use a @dfn{prefix argument}, a number | ||
| 1855 | specified before the command itself. (Don't confuse prefix arguments | ||
| 1856 | with prefix keys.) The prefix argument is represented by a value that | ||
| 1857 | is always available (though it may be @code{nil}, meaning there is no | ||
| 1858 | prefix argument). Each command may use the prefix argument or ignore | ||
| 1859 | it. | ||
| 1860 | |||
| 1861 | There are two representations of the prefix argument: @dfn{raw} and | ||
| 1862 | @dfn{numeric}. The editor command loop uses the raw representation | ||
| 1863 | internally, and so do the Lisp variables that store the information, but | ||
| 1864 | commands can request either representation. | ||
| 1865 | |||
| 1866 | Here are the possible values of a raw prefix argument: | ||
| 1867 | |||
| 1868 | @itemize @bullet | ||
| 1869 | @item | ||
| 1870 | @code{nil}, meaning there is no prefix argument. Its numeric value is | ||
| 1871 | 1, but numerous commands make a distinction between @code{nil} and the | ||
| 1872 | integer 1. | ||
| 1873 | |||
| 1874 | @item | ||
| 1875 | An integer, which stands for itself. | ||
| 1876 | |||
| 1877 | @item | ||
| 1878 | A list of one element, which is an integer. This form of prefix | ||
| 1879 | argument results from one or a succession of @kbd{C-u}'s with no | ||
| 1880 | digits. The numeric value is the integer in the list, but some | ||
| 1881 | commands make a distinction between such a list and an integer alone. | ||
| 1882 | |||
| 1883 | @item | ||
| 1884 | The symbol @code{-}. This indicates that @kbd{M--} or @kbd{C-u -} was | ||
| 1885 | typed, without following digits. The equivalent numeric value is | ||
| 1886 | @minus{}1, but some commands make a distinction between the integer | ||
| 1887 | @minus{}1 and the symbol @code{-}. | ||
| 1888 | @end itemize | ||
| 1889 | |||
| 1890 | The various possibilities may be illustrated by calling the following | ||
| 1891 | function with various prefixes: | ||
| 1892 | |||
| 1893 | @example | ||
| 1894 | @group | ||
| 1895 | (defun display-prefix (arg) | ||
| 1896 | "Display the value of the raw prefix arg." | ||
| 1897 | (interactive "P") | ||
| 1898 | (message "%s" arg)) | ||
| 1899 | @end group | ||
| 1900 | @end example | ||
| 1901 | |||
| 1902 | @noindent | ||
| 1903 | Here are the results of calling @code{display-prefix} with various | ||
| 1904 | raw prefix arguments: | ||
| 1905 | |||
| 1906 | @example | ||
| 1907 | M-x display-prefix @print{} nil | ||
| 1908 | |||
| 1909 | C-u M-x display-prefix @print{} (4) | ||
| 1910 | |||
| 1911 | C-u C-u M-x display-prefix @print{} (16) | ||
| 1912 | |||
| 1913 | C-u 3 M-x display-prefix @print{} 3 | ||
| 1914 | |||
| 1915 | M-3 M-x display-prefix @print{} 3 ; @r{(Same as @code{C-u 3}.)} | ||
| 1916 | |||
| 1917 | C-u - M-x display-prefix @print{} - | ||
| 1918 | |||
| 1919 | M- - M-x display-prefix @print{} - ; @r{(Same as @code{C-u -}.)} | ||
| 1920 | |||
| 1921 | C-u -7 M-x display-prefix @print{} -7 | ||
| 1922 | |||
| 1923 | M- -7 M-x display-prefix @print{} -7 ; @r{(Same as @code{C-u -7}.)} | ||
| 1924 | @end example | ||
| 1925 | |||
| 1926 | Emacs uses two variables to store the prefix argument: | ||
| 1927 | @code{prefix-arg} and @code{current-prefix-arg}. Commands such as | ||
| 1928 | @code{universal-argument} that set up prefix arguments for other | ||
| 1929 | commands store them in @code{prefix-arg}. In contrast, | ||
| 1930 | @code{current-prefix-arg} conveys the prefix argument to the current | ||
| 1931 | command, so setting it has no effect on the prefix arguments for future | ||
| 1932 | commands. | ||
| 1933 | |||
| 1934 | Normally, commands specify which representation to use for the prefix | ||
| 1935 | argument, either numeric or raw, in the @code{interactive} declaration. | ||
| 1936 | (@xref{Interactive Call}.) Alternatively, functions may look at the | ||
| 1937 | value of the prefix argument directly in the variable | ||
| 1938 | @code{current-prefix-arg}, but this is less clean. | ||
| 1939 | |||
| 1940 | Do not call the functions @code{universal-argument}, | ||
| 1941 | @code{digit-argument}, or @code{negative-argument} unless you intend to | ||
| 1942 | let the user enter the prefix argument for the @emph{next} command. | ||
| 1943 | |||
| 1944 | @deffn Command universal-argument | ||
| 1945 | This command reads input and specifies a prefix argument for the | ||
| 1946 | following command. Don't call this command yourself unless you know | ||
| 1947 | what you are doing. | ||
| 1948 | @end deffn | ||
| 1949 | |||
| 1950 | @deffn Command digit-argument arg | ||
| 1951 | This command adds to the prefix argument for the following command. The | ||
| 1952 | argument @var{arg} is the raw prefix argument as it was before this | ||
| 1953 | command; it is used to compute the updated prefix argument. Don't call | ||
| 1954 | this command yourself unless you know what you are doing. | ||
| 1955 | @end deffn | ||
| 1956 | |||
| 1957 | @deffn Command negative-argument arg | ||
| 1958 | This command adds to the numeric argument for the next command. The | ||
| 1959 | argument @var{arg} is the raw prefix argument as it was before this | ||
| 1960 | command; its value is negated to form the new prefix argument. Don't | ||
| 1961 | call this command yourself unless you know what you are doing. | ||
| 1962 | @end deffn | ||
| 1963 | |||
| 1964 | @defun prefix-numeric-value arg | ||
| 1965 | This function returns the numeric meaning of a valid raw prefix argument | ||
| 1966 | value, @var{arg}. The argument may be a symbol, a number, or a list. | ||
| 1967 | If it is @code{nil}, the value 1 is returned; if it is any other symbol, | ||
| 1968 | the value @minus{}1 is returned. If it is a number, that number is | ||
| 1969 | returned; if it is a list, the @sc{car} of that list (which should be a | ||
| 1970 | number) is returned. | ||
| 1971 | @end defun | ||
| 1972 | |||
| 1973 | @defvar current-prefix-arg | ||
| 1974 | This variable is the value of the raw prefix argument for the | ||
| 1975 | @emph{current} command. Commands may examine it directly, but the usual | ||
| 1976 | way to access it is with @code{(interactive "P")}. | ||
| 1977 | @end defvar | ||
| 1978 | |||
| 1979 | @defvar prefix-arg | ||
| 1980 | The value of this variable is the raw prefix argument for the | ||
| 1981 | @emph{next} editing command. Commands that specify prefix arguments for | ||
| 1982 | the following command work by setting this variable. | ||
| 1983 | @end defvar | ||
| 1984 | |||
| 1985 | @node Recursive Editing | ||
| 1986 | @section Recursive Editing | ||
| 1987 | @cindex recursive command loop | ||
| 1988 | @cindex recursive editing level | ||
| 1989 | @cindex command loop, recursive | ||
| 1990 | |||
| 1991 | The Emacs command loop is entered automatically when Emacs starts up. | ||
| 1992 | This top-level invocation of the command loop is never exited until the | ||
| 1993 | Emacs is killed. Lisp programs can also invoke the command loop. Since | ||
| 1994 | this makes more than one activation of the command loop, we call it | ||
| 1995 | @dfn{recursive editing}. A recursive editing level has the effect of | ||
| 1996 | suspending whatever command invoked it and permitting the user to do | ||
| 1997 | arbitrary editing before resuming that command. | ||
| 1998 | |||
| 1999 | The commands available during recursive editing are the same ones | ||
| 2000 | available in the top-level editing loop and defined in the keymaps. | ||
| 2001 | Only a few special commands exit the recursive editing level; the others | ||
| 2002 | return to the recursive editing level when finished. (The special | ||
| 2003 | commands for exiting are always available, but do nothing when recursive | ||
| 2004 | editing is not in progress.) | ||
| 2005 | |||
| 2006 | All command loops, including recursive ones, set up all-purpose error | ||
| 2007 | handlers so that an error in a command run from the command loop will | ||
| 2008 | not exit the loop. | ||
| 2009 | |||
| 2010 | @cindex minibuffer input | ||
| 2011 | Minibuffer input is a special kind of recursive editing. It has a few | ||
| 2012 | special wrinkles, such as enabling display of the minibuffer and the | ||
| 2013 | minibuffer window, but fewer than you might suppose. Certain keys | ||
| 2014 | behave differently in the minibuffer, but that is only because of the | ||
| 2015 | minibuffer's local map; if you switch windows, you get the usual Emacs | ||
| 2016 | commands. | ||
| 2017 | |||
| 2018 | @cindex @code{throw} example | ||
| 2019 | @kindex exit | ||
| 2020 | @cindex exit recursive editing | ||
| 2021 | @cindex aborting | ||
| 2022 | To invoke a recursive editing level, call the function | ||
| 2023 | @code{recursive-edit}. This function contains the command loop; it also | ||
| 2024 | contains a call to @code{catch} with tag @code{exit}, which makes it | ||
| 2025 | possible to exit the recursive editing level by throwing to @code{exit} | ||
| 2026 | (@pxref{Catch and Throw}). If you throw a value other than @code{t}, | ||
| 2027 | then @code{recursive-edit} returns normally to the function that called | ||
| 2028 | it. The command @kbd{C-M-c} (@code{exit-recursive-edit}) does this. | ||
| 2029 | Throwing a @code{t} value causes @code{recursive-edit} to quit, so that | ||
| 2030 | control returns to the command loop one level up. This is called | ||
| 2031 | @dfn{aborting}, and is done by @kbd{C-]} (@code{abort-recursive-edit}). | ||
| 2032 | |||
| 2033 | Most applications should not use recursive editing, except as part of | ||
| 2034 | using the minibuffer. Usually it is more convenient for the user if you | ||
| 2035 | change the major mode of the current buffer temporarily to a special | ||
| 2036 | major mode, which has a command to go back to the previous mode. (This | ||
| 2037 | technique is used by the @kbd{w} command in Rmail.) Or, if you wish to | ||
| 2038 | give the user different text to edit ``recursively'', create and select | ||
| 2039 | a new buffer in a special mode. In this mode, define a command to | ||
| 2040 | complete the processing and go back to the previous buffer. (The | ||
| 2041 | @kbd{m} command in Rmail does this.) | ||
| 2042 | |||
| 2043 | Recursive edits are useful in debugging. You can insert a call to | ||
| 2044 | @code{debug} into a function definition as a sort of breakpoint, so that | ||
| 2045 | you can look around when the function gets there. @code{debug} invokes | ||
| 2046 | a recursive edit but also provides the other features of the debugger. | ||
| 2047 | |||
| 2048 | Recursive editing levels are also used when you type @kbd{C-r} in | ||
| 2049 | @code{query-replace} or use @kbd{C-x q} (@code{kbd-macro-query}). | ||
| 2050 | |||
| 2051 | @defun recursive-edit | ||
| 2052 | @cindex suspend evaluation | ||
| 2053 | This function invokes the editor command loop. It is called | ||
| 2054 | automatically by the initialization of Emacs, to let the user begin | ||
| 2055 | editing. When called from a Lisp program, it enters a recursive editing | ||
| 2056 | level. | ||
| 2057 | |||
| 2058 | In the following example, the function @code{simple-rec} first | ||
| 2059 | advances point one word, then enters a recursive edit, printing out a | ||
| 2060 | message in the echo area. The user can then do any editing desired, and | ||
| 2061 | then type @kbd{C-M-c} to exit and continue executing @code{simple-rec}. | ||
| 2062 | |||
| 2063 | @example | ||
| 2064 | (defun simple-rec () | ||
| 2065 | (forward-word 1) | ||
| 2066 | (message "Recursive edit in progress.") | ||
| 2067 | (recursive-edit) | ||
| 2068 | (forward-word 1)) | ||
| 2069 | @result{} simple-rec | ||
| 2070 | (simple-rec) | ||
| 2071 | @result{} nil | ||
| 2072 | @end example | ||
| 2073 | @end defun | ||
| 2074 | |||
| 2075 | @deffn Command exit-recursive-edit | ||
| 2076 | This function exits from the innermost recursive edit (including | ||
| 2077 | minibuffer input). Its definition is effectively @code{(throw 'exit | ||
| 2078 | nil)}. | ||
| 2079 | @end deffn | ||
| 2080 | |||
| 2081 | @deffn Command abort-recursive-edit | ||
| 2082 | This function aborts the command that requested the innermost recursive | ||
| 2083 | edit (including minibuffer input), by signaling @code{quit} | ||
| 2084 | after exiting the recursive edit. Its definition is effectively | ||
| 2085 | @code{(throw 'exit t)}. @xref{Quitting}. | ||
| 2086 | @end deffn | ||
| 2087 | |||
| 2088 | @deffn Command top-level | ||
| 2089 | This function exits all recursive editing levels; it does not return a | ||
| 2090 | value, as it jumps completely out of any computation directly back to | ||
| 2091 | the main command loop. | ||
| 2092 | @end deffn | ||
| 2093 | |||
| 2094 | @defun recursion-depth | ||
| 2095 | This function returns the current depth of recursive edits. When no | ||
| 2096 | recursive edit is active, it returns 0. | ||
| 2097 | @end defun | ||
| 2098 | |||
| 2099 | @node Disabling Commands | ||
| 2100 | @section Disabling Commands | ||
| 2101 | @cindex disabled command | ||
| 2102 | |||
| 2103 | @dfn{Disabling a command} marks the command as requiring user | ||
| 2104 | confirmation before it can be executed. Disabling is used for commands | ||
| 2105 | which might be confusing to beginning users, to prevent them from using | ||
| 2106 | the commands by accident. | ||
| 2107 | |||
| 2108 | @kindex disabled | ||
| 2109 | The low-level mechanism for disabling a command is to put a | ||
| 2110 | non-@code{nil} @code{disabled} property on the Lisp symbol for the | ||
| 2111 | command. These properties are normally set up by the user's | ||
| 2112 | @file{.emacs} file with Lisp expressions such as this: | ||
| 2113 | |||
| 2114 | @example | ||
| 2115 | (put 'upcase-region 'disabled t) | ||
| 2116 | @end example | ||
| 2117 | |||
| 2118 | @noindent | ||
| 2119 | For a few commands, these properties are present by default and may be | ||
| 2120 | removed by the @file{.emacs} file. | ||
| 2121 | |||
| 2122 | If the value of the @code{disabled} property is a string, that string | ||
| 2123 | is included in the message printed when the command is used: | ||
| 2124 | |||
| 2125 | @example | ||
| 2126 | (put 'delete-region 'disabled | ||
| 2127 | "Text deleted this way cannot be yanked back!\n") | ||
| 2128 | @end example | ||
| 2129 | |||
| 2130 | @xref{Disabling,,, emacs, The GNU Emacs Manual}, for the details on | ||
| 2131 | what happens when a disabled command is invoked interactively. | ||
| 2132 | Disabling a command has no effect on calling it as a function from Lisp | ||
| 2133 | programs. | ||
| 2134 | |||
| 2135 | @deffn Command enable-command command | ||
| 2136 | Allow @var{command} to be executed without special confirmation from now | ||
| 2137 | on. The user's @file{.emacs} file is optionally altered so that this | ||
| 2138 | will apply to future sessions. | ||
| 2139 | @end deffn | ||
| 2140 | |||
| 2141 | @deffn Command disable-command command | ||
| 2142 | Require special confirmation to execute @var{command} from now on. The | ||
| 2143 | user's @file{.emacs} file is optionally altered so that this will apply | ||
| 2144 | to future sessions. | ||
| 2145 | @end deffn | ||
| 2146 | |||
| 2147 | @defvar disabled-command-hook | ||
| 2148 | This variable is a normal hook that is run instead of a disabled command, | ||
| 2149 | when the user runs the disabled command interactively. The hook functions | ||
| 2150 | can use @code{this-command-keys} to determine what the user typed to run | ||
| 2151 | the command, and thus find the command itself. | ||
| 2152 | |||
| 2153 | By default, @code{disabled-command-hook} contains a function that asks | ||
| 2154 | the user whether to proceed. | ||
| 2155 | @end defvar | ||
| 2156 | |||
| 2157 | @node Command History | ||
| 2158 | @section Command History | ||
| 2159 | @cindex command history | ||
| 2160 | @cindex complex command | ||
| 2161 | @cindex history of commands | ||
| 2162 | |||
| 2163 | The command loop keeps a history of the complex commands that have | ||
| 2164 | been executed, to make it convenient to repeat these commands. A | ||
| 2165 | @dfn{complex command} is one for which the interactive argument reading | ||
| 2166 | uses the minibuffer. This includes any @kbd{M-x} command, any | ||
| 2167 | @kbd{M-ESC} command, and any command whose @code{interactive} | ||
| 2168 | specification reads an argument from the minibuffer. Explicit use of | ||
| 2169 | the minibuffer during the execution of the command itself does not cause | ||
| 2170 | the command to be considered complex. | ||
| 2171 | |||
| 2172 | @defvar command-history | ||
| 2173 | This variable's value is a list of recent complex commands, each | ||
| 2174 | represented as a form to evaluate. It continues to accumulate all | ||
| 2175 | complex commands for the duration of the editing session, but all but | ||
| 2176 | the first (most recent) thirty elements are deleted when a garbage | ||
| 2177 | collection takes place (@pxref{Garbage Collection}). | ||
| 2178 | |||
| 2179 | @example | ||
| 2180 | @group | ||
| 2181 | command-history | ||
| 2182 | @result{} ((switch-to-buffer "chistory.texi") | ||
| 2183 | (describe-key "^X^[") | ||
| 2184 | (visit-tags-table "~/emacs/src/") | ||
| 2185 | (find-tag "repeat-complex-command")) | ||
| 2186 | @end group | ||
| 2187 | @end example | ||
| 2188 | @end defvar | ||
| 2189 | |||
| 2190 | This history list is actually a special case of minibuffer history | ||
| 2191 | (@pxref{Minibuffer History}), with one special twist: the elements are | ||
| 2192 | expressions rather than strings. | ||
| 2193 | |||
| 2194 | There are a number of commands devoted to the editing and recall of | ||
| 2195 | previous commands. The commands @code{repeat-complex-command}, and | ||
| 2196 | @code{list-command-history} are described in the user manual | ||
| 2197 | (@pxref{Repetition,,, emacs, The GNU Emacs Manual}). Within the | ||
| 2198 | minibuffer, the history commands used are the same ones available in any | ||
| 2199 | minibuffer. | ||
| 2200 | |||
| 2201 | @node Keyboard Macros | ||
| 2202 | @section Keyboard Macros | ||
| 2203 | @cindex keyboard macros | ||
| 2204 | |||
| 2205 | A @dfn{keyboard macro} is a canned sequence of input events that can | ||
| 2206 | be considered a command and made the definition of a key. Don't confuse | ||
| 2207 | keyboard macros with Lisp macros (@pxref{Macros}). | ||
| 2208 | |||
| 2209 | @defun execute-kbd-macro macro &optional count | ||
| 2210 | This function executes @var{macro} as a sequence of events. If | ||
| 2211 | @var{macro} is a string or vector, then the events in it are executed | ||
| 2212 | exactly as if they had been input by the user. The sequence is | ||
| 2213 | @emph{not} expected to be a single key sequence; normally a keyboard | ||
| 2214 | macro definition consists of several key sequences concatenated. | ||
| 2215 | |||
| 2216 | If @var{macro} is a symbol, then its function definition is used in | ||
| 2217 | place of @var{macro}. If that is another symbol, this process repeats. | ||
| 2218 | Eventually the result should be a string or vector. If the result is | ||
| 2219 | not a symbol, string, or vector, an error is signaled. | ||
| 2220 | |||
| 2221 | The argument @var{count} is a repeat count; @var{macro} is executed that | ||
| 2222 | many times. If @var{count} is omitted or @code{nil}, @var{macro} is | ||
| 2223 | executed once. If it is 0, @var{macro} is executed over and over until it | ||
| 2224 | encounters an error or a failing search. | ||
| 2225 | @end defun | ||
| 2226 | |||
| 2227 | @defvar last-kbd-macro | ||
| 2228 | This variable is the definition of the most recently defined keyboard | ||
| 2229 | macro. Its value is a string or vector, or @code{nil}. | ||
| 2230 | @end defvar | ||
| 2231 | |||
| 2232 | @defvar executing-macro | ||
| 2233 | This variable contains the string or vector that defines the keyboard | ||
| 2234 | macro that is currently executing. It is @code{nil} if no macro is | ||
| 2235 | currently executing. | ||
| 2236 | @end defvar | ||
| 2237 | |||
| 2238 | @defvar defining-kbd-macro | ||
| 2239 | This variable indicates whether a keyboard macro is being defined. It | ||
| 2240 | is set to @code{t} by @code{start-kbd-macro}, and @code{nil} by | ||
| 2241 | @code{end-kbd-macro}. You can use this variable to make a command | ||
| 2242 | behave differently when run from a keyboard macro (perhaps indirectly by | ||
| 2243 | calling @code{interactive-p}). However, do not set this variable | ||
| 2244 | yourself. | ||
| 2245 | @end defvar | ||
| 2246 | |||
| 2247 | @ignore @c It's hard to make this format ok. | ||
| 2248 | The user-level commands for defining, running and editing keyboard | ||
| 2249 | macros include @code{call-last-kbd-macro}, @code{insert-kbd-macro}, | ||
| 2250 | @code{start-kbd-macro}, @code{end-kbd-macro}, @code{kbd-macro-query}, | ||
| 2251 | and @code{name-last-kbd-macro}. | ||
| 2252 | @end ignore | ||
| 2253 | |||
| 2254 | @c Broke paragraph to prevent overfull hbox. --rjc 15mar92 | ||
| 2255 | The commands are described in the user's manual (@pxref{Keyboard | ||
| 2256 | Macros,,, emacs, The GNU Emacs Manual}). | ||
| 2257 | |||