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