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