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