47f826c6fb1a69ef895d7aad7094c4a530b87af1
[chise/xemacs-chise.git] / info / lispref.info-18
1 This is ../info/lispref.info, produced by makeinfo version 4.0 from
2 lispref/lispref.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Lispref: (lispref).           XEmacs Lisp Reference Manual.
7 END-INFO-DIR-ENTRY
8
9    Edition History:
10
11    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
20
21    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
24
25    Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
28
29    Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
33
34    Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
38
39    Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
45
46    Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
51
52 \1f
53 File: lispref.info,  Node: Waiting,  Next: Quitting,  Prev: Reading Input,  Up: Command Loop
54
55 Waiting for Elapsed Time or Input
56 =================================
57
58    The wait functions are designed to wait for a certain amount of time
59 to pass or until there is input.  For example, you may wish to pause in
60 the middle of a computation to allow the user time to view the display.
61 `sit-for' pauses and updates the screen, and returns immediately if
62 input comes in, while `sleep-for' pauses without updating the screen.
63
64    Note that in FSF Emacs, the commands `sit-for' and `sleep-for' take
65 two arguments to specify the time (one integer and one float value),
66 instead of a single argument that can be either an integer or a float.
67
68  - Function: sit-for seconds &optional nodisp
69      This function performs redisplay (provided there is no pending
70      input from the user), then waits SECONDS seconds, or until input is
71      available.  The result is `t' if `sit-for' waited the full time
72      with no input arriving (see `input-pending-p' in *Note Peeking and
73      Discarding::).  Otherwise, the value is `nil'.
74
75      The argument SECONDS need not be an integer.  If it is a floating
76      point number, `sit-for' waits for a fractional number of seconds.
77
78      Redisplay is normally preempted if input arrives, and does not
79      happen at all if input is available before it starts. (You can
80      force screen updating in such a case by using `force-redisplay'.
81      *Note Refresh Screen::.) If there is no input pending, you can
82      force an update with no delay by using `(sit-for 0)'.
83
84      If NODISP is non-`nil', then `sit-for' does not redisplay, but it
85      still returns as soon as input is available (or when the timeout
86      elapses).
87
88      The usual purpose of `sit-for' is to give the user time to read
89      text that you display.
90
91  - Function: sleep-for seconds
92      This function simply pauses for SECONDS seconds without updating
93      the display.  This function pays no attention to available input.
94      It returns `nil'.
95
96      The argument SECONDS need not be an integer.  If it is a floating
97      point number, `sleep-for' waits for a fractional number of seconds.
98
99      Use `sleep-for' when you wish to guarantee a delay.
100
101    *Note Time of Day::, for functions to get the current time.
102
103 \1f
104 File: lispref.info,  Node: Quitting,  Next: Prefix Command Arguments,  Prev: Waiting,  Up: Command Loop
105
106 Quitting
107 ========
108
109    Typing `C-g' while a Lisp function is running causes XEmacs to
110 "quit" whatever it is doing.  This means that control returns to the
111 innermost active command loop.
112
113    Typing `C-g' while the command loop is waiting for keyboard input
114 does not cause a quit; it acts as an ordinary input character.  In the
115 simplest case, you cannot tell the difference, because `C-g' normally
116 runs the command `keyboard-quit', whose effect is to quit.  However,
117 when `C-g' follows a prefix key, the result is an undefined key.  The
118 effect is to cancel the prefix key as well as any prefix argument.
119
120    In the minibuffer, `C-g' has a different definition: it aborts out
121 of the minibuffer.  This means, in effect, that it exits the minibuffer
122 and then quits.  (Simply quitting would return to the command loop
123 _within_ the minibuffer.)  The reason why `C-g' does not quit directly
124 when the command reader is reading input is so that its meaning can be
125 redefined in the minibuffer in this way.  `C-g' following a prefix key
126 is not redefined in the minibuffer, and it has its normal effect of
127 canceling the prefix key and prefix argument.  This too would not be
128 possible if `C-g' always quit directly.
129
130    When `C-g' does directly quit, it does so by setting the variable
131 `quit-flag' to `t'.  XEmacs checks this variable at appropriate times
132 and quits if it is not `nil'.  Setting `quit-flag' non-`nil' in any way
133 thus causes a quit.
134
135    At the level of C code, quitting cannot happen just anywhere; only
136 at the special places that check `quit-flag'.  The reason for this is
137 that quitting at other places might leave an inconsistency in XEmacs's
138 internal state.  Because quitting is delayed until a safe place,
139 quitting cannot make XEmacs crash.
140
141    Certain functions such as `read-key-sequence' or `read-quoted-char'
142 prevent quitting entirely even though they wait for input.  Instead of
143 quitting, `C-g' serves as the requested input.  In the case of
144 `read-key-sequence', this serves to bring about the special behavior of
145 `C-g' in the command loop.  In the case of `read-quoted-char', this is
146 so that `C-q' can be used to quote a `C-g'.
147
148    You can prevent quitting for a portion of a Lisp function by binding
149 the variable `inhibit-quit' to a non-`nil' value.  Then, although `C-g'
150 still sets `quit-flag' to `t' as usual, the usual result of this--a
151 quit--is prevented.  Eventually, `inhibit-quit' will become `nil'
152 again, such as when its binding is unwound at the end of a `let' form.
153 At that time, if `quit-flag' is still non-`nil', the requested quit
154 happens immediately.  This behavior is ideal when you wish to make sure
155 that quitting does not happen within a "critical section" of the
156 program.
157
158    In some functions (such as `read-quoted-char'), `C-g' is handled in
159 a special way that does not involve quitting.  This is done by reading
160 the input with `inhibit-quit' bound to `t', and setting `quit-flag' to
161 `nil' before `inhibit-quit' becomes `nil' again.  This excerpt from the
162 definition of `read-quoted-char' shows how this is done; it also shows
163 that normal quitting is permitted after the first character of input.
164
165      (defun read-quoted-char (&optional prompt)
166        "...DOCUMENTATION..."
167        (let ((count 0) (code 0) char)
168          (while (< count 3)
169            (let ((inhibit-quit (zerop count))
170                  (help-form nil))
171              (and prompt (message "%s-" prompt))
172              (setq char (read-char))
173              (if inhibit-quit (setq quit-flag nil)))
174            ...)
175          (logand 255 code)))
176
177  - Variable: quit-flag
178      If this variable is non-`nil', then XEmacs quits immediately,
179      unless `inhibit-quit' is non-`nil'.  Typing `C-g' ordinarily sets
180      `quit-flag' non-`nil', regardless of `inhibit-quit'.
181
182  - Variable: inhibit-quit
183      This variable determines whether XEmacs should quit when
184      `quit-flag' is set to a value other than `nil'.  If `inhibit-quit'
185      is non-`nil', then `quit-flag' has no special effect.
186
187  - Command: keyboard-quit
188      This function signals the `quit' condition with `(signal 'quit
189      nil)'.  This is the same thing that quitting does.  (See `signal'
190      in *Note Errors::.)
191
192    You can specify a character other than `C-g' to use for quitting.
193 See the function `set-input-mode' in *Note Terminal Input::.
194
195 \1f
196 File: lispref.info,  Node: Prefix Command Arguments,  Next: Recursive Editing,  Prev: Quitting,  Up: Command Loop
197
198 Prefix Command Arguments
199 ========================
200
201    Most XEmacs commands can use a "prefix argument", a number specified
202 before the command itself.  (Don't confuse prefix arguments with prefix
203 keys.)  The prefix argument is at all times represented by a value,
204 which may be `nil', meaning there is currently no prefix argument.
205 Each command may use the prefix argument or ignore it.
206
207    There are two representations of the prefix argument: "raw" and
208 "numeric".  The editor command loop uses the raw representation
209 internally, and so do the Lisp variables that store the information, but
210 commands can request either representation.
211
212    Here are the possible values of a raw prefix argument:
213
214    * `nil', meaning there is no prefix argument.  Its numeric value is
215      1, but numerous commands make a distinction between `nil' and the
216      integer 1.
217
218    * An integer, which stands for itself.
219
220    * A list of one element, which is an integer.  This form of prefix
221      argument results from one or a succession of `C-u''s with no
222      digits.  The numeric value is the integer in the list, but some
223      commands make a distinction between such a list and an integer
224      alone.
225
226    * The symbol `-'.  This indicates that `M--' or `C-u -' was typed,
227      without following digits.  The equivalent numeric value is -1, but
228      some commands make a distinction between the integer -1 and the
229      symbol `-'.
230
231    We illustrate these possibilities by calling the following function
232 with various prefixes:
233
234      (defun display-prefix (arg)
235        "Display the value of the raw prefix arg."
236        (interactive "P")
237        (message "%s" arg))
238
239 Here are the results of calling `display-prefix' with various raw
240 prefix arguments:
241
242              M-x display-prefix  -| nil
243      
244      C-u     M-x display-prefix  -| (4)
245      
246      C-u C-u M-x display-prefix  -| (16)
247      
248      C-u 3   M-x display-prefix  -| 3
249      
250      M-3     M-x display-prefix  -| 3      ; (Same as `C-u 3'.)
251      
252      C-3     M-x display-prefix  -| 3      ; (Same as `C-u 3'.)
253      
254      C-u -   M-x display-prefix  -| -
255      
256      M--     M-x display-prefix  -| -      ; (Same as `C-u -'.)
257      
258      C--     M-x display-prefix  -| -      ; (Same as `C-u -'.)
259      
260      C-u - 7 M-x display-prefix  -| -7
261      
262      M-- 7   M-x display-prefix  -| -7     ; (Same as `C-u -7'.)
263      
264      C-- 7   M-x display-prefix  -| -7     ; (Same as `C-u -7'.)
265
266    XEmacs uses two variables to store the prefix argument: `prefix-arg'
267 and `current-prefix-arg'.  Commands such as `universal-argument' that
268 set up prefix arguments for other commands store them in `prefix-arg'.
269 In contrast, `current-prefix-arg' conveys the prefix argument to the
270 current command, so setting it has no effect on the prefix arguments
271 for future commands.
272
273    Normally, commands specify which representation to use for the prefix
274 argument, either numeric or raw, in the `interactive' declaration.
275 (*Note Using Interactive::.)  Alternatively, functions may look at the
276 value of the prefix argument directly in the variable
277 `current-prefix-arg', but this is less clean.
278
279  - Function: prefix-numeric-value arg
280      This function returns the numeric meaning of a valid raw prefix
281      argument value, ARG.  The argument may be a symbol, a number, or a
282      list.  If it is `nil', the value 1 is returned; if it is `-', the
283      value -1 is returned; if it is a number, that number is returned;
284      if it is a list, the CAR of that list (which should be a number) is
285      returned.
286
287  - Variable: current-prefix-arg
288      This variable holds the raw prefix argument for the _current_
289      command.  Commands may examine it directly, but the usual way to
290      access it is with `(interactive "P")'.
291
292  - Variable: prefix-arg
293      The value of this variable is the raw prefix argument for the
294      _next_ editing command.  Commands that specify prefix arguments for
295      the following command work by setting this variable.
296
297    Do not call the functions `universal-argument', `digit-argument', or
298 `negative-argument' unless you intend to let the user enter the prefix
299 argument for the _next_ command.
300
301  - Command: universal-argument
302      This command reads input and specifies a prefix argument for the
303      following command.  Don't call this command yourself unless you
304      know what you are doing.
305
306  - Command: digit-argument arg
307      This command adds to the prefix argument for the following
308      command.  The argument ARG is the raw prefix argument as it was
309      before this command; it is used to compute the updated prefix
310      argument.  Don't call this command yourself unless you know what
311      you are doing.
312
313  - Command: negative-argument arg
314      This command adds to the numeric argument for the next command.
315      The argument ARG is the raw prefix argument as it was before this
316      command; its value is negated to form the new prefix argument.
317      Don't call this command yourself unless you know what you are
318      doing.
319
320 \1f
321 File: lispref.info,  Node: Recursive Editing,  Next: Disabling Commands,  Prev: Prefix Command Arguments,  Up: Command Loop
322
323 Recursive Editing
324 =================
325
326    The XEmacs command loop is entered automatically when XEmacs starts
327 up.  This top-level invocation of the command loop never exits; it keeps
328 running as long as XEmacs does.  Lisp programs can also invoke the
329 command loop.  Since this makes more than one activation of the command
330 loop, we call it "recursive editing".  A recursive editing level has
331 the effect of suspending whatever command invoked it and permitting the
332 user to do arbitrary editing before resuming that command.
333
334    The commands available during recursive editing are the same ones
335 available in the top-level editing loop and defined in the keymaps.
336 Only a few special commands exit the recursive editing level; the others
337 return to the recursive editing level when they finish.  (The special
338 commands for exiting are always available, but they do nothing when
339 recursive editing is not in progress.)
340
341    All command loops, including recursive ones, set up all-purpose error
342 handlers so that an error in a command run from the command loop will
343 not exit the loop.
344
345    Minibuffer input is a special kind of recursive editing.  It has a
346 few special wrinkles, such as enabling display of the minibuffer and the
347 minibuffer window, but fewer than you might suppose.  Certain keys
348 behave differently in the minibuffer, but that is only because of the
349 minibuffer's local map; if you switch windows, you get the usual XEmacs
350 commands.
351
352    To invoke a recursive editing level, call the function
353 `recursive-edit'.  This function contains the command loop; it also
354 contains a call to `catch' with tag `exit', which makes it possible to
355 exit the recursive editing level by throwing to `exit' (*note Catch and
356 Throw::).  If you throw a value other than `t', then `recursive-edit'
357 returns normally to the function that called it.  The command `C-M-c'
358 (`exit-recursive-edit') does this.  Throwing a `t' value causes
359 `recursive-edit' to quit, so that control returns to the command loop
360 one level up.  This is called "aborting", and is done by `C-]'
361 (`abort-recursive-edit').
362
363    Most applications should not use recursive editing, except as part of
364 using the minibuffer.  Usually it is more convenient for the user if you
365 change the major mode of the current buffer temporarily to a special
366 major mode, which should have a command to go back to the previous mode.
367 (The `e' command in Rmail uses this technique.)  Or, if you wish to
368 give the user different text to edit "recursively", create and select a
369 new buffer in a special mode.  In this mode, define a command to
370 complete the processing and go back to the previous buffer.  (The `m'
371 command in Rmail does this.)
372
373    Recursive edits are useful in debugging.  You can insert a call to
374 `debug' into a function definition as a sort of breakpoint, so that you
375 can look around when the function gets there.  `debug' invokes a
376 recursive edit but also provides the other features of the debugger.
377
378    Recursive editing levels are also used when you type `C-r' in
379 `query-replace' or use `C-x q' (`kbd-macro-query').
380
381  - Function: recursive-edit
382      This function invokes the editor command loop.  It is called
383      automatically by the initialization of XEmacs, to let the user
384      begin editing.  When called from a Lisp program, it enters a
385      recursive editing level.
386
387      In the following example, the function `simple-rec' first advances
388      point one word, then enters a recursive edit, printing out a
389      message in the echo area.  The user can then do any editing
390      desired, and then type `C-M-c' to exit and continue executing
391      `simple-rec'.
392
393           (defun simple-rec ()
394             (forward-word 1)
395             (message "Recursive edit in progress")
396             (recursive-edit)
397             (forward-word 1))
398                => simple-rec
399           (simple-rec)
400                => nil
401
402  - Command: exit-recursive-edit
403      This function exits from the innermost recursive edit (including
404      minibuffer input).  Its definition is effectively `(throw 'exit
405      nil)'.
406
407  - Command: abort-recursive-edit
408      This function aborts the command that requested the innermost
409      recursive edit (including minibuffer input), by signaling `quit'
410      after exiting the recursive edit.  Its definition is effectively
411      `(throw 'exit t)'.  *Note Quitting::.
412
413  - Command: top-level
414      This function exits all recursive editing levels; it does not
415      return a value, as it jumps completely out of any computation
416      directly back to the main command loop.
417
418  - Function: recursion-depth
419      This function returns the current depth of recursive edits.  When
420      no recursive edit is active, it returns 0.
421
422 \1f
423 File: lispref.info,  Node: Disabling Commands,  Next: Command History,  Prev: Recursive Editing,  Up: Command Loop
424
425 Disabling Commands
426 ==================
427
428    "Disabling a command" marks the command as requiring user
429 confirmation before it can be executed.  Disabling is used for commands
430 which might be confusing to beginning users, to prevent them from using
431 the commands by accident.
432
433    The low-level mechanism for disabling a command is to put a
434 non-`nil' `disabled' property on the Lisp symbol for the command.
435 These properties are normally set up by the user's `.emacs' file with
436 Lisp expressions such as this:
437
438      (put 'upcase-region 'disabled t)
439
440 For a few commands, these properties are present by default and may be
441 removed by the `.emacs' file.
442
443    If the value of the `disabled' property is a string, the message
444 saying the command is disabled includes that string.  For example:
445
446      (put 'delete-region 'disabled
447           "Text deleted this way cannot be yanked back!\n")
448
449    *Note Disabling: (xemacs)Disabling, for the details on what happens
450 when a disabled command is invoked interactively.  Disabling a command
451 has no effect on calling it as a function from Lisp programs.
452
453  - Command: enable-command command
454      Allow COMMAND to be executed without special confirmation from now
455      on, and (if the user confirms) alter the user's `.emacs' file so
456      that this will apply to future sessions.
457
458  - Command: disable-command command
459      Require special confirmation to execute COMMAND from now on, and
460      (if the user confirms) alter the user's `.emacs' file so that this
461      will apply to future sessions.
462
463  - Variable: disabled-command-hook
464      This normal hook is run instead of a disabled command, when the
465      user invokes the disabled command interactively.  The hook
466      functions can use `this-command-keys' to determine what the user
467      typed to run the command, and thus find the command itself.  *Note
468      Hooks::.
469
470      By default, `disabled-command-hook' contains a function that asks
471      the user whether to proceed.
472
473 \1f
474 File: lispref.info,  Node: Command History,  Next: Keyboard Macros,  Prev: Disabling Commands,  Up: Command Loop
475
476 Command History
477 ===============
478
479    The command loop keeps a history of the complex commands that have
480 been executed, to make it convenient to repeat these commands.  A
481 "complex command" is one for which the interactive argument reading
482 uses the minibuffer.  This includes any `M-x' command, any `M-:'
483 command, and any command whose `interactive' specification reads an
484 argument from the minibuffer.  Explicit use of the minibuffer during
485 the execution of the command itself does not cause the command to be
486 considered complex.
487
488  - Variable: command-history
489      This variable's value is a list of recent complex commands, each
490      represented as a form to evaluate.  It continues to accumulate all
491      complex commands for the duration of the editing session, but all
492      but the first (most recent) thirty elements are deleted when a
493      garbage collection takes place (*note Garbage Collection::).
494
495           command-history
496           => ((switch-to-buffer "chistory.texi")
497               (describe-key "^X^[")
498               (visit-tags-table "~/emacs/src/")
499               (find-tag "repeat-complex-command"))
500
501    This history list is actually a special case of minibuffer history
502 (*note Minibuffer History::), with one special twist: the elements are
503 expressions rather than strings.
504
505    There are a number of commands devoted to the editing and recall of
506 previous commands.  The commands `repeat-complex-command', and
507 `list-command-history' are described in the user manual (*note
508 Repetition: (xemacs)Repetition.).  Within the minibuffer, the history
509 commands used are the same ones available in any minibuffer.
510
511 \1f
512 File: lispref.info,  Node: Keyboard Macros,  Prev: Command History,  Up: Command Loop
513
514 Keyboard Macros
515 ===============
516
517    A "keyboard macro" is a canned sequence of input events that can be
518 considered a command and made the definition of a key.  The Lisp
519 representation of a keyboard macro is a string or vector containing the
520 events.  Don't confuse keyboard macros with Lisp macros (*note
521 Macros::).
522
523  - Function: execute-kbd-macro macro &optional count
524      This function executes MACRO as a sequence of events.  If MACRO is
525      a string or vector, then the events in it are executed exactly as
526      if they had been input by the user.  The sequence is _not_
527      expected to be a single key sequence; normally a keyboard macro
528      definition consists of several key sequences concatenated.
529
530      If MACRO is a symbol, then its function definition is used in
531      place of MACRO.  If that is another symbol, this process repeats.
532      Eventually the result should be a string or vector.  If the result
533      is not a symbol, string, or vector, an error is signaled.
534
535      The argument COUNT is a repeat count; MACRO is executed that many
536      times.  If COUNT is omitted or `nil', MACRO is executed once.  If
537      it is 0, MACRO is executed over and over until it encounters an
538      error or a failing search.
539
540  - Variable: executing-macro
541      This variable contains the string or vector that defines the
542      keyboard macro that is currently executing.  It is `nil' if no
543      macro is currently executing.  A command can test this variable to
544      behave differently when run from an executing macro.  Do not set
545      this variable yourself.
546
547  - Variable: defining-kbd-macro
548      This variable indicates whether a keyboard macro is being defined.
549      A command can test this variable to behave differently while a
550      macro is being defined.  The commands `start-kbd-macro' and
551      `end-kbd-macro' set this variable--do not set it yourself.
552
553  - Variable: last-kbd-macro
554      This variable is the definition of the most recently defined
555      keyboard macro.  Its value is a string or vector, or `nil'.
556
557    The commands are described in the user's manual (*note Keyboard
558 Macros: (xemacs)Keyboard Macros.).
559
560 \1f
561 File: lispref.info,  Node: Keymaps,  Next: Menus,  Prev: Command Loop,  Up: Top
562
563 Keymaps
564 *******
565
566    The bindings between input events and commands are recorded in data
567 structures called "keymaps".  Each binding in a keymap associates (or
568 "binds") an individual event type either with another keymap or with a
569 command.  When an event is bound to a keymap, that keymap is used to
570 look up the next input event; this continues until a command is found.
571 The whole process is called "key lookup".
572
573 * Menu:
574
575 * Keymap Terminology::       Definitions of terms pertaining to keymaps.
576 * Format of Keymaps::        What a keymap looks like as a Lisp object.
577 * Creating Keymaps::         Functions to create and copy keymaps.
578 * Inheritance and Keymaps::  How one keymap can inherit the bindings
579                                 of another keymap.
580 * Key Sequences::            How to specify key sequences.
581 * Prefix Keys::              Defining a key with a keymap as its definition.
582 * Active Keymaps::           Each buffer has a local keymap
583                                 to override the standard (global) bindings.
584                                 A minor mode can also override them.
585 * Key Lookup::               How extracting elements from keymaps works.
586 * Functions for Key Lookup:: How to request key lookup.
587 * Changing Key Bindings::    Redefining a key in a keymap.
588 * Key Binding Commands::     Interactive interfaces for redefining keys.
589 * Scanning Keymaps::         Looking through all keymaps, for printing help.
590 * Other Keymap Functions::   Miscellaneous keymap functions.
591
592 \1f
593 File: lispref.info,  Node: Keymap Terminology,  Next: Format of Keymaps,  Up: Keymaps
594
595 Keymap Terminology
596 ==================
597
598    A "keymap" is a table mapping event types to definitions (which can
599 be any Lisp objects, though only certain types are meaningful for
600 execution by the command loop).  Given an event (or an event type) and a
601 keymap, XEmacs can get the event's definition.  Events mapped in keymaps
602 include keypresses, button presses, and button releases (*note
603 Events::).
604
605    A sequence of input events that form a unit is called a "key
606 sequence", or "key" for short.  A sequence of one event is always a key
607 sequence, and so are some multi-event sequences.
608
609    A keymap determines a binding or definition for any key sequence.  If
610 the key sequence is a single event, its binding is the definition of the
611 event in the keymap.  The binding of a key sequence of more than one
612 event is found by an iterative process: the binding of the first event
613 is found, and must be a keymap; then the second event's binding is found
614 in that keymap, and so on until all the events in the key sequence are
615 used up.
616
617    If the binding of a key sequence is a keymap, we call the key
618 sequence a "prefix key".  Otherwise, we call it a "complete key"
619 (because no more events can be added to it).  If the binding is `nil',
620 we call the key "undefined".  Examples of prefix keys are `C-c', `C-x',
621 and `C-x 4'.  Examples of defined complete keys are `X', <RET>, and
622 `C-x 4 C-f'.  Examples of undefined complete keys are `C-x C-g', and
623 `C-c 3'.  *Note Prefix Keys::, for more details.
624
625    The rule for finding the binding of a key sequence assumes that the
626 intermediate bindings (found for the events before the last) are all
627 keymaps; if this is not so, the sequence of events does not form a
628 unit--it is not really a key sequence.  In other words, removing one or
629 more events from the end of any valid key must always yield a prefix
630 key.  For example, `C-f C-n' is not a key; `C-f' is not a prefix key,
631 so a longer sequence starting with `C-f' cannot be a key.
632
633    Note that the set of possible multi-event key sequences depends on
634 the bindings for prefix keys; therefore, it can be different for
635 different keymaps, and can change when bindings are changed.  However,
636 a one-event sequence is always a key sequence, because it does not
637 depend on any prefix keys for its well-formedness.
638
639    At any time, several primary keymaps are "active"--that is, in use
640 for finding key bindings.  These are the "global map", which is shared
641 by all buffers; the "local keymap", which is usually associated with a
642 specific major mode; and zero or more "minor mode keymaps", which
643 belong to currently enabled minor modes.  (Not all minor modes have
644 keymaps.)  The local keymap bindings shadow (i.e., take precedence
645 over) the corresponding global bindings.  The minor mode keymaps shadow
646 both local and global keymaps.  *Note Active Keymaps::, for details.
647
648 \1f
649 File: lispref.info,  Node: Format of Keymaps,  Next: Creating Keymaps,  Prev: Keymap Terminology,  Up: Keymaps
650
651 Format of Keymaps
652 =================
653
654    A keymap is a primitive type that associates events with their
655 bindings.  Note that this is different from Emacs 18 and FSF Emacs,
656 where keymaps are lists.
657
658  - Function: keymapp object
659      This function returns `t' if OBJECT is a keymap, `nil' otherwise.
660
661 \1f
662 File: lispref.info,  Node: Creating Keymaps,  Next: Inheritance and Keymaps,  Prev: Format of Keymaps,  Up: Keymaps
663
664 Creating Keymaps
665 ================
666
667    Here we describe the functions for creating keymaps.
668
669  - Function: make-keymap &optional name
670      This function constructs and returns a new keymap object.  All
671      entries in it are `nil', meaning "command undefined".
672
673      Optional argument NAME specifies a name to assign to the keymap,
674      as in `set-keymap-name'.  This name is only a debugging
675      convenience; it is not used except when printing the keymap.
676
677  - Function: make-sparse-keymap &optional name
678      This function constructs and returns a new keymap object.  All
679      entries in it are `nil', meaning "command undefined".  The only
680      difference between this function and `make-keymap' is that this
681      function returns a "smaller" keymap (one that is expected to
682      contain fewer entries).  As keymaps dynamically resize, the
683      distinction is not great.
684
685      Optional argument NAME specifies a name to assign to the keymap,
686      as in `set-keymap-name'.  This name is only a debugging
687      convenience; it is not used except when printing the keymap.
688
689  - Function: set-keymap-name keymap new-name
690      This function assigns a "name" to a keymap.  The name is only a
691      debugging convenience; it is not used except when printing the
692      keymap.
693
694  - Function: keymap-name keymap
695      This function returns the "name" of a keymap, as assigned using
696      `set-keymap-name'.
697
698  - Function: copy-keymap keymap
699      This function returns a copy of KEYMAP.  Any keymaps that appear
700      directly as bindings in KEYMAP are also copied recursively, and so
701      on to any number of levels.  However, recursive copying does not
702      take place when the definition of a character is a symbol whose
703      function definition is a keymap; the same symbol appears in the
704      new copy.
705
706           (setq map (copy-keymap (current-local-map)))
707           => #<keymap 3 entries 0x21f80>
708           
709           (eq map (current-local-map))
710               => nil
711
712 \1f
713 File: lispref.info,  Node: Inheritance and Keymaps,  Next: Key Sequences,  Prev: Creating Keymaps,  Up: Keymaps
714
715 Inheritance and Keymaps
716 =======================
717
718    A keymap can inherit the bindings of other keymaps.  The other
719 keymaps are called the keymap's "parents", and are set with
720 `set-keymap-parents'.  When searching for a binding for a key sequence
721 in a particular keymap, that keymap itself will first be searched;
722 then, if no binding was found in the map and it has parents, the first
723 parent keymap will be searched; then that keymap's parent will be
724 searched, and so on, until either a binding for the key sequence is
725 found, or a keymap without a parent is encountered.  At this point, the
726 search will continue with the next parent of the most recently
727 encountered keymap that has another parent, etc.  Essentially, a
728 depth-first search of all the ancestors of the keymap is conducted.
729
730    `(current-global-map)' is the default parent of all keymaps.
731
732  - Function: set-keymap-parents keymap parents
733      This function sets the parent keymaps of KEYMAP to the list
734      PARENTS.
735
736      If you change the bindings in one of the keymaps in PARENTS using
737      `define-key' or other key-binding functions, these changes are
738      visible in KEYMAP unless shadowed by bindings in that map or in
739      earlier-searched ancestors.  The converse is not true: if you use
740      `define-key' to change KEYMAP, that affects the bindings in that
741      map, but has no effect on any of the keymaps in PARENTS.
742
743  - Function: keymap-parents keymap
744      This function returns the list of parent keymaps of KEYMAP, or
745      `nil' if KEYMAP has no parents.
746
747    As an alternative to specifying a parent, you can also specify a
748 "default binding" that is used whenever a key is not otherwise bound in
749 the keymap.  This is useful for terminal emulators, for example, which
750 may want to trap all keystrokes and pass them on in some modified
751 format.  Note that if you specify a default binding for a keymap,
752 neither the keymap's parents nor the current global map are searched for
753 key bindings.
754
755  - Function: set-keymap-default-binding keymap command
756      This function sets the default binding of KEYMAP to COMMAND, or
757      `nil' if no default is desired.
758
759  - Function: keymap-default-binding keymap
760      This function returns the default binding of KEYMAP, or `nil' if
761      it has none.
762
763 \1f
764 File: lispref.info,  Node: Key Sequences,  Next: Prefix Keys,  Prev: Inheritance and Keymaps,  Up: Keymaps
765
766 Key Sequences
767 =============
768
769    Contrary to popular belief, the world is not ASCII.  When running
770 under a window manager, XEmacs can tell the difference between, for
771 example, the keystrokes `control-h', `control-shift-h', and
772 `backspace'.  You can, in fact, bind different commands to each of
773 these.
774
775    A "key sequence" is a set of keystrokes.  A "keystroke" is a keysym
776 and some set of modifiers (such as <CONTROL> and <META>).  A "keysym"
777 is what is printed on the keys on your keyboard.
778
779    A keysym may be represented by a symbol, or (if and only if it is
780 equivalent to an ASCII character in the range 32 - 255) by a character
781 or its equivalent ASCII code.  The `A' key may be represented by the
782 symbol `A', the character `?A', or by the number 65.  The `break' key
783 may be represented only by the symbol `break'.
784
785    A keystroke may be represented by a list: the last element of the
786 list is the key (a symbol, character, or number, as above) and the
787 preceding elements are the symbolic names of modifier keys (<CONTROL>,
788 <META>, <SUPER>, <HYPER>, <ALT>, and <SHIFT>).  Thus, the sequence
789 `control-b' is represented by the forms `(control b)', `(control ?b)',
790 and `(control 98)'.  A keystroke may also be represented by an event
791 object, as returned by the `next-command-event' and `read-key-sequence'
792 functions.
793
794    Note that in this context, the keystroke `control-b' is _not_
795 represented by the number 2 (the ASCII code for `^B') or the character
796 `?\^B'.  See below.
797
798    The <SHIFT> modifier is somewhat of a special case.  You should not
799 (and cannot) use `(meta shift a)' to mean `(meta A)', since for
800 characters that have ASCII equivalents, the state of the shift key is
801 implicit in the keysym (`a' vs. `A').  You also cannot say `(shift =)'
802 to mean `+', as that sort of thing varies from keyboard to keyboard.
803 The <SHIFT> modifier is for use only with characters that do not have a
804 second keysym on the same key, such as `backspace' and `tab'.
805
806    A key sequence is a vector of keystrokes.  As a degenerate case,
807 elements of this vector may also be keysyms if they have no modifiers.
808 That is, the `A' keystroke is represented by all of these forms:
809
810              A       ?A      65      (A)     (?A)    (65)
811              [A]     [?A]    [65]    [(A)]   [(?A)]  [(65)]
812
813    the `control-a' keystroke is represented by these forms:
814
815              (control A)     (control ?A)    (control 65)
816              [(control A)]   [(control ?A)]  [(control 65)]
817
818    the key sequence `control-c control-a' is represented by these forms:
819
820              [(control c) (control a)]       [(control ?c) (control ?a)]
821              [(control 99) (control 65)]     etc.
822
823    Mouse button clicks work just like keypresses: `(control button1)'
824 means pressing the left mouse button while holding down the control
825 key.  `[(control c) (shift button3)]' means `control-c', hold <SHIFT>,
826 click right.
827
828    Commands may be bound to the mouse-button up-stroke rather than the
829 down-stroke as well.  `button1' means the down-stroke, and `button1up'
830 means the up-stroke.  Different commands may be bound to the up and
831 down strokes, though that is probably not what you want, so be careful.
832
833    For backward compatibility, a key sequence may also be represented by
834 a string.  In this case, it represents the key sequence(s) that would
835 produce that sequence of ASCII characters in a purely ASCII world.  For
836 example, a string containing the ASCII backspace character, `"\^H"',
837 would represent two key sequences: `(control h)' and `backspace'.
838 Binding a command to this will actually bind both of those key
839 sequences.  Likewise for the following pairs:
840
841                      control h       backspace
842                      control i       tab
843                      control m       return
844                      control j       linefeed
845                      control [       escape
846                      control @      control space
847
848    After binding a command to two key sequences with a form like
849
850              (define-key global-map "\^X\^I" 'command-1)
851
852 it is possible to redefine only one of those sequences like so:
853
854              (define-key global-map [(control x) (control i)] 'command-2)
855              (define-key global-map [(control x) tab] 'command-3)
856
857    Of course, all of this applies only when running under a window
858 system.  If you're talking to XEmacs through a TTY connection, you
859 don't get any of these features.
860
861  - Function: event-matches-key-specifier-p event key-specifier
862      This function returns non-`nil' if EVENT matches KEY-SPECIFIER,
863      which can be any valid form representing a key sequence.  This can
864      be useful, e.g., to determine if the user pressed `help-char' or
865      `quit-char'.
866
867 \1f
868 File: lispref.info,  Node: Prefix Keys,  Next: Active Keymaps,  Prev: Key Sequences,  Up: Keymaps
869
870 Prefix Keys
871 ===========
872
873    A "prefix key" has an associated keymap that defines what to do with
874 key sequences that start with the prefix key.  For example, `C-x' is a
875 prefix key, and it uses a keymap that is also stored in the variable
876 `ctl-x-map'.  Here is a list of the standard prefix keys of XEmacs and
877 their keymaps:
878
879    * `help-map' is used for events that follow `C-h'.
880
881    * `mode-specific-map' is for events that follow `C-c'.  This map is
882      not actually mode specific; its name was chosen to be informative
883      for the user in `C-h b' (`display-bindings'), where it describes
884      the main use of the `C-c' prefix key.
885
886    * `ctl-x-map' is the map used for events that follow `C-x'.  This
887      map is also the function definition of `Control-X-prefix'.
888
889    * `ctl-x-4-map' is used for events that follow `C-x 4'.
890
891    * `ctl-x-5-map' is used for events that follow `C-x 5'.
892
893    * The prefix keys `C-x n', `C-x r' and `C-x a' use keymaps that have
894      no special name.
895
896    * `esc-map' is an evil hack that is present for compatibility
897      purposes with Emacs 18.  Defining a key in `esc-map' is equivalent
898      to defining the same key in `global-map' but with the <META>
899      prefix added.  You should _not_ use this in your code. (This map is
900      also the function definition of `ESC-prefix'.)
901
902    The binding of a prefix key is the keymap to use for looking up the
903 events that follow the prefix key.  (It may instead be a symbol whose
904 function definition is a keymap.  The effect is the same, but the symbol
905 serves as a name for the prefix key.)  Thus, the binding of `C-x' is
906 the symbol `Control-X-prefix', whose function definition is the keymap
907 for `C-x' commands.  (The same keymap is also the value of `ctl-x-map'.)
908
909    Prefix key definitions can appear in any active keymap.  The
910 definitions of `C-c', `C-x', `C-h' and <ESC> as prefix keys appear in
911 the global map, so these prefix keys are always available.  Major and
912 minor modes can redefine a key as a prefix by putting a prefix key
913 definition for it in the local map or the minor mode's map.  *Note
914 Active Keymaps::.
915
916    If a key is defined as a prefix in more than one active map, then its
917 various definitions are in effect merged: the commands defined in the
918 minor mode keymaps come first, followed by those in the local map's
919 prefix definition, and then by those from the global map.
920
921    In the following example, we make `C-p' a prefix key in the local
922 keymap, in such a way that `C-p' is identical to `C-x'.  Then the
923 binding for `C-p C-f' is the function `find-file', just like `C-x C-f'.
924 The key sequence `C-p 6' is not found in any active keymap.
925
926      (use-local-map (make-sparse-keymap))
927          => nil
928      (local-set-key "\C-p" ctl-x-map)
929          => nil
930      (key-binding "\C-p\C-f")
931          => find-file
932      
933      (key-binding "\C-p6")
934          => nil
935
936  - Function: define-prefix-command symbol &optional mapvar
937      This function defines SYMBOL as a prefix command: it creates a
938      keymap and stores it as SYMBOL's function definition.  Storing the
939      symbol as the binding of a key makes the key a prefix key that has
940      a name.  If optional argument MAPVAR is not specified, it also
941      sets SYMBOL as a variable, to have the keymap as its value. (If
942      MAPVAR is given and is not `t', its value is stored as the value
943      of SYMBOL.) The function returns SYMBOL.
944
945      In Emacs version 18, only the function definition of SYMBOL was
946      set, not the value as a variable.
947
948 \1f
949 File: lispref.info,  Node: Active Keymaps,  Next: Key Lookup,  Prev: Prefix Keys,  Up: Keymaps
950
951 Active Keymaps
952 ==============
953
954    XEmacs normally contains many keymaps; at any given time, just a few
955 of them are "active" in that they participate in the interpretation of
956 user input.  These are the global keymap, the current buffer's local
957 keymap, and the keymaps of any enabled minor modes.
958
959    The "global keymap" holds the bindings of keys that are defined
960 regardless of the current buffer, such as `C-f'.  The variable
961 `global-map' holds this keymap, which is always active.
962
963    Each buffer may have another keymap, its "local keymap", which may
964 contain new or overriding definitions for keys.  The current buffer's
965 local keymap is always active except when `overriding-local-map' or
966 `overriding-terminal-local-map' overrides it.  Extents and text
967 properties can specify an alternative local map for certain parts of the
968 buffer; see *Note Extents and Events::.
969
970    Each minor mode may have a keymap; if it does, the keymap is active
971 when the minor mode is enabled.
972
973    The variable `overriding-local-map' and
974 `overriding-terminal-local-map', if non-`nil', specify other local
975 keymaps that override the buffer's local map and all the minor mode
976 keymaps.
977
978    All the active keymaps are used together to determine what command to
979 execute when a key is entered.  XEmacs searches these maps one by one,
980 in order of decreasing precedence, until it finds a binding in one of
981 the maps.
982
983    More specifically:
984
985    For key-presses, the order of keymaps searched is:
986
987    * the `keymap' property of any extent(s) or text properties at point;
988
989    * any applicable minor-mode maps;
990
991    * the current local map of the current buffer;
992
993    * the current global map.
994
995    For mouse-clicks, the order of keymaps searched is:
996
997    * the current local map of the `mouse-grabbed-buffer' if any;
998
999    * the `keymap' property of any extent(s) at the position of the click
1000      (this includes modeline extents);
1001
1002    * the `modeline-map' of the buffer corresponding to the modeline
1003      under the mouse (if the click happened over a modeline);
1004
1005    * the value of `toolbar-map' in the current buffer (if the click
1006      happened over a toolbar);
1007
1008    * the current local map of the buffer under the mouse (does not
1009      apply to toolbar clicks);
1010
1011    * any applicable minor-mode maps;
1012
1013    * the current global map.
1014
1015    Note that if `overriding-local-map' or
1016 `overriding-terminal-local-map' is non-`nil', _only_ those two maps and
1017 the current global map are searched.
1018
1019    The procedure for searching a single keymap is called "key lookup";
1020 see *Note Key Lookup::.
1021
1022    Since every buffer that uses the same major mode normally uses the
1023 same local keymap, you can think of the keymap as local to the mode.  A
1024 change to the local keymap of a buffer (using `local-set-key', for
1025 example) is seen also in the other buffers that share that keymap.
1026
1027    The local keymaps that are used for Lisp mode, C mode, and several
1028 other major modes exist even if they have not yet been used.  These
1029 local maps are the values of the variables `lisp-mode-map',
1030 `c-mode-map', and so on.  For most other modes, which are less
1031 frequently used, the local keymap is constructed only when the mode is
1032 used for the first time in a session.
1033
1034    The minibuffer has local keymaps, too; they contain various
1035 completion and exit commands.  *Note Intro to Minibuffers::.
1036
1037    *Note Standard Keymaps::, for a list of standard keymaps.
1038
1039  - Function: current-keymaps &optional event-or-keys
1040      This function returns a list of the current keymaps that will be
1041      searched for bindings.  This lists keymaps such as the current
1042      local map and the minor-mode maps, but does not list the parents
1043      of those keymaps.  EVENT-OR-KEYS controls which keymaps will be
1044      listed.  If EVENT-OR-KEYS is a mouse event (or a vector whose last
1045      element is a mouse event), the keymaps for that mouse event will
1046      be listed.  Otherwise, the keymaps for key presses will be listed.
1047
1048  - Variable: global-map
1049      This variable contains the default global keymap that maps XEmacs
1050      keyboard input to commands.  The global keymap is normally this
1051      keymap.  The default global keymap is a full keymap that binds
1052      `self-insert-command' to all of the printing characters.
1053
1054      It is normal practice to change the bindings in the global map,
1055      but you should not assign this variable any value other than the
1056      keymap it starts out with.
1057
1058  - Function: current-global-map
1059      This function returns the current global keymap.  This is the same
1060      as the value of `global-map' unless you change one or the other.
1061
1062           (current-global-map)
1063           => #<keymap global-map 639 entries 0x221>
1064
1065  - Function: current-local-map
1066      This function returns the current buffer's local keymap, or `nil'
1067      if it has none.  In the following example, the keymap for the
1068      `*scratch*' buffer (using Lisp Interaction mode) has a number of
1069      entries, including one prefix key, `C-x'.
1070
1071           (current-local-map)
1072           => #<keymap lisp-interaction-mode-map 5 entries 0x558>
1073           (describe-bindings-internal (current-local-map))
1074           =>  ; Inserted into the buffer:
1075           backspace       backward-delete-char-untabify
1076           linefeed        eval-print-last-sexp
1077           delete          delete-char
1078           C-j             eval-print-last-sexp
1079           C-x             << Prefix Command >>
1080           M-tab           lisp-complete-symbol
1081           M-;             lisp-indent-for-comment
1082           M-C-i           lisp-complete-symbol
1083           M-C-q           indent-sexp
1084           M-C-x           eval-defun
1085           Alt-backspace   backward-kill-sexp
1086           Alt-delete      kill-sexp
1087           
1088           C-x x           edebug-defun
1089
1090  - Function: current-minor-mode-maps
1091      This function returns a list of the keymaps of currently enabled
1092      minor modes.
1093
1094  - Function: use-global-map keymap
1095      This function makes KEYMAP the new current global keymap.  It
1096      returns `nil'.
1097
1098      It is very unusual to change the global keymap.
1099
1100  - Function: use-local-map keymap &optional buffer
1101      This function makes KEYMAP the new local keymap of BUFFER.  BUFFER
1102      defaults to the current buffer.  If KEYMAP is `nil', then the
1103      buffer has no local keymap.  `use-local-map' returns `nil'.  Most
1104      major mode commands use this function.
1105
1106  - Variable: minor-mode-map-alist
1107      This variable is an alist describing keymaps that may or may not be
1108      active according to the values of certain variables.  Its elements
1109      look like this:
1110
1111           (VARIABLE . KEYMAP)
1112
1113      The keymap KEYMAP is active whenever VARIABLE has a non-`nil'
1114      value.  Typically VARIABLE is the variable that enables or
1115      disables a minor mode.  *Note Keymaps and Minor Modes::.
1116
1117      Note that elements of `minor-mode-map-alist' do not have the same
1118      structure as elements of `minor-mode-alist'.  The map must be the
1119      CDR of the element; a list with the map as the second element will
1120      not do.
1121
1122      What's more, the keymap itself must appear in the CDR.  It does not
1123      work to store a variable in the CDR and make the map the value of
1124      that variable.
1125
1126      When more than one minor mode keymap is active, their order of
1127      priority is the order of `minor-mode-map-alist'.  But you should
1128      design minor modes so that they don't interfere with each other.
1129      If you do this properly, the order will not matter.
1130
1131      See also `minor-mode-key-binding', above.  See *Note Keymaps and
1132      Minor Modes::, for more information about minor modes.
1133
1134  - Variable: modeline-map
1135      This variable holds the keymap consulted for mouse-clicks on the
1136      modeline of a window.  This variable may be buffer-local; its
1137      value will be looked up in the buffer of the window whose modeline
1138      was clicked upon.
1139
1140  - Variable: toolbar-map
1141      This variable holds the keymap consulted for mouse-clicks over a
1142      toolbar.
1143
1144  - Variable: mouse-grabbed-buffer
1145      If non-`nil', a buffer which should be consulted first for all
1146      mouse activity.  When a mouse-click is processed, it will first be
1147      looked up in the local-map of this buffer, and then through the
1148      normal mechanism if there is no binding for that click.  This
1149      buffer's value of `mode-motion-hook' will be consulted instead of
1150      the `mode-motion-hook' of the buffer of the window under the mouse.
1151      You should _bind_ this, not set it.
1152
1153  - Variable: overriding-local-map
1154      If non-`nil', this variable holds a keymap to use instead of the
1155      buffer's local keymap and instead of all the minor mode keymaps.
1156      This keymap, if any, overrides all other maps that would have been
1157      active, except for the current global map.
1158
1159  - Variable: overriding-terminal-local-map
1160      If non-`nil', this variable holds a keymap to use instead of the
1161      buffer's local keymap and instead of all the minor mode keymaps,
1162      but for the selected console only. (In other words, this variable
1163      is always console-local; putting a keymap here only applies to
1164      keystrokes coming from the selected console.  *Note Consoles and
1165      Devices::.) This keymap, if any, overrides all other maps that
1166      would have been active, except for the current global map.
1167