Next: , Previous: Prefix Keys, Up: Keymaps

22.7 Active Keymaps

Emacs normally contains many keymaps; at any given time, just a few of them are active, meaning that they participate in the interpretation of user input. All the active keymaps are used together to determine what command to execute when a key is entered.

Normally the active keymaps are the keymap property keymap, the keymaps of any enabled minor modes, the current buffer's local keymap, and the global keymap, in that order. Emacs searches for each input key sequence in all these keymaps. See Searching Keymaps, for more details of this procedure.

When the key sequence starts with a mouse event, the active keymaps are determined based on the position in that event. If the event happened on a string embedded with a display, before-string, or after-string property (see Special Properties), the non-nil map properties of the string override those of the buffer (if the underlying buffer text contains map properties in its text properties or overlays, they are ignored).

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. The local-map text or overlay property can specify an alternative local keymap for certain parts of the buffer; see Special Properties.

Each minor mode can have a keymap; if it does, the keymap is active when the minor mode is enabled. Modes for emulation can specify additional active keymaps through the variable emulation-mode-map-alists.

The highest precedence normal keymap comes from the keymap text or overlay property. If that is non-nil, it is the first keymap to be processed, in normal circumstances. Next comes any keymap added by the function set-temporary-overlay-map. See Controlling Active Maps.

However, there are also special ways for programs to substitute other keymaps for some of those. The variable overriding-local-map, if non-nil, specifies a keymap that replaces all the usual active keymaps except the global keymap. Another way to do this is with overriding-terminal-local-map; it operates on a per-terminal basis. These variables are documented below.

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 and some other major modes exist even if they have not yet been used. These local keymaps are the values of variables such as lisp-mode-map. For most major 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. See Intro to Minibuffers.

Emacs has other keymaps that are used in a different way—translating events within read-key-sequence. See Translation Keymaps.

See Standard Keymaps, for a list of some standard keymaps.

— Function: current-active-maps &optional olp position

This returns the list of active keymaps that would be used by the command loop in the current circumstances to look up a key sequence. Normally it ignores overriding-local-map and overriding-terminal-local-map, but if olp is non-nil then it pays attention to them. position can optionally be either an event position as returned by event-start or a buffer position, and may change the keymaps as described for key-binding.

— Function: key-binding key &optional accept-defaults no-remap position

This function returns the binding for key according to the current 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 (see Functions for Key Lookup).

When commands are remapped (see Remapping Commands), key-binding normally processes command remappings so as to return the remapped command that will actually be executed. However, if no-remap is non-nil, key-binding ignores remappings and returns the binding directly specified for key.

If key starts with a mouse event (perhaps following a prefix event), the maps to be consulted are determined based on the event's position. Otherwise, they are determined based on the value of point. However, you can override either of them by specifying position. If position is non-nil, it should be either a buffer position or an event position like the value of event-start. Then the maps consulted are determined based on position.

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

          (key-binding "\C-x\C-f")
              ⇒ find-file