Mercurial > emacs
annotate src/keymap.c @ 1151:3dd95c0296ef
*** empty log message ***
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Tue, 15 Sep 1992 21:35:53 +0000 |
parents | 0a486e1a45bc |
children | f7b55bfe1c05 |
rev | line source |
---|---|
250 | 1 /* Manipulation of keymaps |
647 | 2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc. |
250 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
647 | 8 the Free Software Foundation; either version 2, or (at your option) |
250 | 9 any later version. |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | |
20 | |
21 #include "config.h" | |
22 #include <stdio.h> | |
23 #undef NULL | |
24 #include "lisp.h" | |
25 #include "commands.h" | |
26 #include "buffer.h" | |
517 | 27 #include "keyboard.h" |
250 | 28 |
29 #define min(a, b) ((a) < (b) ? (a) : (b)) | |
30 | |
31 /* Dense keymaps look like (keymap VECTOR . ALIST), where VECTOR is a | |
32 128-element vector used to look up bindings for ASCII characters, | |
33 and ALIST is an assoc list for looking up symbols. */ | |
34 #define DENSE_TABLE_SIZE (0200) | |
35 | |
36 /* Actually allocate storage for these variables */ | |
37 | |
38 Lisp_Object current_global_map; /* Current global keymap */ | |
39 | |
40 Lisp_Object global_map; /* default global key bindings */ | |
41 | |
42 Lisp_Object meta_map; /* The keymap used for globally bound | |
43 ESC-prefixed default commands */ | |
44 | |
45 Lisp_Object control_x_map; /* The keymap used for globally bound | |
46 C-x-prefixed default commands */ | |
47 | |
48 /* was MinibufLocalMap */ | |
49 Lisp_Object Vminibuffer_local_map; | |
50 /* The keymap used by the minibuf for local | |
51 bindings when spaces are allowed in the | |
52 minibuf */ | |
53 | |
54 /* was MinibufLocalNSMap */ | |
55 Lisp_Object Vminibuffer_local_ns_map; | |
56 /* The keymap used by the minibuf for local | |
57 bindings when spaces are not encouraged | |
58 in the minibuf */ | |
59 | |
60 /* keymap used for minibuffers when doing completion */ | |
61 /* was MinibufLocalCompletionMap */ | |
62 Lisp_Object Vminibuffer_local_completion_map; | |
63 | |
64 /* keymap used for minibuffers when doing completion and require a match */ | |
65 /* was MinibufLocalMustMatchMap */ | |
66 Lisp_Object Vminibuffer_local_must_match_map; | |
67 | |
465 | 68 /* Alist of minor mode variables and keymaps. */ |
69 Lisp_Object Vminor_mode_map_alist; | |
70 | |
517 | 71 /* Keymap mapping ASCII function key sequences onto their preferred forms. |
72 Initialized by the terminal-specific lisp files. See DEFVAR for more | |
73 documentation. */ | |
74 Lisp_Object Vfunction_key_map; | |
75 | |
250 | 76 Lisp_Object Qkeymapp, Qkeymap; |
77 | |
78 /* A char over 0200 in a key sequence | |
79 is equivalent to prefixing with this character. */ | |
80 | |
81 extern Lisp_Object meta_prefix_char; | |
82 | |
83 void describe_map_tree (); | |
84 static Lisp_Object describe_buffer_bindings (); | |
85 static void describe_command (); | |
86 static void describe_map (); | |
87 static void describe_alist (); | |
88 | |
465 | 89 /* Keymap object support - constructors and predicates. */ |
90 | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
91 DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0, |
250 | 92 "Construct and return a new keymap, of the form (keymap VECTOR . ALIST).\n\ |
93 VECTOR is a 128-element vector which holds the bindings for the ASCII\n\ | |
94 characters. ALIST is an assoc-list which holds bindings for function keys,\n\ | |
95 mouse events, and any other things that appear in the input stream.\n\ | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
96 All entries in it are initially nil, meaning \"command undefined\".\n\n\ |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
97 The optional arg STRING supplies a menu name for the keymap\n\ |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
98 in case you use it as a menu with `x-popup-menu'.") |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
99 (string) |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
100 Lisp_Object string; |
250 | 101 { |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
102 Lisp_Object tail; |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
103 if (!NILP (string)) |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
104 tail = Fcons (string, Qnil); |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
105 else |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
106 tail = Qnil; |
250 | 107 return Fcons (Qkeymap, |
108 Fcons (Fmake_vector (make_number (DENSE_TABLE_SIZE), Qnil), | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
109 tail)); |
250 | 110 } |
111 | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
112 DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0, |
250 | 113 "Construct and return a new sparse-keymap list.\n\ |
114 Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\ | |
115 which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\ | |
116 which binds the function key or mouse event SYMBOL to DEFINITION.\n\ | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
117 Initially the alist is nil.\n\n\ |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
118 The optional arg STRING supplies a menu name for the keymap\n\ |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
119 in case you use it as a menu with `x-popup-menu'.") |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
120 (string) |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
121 Lisp_Object string; |
250 | 122 { |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
123 if (!NILP (string)) |
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
124 return Fcons (Qkeymap, Fcons (string, Qnil)); |
250 | 125 return Fcons (Qkeymap, Qnil); |
126 } | |
127 | |
128 /* This function is used for installing the standard key bindings | |
129 at initialization time. | |
130 | |
131 For example: | |
132 | |
133 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); | |
134 | |
135 I haven't extended these to allow the initializing code to bind | |
136 function keys and mouse events; since they are called by many files, | |
137 I'd have to fix lots of callers, and nobody right now would be using | |
138 the new functionality, so it seems like a waste of time. But there's | |
139 no technical reason not to. -JimB */ | |
140 | |
141 void | |
142 initial_define_key (keymap, key, defname) | |
143 Lisp_Object keymap; | |
144 int key; | |
145 char *defname; | |
146 { | |
147 store_in_keymap (keymap, make_number (key), intern (defname)); | |
148 } | |
149 | |
150 /* Define character fromchar in map frommap as an alias for character | |
151 tochar in map tomap. Subsequent redefinitions of the latter WILL | |
152 affect the former. */ | |
153 | |
154 #if 0 | |
155 void | |
156 synkey (frommap, fromchar, tomap, tochar) | |
157 struct Lisp_Vector *frommap, *tomap; | |
158 int fromchar, tochar; | |
159 { | |
160 Lisp_Object v, c; | |
161 XSET (v, Lisp_Vector, tomap); | |
162 XFASTINT (c) = tochar; | |
163 frommap->contents[fromchar] = Fcons (v, c); | |
164 } | |
165 #endif /* 0 */ | |
166 | |
167 DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0, | |
168 "Return t if ARG is a keymap.\n\ | |
362 | 169 \n\ |
170 A keymap is list (keymap . ALIST), a list (keymap VECTOR . ALIST),\n\ | |
171 or a symbol whose function definition is a keymap is itself a keymap.\n\ | |
172 ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\ | |
173 VECTOR is a 128-element vector of bindings for ASCII characters.") | |
250 | 174 (object) |
175 Lisp_Object object; | |
176 { | |
485 | 177 return (NILP (get_keymap_1 (object, 0)) ? Qnil : Qt); |
250 | 178 } |
179 | |
180 /* Check that OBJECT is a keymap (after dereferencing through any | |
181 symbols). If it is, return it; otherwise, return nil, or signal an | |
182 error if ERROR != 0. */ | |
183 Lisp_Object | |
184 get_keymap_1 (object, error) | |
185 Lisp_Object object; | |
186 int error; | |
187 { | |
188 register Lisp_Object tem; | |
189 | |
647 | 190 tem = indirect_function (object); |
250 | 191 if (CONSP (tem) && EQ (XCONS (tem)->car, Qkeymap)) |
192 return tem; | |
193 if (error) | |
194 wrong_type_argument (Qkeymapp, object); | |
465 | 195 else |
196 return Qnil; | |
250 | 197 } |
198 | |
199 Lisp_Object | |
200 get_keymap (object) | |
201 Lisp_Object object; | |
202 { | |
203 return get_keymap_1 (object, 1); | |
204 } | |
205 | |
206 | |
207 /* If KEYMAP is a dense keymap, return the vector from its cadr. | |
208 Otherwise, return nil. */ | |
209 | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
210 Lisp_Object |
250 | 211 keymap_table (keymap) |
212 Lisp_Object keymap; | |
213 { | |
214 Lisp_Object cadr; | |
215 | |
216 if (CONSP (XCONS (keymap)->cdr) | |
217 && XTYPE (cadr = XCONS (XCONS (keymap)->cdr)->car) == Lisp_Vector | |
218 && XVECTOR (cadr)->size == DENSE_TABLE_SIZE) | |
219 return cadr; | |
220 else | |
221 return Qnil; | |
222 } | |
223 | |
224 | |
225 /* Look up IDX in MAP. IDX may be any sort of event. | |
226 Note that this does only one level of lookup; IDX must | |
227 be a single event, not a sequence. */ | |
228 | |
229 Lisp_Object | |
230 access_keymap (map, idx) | |
231 Lisp_Object map; | |
232 Lisp_Object idx; | |
233 { | |
234 /* If idx is a list (some sort of mouse click, perhaps?), | |
235 the index we want to use is the car of the list, which | |
236 ought to be a symbol. */ | |
517 | 237 if (EVENT_HAS_PARAMETERS (idx)) |
238 idx = EVENT_HEAD (idx); | |
250 | 239 |
240 if (XTYPE (idx) == Lisp_Int | |
241 && (XINT (idx) < 0 || XINT (idx) >= DENSE_TABLE_SIZE)) | |
242 error ("Command key is not an ASCII character"); | |
243 | |
244 { | |
245 Lisp_Object table = keymap_table (map); | |
246 | |
247 /* A dense keymap indexed by a character? */ | |
248 if (XTYPE (idx) == Lisp_Int | |
485 | 249 && ! NILP (table)) |
250 | 250 return XVECTOR (table)->contents[XFASTINT (idx)]; |
251 | |
252 /* This lookup will not involve a vector reference. */ | |
253 else | |
254 { | |
255 /* If idx is a symbol, it might have modifiers, which need to | |
256 be put in the canonical order. */ | |
257 if (XTYPE (idx) == Lisp_Symbol) | |
258 idx = reorder_modifiers (idx); | |
259 | |
260 return Fcdr (Fassq (idx, map)); | |
261 } | |
262 } | |
263 } | |
264 | |
265 /* Given OBJECT which was found in a slot in a keymap, | |
266 trace indirect definitions to get the actual definition of that slot. | |
267 An indirect definition is a list of the form | |
268 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one | |
269 and INDEX is the object to look up in KEYMAP to yield the definition. | |
270 | |
271 Also if OBJECT has a menu string as the first element, | |
272 remove that. */ | |
273 | |
274 Lisp_Object | |
275 get_keyelt (object) | |
276 register Lisp_Object object; | |
277 { | |
278 while (1) | |
279 { | |
280 register Lisp_Object map, tem; | |
281 | |
282 map = get_keymap_1 (Fcar_safe (object), 0); | |
283 tem = Fkeymapp (map); | |
284 | |
285 /* If the contents are (KEYMAP . ELEMENT), go indirect. */ | |
485 | 286 if (!NILP (tem)) |
250 | 287 object = access_keymap (map, Fcdr (object)); |
288 | |
289 /* If the keymap contents looks like (STRING . DEFN), | |
290 use DEFN. | |
291 Keymap alist elements like (CHAR MENUSTRING . DEFN) | |
292 will be used by HierarKey menus. */ | |
293 else if (XTYPE (object) == Lisp_Cons | |
294 && XTYPE (XCONS (object)->car) == Lisp_String) | |
295 object = XCONS (object)->cdr; | |
296 | |
297 else | |
298 /* Anything else is really the value. */ | |
299 return object; | |
300 } | |
301 } | |
302 | |
303 Lisp_Object | |
304 store_in_keymap (keymap, idx, def) | |
305 Lisp_Object keymap; | |
306 register Lisp_Object idx; | |
307 register Lisp_Object def; | |
308 { | |
309 /* If idx is a list (some sort of mouse click, perhaps?), | |
310 the index we want to use is the car of the list, which | |
311 ought to be a symbol. */ | |
517 | 312 if (EVENT_HAS_PARAMETERS (idx)) |
313 idx = EVENT_HEAD (idx); | |
250 | 314 |
315 if (XTYPE (idx) == Lisp_Int | |
316 && (XINT (idx) < 0 || XINT (idx) >= DENSE_TABLE_SIZE)) | |
317 error ("Command key is a character outside of the ASCII set."); | |
318 | |
319 { | |
320 Lisp_Object table = keymap_table (keymap); | |
321 | |
322 /* A dense keymap indexed by a character? */ | |
485 | 323 if (XTYPE (idx) == Lisp_Int && !NILP (table)) |
250 | 324 XVECTOR (table)->contents[XFASTINT (idx)] = def; |
325 | |
326 /* Must be a sparse keymap, or a dense keymap indexed by a symbol. */ | |
327 else | |
328 { | |
329 /* Point to the pointer to the start of the assoc-list part | |
330 of the keymap. */ | |
331 register Lisp_Object *assoc_head | |
485 | 332 = (NILP (table) |
250 | 333 ? & XCONS (keymap)->cdr |
334 : & XCONS (XCONS (keymap)->cdr)->cdr); | |
335 register Lisp_Object defining_pair; | |
336 | |
337 /* If idx is a symbol, it might have modifiers, which need to | |
338 be put in the canonical order. */ | |
339 if (XTYPE (idx) == Lisp_Symbol) | |
340 idx = reorder_modifiers (idx); | |
341 | |
342 /* Point to the pair where idx is bound, if any. */ | |
343 defining_pair = Fassq (idx, *assoc_head); | |
344 | |
485 | 345 if (NILP (defining_pair)) |
250 | 346 *assoc_head = Fcons (Fcons (idx, def), *assoc_head); |
347 else | |
348 Fsetcdr (defining_pair, def); | |
349 } | |
350 } | |
351 | |
352 return def; | |
353 } | |
354 | |
355 DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0, | |
356 "Return a copy of the keymap KEYMAP.\n\ | |
357 The copy starts out with the same definitions of KEYMAP,\n\ | |
358 but changing either the copy or KEYMAP does not affect the other.\n\ | |
362 | 359 Any key definitions that are subkeymaps are recursively copied.\n\ |
360 However, a key definition which is a symbol whose definition is a keymap\n\ | |
361 is not copied.") | |
250 | 362 (keymap) |
363 Lisp_Object keymap; | |
364 { | |
365 register Lisp_Object copy, tail; | |
366 | |
367 copy = Fcopy_alist (get_keymap (keymap)); | |
368 tail = XCONS (copy)->cdr; | |
369 | |
370 /* If this is a dense keymap, copy the vector. */ | |
371 if (CONSP (tail)) | |
372 { | |
373 register Lisp_Object table = XCONS (tail)->car; | |
374 | |
375 if (XTYPE (table) == Lisp_Vector | |
376 && XVECTOR (table)->size == DENSE_TABLE_SIZE) | |
377 { | |
378 register int i; | |
379 | |
380 table = Fcopy_sequence (table); | |
381 | |
382 for (i = 0; i < DENSE_TABLE_SIZE; i++) | |
362 | 383 if (XTYPE (XVECTOR (copy)->contents[i]) != Lisp_Symbol) |
485 | 384 if (! NILP (Fkeymapp (XVECTOR (table)->contents[i]))) |
362 | 385 XVECTOR (table)->contents[i] |
386 = Fcopy_keymap (XVECTOR (table)->contents[i]); | |
250 | 387 XCONS (tail)->car = table; |
388 | |
389 tail = XCONS (tail)->cdr; | |
390 } | |
391 } | |
392 | |
393 /* Copy the alist portion of the keymap. */ | |
394 while (CONSP (tail)) | |
395 { | |
396 register Lisp_Object elt; | |
397 | |
398 elt = XCONS (tail)->car; | |
362 | 399 if (CONSP (elt) |
400 && XTYPE (XCONS (elt)->cdr) != Lisp_Symbol | |
485 | 401 && ! NILP (Fkeymapp (XCONS (elt)->cdr))) |
250 | 402 XCONS (elt)->cdr = Fcopy_keymap (XCONS (elt)->cdr); |
403 | |
404 tail = XCONS (tail)->cdr; | |
405 } | |
406 | |
407 return copy; | |
408 } | |
409 | |
465 | 410 /* Simple Keymap mutators and accessors. */ |
411 | |
250 | 412 DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0, |
413 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\ | |
414 KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\ | |
415 meaning a sequence of keystrokes and events.\n\ | |
416 DEF is anything that can be a key's definition:\n\ | |
417 nil (means key is undefined in this keymap),\n\ | |
418 a command (a Lisp function suitable for interactive calling)\n\ | |
419 a string (treated as a keyboard macro),\n\ | |
420 a keymap (to define a prefix key),\n\ | |
421 a symbol. When the key is looked up, the symbol will stand for its\n\ | |
422 function definition, which should at that time be one of the above,\n\ | |
423 or another symbol whose function definition is used, etc.\n\ | |
424 a cons (STRING . DEFN), meaning that DEFN is the definition\n\ | |
425 (DEFN should be a valid definition in its own right),\n\ | |
368 | 426 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\ |
427 \n\ | |
428 If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\ | |
429 the front of KEYMAP.") | |
250 | 430 (keymap, key, def) |
431 register Lisp_Object keymap; | |
432 Lisp_Object key; | |
433 Lisp_Object def; | |
434 { | |
435 register int idx; | |
436 register Lisp_Object c; | |
437 register Lisp_Object tem; | |
438 register Lisp_Object cmd; | |
439 int metized = 0; | |
440 int length; | |
441 | |
442 keymap = get_keymap (keymap); | |
443 | |
444 if (XTYPE (key) != Lisp_Vector | |
445 && XTYPE (key) != Lisp_String) | |
446 key = wrong_type_argument (Qarrayp, key); | |
447 | |
448 length = Flength (key); | |
449 if (length == 0) | |
450 return Qnil; | |
451 | |
452 idx = 0; | |
453 while (1) | |
454 { | |
455 c = Faref (key, make_number (idx)); | |
456 | |
457 if (XTYPE (c) == Lisp_Int | |
458 && XINT (c) >= 0200 | |
459 && !metized) | |
460 { | |
461 c = meta_prefix_char; | |
462 metized = 1; | |
463 } | |
464 else | |
465 { | |
466 if (XTYPE (c) == Lisp_Int) | |
467 XSETINT (c, XINT (c) & 0177); | |
468 | |
469 metized = 0; | |
470 idx++; | |
471 } | |
472 | |
473 if (idx == length) | |
474 return store_in_keymap (keymap, c, def); | |
475 | |
476 cmd = get_keyelt (access_keymap (keymap, c)); | |
477 | |
485 | 478 if (NILP (cmd)) |
250 | 479 { |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
480 cmd = Fmake_sparse_keymap (Qnil); |
250 | 481 store_in_keymap (keymap, c, cmd); |
482 } | |
483 | |
484 tem = Fkeymapp (cmd); | |
485 | 485 if (NILP (tem)) |
250 | 486 error ("Key sequence %s uses invalid prefix characters", |
487 XSTRING (key)->data); | |
488 | |
489 keymap = get_keymap (cmd); | |
490 } | |
491 } | |
492 | |
493 /* Value is number if KEY is too long; NIL if valid but has no definition. */ | |
494 | |
495 DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 2, 0, | |
496 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\ | |
497 nil means undefined. See doc of `define-key' for kinds of definitions.\n\ | |
498 A number as value means KEY is \"too long\";\n\ | |
499 that is, characters or symbols in it except for the last one\n\ | |
500 fail to be a valid sequence of prefix characters in KEYMAP.\n\ | |
501 The number is how many characters at the front of KEY\n\ | |
502 it takes to reach a non-prefix command.") | |
503 (keymap, key) | |
504 register Lisp_Object keymap; | |
505 Lisp_Object key; | |
506 { | |
507 register int idx; | |
508 register Lisp_Object tem; | |
509 register Lisp_Object cmd; | |
510 register Lisp_Object c; | |
511 int metized = 0; | |
512 int length; | |
513 | |
514 keymap = get_keymap (keymap); | |
515 | |
516 if (XTYPE (key) != Lisp_Vector | |
517 && XTYPE (key) != Lisp_String) | |
518 key = wrong_type_argument (Qarrayp, key); | |
519 | |
520 length = Flength (key); | |
521 if (length == 0) | |
522 return keymap; | |
523 | |
524 idx = 0; | |
525 while (1) | |
526 { | |
527 c = Faref (key, make_number (idx)); | |
528 | |
529 if (XTYPE (c) == Lisp_Int | |
530 && XINT (c) >= 0200 | |
531 && !metized) | |
532 { | |
533 c = meta_prefix_char; | |
534 metized = 1; | |
535 } | |
536 else | |
537 { | |
538 if (XTYPE (c) == Lisp_Int) | |
539 XSETINT (c, XINT (c) & 0177); | |
540 | |
541 metized = 0; | |
542 idx++; | |
543 } | |
544 | |
545 cmd = get_keyelt (access_keymap (keymap, c)); | |
546 if (idx == length) | |
547 return cmd; | |
548 | |
549 tem = Fkeymapp (cmd); | |
485 | 550 if (NILP (tem)) |
250 | 551 return make_number (idx); |
552 | |
553 keymap = get_keymap (cmd); | |
554 QUIT; | |
555 } | |
556 } | |
557 | |
558 /* Append a key to the end of a key sequence. If key_sequence is a | |
559 string and key is a character, the result will be another string; | |
560 otherwise, it will be a vector. */ | |
561 Lisp_Object | |
562 append_key (key_sequence, key) | |
563 Lisp_Object key_sequence, key; | |
564 { | |
565 Lisp_Object args[2]; | |
566 | |
567 args[0] = key_sequence; | |
568 | |
569 if (XTYPE (key_sequence) == Lisp_String | |
570 && XTYPE (key) == Lisp_Int) | |
571 { | |
572 args[1] = Fchar_to_string (key); | |
573 return Fconcat (2, args); | |
574 } | |
575 else | |
576 { | |
577 args[1] = Fcons (key, Qnil); | |
578 return Fvconcat (2, args); | |
579 } | |
580 } | |
581 | |
582 | |
465 | 583 /* Global, local, and minor mode keymap stuff. */ |
584 | |
485 | 585 /* We can't put these variables inside current_minor_maps, since under |
517 | 586 some systems, static gets macro-defined to be the empty string. |
587 Ickypoo. */ | |
485 | 588 static Lisp_Object *cmm_modes, *cmm_maps; |
589 static int cmm_size; | |
590 | |
465 | 591 /* Store a pointer to an array of the keymaps of the currently active |
592 minor modes in *buf, and return the number of maps it contains. | |
593 | |
594 This function always returns a pointer to the same buffer, and may | |
595 free or reallocate it, so if you want to keep it for a long time or | |
596 hand it out to lisp code, copy it. This procedure will be called | |
597 for every key sequence read, so the nice lispy approach (return a | |
598 new assoclist, list, what have you) for each invocation would | |
599 result in a lot of consing over time. | |
600 | |
601 If we used xrealloc/xmalloc and ran out of memory, they would throw | |
602 back to the command loop, which would try to read a key sequence, | |
603 which would call this function again, resulting in an infinite | |
604 loop. Instead, we'll use realloc/malloc and silently truncate the | |
605 list, let the key sequence be read, and hope some other piece of | |
606 code signals the error. */ | |
607 int | |
608 current_minor_maps (modeptr, mapptr) | |
609 Lisp_Object **modeptr, **mapptr; | |
610 { | |
611 int i = 0; | |
517 | 612 Lisp_Object alist, assoc, var, val; |
465 | 613 |
614 for (alist = Vminor_mode_map_alist; | |
615 CONSP (alist); | |
616 alist = XCONS (alist)->cdr) | |
617 if (CONSP (assoc = XCONS (alist)->car) | |
618 && XTYPE (var = XCONS (assoc)->car) == Lisp_Symbol | |
517 | 619 && ! EQ ((val = find_symbol_value (var)), Qunbound) |
620 && ! NILP (val)) | |
465 | 621 { |
485 | 622 if (i >= cmm_size) |
465 | 623 { |
624 Lisp_Object *newmodes, *newmaps; | |
625 | |
485 | 626 if (cmm_maps) |
465 | 627 { |
485 | 628 newmodes = (Lisp_Object *) realloc (cmm_modes, cmm_size *= 2); |
629 newmaps = (Lisp_Object *) realloc (cmm_maps, cmm_size); | |
465 | 630 } |
631 else | |
632 { | |
485 | 633 newmodes = (Lisp_Object *) malloc (cmm_size = 30); |
634 newmaps = (Lisp_Object *) malloc (cmm_size); | |
465 | 635 } |
636 | |
637 if (newmaps && newmodes) | |
638 { | |
485 | 639 cmm_modes = newmodes; |
640 cmm_maps = newmaps; | |
465 | 641 } |
642 else | |
643 break; | |
644 } | |
485 | 645 cmm_modes[i] = var; |
646 cmm_maps [i] = XCONS (assoc)->cdr; | |
465 | 647 i++; |
648 } | |
649 | |
485 | 650 if (modeptr) *modeptr = cmm_modes; |
651 if (mapptr) *mapptr = cmm_maps; | |
465 | 652 return i; |
653 } | |
654 | |
250 | 655 DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 1, 0, |
656 "Return the binding for command KEY in current keymaps.\n\ | |
657 KEY is a string, a sequence of keystrokes.\n\ | |
658 The binding is probably a symbol with a function definition.") | |
659 (key) | |
660 Lisp_Object key; | |
661 { | |
465 | 662 Lisp_Object *maps, value; |
663 int nmaps, i; | |
664 | |
665 nmaps = current_minor_maps (0, &maps); | |
666 for (i = 0; i < nmaps; i++) | |
485 | 667 if (! NILP (maps[i])) |
465 | 668 { |
669 value = Flookup_key (maps[i], key); | |
485 | 670 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
465 | 671 return value; |
672 } | |
673 | |
485 | 674 if (! NILP (current_buffer->keymap)) |
250 | 675 { |
465 | 676 value = Flookup_key (current_buffer->keymap, key); |
485 | 677 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
250 | 678 return value; |
679 } | |
465 | 680 |
681 value = Flookup_key (current_global_map, key); | |
485 | 682 if (! NILP (value) && XTYPE (value) != Lisp_Int) |
465 | 683 return value; |
684 | |
685 return Qnil; | |
250 | 686 } |
687 | |
688 DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 1, 0, | |
689 "Return the binding for command KEYS in current local keymap only.\n\ | |
690 KEYS is a string, a sequence of keystrokes.\n\ | |
691 The binding is probably a symbol with a function definition.") | |
692 (keys) | |
693 Lisp_Object keys; | |
694 { | |
695 register Lisp_Object map; | |
696 map = current_buffer->keymap; | |
485 | 697 if (NILP (map)) |
250 | 698 return Qnil; |
699 return Flookup_key (map, keys); | |
700 } | |
701 | |
702 DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 1, 0, | |
703 "Return the binding for command KEYS in current global keymap only.\n\ | |
704 KEYS is a string, a sequence of keystrokes.\n\ | |
517 | 705 The binding is probably a symbol with a function definition.\n\ |
706 This function's return values are the same as those of lookup-key\n\ | |
707 (which see).") | |
250 | 708 (keys) |
709 Lisp_Object keys; | |
710 { | |
711 return Flookup_key (current_global_map, keys); | |
712 } | |
713 | |
465 | 714 DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 1, 0, |
715 "Find the visible minor mode bindings of KEY.\n\ | |
716 Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\ | |
717 the symbol which names the minor mode binding KEY, and BINDING is\n\ | |
718 KEY's definition in that mode. In particular, if KEY has no\n\ | |
719 minor-mode bindings, return nil. If the first binding is a\n\ | |
720 non-prefix, all subsequent bindings will be omitted, since they would\n\ | |
721 be ignored. Similarly, the list doesn't include non-prefix bindings\n\ | |
722 that come after prefix bindings.") | |
723 (key) | |
724 { | |
725 Lisp_Object *modes, *maps; | |
726 int nmaps; | |
727 Lisp_Object binding; | |
728 int i, j; | |
729 | |
730 nmaps = current_minor_maps (&modes, &maps); | |
731 | |
732 for (i = j = 0; i < nmaps; i++) | |
485 | 733 if (! NILP (maps[i]) |
734 && ! NILP (binding = Flookup_key (maps[i], key)) | |
465 | 735 && XTYPE (binding) != Lisp_Int) |
736 { | |
485 | 737 if (! NILP (get_keymap_1 (binding, 0))) |
465 | 738 maps[j++] = Fcons (modes[i], binding); |
739 else if (j == 0) | |
740 return Fcons (Fcons (modes[i], binding), Qnil); | |
741 } | |
742 | |
743 return Flist (j, maps); | |
744 } | |
745 | |
250 | 746 DEFUN ("global-set-key", Fglobal_set_key, Sglobal_set_key, 2, 2, |
747 "kSet key globally: \nCSet key %s to command: ", | |
748 "Give KEY a global binding as COMMAND.\n\ | |
749 COMMAND is a symbol naming an interactively-callable function.\n\ | |
750 KEY is a string representing a sequence of keystrokes.\n\ | |
751 Note that if KEY has a local binding in the current buffer\n\ | |
752 that local binding will continue to shadow any global binding.") | |
753 (keys, function) | |
754 Lisp_Object keys, function; | |
755 { | |
756 if (XTYPE (keys) != Lisp_Vector | |
757 && XTYPE (keys) != Lisp_String) | |
758 keys = wrong_type_argument (Qarrayp, keys); | |
759 | |
760 Fdefine_key (current_global_map, keys, function); | |
761 return Qnil; | |
762 } | |
763 | |
764 DEFUN ("local-set-key", Flocal_set_key, Slocal_set_key, 2, 2, | |
765 "kSet key locally: \nCSet key %s locally to command: ", | |
766 "Give KEY a local binding as COMMAND.\n\ | |
767 COMMAND is a symbol naming an interactively-callable function.\n\ | |
768 KEY is a string representing a sequence of keystrokes.\n\ | |
769 The binding goes in the current buffer's local map,\n\ | |
770 which is shared with other buffers in the same major mode.") | |
771 (keys, function) | |
772 Lisp_Object keys, function; | |
773 { | |
774 register Lisp_Object map; | |
775 map = current_buffer->keymap; | |
485 | 776 if (NILP (map)) |
250 | 777 { |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
778 map = Fmake_sparse_keymap (Qnil); |
250 | 779 current_buffer->keymap = map; |
780 } | |
781 | |
782 if (XTYPE (keys) != Lisp_Vector | |
783 && XTYPE (keys) != Lisp_String) | |
784 keys = wrong_type_argument (Qarrayp, keys); | |
785 | |
786 Fdefine_key (map, keys, function); | |
787 return Qnil; | |
788 } | |
789 | |
790 DEFUN ("global-unset-key", Fglobal_unset_key, Sglobal_unset_key, | |
791 1, 1, "kUnset key globally: ", | |
792 "Remove global binding of KEY.\n\ | |
793 KEY is a string representing a sequence of keystrokes.") | |
794 (keys) | |
795 Lisp_Object keys; | |
796 { | |
797 return Fglobal_set_key (keys, Qnil); | |
798 } | |
799 | |
800 DEFUN ("local-unset-key", Flocal_unset_key, Slocal_unset_key, 1, 1, | |
801 "kUnset key locally: ", | |
802 "Remove local binding of KEY.\n\ | |
803 KEY is a string representing a sequence of keystrokes.") | |
804 (keys) | |
805 Lisp_Object keys; | |
806 { | |
485 | 807 if (!NILP (current_buffer->keymap)) |
250 | 808 Flocal_set_key (keys, Qnil); |
809 return Qnil; | |
810 } | |
811 | |
812 DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 2, 0, | |
813 "Define COMMAND as a prefix command.\n\ | |
814 A new sparse keymap is stored as COMMAND's function definition and its value.\n\ | |
362 | 815 If a second optional argument MAPVAR is given, the map is stored as\n\ |
816 its value instead of as COMMAND's value; but COMMAND is still defined\n\ | |
817 as a function.") | |
250 | 818 (name, mapvar) |
819 Lisp_Object name, mapvar; | |
820 { | |
821 Lisp_Object map; | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
822 map = Fmake_sparse_keymap (Qnil); |
250 | 823 Ffset (name, map); |
485 | 824 if (!NILP (mapvar)) |
250 | 825 Fset (mapvar, map); |
826 else | |
827 Fset (name, map); | |
828 return name; | |
829 } | |
830 | |
831 DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0, | |
832 "Select KEYMAP as the global keymap.") | |
833 (keymap) | |
834 Lisp_Object keymap; | |
835 { | |
836 keymap = get_keymap (keymap); | |
837 current_global_map = keymap; | |
838 return Qnil; | |
839 } | |
840 | |
841 DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0, | |
842 "Select KEYMAP as the local keymap.\n\ | |
843 If KEYMAP is nil, that means no local keymap.") | |
844 (keymap) | |
845 Lisp_Object keymap; | |
846 { | |
485 | 847 if (!NILP (keymap)) |
250 | 848 keymap = get_keymap (keymap); |
849 | |
850 current_buffer->keymap = keymap; | |
851 | |
852 return Qnil; | |
853 } | |
854 | |
855 DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0, | |
856 "Return current buffer's local keymap, or nil if it has none.") | |
857 () | |
858 { | |
859 return current_buffer->keymap; | |
860 } | |
861 | |
862 DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0, | |
863 "Return the current global keymap.") | |
864 () | |
865 { | |
866 return current_global_map; | |
867 } | |
465 | 868 |
869 DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0, | |
870 "Return a list of keymaps for the minor modes of the current buffer.") | |
871 () | |
872 { | |
873 Lisp_Object *maps; | |
874 int nmaps = current_minor_maps (0, &maps); | |
875 | |
876 return Flist (nmaps, maps); | |
877 } | |
250 | 878 |
465 | 879 /* Help functions for describing and documenting keymaps. */ |
880 | |
250 | 881 DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps, |
882 1, 1, 0, | |
883 "Find all keymaps accessible via prefix characters from KEYMAP.\n\ | |
884 Returns a list of elements of the form (KEYS . MAP), where the sequence\n\ | |
885 KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\ | |
886 so that the KEYS increase in length. The first element is (\"\" . KEYMAP).") | |
887 (startmap) | |
888 Lisp_Object startmap; | |
889 { | |
890 Lisp_Object maps, tail; | |
891 | |
892 maps = Fcons (Fcons (build_string (""), get_keymap (startmap)), Qnil); | |
893 tail = maps; | |
894 | |
895 /* For each map in the list maps, | |
896 look at any other maps it points to, | |
897 and stick them at the end if they are not already in the list. | |
898 | |
899 This is a breadth-first traversal, where tail is the queue of | |
900 nodes, and maps accumulates a list of all nodes visited. */ | |
901 | |
485 | 902 while (!NILP (tail)) |
250 | 903 { |
904 register Lisp_Object thisseq = Fcar (Fcar (tail)); | |
905 register Lisp_Object thismap = Fcdr (Fcar (tail)); | |
906 Lisp_Object last = make_number (XINT (Flength (thisseq)) - 1); | |
907 | |
908 /* Does the current sequence end in the meta-prefix-char? */ | |
909 int is_metized = (XINT (last) >= 0 | |
910 && EQ (Faref (thisseq, last), meta_prefix_char)); | |
911 | |
912 /* Skip the 'keymap element of the list. */ | |
913 thismap = Fcdr (thismap); | |
914 | |
915 if (CONSP (thismap)) | |
916 { | |
917 register Lisp_Object table = XCONS (thismap)->car; | |
918 | |
919 if (XTYPE (table) == Lisp_Vector) | |
920 { | |
921 register int i; | |
922 | |
923 /* Vector keymap. Scan all the elements. */ | |
924 for (i = 0; i < DENSE_TABLE_SIZE; i++) | |
925 { | |
926 register Lisp_Object tem; | |
927 register Lisp_Object cmd; | |
928 | |
929 cmd = get_keyelt (XVECTOR (table)->contents[i]); | |
485 | 930 if (NILP (cmd)) continue; |
250 | 931 tem = Fkeymapp (cmd); |
485 | 932 if (!NILP (tem)) |
250 | 933 { |
934 cmd = get_keymap (cmd); | |
935 /* Ignore keymaps that are already added to maps. */ | |
936 tem = Frassq (cmd, maps); | |
485 | 937 if (NILP (tem)) |
250 | 938 { |
939 /* If the last key in thisseq is meta-prefix-char, | |
940 turn it into a meta-ized keystroke. We know | |
941 that the event we're about to append is an | |
942 ascii keystroke. */ | |
943 if (is_metized) | |
944 { | |
945 tem = Fcopy_sequence (thisseq); | |
946 Faset (tem, last, make_number (i | 0200)); | |
947 | |
948 /* This new sequence is the same length as | |
949 thisseq, so stick it in the list right | |
950 after this one. */ | |
951 XCONS (tail)->cdr = | |
952 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr); | |
953 } | |
954 else | |
955 { | |
956 tem = append_key (thisseq, make_number (i)); | |
957 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil)); | |
958 } | |
959 } | |
960 } | |
961 } | |
962 | |
963 /* Once finished with the lookup elements of the dense | |
964 keymap, go on to scan its assoc list. */ | |
965 thismap = XCONS (thismap)->cdr; | |
966 } | |
967 } | |
968 | |
969 /* The rest is an alist. Scan all the alist elements. */ | |
970 while (CONSP (thismap)) | |
971 { | |
972 Lisp_Object elt = XCONS (thismap)->car; | |
973 | |
974 /* Ignore elements that are not conses. */ | |
975 if (CONSP (elt)) | |
976 { | |
977 register Lisp_Object cmd = get_keyelt (XCONS (elt)->cdr); | |
978 register Lisp_Object tem; | |
979 | |
980 /* Ignore definitions that aren't keymaps themselves. */ | |
981 tem = Fkeymapp (cmd); | |
485 | 982 if (!NILP (tem)) |
250 | 983 { |
984 /* Ignore keymaps that have been seen already. */ | |
985 cmd = get_keymap (cmd); | |
986 tem = Frassq (cmd, maps); | |
485 | 987 if (NILP (tem)) |
250 | 988 { |
989 /* let elt be the event defined by this map entry. */ | |
990 elt = XCONS (elt)->car; | |
991 | |
992 /* If the last key in thisseq is meta-prefix-char, and | |
993 this entry is a binding for an ascii keystroke, | |
994 turn it into a meta-ized keystroke. */ | |
995 if (is_metized && XTYPE (elt) == Lisp_Int) | |
996 { | |
997 tem = Fcopy_sequence (thisseq); | |
998 Faset (tem, last, make_number (XINT (elt) | 0200)); | |
999 | |
1000 /* This new sequence is the same length as | |
1001 thisseq, so stick it in the list right | |
1002 after this one. */ | |
1003 XCONS (tail)->cdr = | |
1004 Fcons (Fcons (tem, cmd), XCONS (tail)->cdr); | |
1005 } | |
1006 else | |
1007 nconc2 (tail, | |
1008 Fcons (Fcons (append_key (thisseq, elt), cmd), | |
1009 Qnil)); | |
1010 } | |
1011 } | |
1012 } | |
1013 | |
1014 thismap = XCONS (thismap)->cdr; | |
1015 } | |
1016 | |
1017 tail = Fcdr (tail); | |
1018 } | |
1019 | |
1020 return maps; | |
1021 } | |
1022 | |
1023 Lisp_Object Qsingle_key_description, Qkey_description; | |
1024 | |
1025 DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0, | |
1026 "Return a pretty description of key-sequence KEYS.\n\ | |
1027 Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\ | |
1028 spaces are put between sequence elements, etc.") | |
1029 (keys) | |
1030 Lisp_Object keys; | |
1031 { | |
1032 return Fmapconcat (Qsingle_key_description, keys, build_string (" ")); | |
1033 } | |
1034 | |
1035 char * | |
1036 push_key_description (c, p) | |
1037 register unsigned int c; | |
1038 register char *p; | |
1039 { | |
1040 if (c >= 0200) | |
1041 { | |
1042 *p++ = 'M'; | |
1043 *p++ = '-'; | |
1044 c -= 0200; | |
1045 } | |
1046 if (c < 040) | |
1047 { | |
1048 if (c == 033) | |
1049 { | |
1050 *p++ = 'E'; | |
1051 *p++ = 'S'; | |
1052 *p++ = 'C'; | |
1053 } | |
1054 else if (c == Ctl('I')) | |
1055 { | |
1056 *p++ = 'T'; | |
1057 *p++ = 'A'; | |
1058 *p++ = 'B'; | |
1059 } | |
1060 else if (c == Ctl('J')) | |
1061 { | |
1062 *p++ = 'L'; | |
1063 *p++ = 'F'; | |
1064 *p++ = 'D'; | |
1065 } | |
1066 else if (c == Ctl('M')) | |
1067 { | |
1068 *p++ = 'R'; | |
1069 *p++ = 'E'; | |
1070 *p++ = 'T'; | |
1071 } | |
1072 else | |
1073 { | |
1074 *p++ = 'C'; | |
1075 *p++ = '-'; | |
1076 if (c > 0 && c <= Ctl ('Z')) | |
1077 *p++ = c + 0140; | |
1078 else | |
1079 *p++ = c + 0100; | |
1080 } | |
1081 } | |
1082 else if (c == 0177) | |
1083 { | |
1084 *p++ = 'D'; | |
1085 *p++ = 'E'; | |
1086 *p++ = 'L'; | |
1087 } | |
1088 else if (c == ' ') | |
1089 { | |
1090 *p++ = 'S'; | |
1091 *p++ = 'P'; | |
1092 *p++ = 'C'; | |
1093 } | |
1094 else | |
1095 *p++ = c; | |
1096 | |
1097 return p; | |
1098 } | |
1099 | |
1100 DEFUN ("single-key-description", Fsingle_key_description, Ssingle_key_description, 1, 1, 0, | |
1101 "Return a pretty description of command character KEY.\n\ | |
1102 Control characters turn into C-whatever, etc.") | |
1103 (key) | |
1104 Lisp_Object key; | |
1105 { | |
1106 register unsigned char c; | |
1107 char tem[6]; | |
1108 | |
517 | 1109 if (EVENT_HAS_PARAMETERS (key)) |
1110 key = EVENT_HEAD (key); | |
1111 | |
250 | 1112 switch (XTYPE (key)) |
1113 { | |
1114 case Lisp_Int: /* Normal character */ | |
1115 c = XINT (key) & 0377; | |
1116 *push_key_description (c, tem) = 0; | |
1117 return build_string (tem); | |
1118 | |
1119 case Lisp_Symbol: /* Function key or event-symbol */ | |
1120 return Fsymbol_name (key); | |
1121 | |
1122 default: | |
1123 error ("KEY must be an integer, cons, or symbol."); | |
1124 } | |
1125 } | |
1126 | |
1127 char * | |
1128 push_text_char_description (c, p) | |
1129 register unsigned int c; | |
1130 register char *p; | |
1131 { | |
1132 if (c >= 0200) | |
1133 { | |
1134 *p++ = 'M'; | |
1135 *p++ = '-'; | |
1136 c -= 0200; | |
1137 } | |
1138 if (c < 040) | |
1139 { | |
1140 *p++ = '^'; | |
1141 *p++ = c + 64; /* 'A' - 1 */ | |
1142 } | |
1143 else if (c == 0177) | |
1144 { | |
1145 *p++ = '^'; | |
1146 *p++ = '?'; | |
1147 } | |
1148 else | |
1149 *p++ = c; | |
1150 return p; | |
1151 } | |
1152 | |
1153 DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0, | |
1154 "Return a pretty description of file-character CHAR.\n\ | |
1155 Control characters turn into \"^char\", etc.") | |
1156 (chr) | |
1157 Lisp_Object chr; | |
1158 { | |
1159 char tem[6]; | |
1160 | |
1161 CHECK_NUMBER (chr, 0); | |
1162 | |
1163 *push_text_char_description (XINT (chr) & 0377, tem) = 0; | |
1164 | |
1165 return build_string (tem); | |
1166 } | |
1167 | |
465 | 1168 /* where-is - finding a command in a set of keymaps. */ |
1169 | |
250 | 1170 DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 5, 0, |
1171 "Return list of keys that invoke DEFINITION in KEYMAP or KEYMAP1.\n\ | |
1172 If KEYMAP is nil, search only KEYMAP1.\n\ | |
1173 If KEYMAP1 is nil, use the current global map.\n\ | |
1174 \n\ | |
1175 If optional 4th arg FIRSTONLY is non-nil,\n\ | |
1176 return a string representing the first key sequence found,\n\ | |
1177 rather than a list of all possible key sequences.\n\ | |
1178 \n\ | |
1179 If optional 5th arg NOINDIRECT is non-nil, don't follow indirections\n\ | |
1180 to other keymaps or slots. This makes it possible to search for an\n\ | |
1181 indirect definition itself.") | |
1182 (definition, local_keymap, global_keymap, firstonly, noindirect) | |
1183 Lisp_Object definition, local_keymap, global_keymap; | |
1184 Lisp_Object firstonly, noindirect; | |
1185 { | |
1186 register Lisp_Object maps; | |
1187 Lisp_Object found; | |
1188 | |
485 | 1189 if (NILP (global_keymap)) |
250 | 1190 global_keymap = current_global_map; |
1191 | |
485 | 1192 if (!NILP (local_keymap)) |
250 | 1193 maps = nconc2 (Faccessible_keymaps (get_keymap (local_keymap)), |
1194 Faccessible_keymaps (get_keymap (global_keymap))); | |
1195 else | |
1196 maps = Faccessible_keymaps (get_keymap (global_keymap)); | |
1197 | |
1198 found = Qnil; | |
1199 | |
485 | 1200 for (; !NILP (maps); maps = Fcdr (maps)) |
250 | 1201 { |
1202 register this = Fcar (Fcar (maps)); /* Key sequence to reach map */ | |
1203 register map = Fcdr (Fcar (maps)); /* The map that it reaches */ | |
1204 register dense_alist; | |
1205 register int i = 0; | |
1206 | |
1207 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into | |
1208 [M-CHAR] sequences, check if last character of the sequence | |
1209 is the meta-prefix char. */ | |
1210 Lisp_Object last = make_number (XINT (Flength (this)) - 1); | |
1211 int last_is_meta = (XINT (last) >= 0 | |
1212 && EQ (Faref (this, last), meta_prefix_char)); | |
1213 | |
1214 /* Skip the 'keymap element of the list. */ | |
1215 map = Fcdr (map); | |
1216 | |
1217 /* If the keymap is sparse, map traverses the alist to the end. | |
1218 | |
1219 If the keymap is dense, we set map to the vector and | |
1220 dense_alist to the assoc-list portion of the keymap. When we | |
1221 are finished dealing with the vector portion, we set map to | |
1222 dense_alist, and handle the rest like a sparse keymap. */ | |
1223 if (XTYPE (XCONS (map)->car) == Lisp_Vector) | |
1224 { | |
1225 dense_alist = XCONS (map)->cdr; | |
1226 map = XCONS (map)->car; | |
1227 } | |
1228 | |
1229 while (1) | |
1230 { | |
1231 register Lisp_Object key, binding, sequence; | |
1232 | |
1233 QUIT; | |
1234 if (XTYPE (map) == Lisp_Vector) | |
1235 { | |
1236 /* In a vector, look at each element. */ | |
1237 binding = XVECTOR (map)->contents[i]; | |
1238 XFASTINT (key) = i; | |
1239 i++; | |
1240 | |
1241 /* If we've just finished scanning a vector, switch map to | |
1242 the assoc-list at the end of the vector. */ | |
1243 if (i >= DENSE_TABLE_SIZE) | |
1244 map = dense_alist; | |
1245 } | |
1246 else if (CONSP (map)) | |
1247 { | |
1248 /* In an alist, ignore elements that aren't conses. */ | |
1249 if (! CONSP (XCONS (map)->car)) | |
1250 { | |
1251 /* Ignore other elements. */ | |
1252 map = Fcdr (map); | |
1253 continue; | |
1254 } | |
1255 binding = Fcdr (Fcar (map)); | |
1256 key = Fcar (Fcar (map)); | |
1257 map = Fcdr (map); | |
1258 } | |
1259 else | |
1260 break; | |
1261 | |
1262 /* Search through indirections unless that's not wanted. */ | |
485 | 1263 if (NILP (noindirect)) |
250 | 1264 binding = get_keyelt (binding); |
1265 | |
1266 /* End this iteration if this element does not match | |
1267 the target. */ | |
1268 | |
1269 if (XTYPE (definition) == Lisp_Cons) | |
1270 { | |
1271 Lisp_Object tem; | |
1272 tem = Fequal (binding, definition); | |
485 | 1273 if (NILP (tem)) |
250 | 1274 continue; |
1275 } | |
1276 else | |
1277 if (!EQ (binding, definition)) | |
1278 continue; | |
1279 | |
1280 /* We have found a match. | |
1281 Construct the key sequence where we found it. */ | |
1282 if (XTYPE (key) == Lisp_Int && last_is_meta) | |
1283 { | |
1284 sequence = Fcopy_sequence (this); | |
1285 Faset (sequence, last, make_number (XINT (key) | 0200)); | |
1286 } | |
1287 else | |
1288 sequence = append_key (this, key); | |
1289 | |
1290 /* Verify that this key binding is not shadowed by another | |
1291 binding for the same key, before we say it exists. | |
1292 | |
1293 Mechanism: look for local definition of this key and if | |
1294 it is defined and does not match what we found then | |
1295 ignore this key. | |
1296 | |
1297 Either nil or number as value from Flookup_key | |
1298 means undefined. */ | |
485 | 1299 if (!NILP (local_keymap)) |
250 | 1300 { |
1301 binding = Flookup_key (local_keymap, sequence); | |
485 | 1302 if (!NILP (binding) && XTYPE (binding) != Lisp_Int) |
250 | 1303 { |
1304 if (XTYPE (definition) == Lisp_Cons) | |
1305 { | |
1306 Lisp_Object tem; | |
1307 tem = Fequal (binding, definition); | |
485 | 1308 if (NILP (tem)) |
250 | 1309 continue; |
1310 } | |
1311 else | |
1312 if (!EQ (binding, definition)) | |
1313 continue; | |
1314 } | |
1315 } | |
1316 | |
1317 /* It is a true unshadowed match. Record it. */ | |
1318 | |
485 | 1319 if (!NILP (firstonly)) |
250 | 1320 return sequence; |
1321 found = Fcons (sequence, found); | |
1322 } | |
1323 } | |
1324 return Fnreverse (found); | |
1325 } | |
1326 | |
1327 /* Return a string listing the keys and buttons that run DEFINITION. */ | |
1328 | |
1329 static Lisp_Object | |
1330 where_is_string (definition) | |
1331 Lisp_Object definition; | |
1332 { | |
1333 register Lisp_Object keys, keys1; | |
1334 | |
1335 keys = Fwhere_is_internal (definition, | |
1336 current_buffer->keymap, Qnil, Qnil, Qnil); | |
1337 keys1 = Fmapconcat (Qkey_description, keys, build_string (", ")); | |
1338 | |
1339 return keys1; | |
1340 } | |
1341 | |
1342 DEFUN ("where-is", Fwhere_is, Swhere_is, 1, 1, "CWhere is command: ", | |
1343 "Print message listing key sequences that invoke specified command.\n\ | |
1344 Argument is a command definition, usually a symbol with a function definition.") | |
1345 (definition) | |
1346 Lisp_Object definition; | |
1347 { | |
1348 register Lisp_Object string; | |
1349 | |
1350 CHECK_SYMBOL (definition, 0); | |
1351 string = where_is_string (definition); | |
1352 | |
1353 if (XSTRING (string)->size) | |
1354 message ("%s is on %s", XSYMBOL (definition)->name->data, | |
1355 XSTRING (string)->data); | |
1356 else | |
1357 message ("%s is not on any key", XSYMBOL (definition)->name->data); | |
1358 return Qnil; | |
1359 } | |
1360 | |
465 | 1361 /* describe-bindings - summarizing all the bindings in a set of keymaps. */ |
1362 | |
250 | 1363 DEFUN ("describe-bindings", Fdescribe_bindings, Sdescribe_bindings, 0, 0, "", |
1364 "Show a list of all defined keys, and their definitions.\n\ | |
1365 The list is put in a buffer, which is displayed.") | |
1366 () | |
1367 { | |
1368 register Lisp_Object thisbuf; | |
1369 XSET (thisbuf, Lisp_Buffer, current_buffer); | |
1370 internal_with_output_to_temp_buffer ("*Help*", | |
1371 describe_buffer_bindings, | |
1372 thisbuf); | |
1373 return Qnil; | |
1374 } | |
1375 | |
1376 static Lisp_Object | |
1377 describe_buffer_bindings (descbuf) | |
1378 Lisp_Object descbuf; | |
1379 { | |
1380 register Lisp_Object start1, start2; | |
1381 | |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1382 char *key_heading |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1383 = "\ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1384 key binding\n\ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1385 --- -------\n"; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1386 char *alternate_heading |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1387 = "\ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1388 Alternate Characters (use anywhere the nominal character is listed):\n\ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1389 nominal alternate\n\ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1390 ------- ---------\n"; |
250 | 1391 |
1392 Fset_buffer (Vstandard_output); | |
1393 | |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1394 /* Report on alternates for keys. */ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1395 if (XTYPE (Vkeyboard_translate_table) == Lisp_String) |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1396 { |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1397 int c; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1398 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1399 int translate_len = XSTRING (Vkeyboard_translate_table)->size; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1400 |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1401 for (c = 0; c < translate_len; c++) |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1402 if (translate[c] != c) |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1403 { |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1404 char buf[20]; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1405 char *bufend; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1406 |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1407 if (alternate_heading) |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1408 { |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1409 insert_string (alternate_heading); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1410 alternate_heading = 0; |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1411 } |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1412 |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1413 bufend = push_key_description (translate[c], buf); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1414 insert (buf, bufend - buf); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1415 Findent_to (make_number (16), make_number (1)); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1416 bufend = push_key_description (c, buf); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1417 insert (buf, bufend - buf); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1418 |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1419 insert ("\n", 1); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1420 } |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1421 |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1422 insert ("\n", 1); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1423 } |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1424 |
465 | 1425 { |
1426 int i, nmaps; | |
1427 Lisp_Object *modes, *maps; | |
1428 | |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1429 /* Temporarily switch to descbuf, so that we can get that buffer's |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1430 minor modes correctly. */ |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1431 Fset_buffer (descbuf); |
465 | 1432 nmaps = current_minor_maps (&modes, &maps); |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1433 Fset_buffer (Vstandard_output); |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1434 |
465 | 1435 for (i = 0; i < nmaps; i++) |
1436 { | |
1437 if (XTYPE (modes[i]) == Lisp_Symbol) | |
1438 { | |
1439 insert_char ('`'); | |
1440 insert_string (XSYMBOL (modes[i])->name->data); | |
1441 insert_char ('\''); | |
1442 } | |
1443 else | |
1444 insert_string ("Strangely Named"); | |
1445 insert_string (" Minor Mode Bindings:\n"); | |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1446 insert_string (key_heading); |
465 | 1447 describe_map_tree (maps[i], 0, Qnil); |
1448 insert_char ('\n'); | |
1449 } | |
1450 } | |
1451 | |
250 | 1452 start1 = XBUFFER (descbuf)->keymap; |
485 | 1453 if (!NILP (start1)) |
250 | 1454 { |
1455 insert_string ("Local Bindings:\n"); | |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1456 insert_string (key_heading); |
465 | 1457 describe_map_tree (start1, 0, Qnil); |
250 | 1458 insert_string ("\n"); |
1459 } | |
1460 | |
1461 insert_string ("Global Bindings:\n"); | |
1120
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1462 if (NILP (start1)) |
0a486e1a45bc
* keymap.c (describe_buffer_bindings): Adjust key_heading to match
Jim Blandy <jimb@redhat.com>
parents:
1095
diff
changeset
|
1463 insert_string (key_heading); |
250 | 1464 |
465 | 1465 describe_map_tree (current_global_map, 0, XBUFFER (descbuf)->keymap); |
250 | 1466 |
1467 Fset_buffer (descbuf); | |
1468 return Qnil; | |
1469 } | |
1470 | |
1471 /* Insert a desription of the key bindings in STARTMAP, | |
1472 followed by those of all maps reachable through STARTMAP. | |
1473 If PARTIAL is nonzero, omit certain "uninteresting" commands | |
1474 (such as `undefined'). | |
1475 If SHADOW is non-nil, it is another map; | |
1476 don't mention keys which would be shadowed by it. */ | |
1477 | |
1478 void | |
1479 describe_map_tree (startmap, partial, shadow) | |
1480 Lisp_Object startmap, shadow; | |
1481 int partial; | |
1482 { | |
1483 register Lisp_Object elt, sh; | |
1484 Lisp_Object maps; | |
1485 struct gcpro gcpro1; | |
1486 | |
1487 maps = Faccessible_keymaps (startmap); | |
1488 GCPRO1 (maps); | |
1489 | |
485 | 1490 for (; !NILP (maps); maps = Fcdr (maps)) |
250 | 1491 { |
1492 elt = Fcar (maps); | |
1493 sh = Fcar (elt); | |
1494 | |
1495 /* If there is no shadow keymap given, don't shadow. */ | |
485 | 1496 if (NILP (shadow)) |
250 | 1497 sh = Qnil; |
1498 | |
1499 /* If the sequence by which we reach this keymap is zero-length, | |
1500 then the shadow map for this keymap is just SHADOW. */ | |
1501 else if ((XTYPE (sh) == Lisp_String | |
1502 && XSTRING (sh)->size == 0) | |
1503 || (XTYPE (sh) == Lisp_Vector | |
1504 && XVECTOR (sh)->size == 0)) | |
1505 sh = shadow; | |
1506 | |
1507 /* If the sequence by which we reach this keymap actually has | |
1508 some elements, then the sequence's definition in SHADOW is | |
1509 what we should use. */ | |
1510 else | |
1511 { | |
1512 sh = Flookup_key (shadow, Fcar (elt)); | |
1513 if (XTYPE (sh) == Lisp_Int) | |
1514 sh = Qnil; | |
1515 } | |
1516 | |
1517 /* If sh is null (meaning that the current map is not shadowed), | |
1518 or a keymap (meaning that bindings from the current map might | |
1519 show through), describe the map. Otherwise, sh is a command | |
1520 that completely shadows the current map, and we shouldn't | |
1521 bother. */ | |
485 | 1522 if (NILP (sh) || !NILP (Fkeymapp (sh))) |
250 | 1523 describe_map (Fcdr (elt), Fcar (elt), partial, sh); |
1524 } | |
1525 | |
1526 UNGCPRO; | |
1527 } | |
1528 | |
1529 static void | |
1530 describe_command (definition) | |
1531 Lisp_Object definition; | |
1532 { | |
1533 register Lisp_Object tem1; | |
1534 | |
1535 Findent_to (make_number (16), make_number (1)); | |
1536 | |
1537 if (XTYPE (definition) == Lisp_Symbol) | |
1538 { | |
1539 XSET (tem1, Lisp_String, XSYMBOL (definition)->name); | |
1540 insert1 (tem1); | |
1541 insert_string ("\n"); | |
1542 } | |
1543 else | |
1544 { | |
1545 tem1 = Fkeymapp (definition); | |
485 | 1546 if (!NILP (tem1)) |
250 | 1547 insert_string ("Prefix Command\n"); |
1548 else | |
1549 insert_string ("??\n"); | |
1550 } | |
1551 } | |
1552 | |
1553 /* Describe the contents of map MAP, assuming that this map itself is | |
1554 reached by the sequence of prefix keys KEYS (a string or vector). | |
1555 PARTIAL, SHADOW is as in `describe_map_tree' above. */ | |
1556 | |
1557 static void | |
1558 describe_map (map, keys, partial, shadow) | |
1559 Lisp_Object map, keys; | |
1560 int partial; | |
1561 Lisp_Object shadow; | |
1562 { | |
1563 register Lisp_Object keysdesc; | |
1564 | |
485 | 1565 if (!NILP (keys) && Flength (keys) > 0) |
250 | 1566 keysdesc = concat2 (Fkey_description (keys), |
1567 build_string (" ")); | |
1568 else | |
1569 keysdesc = Qnil; | |
1570 | |
1571 /* Skip the 'keymap element of the list. */ | |
1572 map = Fcdr (map); | |
1573 | |
1574 /* If this is a dense keymap, take care of the table. */ | |
1575 if (CONSP (map) | |
1576 && XTYPE (XCONS (map)->car) == Lisp_Vector) | |
1577 { | |
1578 describe_vector (XCONS (map)->car, keysdesc, describe_command, | |
1579 partial, shadow); | |
1580 map = XCONS (map)->cdr; | |
1581 } | |
1582 | |
1583 /* Now map is an alist. */ | |
1584 describe_alist (map, keysdesc, describe_command, partial, shadow); | |
1585 } | |
1586 | |
1587 /* Insert a description of ALIST into the current buffer. | |
1588 Note that ALIST is just a plain association list, not a keymap. */ | |
1589 | |
1590 static void | |
1591 describe_alist (alist, elt_prefix, elt_describer, partial, shadow) | |
1592 register Lisp_Object alist; | |
1593 Lisp_Object elt_prefix; | |
1594 int (*elt_describer) (); | |
1595 int partial; | |
1596 Lisp_Object shadow; | |
1597 { | |
1598 Lisp_Object this; | |
1599 Lisp_Object tem1, tem2 = Qnil; | |
1600 Lisp_Object suppress; | |
1601 Lisp_Object kludge; | |
1602 int first = 1; | |
1603 struct gcpro gcpro1, gcpro2, gcpro3; | |
1604 | |
1605 if (partial) | |
1606 suppress = intern ("suppress-keymap"); | |
1607 | |
1608 /* This vector gets used to present single keys to Flookup_key. Since | |
1609 that is done once per alist element, we don't want to cons up a | |
1610 fresh vector every time. */ | |
1611 kludge = Fmake_vector (make_number (1), Qnil); | |
1612 | |
1613 GCPRO3 (elt_prefix, tem2, kludge); | |
1614 | |
1615 for (; CONSP (alist); alist = Fcdr (alist)) | |
1616 { | |
1617 QUIT; | |
1618 tem1 = Fcar_safe (Fcar (alist)); | |
1619 tem2 = get_keyelt (Fcdr_safe (Fcar (alist))); | |
1620 | |
1621 /* Don't show undefined commands or suppressed commands. */ | |
485 | 1622 if (NILP (tem2)) continue; |
250 | 1623 if (XTYPE (tem2) == Lisp_Symbol && partial) |
1624 { | |
1625 this = Fget (tem2, suppress); | |
485 | 1626 if (!NILP (this)) |
250 | 1627 continue; |
1628 } | |
1629 | |
1630 /* Don't show a command that isn't really visible | |
1631 because a local definition of the same key shadows it. */ | |
1632 | |
485 | 1633 if (!NILP (shadow)) |
250 | 1634 { |
1635 Lisp_Object tem; | |
1636 | |
1637 XVECTOR (kludge)->contents[0] = tem1; | |
1638 tem = Flookup_key (shadow, kludge); | |
485 | 1639 if (!NILP (tem)) continue; |
250 | 1640 } |
1641 | |
1642 if (first) | |
1643 { | |
1644 insert ("\n", 1); | |
1645 first = 0; | |
1646 } | |
1647 | |
485 | 1648 if (!NILP (elt_prefix)) |
250 | 1649 insert1 (elt_prefix); |
1650 | |
1651 /* THIS gets the string to describe the character TEM1. */ | |
1652 this = Fsingle_key_description (tem1); | |
1653 insert1 (this); | |
1654 | |
1655 /* Print a description of the definition of this character. | |
1656 elt_describer will take care of spacing out far enough | |
1657 for alignment purposes. */ | |
1658 (*elt_describer) (tem2); | |
1659 } | |
1660 | |
1661 UNGCPRO; | |
1662 } | |
1663 | |
1664 static int | |
1665 describe_vector_princ (elt) | |
1666 Lisp_Object elt; | |
1667 { | |
1668 Fprinc (elt, Qnil); | |
1669 } | |
1670 | |
1671 DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0, | |
1672 "Print on `standard-output' a description of contents of VECTOR.\n\ | |
1673 This is text showing the elements of vector matched against indices.") | |
1674 (vector) | |
1675 Lisp_Object vector; | |
1676 { | |
1677 CHECK_VECTOR (vector, 0); | |
1678 describe_vector (vector, Qnil, describe_vector_princ, 0, Qnil, Qnil); | |
1679 } | |
1680 | |
1681 describe_vector (vector, elt_prefix, elt_describer, partial, shadow) | |
1682 register Lisp_Object vector; | |
1683 Lisp_Object elt_prefix; | |
1684 int (*elt_describer) (); | |
1685 int partial; | |
1686 Lisp_Object shadow; | |
1687 { | |
1688 Lisp_Object this; | |
1689 Lisp_Object dummy; | |
1690 Lisp_Object tem1, tem2; | |
1691 register int i; | |
1692 Lisp_Object suppress; | |
1693 Lisp_Object kludge; | |
1694 int first = 1; | |
1695 struct gcpro gcpro1, gcpro2, gcpro3; | |
1696 | |
1697 tem1 = Qnil; | |
1698 | |
1699 /* This vector gets used to present single keys to Flookup_key. Since | |
1700 that is done once per vector element, we don't want to cons up a | |
1701 fresh vector every time. */ | |
1702 kludge = Fmake_vector (make_number (1), Qnil); | |
1703 GCPRO3 (elt_prefix, tem1, kludge); | |
1704 | |
1705 if (partial) | |
1706 suppress = intern ("suppress-keymap"); | |
1707 | |
1708 for (i = 0; i < DENSE_TABLE_SIZE; i++) | |
1709 { | |
1710 QUIT; | |
1711 tem1 = get_keyelt (XVECTOR (vector)->contents[i]); | |
1712 | |
485 | 1713 if (NILP (tem1)) continue; |
250 | 1714 |
1715 /* Don't mention suppressed commands. */ | |
1716 if (XTYPE (tem1) == Lisp_Symbol && partial) | |
1717 { | |
1718 this = Fget (tem1, suppress); | |
485 | 1719 if (!NILP (this)) |
250 | 1720 continue; |
1721 } | |
1722 | |
1723 /* If this command in this map is shadowed by some other map, | |
1724 ignore it. */ | |
485 | 1725 if (!NILP (shadow)) |
250 | 1726 { |
1727 Lisp_Object tem; | |
1728 | |
1729 XVECTOR (kludge)->contents[0] = make_number (i); | |
1730 tem = Flookup_key (shadow, kludge); | |
1731 | |
485 | 1732 if (!NILP (tem)) continue; |
250 | 1733 } |
1734 | |
1735 if (first) | |
1736 { | |
1737 insert ("\n", 1); | |
1738 first = 0; | |
1739 } | |
1740 | |
1741 /* Output the prefix that applies to every entry in this map. */ | |
485 | 1742 if (!NILP (elt_prefix)) |
250 | 1743 insert1 (elt_prefix); |
1744 | |
1745 /* Get the string to describe the character I, and print it. */ | |
1746 XFASTINT (dummy) = i; | |
1747 | |
1748 /* THIS gets the string to describe the character DUMMY. */ | |
1749 this = Fsingle_key_description (dummy); | |
1750 insert1 (this); | |
1751 | |
1752 /* Find all consecutive characters that have the same definition. */ | |
1753 while (i + 1 < DENSE_TABLE_SIZE | |
1754 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i+1]), | |
1755 EQ (tem2, tem1))) | |
1756 i++; | |
1757 | |
1758 /* If we have a range of more than one character, | |
1759 print where the range reaches to. */ | |
1760 | |
1761 if (i != XINT (dummy)) | |
1762 { | |
1763 insert (" .. ", 4); | |
485 | 1764 if (!NILP (elt_prefix)) |
250 | 1765 insert1 (elt_prefix); |
1766 | |
1767 XFASTINT (dummy) = i; | |
1768 insert1 (Fsingle_key_description (dummy)); | |
1769 } | |
1770 | |
1771 /* Print a description of the definition of this character. | |
1772 elt_describer will take care of spacing out far enough | |
1773 for alignment purposes. */ | |
1774 (*elt_describer) (tem1); | |
1775 } | |
1776 | |
1777 UNGCPRO; | |
1778 } | |
1779 | |
465 | 1780 /* Apropos - finding all symbols whose names match a regexp. */ |
250 | 1781 Lisp_Object apropos_predicate; |
1782 Lisp_Object apropos_accumulate; | |
1783 | |
1784 static void | |
1785 apropos_accum (symbol, string) | |
1786 Lisp_Object symbol, string; | |
1787 { | |
1788 register Lisp_Object tem; | |
1789 | |
1790 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil); | |
485 | 1791 if (!NILP (tem) && !NILP (apropos_predicate)) |
250 | 1792 tem = call1 (apropos_predicate, symbol); |
485 | 1793 if (!NILP (tem)) |
250 | 1794 apropos_accumulate = Fcons (symbol, apropos_accumulate); |
1795 } | |
1796 | |
1797 DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0, | |
1798 "Show all symbols whose names contain match for REGEXP.\n\ | |
1799 If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done\n\ | |
1800 for each symbol and a symbol is mentioned only if that returns non-nil.\n\ | |
1801 Return list of symbols found.") | |
1802 (string, pred) | |
1803 Lisp_Object string, pred; | |
1804 { | |
1805 struct gcpro gcpro1, gcpro2; | |
1806 CHECK_STRING (string, 0); | |
1807 apropos_predicate = pred; | |
1808 GCPRO2 (apropos_predicate, apropos_accumulate); | |
1809 apropos_accumulate = Qnil; | |
1810 map_obarray (Vobarray, apropos_accum, string); | |
1811 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp); | |
1812 UNGCPRO; | |
1813 return apropos_accumulate; | |
1814 } | |
1815 | |
1816 syms_of_keymap () | |
1817 { | |
1818 Lisp_Object tem; | |
1819 | |
1820 Qkeymap = intern ("keymap"); | |
1821 staticpro (&Qkeymap); | |
1822 | |
1823 /* Initialize the keymaps standardly used. | |
1824 Each one is the value of a Lisp variable, and is also | |
1825 pointed to by a C variable */ | |
1826 | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1827 global_map = Fmake_keymap (Qnil); |
250 | 1828 Fset (intern ("global-map"), global_map); |
1829 | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1830 meta_map = Fmake_keymap (Qnil); |
250 | 1831 Fset (intern ("esc-map"), meta_map); |
1832 Ffset (intern ("ESC-prefix"), meta_map); | |
1833 | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1834 control_x_map = Fmake_keymap (Qnil); |
250 | 1835 Fset (intern ("ctl-x-map"), control_x_map); |
1836 Ffset (intern ("Control-X-prefix"), control_x_map); | |
1837 | |
1838 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map, | |
1839 "Default keymap to use when reading from the minibuffer."); | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1840 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil); |
250 | 1841 |
1842 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map, | |
1843 "Local keymap for the minibuffer when spaces are not allowed."); | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1844 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil); |
250 | 1845 |
1846 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map, | |
1847 "Local keymap for minibuffer input with completion."); | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1848 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil); |
250 | 1849 |
1850 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map, | |
1851 "Local keymap for minibuffer input with completion, for exact match."); | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1852 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil); |
250 | 1853 |
1854 current_global_map = global_map; | |
1855 | |
465 | 1856 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist, |
1857 "Alist of keymaps to use for minor modes.\n\ | |
1858 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\ | |
1859 key sequences and look up bindings iff VARIABLE's value is non-nil.\n\ | |
1860 If two active keymaps bind the same key, the keymap appearing earlier\n\ | |
1861 in the list takes precedence."); | |
1862 Vminor_mode_map_alist = Qnil; | |
1863 | |
517 | 1864 DEFVAR_LISP ("function-key-map", &Vfunction_key_map, |
1865 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\ | |
1866 This allows Emacs to recognize function keys sent from ASCII\n\ | |
1867 terminals at any point in a key sequence.\n\ | |
1868 \n\ | |
1869 The read-key-sequence function replaces subsequences bound by\n\ | |
1870 function-key-map with their bindings. When the current local and global\n\ | |
1871 keymaps have no binding for the current key sequence but\n\ | |
1872 function-key-map binds a suffix of the sequence to a vector,\n\ | |
1873 read-key-sequence replaces the matching suffix with its binding, and\n\ | |
1874 continues with the new sequence.\n\ | |
1875 \n\ | |
1876 For example, suppose function-key-map binds `ESC O P' to [pf1].\n\ | |
1877 Typing `ESC O P' to read-key-sequence would return [pf1]. Typing\n\ | |
1878 `C-x ESC O P' would return [?\C-x pf1]. If [pf1] were a prefix\n\ | |
1879 key, typing `ESC O P x' would return [pf1 x]."); | |
1095
6578f07e9eb8
(Fmake_sparse_keymap, Fmake_keymap): New optional arg. Callers changed.
Richard M. Stallman <rms@gnu.org>
parents:
647
diff
changeset
|
1880 Vfunction_key_map = Fmake_sparse_keymap (Qnil); |
517 | 1881 |
250 | 1882 Qsingle_key_description = intern ("single-key-description"); |
1883 staticpro (&Qsingle_key_description); | |
1884 | |
1885 Qkey_description = intern ("key-description"); | |
1886 staticpro (&Qkey_description); | |
1887 | |
1888 Qkeymapp = intern ("keymapp"); | |
1889 staticpro (&Qkeymapp); | |
1890 | |
1891 defsubr (&Skeymapp); | |
1892 defsubr (&Smake_keymap); | |
1893 defsubr (&Smake_sparse_keymap); | |
1894 defsubr (&Scopy_keymap); | |
1895 defsubr (&Skey_binding); | |
1896 defsubr (&Slocal_key_binding); | |
1897 defsubr (&Sglobal_key_binding); | |
465 | 1898 defsubr (&Sminor_mode_key_binding); |
250 | 1899 defsubr (&Sglobal_set_key); |
1900 defsubr (&Slocal_set_key); | |
1901 defsubr (&Sdefine_key); | |
1902 defsubr (&Slookup_key); | |
1903 defsubr (&Sglobal_unset_key); | |
1904 defsubr (&Slocal_unset_key); | |
1905 defsubr (&Sdefine_prefix_command); | |
1906 defsubr (&Suse_global_map); | |
1907 defsubr (&Suse_local_map); | |
1908 defsubr (&Scurrent_local_map); | |
1909 defsubr (&Scurrent_global_map); | |
465 | 1910 defsubr (&Scurrent_minor_mode_maps); |
250 | 1911 defsubr (&Saccessible_keymaps); |
1912 defsubr (&Skey_description); | |
1913 defsubr (&Sdescribe_vector); | |
1914 defsubr (&Ssingle_key_description); | |
1915 defsubr (&Stext_char_description); | |
1916 defsubr (&Swhere_is_internal); | |
1917 defsubr (&Swhere_is); | |
1918 defsubr (&Sdescribe_bindings); | |
1919 defsubr (&Sapropos_internal); | |
1920 } | |
1921 | |
1922 keys_of_keymap () | |
1923 { | |
1924 Lisp_Object tem; | |
1925 | |
1926 initial_define_key (global_map, 033, "ESC-prefix"); | |
1927 initial_define_key (global_map, Ctl('X'), "Control-X-prefix"); | |
1928 } |