comparison lispref/keymaps.texi @ 70922:329ad40a4c40

* keymaps.texi (Key Sequences): Some clarifications.
author Chong Yidong <cyd@stupidchicken.com>
date Thu, 25 May 2006 16:48:37 +0000
parents f137d96866f0
children 24a04d2faed9
comparison
equal deleted inserted replaced
70921:77206b6b6ec5 70922:329ad40a4c40
80 for prefix keys; therefore, it can be different for different keymaps, 80 for prefix keys; therefore, it can be different for different keymaps,
81 and can change when bindings are changed. However, a one-event sequence 81 and can change when bindings are changed. However, a one-event sequence
82 is always a key sequence, because it does not depend on any prefix keys 82 is always a key sequence, because it does not depend on any prefix keys
83 for its well-formedness. 83 for its well-formedness.
84 84
85 A key sequence can be represented in Emacs Lisp as either a string
86 or vector. Unless otherwise stated, any Emacs Lisp function that
87 accepts a key sequence as an argument can handle both representations.
88
89 In the string representation, alphanumeric characters ordinarily
90 stand for themselves; for example, @code{"a"} represents @key{a} and
91 and @code{"2"} represents @key{2}. Control character events are
92 prefixed by the substring @code{"\C-"}, and meta characters by
93 @code{"\M-"}; for example, @code{"\C-x"} represents the key @kbd{C-x}.
94 In addition, the @kbd{<TAB>}, @kbd{<RET>}, @kbd{<ESC>}, and
95 @kbd{<DEL>} events are represented by @code{"\t"}, @code{"\r"},
96 @code{"\e"}, and @code{"\d"} respectively. The string representation
97 of a complete key sequence is then obtained by concatenating the
98 string representations of each constituent event; thus, @code{"\C-xl"}
99 represents the key sequence @kbd{C-x l}.
100
101 Key sequences containing function keys, mouse button events, or
102 non-ASCII characters such as @kbd{C-=} or @kbd{H-a} cannot be
103 represented by strings; they have to be represented by vectors.
104
105 In the vector representation, each element of the vector represents
106 a consecutive input element, in its Lisp form. @xref{Input Events}.
107 For example, the vector @code{[?\C-x ?l]} represents the key sequence
108 @kbd{C-x l}.
109
110 For examples of key sequences written in string and vector
111 representations, @ref{Init Rebinding,,, emacs, The GNU Emacs Manual}.
112
113 The @code{kbd} macro provides a convenient way to generate an Emacs
114 Lisp key sequence:
115
116 @defmac kbd keyseq-text
117 This macro converts the text @var{keyseq-text} (a string constant)
118 into a key sequence (a string or vector constant). The contents
119 of @var{keyseq-text} should describe the key sequence using the syntax
120 used in this manual. More precisely, it uses the same syntax that
121 Edit Macro mode uses for editing keyboard macros (@pxref{Edit Keyboard
122 Macro,,, emacs, The GNU Emacs Manual}).
123
124 @example
125 (kbd "C-x") @result{} "\C-x"
126 (kbd "C-x C-f") @result{} "\C-x\C-f"
127 (kbd "C-x 4 C-f") @result{} "\C-x4\C-f"
128 (kbd "X") @result{} "X"
129 (kbd "RET") @result{} "\^M"
130 (kbd "C-c SPC") @result{} "\C-c@ "
131 (kbd "<f1> SPC") @result{} [f1 32]
132 (kbd "C-M-<down>") @result{} [C-M-down]
133 @end example
134 @end defmac
135
136 @node Format of Keymaps
137 @section Format of Keymaps
138 @cindex format of keymaps
139 @cindex keymap format
140 @cindex full keymap
141 @cindex sparse keymap
142
143 A @dfn{keymap} is a table mapping event types to definitions (which
144 can be any Lisp objects, though only certain types are meaningful for
145 execution by the command loop). Given an event (or an event type) and a
146 keymap, Emacs can get the event's definition. Events include
147 characters, function keys, and mouse actions (@pxref{Input Events}).
148
85 At any time, several primary keymaps are @dfn{active}---that is, in 149 At any time, several primary keymaps are @dfn{active}---that is, in
86 use for finding key bindings. These are the @dfn{global map}, which is 150 use for finding key bindings. These are the @dfn{global map}, which is
87 shared by all buffers; the @dfn{local keymap}, which is usually 151 shared by all buffers; the @dfn{local keymap}, which is usually
88 associated with a specific major mode; and zero or more @dfn{minor mode 152 associated with a specific major mode; and zero or more @dfn{minor mode
89 keymaps}, which belong to currently enabled minor modes. (Not all minor 153 keymaps}, which belong to currently enabled minor modes. (Not all minor
90 modes have keymaps.) The local keymap bindings shadow (i.e., take 154 modes have keymaps.) The local keymap bindings shadow (i.e., take
91 precedence over) the corresponding global bindings. The minor mode 155 precedence over) the corresponding global bindings. The minor mode
92 keymaps shadow both local and global keymaps. @xref{Active Keymaps}, 156 keymaps shadow both local and global keymaps. @xref{Active Keymaps},
93 for details. 157 for details.
94
95 The Emacs Lisp representation for a key sequence is a string or vector.
96
97 In the string representation, alphanumeric characters ordinarily
98 stand for themselves; for example, @code{"a"} represents @key{a} and
99 and @code{"1"} represents @key{1}. Control character events are
100 prefixed by the substring @code{"\C-"}, and meta characters by
101 @code{"\M-"}; for example, @code{"\C-x"} represents the key @kbd{C-x}.
102 In addition, the @kbd{<TAB>}, @kbd{<RET>}, @kbd{<ESC>}, and
103 @kbd{<DEL>} events are represented by @code{"\t"}, @code{"\r"},
104 @code{"\e"}, and @code{"\d"} respectively. The string representation
105 of a complete key sequence is then obtained by concatenating the
106 string representations of each constituent event; thus, @code{"\C-x"}
107 represents the key sequence @kbd{C-x}.
108
109 Key sequences containing function keys, mouse button events, or
110 non-ASCII characters such as @kbd{C-=} or @kbd{H-a} cannot be
111 represented as strings; they have to be represented as vectors.
112
113 In the vector representation, each element of the vector represents
114 a consecutive input element, in its Lisp form. @xref{Input Events}.
115 For example, ordinary keyboard events are represented by Lisp
116 characters (@pxref{Keyboard Events}), so the character @code{?a}
117 represents @key{a}.
118
119 @defmac kbd keyseq-text
120 This macro converts the text @var{keyseq-text} (a string constant)
121 into a key sequence (a string or vector constant). The contents
122 of @var{keyseq-text} should describe the key sequence using the syntax
123 used in this manual. More precisely, it uses the same syntax that
124 Edit Macro mode uses for editing keyboard macros (@pxref{Edit Keyboard
125 Macro,,, emacs, The GNU Emacs Manual}).
126
127 @example
128 (kbd "C-x") @result{} "\C-x"
129 (kbd "C-x C-f") @result{} "\C-x\C-f"
130 (kbd "C-x 4 C-f") @result{} "\C-x4\C-f"
131 (kbd "X") @result{} "X"
132 (kbd "RET") @result{} "\^M"
133 (kbd "C-c SPC") @result{} "\C-c@ "
134 (kbd "<f1> SPC") @result{} [f1 32]
135 (kbd "C-M-<down>") @result{} [C-M-down]
136 @end example
137 @end defmac
138
139 @node Format of Keymaps
140 @section Format of Keymaps
141 @cindex format of keymaps
142 @cindex keymap format
143 @cindex full keymap
144 @cindex sparse keymap
145
146 A @dfn{keymap} is a table mapping event types to definitions (which
147 can be any Lisp objects, though only certain types are meaningful for
148 execution by the command loop). Given an event (or an event type) and a
149 keymap, Emacs can get the event's definition. Events include
150 characters, function keys, and mouse actions (@pxref{Input Events}).
151 158
152 Each keymap is a list whose @sc{car} is the symbol @code{keymap}. The 159 Each keymap is a list whose @sc{car} is the symbol @code{keymap}. The
153 remaining elements of the list define the key bindings of the keymap. 160 remaining elements of the list define the key bindings of the keymap.
154 A symbol whose function definition is a keymap is also a keymap. Use 161 A symbol whose function definition is a keymap is also a keymap. Use
155 the function @code{keymapp} (see below) to test whether an object is a 162 the function @code{keymapp} (see below) to test whether an object is a