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