Functions for Key Lookup
Here are the functions and variables pertaining to key lookup.
- Function: keymap-lookup keymap key &optional accept-defaults no-remap position
-
This function returns the definition of key in keymap. All the other functions described in this chapter that look up keys use
keymap-lookup. Here are examples:(keymap-lookup (current-global-map) "C-x C-f") ⇒ find-file(keymap-lookup (current-global-map) "C-x C-f 1 2 3 4 5") ⇒ 2If the string or vector key is not a valid key sequence according to the prefix keys specified in keymap, it must be too long and have extra events at the end that do not fit into a single key sequence. 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, thenkeymap-lookupconsiders default bindings as well as bindings for the specific events in key. Otherwise,keymap-lookupreports only bindings for the specific sequence key, ignoring default bindings except when you explicitly ask about them. (To do this, supplytas an element of key; see Format of Keymaps.)If key contains a meta character (not a function key), 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.(keymap-lookup (current-global-map) "M-f") ⇒ forward-word(keymap-lookup (current-global-map) "ESC f") ⇒ forward-wordThe keymap argument can be
nil, meaning to look up key in the current keymaps (as returned bycurrent-active-maps, see Active Keymaps); or it can be a keymap or a list of keymaps, meaning to look up key only in the specified keymaps.Unlike
read-key-sequence, this function does not modify the specified events in ways that discard information (see Key Sequence Input). In particular, it does not convert letters to lower case and it does not change drag events to clicks.Like the normal command loop,
keymap-lookupwill remap the command resulting from looking up key by looking up the command in the current keymaps. However, if the optional third argument no-remap is non-nil,keymap-lookupreturns the command without remapping.If the optional argument position is non-
nil, it specifies a mouse position as returned byevent-startandevent-end, and the lookup occurs in the keymaps associated with that position, instead of in keymap. position can also be a number or a marker, in which case it is interpreted as a buffer position, and the function uses the keymap properties at that position instead of at point.
- Command: undefined
Used in keymaps to undefine keys. It calls
ding, but does not cause an error.
- Function: keymap-local-lookup key &optional accept-defaults
-
This function returns the binding for key in the current local keymap, or
nilif it is undefined there.The argument accept-defaults controls checking for default bindings, as in
keymap-lookup(above).
- Function: keymap-global-lookup key &optional accept-defaults
-
This function returns the binding for command key in the current global keymap, or
nilif it is undefined there.The argument accept-defaults controls checking for default bindings, as in
keymap-lookup(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 isnil.If the first binding found is not a prefix definition (a keymap or a symbol defined as a keymap), 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
keymap-lookup(above).
- User Option: meta-prefix-char
-
This variable is the meta-prefix character code. It is used for 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 Prefix Keys). The default value is 27, which is the ASCII code for ESC.
As long as the value of
meta-prefix-charremains 27, key lookup translates M-b into ESC b, which is normally defined as thebackward-wordcommand. However, if you were to setmeta-prefix-charto 24, the code for C-x, then Emacs will translate M-b into C-x b, whose standard binding is theswitch-to-buffercommand. (Don’t actually do this!) Here is an illustration of what would happen: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!This translation of one event into two happens only for characters, not for other kinds of input events. Thus, M-F1, a function key, is not converted into ESC F1.
Copyright © 1990-1996, 1998-2026 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Functions-for-Key-Lookup.html