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