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