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