[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1 Keymaps

The bindings between input events and commands are recorded in data structures called keymaps. Each binding in a keymap associates (or binds) an individual event type either with another keymap or with a command. When an event is bound to a keymap, that keymap is used to look up the next input event; this continues until a command is found. The whole process is called key lookup.

NOTE: Keymap documentation has not been completely updated for XEmacs. Some of the information below is incorrect.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.1 Keymap Terminology

A keymap is a table mapping event types to definitions (which can be any Lisp objects, though only certain types are meaningful for execution by the command loop). Given an event (or an event type) and a keymap, XEmacs can get the event’s definition. Events mapped in keymaps include keypresses, button presses, and button releases (@pxref{Events}).

A sequence of input events that form a unit is called a key sequence, or key for short. A sequence of one event is always a key sequence, and so are some multi-event sequences.

A keymap determines a binding or definition for any key sequence. If the key sequence is a single event, its binding is the definition of the event in the keymap. The binding of a key sequence of more than one event is found by an iterative process: the binding of the first event is found, and must be a keymap; then the second event’s binding is found in that keymap, and so on until all the events in the key sequence are used up.

If the binding of a key sequence is a keymap, we call the key sequence a prefix key. Otherwise, we call it a complete key (because no more events can be added to it). If the binding is nil, we call the key undefined. Examples of prefix keys are C-c, C-x, and C-x 4. Examples of defined complete keys are X, <RET>, and C-x 4 C-f. Examples of undefined complete keys are C-x C-g, and C-c 3. See section Prefix Keys, for more details.

The rule for finding the binding of a key sequence assumes that the intermediate bindings (found for the events before the last) are all keymaps; if this is not so, the sequence of events does not form a unit—it is not really a key sequence. In other words, removing one or more events from the end of any valid key must always yield a prefix key. For example, C-f C-n is not a key; C-f is not a prefix key, so a longer sequence starting with C-f cannot be a key.

Note that the set of possible multi-event key sequences depends on the bindings for prefix keys; therefore, it can be different for different keymaps, and can change when bindings are changed. However, a one-event sequence is always a key sequence, because it does not depend on any prefix keys for its well-formedness.

At any time, several primary keymaps are active—that is, in use for finding key bindings. These are the global map, which is shared by all buffers; the local keymap, which is usually associated with a specific major mode; and zero or more minor mode keymaps, which belong to currently enabled minor modes. (Not all minor modes have keymaps.) The local keymap bindings shadow (i.e., take precedence over) the corresponding global bindings. The minor mode keymaps shadow both local and global keymaps. See section Active Keymaps, for details.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2 Format of Keymaps

A keymap is a primitive type that associates events with their bindings. Note that this is different from Emacs 18 and FSF Emacs, where keymaps are lists.

Function: keymapp object

This function returns t if object is a keymap, nil otherwise.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3 Creating Keymaps

Here we describe the functions for creating keymaps.

Function: make-keymap

This function constructs and returns a new keymap object. All entries in it are nil, meaning “command undefined”.

Function: make-sparse-keymap

This function constructs and returns a new keymap object. All entries in it are nil, meaning “command undefined”. The only difference between this function and make-keymap is that this function returns a “smaller” keymap (one that is expected to contain fewer entries). As keymaps dynamically resize, the distinction is not great.

Function: copy-keymap keymap

This function returns a copy of keymap. Any keymaps that appear directly as bindings in keymap are also copied recursively, and so on to any number of levels. However, recursive copying does not take place when the definition of a character is a symbol whose function definition is a keymap; the same symbol appears in the new copy.

(setq map (copy-keymap (current-local-map)))
⇒ #<keymap 3 entries 0x21f80>
(eq map (current-local-map))
    ⇒ nil

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4 Inheritance and Keymaps

A keymap can inherit the bindings of other keymaps. The other keymaps are called the keymap’s parents, and are set with set-keymap-parents. When searching for a binding for a key sequence in a particular keymap, that keymap itself will first be searched; then, if no binding was found in the map and it has parents, the first parent keymap will be searched; then that keymap’s parent will be searched, and so on, until either a binding for the key sequence is found, or a keymap without a parent is encountered. At this point, the search will continue with the next parent of the most recently encountered keymap that has another parent, etc. Essentially, a depth-first search of all the ancestors of the keymap is conducted.

(current-global-map) is the default parent of all keymaps.

Function: set-keymap-parents keymap parents

This function sets the parent keymaps of keymap to the list parents.

If you change the bindings in one of the keymaps in parents using define-key or other key-binding functions, these changes are visible in keymap unless shadowed by bindings in that map or in earlier-searched ancestors. The converse is not true: if you use define-key to change keymap, that affects the bindings in that map, but has no effect on any of the keymaps in parents.

Function: keymap-parents keymap

This function returns the list of parent keymaps of keymap, or nil if keymap has no parents.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5 Prefix Keys

A prefix key has an associated keymap that defines what to do with key sequences that start with the prefix key. For example, C-x is a prefix key, and it uses a keymap that is also stored in the variable ctl-x-map. Here is a list of the standard prefix keys of XEmacs and their keymaps:

The binding of a prefix key is the keymap to use for looking up the events that follow the prefix key. (It may instead be a symbol whose function definition is a keymap. The effect is the same, but the symbol serves as a name for the prefix key.) Thus, the binding of C-x is the symbol Control-X-prefix, whose function definition is the keymap for C-x commands. (The same keymap is also the value of ctl-x-map.)

Prefix key definitions can appear in any active keymap. The definitions of C-c, C-x, C-h and <ESC> as prefix keys appear in the global map, so these prefix keys are always available. Major and minor modes can redefine a key as a prefix by putting a prefix key definition for it in the local map or the minor mode’s map. See section Active Keymaps.

If a key is defined as a prefix in more than one active map, then its various definitions are in effect merged: the commands defined in the minor mode keymaps come first, followed by those in the local map’s prefix definition, and then by those from the global map.

In the following example, we make C-p a prefix key in the local keymap, in such a way that C-p is identical to C-x. Then the binding for C-p C-f is the function find-file, just like C-x C-f. The key sequence C-p 6 is not found in any active keymap.

(use-local-map (make-sparse-keymap))
    ⇒ nil
(local-set-key "\C-p" ctl-x-map)
    ⇒ nil
(key-binding "\C-p\C-f")
    ⇒ find-file
(key-binding "\C-p6")
    ⇒ nil
Function: define-prefix-command symbol &optional mapvar

This function defines symbol as a prefix command: it creates a keymap and stores it as symbol’s function definition. Storing the symbol as the binding of a key makes the key a prefix key that has a name. If optional argument mapvar is not specified, it also sets symbol as a variable, to have the keymap as its value. (If mapvar is given and is not t, its value is stored as the value of symbol.) The function returns symbol.

In Emacs version 18, only the function definition of symbol was set, not the value as a variable.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6 Active Keymaps

XEmacs normally contains many keymaps; at any given time, just a few of them are active in that they participate in the interpretation of user input. These are the global keymap, the current buffer’s local keymap, and the keymaps of any enabled minor modes.

The global keymap holds the bindings of keys that are defined regardless of the current buffer, such as C-f. The variable global-map holds this keymap, which is always active.

Each buffer may have another keymap, its local keymap, which may contain new or overriding definitions for keys. The current buffer’s local keymap is always active except when overriding-local-map overrides it. Text properties can specify an alternative local map for certain parts of the buffer; see @ref{Special Properties}.

Each minor mode may have a keymap; if it does, the keymap is active when the minor mode is enabled.

The variable overriding-local-map, if non-nil, specifies another local keymap that overrides the buffer’s local map and all the minor mode keymaps.

All the active keymaps are used together to determine what command to execute when a key is entered. XEmacs searches these maps one by one, in order of decreasing precedence, until it finds a binding in one of the maps.

Normally, XEmacs first searches for the key in the minor mode maps (one map at a time); if they do not supply a binding for the key, XEmacs searches the local map; if that too has no binding, Emacs then searches the global map. However, if overriding-local-map is non-nil, XEmacs searches that map first, followed by the global map.

The procedure for searching a single keymap is called key lookup; see Key Lookup.

Since every buffer that uses the same major mode normally uses the same local keymap, you can think of the keymap as local to the mode. A change to the local keymap of a buffer (using local-set-key, for example) is seen also in the other buffers that share that keymap.

The local keymaps that are used for Lisp mode, C mode, and several other major modes exist even if they have not yet been used. These local maps are the values of the variables lisp-mode-map, c-mode-map, and so on. For most other modes, which are less frequently used, the local keymap is constructed only when the mode is used for the first time in a session.

The minibuffer has local keymaps, too; they contain various completion and exit commands. @xref{Intro to Minibuffers}.

@xref{Standard Keymaps}, for a list of standard keymaps.

Variable: global-map

This variable contains the default global keymap that maps XEmacs keyboard input to commands. The global keymap is normally this keymap. The default global keymap is a full keymap that binds self-insert-command to all of the printing characters.

It is normal practice to change the bindings in the global map, but you should not assign this variable any value other than the keymap it starts out with.

Function: current-global-map

This function returns the current global keymap. This is the same as the value of global-map unless you change one or the other.

(current-global-map)
⇒ (keymap [set-mark-command beginning-of-line … 
            delete-backward-char])
Function: current-local-map

This function returns the current buffer’s local keymap, or nil if it has none. In the following example, the keymap for the ‘*scratch*’ buffer (using Lisp Interaction mode) is a sparse keymap in which the entry for <ESC>, ASCII code 27, is another sparse keymap.

(current-local-map)
⇒ (keymap 
    (10 . eval-print-last-sexp) 
    (9 . lisp-indent-line) 
    (127 . backward-delete-char-untabify) 
    (27 keymap 
        (24 . eval-defun) 
        (17 . indent-sexp)))
Function: current-minor-mode-maps

This function returns a list of the keymaps of currently enabled minor modes.

Function: use-global-map keymap

This function makes keymap the new current global keymap. It returns nil.

It is very unusual to change the global keymap.

Function: use-local-map keymap

This function makes keymap the new local keymap of the current buffer. If keymap is nil, then the buffer has no local keymap. use-local-map returns nil. Most major mode commands use this function.

Variable: minor-mode-map-alist

This variable is an alist describing keymaps that may or may not be active according to the values of certain variables. Its elements look like this:

(variable . keymap)

The keymap keymap is active whenever variable has a non-nil value. Typically variable is the variable that enables or disables a minor mode. @xref{Keymaps and Minor Modes}.

Note that elements of minor-mode-map-alist do not have the same structure as elements of minor-mode-alist. The map must be the CDR of the element; a list with the map as the second element will not do.

What’s more, the keymap itself must appear in the CDR. It does not work to store a variable in the CDR and make the map the value of that variable.

When more than one minor mode keymap is active, their order of priority is the order of minor-mode-map-alist. But you should design minor modes so that they don’t interfere with each other. If you do this properly, the order will not matter.

See also minor-mode-key-binding, above. See @ref{Keymaps and Minor Modes}, for more information about minor modes.

Variable: overriding-local-map

If non-nil, this variable holds a keymap to use instead of the buffer’s local keymap and instead of all the minor mode keymaps. This keymap, if any, overrides all other maps that would have been active, except for the current global map.

Variable: overriding-local-map-menu-flag

If this variable is non-nil, the value of overriding-local-map can affect the display of the menu bar. The default value is nil, so overriding-local-map has no effect on the menu bar.

Note that overriding-local-map does affect the execution of key sequences entered using the menu bar, even if it does not affect the menu bar display. So if a menu bar key sequence comes in, you should clear overriding-local-map before looking up and executing that key sequence. Modes that use overriding-local-map would typically do this anyway; normally they respond to events that they do not handle by “unreading” them and exiting.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.7 Key Lookup

Key lookup is the process of finding the binding of a key sequence from a given keymap. Actual execution of the binding is not part of key lookup.

Key lookup uses just the event type of each event in the key sequence; the rest of the event is ignored. In fact, a key sequence used for key lookup may designate mouse events with just their types (symbols) instead of with entire mouse events (lists). @xref{Events}. Such a pseudo-key-sequence is insufficient for command-execute, but it is sufficient for looking up or rebinding a key.

When the key sequence consists of multiple events, key lookup processes the events sequentially: the binding of the first event is found, and must be a keymap; then the second event’s binding is found in that keymap, and so on until all the events in the key sequence are used up. (The binding thus found for the last event may or may not be a keymap.) Thus, the process of key lookup is defined in terms of a simpler process for looking up a single event in a keymap. How that is done depends on the type of object associated with the event in that keymap.

Let’s use the term keymap entry to describe the value found by looking up an event type in a keymap. (This doesn’t include the item string and other extra elements in menu key bindings because lookup-key and other key lookup functions don’t include them in the returned value.) While any Lisp object may be stored in a keymap as a keymap entry, not all make sense for key lookup. Here is a list of the meaningful kinds of keymap entries:

nil

nil means that the events used so far in the lookup form an undefined key. When a keymap fails to mention an event type at all, and has no default binding, that is equivalent to a binding of nil for that event type.

keymap

The events used so far in the lookup form a prefix key. The next event of the key sequence is looked up in keymap.

command

The events used so far in the lookup form a complete key, and command is its binding. @xref{What Is a Function}.

array

The array (either a string or a vector) is a keyboard macro. The events used so far in the lookup form a complete key, and the array is its binding. See @ref{Keyboard Macros}, for more information.

list

The meaning of a list depends on the types of the elements of the list.

symbol

The function definition of symbol is used in place of symbol. If that too is a symbol, then this process is repeated, any number of times. Ultimately this should lead to an object that is a keymap, a command or a keyboard macro. A list is allowed if it is a keymap or a command, but indirect entries are not understood when found via symbols.

Note that keymaps and keyboard macros (strings and vectors) are not valid functions, so a symbol with a keymap, string, or vector as its function definition is invalid as a function. It is, however, valid as a key binding. If the definition is a keyboard macro, then the symbol is also valid as an argument to command-execute (@pxref{Interactive Call}).

The symbol undefined is worth special mention: it means to treat the key as undefined. Strictly speaking, the key is defined, and its binding is the command undefined; but that command does the same thing that is done automatically for an undefined key: it rings the bell (by calling ding) but does not signal an error.

undefined is used in local keymaps to override a global key binding and make the key “undefined” locally. A local binding of nil would fail to do this because it would not override the global binding.

anything else

If any other type of object is found, the events used so far in the lookup form a complete key, and the object is its binding, but the binding is not executable as a command.

In short, a keymap entry may be a keymap, a command, a keyboard macro, a symbol that leads to one of them, or an indirection or nil. Here is an example of a sparse keymap with two characters bound to commands and one bound to another keymap. This map is the normal value of emacs-lisp-mode-map. Note that 9 is the code for <TAB>, 127 for <DEL>, 27 for <ESC>, 17 for C-q and 24 for C-x.

(keymap (9 . lisp-indent-line)
        (127 . backward-delete-char-untabify)
        (27 keymap (17 . indent-sexp) (24 . eval-defun)))

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.8 Functions for Key Lookup

Here are the functions and variables pertaining to key lookup.

Function: lookup-key keymap key &optional accept-defaults

This function returns the definition of key in keymap. If the string or vector key is not a valid key sequence according to the prefix keys specified in keymap (which means it is “too long” and has extra events at the end), then the value is a number, the number of events at the front of key that compose a complete key.

If accept-defaults is non-nil, then lookup-key considers default bindings as well as bindings for the specific events in key. Otherwise, lookup-key reports only bindings for the specific sequence key, ignoring default bindings except when you explicitly ask about them. (To do this, supply t as an element of key; see Format of Keymaps.)

All the other functions described in this chapter that look up keys use lookup-key.

(lookup-key (current-global-map) "\C-x\C-f")
    ⇒ find-file
(lookup-key (current-global-map) "\C-x\C-f12345")
    ⇒ 2

If key contains a meta character, that character is implicitly replaced by a two-character sequence: the value of meta-prefix-char, followed by the corresponding non-meta character. Thus, the first example below is handled by conversion into the second example.

(lookup-key (current-global-map) "\M-f")
    ⇒ forward-word
(lookup-key (current-global-map) "\ef")
    ⇒ forward-word

Unlike read-key-sequence, this function does not modify the specified events in ways that discard information (@pxref{Key Sequence Input}). In particular, it does not convert letters to lower case and it does not change drag events to clicks.

Command: undefined

Used in keymaps to undefine keys. It calls ding, but does not cause an error.

Function: key-binding key &optional accept-defaults

This function returns the binding for key in the current keymaps, trying all the active keymaps. The result is nil if key is undefined in the keymaps.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

An error is signaled if key is not a string or a vector.

(key-binding "\C-x\C-f")
    ⇒ find-file
Function: local-key-binding key &optional accept-defaults

This function returns the binding for key in the current local keymap, or nil if it is undefined there.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

Function: global-key-binding key &optional accept-defaults

This function returns the binding for command key in the current global keymap, or nil if it is undefined there.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

Function: minor-mode-key-binding key &optional accept-defaults

This function returns a list of all the active minor mode bindings of key. More precisely, it returns an alist of pairs (modename . binding), where modename is the variable that enables the minor mode, and binding is key’s binding in that mode. If key has no minor-mode bindings, the value is nil.

If the first binding is not a prefix command, all subsequent bindings from other minor modes are omitted, since they would be completely shadowed. Similarly, the list omits non-prefix bindings that follow prefix bindings.

The argument accept-defaults controls checking for default bindings, as in lookup-key (above).

Variable: meta-prefix-char

This variable is the meta-prefix character code. It is used when translating a meta character to a two-character sequence so it can be looked up in a keymap. For useful results, the value should be a prefix event (see section Prefix Keys). The default value is 27, which is the ASCII code for <ESC>.

As long as the value of meta-prefix-char remains 27, key lookup translates M-b into <ESC> b, which is normally defined as the backward-word command. However, if you set meta-prefix-char to 24, the code for C-x, then XEmacs will translate M-b into C-x b, whose standard binding is the switch-to-buffer command.

meta-prefix-char                    ; The default value.
     ⇒ 27
(key-binding "\M-b")
     ⇒ backward-word
?\C-x                               ; The print representation
     ⇒ 24                          ;   of a character.
(setq meta-prefix-char 24)
     ⇒ 24      
(key-binding "\M-b")
     ⇒ switch-to-buffer            ; Now, typing M-b is
                                    ;   like typing C-x b.

(setq meta-prefix-char 27)          ; Avoid confusion!
     ⇒ 27                          ; Restore the default value!

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.9 Changing Key Bindings

The way to rebind a key is to change its entry in a keymap. If you change a binding in the global keymap, the change is effective in all buffers (though it has no direct effect in buffers that shadow the global binding with a local one). If you change the current buffer’s local map, that usually affects all buffers using the same major mode. The global-set-key and local-set-key functions are convenient interfaces for these operations (see section Commands for Binding Keys). You can also use define-key, a more general function; then you must specify explicitly the map to change.

In writing the key sequence to rebind, it is good to use the special escape sequences for control and meta characters (@pxref{String Type}). The syntax ‘\C-’ means that the following character is a control character and ‘\M-’ means that the following character is a meta character. Thus, the string "\M-x" is read as containing a single M-x, "\C-f" is read as containing a single C-f, and "\M-\C-x" and "\C-\M-x" are both read as containing a single C-M-x. You can also use this escape syntax in vectors, as well as others that aren’t allowed in strings; one example is ‘[?\C-\H-x home]’. @xref{Character Type}.

The key definition and lookup functions accept an alternate syntax for event types in a key sequence that is a vector: you can use a list containing modifier names plus one base event (a character or function key name). For example, (control ?a) is equivalent to ?\C-a and (hyper control left) is equivalent to C-H-left.

One advantage of using a list to represent the event type is that the precise numeric codes for the modifier bits don’t appear in compiled files.

For the functions below, an error is signaled if keymap is not a keymap or if key is not a string or vector representing a key sequence. You can use event types (symbols) as shorthand for events that are lists.

Function: define-key keymap key binding

This function sets the binding for key in keymap. (If key is more than one event long, the change is actually made in another keymap reached from keymap.) The argument binding can be any Lisp object, but only certain types are meaningful. (For a list of meaningful types, see Key Lookup.) The value returned by define-key is binding.

Every prefix of key must be a prefix key (i.e., bound to a keymap) or undefined; otherwise an error is signaled.

If some prefix of key is undefined, then define-key defines it as a prefix key so that the rest of key may be defined as specified.

Here is an example that creates a sparse keymap and makes a number of bindings in it:

(setq map (make-sparse-keymap))
    ⇒ (keymap)
(define-key map "\C-f" 'forward-char)
    ⇒ forward-char
map
    ⇒ (keymap (6 . forward-char))
;; Build sparse submap for C-x and bind f in that.
(define-key map "\C-xf" 'forward-word)
    ⇒ forward-word
map
⇒ (keymap 
    (24 keymap                ; C-x
        (102 . forward-word)) ;      f
    (6 . forward-char))       ; C-f
;; Bind C-p to the ctl-x-map.
(define-key map "\C-p" ctl-x-map)
;; ctl-x-map
⇒ [nil … find-file … backward-kill-sentence] 
;; Bind C-f to foo in the ctl-x-map.
(define-key map "\C-p\C-f" 'foo)
⇒ 'foo
map
⇒ (keymap     ; Note foo in ctl-x-map.
    (16 keymap [nil … foo … backward-kill-sentence])
    (24 keymap 
        (102 . forward-word))
    (6 . forward-char))

Note that storing a new binding for C-p C-f actually works by changing an entry in ctl-x-map, and this has the effect of changing the bindings of both C-p C-f and C-x C-f in the default global map.

Function: substitute-key-definition olddef newdef keymap &optional oldmap

This function replaces olddef with newdef for any keys in keymap that were bound to olddef. In other words, olddef is replaced with newdef wherever it appears. The function returns nil.

For example, this redefines C-x C-f, if you do it in an XEmacs with standard bindings:

(substitute-key-definition 
 'find-file 'find-file-read-only (current-global-map))

If oldmap is non-nil, then its bindings determine which keys to rebind. The rebindings still happen in newmap, not in oldmap. Thus, you can change one map under the control of the bindings in another. For example,

(substitute-key-definition
  'delete-backward-char 'my-funny-delete
  my-map global-map)

puts the special deletion command in my-map for whichever keys are globally bound to the standard deletion command.

Here is an example showing a keymap before and after substitution:

(setq map '(keymap 
            (?1 . olddef-1) 
            (?2 . olddef-2) 
            (?3 . olddef-1)))
⇒ (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1))
(substitute-key-definition 'olddef-1 'newdef map)
⇒ nil
map
⇒ (keymap (49 . newdef) (50 . olddef-2) (51 . newdef))
Function: suppress-keymap keymap &optional nodigits

This function changes the contents of the full keymap keymap by making all the printing characters undefined. More precisely, it binds them to the command undefined. This makes ordinary insertion of text impossible. suppress-keymap returns nil.

If nodigits is nil, then suppress-keymap defines digits to run digit-argument, and - to run negative-argument. Otherwise it makes them undefined like the rest of the printing characters.

The suppress-keymap function does not make it impossible to modify a buffer, as it does not suppress commands such as yank and quoted-insert. To prevent any modification of a buffer, make it read-only (@pxref{Read Only Buffers}).

Since this function modifies keymap, you would normally use it on a newly created keymap. Operating on an existing keymap that is used for some other purpose is likely to cause trouble; for example, suppressing global-map would make it impossible to use most of XEmacs.

Most often, suppress-keymap is used to initialize local keymaps of modes such as Rmail and Dired where insertion of text is not desirable and the buffer is read-only. Here is an example taken from the file ‘emacs/lisp/dired.el’, showing how the local keymap for Dired mode is set up:

  …
  (setq dired-mode-map (make-keymap))
  (suppress-keymap dired-mode-map)
  (define-key dired-mode-map "r" 'dired-rename-file)
  (define-key dired-mode-map "\C-d" 'dired-flag-file-deleted)
  (define-key dired-mode-map "d" 'dired-flag-file-deleted)
  (define-key dired-mode-map "v" 'dired-view-file)
  (define-key dired-mode-map "e" 'dired-find-file)
  (define-key dired-mode-map "f" 'dired-find-file)
  …

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.10 Commands for Binding Keys

This section describes some convenient interactive interfaces for changing key bindings. They work by calling define-key.

People often use global-set-key in their ‘.emacs’ file for simple customization. For example,

(global-set-key "\C-x\C-\\" 'next-line)

or

(global-set-key [?\C-x ?\C-\\] 'next-line)

or

(global-set-key [(control ?x) (control ?\\)] 'next-line)

redefines C-x C-\ to move down a line.

(global-set-key [M-mouse-1] 'mouse-set-point)

redefines the first (leftmost) mouse button, typed with the Meta key, to set point where you click.

Command: global-set-key key definition

This function sets the binding of key in the current global map to definition.

(global-set-key key definition)
≡
(define-key (current-global-map) key definition)
Command: global-unset-key key

This function removes the binding of key from the current global map.

One use of this function is in preparation for defining a longer key that uses key as a prefix—which would not be allowed if key has a non-prefix binding. For example:

(global-unset-key "\C-l")
    ⇒ nil
(global-set-key "\C-l\C-l" 'redraw-display)
    ⇒ nil

This function is implemented simply using define-key:

(global-unset-key key)
≡
(define-key (current-global-map) key nil)
Command: local-set-key key definition

This function sets the binding of key in the current local keymap to definition.

(local-set-key key definition)
≡
(define-key (current-local-map) key definition)
Command: local-unset-key key

This function removes the binding of key from the current local map.

(local-unset-key key)
≡
(define-key (current-local-map) key nil)

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.11 Scanning Keymaps

This section describes functions used to scan all the current keymaps for the sake of printing help information.

Function: accessible-keymaps keymap &optional prefix

This function returns a list of all the keymaps that can be accessed (via prefix keys) from keymap. The value is an association list with elements of the form (key . map), where key is a prefix key whose definition in keymap is map.

The elements of the alist are ordered so that the key increases in length. The first element is always ("" . keymap), because the specified keymap is accessible from itself with a prefix of no events.

If prefix is given, it should be a prefix key sequence; then accessible-keymaps includes only the submaps whose prefixes start with prefix. These elements look just as they do in the value of (accessible-keymaps); the only difference is that some elements are omitted.

In the example below, the returned alist indicates that the key <ESC>, which is displayed as ‘^[’, is a prefix key whose definition is the sparse keymap (keymap (83 . center-paragraph) (115 . foo)).

(accessible-keymaps (current-local-map))
⇒(("" keymap 
      (27 keymap   ; Note this keymap for <ESC> is repeated below.
          (83 . center-paragraph)
          (115 . center-line))
      (9 . tab-to-tab-stop))
   ("^[" keymap 
    (83 . center-paragraph) 
    (115 . foo)))

These are not all the keymaps you would see in an actual case.

Function: where-is-internal command &optional keymap firstonly noindirect

This function returns a list of key sequences (of any length) that are bound to command in a set of keymaps.

The argument command can be any object; it is compared with all keymap entries using eq.

If keymap is nil, then the maps used are the current active keymaps, disregarding overriding-local-map (that is, pretending its value is nil). If keymap is non-nil, then the maps searched are keymap and the global keymap.

Usually it’s best to use overriding-local-map as the expression for keymap. Then where-is-internal searches precisely the keymaps that are active. To search only the global map, pass (keymap) (an empty keymap) as keymap.

If firstonly is non-ascii, then the value is a single string representing the first key sequence found, rather than a list of all possible key sequences. If firstonly is t, then the value is the first key sequence, except that key sequences consisting entirely of ASCII characters (or meta variants of ASCII characters) are preferred to all other key sequences.

If noindirect is non-nil, where-is-internal doesn’t follow indirect keymap bindings. This makes it possible to search for an indirect definition itself.

This function is used by where-is (see Help in The XEmacs Reference Manual).

(where-is-internal 'describe-function)
    ⇒ ("\^hf" "\^hd")
Command: describe-bindings prefix

This function creates a listing of all defined keys and their definitions. It writes the listing in a buffer named ‘*Help*’ and displays it in a window.

If prefix is non-nil, it should be a prefix key; then the listing includes only keys that start with prefix.

The listing describes meta characters as <ESC> followed by the corresponding non-meta character.

When several characters with consecutive ASCII codes have the same definition, they are shown together, as ‘firstchar..lastchar’. In this instance, you need to know the ASCII codes to understand which characters this means. For example, in the default global map, the characters ‘<SPC> .. ~’ are described by a single line. <SPC> is ASCII 32, ~ is ASCII 126, and the characters between them include all the normal printing characters, (e.g., letters, digits, punctuation, etc.); all these characters are bound to self-insert-command.


[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated on December 6, 2024 using texi2html 5.0.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ < ] Back Previous section in reading order 1.2.2
[ Up ] Up Up section 1.2
[ > ] Forward Next section in reading order 1.2.4
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document  
[Contents] Contents Table of contents  
[Index] Index Index  
[ ? ] About About (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:


This document was generated on December 6, 2024 using texi2html 5.0.