(font-lock-comment-face): Change dark-background,
[bpt/emacs.git] / src / keymap.c
CommitLineData
2c6f1a39 1/* Manipulation of keymaps
68c45bf0 2 Copyright (C) 1985, 86,87,88,93,94,95,98,99 Free Software Foundation, Inc.
2c6f1a39
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
502ddf23 8the Free Software Foundation; either version 2, or (at your option)
2c6f1a39
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
2c6f1a39
JB
20
21
18160b98 22#include <config.h>
2c6f1a39 23#include <stdio.h>
2c6f1a39
JB
24#include "lisp.h"
25#include "commands.h"
26#include "buffer.h"
a98f1d1d 27#include "charset.h"
6bbbd9b0 28#include "keyboard.h"
6ba6e250 29#include "termhooks.h"
9ac0d9e0 30#include "blockinput.h"
d964248c 31#include "puresize.h"
93d2aa1c 32#include "intervals.h"
2c6f1a39
JB
33
34#define min(a, b) ((a) < (b) ? (a) : (b))
57495396 35#define KEYMAPP(m) (!NILP (Fkeymapp (m)))
2c6f1a39 36
f5b79c1c 37/* The number of elements in keymap vectors. */
2c6f1a39
JB
38#define DENSE_TABLE_SIZE (0200)
39
40/* Actually allocate storage for these variables */
41
42Lisp_Object current_global_map; /* Current global keymap */
43
44Lisp_Object global_map; /* default global key bindings */
45
46Lisp_Object meta_map; /* The keymap used for globally bound
47 ESC-prefixed default commands */
48
49Lisp_Object control_x_map; /* The keymap used for globally bound
50 C-x-prefixed default commands */
51
52/* was MinibufLocalMap */
53Lisp_Object Vminibuffer_local_map;
54 /* The keymap used by the minibuf for local
55 bindings when spaces are allowed in the
56 minibuf */
57
58/* was MinibufLocalNSMap */
59Lisp_Object Vminibuffer_local_ns_map;
60 /* The keymap used by the minibuf for local
61 bindings when spaces are not encouraged
62 in the minibuf */
63
64/* keymap used for minibuffers when doing completion */
65/* was MinibufLocalCompletionMap */
66Lisp_Object Vminibuffer_local_completion_map;
67
68/* keymap used for minibuffers when doing completion and require a match */
69/* was MinibufLocalMustMatchMap */
70Lisp_Object Vminibuffer_local_must_match_map;
71
cc0a8174
JB
72/* Alist of minor mode variables and keymaps. */
73Lisp_Object Vminor_mode_map_alist;
74
dd9cda06
RS
75/* Alist of major-mode-specific overrides for
76 minor mode variables and keymaps. */
77Lisp_Object Vminor_mode_overriding_map_alist;
78
6bbbd9b0
JB
79/* Keymap mapping ASCII function key sequences onto their preferred forms.
80 Initialized by the terminal-specific lisp files. See DEFVAR for more
81 documentation. */
82Lisp_Object Vfunction_key_map;
83
d7bf9bf5
RS
84/* Keymap mapping ASCII function key sequences onto their preferred forms. */
85Lisp_Object Vkey_translation_map;
86
107fd03d
RS
87/* A list of all commands given new bindings since a certain time
88 when nil was stored here.
89 This is used to speed up recomputation of menu key equivalents
90 when Emacs starts up. t means don't record anything here. */
91Lisp_Object Vdefine_key_rebound_commands;
92
a3fc8840 93Lisp_Object Qkeymapp, Qkeymap, Qnon_ascii, Qmenu_item;
2c6f1a39 94
3d248688
JB
95/* A char with the CHAR_META bit set in a vector or the 0200 bit set
96 in a string key sequence is equivalent to prefixing with this
97 character. */
2c6f1a39
JB
98extern Lisp_Object meta_prefix_char;
99
7d92e329
RS
100extern Lisp_Object Voverriding_local_map;
101
57495396
SM
102static Lisp_Object store_in_keymap P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
103static void fix_submap_inheritance P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
104
105static Lisp_Object define_as_prefix P_ ((Lisp_Object, Lisp_Object));
106static Lisp_Object describe_buffer_bindings P_ ((Lisp_Object));
107static void describe_command P_ ((Lisp_Object));
108static void describe_translation P_ ((Lisp_Object));
109static void describe_map P_ ((Lisp_Object, Lisp_Object,
110 void (*) P_ ((Lisp_Object)),
111 int, Lisp_Object, Lisp_Object*, int));
2c6f1a39 112\f
cc0a8174
JB
113/* Keymap object support - constructors and predicates. */
114
ce6e5d0b 115DEFUN ("make-keymap", Fmake_keymap, Smake_keymap, 0, 1, 0,
69eca94c
RS
116 "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).\n\
117CHARTABLE is a char-table that holds the bindings for the ASCII\n\
2c6f1a39
JB
118characters. ALIST is an assoc-list which holds bindings for function keys,\n\
119mouse events, and any other things that appear in the input stream.\n\
ce6e5d0b
RS
120All entries in it are initially nil, meaning \"command undefined\".\n\n\
121The optional arg STRING supplies a menu name for the keymap\n\
122in case you use it as a menu with `x-popup-menu'.")
123 (string)
124 Lisp_Object string;
2c6f1a39 125{
ce6e5d0b
RS
126 Lisp_Object tail;
127 if (!NILP (string))
128 tail = Fcons (string, Qnil);
129 else
130 tail = Qnil;
2c6f1a39 131 return Fcons (Qkeymap,
0403641f 132 Fcons (Fmake_char_table (Qkeymap, Qnil), tail));
2c6f1a39
JB
133}
134
ce6e5d0b 135DEFUN ("make-sparse-keymap", Fmake_sparse_keymap, Smake_sparse_keymap, 0, 1, 0,
2c6f1a39
JB
136 "Construct and return a new sparse-keymap list.\n\
137Its car is `keymap' and its cdr is an alist of (CHAR . DEFINITION),\n\
138which binds the character CHAR to DEFINITION, or (SYMBOL . DEFINITION),\n\
139which binds the function key or mouse event SYMBOL to DEFINITION.\n\
ce6e5d0b
RS
140Initially the alist is nil.\n\n\
141The optional arg STRING supplies a menu name for the keymap\n\
142in case you use it as a menu with `x-popup-menu'.")
143 (string)
144 Lisp_Object string;
2c6f1a39 145{
ce6e5d0b
RS
146 if (!NILP (string))
147 return Fcons (Qkeymap, Fcons (string, Qnil));
2c6f1a39
JB
148 return Fcons (Qkeymap, Qnil);
149}
150
151/* This function is used for installing the standard key bindings
152 at initialization time.
153
154 For example:
155
e25c4e44 156 initial_define_key (control_x_map, Ctl('X'), "exchange-point-and-mark"); */
2c6f1a39
JB
157
158void
159initial_define_key (keymap, key, defname)
160 Lisp_Object keymap;
161 int key;
162 char *defname;
163{
164 store_in_keymap (keymap, make_number (key), intern (defname));
165}
166
e25c4e44
JB
167void
168initial_define_lispy_key (keymap, keyname, defname)
169 Lisp_Object keymap;
170 char *keyname;
171 char *defname;
172{
173 store_in_keymap (keymap, intern (keyname), intern (defname));
174}
175
2c6f1a39
JB
176/* Define character fromchar in map frommap as an alias for character
177 tochar in map tomap. Subsequent redefinitions of the latter WILL
178 affect the former. */
179
180#if 0
181void
182synkey (frommap, fromchar, tomap, tochar)
183 struct Lisp_Vector *frommap, *tomap;
184 int fromchar, tochar;
185{
186 Lisp_Object v, c;
bff4ec1f 187 XSETVECTOR (v, tomap);
6e344130 188 XSETFASTINT (c, tochar);
2c6f1a39
JB
189 frommap->contents[fromchar] = Fcons (v, c);
190}
191#endif /* 0 */
192
193DEFUN ("keymapp", Fkeymapp, Skeymapp, 1, 1, 0,
88539837 194 "Return t if OBJECT is a keymap.\n\
1d8d96fa 195\n\
926a64aa 196A keymap is a list (keymap . ALIST),\n\
90f80bcf 197or a symbol whose function definition is itself a keymap.\n\
1d8d96fa 198ALIST elements look like (CHAR . DEFN) or (SYMBOL . DEFN);\n\
926a64aa
RS
199a vector of densely packed bindings for small character codes\n\
200is also allowed as an element.")
2c6f1a39
JB
201 (object)
202 Lisp_Object object;
203{
57495396 204 /* FIXME: Maybe this should return t for autoloaded keymaps? -sm */
d09b2024 205 return (NILP (get_keymap_1 (object, 0, 0)) ? Qnil : Qt);
2c6f1a39
JB
206}
207
208/* Check that OBJECT is a keymap (after dereferencing through any
d09b2024
JB
209 symbols). If it is, return it.
210
211 If AUTOLOAD is non-zero and OBJECT is a symbol whose function value
212 is an autoload form, do the autoload and try again.
21a0d7a0 213 If AUTOLOAD is nonzero, callers must assume GC is possible.
d09b2024
JB
214
215 ERROR controls how we respond if OBJECT isn't a keymap.
216 If ERROR is non-zero, signal an error; otherwise, just return Qnil.
217
218 Note that most of the time, we don't want to pursue autoloads.
219 Functions like Faccessible_keymaps which scan entire keymap trees
220 shouldn't load every autoloaded keymap. I'm not sure about this,
221 but it seems to me that only read_key_sequence, Flookup_key, and
df75b1a3
GM
222 Fdefine_key should cause keymaps to be autoloaded.
223
224 This function can GC when AUTOLOAD is non-zero, because it calls
225 do_autoload which can GC. */
d09b2024 226
2c6f1a39 227Lisp_Object
d09b2024 228get_keymap_1 (object, error, autoload)
2c6f1a39 229 Lisp_Object object;
d09b2024 230 int error, autoload;
2c6f1a39 231{
d09b2024 232 Lisp_Object tem;
2c6f1a39 233
d09b2024 234 autoload_retry:
b1314e15
KH
235 if (NILP (object))
236 goto end;
237 if (CONSP (object) && EQ (XCAR (object), Qkeymap))
238 return object;
239 else
240 {
241 tem = indirect_function (object);
03699b14 242 if (CONSP (tem) && EQ (XCAR (tem), Qkeymap))
b1314e15
KH
243 return tem;
244 }
f5b79c1c 245
8e4dfd54
JB
246 /* Should we do an autoload? Autoload forms for keymaps have
247 Qkeymap as their fifth element. */
d09b2024 248 if (autoload
47684cd9 249 && SYMBOLP (object)
d09b2024 250 && CONSP (tem)
03699b14 251 && EQ (XCAR (tem), Qautoload))
d09b2024 252 {
8e4dfd54 253 Lisp_Object tail;
d09b2024 254
8e4dfd54
JB
255 tail = Fnth (make_number (4), tem);
256 if (EQ (tail, Qkeymap))
257 {
258 struct gcpro gcpro1, gcpro2;
d09b2024 259
81fa9e2f
RS
260 GCPRO2 (tem, object);
261 do_autoload (tem, object);
8e4dfd54
JB
262 UNGCPRO;
263
264 goto autoload_retry;
265 }
d09b2024
JB
266 }
267
b1314e15 268 end:
2c6f1a39
JB
269 if (error)
270 wrong_type_argument (Qkeymapp, object);
6bbd7a29 271 return Qnil;
2c6f1a39
JB
272}
273
d09b2024
JB
274
275/* Follow any symbol chaining, and return the keymap denoted by OBJECT.
276 If OBJECT doesn't denote a keymap at all, signal an error. */
2c6f1a39
JB
277Lisp_Object
278get_keymap (object)
279 Lisp_Object object;
280{
224a16e8 281 return get_keymap_1 (object, 1, 0);
2c6f1a39 282}
7d58ed99
RS
283\f
284/* Return the parent map of the keymap MAP, or nil if it has none.
285 We assume that MAP is a valid keymap. */
286
287DEFUN ("keymap-parent", Fkeymap_parent, Skeymap_parent, 1, 1, 0,
288 "Return the parent keymap of KEYMAP.")
289 (keymap)
290 Lisp_Object keymap;
291{
292 Lisp_Object list;
293
294 keymap = get_keymap_1 (keymap, 1, 1);
295
296 /* Skip past the initial element `keymap'. */
03699b14
KR
297 list = XCDR (keymap);
298 for (; CONSP (list); list = XCDR (list))
7d58ed99
RS
299 {
300 /* See if there is another `keymap'. */
57495396 301 if (KEYMAPP (list))
7d58ed99
RS
302 return list;
303 }
304
305 return Qnil;
306}
307
3015eec0 308
b1904cd9
SM
309/* Check whether MAP is one of MAPS parents. */
310int
311keymap_memberp (map, maps)
312 Lisp_Object map, maps;
313{
314 while (KEYMAPP (maps) && !EQ (map, maps))
315 maps = Fkeymap_parent (maps);
316 return (EQ (map, maps));
317}
318
7d58ed99
RS
319/* Set the parent keymap of MAP to PARENT. */
320
321DEFUN ("set-keymap-parent", Fset_keymap_parent, Sset_keymap_parent, 2, 2, 0,
322 "Modify KEYMAP to set its parent map to PARENT.\n\
323PARENT should be nil or another keymap.")
324 (keymap, parent)
325 Lisp_Object keymap, parent;
326{
327 Lisp_Object list, prev;
df75b1a3 328 struct gcpro gcpro1;
7d58ed99 329 int i;
2c6f1a39 330
7d58ed99 331 keymap = get_keymap_1 (keymap, 1, 1);
df75b1a3
GM
332 GCPRO1 (keymap);
333
7d58ed99 334 if (!NILP (parent))
3015eec0 335 {
3015eec0
GM
336 parent = get_keymap_1 (parent, 1, 1);
337
338 /* Check for cycles. */
b1904cd9 339 if (keymap_memberp (keymap, parent))
3015eec0
GM
340 error ("Cyclic keymap inheritance");
341 }
2c6f1a39 342
7d58ed99
RS
343 /* Skip past the initial element `keymap'. */
344 prev = keymap;
345 while (1)
346 {
03699b14 347 list = XCDR (prev);
7d58ed99
RS
348 /* If there is a parent keymap here, replace it.
349 If we came to the end, add the parent in PREV. */
57495396 350 if (! CONSP (list) || KEYMAPP (list))
7d58ed99 351 {
2a5af1cf
RS
352 /* If we already have the right parent, return now
353 so that we avoid the loops below. */
03699b14 354 if (EQ (XCDR (prev), parent))
df75b1a3 355 RETURN_UNGCPRO (parent);
2a5af1cf 356
03699b14 357 XCDR (prev) = parent;
7d58ed99
RS
358 break;
359 }
360 prev = list;
361 }
362
363 /* Scan through for submaps, and set their parents too. */
364
03699b14 365 for (list = XCDR (keymap); CONSP (list); list = XCDR (list))
7d58ed99
RS
366 {
367 /* Stop the scan when we come to the parent. */
03699b14 368 if (EQ (XCAR (list), Qkeymap))
7d58ed99
RS
369 break;
370
371 /* If this element holds a prefix map, deal with it. */
03699b14
KR
372 if (CONSP (XCAR (list))
373 && CONSP (XCDR (XCAR (list))))
374 fix_submap_inheritance (keymap, XCAR (XCAR (list)),
375 XCDR (XCAR (list)));
376
377 if (VECTORP (XCAR (list)))
378 for (i = 0; i < XVECTOR (XCAR (list))->size; i++)
379 if (CONSP (XVECTOR (XCAR (list))->contents[i]))
7d58ed99 380 fix_submap_inheritance (keymap, make_number (i),
03699b14 381 XVECTOR (XCAR (list))->contents[i]);
0403641f 382
03699b14 383 if (CHAR_TABLE_P (XCAR (list)))
0403641f 384 {
23cf1efa 385 Lisp_Object indices[3];
0403641f 386
03699b14 387 map_char_table (fix_submap_inheritance, Qnil, XCAR (list),
0403641f
RS
388 keymap, 0, indices);
389 }
7d58ed99
RS
390 }
391
df75b1a3 392 RETURN_UNGCPRO (parent);
7d58ed99
RS
393}
394
395/* EVENT is defined in MAP as a prefix, and SUBMAP is its definition.
396 if EVENT is also a prefix in MAP's parent,
397 make sure that SUBMAP inherits that definition as its own parent. */
398
57495396 399static void
7d58ed99
RS
400fix_submap_inheritance (map, event, submap)
401 Lisp_Object map, event, submap;
402{
403 Lisp_Object map_parent, parent_entry;
404
405 /* SUBMAP is a cons that we found as a key binding.
406 Discard the other things found in a menu key binding. */
407
b1904cd9 408 submap = get_keymap_1 (get_keyelt (submap, 0), 0, 0);
7d58ed99
RS
409
410 /* If it isn't a keymap now, there's no work to do. */
b1904cd9 411 if (NILP (submap))
7d58ed99
RS
412 return;
413
414 map_parent = Fkeymap_parent (map);
415 if (! NILP (map_parent))
b1904cd9 416 parent_entry = get_keyelt (access_keymap (map_parent, event, 0, 0), 0);
7d58ed99
RS
417 else
418 parent_entry = Qnil;
419
3393c3f5
RS
420 /* If MAP's parent has something other than a keymap,
421 our own submap shadows it completely, so use nil as SUBMAP's parent. */
b1904cd9 422 if (! KEYMAPP (parent_entry))
3393c3f5
RS
423 parent_entry = Qnil;
424
7d58ed99 425 if (! EQ (parent_entry, submap))
61684f41
RS
426 {
427 Lisp_Object submap_parent;
428 submap_parent = submap;
429 while (1)
430 {
431 Lisp_Object tem;
432 tem = Fkeymap_parent (submap_parent);
b1904cd9
SM
433 if (keymap_memberp (tem, parent_entry))
434 /* Fset_keymap_parent could create a cycle. */
61684f41 435 return;
b1904cd9 436 if (KEYMAPP (tem))
61684f41
RS
437 submap_parent = tem;
438 else
439 break;
440 }
441 Fset_keymap_parent (submap_parent, parent_entry);
442 }
7d58ed99
RS
443}
444\f
2c6f1a39 445/* Look up IDX in MAP. IDX may be any sort of event.
f5b79c1c 446 Note that this does only one level of lookup; IDX must be a single
e25c4e44
JB
447 event, not a sequence.
448
449 If T_OK is non-zero, bindings for Qt are treated as default
450 bindings; any key left unmentioned by other tables and bindings is
451 given the binding of Qt.
452
c07aec97
RS
453 If T_OK is zero, bindings for Qt are not treated specially.
454
455 If NOINHERIT, don't accept a subkeymap found in an inherited keymap. */
2c6f1a39
JB
456
457Lisp_Object
c07aec97 458access_keymap (map, idx, t_ok, noinherit)
2c6f1a39
JB
459 Lisp_Object map;
460 Lisp_Object idx;
e25c4e44 461 int t_ok;
c07aec97 462 int noinherit;
2c6f1a39 463{
c07aec97
RS
464 int noprefix = 0;
465 Lisp_Object val;
466
2c6f1a39
JB
467 /* If idx is a list (some sort of mouse click, perhaps?),
468 the index we want to use is the car of the list, which
469 ought to be a symbol. */
cebd887d 470 idx = EVENT_HEAD (idx);
2c6f1a39 471
f5b79c1c
JB
472 /* If idx is a symbol, it might have modifiers, which need to
473 be put in the canonical order. */
47684cd9 474 if (SYMBOLP (idx))
f5b79c1c 475 idx = reorder_modifiers (idx);
2732bdbb
RS
476 else if (INTEGERP (idx))
477 /* Clobber the high bits that can be present on a machine
478 with more than 24 bits of integer. */
6e344130 479 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
2c6f1a39 480
f5b79c1c
JB
481 {
482 Lisp_Object tail;
e9b6dfb0 483 Lisp_Object t_binding;
2c6f1a39 484
e9b6dfb0 485 t_binding = Qnil;
03699b14 486 for (tail = map; CONSP (tail); tail = XCDR (tail))
2c6f1a39 487 {
e9b6dfb0 488 Lisp_Object binding;
f5b79c1c 489
03699b14 490 binding = XCAR (tail);
783a2838 491 if (SYMBOLP (binding))
f5b79c1c 492 {
c07aec97
RS
493 /* If NOINHERIT, stop finding prefix definitions
494 after we pass a second occurrence of the `keymap' symbol. */
495 if (noinherit && EQ (binding, Qkeymap) && ! EQ (tail, map))
496 noprefix = 1;
783a2838
KH
497 }
498 else if (CONSP (binding))
499 {
03699b14 500 if (EQ (XCAR (binding), idx))
c07aec97 501 {
03699b14 502 val = XCDR (binding);
b1904cd9 503 if (noprefix && KEYMAPP (val))
c07aec97 504 return Qnil;
7d58ed99
RS
505 if (CONSP (val))
506 fix_submap_inheritance (map, idx, val);
c07aec97
RS
507 return val;
508 }
03699b14
KR
509 if (t_ok && EQ (XCAR (binding), Qt))
510 t_binding = XCDR (binding);
783a2838
KH
511 }
512 else if (VECTORP (binding))
513 {
be3bfff1 514 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (binding)->size)
c07aec97 515 {
783a2838 516 val = XVECTOR (binding)->contents[XFASTINT (idx)];
b1904cd9 517 if (noprefix && KEYMAPP (val))
c07aec97 518 return Qnil;
7d58ed99
RS
519 if (CONSP (val))
520 fix_submap_inheritance (map, idx, val);
c07aec97
RS
521 return val;
522 }
f5b79c1c 523 }
0403641f
RS
524 else if (CHAR_TABLE_P (binding))
525 {
6418ea16
RS
526 /* Character codes with modifiers
527 are not included in a char-table.
528 All character codes without modifiers are included. */
529 if (NATNUMP (idx)
530 && ! (XFASTINT (idx)
531 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
532 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
0403641f
RS
533 {
534 val = Faref (binding, idx);
b1904cd9 535 if (noprefix && KEYMAPP (val))
0403641f
RS
536 return Qnil;
537 if (CONSP (val))
538 fix_submap_inheritance (map, idx, val);
539 return val;
540 }
541 }
20218e2f
JB
542
543 QUIT;
2c6f1a39 544 }
fde3a52f 545
e25c4e44
JB
546 return t_binding;
547 }
2c6f1a39
JB
548}
549
550/* Given OBJECT which was found in a slot in a keymap,
551 trace indirect definitions to get the actual definition of that slot.
552 An indirect definition is a list of the form
553 (KEYMAP . INDEX), where KEYMAP is a keymap or a symbol defined as one
554 and INDEX is the object to look up in KEYMAP to yield the definition.
555
556 Also if OBJECT has a menu string as the first element,
224a16e8
RS
557 remove that. Also remove a menu help string as second element.
558
559 If AUTOLOAD is nonzero, load autoloadable keymaps
560 that are referred to with indirection. */
2c6f1a39
JB
561
562Lisp_Object
224a16e8 563get_keyelt (object, autoload)
2c6f1a39 564 register Lisp_Object object;
224a16e8 565 int autoload;
2c6f1a39
JB
566{
567 while (1)
568 {
b1314e15
KH
569 if (!(CONSP (object)))
570 /* This is really the value. */
571 return object;
2c6f1a39 572
b1314e15
KH
573 /* If the keymap contents looks like (keymap ...) or (lambda ...)
574 then use itself. */
575 else if (EQ (XCAR (object), Qkeymap) || EQ (XCAR (object), Qlambda))
576 return object;
577
578 /* If the keymap contents looks like (menu-item name . DEFN)
579 or (menu-item name DEFN ...) then use DEFN.
3fc720e4 580 This is a new format menu item. */
b1314e15 581 else if (EQ (XCAR (object), Qmenu_item))
0403641f 582 {
b1314e15 583 if (CONSP (XCDR (object)))
0403641f 584 {
3fc720e4
GM
585 Lisp_Object tem;
586
b1314e15 587 object = XCDR (XCDR (object));
3fc720e4 588 tem = object;
b1314e15
KH
589 if (CONSP (object))
590 object = XCAR (object);
3fc720e4
GM
591
592 /* If there's a `:filter FILTER', apply FILTER to the
593 menu-item's definition to get the real definition to
53654733
GM
594 use. Temporarily inhibit GC while evaluating FILTER,
595 because not functions calling get_keyelt are prepared
596 for a GC. */
3fc720e4
GM
597 for (; CONSP (tem) && CONSP (XCDR (tem)); tem = XCDR (tem))
598 if (EQ (XCAR (tem), QCfilter))
599 {
53654733 600 int count = inhibit_garbage_collection ();
3fc720e4
GM
601 Lisp_Object filter;
602 filter = XCAR (XCDR (tem));
603 filter = list2 (filter, list2 (Qquote, object));
604 object = menu_item_eval_property (filter);
53654733 605 unbind_to (count, Qnil);
3fc720e4
GM
606 break;
607 }
0403641f
RS
608 }
609 else
b1314e15
KH
610 /* Invalid keymap */
611 return object;
0403641f
RS
612 }
613
b1314e15 614 /* If the keymap contents looks like (STRING . DEFN), use DEFN.
2c6f1a39
JB
615 Keymap alist elements like (CHAR MENUSTRING . DEFN)
616 will be used by HierarKey menus. */
b1314e15 617 else if (STRINGP (XCAR (object)))
1a8c3f10 618 {
b1314e15 619 object = XCDR (object);
1a8c3f10
RS
620 /* Also remove a menu help string, if any,
621 following the menu item name. */
b1314e15
KH
622 if (CONSP (object) && STRINGP (XCAR (object)))
623 object = XCDR (object);
c6ec9f6e 624 /* Also remove the sublist that caches key equivalences, if any. */
b1314e15 625 if (CONSP (object) && CONSP (XCAR (object)))
ffab2bd6 626 {
c6ec9f6e 627 Lisp_Object carcar;
b1314e15 628 carcar = XCAR (XCAR (object));
c6ec9f6e 629 if (NILP (carcar) || VECTORP (carcar))
b1314e15 630 object = XCDR (object);
ffab2bd6 631 }
1a8c3f10 632 }
2c6f1a39 633
b1314e15
KH
634 /* If the contents are (KEYMAP . ELEMENT), go indirect. */
635 else
a3fc8840 636 {
df75b1a3
GM
637 Lisp_Object map;
638
b1314e15
KH
639 map = get_keymap_1 (Fcar_safe (object), 0, autoload);
640 if (NILP (map))
641 /* Invalid keymap */
642 return object;
643 else
644 {
645 Lisp_Object key;
646 key = Fcdr (object);
140c4ac6 647 if (INTEGERP (key) && (XUINT (key) & meta_modifier))
b1314e15
KH
648 {
649 object = access_keymap (map, meta_prefix_char, 0, 0);
650 map = get_keymap_1 (object, 0, autoload);
651 object = access_keymap (map, make_number (XINT (key)
652 & ~meta_modifier),
653 0, 0);
654 }
655 else
656 object = access_keymap (map, key, 0, 0);
657 }
a3fc8840 658 }
2c6f1a39
JB
659 }
660}
661
2d929694 662static Lisp_Object
2c6f1a39
JB
663store_in_keymap (keymap, idx, def)
664 Lisp_Object keymap;
665 register Lisp_Object idx;
666 register Lisp_Object def;
667{
dce4372a 668 /* If we are preparing to dump, and DEF is a menu element
a3fc8840
RS
669 with a menu item indicator, copy it to ensure it is not pure. */
670 if (CONSP (def) && PURE_P (def)
03699b14
KR
671 && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
672 def = Fcons (XCAR (def), XCDR (def));
32ce36ad 673
03699b14 674 if (!CONSP (keymap) || ! EQ (XCAR (keymap), Qkeymap))
f5b79c1c
JB
675 error ("attempt to define a key in a non-keymap");
676
2c6f1a39
JB
677 /* If idx is a list (some sort of mouse click, perhaps?),
678 the index we want to use is the car of the list, which
679 ought to be a symbol. */
cebd887d 680 idx = EVENT_HEAD (idx);
2c6f1a39 681
f5b79c1c
JB
682 /* If idx is a symbol, it might have modifiers, which need to
683 be put in the canonical order. */
416349ec 684 if (SYMBOLP (idx))
f5b79c1c 685 idx = reorder_modifiers (idx);
2732bdbb
RS
686 else if (INTEGERP (idx))
687 /* Clobber the high bits that can be present on a machine
688 with more than 24 bits of integer. */
6e344130 689 XSETFASTINT (idx, XINT (idx) & (CHAR_META | (CHAR_META - 1)));
f5b79c1c
JB
690
691 /* Scan the keymap for a binding of idx. */
2c6f1a39 692 {
f5b79c1c 693 Lisp_Object tail;
2c6f1a39 694
f5b79c1c
JB
695 /* The cons after which we should insert new bindings. If the
696 keymap has a table element, we record its position here, so new
697 bindings will go after it; this way, the table will stay
698 towards the front of the alist and character lookups in dense
699 keymaps will remain fast. Otherwise, this just points at the
700 front of the keymap. */
e9b6dfb0 701 Lisp_Object insertion_point;
2c6f1a39 702
e9b6dfb0 703 insertion_point = keymap;
03699b14 704 for (tail = XCDR (keymap); CONSP (tail); tail = XCDR (tail))
2c6f1a39 705 {
e9b6dfb0 706 Lisp_Object elt;
f5b79c1c 707
03699b14 708 elt = XCAR (tail);
783a2838 709 if (VECTORP (elt))
f5b79c1c 710 {
be3bfff1 711 if (NATNUMP (idx) && XFASTINT (idx) < XVECTOR (elt)->size)
f5b79c1c
JB
712 {
713 XVECTOR (elt)->contents[XFASTINT (idx)] = def;
714 return def;
715 }
716 insertion_point = tail;
783a2838 717 }
0403641f
RS
718 else if (CHAR_TABLE_P (elt))
719 {
6418ea16
RS
720 /* Character codes with modifiers
721 are not included in a char-table.
722 All character codes without modifiers are included. */
723 if (NATNUMP (idx)
724 && ! (XFASTINT (idx)
725 & (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
726 | CHAR_SHIFT | CHAR_CTL | CHAR_META)))
0403641f
RS
727 {
728 Faset (elt, idx, def);
729 return def;
730 }
731 insertion_point = tail;
732 }
783a2838
KH
733 else if (CONSP (elt))
734 {
03699b14 735 if (EQ (idx, XCAR (elt)))
f5b79c1c 736 {
03699b14 737 XCDR (elt) = def;
f5b79c1c
JB
738 return def;
739 }
783a2838
KH
740 }
741 else if (SYMBOLP (elt))
742 {
f5b79c1c
JB
743 /* If we find a 'keymap' symbol in the spine of KEYMAP,
744 then we must have found the start of a second keymap
745 being used as the tail of KEYMAP, and a binding for IDX
746 should be inserted before it. */
747 if (EQ (elt, Qkeymap))
748 goto keymap_end;
f5b79c1c 749 }
0188441d
JB
750
751 QUIT;
2c6f1a39 752 }
2c6f1a39 753
f5b79c1c
JB
754 keymap_end:
755 /* We have scanned the entire keymap, and not found a binding for
756 IDX. Let's add one. */
03699b14
KR
757 XCDR (insertion_point)
758 = Fcons (Fcons (idx, def), XCDR (insertion_point));
f5b79c1c 759 }
b1904cd9 760
2c6f1a39
JB
761 return def;
762}
763
69248761 764void
0403641f
RS
765copy_keymap_1 (chartable, idx, elt)
766 Lisp_Object chartable, idx, elt;
767{
bee3fc83
RS
768 if (!SYMBOLP (elt) && ! NILP (Fkeymapp (elt)))
769 Faset (chartable, idx, Fcopy_keymap (elt));
0403641f 770}
f5b79c1c 771
2c6f1a39
JB
772DEFUN ("copy-keymap", Fcopy_keymap, Scopy_keymap, 1, 1, 0,
773 "Return a copy of the keymap KEYMAP.\n\
774The copy starts out with the same definitions of KEYMAP,\n\
775but changing either the copy or KEYMAP does not affect the other.\n\
1d8d96fa
JB
776Any key definitions that are subkeymaps are recursively copied.\n\
777However, a key definition which is a symbol whose definition is a keymap\n\
778is not copied.")
2c6f1a39
JB
779 (keymap)
780 Lisp_Object keymap;
781{
782 register Lisp_Object copy, tail;
783
784 copy = Fcopy_alist (get_keymap (keymap));
2c6f1a39 785
03699b14 786 for (tail = copy; CONSP (tail); tail = XCDR (tail))
2c6f1a39 787 {
e9b6dfb0 788 Lisp_Object elt;
2c6f1a39 789
03699b14 790 elt = XCAR (tail);
0403641f
RS
791 if (CHAR_TABLE_P (elt))
792 {
23cf1efa 793 Lisp_Object indices[3];
0403641f
RS
794
795 elt = Fcopy_sequence (elt);
03699b14 796 XCAR (tail) = elt;
7cc06296 797
0403641f
RS
798 map_char_table (copy_keymap_1, Qnil, elt, elt, 0, indices);
799 }
800 else if (VECTORP (elt))
2c6f1a39 801 {
f5b79c1c 802 int i;
2c6f1a39 803
f5b79c1c 804 elt = Fcopy_sequence (elt);
03699b14 805 XCAR (tail) = elt;
2c6f1a39 806
926a64aa 807 for (i = 0; i < XVECTOR (elt)->size; i++)
416349ec 808 if (!SYMBOLP (XVECTOR (elt)->contents[i])
98006242 809 && ! NILP (Fkeymapp (XVECTOR (elt)->contents[i])))
0403641f
RS
810 XVECTOR (elt)->contents[i]
811 = Fcopy_keymap (XVECTOR (elt)->contents[i]);
2c6f1a39 812 }
03699b14 813 else if (CONSP (elt) && CONSP (XCDR (elt)))
d65a13c5 814 {
a3fc8840 815 Lisp_Object tem;
03699b14 816 tem = XCDR (elt);
d65a13c5 817
a3fc8840 818 /* Is this a new format menu item. */
03699b14 819 if (EQ (XCAR (tem),Qmenu_item))
a3fc8840
RS
820 {
821 /* Copy cell with menu-item marker. */
03699b14
KR
822 XCDR (elt)
823 = Fcons (XCAR (tem), XCDR (tem));
824 elt = XCDR (elt);
825 tem = XCDR (elt);
a3fc8840
RS
826 if (CONSP (tem))
827 {
828 /* Copy cell with menu-item name. */
03699b14
KR
829 XCDR (elt)
830 = Fcons (XCAR (tem), XCDR (tem));
831 elt = XCDR (elt);
832 tem = XCDR (elt);
a3fc8840
RS
833 };
834 if (CONSP (tem))
835 {
836 /* Copy cell with binding and if the binding is a keymap,
837 copy that. */
03699b14
KR
838 XCDR (elt)
839 = Fcons (XCAR (tem), XCDR (tem));
840 elt = XCDR (elt);
841 tem = XCAR (elt);
a3fc8840 842 if (!(SYMBOLP (tem) || NILP (Fkeymapp (tem))))
03699b14
KR
843 XCAR (elt) = Fcopy_keymap (tem);
844 tem = XCDR (elt);
845 if (CONSP (tem) && CONSP (XCAR (tem)))
a3fc8840 846 /* Delete cache for key equivalences. */
03699b14 847 XCDR (elt) = XCDR (tem);
a3fc8840
RS
848 }
849 }
850 else
851 {
852 /* It may be an old fomat menu item.
853 Skip the optional menu string.
854 */
03699b14 855 if (STRINGP (XCAR (tem)))
d65a13c5 856 {
a3fc8840 857 /* Copy the cell, since copy-alist didn't go this deep. */
03699b14
KR
858 XCDR (elt)
859 = Fcons (XCAR (tem), XCDR (tem));
860 elt = XCDR (elt);
861 tem = XCDR (elt);
a3fc8840 862 /* Also skip the optional menu help string. */
03699b14 863 if (CONSP (tem) && STRINGP (XCAR (tem)))
a3fc8840 864 {
03699b14
KR
865 XCDR (elt)
866 = Fcons (XCAR (tem), XCDR (tem));
867 elt = XCDR (elt);
868 tem = XCDR (elt);
a3fc8840
RS
869 }
870 /* There may also be a list that caches key equivalences.
871 Just delete it for the new keymap. */
872 if (CONSP (tem)
03699b14
KR
873 && CONSP (XCAR (tem))
874 && (NILP (XCAR (XCAR (tem)))
875 || VECTORP (XCAR (XCAR (tem)))))
876 XCDR (elt) = XCDR (tem);
d65a13c5 877 }
a3fc8840 878 if (CONSP (elt)
03699b14
KR
879 && ! SYMBOLP (XCDR (elt))
880 && ! NILP (Fkeymapp (XCDR (elt))))
881 XCDR (elt) = Fcopy_keymap (XCDR (elt));
d65a13c5 882 }
a3fc8840 883
d65a13c5 884 }
2c6f1a39 885 }
a3fc8840 886
2c6f1a39
JB
887 return copy;
888}
889\f
cc0a8174
JB
890/* Simple Keymap mutators and accessors. */
891
21a0d7a0
RS
892/* GC is possible in this function if it autoloads a keymap. */
893
2c6f1a39
JB
894DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
895 "Args KEYMAP, KEY, DEF. Define key sequence KEY, in KEYMAP, as DEF.\n\
896KEYMAP is a keymap. KEY is a string or a vector of symbols and characters\n\
897meaning a sequence of keystrokes and events.\n\
c818754b
RS
898Non-ASCII characters with codes above 127 (such as ISO Latin-1)\n\
899can be included if you use a vector.\n\
2c6f1a39
JB
900DEF is anything that can be a key's definition:\n\
901 nil (means key is undefined in this keymap),\n\
902 a command (a Lisp function suitable for interactive calling)\n\
903 a string (treated as a keyboard macro),\n\
904 a keymap (to define a prefix key),\n\
905 a symbol. When the key is looked up, the symbol will stand for its\n\
906 function definition, which should at that time be one of the above,\n\
907 or another symbol whose function definition is used, etc.\n\
908 a cons (STRING . DEFN), meaning that DEFN is the definition\n\
909 (DEFN should be a valid definition in its own right),\n\
6e8290aa
JB
910 or a cons (KEYMAP . CHAR), meaning use definition of CHAR in map KEYMAP.\n\
911\n\
912If KEYMAP is a sparse keymap, the pair binding KEY to DEF is added at\n\
913the front of KEYMAP.")
2c6f1a39 914 (keymap, key, def)
d09b2024 915 Lisp_Object keymap;
2c6f1a39
JB
916 Lisp_Object key;
917 Lisp_Object def;
918{
919 register int idx;
920 register Lisp_Object c;
2c6f1a39
JB
921 register Lisp_Object cmd;
922 int metized = 0;
6ba6e250 923 int meta_bit;
2c6f1a39 924 int length;
d09b2024 925 struct gcpro gcpro1, gcpro2, gcpro3;
2c6f1a39 926
224a16e8 927 keymap = get_keymap_1 (keymap, 1, 1);
2c6f1a39 928
416349ec 929 if (!VECTORP (key) && !STRINGP (key))
2c6f1a39
JB
930 key = wrong_type_argument (Qarrayp, key);
931
d09b2024 932 length = XFASTINT (Flength (key));
2c6f1a39
JB
933 if (length == 0)
934 return Qnil;
935
107fd03d
RS
936 if (SYMBOLP (def) && !EQ (Vdefine_key_rebound_commands, Qt))
937 Vdefine_key_rebound_commands = Fcons (def, Vdefine_key_rebound_commands);
938
d09b2024
JB
939 GCPRO3 (keymap, key, def);
940
416349ec 941 if (VECTORP (key))
6ba6e250
RS
942 meta_bit = meta_modifier;
943 else
944 meta_bit = 0x80;
945
2c6f1a39
JB
946 idx = 0;
947 while (1)
948 {
949 c = Faref (key, make_number (idx));
950
f09bc924 951 if (CONSP (c) && lucid_event_type_list_p (c))
41015a19 952 c = Fevent_convert_list (c);
f09bc924 953
416349ec 954 if (INTEGERP (c)
6ba6e250 955 && (XINT (c) & meta_bit)
2c6f1a39
JB
956 && !metized)
957 {
958 c = meta_prefix_char;
959 metized = 1;
960 }
961 else
962 {
416349ec 963 if (INTEGERP (c))
0b8fc2d4 964 XSETINT (c, XINT (c) & ~meta_bit);
2c6f1a39
JB
965
966 metized = 0;
967 idx++;
968 }
969
5907b863 970 if (! INTEGERP (c) && ! SYMBOLP (c) && ! CONSP (c))
4b04c52e 971 error ("Key sequence contains invalid events");
5907b863 972
2c6f1a39 973 if (idx == length)
d09b2024 974 RETURN_UNGCPRO (store_in_keymap (keymap, c, def));
2c6f1a39 975
224a16e8 976 cmd = get_keyelt (access_keymap (keymap, c, 0, 1), 1);
2c6f1a39 977
c07aec97 978 /* If this key is undefined, make it a prefix. */
265a9e55 979 if (NILP (cmd))
c07aec97 980 cmd = define_as_prefix (keymap, c);
2c6f1a39 981
d09b2024
JB
982 keymap = get_keymap_1 (cmd, 0, 1);
983 if (NILP (keymap))
e9b6dfb0
KH
984 /* We must use Fkey_description rather than just passing key to
985 error; key might be a vector, not a string. */
986 error ("Key sequence %s uses invalid prefix characters",
987 XSTRING (Fkey_description (key))->data);
2c6f1a39
JB
988 }
989}
990
991/* Value is number if KEY is too long; NIL if valid but has no definition. */
21a0d7a0 992/* GC is possible in this function if it autoloads a keymap. */
2c6f1a39 993
7c140252 994DEFUN ("lookup-key", Flookup_key, Slookup_key, 2, 3, 0,
2c6f1a39
JB
995 "In keymap KEYMAP, look up key sequence KEY. Return the definition.\n\
996nil means undefined. See doc of `define-key' for kinds of definitions.\n\
7c140252 997\n\
2c6f1a39
JB
998A number as value means KEY is \"too long\";\n\
999that is, characters or symbols in it except for the last one\n\
1000fail to be a valid sequence of prefix characters in KEYMAP.\n\
1001The number is how many characters at the front of KEY\n\
7c140252
JB
1002it takes to reach a non-prefix command.\n\
1003\n\
1004Normally, `lookup-key' ignores bindings for t, which act as default\n\
1005bindings, used when nothing else in the keymap applies; this makes it\n\
b31a4218 1006usable as a general function for probing keymaps. However, if the\n\
7c140252
JB
1007third optional argument ACCEPT-DEFAULT is non-nil, `lookup-key' will\n\
1008recognize the default bindings, just as `read-key-sequence' does.")
1009 (keymap, key, accept_default)
2c6f1a39
JB
1010 register Lisp_Object keymap;
1011 Lisp_Object key;
7c140252 1012 Lisp_Object accept_default;
2c6f1a39
JB
1013{
1014 register int idx;
2c6f1a39
JB
1015 register Lisp_Object cmd;
1016 register Lisp_Object c;
1017 int metized = 0;
1018 int length;
7c140252 1019 int t_ok = ! NILP (accept_default);
6ba6e250 1020 int meta_bit;
21a0d7a0 1021 struct gcpro gcpro1;
2c6f1a39 1022
224a16e8 1023 keymap = get_keymap_1 (keymap, 1, 1);
2c6f1a39 1024
416349ec 1025 if (!VECTORP (key) && !STRINGP (key))
2c6f1a39
JB
1026 key = wrong_type_argument (Qarrayp, key);
1027
d09b2024 1028 length = XFASTINT (Flength (key));
2c6f1a39
JB
1029 if (length == 0)
1030 return keymap;
1031
416349ec 1032 if (VECTORP (key))
6ba6e250
RS
1033 meta_bit = meta_modifier;
1034 else
1035 meta_bit = 0x80;
1036
21a0d7a0
RS
1037 GCPRO1 (key);
1038
2c6f1a39
JB
1039 idx = 0;
1040 while (1)
1041 {
1042 c = Faref (key, make_number (idx));
1043
f09bc924 1044 if (CONSP (c) && lucid_event_type_list_p (c))
41015a19 1045 c = Fevent_convert_list (c);
f09bc924 1046
416349ec 1047 if (INTEGERP (c)
6ba6e250 1048 && (XINT (c) & meta_bit)
2c6f1a39
JB
1049 && !metized)
1050 {
1051 c = meta_prefix_char;
1052 metized = 1;
1053 }
1054 else
1055 {
416349ec 1056 if (INTEGERP (c))
6ba6e250 1057 XSETINT (c, XINT (c) & ~meta_bit);
2c6f1a39
JB
1058
1059 metized = 0;
1060 idx++;
1061 }
1062
224a16e8 1063 cmd = get_keyelt (access_keymap (keymap, c, t_ok, 0), 1);
2c6f1a39 1064 if (idx == length)
21a0d7a0 1065 RETURN_UNGCPRO (cmd);
2c6f1a39 1066
224a16e8 1067 keymap = get_keymap_1 (cmd, 0, 1);
d09b2024 1068 if (NILP (keymap))
21a0d7a0 1069 RETURN_UNGCPRO (make_number (idx));
2c6f1a39 1070
2c6f1a39
JB
1071 QUIT;
1072 }
1073}
1074
c07aec97
RS
1075/* Make KEYMAP define event C as a keymap (i.e., as a prefix).
1076 Assume that currently it does not define C at all.
1077 Return the keymap. */
1078
1079static Lisp_Object
1080define_as_prefix (keymap, c)
1081 Lisp_Object keymap, c;
1082{
1083 Lisp_Object inherit, cmd;
1084
1085 cmd = Fmake_sparse_keymap (Qnil);
1086 /* If this key is defined as a prefix in an inherited keymap,
1087 make it a prefix in this map, and make its definition
1088 inherit the other prefix definition. */
1089 inherit = access_keymap (keymap, c, 0, 0);
7d58ed99
RS
1090#if 0
1091 /* This code is needed to do the right thing in the following case:
1092 keymap A inherits from B,
1093 you define KEY as a prefix in A,
1094 then later you define KEY as a prefix in B.
1095 We want the old prefix definition in A to inherit from that in B.
1096 It is hard to do that retroactively, so this code
1097 creates the prefix in B right away.
1098
1099 But it turns out that this code causes problems immediately
1100 when the prefix in A is defined: it causes B to define KEY
1101 as a prefix with no subcommands.
1102
1103 So I took out this code. */
c07aec97
RS
1104 if (NILP (inherit))
1105 {
1106 /* If there's an inherited keymap
1107 and it doesn't define this key,
1108 make it define this key. */
1109 Lisp_Object tail;
1110
03699b14
KR
1111 for (tail = Fcdr (keymap); CONSP (tail); tail = XCDR (tail))
1112 if (EQ (XCAR (tail), Qkeymap))
c07aec97
RS
1113 break;
1114
1115 if (!NILP (tail))
1116 inherit = define_as_prefix (tail, c);
1117 }
7d58ed99 1118#endif
c07aec97
RS
1119
1120 cmd = nconc2 (cmd, inherit);
1121 store_in_keymap (keymap, c, cmd);
1122
1123 return cmd;
1124}
1125
0b8fc2d4
RS
1126/* Append a key to the end of a key sequence. We always make a vector. */
1127
2c6f1a39
JB
1128Lisp_Object
1129append_key (key_sequence, key)
1130 Lisp_Object key_sequence, key;
1131{
1132 Lisp_Object args[2];
1133
1134 args[0] = key_sequence;
1135
0b8fc2d4
RS
1136 args[1] = Fcons (key, Qnil);
1137 return Fvconcat (2, args);
2c6f1a39
JB
1138}
1139
1140\f
cc0a8174
JB
1141/* Global, local, and minor mode keymap stuff. */
1142
265a9e55 1143/* We can't put these variables inside current_minor_maps, since under
6bbbd9b0
JB
1144 some systems, static gets macro-defined to be the empty string.
1145 Ickypoo. */
265a9e55
JB
1146static Lisp_Object *cmm_modes, *cmm_maps;
1147static int cmm_size;
1148
fbb90829
KH
1149/* Error handler used in current_minor_maps. */
1150static Lisp_Object
1151current_minor_maps_error ()
1152{
1153 return Qnil;
1154}
1155
cc0a8174
JB
1156/* Store a pointer to an array of the keymaps of the currently active
1157 minor modes in *buf, and return the number of maps it contains.
1158
1159 This function always returns a pointer to the same buffer, and may
1160 free or reallocate it, so if you want to keep it for a long time or
1161 hand it out to lisp code, copy it. This procedure will be called
1162 for every key sequence read, so the nice lispy approach (return a
1163 new assoclist, list, what have you) for each invocation would
1164 result in a lot of consing over time.
1165
1166 If we used xrealloc/xmalloc and ran out of memory, they would throw
1167 back to the command loop, which would try to read a key sequence,
1168 which would call this function again, resulting in an infinite
1169 loop. Instead, we'll use realloc/malloc and silently truncate the
1170 list, let the key sequence be read, and hope some other piece of
1171 code signals the error. */
1172int
1173current_minor_maps (modeptr, mapptr)
1174 Lisp_Object **modeptr, **mapptr;
1175{
cc0a8174 1176 int i = 0;
dd9cda06 1177 int list_number = 0;
6bbbd9b0 1178 Lisp_Object alist, assoc, var, val;
dd9cda06
RS
1179 Lisp_Object lists[2];
1180
1181 lists[0] = Vminor_mode_overriding_map_alist;
1182 lists[1] = Vminor_mode_map_alist;
1183
1184 for (list_number = 0; list_number < 2; list_number++)
1185 for (alist = lists[list_number];
1186 CONSP (alist);
03699b14
KR
1187 alist = XCDR (alist))
1188 if ((assoc = XCAR (alist), CONSP (assoc))
1189 && (var = XCAR (assoc), SYMBOLP (var))
dd9cda06
RS
1190 && (val = find_symbol_value (var), ! EQ (val, Qunbound))
1191 && ! NILP (val))
1192 {
1193 Lisp_Object temp;
cc0a8174 1194
64dd3629
RS
1195 /* If a variable has an entry in Vminor_mode_overriding_map_alist,
1196 and also an entry in Vminor_mode_map_alist,
1197 ignore the latter. */
1198 if (list_number == 1)
1199 {
1200 val = assq_no_quit (var, lists[0]);
1201 if (!NILP (val))
1202 break;
1203 }
1204
dd9cda06
RS
1205 if (i >= cmm_size)
1206 {
1207 Lisp_Object *newmodes, *newmaps;
cc0a8174 1208
dd9cda06
RS
1209 if (cmm_maps)
1210 {
1211 BLOCK_INPUT;
1212 cmm_size *= 2;
1213 newmodes
1214 = (Lisp_Object *) realloc (cmm_modes,
1215 cmm_size * sizeof (Lisp_Object));
1216 newmaps
1217 = (Lisp_Object *) realloc (cmm_maps,
1218 cmm_size * sizeof (Lisp_Object));
1219 UNBLOCK_INPUT;
1220 }
1221 else
1222 {
1223 BLOCK_INPUT;
1224 cmm_size = 30;
1225 newmodes
9731fda5 1226 = (Lisp_Object *) xmalloc (cmm_size * sizeof (Lisp_Object));
dd9cda06 1227 newmaps
9731fda5 1228 = (Lisp_Object *) xmalloc (cmm_size * sizeof (Lisp_Object));
dd9cda06
RS
1229 UNBLOCK_INPUT;
1230 }
cc0a8174 1231
dd9cda06
RS
1232 if (newmaps && newmodes)
1233 {
1234 cmm_modes = newmodes;
1235 cmm_maps = newmaps;
1236 }
1237 else
1238 break;
1239 }
fbb90829 1240
dd9cda06
RS
1241 /* Get the keymap definition--or nil if it is not defined. */
1242 temp = internal_condition_case_1 (Findirect_function,
03699b14 1243 XCDR (assoc),
dd9cda06
RS
1244 Qerror, current_minor_maps_error);
1245 if (!NILP (temp))
1246 {
1247 cmm_modes[i] = var;
1248 cmm_maps [i] = temp;
1249 i++;
1250 }
1251 }
cc0a8174 1252
265a9e55
JB
1253 if (modeptr) *modeptr = cmm_modes;
1254 if (mapptr) *mapptr = cmm_maps;
cc0a8174
JB
1255 return i;
1256}
1257
21a0d7a0
RS
1258/* GC is possible in this function if it autoloads a keymap. */
1259
7c140252 1260DEFUN ("key-binding", Fkey_binding, Skey_binding, 1, 2, 0,
2c6f1a39 1261 "Return the binding for command KEY in current keymaps.\n\
7c140252
JB
1262KEY is a string or vector, a sequence of keystrokes.\n\
1263The binding is probably a symbol with a function definition.\n\
1264\n\
1265Normally, `key-binding' ignores bindings for t, which act as default\n\
1266bindings, used when nothing else in the keymap applies; this makes it\n\
d831234b
RS
1267usable as a general function for probing keymaps. However, if the\n\
1268optional second argument ACCEPT-DEFAULT is non-nil, `key-binding' does\n\
7c140252
JB
1269recognize the default bindings, just as `read-key-sequence' does.")
1270 (key, accept_default)
c2a2858a 1271 Lisp_Object key, accept_default;
2c6f1a39 1272{
cc0a8174
JB
1273 Lisp_Object *maps, value;
1274 int nmaps, i;
21a0d7a0
RS
1275 struct gcpro gcpro1;
1276
1277 GCPRO1 (key);
cc0a8174 1278
e784236d
KH
1279 if (!NILP (current_kboard->Voverriding_terminal_local_map))
1280 {
1281 value = Flookup_key (current_kboard->Voverriding_terminal_local_map,
1282 key, accept_default);
1283 if (! NILP (value) && !INTEGERP (value))
1284 RETURN_UNGCPRO (value);
1285 }
1286 else if (!NILP (Voverriding_local_map))
2c6f1a39 1287 {
7d92e329 1288 value = Flookup_key (Voverriding_local_map, key, accept_default);
416349ec 1289 if (! NILP (value) && !INTEGERP (value))
21a0d7a0 1290 RETURN_UNGCPRO (value);
2c6f1a39 1291 }
7d92e329
RS
1292 else
1293 {
d964248c
KH
1294 Lisp_Object local;
1295
7d92e329 1296 nmaps = current_minor_maps (0, &maps);
21a0d7a0
RS
1297 /* Note that all these maps are GCPRO'd
1298 in the places where we found them. */
1299
7d92e329
RS
1300 for (i = 0; i < nmaps; i++)
1301 if (! NILP (maps[i]))
1302 {
1303 value = Flookup_key (maps[i], key, accept_default);
416349ec 1304 if (! NILP (value) && !INTEGERP (value))
21a0d7a0 1305 RETURN_UNGCPRO (value);
7d92e329
RS
1306 }
1307
93d2aa1c
DL
1308 local = get_local_map (PT, current_buffer, keymap);
1309 if (! NILP (local))
1310 {
1311 value = Flookup_key (local, key, accept_default);
1312 if (! NILP (value) && !INTEGERP (value))
1313 RETURN_UNGCPRO (value);
1314 }
1315
1316 local = get_local_map (PT, current_buffer, local_map);
d964248c
KH
1317
1318 if (! NILP (local))
7d92e329 1319 {
d964248c 1320 value = Flookup_key (local, key, accept_default);
416349ec 1321 if (! NILP (value) && !INTEGERP (value))
21a0d7a0 1322 RETURN_UNGCPRO (value);
7d92e329
RS
1323 }
1324 }
cc0a8174 1325
7c140252 1326 value = Flookup_key (current_global_map, key, accept_default);
21a0d7a0 1327 UNGCPRO;
416349ec 1328 if (! NILP (value) && !INTEGERP (value))
cc0a8174
JB
1329 return value;
1330
1331 return Qnil;
2c6f1a39
JB
1332}
1333
21a0d7a0
RS
1334/* GC is possible in this function if it autoloads a keymap. */
1335
7c140252 1336DEFUN ("local-key-binding", Flocal_key_binding, Slocal_key_binding, 1, 2, 0,
2c6f1a39
JB
1337 "Return the binding for command KEYS in current local keymap only.\n\
1338KEYS is a string, a sequence of keystrokes.\n\
7c140252
JB
1339The binding is probably a symbol with a function definition.\n\
1340\n\
1341If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1342bindings; see the description of `lookup-key' for more details about this.")
1343 (keys, accept_default)
1344 Lisp_Object keys, accept_default;
2c6f1a39
JB
1345{
1346 register Lisp_Object map;
1347 map = current_buffer->keymap;
265a9e55 1348 if (NILP (map))
2c6f1a39 1349 return Qnil;
7c140252 1350 return Flookup_key (map, keys, accept_default);
2c6f1a39
JB
1351}
1352
21a0d7a0
RS
1353/* GC is possible in this function if it autoloads a keymap. */
1354
7c140252 1355DEFUN ("global-key-binding", Fglobal_key_binding, Sglobal_key_binding, 1, 2, 0,
2c6f1a39
JB
1356 "Return the binding for command KEYS in current global keymap only.\n\
1357KEYS is a string, a sequence of keystrokes.\n\
6bbbd9b0
JB
1358The binding is probably a symbol with a function definition.\n\
1359This function's return values are the same as those of lookup-key\n\
21a0d7a0 1360\(which see).\n\
7c140252
JB
1361\n\
1362If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1363bindings; see the description of `lookup-key' for more details about this.")
1364 (keys, accept_default)
1365 Lisp_Object keys, accept_default;
2c6f1a39 1366{
7c140252 1367 return Flookup_key (current_global_map, keys, accept_default);
2c6f1a39
JB
1368}
1369
21a0d7a0
RS
1370/* GC is possible in this function if it autoloads a keymap. */
1371
7c140252 1372DEFUN ("minor-mode-key-binding", Fminor_mode_key_binding, Sminor_mode_key_binding, 1, 2, 0,
cc0a8174
JB
1373 "Find the visible minor mode bindings of KEY.\n\
1374Return an alist of pairs (MODENAME . BINDING), where MODENAME is the\n\
1375the symbol which names the minor mode binding KEY, and BINDING is\n\
1376KEY's definition in that mode. In particular, if KEY has no\n\
1377minor-mode bindings, return nil. If the first binding is a\n\
1378non-prefix, all subsequent bindings will be omitted, since they would\n\
1379be ignored. Similarly, the list doesn't include non-prefix bindings\n\
7c140252
JB
1380that come after prefix bindings.\n\
1381\n\
1382If optional argument ACCEPT-DEFAULT is non-nil, recognize default\n\
1383bindings; see the description of `lookup-key' for more details about this.")
1384 (key, accept_default)
1385 Lisp_Object key, accept_default;
cc0a8174
JB
1386{
1387 Lisp_Object *modes, *maps;
1388 int nmaps;
1389 Lisp_Object binding;
1390 int i, j;
21a0d7a0 1391 struct gcpro gcpro1, gcpro2;
cc0a8174
JB
1392
1393 nmaps = current_minor_maps (&modes, &maps);
21a0d7a0
RS
1394 /* Note that all these maps are GCPRO'd
1395 in the places where we found them. */
1396
1397 binding = Qnil;
1398 GCPRO2 (key, binding);
cc0a8174
JB
1399
1400 for (i = j = 0; i < nmaps; i++)
265a9e55 1401 if (! NILP (maps[i])
7c140252 1402 && ! NILP (binding = Flookup_key (maps[i], key, accept_default))
416349ec 1403 && !INTEGERP (binding))
cc0a8174 1404 {
d09b2024 1405 if (! NILP (get_keymap (binding)))
cc0a8174
JB
1406 maps[j++] = Fcons (modes[i], binding);
1407 else if (j == 0)
21a0d7a0 1408 RETURN_UNGCPRO (Fcons (Fcons (modes[i], binding), Qnil));
cc0a8174
JB
1409 }
1410
21a0d7a0 1411 UNGCPRO;
cc0a8174
JB
1412 return Flist (j, maps);
1413}
1414
7f8f0e67 1415DEFUN ("define-prefix-command", Fdefine_prefix_command, Sdefine_prefix_command, 1, 3, 0,
cd8520b9 1416 "Define COMMAND as a prefix command. COMMAND should be a symbol.\n\
2c6f1a39 1417A new sparse keymap is stored as COMMAND's function definition and its value.\n\
1d8d96fa
JB
1418If a second optional argument MAPVAR is given, the map is stored as\n\
1419its value instead of as COMMAND's value; but COMMAND is still defined\n\
7f8f0e67
EZ
1420as a function.\n\
1421The third optional argument NAME, if given, supplies a menu name\n\
1422string for the map. This is required to use the keymap as a menu.")
1423 (command, mapvar, name)
1424 Lisp_Object command, mapvar, name;
2c6f1a39
JB
1425{
1426 Lisp_Object map;
7f8f0e67 1427 map = Fmake_sparse_keymap (name);
88539837 1428 Ffset (command, map);
265a9e55 1429 if (!NILP (mapvar))
2c6f1a39
JB
1430 Fset (mapvar, map);
1431 else
88539837
EN
1432 Fset (command, map);
1433 return command;
2c6f1a39
JB
1434}
1435
1436DEFUN ("use-global-map", Fuse_global_map, Suse_global_map, 1, 1, 0,
1437 "Select KEYMAP as the global keymap.")
1438 (keymap)
1439 Lisp_Object keymap;
1440{
1441 keymap = get_keymap (keymap);
1442 current_global_map = keymap;
6f27e7a2 1443
2c6f1a39
JB
1444 return Qnil;
1445}
1446
1447DEFUN ("use-local-map", Fuse_local_map, Suse_local_map, 1, 1, 0,
1448 "Select KEYMAP as the local keymap.\n\
1449If KEYMAP is nil, that means no local keymap.")
1450 (keymap)
1451 Lisp_Object keymap;
1452{
265a9e55 1453 if (!NILP (keymap))
2c6f1a39
JB
1454 keymap = get_keymap (keymap);
1455
1456 current_buffer->keymap = keymap;
1457
1458 return Qnil;
1459}
1460
1461DEFUN ("current-local-map", Fcurrent_local_map, Scurrent_local_map, 0, 0, 0,
1462 "Return current buffer's local keymap, or nil if it has none.")
1463 ()
1464{
1465 return current_buffer->keymap;
1466}
1467
1468DEFUN ("current-global-map", Fcurrent_global_map, Scurrent_global_map, 0, 0, 0,
1469 "Return the current global keymap.")
1470 ()
1471{
1472 return current_global_map;
1473}
cc0a8174
JB
1474
1475DEFUN ("current-minor-mode-maps", Fcurrent_minor_mode_maps, Scurrent_minor_mode_maps, 0, 0, 0,
1476 "Return a list of keymaps for the minor modes of the current buffer.")
1477 ()
1478{
1479 Lisp_Object *maps;
1480 int nmaps = current_minor_maps (0, &maps);
1481
1482 return Flist (nmaps, maps);
1483}
2c6f1a39 1484\f
cc0a8174
JB
1485/* Help functions for describing and documenting keymaps. */
1486
69248761 1487static void accessible_keymaps_char_table ();
0403641f 1488
21a0d7a0
RS
1489/* This function cannot GC. */
1490
2c6f1a39 1491DEFUN ("accessible-keymaps", Faccessible_keymaps, Saccessible_keymaps,
53c8f9fa 1492 1, 2, 0,
2c6f1a39
JB
1493 "Find all keymaps accessible via prefix characters from KEYMAP.\n\
1494Returns a list of elements of the form (KEYS . MAP), where the sequence\n\
1495KEYS starting from KEYMAP gets you to MAP. These elements are ordered\n\
c3f27064 1496so that the KEYS increase in length. The first element is ([] . KEYMAP).\n\
f66ef185
RS
1497An optional argument PREFIX, if non-nil, should be a key sequence;\n\
1498then the value includes only maps for prefixes that start with PREFIX.")
88539837
EN
1499 (keymap, prefix)
1500 Lisp_Object keymap, prefix;
2c6f1a39 1501{
53c8f9fa
RS
1502 Lisp_Object maps, good_maps, tail;
1503 int prefixlen = 0;
1504
21a0d7a0
RS
1505 /* no need for gcpro because we don't autoload any keymaps. */
1506
53c8f9fa
RS
1507 if (!NILP (prefix))
1508 prefixlen = XINT (Flength (prefix));
2c6f1a39 1509
44a4a59b
RS
1510 if (!NILP (prefix))
1511 {
1512 /* If a prefix was specified, start with the keymap (if any) for
1513 that prefix, so we don't waste time considering other prefixes. */
1514 Lisp_Object tem;
88539837 1515 tem = Flookup_key (keymap, prefix, Qt);
1ae2097f
RS
1516 /* Flookup_key may give us nil, or a number,
1517 if the prefix is not defined in this particular map.
1518 It might even give us a list that isn't a keymap. */
1519 tem = get_keymap_1 (tem, 0, 0);
44a4a59b 1520 if (!NILP (tem))
67fc16a3
RS
1521 {
1522 /* Convert PREFIX to a vector now, so that later on
1523 we don't have to deal with the possibility of a string. */
1524 if (STRINGP (prefix))
1525 {
f3ba5409 1526 int i, i_byte, c;
67fc16a3
RS
1527 Lisp_Object copy;
1528
1529 copy = Fmake_vector (make_number (XSTRING (prefix)->size), Qnil);
b91f7a6f 1530 for (i = 0, i_byte = 0; i < XSTRING (prefix)->size;)
67fc16a3 1531 {
f3ba5409 1532 int i_before = i;
54e03a4a
KH
1533
1534 FETCH_STRING_CHAR_ADVANCE (c, prefix, i, i_byte);
1535 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1536 c ^= 0200 | meta_modifier;
f3ba5409 1537 XVECTOR (copy)->contents[i_before] = make_number (c);
67fc16a3
RS
1538 }
1539 prefix = copy;
1540 }
1541 maps = Fcons (Fcons (prefix, tem), Qnil);
1542 }
44a4a59b
RS
1543 else
1544 return Qnil;
1545 }
1546 else
1547 maps = Fcons (Fcons (Fmake_vector (make_number (0), Qnil),
88539837 1548 get_keymap (keymap)),
44a4a59b 1549 Qnil);
2c6f1a39
JB
1550
1551 /* For each map in the list maps,
1552 look at any other maps it points to,
1553 and stick them at the end if they are not already in the list.
1554
1555 This is a breadth-first traversal, where tail is the queue of
1556 nodes, and maps accumulates a list of all nodes visited. */
1557
03699b14 1558 for (tail = maps; CONSP (tail); tail = XCDR (tail))
2c6f1a39 1559 {
e9b6dfb0
KH
1560 register Lisp_Object thisseq, thismap;
1561 Lisp_Object last;
2c6f1a39 1562 /* Does the current sequence end in the meta-prefix-char? */
e9b6dfb0
KH
1563 int is_metized;
1564
1565 thisseq = Fcar (Fcar (tail));
1566 thismap = Fcdr (Fcar (tail));
1567 last = make_number (XINT (Flength (thisseq)) - 1);
1568 is_metized = (XINT (last) >= 0
97ae4b89
RS
1569 /* Don't metize the last char of PREFIX. */
1570 && XINT (last) >= prefixlen
e9b6dfb0 1571 && EQ (Faref (thisseq, last), meta_prefix_char));
2c6f1a39 1572
03699b14 1573 for (; CONSP (thismap); thismap = XCDR (thismap))
2c6f1a39 1574 {
e9b6dfb0
KH
1575 Lisp_Object elt;
1576
03699b14 1577 elt = XCAR (thismap);
2c6f1a39 1578
f5b79c1c
JB
1579 QUIT;
1580
0403641f
RS
1581 if (CHAR_TABLE_P (elt))
1582 {
23cf1efa 1583 Lisp_Object indices[3];
0403641f
RS
1584
1585 map_char_table (accessible_keymaps_char_table, Qnil,
1586 elt, Fcons (maps, Fcons (tail, thisseq)),
1587 0, indices);
1588 }
1589 else if (VECTORP (elt))
2c6f1a39
JB
1590 {
1591 register int i;
1592
1593 /* Vector keymap. Scan all the elements. */
db6f9d95 1594 for (i = 0; i < XVECTOR (elt)->size; i++)
2c6f1a39
JB
1595 {
1596 register Lisp_Object tem;
1597 register Lisp_Object cmd;
1598
224a16e8 1599 cmd = get_keyelt (XVECTOR (elt)->contents[i], 0);
265a9e55 1600 if (NILP (cmd)) continue;
2c6f1a39 1601 tem = Fkeymapp (cmd);
265a9e55 1602 if (!NILP (tem))
2c6f1a39
JB
1603 {
1604 cmd = get_keymap (cmd);
1605 /* Ignore keymaps that are already added to maps. */
1606 tem = Frassq (cmd, maps);
265a9e55 1607 if (NILP (tem))
2c6f1a39
JB
1608 {
1609 /* If the last key in thisseq is meta-prefix-char,
1610 turn it into a meta-ized keystroke. We know
1611 that the event we're about to append is an
f5b79c1c
JB
1612 ascii keystroke since we're processing a
1613 keymap table. */
2c6f1a39
JB
1614 if (is_metized)
1615 {
0b8fc2d4 1616 int meta_bit = meta_modifier;
2c6f1a39 1617 tem = Fcopy_sequence (thisseq);
0b8fc2d4
RS
1618
1619 Faset (tem, last, make_number (i | meta_bit));
2c6f1a39
JB
1620
1621 /* This new sequence is the same length as
1622 thisseq, so stick it in the list right
1623 after this one. */
03699b14
KR
1624 XCDR (tail)
1625 = Fcons (Fcons (tem, cmd), XCDR (tail));
2c6f1a39
JB
1626 }
1627 else
1628 {
1629 tem = append_key (thisseq, make_number (i));
1630 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1631 }
1632 }
1633 }
1634 }
0403641f 1635 }
f5b79c1c 1636 else if (CONSP (elt))
2c6f1a39 1637 {
47935df1 1638 register Lisp_Object cmd, tem;
2c6f1a39 1639
03699b14 1640 cmd = get_keyelt (XCDR (elt), 0);
2c6f1a39
JB
1641 /* Ignore definitions that aren't keymaps themselves. */
1642 tem = Fkeymapp (cmd);
265a9e55 1643 if (!NILP (tem))
2c6f1a39
JB
1644 {
1645 /* Ignore keymaps that have been seen already. */
1646 cmd = get_keymap (cmd);
1647 tem = Frassq (cmd, maps);
265a9e55 1648 if (NILP (tem))
2c6f1a39 1649 {
53c8f9fa 1650 /* Let elt be the event defined by this map entry. */
03699b14 1651 elt = XCAR (elt);
2c6f1a39
JB
1652
1653 /* If the last key in thisseq is meta-prefix-char, and
1654 this entry is a binding for an ascii keystroke,
1655 turn it into a meta-ized keystroke. */
416349ec 1656 if (is_metized && INTEGERP (elt))
2c6f1a39 1657 {
97ae4b89
RS
1658 Lisp_Object element;
1659
1660 element = thisseq;
1661 tem = Fvconcat (1, &element);
2e34157c
RS
1662 XSETFASTINT (XVECTOR (tem)->contents[XINT (last)],
1663 XINT (elt) | meta_modifier);
2c6f1a39
JB
1664
1665 /* This new sequence is the same length as
1666 thisseq, so stick it in the list right
1667 after this one. */
03699b14
KR
1668 XCDR (tail)
1669 = Fcons (Fcons (tem, cmd), XCDR (tail));
2c6f1a39
JB
1670 }
1671 else
1672 nconc2 (tail,
1673 Fcons (Fcons (append_key (thisseq, elt), cmd),
1674 Qnil));
1675 }
1676 }
1677 }
2c6f1a39 1678 }
2c6f1a39
JB
1679 }
1680
53c8f9fa
RS
1681 if (NILP (prefix))
1682 return maps;
1683
1684 /* Now find just the maps whose access prefixes start with PREFIX. */
1685
1686 good_maps = Qnil;
03699b14 1687 for (; CONSP (maps); maps = XCDR (maps))
53c8f9fa
RS
1688 {
1689 Lisp_Object elt, thisseq;
03699b14
KR
1690 elt = XCAR (maps);
1691 thisseq = XCAR (elt);
53c8f9fa
RS
1692 /* The access prefix must be at least as long as PREFIX,
1693 and the first elements must match those of PREFIX. */
1694 if (XINT (Flength (thisseq)) >= prefixlen)
1695 {
1696 int i;
1697 for (i = 0; i < prefixlen; i++)
1698 {
1699 Lisp_Object i1;
6e344130 1700 XSETFASTINT (i1, i);
53c8f9fa
RS
1701 if (!EQ (Faref (thisseq, i1), Faref (prefix, i1)))
1702 break;
1703 }
1704 if (i == prefixlen)
1705 good_maps = Fcons (elt, good_maps);
1706 }
1707 }
1708
1709 return Fnreverse (good_maps);
2c6f1a39
JB
1710}
1711
69248761 1712static void
0403641f
RS
1713accessible_keymaps_char_table (args, index, cmd)
1714 Lisp_Object args, index, cmd;
1715{
1716 Lisp_Object tem;
1717 Lisp_Object maps, tail, thisseq;
1718
1719 if (NILP (cmd))
69248761 1720 return;
0403641f 1721
03699b14
KR
1722 maps = XCAR (args);
1723 tail = XCAR (XCDR (args));
1724 thisseq = XCDR (XCDR (args));
0403641f
RS
1725
1726 tem = Fkeymapp (cmd);
1727 if (!NILP (tem))
1728 {
1729 cmd = get_keymap (cmd);
1730 /* Ignore keymaps that are already added to maps. */
1731 tem = Frassq (cmd, maps);
1732 if (NILP (tem))
1733 {
1734 tem = append_key (thisseq, index);
1735 nconc2 (tail, Fcons (Fcons (tem, cmd), Qnil));
1736 }
1737 }
0403641f
RS
1738}
1739\f
2c6f1a39
JB
1740Lisp_Object Qsingle_key_description, Qkey_description;
1741
21a0d7a0
RS
1742/* This function cannot GC. */
1743
2c6f1a39
JB
1744DEFUN ("key-description", Fkey_description, Skey_description, 1, 1, 0,
1745 "Return a pretty description of key-sequence KEYS.\n\
1746Control characters turn into \"C-foo\" sequences, meta into \"M-foo\"\n\
1747spaces are put between sequence elements, etc.")
1748 (keys)
1749 Lisp_Object keys;
1750{
6bbd7a29 1751 int len = 0;
f3ba5409 1752 int i, i_byte;
4c7d5f13 1753 Lisp_Object sep;
6bbd7a29 1754 Lisp_Object *args = NULL;
4c7d5f13 1755
47684cd9 1756 if (STRINGP (keys))
6ba6e250
RS
1757 {
1758 Lisp_Object vector;
6ba6e250 1759 vector = Fmake_vector (Flength (keys), Qnil);
b91f7a6f 1760 for (i = 0, i_byte = 0; i < XSTRING (keys)->size; )
6ba6e250 1761 {
f3ba5409 1762 int c;
28246d85 1763 int i_before = i;
f3ba5409 1764
54e03a4a
KH
1765 FETCH_STRING_CHAR_ADVANCE (c, keys, i, i_byte);
1766 if (SINGLE_BYTE_CHAR_P (c) && (c & 0200))
1767 c ^= 0200 | meta_modifier;
b91f7a6f 1768 XSETFASTINT (XVECTOR (vector)->contents[i_before], c);
6ba6e250
RS
1769 }
1770 keys = vector;
1771 }
4c7d5f13 1772
5c9c2c3f
RS
1773 if (VECTORP (keys))
1774 {
1775 /* In effect, this computes
1776 (mapconcat 'single-key-description keys " ")
1777 but we shouldn't use mapconcat because it can do GC. */
4c7d5f13 1778
5c9c2c3f
RS
1779 len = XVECTOR (keys)->size;
1780 sep = build_string (" ");
1781 /* This has one extra element at the end that we don't pass to Fconcat. */
1782 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
4c7d5f13 1783
5c9c2c3f
RS
1784 for (i = 0; i < len; i++)
1785 {
c1848a97
GM
1786 args[i * 2] = Fsingle_key_description (XVECTOR (keys)->contents[i],
1787 Qnil);
5c9c2c3f
RS
1788 args[i * 2 + 1] = sep;
1789 }
1790 }
1791 else if (CONSP (keys))
4c7d5f13 1792 {
5c9c2c3f
RS
1793 /* In effect, this computes
1794 (mapconcat 'single-key-description keys " ")
1795 but we shouldn't use mapconcat because it can do GC. */
1796
1797 len = XFASTINT (Flength (keys));
1798 sep = build_string (" ");
1799 /* This has one extra element at the end that we don't pass to Fconcat. */
1800 args = (Lisp_Object *) alloca (len * 2 * sizeof (Lisp_Object));
1801
1802 for (i = 0; i < len; i++)
1803 {
c1848a97 1804 args[i * 2] = Fsingle_key_description (XCAR (keys), Qnil);
5c9c2c3f 1805 args[i * 2 + 1] = sep;
03699b14 1806 keys = XCDR (keys);
5c9c2c3f 1807 }
4c7d5f13 1808 }
5c9c2c3f
RS
1809 else
1810 keys = wrong_type_argument (Qarrayp, keys);
4c7d5f13
RS
1811
1812 return Fconcat (len * 2 - 1, args);
2c6f1a39
JB
1813}
1814
1815char *
1816push_key_description (c, p)
1817 register unsigned int c;
1818 register char *p;
1819{
bc89c609
GM
1820 unsigned c2;
1821
71ac885b
RS
1822 /* Clear all the meaningless bits above the meta bit. */
1823 c &= meta_modifier | ~ - meta_modifier;
bc89c609
GM
1824 c2 = c & ~(alt_modifier | ctrl_modifier | hyper_modifier
1825 | meta_modifier | shift_modifier | super_modifier);
71ac885b 1826
6ba6e250
RS
1827 if (c & alt_modifier)
1828 {
1829 *p++ = 'A';
1830 *p++ = '-';
1831 c -= alt_modifier;
1832 }
bc89c609
GM
1833 if ((c & ctrl_modifier) != 0
1834 || (c2 < ' ' && c2 != 27 && c2 != '\t' && c2 != Ctl ('M')))
6ba6e250
RS
1835 {
1836 *p++ = 'C';
1837 *p++ = '-';
bc89c609 1838 c &= ~ctrl_modifier;
6ba6e250
RS
1839 }
1840 if (c & hyper_modifier)
1841 {
1842 *p++ = 'H';
1843 *p++ = '-';
1844 c -= hyper_modifier;
1845 }
1846 if (c & meta_modifier)
2c6f1a39
JB
1847 {
1848 *p++ = 'M';
1849 *p++ = '-';
6ba6e250
RS
1850 c -= meta_modifier;
1851 }
1852 if (c & shift_modifier)
1853 {
1854 *p++ = 'S';
1855 *p++ = '-';
1856 c -= shift_modifier;
1857 }
1858 if (c & super_modifier)
1859 {
1860 *p++ = 's';
1861 *p++ = '-';
1862 c -= super_modifier;
2c6f1a39
JB
1863 }
1864 if (c < 040)
1865 {
1866 if (c == 033)
1867 {
1868 *p++ = 'E';
1869 *p++ = 'S';
1870 *p++ = 'C';
1871 }
6ba6e250 1872 else if (c == '\t')
2c6f1a39
JB
1873 {
1874 *p++ = 'T';
1875 *p++ = 'A';
1876 *p++ = 'B';
1877 }
b8cab006 1878 else if (c == Ctl ('M'))
2c6f1a39
JB
1879 {
1880 *p++ = 'R';
1881 *p++ = 'E';
1882 *p++ = 'T';
1883 }
1884 else
1885 {
bc89c609 1886 /* `C-' already added above. */
2c6f1a39
JB
1887 if (c > 0 && c <= Ctl ('Z'))
1888 *p++ = c + 0140;
1889 else
1890 *p++ = c + 0100;
1891 }
1892 }
1893 else if (c == 0177)
1894 {
1895 *p++ = 'D';
1896 *p++ = 'E';
1897 *p++ = 'L';
1898 }
1899 else if (c == ' ')
9fb71293 1900 {
2c6f1a39
JB
1901 *p++ = 'S';
1902 *p++ = 'P';
1903 *p++ = 'C';
1904 }
d3c00496
KH
1905 else if (c < 128
1906 || (NILP (current_buffer->enable_multibyte_characters)
1907 && SINGLE_BYTE_CHAR_P (c)))
1df19f02 1908 *p++ = c;
6ba6e250
RS
1909 else
1910 {
9fb71293
KH
1911 if (! NILP (current_buffer->enable_multibyte_characters))
1912 c = unibyte_char_to_multibyte (c);
1913
1914 if (NILP (current_buffer->enable_multibyte_characters)
1915 || SINGLE_BYTE_CHAR_P (c)
1916 || ! char_valid_p (c, 0))
1917 {
1918 int bit_offset;
1919 *p++ = '\\';
1920 /* The biggest character code uses 19 bits. */
1921 for (bit_offset = 18; bit_offset >= 0; bit_offset -= 3)
1922 {
1923 if (c >= (1 << bit_offset))
1924 *p++ = ((c & (7 << bit_offset)) >> bit_offset) + '0';
1925 }
1926 }
1927 else
1928 {
0a16479f 1929 p += CHAR_STRING (c, p);
9fb71293 1930 }
6ba6e250 1931 }
2c6f1a39
JB
1932
1933 return p;
1934}
1935
21a0d7a0
RS
1936/* This function cannot GC. */
1937
c1848a97
GM
1938DEFUN ("single-key-description", Fsingle_key_description,
1939 Ssingle_key_description, 1, 2, 0,
2c6f1a39 1940 "Return a pretty description of command character KEY.\n\
c1848a97
GM
1941Control characters turn into C-whatever, etc.\n\
1942Optional argument NO-ANGLES non-nil means don't put angle brackets\n\
1943around function keys and event symbols.")
1944 (key, no_angles)
1945 Lisp_Object key, no_angles;
2c6f1a39 1946{
5c9c2c3f
RS
1947 if (CONSP (key) && lucid_event_type_list_p (key))
1948 key = Fevent_convert_list (key);
1949
cebd887d 1950 key = EVENT_HEAD (key);
6bbbd9b0 1951
e958fd9a 1952 if (INTEGERP (key)) /* Normal character */
2c6f1a39 1953 {
47a18cef 1954 unsigned int charset, c1, c2;
f4977051 1955 int without_bits = XINT (key) & ~((-1) << CHARACTERBITS);
47a18cef 1956
f4977051 1957 if (SINGLE_BYTE_CHAR_P (without_bits))
47a18cef
RS
1958 charset = 0;
1959 else
54e03a4a 1960 SPLIT_CHAR (without_bits, charset, c1, c2);
47a18cef
RS
1961
1962 if (charset
9fb71293 1963 && CHARSET_DEFINED_P (charset)
47a18cef
RS
1964 && ((c1 >= 0 && c1 < 32)
1965 || (c2 >= 0 && c2 < 32)))
1966 {
1967 /* Handle a generic character. */
1968 Lisp_Object name;
1969 name = CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX);
1970 CHECK_STRING (name, 0);
1971 return concat2 (build_string ("Character set "), name);
1972 }
1973 else
1974 {
d2d9586a 1975 char tem[KEY_DESCRIPTION_SIZE];
47a18cef
RS
1976
1977 *push_key_description (XUINT (key), tem) = 0;
1978 return build_string (tem);
1979 }
2c6f1a39 1980 }
e958fd9a 1981 else if (SYMBOLP (key)) /* Function key or event-symbol */
c7edb960 1982 {
c1848a97
GM
1983 if (NILP (no_angles))
1984 {
1985 char *buffer
1986 = (char *) alloca (STRING_BYTES (XSYMBOL (key)->name) + 5);
1987 sprintf (buffer, "<%s>", XSYMBOL (key)->name->data);
1988 return build_string (buffer);
1989 }
1990 else
1991 return Fsymbol_name (key);
c7edb960 1992 }
e958fd9a
KH
1993 else if (STRINGP (key)) /* Buffer names in the menubar. */
1994 return Fcopy_sequence (key);
1995 else
1996 error ("KEY must be an integer, cons, symbol, or string");
6bbd7a29 1997 return Qnil;
2c6f1a39
JB
1998}
1999
2000char *
2001push_text_char_description (c, p)
2002 register unsigned int c;
2003 register char *p;
2004{
2005 if (c >= 0200)
2006 {
2007 *p++ = 'M';
2008 *p++ = '-';
2009 c -= 0200;
2010 }
2011 if (c < 040)
2012 {
2013 *p++ = '^';
2014 *p++ = c + 64; /* 'A' - 1 */
2015 }
2016 else if (c == 0177)
2017 {
2018 *p++ = '^';
2019 *p++ = '?';
2020 }
2021 else
2022 *p++ = c;
2023 return p;
2024}
2025
21a0d7a0
RS
2026/* This function cannot GC. */
2027
2c6f1a39 2028DEFUN ("text-char-description", Ftext_char_description, Stext_char_description, 1, 1, 0,
88539837 2029 "Return a pretty description of file-character CHARACTER.\n\
2c6f1a39 2030Control characters turn into \"^char\", etc.")
88539837
EN
2031 (character)
2032 Lisp_Object character;
2c6f1a39 2033{
0a16479f
KH
2034 /* Currently MAX_MULTIBYTE_LENGTH is 4 (< 6). */
2035 unsigned char str[6];
2036 int c;
2c6f1a39 2037
88539837 2038 CHECK_NUMBER (character, 0);
2c6f1a39 2039
0a16479f
KH
2040 c = XINT (character);
2041 if (!SINGLE_BYTE_CHAR_P (c))
a98f1d1d 2042 {
0a16479f 2043 int len = CHAR_STRING (c, str);
a98f1d1d 2044
f3ba5409 2045 return make_multibyte_string (str, 1, len);
a98f1d1d
KH
2046 }
2047
0a16479f 2048 *push_text_char_description (c & 0377, str) = 0;
2c6f1a39 2049
0a16479f 2050 return build_string (str);
2c6f1a39 2051}
2fc66973
JB
2052
2053/* Return non-zero if SEQ contains only ASCII characters, perhaps with
2054 a meta bit. */
2055static int
2056ascii_sequence_p (seq)
2057 Lisp_Object seq;
2058{
6e344130 2059 int i;
2fc66973 2060 int len = XINT (Flength (seq));
ffab2bd6 2061
6e344130 2062 for (i = 0; i < len; i++)
2fc66973 2063 {
6e344130 2064 Lisp_Object ii, elt;
ffab2bd6 2065
6e344130
KH
2066 XSETFASTINT (ii, i);
2067 elt = Faref (seq, ii);
2fc66973 2068
416349ec 2069 if (!INTEGERP (elt)
2fc66973
JB
2070 || (XUINT (elt) & ~CHAR_META) >= 0x80)
2071 return 0;
2072 }
2073
2074 return 1;
2075}
2076
2c6f1a39 2077\f
cc0a8174
JB
2078/* where-is - finding a command in a set of keymaps. */
2079
0403641f 2080static Lisp_Object where_is_internal_1 ();
69248761 2081static void where_is_internal_2 ();
0403641f 2082
21a0d7a0
RS
2083/* This function can GC if Flookup_key autoloads any keymaps. */
2084
f0148b5e
RS
2085DEFUN ("where-is-internal", Fwhere_is_internal, Swhere_is_internal, 1, 4, 0,
2086 "Return list of keys that invoke DEFINITION.\n\
2087If KEYMAP is non-nil, search only KEYMAP and the global keymap.\n\
2088If KEYMAP is nil, search all the currently active keymaps.\n\
2c6f1a39 2089\n\
f0148b5e 2090If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,\n\
b8d584f6 2091rather than a list of all possible key sequences.\n\
0bc395d4
RS
2092If FIRSTONLY is the symbol `non-ascii', return the first binding found,\n\
2093no matter what it is.\n\
d7ec5fa2 2094If FIRSTONLY has another non-nil value, prefer sequences of ASCII characters,\n\
0bc395d4 2095and entirely reject menu bindings.\n\
2c6f1a39 2096\n\
f0148b5e 2097If optional 4th arg NOINDIRECT is non-nil, don't follow indirections\n\
2c6f1a39
JB
2098to other keymaps or slots. This makes it possible to search for an\n\
2099indirect definition itself.")
dc1741f3
KR
2100 (definition, xkeymap, firstonly, noindirect)
2101 Lisp_Object definition, xkeymap;
2c6f1a39
JB
2102 Lisp_Object firstonly, noindirect;
2103{
21a0d7a0 2104 Lisp_Object maps;
0403641f 2105 Lisp_Object found, sequences;
60b06e5e 2106 Lisp_Object keymap1;
dc1741f3 2107 int keymap_specified = !NILP (xkeymap);
21a0d7a0 2108 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
0bc395d4
RS
2109 /* 1 means ignore all menu bindings entirely. */
2110 int nomenus = !NILP (firstonly) && !EQ (firstonly, Qnon_ascii);
2c6f1a39 2111
60b06e5e
KH
2112 /* Find keymaps accessible from `keymap' or the current
2113 context. But don't muck with the value of `keymap',
2114 because `where_is_internal_1' uses it to check for
2115 shadowed bindings. */
dc1741f3 2116 keymap1 = xkeymap;
f0148b5e 2117 if (! keymap_specified)
93d2aa1c 2118 keymap1 = get_local_map (PT, current_buffer, keymap);
60b06e5e
KH
2119
2120 if (!NILP (keymap1))
2121 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
f0148b5e
RS
2122 Faccessible_keymaps (get_keymap (current_global_map),
2123 Qnil));
2c6f1a39 2124 else
93d2aa1c 2125 {
dc1741f3 2126 keymap1 = xkeymap;
93d2aa1c
DL
2127 if (! keymap_specified)
2128 keymap1 = get_local_map (PT, current_buffer, local_map);
2129
2130 if (!NILP (keymap1))
2131 maps = nconc2 (Faccessible_keymaps (get_keymap (keymap1), Qnil),
2132 Faccessible_keymaps (get_keymap (current_global_map),
2133 Qnil));
2134 else
2135 maps = Faccessible_keymaps (get_keymap (current_global_map), Qnil);
2136 }
f0148b5e
RS
2137
2138 /* Put the minor mode keymaps on the front. */
2139 if (! keymap_specified)
2140 {
2141 Lisp_Object minors;
2142 minors = Fnreverse (Fcurrent_minor_mode_maps ());
2143 while (!NILP (minors))
2144 {
03699b14 2145 maps = nconc2 (Faccessible_keymaps (get_keymap (XCAR (minors)),
f0148b5e
RS
2146 Qnil),
2147 maps);
03699b14 2148 minors = XCDR (minors);
f0148b5e
RS
2149 }
2150 }
2c6f1a39 2151
dc1741f3 2152 GCPRO5 (definition, xkeymap, maps, found, sequences);
2c6f1a39 2153 found = Qnil;
0403641f 2154 sequences = Qnil;
2c6f1a39 2155
265a9e55 2156 for (; !NILP (maps); maps = Fcdr (maps))
2c6f1a39 2157 {
e9b6dfb0
KH
2158 /* Key sequence to reach map, and the map that it reaches */
2159 register Lisp_Object this, map;
f5b79c1c 2160
2c6f1a39
JB
2161 /* In order to fold [META-PREFIX-CHAR CHAR] sequences into
2162 [M-CHAR] sequences, check if last character of the sequence
2163 is the meta-prefix char. */
e9b6dfb0
KH
2164 Lisp_Object last;
2165 int last_is_meta;
2166
2167 this = Fcar (Fcar (maps));
2168 map = Fcdr (Fcar (maps));
2169 last = make_number (XINT (Flength (this)) - 1);
2170 last_is_meta = (XINT (last) >= 0
2171 && EQ (Faref (this, last), meta_prefix_char));
2c6f1a39 2172
35810b6f
SM
2173 if (nomenus && XINT (last) >= 0)
2174 { /* If no menu entries should be returned, skip over the
2175 keymaps bound to `menu-bar' and `tool-bar'. */
2176 Lisp_Object tem = Faref (this, 0);
2177 if (EQ (tem, Qmenu_bar) || EQ (tem, Qtool_bar))
2178 continue;
2179 }
2180
fde3a52f
JB
2181 QUIT;
2182
f5b79c1c 2183 while (CONSP (map))
2c6f1a39 2184 {
f5b79c1c
JB
2185 /* Because the code we want to run on each binding is rather
2186 large, we don't want to have two separate loop bodies for
2187 sparse keymap bindings and tables; we want to iterate one
2188 loop body over both keymap and vector bindings.
2189
2190 For this reason, if Fcar (map) is a vector, we don't
2191 advance map to the next element until i indicates that we
2192 have finished off the vector. */
21a0d7a0 2193 Lisp_Object elt, key, binding;
03699b14
KR
2194 elt = XCAR (map);
2195 map = XCDR (map);
0403641f
RS
2196
2197 sequences = Qnil;
f5b79c1c 2198
fde3a52f
JB
2199 QUIT;
2200
f5b79c1c
JB
2201 /* Set key and binding to the current key and binding, and
2202 advance map and i to the next binding. */
416349ec 2203 if (VECTORP (elt))
2c6f1a39 2204 {
0403641f
RS
2205 Lisp_Object sequence;
2206 int i;
2c6f1a39 2207 /* In a vector, look at each element. */
0403641f 2208 for (i = 0; i < XVECTOR (elt)->size; i++)
2c6f1a39 2209 {
0403641f
RS
2210 binding = XVECTOR (elt)->contents[i];
2211 XSETFASTINT (key, i);
2212 sequence = where_is_internal_1 (binding, key, definition,
dc1741f3 2213 noindirect, xkeymap, this,
0403641f
RS
2214 last, nomenus, last_is_meta);
2215 if (!NILP (sequence))
2216 sequences = Fcons (sequence, sequences);
2c6f1a39 2217 }
f5b79c1c 2218 }
0403641f 2219 else if (CHAR_TABLE_P (elt))
f5b79c1c 2220 {
23cf1efa 2221 Lisp_Object indices[3];
0403641f 2222 Lisp_Object args;
23cf1efa 2223
0403641f 2224 args = Fcons (Fcons (Fcons (definition, noindirect),
dc1741f3 2225 Fcons (xkeymap, Qnil)),
0403641f
RS
2226 Fcons (Fcons (this, last),
2227 Fcons (make_number (nomenus),
2228 make_number (last_is_meta))));
0403641f
RS
2229 map_char_table (where_is_internal_2, Qnil, elt, args,
2230 0, indices);
03699b14 2231 sequences = XCDR (XCDR (XCAR (args)));
2c6f1a39 2232 }
0403641f 2233 else if (CONSP (elt))
fde3a52f 2234 {
0403641f 2235 Lisp_Object sequence;
2c6f1a39 2236
03699b14
KR
2237 key = XCAR (elt);
2238 binding = XCDR (elt);
2c6f1a39 2239
0403641f 2240 sequence = where_is_internal_1 (binding, key, definition,
dc1741f3 2241 noindirect, xkeymap, this,
0403641f
RS
2242 last, nomenus, last_is_meta);
2243 if (!NILP (sequence))
2244 sequences = Fcons (sequence, sequences);
2c6f1a39 2245 }
2c6f1a39 2246
2c6f1a39 2247
03699b14 2248 for (; ! NILP (sequences); sequences = XCDR (sequences))
2c6f1a39 2249 {
0403641f
RS
2250 Lisp_Object sequence;
2251
03699b14 2252 sequence = XCAR (sequences);
0403641f
RS
2253
2254 /* It is a true unshadowed match. Record it, unless it's already
2255 been seen (as could happen when inheriting keymaps). */
2256 if (NILP (Fmember (sequence, found)))
2257 found = Fcons (sequence, found);
2258
2259 /* If firstonly is Qnon_ascii, then we can return the first
2260 binding we find. If firstonly is not Qnon_ascii but not
2261 nil, then we should return the first ascii-only binding
2262 we find. */
2263 if (EQ (firstonly, Qnon_ascii))
2264 RETURN_UNGCPRO (sequence);
2265 else if (! NILP (firstonly) && ascii_sequence_p (sequence))
2266 RETURN_UNGCPRO (sequence);
2c6f1a39 2267 }
2c6f1a39
JB
2268 }
2269 }
2fc66973 2270
21a0d7a0
RS
2271 UNGCPRO;
2272
2fc66973
JB
2273 found = Fnreverse (found);
2274
2275 /* firstonly may have been t, but we may have gone all the way through
2276 the keymaps without finding an all-ASCII key sequence. So just
2277 return the best we could find. */
2278 if (! NILP (firstonly))
2279 return Fcar (found);
2280
2281 return found;
2c6f1a39 2282}
0403641f
RS
2283
2284/* This is the function that Fwhere_is_internal calls using map_char_table.
2285 ARGS has the form
2286 (((DEFINITION . NOINDIRECT) . (KEYMAP . RESULT))
2287 .
2288 ((THIS . LAST) . (NOMENUS . LAST_IS_META)))
2289 Since map_char_table doesn't really use the return value from this function,
df75b1a3
GM
2290 we the result append to RESULT, the slot in ARGS.
2291
2292 This function can GC because it calls where_is_internal_1 which can
2293 GC. */
0403641f 2294
69248761 2295static void
0403641f
RS
2296where_is_internal_2 (args, key, binding)
2297 Lisp_Object args, key, binding;
2298{
2299 Lisp_Object definition, noindirect, keymap, this, last;
2300 Lisp_Object result, sequence;
2301 int nomenus, last_is_meta;
df75b1a3 2302 struct gcpro gcpro1, gcpro2, gcpro3;
0403641f 2303
df75b1a3 2304 GCPRO3 (args, key, binding);
03699b14
KR
2305 result = XCDR (XCDR (XCAR (args)));
2306 definition = XCAR (XCAR (XCAR (args)));
2307 noindirect = XCDR (XCAR (XCAR (args)));
2308 keymap = XCAR (XCDR (XCAR (args)));
2309 this = XCAR (XCAR (XCDR (args)));
2310 last = XCDR (XCAR (XCDR (args)));
2311 nomenus = XFASTINT (XCAR (XCDR (XCDR (args))));
2312 last_is_meta = XFASTINT (XCDR (XCDR (XCDR (args))));
0403641f
RS
2313
2314 sequence = where_is_internal_1 (binding, key, definition, noindirect, keymap,
2315 this, last, nomenus, last_is_meta);
2316
2317 if (!NILP (sequence))
df75b1a3
GM
2318 XCDR (XCDR (XCAR (args))) = Fcons (sequence, result);
2319
2320 UNGCPRO;
0403641f
RS
2321}
2322
df75b1a3
GM
2323
2324/* This function can GC.because Flookup_key calls get_keymap_1 with
2325 non-zero argument AUTOLOAD. */
2326
0403641f
RS
2327static Lisp_Object
2328where_is_internal_1 (binding, key, definition, noindirect, keymap, this, last,
2329 nomenus, last_is_meta)
2330 Lisp_Object binding, key, definition, noindirect, keymap, this, last;
2331 int nomenus, last_is_meta;
2332{
2333 Lisp_Object sequence;
2334 int keymap_specified = !NILP (keymap);
df75b1a3 2335 struct gcpro gcpro1, gcpro2;
0403641f
RS
2336
2337 /* Search through indirections unless that's not wanted. */
2338 if (NILP (noindirect))
35810b6f 2339 binding = get_keyelt (binding, 0);
0403641f
RS
2340
2341 /* End this iteration if this element does not match
2342 the target. */
2343
2344 if (CONSP (definition))
2345 {
2346 Lisp_Object tem;
2347 tem = Fequal (binding, definition);
2348 if (NILP (tem))
2349 return Qnil;
2350 }
2351 else
2352 if (!EQ (binding, definition))
2353 return Qnil;
2354
2355 /* We have found a match.
2356 Construct the key sequence where we found it. */
2357 if (INTEGERP (key) && last_is_meta)
2358 {
2359 sequence = Fcopy_sequence (this);
2360 Faset (sequence, last, make_number (XINT (key) | meta_modifier));
2361 }
2362 else
2363 sequence = append_key (this, key);
2364
2365 /* Verify that this key binding is not shadowed by another
2366 binding for the same key, before we say it exists.
2367
2368 Mechanism: look for local definition of this key and if
2369 it is defined and does not match what we found then
2370 ignore this key.
2371
2372 Either nil or number as value from Flookup_key
2373 means undefined. */
df75b1a3 2374 GCPRO2 (sequence, binding);
0403641f
RS
2375 if (keymap_specified)
2376 {
2377 binding = Flookup_key (keymap, sequence, Qnil);
2378 if (!NILP (binding) && !INTEGERP (binding))
2379 {
2380 if (CONSP (definition))
2381 {
2382 Lisp_Object tem;
2383 tem = Fequal (binding, definition);
2384 if (NILP (tem))
df75b1a3 2385 RETURN_UNGCPRO (Qnil);
0403641f
RS
2386 }
2387 else
2388 if (!EQ (binding, definition))
df75b1a3 2389 RETURN_UNGCPRO (Qnil);
0403641f
RS
2390 }
2391 }
2392 else
2393 {
2394 binding = Fkey_binding (sequence, Qnil);
2395 if (!EQ (binding, definition))
df75b1a3 2396 RETURN_UNGCPRO (Qnil);
0403641f
RS
2397 }
2398
df75b1a3 2399 RETURN_UNGCPRO (sequence);
0403641f 2400}
2c6f1a39 2401\f
cc0a8174
JB
2402/* describe-bindings - summarizing all the bindings in a set of keymaps. */
2403
6cec169a 2404DEFUN ("describe-bindings-internal", Fdescribe_bindings_internal, Sdescribe_bindings_internal, 0, 2, "",
2c6f1a39 2405 "Show a list of all defined keys, and their definitions.\n\
6cec169a
RS
2406We put that list in a buffer, and display the buffer.\n\
2407\n\
2408The optional argument MENUS, if non-nil, says to mention menu bindings.\n\
2409\(Ordinarily these are omitted from the output.)\n\
2410The optional argument PREFIX, if non-nil, should be a key sequence;\n\
53c8f9fa 2411then we display only bindings that start with that prefix.")
6cec169a
RS
2412 (menus, prefix)
2413 Lisp_Object menus, prefix;
2c6f1a39
JB
2414{
2415 register Lisp_Object thisbuf;
bff4ec1f 2416 XSETBUFFER (thisbuf, current_buffer);
2c6f1a39
JB
2417 internal_with_output_to_temp_buffer ("*Help*",
2418 describe_buffer_bindings,
6cec169a 2419 list3 (thisbuf, prefix, menus));
2c6f1a39
JB
2420 return Qnil;
2421}
2422
6cec169a 2423/* ARG is (BUFFER PREFIX MENU-FLAG). */
53c8f9fa 2424
2c6f1a39 2425static Lisp_Object
53c8f9fa
RS
2426describe_buffer_bindings (arg)
2427 Lisp_Object arg;
2c6f1a39 2428{
53c8f9fa 2429 Lisp_Object descbuf, prefix, shadow;
6cec169a 2430 int nomenu;
d7ab90a9
KH
2431 register Lisp_Object start1;
2432 struct gcpro gcpro1;
2c6f1a39 2433
4726a9f1
JB
2434 char *alternate_heading
2435 = "\
6cec169a
RS
2436Keyboard translations:\n\n\
2437You type Translation\n\
2438-------- -----------\n";
2c6f1a39 2439
03699b14
KR
2440 descbuf = XCAR (arg);
2441 arg = XCDR (arg);
2442 prefix = XCAR (arg);
2443 arg = XCDR (arg);
2444 nomenu = NILP (XCAR (arg));
6cec169a 2445
a588e041 2446 shadow = Qnil;
d7ab90a9 2447 GCPRO1 (shadow);
53c8f9fa 2448
2c6f1a39
JB
2449 Fset_buffer (Vstandard_output);
2450
4726a9f1 2451 /* Report on alternates for keys. */
d7bf9bf5 2452 if (STRINGP (Vkeyboard_translate_table) && !NILP (prefix))
4726a9f1
JB
2453 {
2454 int c;
2455 unsigned char *translate = XSTRING (Vkeyboard_translate_table)->data;
2456 int translate_len = XSTRING (Vkeyboard_translate_table)->size;
2457
2458 for (c = 0; c < translate_len; c++)
2459 if (translate[c] != c)
2460 {
d2d9586a 2461 char buf[KEY_DESCRIPTION_SIZE];
4726a9f1
JB
2462 char *bufend;
2463
2464 if (alternate_heading)
2465 {
2466 insert_string (alternate_heading);
2467 alternate_heading = 0;
2468 }
2469
2470 bufend = push_key_description (translate[c], buf);
2471 insert (buf, bufend - buf);
2472 Findent_to (make_number (16), make_number (1));
2473 bufend = push_key_description (c, buf);
2474 insert (buf, bufend - buf);
2475
2476 insert ("\n", 1);
2477 }
2478
2479 insert ("\n", 1);
2480 }
2481
d7bf9bf5
RS
2482 if (!NILP (Vkey_translation_map))
2483 describe_map_tree (Vkey_translation_map, 0, Qnil, prefix,
6cec169a 2484 "Key translations", nomenu, 1, 0);
d7bf9bf5 2485
cc0a8174
JB
2486 {
2487 int i, nmaps;
2488 Lisp_Object *modes, *maps;
2489
4726a9f1
JB
2490 /* Temporarily switch to descbuf, so that we can get that buffer's
2491 minor modes correctly. */
2492 Fset_buffer (descbuf);
d7bf9bf5 2493
e784236d
KH
2494 if (!NILP (current_kboard->Voverriding_terminal_local_map)
2495 || !NILP (Voverriding_local_map))
7d92e329
RS
2496 nmaps = 0;
2497 else
2498 nmaps = current_minor_maps (&modes, &maps);
4726a9f1
JB
2499 Fset_buffer (Vstandard_output);
2500
53c8f9fa 2501 /* Print the minor mode maps. */
cc0a8174
JB
2502 for (i = 0; i < nmaps; i++)
2503 {
c9b7c53a 2504 /* The title for a minor mode keymap
07f15dfd
RS
2505 is constructed at run time.
2506 We let describe_map_tree do the actual insertion
2507 because it takes care of other features when doing so. */
c9b7c53a 2508 char *title, *p;
07f15dfd 2509
416349ec 2510 if (!SYMBOLP (modes[i]))
d7ab90a9
KH
2511 abort();
2512
97d4edaa
GM
2513 p = title = (char *) alloca (42 + XSYMBOL (modes[i])->name->size);
2514 *p++ = '\f';
2515 *p++ = '\n';
d7ab90a9
KH
2516 *p++ = '`';
2517 bcopy (XSYMBOL (modes[i])->name->data, p,
2518 XSYMBOL (modes[i])->name->size);
2519 p += XSYMBOL (modes[i])->name->size;
2520 *p++ = '\'';
c9b7c53a
KH
2521 bcopy (" Minor Mode Bindings", p, sizeof (" Minor Mode Bindings") - 1);
2522 p += sizeof (" Minor Mode Bindings") - 1;
07f15dfd
RS
2523 *p = 0;
2524
6cec169a 2525 describe_map_tree (maps[i], 1, shadow, prefix, title, nomenu, 0, 0);
53c8f9fa 2526 shadow = Fcons (maps[i], shadow);
cc0a8174
JB
2527 }
2528 }
2529
53c8f9fa 2530 /* Print the (major mode) local map. */
e784236d
KH
2531 if (!NILP (current_kboard->Voverriding_terminal_local_map))
2532 start1 = current_kboard->Voverriding_terminal_local_map;
2533 else if (!NILP (Voverriding_local_map))
7d92e329
RS
2534 start1 = Voverriding_local_map;
2535 else
2536 start1 = XBUFFER (descbuf)->keymap;
2537
265a9e55 2538 if (!NILP (start1))
2c6f1a39 2539 {
91f64ec2 2540 describe_map_tree (start1, 1, shadow, prefix,
97d4edaa 2541 "\f\nMajor Mode Bindings", nomenu, 0, 0);
53c8f9fa 2542 shadow = Fcons (start1, shadow);
2c6f1a39
JB
2543 }
2544
91f64ec2 2545 describe_map_tree (current_global_map, 1, shadow, prefix,
97d4edaa 2546 "\f\nGlobal Bindings", nomenu, 0, 1);
d7bf9bf5
RS
2547
2548 /* Print the function-key-map translations under this prefix. */
2549 if (!NILP (Vfunction_key_map))
2550 describe_map_tree (Vfunction_key_map, 0, Qnil, prefix,
97d4edaa 2551 "\f\nFunction key map translations", nomenu, 1, 0);
2c6f1a39 2552
04befa07 2553 call0 (intern ("help-mode"));
2c6f1a39 2554 Fset_buffer (descbuf);
d7ab90a9 2555 UNGCPRO;
2c6f1a39
JB
2556 return Qnil;
2557}
2558
b31a4218 2559/* Insert a description of the key bindings in STARTMAP,
2c6f1a39
JB
2560 followed by those of all maps reachable through STARTMAP.
2561 If PARTIAL is nonzero, omit certain "uninteresting" commands
2562 (such as `undefined').
53c8f9fa
RS
2563 If SHADOW is non-nil, it is a list of maps;
2564 don't mention keys which would be shadowed by any of them.
2565 PREFIX, if non-nil, says mention only keys that start with PREFIX.
07f15dfd 2566 TITLE, if not 0, is a string to insert at the beginning.
af1d6f09 2567 TITLE should not end with a colon or a newline; we supply that.
d7bf9bf5
RS
2568 If NOMENU is not 0, then omit menu-bar commands.
2569
2570 If TRANSL is nonzero, the definitions are actually key translations
c2b714de
RS
2571 so print strings and vectors differently.
2572
2573 If ALWAYS_TITLE is nonzero, print the title even if there are no maps
2574 to look through. */
2c6f1a39
JB
2575
2576void
c2b714de
RS
2577describe_map_tree (startmap, partial, shadow, prefix, title, nomenu, transl,
2578 always_title)
53c8f9fa 2579 Lisp_Object startmap, shadow, prefix;
2c6f1a39 2580 int partial;
53c8f9fa 2581 char *title;
af1d6f09 2582 int nomenu;
d7bf9bf5 2583 int transl;
c2b714de 2584 int always_title;
2c6f1a39 2585{
e4b6f8e3 2586 Lisp_Object maps, orig_maps, seen, sub_shadows;
e3dfcd4e 2587 struct gcpro gcpro1, gcpro2, gcpro3;
07f15dfd 2588 int something = 0;
53c8f9fa
RS
2589 char *key_heading
2590 = "\
2591key binding\n\
2592--- -------\n";
2c6f1a39 2593
e4b6f8e3 2594 orig_maps = maps = Faccessible_keymaps (startmap, prefix);
925083d1 2595 seen = Qnil;
e3dfcd4e
KH
2596 sub_shadows = Qnil;
2597 GCPRO3 (maps, seen, sub_shadows);
2c6f1a39 2598
af1d6f09
RS
2599 if (nomenu)
2600 {
2601 Lisp_Object list;
2602
2603 /* Delete from MAPS each element that is for the menu bar. */
03699b14 2604 for (list = maps; !NILP (list); list = XCDR (list))
af1d6f09
RS
2605 {
2606 Lisp_Object elt, prefix, tem;
2607
2608 elt = Fcar (list);
2609 prefix = Fcar (elt);
2610 if (XVECTOR (prefix)->size >= 1)
2611 {
2612 tem = Faref (prefix, make_number (0));
2613 if (EQ (tem, Qmenu_bar))
2614 maps = Fdelq (elt, maps);
2615 }
2616 }
2617 }
2618
c2b714de 2619 if (!NILP (maps) || always_title)
53c8f9fa
RS
2620 {
2621 if (title)
07f15dfd
RS
2622 {
2623 insert_string (title);
2624 if (!NILP (prefix))
2625 {
2626 insert_string (" Starting With ");
2627 insert1 (Fkey_description (prefix));
2628 }
2629 insert_string (":\n");
2630 }
53c8f9fa 2631 insert_string (key_heading);
07f15dfd 2632 something = 1;
53c8f9fa
RS
2633 }
2634
265a9e55 2635 for (; !NILP (maps); maps = Fcdr (maps))
2c6f1a39 2636 {
e3dfcd4e 2637 register Lisp_Object elt, prefix, tail;
53c8f9fa 2638
2c6f1a39 2639 elt = Fcar (maps);
53c8f9fa
RS
2640 prefix = Fcar (elt);
2641
2642 sub_shadows = Qnil;
2643
03699b14 2644 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
2c6f1a39 2645 {
53c8f9fa
RS
2646 Lisp_Object shmap;
2647
03699b14 2648 shmap = XCAR (tail);
53c8f9fa
RS
2649
2650 /* If the sequence by which we reach this keymap is zero-length,
2651 then the shadow map for this keymap is just SHADOW. */
416349ec
KH
2652 if ((STRINGP (prefix) && XSTRING (prefix)->size == 0)
2653 || (VECTORP (prefix) && XVECTOR (prefix)->size == 0))
53c8f9fa
RS
2654 ;
2655 /* If the sequence by which we reach this keymap actually has
2656 some elements, then the sequence's definition in SHADOW is
2657 what we should use. */
2658 else
2659 {
98234407 2660 shmap = Flookup_key (shmap, Fcar (elt), Qt);
416349ec 2661 if (INTEGERP (shmap))
53c8f9fa
RS
2662 shmap = Qnil;
2663 }
2664
2665 /* If shmap is not nil and not a keymap,
2666 it completely shadows this map, so don't
2667 describe this map at all. */
2668 if (!NILP (shmap) && NILP (Fkeymapp (shmap)))
2669 goto skip;
2670
2671 if (!NILP (shmap))
2672 sub_shadows = Fcons (shmap, sub_shadows);
2c6f1a39
JB
2673 }
2674
e4b6f8e3
RS
2675 /* Maps we have already listed in this loop shadow this map. */
2676 for (tail = orig_maps; ! EQ (tail, maps); tail = XCDR (tail))
2677 {
2678 Lisp_Object tem;
2679 tem = Fequal (Fcar (XCAR (tail)), prefix);
2680 if (! NILP (tem))
2681 sub_shadows = Fcons (XCDR (XCAR (tail)), sub_shadows);
2682 }
2683
2684 describe_map (Fcdr (elt), prefix,
d7bf9bf5 2685 transl ? describe_translation : describe_command,
279a482a 2686 partial, sub_shadows, &seen, nomenu);
53c8f9fa
RS
2687
2688 skip: ;
2c6f1a39
JB
2689 }
2690
07f15dfd
RS
2691 if (something)
2692 insert_string ("\n");
2693
2c6f1a39
JB
2694 UNGCPRO;
2695}
2696
c3f27064
KH
2697static int previous_description_column;
2698
2c6f1a39
JB
2699static void
2700describe_command (definition)
2701 Lisp_Object definition;
2702{
2703 register Lisp_Object tem1;
c3f27064
KH
2704 int column = current_column ();
2705 int description_column;
2c6f1a39 2706
c3f27064
KH
2707 /* If column 16 is no good, go to col 32;
2708 but don't push beyond that--go to next line instead. */
2709 if (column > 30)
2710 {
2711 insert_char ('\n');
2712 description_column = 32;
2713 }
2714 else if (column > 14 || (column > 10 && previous_description_column == 32))
2715 description_column = 32;
2716 else
2717 description_column = 16;
2718
2719 Findent_to (make_number (description_column), make_number (1));
2720 previous_description_column = description_column;
2c6f1a39 2721
416349ec 2722 if (SYMBOLP (definition))
2c6f1a39 2723 {
bff4ec1f 2724 XSETSTRING (tem1, XSYMBOL (definition)->name);
2c6f1a39 2725 insert1 (tem1);
055234ef 2726 insert_string ("\n");
2c6f1a39 2727 }
d7bf9bf5 2728 else if (STRINGP (definition) || VECTORP (definition))
24065b9c 2729 insert_string ("Keyboard Macro\n");
2c6f1a39
JB
2730 else
2731 {
2732 tem1 = Fkeymapp (definition);
265a9e55 2733 if (!NILP (tem1))
2c6f1a39
JB
2734 insert_string ("Prefix Command\n");
2735 else
2736 insert_string ("??\n");
2737 }
2738}
2739
d7bf9bf5
RS
2740static void
2741describe_translation (definition)
2742 Lisp_Object definition;
2743{
2744 register Lisp_Object tem1;
2745
2746 Findent_to (make_number (16), make_number (1));
2747
2748 if (SYMBOLP (definition))
2749 {
2750 XSETSTRING (tem1, XSYMBOL (definition)->name);
2751 insert1 (tem1);
2752 insert_string ("\n");
2753 }
2754 else if (STRINGP (definition) || VECTORP (definition))
b902ac28
RS
2755 {
2756 insert1 (Fkey_description (definition));
2757 insert_string ("\n");
2758 }
d7bf9bf5
RS
2759 else
2760 {
2761 tem1 = Fkeymapp (definition);
2762 if (!NILP (tem1))
2763 insert_string ("Prefix Command\n");
2764 else
2765 insert_string ("??\n");
2766 }
2767}
2768
53c8f9fa
RS
2769/* Like Flookup_key, but uses a list of keymaps SHADOW instead of a single map.
2770 Returns the first non-nil binding found in any of those maps. */
2771
2772static Lisp_Object
2773shadow_lookup (shadow, key, flag)
2774 Lisp_Object shadow, key, flag;
2775{
2776 Lisp_Object tail, value;
2777
03699b14 2778 for (tail = shadow; CONSP (tail); tail = XCDR (tail))
53c8f9fa 2779 {
03699b14 2780 value = Flookup_key (XCAR (tail), key, flag);
53c8f9fa
RS
2781 if (!NILP (value))
2782 return value;
2783 }
2784 return Qnil;
2785}
2786
c3c0ee93
KH
2787/* Describe the contents of map MAP, assuming that this map itself is
2788 reached by the sequence of prefix keys KEYS (a string or vector).
279a482a 2789 PARTIAL, SHADOW, NOMENU are as in `describe_map_tree' above. */
2c6f1a39
JB
2790
2791static void
279a482a 2792describe_map (map, keys, elt_describer, partial, shadow, seen, nomenu)
c3c0ee93
KH
2793 register Lisp_Object map;
2794 Lisp_Object keys;
6e068770 2795 void (*elt_describer) P_ ((Lisp_Object));
2c6f1a39
JB
2796 int partial;
2797 Lisp_Object shadow;
925083d1 2798 Lisp_Object *seen;
279a482a 2799 int nomenu;
2c6f1a39 2800{
c3c0ee93 2801 Lisp_Object elt_prefix;
53c8f9fa 2802 Lisp_Object tail, definition, event;
99a225a9 2803 Lisp_Object tem;
2c6f1a39
JB
2804 Lisp_Object suppress;
2805 Lisp_Object kludge;
2806 int first = 1;
2807 struct gcpro gcpro1, gcpro2, gcpro3;
2808
6bbd7a29
GM
2809 suppress = Qnil;
2810
c3c0ee93
KH
2811 if (!NILP (keys) && XFASTINT (Flength (keys)) > 0)
2812 {
c3c0ee93
KH
2813 /* Call Fkey_description first, to avoid GC bug for the other string. */
2814 tem = Fkey_description (keys);
2815 elt_prefix = concat2 (tem, build_string (" "));
2816 }
2817 else
2818 elt_prefix = Qnil;
2819
2c6f1a39
JB
2820 if (partial)
2821 suppress = intern ("suppress-keymap");
2822
2823 /* This vector gets used to present single keys to Flookup_key. Since
f5b79c1c 2824 that is done once per keymap element, we don't want to cons up a
2c6f1a39
JB
2825 fresh vector every time. */
2826 kludge = Fmake_vector (make_number (1), Qnil);
99a225a9 2827 definition = Qnil;
2c6f1a39 2828
99a225a9 2829 GCPRO3 (elt_prefix, definition, kludge);
2c6f1a39 2830
03699b14 2831 for (tail = map; CONSP (tail); tail = XCDR (tail))
2c6f1a39
JB
2832 {
2833 QUIT;
2c6f1a39 2834
03699b14
KR
2835 if (VECTORP (XCAR (tail))
2836 || CHAR_TABLE_P (XCAR (tail)))
2837 describe_vector (XCAR (tail),
0403641f
RS
2838 elt_prefix, elt_describer, partial, shadow, map,
2839 (int *)0, 0);
03699b14 2840 else if (CONSP (XCAR (tail)))
2c6f1a39 2841 {
03699b14 2842 event = XCAR (XCAR (tail));
2c3b35b0
RS
2843
2844 /* Ignore bindings whose "keys" are not really valid events.
2845 (We get these in the frames and buffers menu.) */
2846 if (! (SYMBOLP (event) || INTEGERP (event)))
c96dcc01 2847 continue;
2c3b35b0 2848
279a482a
KH
2849 if (nomenu && EQ (event, Qmenu_bar))
2850 continue;
2851
03699b14 2852 definition = get_keyelt (XCDR (XCAR (tail)), 0);
2c6f1a39 2853
f5b79c1c 2854 /* Don't show undefined commands or suppressed commands. */
99a225a9 2855 if (NILP (definition)) continue;
416349ec 2856 if (SYMBOLP (definition) && partial)
f5b79c1c 2857 {
99a225a9
RS
2858 tem = Fget (definition, suppress);
2859 if (!NILP (tem))
f5b79c1c
JB
2860 continue;
2861 }
2c6f1a39 2862
f5b79c1c
JB
2863 /* Don't show a command that isn't really visible
2864 because a local definition of the same key shadows it. */
2c6f1a39 2865
99a225a9 2866 XVECTOR (kludge)->contents[0] = event;
f5b79c1c
JB
2867 if (!NILP (shadow))
2868 {
53c8f9fa 2869 tem = shadow_lookup (shadow, kludge, Qt);
f5b79c1c
JB
2870 if (!NILP (tem)) continue;
2871 }
2872
c3c0ee93 2873 tem = Flookup_key (map, kludge, Qt);
99a225a9
RS
2874 if (! EQ (tem, definition)) continue;
2875
f5b79c1c
JB
2876 if (first)
2877 {
c3f27064 2878 previous_description_column = 0;
f5b79c1c
JB
2879 insert ("\n", 1);
2880 first = 0;
2881 }
2c6f1a39 2882
f5b79c1c
JB
2883 if (!NILP (elt_prefix))
2884 insert1 (elt_prefix);
2c6f1a39 2885
99a225a9 2886 /* THIS gets the string to describe the character EVENT. */
c1848a97 2887 insert1 (Fsingle_key_description (event, Qnil));
2c6f1a39 2888
f5b79c1c
JB
2889 /* Print a description of the definition of this character.
2890 elt_describer will take care of spacing out far enough
2891 for alignment purposes. */
99a225a9 2892 (*elt_describer) (definition);
f5b79c1c 2893 }
03699b14 2894 else if (EQ (XCAR (tail), Qkeymap))
925083d1
KH
2895 {
2896 /* The same keymap might be in the structure twice, if we're
2897 using an inherited keymap. So skip anything we've already
2898 encountered. */
2899 tem = Fassq (tail, *seen);
03699b14 2900 if (CONSP (tem) && !NILP (Fequal (XCAR (tem), keys)))
925083d1
KH
2901 break;
2902 *seen = Fcons (Fcons (tail, keys), *seen);
2903 }
2c6f1a39
JB
2904 }
2905
2906 UNGCPRO;
2907}
2908
69248761 2909static void
2c6f1a39
JB
2910describe_vector_princ (elt)
2911 Lisp_Object elt;
2912{
81fa9e2f 2913 Findent_to (make_number (16), make_number (1));
2c6f1a39 2914 Fprinc (elt, Qnil);
ad4ec84a 2915 Fterpri (Qnil);
2c6f1a39
JB
2916}
2917
2918DEFUN ("describe-vector", Fdescribe_vector, Sdescribe_vector, 1, 1, 0,
ad4ec84a 2919 "Insert a description of contents of VECTOR.\n\
2c6f1a39
JB
2920This is text showing the elements of vector matched against indices.")
2921 (vector)
2922 Lisp_Object vector;
2923{
ad4ec84a
RS
2924 int count = specpdl_ptr - specpdl;
2925
2926 specbind (Qstandard_output, Fcurrent_buffer ());
352e5dea 2927 CHECK_VECTOR_OR_CHAR_TABLE (vector, 0);
0403641f
RS
2928 describe_vector (vector, Qnil, describe_vector_princ, 0,
2929 Qnil, Qnil, (int *)0, 0);
ad4ec84a
RS
2930
2931 return unbind_to (count, Qnil);
2c6f1a39
JB
2932}
2933
352e5dea
RS
2934/* Insert in the current buffer a description of the contents of VECTOR.
2935 We call ELT_DESCRIBER to insert the description of one value found
2936 in VECTOR.
2937
2938 ELT_PREFIX describes what "comes before" the keys or indices defined
0403641f
RS
2939 by this vector. This is a human-readable string whose size
2940 is not necessarily related to the situation.
352e5dea
RS
2941
2942 If the vector is in a keymap, ELT_PREFIX is a prefix key which
2943 leads to this keymap.
2944
2945 If the vector is a chartable, ELT_PREFIX is the vector
2946 of bytes that lead to the character set or portion of a character
2947 set described by this chartable.
2948
2949 If PARTIAL is nonzero, it means do not mention suppressed commands
2950 (that assumes the vector is in a keymap).
2951
2952 SHADOW is a list of keymaps that shadow this map.
2953 If it is non-nil, then we look up the key in those maps
2954 and we don't mention it now if it is defined by any of them.
2955
2956 ENTIRE_MAP is the keymap in which this vector appears.
2957 If the definition in effect in the whole map does not match
0403641f
RS
2958 the one in this vector, we ignore this one.
2959
2960 When describing a sub-char-table, INDICES is a list of
2961 indices at higher levels in this char-table,
2962 and CHAR_TABLE_DEPTH says how many levels down we have gone. */
352e5dea 2963
71a956a6 2964void
32bfcae1 2965describe_vector (vector, elt_prefix, elt_describer,
0403641f
RS
2966 partial, shadow, entire_map,
2967 indices, char_table_depth)
2c6f1a39
JB
2968 register Lisp_Object vector;
2969 Lisp_Object elt_prefix;
69248761 2970 void (*elt_describer) P_ ((Lisp_Object));
2c6f1a39
JB
2971 int partial;
2972 Lisp_Object shadow;
32bfcae1 2973 Lisp_Object entire_map;
0403641f
RS
2974 int *indices;
2975 int char_table_depth;
2c6f1a39 2976{
32bfcae1
KH
2977 Lisp_Object definition;
2978 Lisp_Object tem2;
2c6f1a39
JB
2979 register int i;
2980 Lisp_Object suppress;
2981 Lisp_Object kludge;
2982 int first = 1;
47935df1 2983 struct gcpro gcpro1, gcpro2, gcpro3;
a98f1d1d
KH
2984 /* Range of elements to be handled. */
2985 int from, to;
a98f1d1d
KH
2986 /* A flag to tell if a leaf in this level of char-table is not a
2987 generic character (i.e. a complete multibyte character). */
2988 int complete_char;
0403641f
RS
2989 int character;
2990 int starting_i;
2991
6bbd7a29
GM
2992 suppress = Qnil;
2993
0403641f 2994 if (indices == 0)
2e34157c 2995 indices = (int *) alloca (3 * sizeof (int));
2c6f1a39 2996
32bfcae1 2997 definition = Qnil;
2c6f1a39
JB
2998
2999 /* This vector gets used to present single keys to Flookup_key. Since
3000 that is done once per vector element, we don't want to cons up a
3001 fresh vector every time. */
3002 kludge = Fmake_vector (make_number (1), Qnil);
0403641f 3003 GCPRO3 (elt_prefix, definition, kludge);
2c6f1a39
JB
3004
3005 if (partial)
3006 suppress = intern ("suppress-keymap");
3007
a98f1d1d
KH
3008 if (CHAR_TABLE_P (vector))
3009 {
0403641f 3010 if (char_table_depth == 0)
a98f1d1d 3011 {
a1942d88 3012 /* VECTOR is a top level char-table. */
0403641f 3013 complete_char = 1;
a98f1d1d
KH
3014 from = 0;
3015 to = CHAR_TABLE_ORDINARY_SLOTS;
3016 }
3017 else
3018 {
a1942d88 3019 /* VECTOR is a sub char-table. */
0403641f
RS
3020 if (char_table_depth >= 3)
3021 /* A char-table is never that deep. */
a1942d88 3022 error ("Too deep char table");
a98f1d1d 3023
a98f1d1d 3024 complete_char
0403641f
RS
3025 = (CHARSET_VALID_P (indices[0])
3026 && ((CHARSET_DIMENSION (indices[0]) == 1
3027 && char_table_depth == 1)
3028 || char_table_depth == 2));
a98f1d1d
KH
3029
3030 /* Meaningful elements are from 32th to 127th. */
3031 from = 32;
a1942d88 3032 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
a98f1d1d 3033 }
a98f1d1d
KH
3034 }
3035 else
3036 {
a98f1d1d 3037 /* This does the right thing for ordinary vectors. */
0403641f
RS
3038
3039 complete_char = 1;
3040 from = 0;
3041 to = XVECTOR (vector)->size;
a98f1d1d 3042 }
b5585f5c 3043
a98f1d1d 3044 for (i = from; i < to; i++)
2c6f1a39
JB
3045 {
3046 QUIT;
2c6f1a39 3047
a1942d88
KH
3048 if (CHAR_TABLE_P (vector))
3049 {
0403641f
RS
3050 if (char_table_depth == 0 && i >= CHAR_TABLE_SINGLE_BYTE_SLOTS)
3051 complete_char = 0;
3052
a1942d88
KH
3053 if (i >= CHAR_TABLE_SINGLE_BYTE_SLOTS
3054 && !CHARSET_DEFINED_P (i - 128))
3055 continue;
0403641f
RS
3056
3057 definition
3058 = get_keyelt (XCHAR_TABLE (vector)->contents[i], 0);
a1942d88
KH
3059 }
3060 else
3061 definition = get_keyelt (XVECTOR (vector)->contents[i], 0);
2c6f1a39 3062
cc3e6465
RS
3063 if (NILP (definition)) continue;
3064
2c6f1a39 3065 /* Don't mention suppressed commands. */
32bfcae1 3066 if (SYMBOLP (definition) && partial)
2c6f1a39 3067 {
a98f1d1d
KH
3068 Lisp_Object tem;
3069
3070 tem = Fget (definition, suppress);
3071
3072 if (!NILP (tem)) continue;
2c6f1a39
JB
3073 }
3074
0403641f
RS
3075 /* Set CHARACTER to the character this entry describes, if any.
3076 Also update *INDICES. */
3077 if (CHAR_TABLE_P (vector))
3078 {
3079 indices[char_table_depth] = i;
3080
3081 if (char_table_depth == 0)
3082 {
3083 character = i;
3084 indices[0] = i - 128;
3085 }
3086 else if (complete_char)
3087 {
54e03a4a 3088 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
0403641f
RS
3089 }
3090 else
3091 character = 0;
3092 }
3093 else
3094 character = i;
3095
32bfcae1 3096 /* If this binding is shadowed by some other map, ignore it. */
0403641f 3097 if (!NILP (shadow) && complete_char)
2c6f1a39
JB
3098 {
3099 Lisp_Object tem;
3100
0403641f 3101 XVECTOR (kludge)->contents[0] = make_number (character);
53c8f9fa 3102 tem = shadow_lookup (shadow, kludge, Qt);
2c6f1a39 3103
265a9e55 3104 if (!NILP (tem)) continue;
2c6f1a39
JB
3105 }
3106
32bfcae1
KH
3107 /* Ignore this definition if it is shadowed by an earlier
3108 one in the same keymap. */
0403641f 3109 if (!NILP (entire_map) && complete_char)
32bfcae1
KH
3110 {
3111 Lisp_Object tem;
3112
0403641f 3113 XVECTOR (kludge)->contents[0] = make_number (character);
32bfcae1
KH
3114 tem = Flookup_key (entire_map, kludge, Qt);
3115
3116 if (! EQ (tem, definition))
3117 continue;
3118 }
3119
2c6f1a39
JB
3120 if (first)
3121 {
0403641f 3122 if (char_table_depth == 0)
a98f1d1d 3123 insert ("\n", 1);
2c6f1a39
JB
3124 first = 0;
3125 }
3126
0403641f
RS
3127 /* For a sub char-table, show the depth by indentation.
3128 CHAR_TABLE_DEPTH can be greater than 0 only for a char-table. */
3129 if (char_table_depth > 0)
3130 insert (" ", char_table_depth * 2); /* depth is 1 or 2. */
a98f1d1d 3131
0403641f
RS
3132 /* Output the prefix that applies to every entry in this map. */
3133 if (!NILP (elt_prefix))
3134 insert1 (elt_prefix);
a98f1d1d 3135
0403641f
RS
3136 /* Insert or describe the character this slot is for,
3137 or a description of what it is for. */
3138 if (SUB_CHAR_TABLE_P (vector))
a1942d88 3139 {
0403641f
RS
3140 if (complete_char)
3141 insert_char (character);
3142 else
3143 {
3144 /* We need an octal representation for this block of
3145 characters. */
542d7fd2
RS
3146 char work[16];
3147 sprintf (work, "(row %d)", i);
3148 insert (work, strlen (work));
0403641f
RS
3149 }
3150 }
3151 else if (CHAR_TABLE_P (vector))
3152 {
3153 if (complete_char)
c1848a97 3154 insert1 (Fsingle_key_description (make_number (character), Qnil));
a1942d88
KH
3155 else
3156 {
3157 /* Print the information for this character set. */
3158 insert_string ("<");
3159 tem2 = CHARSET_TABLE_INFO (i - 128, CHARSET_SHORT_NAME_IDX);
3160 if (STRINGP (tem2))
f3ba5409 3161 insert_from_string (tem2, 0, 0, XSTRING (tem2)->size,
fc932ac6 3162 STRING_BYTES (XSTRING (tem2)), 0);
a1942d88
KH
3163 else
3164 insert ("?", 1);
3165 insert (">", 1);
3166 }
3167 }
352e5dea
RS
3168 else
3169 {
c1848a97 3170 insert1 (Fsingle_key_description (make_number (character), Qnil));
a98f1d1d 3171 }
352e5dea 3172
a1942d88 3173 /* If we find a sub char-table within a char-table,
a98f1d1d
KH
3174 scan it recursively; it defines the details for
3175 a character set or a portion of a character set. */
f3ba5409 3176 if (CHAR_TABLE_P (vector) && SUB_CHAR_TABLE_P (definition))
a98f1d1d 3177 {
a98f1d1d 3178 insert ("\n", 1);
0403641f
RS
3179 describe_vector (definition, elt_prefix, elt_describer,
3180 partial, shadow, entire_map,
3181 indices, char_table_depth + 1);
a98f1d1d 3182 continue;
352e5dea 3183 }
2c6f1a39 3184
0403641f
RS
3185 starting_i = i;
3186
542d7fd2 3187 /* Find all consecutive characters or rows that have the same
a1942d88
KH
3188 definition. But, for elements of a top level char table, if
3189 they are for charsets, we had better describe one by one even
3190 if they have the same definition. */
3191 if (CHAR_TABLE_P (vector))
3192 {
0403641f
RS
3193 int limit = to;
3194
3195 if (char_table_depth == 0)
3196 limit = CHAR_TABLE_SINGLE_BYTE_SLOTS;
3197
3198 while (i + 1 < limit
3199 && (tem2 = get_keyelt (XCHAR_TABLE (vector)->contents[i + 1], 0),
3200 !NILP (tem2))
3201 && !NILP (Fequal (tem2, definition)))
3202 i++;
a1942d88
KH
3203 }
3204 else
0403641f 3205 while (i + 1 < to
a1942d88
KH
3206 && (tem2 = get_keyelt (XVECTOR (vector)->contents[i + 1], 0),
3207 !NILP (tem2))
3208 && !NILP (Fequal (tem2, definition)))
3209 i++;
3210
2c6f1a39
JB
3211
3212 /* If we have a range of more than one character,
3213 print where the range reaches to. */
3214
0403641f 3215 if (i != starting_i)
2c6f1a39
JB
3216 {
3217 insert (" .. ", 4);
0403641f
RS
3218
3219 if (!NILP (elt_prefix))
3220 insert1 (elt_prefix);
3221
352e5dea
RS
3222 if (CHAR_TABLE_P (vector))
3223 {
0403641f 3224 if (char_table_depth == 0)
a98f1d1d 3225 {
c1848a97 3226 insert1 (Fsingle_key_description (make_number (i), Qnil));
a98f1d1d 3227 }
0403641f 3228 else if (complete_char)
352e5dea 3229 {
0403641f 3230 indices[char_table_depth] = i;
54e03a4a 3231 character = MAKE_CHAR (indices[0], indices[1], indices[2]);
0403641f 3232 insert_char (character);
352e5dea
RS
3233 }
3234 else
3235 {
542d7fd2
RS
3236 /* We need an octal representation for this block of
3237 characters. */
3238 char work[16];
3239 sprintf (work, "(row %d)", i);
3240 insert (work, strlen (work));
352e5dea
RS
3241 }
3242 }
3243 else
3244 {
c1848a97 3245 insert1 (Fsingle_key_description (make_number (i), Qnil));
352e5dea 3246 }
2c6f1a39
JB
3247 }
3248
3249 /* Print a description of the definition of this character.
3250 elt_describer will take care of spacing out far enough
3251 for alignment purposes. */
32bfcae1 3252 (*elt_describer) (definition);
2c6f1a39
JB
3253 }
3254
a1942d88 3255 /* For (sub) char-table, print `defalt' slot at last. */
a98f1d1d
KH
3256 if (CHAR_TABLE_P (vector) && !NILP (XCHAR_TABLE (vector)->defalt))
3257 {
0403641f 3258 insert (" ", char_table_depth * 2);
a98f1d1d
KH
3259 insert_string ("<<default>>");
3260 (*elt_describer) (XCHAR_TABLE (vector)->defalt);
3261 }
3262
2c6f1a39
JB
3263 UNGCPRO;
3264}
3265\f
cc0a8174 3266/* Apropos - finding all symbols whose names match a regexp. */
2c6f1a39
JB
3267Lisp_Object apropos_predicate;
3268Lisp_Object apropos_accumulate;
3269
3270static void
3271apropos_accum (symbol, string)
3272 Lisp_Object symbol, string;
3273{
3274 register Lisp_Object tem;
3275
3276 tem = Fstring_match (string, Fsymbol_name (symbol), Qnil);
265a9e55 3277 if (!NILP (tem) && !NILP (apropos_predicate))
2c6f1a39 3278 tem = call1 (apropos_predicate, symbol);
265a9e55 3279 if (!NILP (tem))
2c6f1a39
JB
3280 apropos_accumulate = Fcons (symbol, apropos_accumulate);
3281}
3282
3283DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0,
3284 "Show all symbols whose names contain match for REGEXP.\n\
88539837 3285If optional 2nd arg PREDICATE is non-nil, (funcall PREDICATE SYMBOL) is done\n\
2c6f1a39
JB
3286for each symbol and a symbol is mentioned only if that returns non-nil.\n\
3287Return list of symbols found.")
88539837
EN
3288 (regexp, predicate)
3289 Lisp_Object regexp, predicate;
2c6f1a39
JB
3290{
3291 struct gcpro gcpro1, gcpro2;
9cd8b13a 3292 CHECK_STRING (regexp, 0);
88539837 3293 apropos_predicate = predicate;
2c6f1a39
JB
3294 GCPRO2 (apropos_predicate, apropos_accumulate);
3295 apropos_accumulate = Qnil;
88539837 3296 map_obarray (Vobarray, apropos_accum, regexp);
2c6f1a39
JB
3297 apropos_accumulate = Fsort (apropos_accumulate, Qstring_lessp);
3298 UNGCPRO;
3299 return apropos_accumulate;
3300}
3301\f
dfcf069d 3302void
2c6f1a39
JB
3303syms_of_keymap ()
3304{
2c6f1a39
JB
3305 Qkeymap = intern ("keymap");
3306 staticpro (&Qkeymap);
3307
0403641f
RS
3308 /* Now we are ready to set up this property, so we can
3309 create char tables. */
3310 Fput (Qkeymap, Qchar_table_extra_slots, make_number (0));
3311
3312 /* Initialize the keymaps standardly used.
3313 Each one is the value of a Lisp variable, and is also
3314 pointed to by a C variable */
2c6f1a39 3315
0403641f 3316 global_map = Fmake_keymap (Qnil);
2c6f1a39
JB
3317 Fset (intern ("global-map"), global_map);
3318
44bff953 3319 current_global_map = global_map;
a3e99933 3320 staticpro (&global_map);
44bff953
RS
3321 staticpro (&current_global_map);
3322
ce6e5d0b 3323 meta_map = Fmake_keymap (Qnil);
2c6f1a39
JB
3324 Fset (intern ("esc-map"), meta_map);
3325 Ffset (intern ("ESC-prefix"), meta_map);
3326
ce6e5d0b 3327 control_x_map = Fmake_keymap (Qnil);
2c6f1a39
JB
3328 Fset (intern ("ctl-x-map"), control_x_map);
3329 Ffset (intern ("Control-X-prefix"), control_x_map);
3330
107fd03d
RS
3331 DEFVAR_LISP ("define-key-rebound-commands", &Vdefine_key_rebound_commands,
3332 "List of commands given new key bindings recently.\n\
3333This is used for internal purposes during Emacs startup;\n\
3334don't alter it yourself.");
3335 Vdefine_key_rebound_commands = Qt;
3336
2c6f1a39
JB
3337 DEFVAR_LISP ("minibuffer-local-map", &Vminibuffer_local_map,
3338 "Default keymap to use when reading from the minibuffer.");
ce6e5d0b 3339 Vminibuffer_local_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
3340
3341 DEFVAR_LISP ("minibuffer-local-ns-map", &Vminibuffer_local_ns_map,
3342 "Local keymap for the minibuffer when spaces are not allowed.");
ce6e5d0b 3343 Vminibuffer_local_ns_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
3344
3345 DEFVAR_LISP ("minibuffer-local-completion-map", &Vminibuffer_local_completion_map,
3346 "Local keymap for minibuffer input with completion.");
ce6e5d0b 3347 Vminibuffer_local_completion_map = Fmake_sparse_keymap (Qnil);
2c6f1a39
JB
3348
3349 DEFVAR_LISP ("minibuffer-local-must-match-map", &Vminibuffer_local_must_match_map,
3350 "Local keymap for minibuffer input with completion, for exact match.");
ce6e5d0b 3351 Vminibuffer_local_must_match_map = Fmake_sparse_keymap (Qnil);
2c6f1a39 3352
cc0a8174
JB
3353 DEFVAR_LISP ("minor-mode-map-alist", &Vminor_mode_map_alist,
3354 "Alist of keymaps to use for minor modes.\n\
3355Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read\n\
3356key sequences and look up bindings iff VARIABLE's value is non-nil.\n\
3357If two active keymaps bind the same key, the keymap appearing earlier\n\
3358in the list takes precedence.");
3359 Vminor_mode_map_alist = Qnil;
3360
dd9cda06
RS
3361 DEFVAR_LISP ("minor-mode-overriding-map-alist", &Vminor_mode_overriding_map_alist,
3362 "Alist of keymaps to use for minor modes, in current major mode.\n\
3363This variable is a alist just like `minor-mode-map-alist', and it is\n\
3364used the same way (and before `minor-mode-map-alist'); however,\n\
3365it is provided for major modes to bind locally.");
3366 Vminor_mode_overriding_map_alist = Qnil;
3367
6bbbd9b0
JB
3368 DEFVAR_LISP ("function-key-map", &Vfunction_key_map,
3369 "Keymap mapping ASCII function key sequences onto their preferred forms.\n\
3370This allows Emacs to recognize function keys sent from ASCII\n\
3371terminals at any point in a key sequence.\n\
3372\n\
1981e886
RS
3373The `read-key-sequence' function replaces any subsequence bound by\n\
3374`function-key-map' with its binding. More precisely, when the active\n\
6bbbd9b0 3375keymaps have no binding for the current key sequence but\n\
1981e886
RS
3376`function-key-map' binds a suffix of the sequence to a vector or string,\n\
3377`read-key-sequence' replaces the matching suffix with its binding, and\n\
6bbbd9b0
JB
3378continues with the new sequence.\n\
3379\n\
1981e886
RS
3380The events that come from bindings in `function-key-map' are not\n\
3381themselves looked up in `function-key-map'.\n\
3382\n\
3383For example, suppose `function-key-map' binds `ESC O P' to [f1].\n\
3384Typing `ESC O P' to `read-key-sequence' would return [f1]. Typing\n\
718ca51e
JB
3385`C-x ESC O P' would return [?\\C-x f1]. If [f1] were a prefix\n\
3386key, typing `ESC O P x' would return [f1 x].");
ce6e5d0b 3387 Vfunction_key_map = Fmake_sparse_keymap (Qnil);
6bbbd9b0 3388
d7bf9bf5
RS
3389 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
3390 "Keymap of key translations that can override keymaps.\n\
3391This keymap works like `function-key-map', but comes after that,\n\
3392and applies even for keys that have ordinary bindings.");
3393 Vkey_translation_map = Qnil;
3394
2c6f1a39
JB
3395 Qsingle_key_description = intern ("single-key-description");
3396 staticpro (&Qsingle_key_description);
3397
3398 Qkey_description = intern ("key-description");
3399 staticpro (&Qkey_description);
3400
3401 Qkeymapp = intern ("keymapp");
3402 staticpro (&Qkeymapp);
3403
2fc66973
JB
3404 Qnon_ascii = intern ("non-ascii");
3405 staticpro (&Qnon_ascii);
3406
a3fc8840
RS
3407 Qmenu_item = intern ("menu-item");
3408 staticpro (&Qmenu_item);
3409
2c6f1a39 3410 defsubr (&Skeymapp);
7d58ed99
RS
3411 defsubr (&Skeymap_parent);
3412 defsubr (&Sset_keymap_parent);
2c6f1a39
JB
3413 defsubr (&Smake_keymap);
3414 defsubr (&Smake_sparse_keymap);
3415 defsubr (&Scopy_keymap);
3416 defsubr (&Skey_binding);
3417 defsubr (&Slocal_key_binding);
3418 defsubr (&Sglobal_key_binding);
cc0a8174 3419 defsubr (&Sminor_mode_key_binding);
2c6f1a39
JB
3420 defsubr (&Sdefine_key);
3421 defsubr (&Slookup_key);
2c6f1a39
JB
3422 defsubr (&Sdefine_prefix_command);
3423 defsubr (&Suse_global_map);
3424 defsubr (&Suse_local_map);
3425 defsubr (&Scurrent_local_map);
3426 defsubr (&Scurrent_global_map);
cc0a8174 3427 defsubr (&Scurrent_minor_mode_maps);
2c6f1a39
JB
3428 defsubr (&Saccessible_keymaps);
3429 defsubr (&Skey_description);
3430 defsubr (&Sdescribe_vector);
3431 defsubr (&Ssingle_key_description);
3432 defsubr (&Stext_char_description);
3433 defsubr (&Swhere_is_internal);
6cec169a 3434 defsubr (&Sdescribe_bindings_internal);
2c6f1a39
JB
3435 defsubr (&Sapropos_internal);
3436}
3437
dfcf069d 3438void
2c6f1a39
JB
3439keys_of_keymap ()
3440{
2c6f1a39
JB
3441 initial_define_key (global_map, 033, "ESC-prefix");
3442 initial_define_key (global_map, Ctl('X'), "Control-X-prefix");
3443}