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