[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 to another keymap or to a command. When an event type 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.
22.1 Keymap Terminology Definitions of terms pertaining to keymaps. 22.2 Format of Keymaps What a keymap looks like as a Lisp object. 22.3 Creating Keymaps Functions to create and copy keymaps. 22.4 Inheritance and Keymaps How one keymap can inherit the bindings of another keymap. 22.5 Prefix Keys Defining a key with a keymap as its definition. 22.6 Active Keymaps Each buffer has a local keymap to override the standard (global) bindings. A minor mode can also override them. 22.7 Key Lookup How extracting elements from keymaps works. 22.8 Functions for Key Lookup How to request key lookup. 22.9 Changing Key Bindings Redefining a key in a keymap. 22.10 Commands for Binding Keys Interactive interfaces for redefining keys. 22.11 Scanning Keymaps Looking through all keymaps, for printing help. 22.12 Menu Keymaps Defining a menu as a keymap.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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, Emacs can get the event's definition. Events include characters, function keys, and mouse actions (see section 21.6 Input 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 22.5 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 one key sequence. In other words, removing one or more events from the end of any valid key sequence must always yield a prefix key. For example, C-f C-n is not a key sequence; C-f is not a prefix key, so a longer sequence starting with C-f cannot be a key sequence.
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 22.6 Active Keymaps, for details.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A keymap is a list whose CAR is the symbol keymap
. The
remaining elements of the list define the key bindings of the keymap.
Use the function keymapp
(see below) to test whether an object is
a keymap.
Several kinds of elements may appear in a keymap, after the symbol
keymap
that begins it:
(type . binding)
(t . binding)
vector
When a keymap contains a vector, it always defines a binding for each
ASCII character, even if the vector contains nil
for that
character. Such a binding of nil
overrides any default key
binding in the keymap, for ASCII characters. However, default
bindings are still meaningful for events other than ASCII
characters. A binding of nil
does not override
lower-precedence keymaps; thus, if the local map gives a binding of
nil
, Emacs uses the binding from the global map.
string
Keymaps do not directly record bindings for the meta characters.
Instead, meta characters are regarded for purposes of key lookup as
sequences of two characters, the first of which is ESC (or
whatever is currently the value of meta-prefix-char
). Thus, the
key M-a is internally represented as ESC a, and its
global binding is found at the slot for a in esc-map
(see section 22.5 Prefix Keys).
This conversion applies only to characters, not to function keys or other input events; thus, M-end has nothing to do with ESC end.
Here as an example is the local keymap for Lisp mode, a sparse keymap. It defines bindings for DEL and TAB, plus C-c C-l, M-C-q, and M-C-x.
lisp-mode-map => (keymap ;; TAB (9 . lisp-indent-line) ;; DEL (127 . backward-delete-char-untabify) (3 keymap ;; C-c C-l (12 . run-lisp)) (27 keymap ;; M-C-q, treated as ESC C-q (17 . indent-sexp) ;; M-C-x, treated as ESC C-x (24 . lisp-send-defun))) |
t
if object is a keymap, nil
otherwise. More precisely, this function tests for a list whose
CAR is keymap
.
(keymapp '(keymap)) => t (keymapp (current-global-map)) => t |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here we describe the functions for creating keymaps.
nil
, and does not bind any other kind of event.
(make-keymap) => (keymap [nil nil nil ... nil nil]) |
If you specify prompt, that becomes the overall prompt string for the keymap. The prompt string should be provided for menu keymaps (see section 22.12.1 Defining Menus).
make-keymap
,
and does not bind any events. The argument prompt specifies a
prompt string, as in make-keymap
.
(make-sparse-keymap) => (keymap) |
(setq map (copy-keymap (current-local-map))) => (keymap ;; (This implements meta characters.) (27 keymap (83 . center-paragraph) (115 . center-line)) (9 . tab-to-tab-stop)) (eq map (current-local-map)) => nil (equal map (current-local-map)) => t |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A keymap can inherit the bindings of another keymap, which we call the parent keymap. Such a keymap looks like this:
(keymap bindings... . parent-keymap) |
The effect is that this keymap inherits all the bindings of parent-keymap, whatever they may be at the time a key is looked up, but can add to them or override them with bindings.
If you change the bindings in parent-keymap using define-key
or other key-binding functions, these changes are visible in the
inheriting keymap unless shadowed by bindings. The converse is
not true: if you use define-key
to change the inheriting keymap,
that affects bindings, but has no effect on parent-keymap.
The proper way to construct a keymap with a parent is to use
set-keymap-parent
; if you have code that directly constructs a
keymap with a parent, please convert the program to use
set-keymap-parent
instead.
keymap-parent
returns nil
.
nil
, this function gives
keymap no parent at all.
If keymap has submaps (bindings for prefix keys), they too receive new parent keymaps that reflect what parent specifies for those prefix keys.
Here is an example showing how to make a keymap that inherits
from text-mode-map
:
(let ((map (make-sparse-keymap))) (set-keymap-parent map text-mode-map) map) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A prefix key is a key sequence whose binding is a keymap. The
keymap defines what to do with key sequences that extend 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
. This keymap defines
bindings for key sequences starting with C-x.
Some of the standard Emacs prefix keys use keymaps that are also found in Lisp variables:
esc-map
is the global keymap for the ESC prefix key. Thus,
the global definitions of all meta characters are actually found here.
This map is also the function definition of ESC-prefix
.
help-map
is the global keymap for the C-h prefix key.
mode-specific-map
is the global keymap for the prefix key
C-c. This map is actually global, not mode-specific, but its name
provides useful information about C-c in the output of C-h b
(display-bindings
), since the main use of this prefix key is for
mode-specific bindings.
ctl-x-map
is the global keymap used for the C-x prefix key.
This map is found via the function cell of the symbol
Control-X-prefix
.
mule-keymap
is the global keymap used for the C-x RET
prefix key.
ctl-x-4-map
is the global keymap used for the C-x 4 prefix
key.
ctl-x-5-map
is the global keymap used for the C-x 5 prefix
key.
2C-mode-map
is the global keymap used for the C-x 6 prefix
key.
vc-prefix-map
is the global keymap used for the C-x v prefix
key.
facemenu-keymap
is the global keymap used for the M-g
prefix key.
The keymap binding of a prefix key is used for looking up the event
that follows 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 cell holds 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 22.6 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 |
symbol
.
This function also sets symbol as a variable, with the keymap as
its value. But if mapvar is non-nil
, it sets mapvar
as a variable instead.
If prompt is non-nil
, that becomes the overall prompt
string for the keymap. The prompt string should be given for menu keymaps
(see section 22.12.1 Defining Menus).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Emacs 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 32.19.4 Properties with Special Meanings.
Each minor mode can 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. Emacs searches these maps one by one, in order of decreasing precedence, until it finds a binding in one of the maps. The procedure for searching a single keymap is called key lookup; see 22.7 Key Lookup.
Normally, Emacs first searches for the key in the minor mode maps, in
the order specified by minor-mode-map-alist
; if they do not
supply a binding for the key, Emacs searches the local map; if that too
has no binding, Emacs then searches the global map. However, if
overriding-local-map
is non-nil
, Emacs searches that map
first, before the global map.
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 maps 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 section 20.1 Introduction to Minibuffers.
Emacs has other keymaps that are used in a different way--translating
events within read-key-sequence
. See section 40.8.2 Translating Input Events.
See section H. Standard Keymaps, for a list of standard keymaps.
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.
global-map
unless you change one or the
other.
(current-global-map) => (keymap [set-mark-command beginning-of-line ... delete-backward-char]) |
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))) |
nil
.
It is very unusual to change the global keymap.
nil
, then the buffer has no local
keymap. use-local-map
returns nil
. Most major mode
commands use this function.
(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 section 23.2.2 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. The CDR can be either a keymap (a list) or a symbol whose
function definition is a keymap.
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 23.2.2 Keymaps and Minor Modes, for more information about minor
modes. See also minor-mode-key-binding
(see section 22.8 Functions for Key Lookup).
minor-mode-map-alist
: (variable
. keymap)
.
If a variable appears as an element of
minor-mode-overriding-map-alist
, the map specified by that
element totally replaces any map specified for the same variable in
minor-mode-map-alist
.
minor-mode-overriding-map-alist
is automatically buffer-local in
all buffers.
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.
nil
, this variable holds a keymap to use instead of
overriding-local-map
, the buffer's local keymap and all the minor
mode keymaps.
This variable is always local to the current terminal and cannot be buffer-local. See section 29.2 Multiple Displays. It is used to implement incremental search mode.
nil
, the value of
overriding-local-map
or overriding-terminal-local-map
can
affect the display of the menu bar. The default value is nil
, so
those map variables have no effect on the menu bar.
Note that these two map variables do affect the execution of key sequences entered using the menu bar, even if they do not affect the menu bar display. So if a menu bar key sequence comes in, you should clear the variables before looking up and executing that key sequence. Modes that use the variables would typically do this anyway; normally they respond to events that they do not handle by "unreading" them and exiting.
read-event
. See section 21.8 Special Events.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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). See section 21.6 Input Events. Such
a "key-sequence" is insufficient for command-execute
to run,
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 table 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
, then the list
is a keymap, and is treated as a keymap (see above).
lambda
, then the list is a
lambda expression. This is presumed to be a command, and is treated as
such (see above).
(othermap . othertype) |
When key lookup encounters an indirect entry, it looks up instead the binding of othertype in othermap and uses that.
This feature permits you to define one key as an alias for another key.
For example, an entry whose CAR is the keymap called esc-map
and whose CDR is 32 (the code for SPC) means, "Use the global
binding of Meta-SPC, whatever that may be."
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
(see section 21.3 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.
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] | [ ? ] |
Here are the functions and variables pertaining to key lookup.
lookup-key
. Here are examples:
(lookup-key (current-global-map) "\C-x\C-f") => find-file (lookup-key (current-global-map) "\C-x\C-f12345") => 2 |
If 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
, 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 22.2 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.
(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 (see section 21.7.1 Key Sequence Input). In particular, it does not convert letters to lower case and
it does not change drag events to clicks.
ding
, but does
not cause an error.
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 |
nil
if it is undefined there.
The argument accept-defaults controls checking for default bindings,
as in lookup-key
(above).
nil
if it is undefined there.
The argument accept-defaults controls checking for default bindings,
as in lookup-key
(above).
(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 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 lookup-key
(above).
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 were to set
meta-prefix-char
to 24, the code for C-x, then Emacs will
translate M-b into C-x b, whose standard binding is the
switch-to-buffer
command. (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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 22.10 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 (see section 2.3.8 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]'. See section 2.3.3 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 such lists 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.
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 can be defined as specified.
If there was previously no binding for key in keymap, the new binding is added at the beginning of keymap. The order of bindings in a keymap makes no difference in most cases, but it does matter for menu keymaps (see section 22.12 Menu Keymaps).
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 |
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.
nil
.
For example, this redefines C-x C-f, if you do it in an Emacs with standard bindings:
(substitute-key-definition 'find-file 'find-file-read-only (current-global-map)) |
If oldmap is non-nil
, that changes the behavior of
substitute-key-definition
: the bindings in oldmap determine
which keys to rebind. The rebindings still happen in keymap, 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)) |
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 (see section 27.7 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 Emacs.
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] | [ ? ] |
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 init files
(see section 40.1.2 The Init File, `.emacs') 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.
Be careful when using non-ASCII text characters in Lisp specifications of keys to bind. If these are read as multibyte text, as they usually will be in a Lisp file (see section 15.3 Loading Non-ASCII Characters), you must type the keys as multibyte too. For instance, if you use this:
(global-set-key "ö" 'my-function) ; bind o-umlaut |
or
(global-set-key ?ö 'my-function) ; bind o-umlaut |
and your language environment is multibyte Latin-1, these commands actually bind the multibyte character with code 2294, not the unibyte Latin-1 character with code 246 (M-v). In order to use this binding, you need to enter the multibyte Latin-1 character as keyboard input. One way to do this is by using an appropriate input method (see section `Input Methods' in The GNU Emacs Manual).
If you want to use a unibyte character in the key binding, you can
construct the key sequence string using multibyte-char-to-unibyte
or string-make-unibyte
(see section 33.2 Converting Text Representations).
(global-set-key key definition) == (define-key (current-global-map) key definition) |
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) |
(local-set-key key definition) == (define-key (current-local-map) key definition) |
(local-unset-key key) == (define-key (current-local-map) key nil) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes functions used to scan all the current keymaps for the sake of printing help information.
(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))) |
In the following example, C-h is a prefix key that uses a sparse
keymap starting with (keymap (118 . describe-variable)...)
.
Another prefix, C-x 4, uses a keymap which is also the value of
the variable ctl-x-4-map
. The event mode-line
is one of
several dummy events used as prefixes for mouse actions in special parts
of a window.
(accessible-keymaps (current-global-map)) => (("" keymap [set-mark-command beginning-of-line ... delete-backward-char]) ("^H" keymap (118 . describe-variable) ... (8 . help-for-help)) ("^X" keymap [x-flush-mouse-queue ... backward-kill-sentence]) ("^[" keymap [mark-sexp backward-sexp ... backward-kill-word]) ("^X4" keymap (15 . display-buffer) ...) ([mode-line] keymap (S-mouse-2 . mouse-split-window-horizontally) ...)) |
These are not all the keymaps you would see in actuality.
where-is
command
(see section `Help' in The GNU Emacs Manual). It 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. If keymap
is a list of keymaps, only those keymaps are searched.
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.
(where-is-internal 'describe-function) => ("\^hf" "\^hd") |
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
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A keymap can define a menu as well as bindings for keyboard keys and mouse button. Menus are usually actuated with the mouse, but they can work with the keyboard also.
22.12.1 Defining Menus How to make a keymap that defines a menu. 22.12.2 Menus and the Mouse How users actuate the menu with the mouse. 22.12.3 Menus and the Keyboard How they actuate it with the keyboard. 22.12.4 Menu Example Making a simple menu. 22.12.5 The Menu Bar How to customize the menu bar. 22.12.6 Tool bars A tool bar is a row of images. 22.12.7 Modifying Menus How to add new items to a menu.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A keymap is suitable for menu use if it has an overall prompt string, which is a string that appears as an element of the keymap. (See section 22.2 Format of Keymaps.) The string should describe the purpose of the menu's commands. Emacs displays the overall prompt string as the menu title in some cases, depending on the toolkit (if any) used for displaying menus.(6) Keyboard menus also display the overall prompt string.
The easiest way to construct a keymap with a prompt string is to specify
the string as an argument when you call make-keymap
,
make-sparse-keymap
or define-prefix-command
(see section 22.3 Creating Keymaps).
The order of items in the menu is the same as the order of bindings in
the keymap. Since define-key
puts new bindings at the front, you
should define the menu items starting at the bottom of the menu and
moving to the top, if you care about the order. When you add an item to
an existing menu, you can specify its position in the menu using
define-key-after
(see section 22.12.7 Modifying Menus).
22.12.1.1 Simple Menu Items A simple kind of menu key binding, limited in capabilities. 22.12.1.2 Extended Menu Items More powerful menu item definitions let you specify keywords to enable various features. 22.12.1.3 Menu Separators Drawing a horizontal line through a menu. 22.12.1.4 Alias Menu Items Using command aliases in menu items.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The simpler and older way to define a menu keymap binding looks like this:
(item-string . real-binding) |
The CAR, item-string, is the string to be displayed in the menu. It should be short--preferably one to three words. It should describe the action of the command it corresponds to.
You can also supply a second string, called the help string, as follows:
(item-string help . real-binding) |
help specifies a "help-echo" string to display while the mouse
is on that item in the same way as help-echo
text properties
(see Help display).
As far as define-key
is concerned, item-string and
help-string are part of the event's binding. However,
lookup-key
returns just real-binding, and only
real-binding is used for executing the key.
If real-binding is nil
, then item-string appears in
the menu but cannot be selected.
If real-binding is a symbol and has a non-nil
menu-enable
property, that property is an expression that
controls whether the menu item is enabled. Every time the keymap is
used to display a menu, Emacs evaluates the expression, and it enables
the menu item only if the expression's value is non-nil
. When a
menu item is disabled, it is displayed in a "fuzzy" fashion, and
cannot be selected.
The menu bar does not recalculate which items are enabled every time you
look at a menu. This is because the X toolkit requires the whole tree
of menus in advance. To force recalculation of the menu bar, call
force-mode-line-update
(see section 23.3 Mode Line Format).
You've probably noticed that menu items show the equivalent keyboard key sequence (if any) to invoke the same command. To save time on recalculation, menu display caches this information in a sublist in the binding, like this:
(item-string [help-string] (key-binding-data) . real-binding) |
Don't put these sublists in the menu item yourself; menu display calculates them automatically. Don't mention keyboard equivalents in the item strings themselves, since that is redundant.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An extended-format menu item is a more flexible and also cleaner
alternative to the simple format. It consists of a list that starts
with the symbol menu-item
. To define a non-selectable string,
the item looks like this:
(menu-item item-name) |
A string starting with two or more dashes specifies a separator line; see 22.12.1.3 Menu Separators.
To define a real menu item which can be selected, the extended format item looks like this:
(menu-item item-name real-binding . item-property-list) |
Here, item-name is an expression which evaluates to the menu item string. Thus, the string need not be a constant. The third element, real-binding, is the command to execute. The tail of the list, item-property-list, has the form of a property list which contains other information. Here is a table of the properties that are supported:
:enable form
nil
means yes). If the item is not enabled,
you can't really click on it.
:visible form
nil
means yes). If the item
does not appear, then the menu is displayed as if this item were
not defined at all.
:help help
help-echo
text properties (see Help display).
Note that this must be a constant string, unlike the help-echo
property for text and overlays.
:button (type . selected)
:toggle
or
:radio
. The CDR, selected, should be a form; the
result of evaluating it says whether this button is currently selected.
A toggle is a menu item which is labeled as either "on" or "off"
according to the value of selected. The command itself should
toggle selected, setting it to t
if it is nil
,
and to nil
if it is t
. Here is how the menu item
to toggle the debug-on-error
flag is defined:
(menu-item "Debug on Error" toggle-debug-on-error :button (:toggle . (and (boundp 'debug-on-error) debug-on-error))) |
This works because toggle-debug-on-error
is defined as a command
which toggles the variable debug-on-error
.
Radio buttons are a group of menu items, in which at any time one and only one is "selected." There should be a variable whose value says which one is selected at any time. The selected form for each radio button in the group should check whether the variable has the right value for selecting that button. Clicking on the button should set the variable so that the button you clicked on becomes selected.
:key-sequence key-sequence
If you specify the wrong key sequence, it has no effect; before Emacs displays key-sequence in the menu, it verifies that key-sequence is really equivalent to this menu item.
:key-sequence nil
However, if the user has rebound this item's definition to a key
sequence, Emacs ignores the :keys
property and finds the keyboard
equivalent anyway.
:keys string
:filter filter-fn
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A menu separator is a kind of menu item that doesn't display any text--instead, it divides the menu into subparts with a horizontal line. A separator looks like this in the menu keymap:
(menu-item separator-type) |
where separator-type is a string starting with two or more dashes.
In the simplest case, separator-type consists of only dashes.
That specifies the default kind of separator. (For compatibility,
""
and -
also count as separators.)
Starting in Emacs 21, certain other values of separator-type specify a different style of separator. Here is a table of them:
"--no-line"
"--space"
"--single-line"
"--double-line"
"--single-dashed-line"
"--double-dashed-line"
"--shadow-etched-in"
"--shadow-etched-out"
"--shadow-etched-in-dash"
"--shadow-etched-out-dash"
"--shadow-double-etched-in"
"--shadow-double-etched-out"
"--shadow-double-etched-in-dash"
"--shadow-double-etched-out-dash"
You can also give these names in another style, adding a colon after
the double-dash and replacing each single dash with capitalization of
the following word. Thus, "--:singleLine"
, is equivalent to
"--single-line"
.
Some systems and display toolkits don't really handle all of these separator types. If you use a type that isn't supported, the menu displays a similar kind of separator that is supported.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Sometimes it is useful to make menu items that use the "same"
command but with different enable conditions. The best way to do this
in Emacs now is with extended menu items; before that feature existed,
it could be done by defining alias commands and using them in menu
items. Here's an example that makes two aliases for
toggle-read-only
and gives them different enable conditions:
(defalias 'make-read-only 'toggle-read-only) (put 'make-read-only 'menu-enable '(not buffer-read-only)) (defalias 'make-writable 'toggle-read-only) (put 'make-writable 'menu-enable 'buffer-read-only) |
When using aliases in menus, often it is useful to display the
equivalent key bindings for the "real" command name, not the aliases
(which typically don't have any key bindings except for the menu
itself). To request this, give the alias symbol a non-nil
menu-alias
property. Thus,
(put 'make-read-only 'menu-alias t) (put 'make-writable 'menu-alias t) |
causes menu items for make-read-only
and make-writable
to
show the keyboard bindings for toggle-read-only
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The usual way to make a menu keymap produce a menu is to make it the definition of a prefix key. (A Lisp program can explicitly pop up a menu and receive the user's choice--see 29.15 Pop-Up Menus.)
If the prefix key ends with a mouse event, Emacs handles the menu keymap by popping up a visible menu, so that the user can select a choice with the mouse. When the user clicks on a menu item, the event generated is whatever character or symbol has the binding that brought about that menu item. (A menu item may generate a series of events if the menu has multiple levels or comes from the menu bar.)
It's often best to use a button-down event to trigger the menu. Then the user can select a menu item by releasing the button.
A single keymap can appear as multiple menu panes, if you explicitly arrange for this. The way to do this is to make a keymap for each pane, then create a binding for each of those maps in the main keymap of the menu. Give each of these bindings an item string that starts with `@'. The rest of the item string becomes the name of the pane. See the file `lisp/mouse.el' for an example of this. Any ordinary bindings with `@'-less item strings are grouped into one pane, which appears along with the other panes explicitly created for the submaps.
X toolkit menus don't have panes; instead, they can have submenus. Every nested keymap becomes a submenu, whether the item string starts with `@' or not. In a toolkit version of Emacs, the only thing special about `@' at the beginning of an item string is that the `@' doesn't appear in the menu item.
You can also produce multiple panes or submenus from separate keymaps. The full definition of a prefix key always comes from merging the definitions supplied by the various active keymaps (minor mode, local, and global). When more than one of these keymaps is a menu, each of them makes a separate pane or panes (when Emacs does not use an X-toolkit) or a separate submenu (when using an X-toolkit). See section 22.6 Active Keymaps.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When a prefix key ending with a keyboard event (a character or function key) has a definition that is a menu keymap, the user can use the keyboard to choose a menu item.
Emacs displays the menu's overall prompt string followed by the
alternatives (the item strings of the bindings) in the echo area. If
the bindings don't all fit at once, the user can type SPC to see
the next line of alternatives. Successive uses of SPC eventually
get to the end of the menu and then cycle around to the beginning. (The
variable menu-prompt-more-char
specifies which character is used
for this; SPC is the default.)
When the user has found the desired alternative from the menu, he or she should type the corresponding character--the one whose binding is that alternative.
This way of using menus in an Emacs-like editor was inspired by the Hierarkey system.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is a complete example of defining a menu keymap. It is the definition of the `Print' submenu in the `Tools' menu in the menu bar, and it uses the simple menu item format (see section 22.12.1.1 Simple Menu Items). First we create the keymap, and give it a name:
(defvar menu-bar-print-menu (make-sparse-keymap "Print")) |
Next we define the menu items:
(define-key menu-bar-print-menu [ps-print-region] '("Postscript Print Region" . ps-print-region-with-faces)) (define-key menu-bar-print-menu [ps-print-buffer] '("Postscript Print Buffer" . ps-print-buffer-with-faces)) (define-key menu-bar-print-menu [separator-ps-print] '("--")) (define-key menu-bar-print-menu [print-region] '("Print Region" . print-region)) (define-key menu-bar-print-menu [print-buffer] '("Print Buffer" . print-buffer)) |
Note the symbols which the bindings are "made for"; these appear
inside square brackets, in the key sequence being defined. In some
cases, this symbol is the same as the command name; sometimes it is
different. These symbols are treated as "function keys", but they are
not real function keys on the keyboard. They do not affect the
functioning of the menu itself, but they are "echoed" in the echo area
when the user selects from the menu, and they appear in the output of
where-is
and apropos
.
The binding whose definition is ("--")
is a separator line.
Like a real menu item, the separator has a key symbol, in this case
separator-ps-print
. If one menu has two separators, they must
have two different key symbols.
Here is code to define enable conditions for two of the commands in the menu:
(put 'print-region 'menu-enable 'mark-active) (put 'ps-print-region-with-faces 'menu-enable 'mark-active) |
Here is how we make this menu appear as an item in the parent menu:
(define-key menu-bar-tools-menu [print] (cons "Print" menu-bar-print-menu)) |
Note that this incorporates the submenu keymap, which is the value of
the variable menu-bar-print-menu
, rather than the symbol
menu-bar-print-menu
itself. Using that symbol in the parent menu
item would be meaningless because menu-bar-print-menu
is not a
command.
If you wanted to attach the same print menu to a mouse click, you can do it this way:
(define-key global-map [C-S-down-mouse-1] menu-bar-print-menu) |
We could equally well use an extended menu item (see section 22.12.1.2 Extended Menu Items) for print-region
, like this:
(define-key menu-bar-print-menu [print-region] '(menu-item "Print Region" print-region :enable mark-active)) |
With the extended menu item, the enable condition is specified inside the menu item itself. If we wanted to make this item disappear from the menu entirely when the mark is inactive, we could do it this way:
(define-key menu-bar-print-menu [print-region] '(menu-item "Print Region" print-region :visible mark-active)) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Most window systems allow each frame to have a menu bar---a
permanently displayed menu stretching horizontally across the top of the
frame. The items of the menu bar are the subcommands of the fake
"function key" menu-bar
, as defined by all the active keymaps.
To add an item to the menu bar, invent a fake "function key" of your
own (let's call it key), and make a binding for the key sequence
[menu-bar key]
. Most often, the binding is a menu keymap,
so that pressing a button on the menu bar item leads to another menu.
When more than one active keymap defines the same fake function key for the menu bar, the item appears just once. If the user clicks on that menu bar item, it brings up a single, combined menu containing all the subcommands of that item--the global subcommands, the local subcommands, and the minor mode subcommands.
The variable overriding-local-map
is normally ignored when
determining the menu bar contents. That is, the menu bar is computed
from the keymaps that would be active if overriding-local-map
were nil
. See section 22.6 Active Keymaps.
In order for a frame to display a menu bar, its menu-bar-lines
parameter must be greater than zero. Emacs uses just one line for the
menu bar itself; if you specify more than one line, the other lines
serve to separate the menu bar from the windows in the frame. We
recommend 1 or 2 as the value of menu-bar-lines
. See section 29.3.3 Window Frame Parameters.
Here's an example of setting up a menu bar item:
(modify-frame-parameters (selected-frame) '((menu-bar-lines . 2))) ;; Make a menu keymap (with a prompt string) ;; and make it the menu bar item's definition. (define-key global-map [menu-bar words] (cons "Words" (make-sparse-keymap "Words"))) ;; Define specific subcommands in this menu. (define-key global-map [menu-bar words forward] '("Forward word" . forward-word)) (define-key global-map [menu-bar words backward] '("Backward word" . backward-word)) |
A local keymap can cancel a menu bar item made by the global keymap by
rebinding the same fake function key with undefined
as the
binding. For example, this is how Dired suppresses the `Edit' menu
bar item:
(define-key dired-mode-map [menu-bar edit] 'undefined) |
edit
is the fake function key used by the global map for the
`Edit' menu bar item. The main reason to suppress a global
menu bar item is to regain space for mode-specific items.
This variable holds a list of fake function keys for items to display at
the end of the menu bar rather than in normal sequence. The default
value is (help-menu)
; thus, the `Help' menu item normally appears
at the end of the menu bar, following local menu items.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A tool bar is a row of icons at the top of a frame, that execute commands when you click on them--in effect, a kind of graphical menu bar. Emacs supports tool bars starting with version 21.
The frame parameter tool-bar-lines
(X resource `toolBar')
controls how many lines' worth of height to reserve for the tool bar. A
zero value suppresses the tool bar. If the value is nonzero, and
auto-resize-tool-bars
is non-nil
, the tool bar expands and
contracts automatically as needed to hold the specified contents.
The tool bar contents are controlled by a menu keymap attached to a
fake "function key" called tool-bar
(much like the way the menu
bar is controlled). So you define a tool bar item using
define-key
, like this:
(define-key global-map [tool-bar key] item) |
where key is a fake "function key" to distinguish this item from other items, and item is a menu item key binding (see section 22.12.1.2 Extended Menu Items), which says how to display this item and how it behaves.
The usual menu keymap item properties, :visible
,
:enable
, :button
, and :filter
, are useful in
tool bar bindings and have their normal meanings. The real-binding
in the item must be a command, not a keymap; in other words, it does not
work to define a tool bar icon as a prefix key.
The :help
property specifies a "help-echo" string to display
while the mouse is on that item. This is displayed in the same way as
help-echo
text properties (see Help display).
In addition, you should use the :image
property;
this is how you specify the image to display in the tool bar:
:image image
If image is a single image specification, Emacs draws the tool bar button in disabled state by applying an edge-detection algorithm to the image.
The default tool bar is defined so that items specific to editing do not
appear for major modes whose command symbol has a mode-class
property of special
(see section 23.1.1 Major Mode Conventions). Major
modes may add items to the global bar by binding [tool-bar
foo]
in their local map. It makes sense for some major modes to
replace the default tool bar items completely, since not many can be
accommodated conveniently, and the default bindings make this easy by
using an indirection through tool-bar-map
.
[tool-bar]
as follows:
(global-set-key [tool-bar] '(menu-item "tool bar" ignore :filter (lambda (ignore) tool-bar-map))) |
tool-bar-map
and you should normally adjust the default (global)
tool bar by changing that map. Major modes may replace the global bar
completely by making tool-bar-map
buffer-local and set to a
keymap containing only the desired items. Info mode provides an
example.
There are two convenience functions for defining tool bar items, as follows.
tool-bar-map
. The image to use is defined by icon, which
is the base name of an XPM, XBM or PBM image file to located by
find-image
. Given a value `"exit"', say, `exit.xpm',
`exit.pbm' and `exit.xbm' would be searched for in that order
on a color display. On a monochrome display, the search order is
`.pbm', `.xbm' and `.xpm'. The binding to use is the
command def, and key is the fake function key symbol in the
prefix keymap. The remaining arguments props are additional
property list elements to add to the menu item specification.
To define items in some local map, bind `tool-bar-map
with
let
around calls of this function:
(defvar foo-tool-bar-map (let ((tool-bar-map (make-sparse-keymap))) (tool-bar-add-item ...) ... tool-bar-map)) |
global-map
) and modified to add an image specification for
icon, which is looked for in the same way as by
tool-bar-add-item
. The resulting binding is then placed in
tool-bar-map
. map must contain an appropriate keymap bound
to [menu-bar]
. The remaining arguments props are
additional property list elements to add to the menu item specification.
nil
, the tool bar automatically resizes to
show all defined tool bar items--but not larger than a quarter of the
frame's height.
nil
, tool bar items display
in raised form when the mouse moves over them.
You can define a special meaning for clicking on a tool bar item with the shift, control, meta, etc., modifiers. You do this by setting up additional items that relate to the original item through the fake function keys. Specifically, the additional items should use the modified versions of the same fake function key used to name the original item.
Thus, if the original item was defined this way,
(define-key global-map [tool-bar shell] '(menu-item "Shell" shell :image (image :type xpm :file "shell.xpm"))) |
then here is how you can define clicking on the same tool bar image with the shift modifier:
(define-key global-map [tool-bar S-shell] 'some-command) |
See section 21.6.2 Function Keys, for more information about how to add modifiers to function keys.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When you insert a new item in an existing menu, you probably want to
put it in a particular place among the menu's existing items. If you
use define-key
to add the item, it normally goes at the front of
the menu. To put it elsewhere in the menu, use define-key-after
:
define-key
, but position the binding in map after
the binding for the event after. The argument key should be
of length one--a vector or string with just one element. But
after should be a single event type--a symbol or a character, not
a sequence. The new binding goes after the binding for after. If
after is t
or is omitted, then the new binding goes last, at
the end of the keymap. However, new bindings are added before any
inherited keymap.
Here is an example:
(define-key-after my-menu [drink] '("Drink" . drink-command) 'eat) |
makes a binding for the fake function key DRINK and puts it right after the binding for EAT.
Here is how to insert an item called `Work' in the `Signals'
menu of Shell mode, after the item break
:
(define-key-after (lookup-key shell-mode-map [menu-bar signals]) [work] '("Work" . work-command) 'break) |
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |