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