Next: , Previous: , Up: Keymaps   [Contents][Index]


26.7 Active Keymaps

SXEmacs 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 or overriding-terminal-local-map overrides it. Extents and text properties can specify an alternative local map for certain parts of the buffer; see Extents and Events.

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 and overriding-terminal-local-map, if non-nil, specify other local keymaps that override 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. SXEmacs searches these maps one by one, in order of decreasing precedence, until it finds a binding in one of the maps.

More specifically:

For key-presses, the order of keymaps searched is:

For mouse-clicks, the order of keymaps searched is:

Note that if overriding-local-map or overriding-terminal-local-map is non-nil, only those two maps and the current global map are searched.

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. See Intro to Minibuffers.

See Standard Keymaps, for a list of standard keymaps.

Function: current-keymaps &optional event-or-keys

This function returns a list of the current keymaps that will be searched for bindings. This lists keymaps such as the current local map and the minor-mode maps, but does not list the parents of those keymaps. event-or-keys controls which keymaps will be listed. If event-or-keys is a mouse event (or a vector whose last element is a mouse event), the keymaps for that mouse event will be listed. Otherwise, the keymaps for key presses will be listed.

Variable: global-map

This variable contains the default global keymap that maps SXEmacs 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 global-map 639 entries 0x221>
Function: current-local-map &optional buffer

This function returns buffer’s local keymap, or nil if it has none. buffer defaults to the current buffer.

In the following example, the keymap for the ‘*scratch*’ buffer (using Lisp Interaction mode) has a number of entries, including one prefix key, C-x.

(current-local-map)
⇒ #<keymap lisp-interaction-mode-map 5 entries 0x558>
(describe-bindings-internal (current-local-map))
⇒  ; Inserted into the buffer:
backspace       backward-delete-char-untabify
linefeed        eval-print-last-sexp
delete          delete-char
C-j             eval-print-last-sexp
C-x             << Prefix Command >>
M-tab           lisp-complete-symbol
M-;             lisp-indent-for-comment
M-C-i           lisp-complete-symbol
M-C-q           indent-sexp
M-C-x           eval-defun
Alt-backspace   backward-kill-sexp
Alt-delete      kill-sexp
C-x x           edebug-defun
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 &optional buffer

This function makes keymap the new local keymap of buffer. buffer defaults to 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. See 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 Keymaps and Minor Modes, for more information about minor modes.

Variable: modeline-map

This variable holds the keymap consulted for mouse-clicks on the modeline of a window. This variable may be buffer-local; its value will be looked up in the buffer of the window whose modeline was clicked upon.

Variable: toolbar-map

This variable holds the keymap consulted for mouse-clicks over a toolbar.

Variable: mouse-grabbed-buffer

If non-nil, a buffer which should be consulted first for all mouse activity. When a mouse-click is processed, it will first be looked up in the local-map of this buffer, and then through the normal mechanism if there is no binding for that click. This buffer’s value of mode-motion-hook will be consulted instead of the mode-motion-hook of the buffer of the window under the mouse. You should bind this, not set it.

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-terminal-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, but for the selected console only. (In other words, this variable is always console-local; putting a keymap here only applies to keystrokes coming from the selected console. See Consoles and Devices.) This keymap, if any, overrides all other maps that would have been active, except for the current global map.


Next: , Previous: , Up: Keymaps   [Contents][Index]