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