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