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