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