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